1 /*
   2  * Copyright (c) 2014, 2018, 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 
  24 
  25 package org.graalvm.compiler.nodes.calc;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
  28 
  29 import org.graalvm.compiler.core.common.type.ArithmeticOpTable;
  30 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp;
  31 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.Narrow;
  32 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.SignExtend;
  33 import org.graalvm.compiler.core.common.type.IntegerStamp;
  34 import org.graalvm.compiler.core.common.type.PrimitiveStamp;
  35 import org.graalvm.compiler.graph.NodeClass;
  36 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  37 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
  38 import org.graalvm.compiler.nodeinfo.NodeInfo;
  39 import org.graalvm.compiler.nodes.NodeView;
  40 import org.graalvm.compiler.nodes.ValueNode;
  41 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  42 
  43 import jdk.vm.ci.code.CodeUtil;
  44 
  45 /**
  46  * The {@code NarrowNode} converts an integer to a narrower integer.
  47  */
  48 @NodeInfo(cycles = CYCLES_1)
  49 public final class NarrowNode extends IntegerConvertNode<Narrow, SignExtend> {
  50 
  51     public static final NodeClass<NarrowNode> TYPE = NodeClass.create(NarrowNode.class);
  52 
  53     public NarrowNode(ValueNode input, int resultBits) {
  54         this(input, PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT)), resultBits);
  55         assert 0 < resultBits && resultBits <= PrimitiveStamp.getBits(input.stamp(NodeView.DEFAULT));
  56     }
  57 
  58     public NarrowNode(ValueNode input, int inputBits, int resultBits) {
  59         super(TYPE, ArithmeticOpTable::getNarrow, ArithmeticOpTable::getSignExtend, inputBits, resultBits, input);
  60     }
  61 
  62     public static ValueNode create(ValueNode input, int resultBits, NodeView view) {
  63         return create(input, PrimitiveStamp.getBits(input.stamp(view)), resultBits, view);
  64     }
  65 
  66     public static ValueNode create(ValueNode input, int inputBits, int resultBits, NodeView view) {
  67         IntegerConvertOp<Narrow> signExtend = ArithmeticOpTable.forStamp(input.stamp(view)).getNarrow();
  68         ValueNode synonym = findSynonym(signExtend, input, inputBits, resultBits, signExtend.foldStamp(inputBits, resultBits, input.stamp(view)));
  69         if (synonym != null) {
  70             return synonym;
  71         } else {
  72             return new NarrowNode(input, inputBits, resultBits);
  73         }
  74     }
  75 
  76     @Override
  77     public boolean isLossless() {
  78         return false;
  79     }
  80 
  81     @Override
  82     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
  83         NodeView view = NodeView.from(tool);
  84         ValueNode ret = super.canonical(tool, forValue);
  85         if (ret != this) {
  86             return ret;
  87         }
  88 
  89         if (forValue instanceof NarrowNode) {
  90             // zzzzzzzz yyyyxxxx -(narrow)-> yyyyxxxx -(narrow)-> xxxx
  91             // ==> zzzzzzzz yyyyxxxx -(narrow)-> xxxx
  92             NarrowNode other = (NarrowNode) forValue;
  93             return new NarrowNode(other.getValue(), other.getInputBits(), getResultBits());
  94         } else if (forValue instanceof IntegerConvertNode) {
  95             // SignExtendNode or ZeroExtendNode
  96             IntegerConvertNode<?, ?> other = (IntegerConvertNode<?, ?>) forValue;
  97             if (other.getValue().hasExactlyOneUsage() && other.hasMoreThanOneUsage()) {
  98                 // Do not perform if this will introduce a new live value.
  99                 // If the original value's usage count is > 1, there is already another user.
 100                 // If the convert's usage count is <=1, it will be dead code eliminated.
 101                 return this;
 102             }
 103             if (getResultBits() == other.getInputBits()) {
 104                 // xxxx -(extend)-> yyyy xxxx -(narrow)-> xxxx
 105                 // ==> no-op
 106                 return other.getValue();
 107             } else if (getResultBits() < other.getInputBits()) {
 108                 // yyyyxxxx -(extend)-> zzzzzzzz yyyyxxxx -(narrow)-> xxxx
 109                 // ==> yyyyxxxx -(narrow)-> xxxx
 110                 return new NarrowNode(other.getValue(), other.getInputBits(), getResultBits());
 111             } else {
 112                 if (other instanceof SignExtendNode) {
 113                     // sxxx -(sign-extend)-> ssssssss sssssxxx -(narrow)-> sssssxxx
 114                     // ==> sxxx -(sign-extend)-> sssssxxx
 115                     return SignExtendNode.create(other.getValue(), other.getInputBits(), getResultBits(), view);
 116                 } else if (other instanceof ZeroExtendNode) {
 117                     // xxxx -(zero-extend)-> 00000000 0000xxxx -(narrow)-> 0000xxxx
 118                     // ==> xxxx -(zero-extend)-> 0000xxxx
 119                     return new ZeroExtendNode(other.getValue(), other.getInputBits(), getResultBits(), ((ZeroExtendNode) other).isInputAlwaysPositive());
 120                 }
 121             }
 122         } else if (forValue instanceof AndNode) {
 123             AndNode andNode = (AndNode) forValue;
 124             IntegerStamp yStamp = (IntegerStamp) andNode.getY().stamp(view);
 125             IntegerStamp xStamp = (IntegerStamp) andNode.getX().stamp(view);
 126             long relevantMask = CodeUtil.mask(this.getResultBits());
 127             if ((relevantMask & yStamp.downMask()) == relevantMask) {
 128                 return create(andNode.getX(), this.getResultBits(), view);
 129             } else if ((relevantMask & xStamp.downMask()) == relevantMask) {
 130                 return create(andNode.getY(), this.getResultBits(), view);
 131             }
 132         }
 133 
 134         return this;
 135     }
 136 
 137     @Override
 138     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
 139         nodeValueMap.setResult(this, gen.emitNarrow(nodeValueMap.operand(getValue()), getResultBits()));
 140     }
 141 
 142     @Override
 143     public boolean mayNullCheckSkipConversion() {
 144         return false;
 145     }
 146 }