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.calc.Condition;
  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.ArithmeticOpTable.IntegerConvertOp.Narrow;
  31 import org.graalvm.compiler.core.common.type.ArithmeticOpTable.IntegerConvertOp.ZeroExtend;
  32 import org.graalvm.compiler.core.common.type.IntegerStamp;
  33 import org.graalvm.compiler.core.common.type.PrimitiveStamp;
  34 import org.graalvm.compiler.core.common.type.Stamp;
  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.ValueNode;
  40 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  41 
  42 import jdk.vm.ci.code.CodeUtil;
  43 
  44 /**
  45  * The {@code ZeroExtendNode} converts an integer to a wider integer using zero extension.
  46  */
  47 @NodeInfo(cycles = CYCLES_1)
  48 public final class ZeroExtendNode extends IntegerConvertNode<ZeroExtend, Narrow> {
  49 
  50     public static final NodeClass<ZeroExtendNode> TYPE = NodeClass.create(ZeroExtendNode.class);
  51 
  52     public ZeroExtendNode(ValueNode input, int resultBits) {
  53         this(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
  54         assert 0 < PrimitiveStamp.getBits(input.stamp()) && PrimitiveStamp.getBits(input.stamp()) <= resultBits;
  55     }
  56 
  57     public ZeroExtendNode(ValueNode input, int inputBits, int resultBits) {
  58         super(TYPE, ArithmeticOpTable::getZeroExtend, ArithmeticOpTable::getNarrow, inputBits, resultBits, input);
  59     }
  60 
  61     public static ValueNode create(ValueNode input, int resultBits) {
  62         return create(input, PrimitiveStamp.getBits(input.stamp()), resultBits);
  63     }
  64 
  65     public static ValueNode create(ValueNode input, int inputBits, int resultBits) {
  66         IntegerConvertOp<ZeroExtend> signExtend = ArithmeticOpTable.forStamp(input.stamp()).getZeroExtend();
  67         ValueNode synonym = findSynonym(signExtend, input, inputBits, resultBits, signExtend.foldStamp(inputBits, resultBits, input.stamp()));
  68         if (synonym != null) {
  69             return synonym;
  70         } else {
  71             return new ZeroExtendNode(input, inputBits, resultBits);
  72         }
  73     }
  74 
  75     @Override
  76     public boolean isLossless() {
  77         return true;
  78     }
  79 
  80     @Override
  81     public boolean preservesOrder(Condition cond) {
  82         switch (cond) {
  83             case GE:
  84             case GT:
  85             case LE:
  86             case LT:
  87                 return false;
  88             default:
  89                 return true;
  90         }
  91     }
  92 
  93     @Override
  94     public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
  95         ValueNode ret = super.canonical(tool, forValue);
  96         if (ret != this) {
  97             return ret;
  98         }
  99 
 100         if (forValue instanceof ZeroExtendNode) {
 101             // xxxx -(zero-extend)-> 0000 xxxx -(zero-extend)-> 00000000 0000xxxx
 102             // ==> xxxx -(zero-extend)-> 00000000 0000xxxx
 103             ZeroExtendNode other = (ZeroExtendNode) forValue;
 104             return new ZeroExtendNode(other.getValue(), other.getInputBits(), getResultBits());
 105         }
 106         if (forValue instanceof NarrowNode) {
 107             NarrowNode narrow = (NarrowNode) forValue;
 108             Stamp inputStamp = narrow.getValue().stamp();
 109             if (inputStamp instanceof IntegerStamp && inputStamp.isCompatible(stamp())) {
 110                 IntegerStamp istamp = (IntegerStamp) inputStamp;
 111                 long mask = CodeUtil.mask(PrimitiveStamp.getBits(narrow.stamp()));
 112                 if (((istamp.upMask() | istamp.downMask()) & ~mask) == 0) {
 113                     // The original value is in the range of the masked zero extended result so
 114                     // simply return the original input.
 115                     return narrow.getValue();
 116                 }
 117             }
 118         }
 119 
 120         return this;
 121     }
 122 
 123     @Override
 124     public void generate(NodeLIRBuilderTool nodeValueMap, ArithmeticLIRGeneratorTool gen) {
 125         nodeValueMap.setResult(this, gen.emitZeroExtend(nodeValueMap.operand(getValue()), getInputBits(), getResultBits()));
 126     }
 127 }