1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package com.oracle.graal.replacements.nodes;
  24 
  25 import com.oracle.graal.api.code.*;
  26 import com.oracle.graal.api.meta.*;
  27 import com.oracle.graal.compiler.common.type.*;
  28 import com.oracle.graal.graph.spi.*;
  29 import com.oracle.graal.nodeinfo.*;
  30 import com.oracle.graal.nodes.*;
  31 import com.oracle.graal.nodes.calc.*;
  32 import com.oracle.graal.nodes.spi.*;
  33 
  34 /**
  35  * Determines the index of the most significant "1" bit. Note that the result is undefined if the
  36  * input is zero.
  37  */
  38 @NodeInfo
  39 public class BitScanReverseNode extends UnaryNode implements LIRLowerable {
  40 
  41     public static BitScanReverseNode create(ValueNode value) {
  42         return new BitScanReverseNode(value);
  43     }
  44 
  45     protected BitScanReverseNode(ValueNode value) {
  46         super(StampFactory.forInteger(Kind.Int, 0, ((PrimitiveStamp) value.stamp()).getBits()), value);
  47         assert value.getKind() == Kind.Int || value.getKind() == Kind.Long;
  48     }
  49 
  50     @Override
  51     public boolean inferStamp() {
  52         IntegerStamp valueStamp = (IntegerStamp) getValue().stamp();
  53         int min;
  54         int max;
  55         long mask = CodeUtil.mask(valueStamp.getBits());
  56         int lastAlwaysSetBit = scan(valueStamp.downMask() & mask);
  57         if (lastAlwaysSetBit == -1) {
  58             min = -1;
  59         } else {
  60             min = lastAlwaysSetBit;
  61         }
  62         int lastMaybeSetBit = scan(valueStamp.upMask() & mask);
  63         max = lastMaybeSetBit;
  64         return updateStamp(StampFactory.forInteger(Kind.Int, min, max));
  65     }
  66 
  67     @Override
  68     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
  69         if (forValue.isConstant()) {
  70             JavaConstant c = forValue.asJavaConstant();
  71             if (c.asLong() != 0) {
  72                 return ConstantNode.forInt(forValue.getKind() == Kind.Int ? scan(c.asInt()) : scan(c.asLong()));
  73             }
  74         }
  75         return this;
  76     }
  77 
  78     /**
  79      * Utility method with defined return value for 0.
  80      *
  81      * @param v
  82      * @return index of first set bit or -1 if {@code v} == 0.
  83      */
  84     public static int scan(long v) {
  85         return 63 - Long.numberOfLeadingZeros(v);
  86     }
  87 
  88     /**
  89      * Utility method with defined return value for 0.
  90      *
  91      * @param v
  92      * @return index of first set bit or -1 if {@code v} == 0.
  93      */
  94     public static int scan(int v) {
  95         return 31 - Integer.numberOfLeadingZeros(v);
  96     }
  97 
  98     /**
  99      * Raw intrinsic for bsr instruction.
 100      *
 101      * @param v
 102      * @return index of first set bit or an undefined value if {@code v} == 0.
 103      */
 104     @NodeIntrinsic
 105     public static native int unsafeScan(int v);
 106 
 107     /**
 108      * Raw intrinsic for bsr instruction.
 109      *
 110      * @param v
 111      * @return index of first set bit or an undefined value if {@code v} == 0.
 112      */
 113     @NodeIntrinsic
 114     public static native int unsafeScan(long v);
 115 
 116     @Override
 117     public void generate(NodeLIRBuilderTool gen) {
 118         Value result = gen.getLIRGeneratorTool().emitBitScanReverse(gen.operand(getValue()));
 119         gen.setResult(this, result);
 120     }
 121 
 122 }