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.hotspot.replacements; 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 * Count the number of leading zeros. 36 */ 37 @NodeInfo 38 public class CountLeadingZerosNode extends UnaryNode implements LIRLowerable { 39 40 public static CountLeadingZerosNode create(ValueNode value) { 41 return new CountLeadingZerosNode(value); 42 } 43 44 protected CountLeadingZerosNode(ValueNode value) { 45 super(StampFactory.forInteger(Kind.Int, 0, ((PrimitiveStamp) value.stamp()).getBits()), value); 46 assert value.getKind() == Kind.Int || value.getKind() == Kind.Long; 47 } 48 49 @Override 50 public boolean inferStamp() { 51 IntegerStamp valueStamp = (IntegerStamp) getValue().stamp(); 52 long mask = CodeUtil.mask(valueStamp.getBits()); 53 int min = Long.numberOfLeadingZeros(valueStamp.upMask() & mask); 54 int max = Long.numberOfLeadingZeros(valueStamp.downMask() & mask); 55 return updateStamp(StampFactory.forInteger(Kind.Int, min, max)); 56 } 57 58 @Override 59 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { 60 if (forValue.isConstant()) { 61 JavaConstant c = forValue.asJavaConstant(); 62 if (forValue.getKind() == Kind.Int) { 63 return ConstantNode.forInt(Integer.numberOfLeadingZeros(c.asInt())); 64 } else { 65 return ConstantNode.forInt(Long.numberOfLeadingZeros(c.asLong())); 66 } 67 } 68 return this; 69 } 70 71 /** 72 * Raw intrinsic for lzcntq instruction. 73 * 74 * @param v 75 * @return number of trailing zeros 76 */ 77 @NodeIntrinsic 78 public static native int count(long v); 79 80 /** 81 * Raw intrinsic for lzcntl instruction. 82 * 83 * @param v 84 * @return number of trailing zeros 85 */ 86 @NodeIntrinsic 87 public static native int count(int v); 88 89 @Override 90 public void generate(NodeLIRBuilderTool gen) { 91 Value result = gen.getLIRGeneratorTool().emitCountLeadingZeros(gen.operand(getValue())); 92 gen.setResult(this, result); 93 } 94 }