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.PrimitiveStamp;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  34 import org.graalvm.compiler.lir.gen.ArithmeticLIRGeneratorTool;
  35 import org.graalvm.compiler.nodeinfo.NodeInfo;
  36 import org.graalvm.compiler.nodes.ValueNode;
  37 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  38 
  39 /**
  40  * The {@code NarrowNode} converts an integer to a narrower integer.
  41  */
  42 @NodeInfo(cycles = CYCLES_1)
  43 public final class NarrowNode extends IntegerConvertNode<Narrow, SignExtend> {
  44 
  45     public static final NodeClass<NarrowNode> TYPE = NodeClass.create(NarrowNode.class);
  46 
  47     public NarrowNode(ValueNode input, int resultBits) {
  48         this(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
  49         assert 0 < resultBits && resultBits <= PrimitiveStamp.getBits(input.stamp());
  50     }
  51 
  52     public NarrowNode(ValueNode input, int inputBits, int resultBits) {
  53         super(TYPE, ArithmeticOpTable::getNarrow, ArithmeticOpTable::getSignExtend, inputBits, resultBits, input);
  54     }
  55 
  56     public static ValueNode create(ValueNode input, int resultBits) {
  57         return create(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
  58     }
  59 
  60     public static ValueNode create(ValueNode input, int inputBits, int resultBits) {
  61         IntegerConvertOp<Narrow> signExtend = ArithmeticOpTable.forStamp(input.stamp()).getNarrow();
  62         ValueNode synonym = findSynonym(signExtend, input, inputBits, resultBits, signExtend.foldStamp(inputBits, resultBits, input.stamp()));
  63         if (synonym != null) {
  64             return synonym;
  65         } else {
  66             return new NarrowNode(input, inputBits, resultBits);
  67         }
  68     }
  69 
  70     @Override
  71     public boolean isLossless() {
  72         return false;
  73     }
  74 
  75     @Override
  76     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
  77         ValueNode ret = super.canonical(tool, forValue);
  78         if (ret != this) {
  79             return ret;
  80         }
  81 
  82         if (forValue instanceof NarrowNode) {
  83             // zzzzzzzz yyyyxxxx -(narrow)-> yyyyxxxx -(narrow)-> xxxx
  84             // ==> zzzzzzzz yyyyxxxx -(narrow)-> xxxx
  85             NarrowNode other = (NarrowNode) forValue;
  86             return new NarrowNode(other.getValue(), other.getInputBits(), getResultBits());
  87         } else if (forValue instanceof IntegerConvertNode) {
  88             // SignExtendNode or ZeroExtendNode
  89             IntegerConvertNode<?, ?> other = (IntegerConvertNode<?, ?>) forValue;
  90             if (other.getValue().getUsageCount() == 1 && other.getUsageCount() > 1) {
  91                 // Do not perform if this will introduce a new live value.
  92                 // If the original value's usage count is > 1, there is already another user.
  93                 // If the convert's usage count is <=1, it will be dead code eliminated.
  94                 return this;
  95             }
  96             if (getResultBits() == other.getInputBits()) {
  97                 // xxxx -(extend)-> yyyy xxxx -(narrow)-> xxxx
  98                 // ==> no-op
  99                 return other.getValue();
 100             } else if (getResultBits() < other.getInputBits()) {
 101                 // yyyyxxxx -(extend)-> zzzzzzzz yyyyxxxx -(narrow)-> xxxx
 102                 // ==> yyyyxxxx -(narrow)-> xxxx
 103                 return new NarrowNode(other.getValue(), other.getInputBits(), getResultBits());
 104             } else {
 105                 if (other instanceof SignExtendNode) {
 106                     // sxxx -(sign-extend)-> ssssssss sssssxxx -(narrow)-> sssssxxx
 107                     // ==> sxxx -(sign-extend)-> sssssxxx
 108                     return new SignExtendNode(other.getValue(), other.getInputBits(), getResultBits());
 109                 } else if (other instanceof ZeroExtendNode) {
 110                     // xxxx -(zero-extend)-> 00000000 00000xxx -(narrow)-> 0000xxxx
 111                     // ==> xxxx -(zero-extend)-> 0000xxxx
 112                     return new ZeroExtendNode(other.getValue(), other.getInputBits(), getResultBits());
 113                 }
 114             }
 115         }
 116         return this;
 117     }
 118 
 119     @Override
 120     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
 121         nodeValueMap.setResult(this, gen.emitNarrow(nodeValueMap.operand(getValue()), getResultBits()));
 122     }
 123 }