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