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