1 /* 2 * Copyright (c) 2014, 2015, 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 org.graalvm.compiler.nodes.calc; 24 25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1; 26 27 import org.graalvm.compiler.core.common.type.ArithmeticOpTable; 28 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp; 29 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.Narrow; 30 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.SignExtend; 31 import org.graalvm.compiler.core.common.type.IntegerStamp; 32 import org.graalvm.compiler.core.common.type.PrimitiveStamp; 33 import org.graalvm.compiler.graph.NodeClass; 34 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 35 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool; 36 import org.graalvm.compiler.nodeinfo.NodeInfo; 37 import org.graalvm.compiler.nodes.ValueNode; 38 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 39 40 /** 41 * The {@code SignExtendNode} converts an integer to a wider integer using sign extension. 42 */ 43 @NodeInfo(cycles = CYCLES_1) 44 public final class SignExtendNode extends IntegerConvertNode<SignExtend, Narrow> { 45 46 public static final NodeClass<SignExtendNode> TYPE = NodeClass.create(SignExtendNode.class); 47 48 public SignExtendNode(ValueNode input, int resultBits) { 49 this(input, PrimitiveStamp.getBits(input.stamp()), resultBits); 50 assert 0 < PrimitiveStamp.getBits(input.stamp()) && PrimitiveStamp.getBits(input.stamp()) <= resultBits; 51 } 52 53 public SignExtendNode(ValueNode input, int inputBits, int resultBits) { 54 super(TYPE, ArithmeticOpTable::getSignExtend, ArithmeticOpTable::getNarrow, inputBits, resultBits, input); 55 } 56 57 public static ValueNode create(ValueNode input, int resultBits) { 58 return create(input, PrimitiveStamp.getBits(input.stamp()), resultBits); 59 } 60 61 public static ValueNode create(ValueNode input, int inputBits, int resultBits) { 62 IntegerConvertOp<SignExtend> signExtend = ArithmeticOpTable.forStamp(input.stamp()).getSignExtend(); 63 ValueNode synonym = findSynonym(signExtend, input, inputBits, resultBits, signExtend.foldStamp(inputBits, resultBits, input.stamp())); 64 if (synonym != null) { 65 return synonym; 66 } else { 67 return new SignExtendNode(input, inputBits, resultBits); 68 } 69 } 70 71 @Override 72 public boolean isLossless() { 73 return true; 74 } 75 76 @Override 77 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { 78 ValueNode ret = super.canonical(tool, forValue); 79 if (ret != this) { 80 return ret; 81 } 82 83 if (forValue instanceof SignExtendNode) { 84 // sxxx -(sign-extend)-> ssss sxxx -(sign-extend)-> ssssssss sssssxxx 85 // ==> sxxx -(sign-extend)-> ssssssss sssssxxx 86 SignExtendNode other = (SignExtendNode) forValue; 87 return new SignExtendNode(other.getValue(), other.getInputBits(), getResultBits()); 88 } else if (forValue instanceof ZeroExtendNode) { 89 ZeroExtendNode other = (ZeroExtendNode) forValue; 90 if (other.getResultBits() > other.getInputBits()) { 91 // sxxx -(zero-extend)-> 0000 sxxx -(sign-extend)-> 00000000 0000sxxx 92 // ==> sxxx -(zero-extend)-> 00000000 0000sxxx 93 return new ZeroExtendNode(other.getValue(), other.getInputBits(), getResultBits()); 94 } 95 } 96 97 if (forValue.stamp() instanceof IntegerStamp) { 98 IntegerStamp inputStamp = (IntegerStamp) forValue.stamp(); 99 if ((inputStamp.upMask() & (1L << (getInputBits() - 1))) == 0L) { 100 // 0xxx -(sign-extend)-> 0000 0xxx 101 // ==> 0xxx -(zero-extend)-> 0000 0xxx 102 return new ZeroExtendNode(forValue, getInputBits(), getResultBits()); 103 } 104 } 105 106 return this; 107 } 108 109 @Override 110 public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) { 111 nodeValueMap.setResult(this, gen.emitSignExtend(nodeValueMap.operand(getValue()), getInputBits(), getResultBits())); 112 } 113 }