src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/calc/IntegerConvertNode.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 52910 583fd71c47d6
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 
    23 
    24 
    24 
    25 package org.graalvm.compiler.nodes.calc;
    25 package org.graalvm.compiler.nodes.calc;
    26 
    26 
    27 import java.io.Serializable;
    27 import static org.graalvm.compiler.nodes.calc.BinaryArithmeticNode.getArithmeticOpTable;
    28 import java.util.function.Function;
       
    29 
    28 
    30 import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
    29 import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
    31 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp;
    30 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp;
    32 import org.graalvm.compiler.core.common.type.IntegerStamp;
    31 import org.graalvm.compiler.core.common.type.IntegerStamp;
    33 import org.graalvm.compiler.core.common.type.PrimitiveStamp;
    32 import org.graalvm.compiler.core.common.type.PrimitiveStamp;
    51  */
    50  */
    52 @NodeInfo
    51 @NodeInfo
    53 public abstract class IntegerConvertNode<OP, REV> extends UnaryNode implements ArithmeticOperation, ConvertNode, ArithmeticLIRLowerable, StampInverter {
    52 public abstract class IntegerConvertNode<OP, REV> extends UnaryNode implements ArithmeticOperation, ConvertNode, ArithmeticLIRLowerable, StampInverter {
    54     @SuppressWarnings("rawtypes") public static final NodeClass<IntegerConvertNode> TYPE = NodeClass.create(IntegerConvertNode.class);
    53     @SuppressWarnings("rawtypes") public static final NodeClass<IntegerConvertNode> TYPE = NodeClass.create(IntegerConvertNode.class);
    55 
    54 
    56     protected final SerializableIntegerConvertFunction<OP> getOp;
       
    57     protected final SerializableIntegerConvertFunction<REV> getReverseOp;
       
    58 
       
    59     protected final int inputBits;
    55     protected final int inputBits;
    60     protected final int resultBits;
    56     protected final int resultBits;
    61 
    57 
    62     protected interface SerializableIntegerConvertFunction<T> extends Function<ArithmeticOpTable, IntegerConvertOp<T>>, Serializable {
    58     protected IntegerConvertNode(NodeClass<? extends IntegerConvertNode<OP, REV>> c, IntegerConvertOp<OP> opForStampComputation, int inputBits, int resultBits, ValueNode input) {
    63     }
    59         super(c, opForStampComputation.foldStamp(inputBits, resultBits, input.stamp(NodeView.DEFAULT)), input);
    64 
       
    65     protected IntegerConvertNode(NodeClass<? extends IntegerConvertNode<OP, REV>> c, SerializableIntegerConvertFunction<OP> getOp, SerializableIntegerConvertFunction<REV> getReverseOp, int inputBits,
       
    66                     int resultBits, ValueNode input) {
       
    67         super(c, getOp.apply(ArithmeticOpTable.forStamp(input.stamp(NodeView.DEFAULT))).foldStamp(inputBits, resultBits, input.stamp(NodeView.DEFAULT)), input);
       
    68         this.getOp = getOp;
       
    69         this.getReverseOp = getReverseOp;
       
    70         this.inputBits = inputBits;
    60         this.inputBits = inputBits;
    71         this.resultBits = resultBits;
    61         this.resultBits = resultBits;
    72         assert ((PrimitiveStamp) input.stamp(NodeView.DEFAULT)).getBits() == inputBits;
    62         assert PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)) == 0 || PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)) == inputBits;
    73     }
    63     }
    74 
    64 
    75     public int getInputBits() {
    65     public int getInputBits() {
    76         return inputBits;
    66         return inputBits;
    77     }
    67     }
    78 
    68 
    79     public int getResultBits() {
    69     public int getResultBits() {
    80         return resultBits;
    70         return resultBits;
    81     }
    71     }
    82 
    72 
    83     protected final IntegerConvertOp<OP> getOp(ValueNode forValue) {
    73     protected abstract IntegerConvertOp<OP> getOp(ArithmeticOpTable table);
    84         return getOp.apply(ArithmeticOpTable.forStamp(forValue.stamp(NodeView.DEFAULT)));
    74 
    85     }
    75     protected abstract IntegerConvertOp<REV> getReverseOp(ArithmeticOpTable table);
    86 
    76 
    87     @Override
    77     @Override
    88     public final IntegerConvertOp<OP> getArithmeticOp() {
    78     public final IntegerConvertOp<OP> getArithmeticOp() {
    89         return getOp(getValue());
    79         return getOp(getArithmeticOpTable(getValue()));
    90     }
    80     }
    91 
    81 
    92     @Override
    82     @Override
    93     public Constant convert(Constant c, ConstantReflectionProvider constantReflection) {
    83     public Constant convert(Constant c, ConstantReflectionProvider constantReflection) {
    94         return getArithmeticOp().foldConstant(getInputBits(), getResultBits(), c);
    84         return getArithmeticOp().foldConstant(getInputBits(), getResultBits(), c);
    95     }
    85     }
    96 
    86 
    97     @Override
    87     @Override
    98     public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) {
    88     public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) {
    99         IntegerConvertOp<REV> reverse = getReverseOp.apply(ArithmeticOpTable.forStamp(stamp(NodeView.DEFAULT)));
    89         IntegerConvertOp<REV> reverse = getReverseOp(ArithmeticOpTable.forStamp(stamp(NodeView.DEFAULT)));
   100         return reverse.foldConstant(getResultBits(), getInputBits(), c);
    90         return reverse.foldConstant(getResultBits(), getInputBits(), c);
   101     }
    91     }
   102 
    92 
   103     @Override
    93     @Override
   104     public Stamp foldStamp(Stamp newStamp) {
    94     public Stamp foldStamp(Stamp newStamp) {
   106         return getArithmeticOp().foldStamp(inputBits, resultBits, newStamp);
    96         return getArithmeticOp().foldStamp(inputBits, resultBits, newStamp);
   107     }
    97     }
   108 
    98 
   109     @Override
    99     @Override
   110     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
   100     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
   111         ValueNode synonym = findSynonym(getOp(forValue), forValue, inputBits, resultBits, stamp(NodeView.DEFAULT));
   101         ValueNode synonym = findSynonym(getOp(getArithmeticOpTable(forValue)), forValue, inputBits, resultBits, stamp(NodeView.DEFAULT));
   112         if (synonym != null) {
   102         if (synonym != null) {
   113             return synonym;
   103             return synonym;
   114         }
   104         }
   115         return this;
   105         return this;
   116     }
   106     }