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 least significant "1" bit. Note that the result is undefined if the
  36  * input is zero.
  37  */
  38 @NodeInfo
  39 public class BitScanForwardNode extends UnaryNode implements LIRLowerable {
  40 
  41     public static BitScanForwardNode create(ValueNode value) {
  42         return new BitScanForwardNode(value);
  43     }
  44 
  45     protected BitScanForwardNode(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 firstAlwaysSetBit = scan(valueStamp.downMask() & mask);
  57         if (firstAlwaysSetBit == -1) {
  58             int lastMaybeSetBit = BitScanReverseNode.scan(valueStamp.upMask() & mask);
  59             int firstMaybeSetBit = scan(valueStamp.upMask() & mask);
  60             min = firstMaybeSetBit;
  61             max = lastMaybeSetBit;
  62         } else {
  63             int firstMaybeSetBit = scan(valueStamp.upMask() & mask);
  64             min = firstMaybeSetBit;
  65             max = firstAlwaysSetBit;
  66         }
  67         return updateStamp(StampFactory.forInteger(Kind.Int, min, max));
  68     }
  69 
  70     @Override
  71     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
  72         if (forValue.isConstant()) {
  73             JavaConstant c = forValue.asJavaConstant();
  74             if (c.asLong() != 0) {
  75                 return ConstantNode.forInt(forValue.getKind() == Kind.Int ? scan(c.asInt()) : scan(c.asLong()));
  76             }
  77         }
  78         return this;
  79     }
  80 
  81     /**
  82      * Utility method with defined return value for 0.
  83      *
  84      * @param v
  85      * @return number of trailing zeros or -1 if {@code v} == 0.
  86      */
  87     public static int scan(long v) {
  88         if (v == 0) {
  89             return -1;
  90         }
  91         return Long.numberOfTrailingZeros(v);
  92     }
  93 
  94     /**
  95      * Utility method with defined return value for 0.
  96      *
  97      * @param v
  98      * @return number of trailing zeros or -1 if {@code v} == 0.
  99      */
 100     public static int scan(int v) {
 101         return scan(0xffffffffL & v);
 102     }
 103 
 104     /**
 105      * Raw intrinsic for bsf instruction.
 106      *
 107      * @param v
 108      * @return number of trailing zeros or an undefined value if {@code v} == 0.
 109      */
 110     @NodeIntrinsic
 111     public static native int unsafeScan(long v);
 112 
 113     /**
 114      * Raw intrinsic for bsf instruction.
 115      *
 116      * @param v
 117      * @return number of trailing zeros or an undefined value if {@code v} == 0.
 118      */
 119     @NodeIntrinsic
 120     public static native int unsafeScan(int v);
 121 
 122     @Override
 123     public void generate(NodeLIRBuilderTool gen) {
 124         Value result = gen.getLIRGeneratorTool().emitBitScanForward(gen.operand(getValue()));
 125         gen.setResult(this, result);
 126     }
 127 }