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 java.io.Serializable;
  26 import java.util.function.Function;
  27 
  28 import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
  29 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp;
  30 import org.graalvm.compiler.core.common.type.IntegerStamp;
  31 import org.graalvm.compiler.core.common.type.PrimitiveStamp;
  32 import org.graalvm.compiler.core.common.type.Stamp;
  33 import org.graalvm.compiler.graph.NodeClass;
  34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  35 import org.graalvm.compiler.nodeinfo.NodeInfo;
  36 import org.graalvm.compiler.nodes.ArithmeticOperation;
  37 import org.graalvm.compiler.nodes.ConstantNode;
  38 import org.graalvm.compiler.nodes.StructuredGraph;
  39 import org.graalvm.compiler.nodes.ValueNode;
  40 import org.graalvm.compiler.nodes.spi.ArithmeticLIRLowerable;
  41 import org.graalvm.compiler.nodes.spi.StampInverter;
  42 
  43 import jdk.vm.ci.meta.Constant;
  44 import jdk.vm.ci.meta.ConstantReflectionProvider;
  45 
  46 /**
  47  * An {@code IntegerConvert} converts an integer to an integer of different width.
  48  */
  49 @NodeInfo
  50 public abstract class IntegerConvertNode<OP, REV> extends UnaryNode implements ArithmeticOperation, ConvertNode, ArithmeticLIRLowerable, StampInverter {
  51     @SuppressWarnings("rawtypes") public static final NodeClass<IntegerConvertNode> TYPE = NodeClass.create(IntegerConvertNode.class);
  52 
  53     protected final SerializableIntegerConvertFunction<OP> getOp;
  54     protected final SerializableIntegerConvertFunction<REV> getReverseOp;
  55 
  56     protected final int inputBits;
  57     protected final int resultBits;
  58 
  59     protected interface SerializableIntegerConvertFunction<T> extends Function<ArithmeticOpTable, IntegerConvertOp<T>>, Serializable {
  60     }
  61 
  62     protected IntegerConvertNode(NodeClass<? extends IntegerConvertNode<OP, REV>> c, SerializableIntegerConvertFunction<OP> getOp, SerializableIntegerConvertFunction<REV> getReverseOp, int inputBits,
  63                     int resultBits, ValueNode input) {
  64         super(c, getOp.apply(ArithmeticOpTable.forStamp(input.stamp())).foldStamp(inputBits, resultBits, input.stamp()), input);
  65         this.getOp = getOp;
  66         this.getReverseOp = getReverseOp;
  67         this.inputBits = inputBits;
  68         this.resultBits = resultBits;
  69         assert ((PrimitiveStamp) input.stamp()).getBits() == inputBits;
  70     }
  71 
  72     public int getInputBits() {
  73         return inputBits;
  74     }
  75 
  76     public int getResultBits() {
  77         return resultBits;
  78     }
  79 
  80     protected final IntegerConvertOp<OP> getOp(ValueNode forValue) {
  81         return getOp.apply(ArithmeticOpTable.forStamp(forValue.stamp()));
  82     }
  83 
  84     @Override
  85     public final IntegerConvertOp<OP> getArithmeticOp() {
  86         return getOp(getValue());
  87     }
  88 
  89     @Override
  90     public Constant convert(Constant c, ConstantReflectionProvider constantReflection) {
  91         return getArithmeticOp().foldConstant(getInputBits(), getResultBits(), c);
  92     }
  93 
  94     @Override
  95     public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) {
  96         IntegerConvertOp<REV> reverse = getReverseOp.apply(ArithmeticOpTable.forStamp(stamp()));
  97         return reverse.foldConstant(getResultBits(), getInputBits(), c);
  98     }
  99 
 100     @Override
 101     public Stamp foldStamp(Stamp newStamp) {
 102         assert newStamp.isCompatible(getValue().stamp());
 103         return getArithmeticOp().foldStamp(inputBits, resultBits, newStamp);
 104     }
 105 
 106     @Override
 107     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
 108         ValueNode synonym = findSynonym(getOp(forValue), forValue, inputBits, resultBits, stamp());
 109         if (synonym != null) {
 110             return synonym;
 111         }
 112         return this;
 113     }
 114 
 115     protected static <T> ValueNode findSynonym(IntegerConvertOp<T> operation, ValueNode value, int inputBits, int resultBits, Stamp stamp) {
 116         if (inputBits == resultBits) {
 117             return value;
 118         } else if (value.isConstant()) {
 119             return ConstantNode.forPrimitive(stamp, operation.foldConstant(inputBits, resultBits, value.asConstant()));
 120         }
 121         return null;
 122     }
 123 
 124     public static ValueNode convert(ValueNode input, Stamp stamp) {
 125         return convert(input, stamp, false);
 126     }
 127 
 128     public static ValueNode convert(ValueNode input, Stamp stamp, StructuredGraph graph) {
 129         ValueNode convert = convert(input, stamp, false);
 130         if (!convert.isAlive()) {
 131             assert !convert.isDeleted();
 132             convert = graph.addOrUniqueWithInputs(convert);
 133         }
 134         return convert;
 135     }
 136 
 137     public static ValueNode convertUnsigned(ValueNode input, Stamp stamp) {
 138         return convert(input, stamp, true);
 139     }
 140 
 141     public static ValueNode convert(ValueNode input, Stamp stamp, boolean zeroExtend) {
 142         IntegerStamp fromStamp = (IntegerStamp) input.stamp();
 143         IntegerStamp toStamp = (IntegerStamp) stamp;
 144 
 145         ValueNode result;
 146         if (toStamp.getBits() == fromStamp.getBits()) {
 147             result = input;
 148         } else if (toStamp.getBits() < fromStamp.getBits()) {
 149             result = new NarrowNode(input, fromStamp.getBits(), toStamp.getBits());
 150         } else if (zeroExtend) {
 151             // toStamp.getBits() > fromStamp.getBits()
 152             result = ZeroExtendNode.create(input, toStamp.getBits());
 153         } else {
 154             // toStamp.getBits() > fromStamp.getBits()
 155             result = SignExtendNode.create(input, toStamp.getBits());
 156         }
 157 
 158         IntegerStamp resultStamp = (IntegerStamp) result.stamp();
 159         assert toStamp.getBits() == resultStamp.getBits();
 160         return result;
 161     }
 162 
 163     @Override
 164     public Stamp invertStamp(Stamp outStamp) {
 165         return getArithmeticOp().invertStamp(inputBits, resultBits, outStamp);
 166     }
 167 }