1 /*
   2  * Copyright (c) 2013, 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 
  24 
  25 package org.graalvm.compiler.word;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  29 
  30 import org.graalvm.compiler.core.common.LIRKind;
  31 import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
  32 import org.graalvm.compiler.core.common.type.IntegerStamp;
  33 import org.graalvm.compiler.core.common.type.ObjectStamp;
  34 import org.graalvm.compiler.core.common.type.Stamp;
  35 import org.graalvm.compiler.core.common.type.StampFactory;
  36 import org.graalvm.compiler.graph.Node;
  37 import org.graalvm.compiler.graph.NodeClass;
  38 import org.graalvm.compiler.graph.spi.Canonicalizable;
  39 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  40 import org.graalvm.compiler.lir.ConstantValue;
  41 import org.graalvm.compiler.nodeinfo.NodeInfo;
  42 import org.graalvm.compiler.nodes.ConstantNode;
  43 import org.graalvm.compiler.nodes.FixedWithNextNode;
  44 import org.graalvm.compiler.nodes.NodeView;
  45 import org.graalvm.compiler.nodes.ValueNode;
  46 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  47 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  48 import org.graalvm.compiler.nodes.type.NarrowOopStamp;
  49 
  50 import jdk.vm.ci.meta.AllocatableValue;
  51 import jdk.vm.ci.meta.JavaConstant;
  52 import jdk.vm.ci.meta.JavaKind;
  53 import jdk.vm.ci.meta.Value;
  54 import jdk.vm.ci.meta.ValueKind;
  55 
  56 /**
  57  * Casts between Word and Object exposed by the {@link Word#fromAddress},
  58  * {@link Word#objectToTrackedPointer}, {@link Word#objectToUntrackedPointer} and
  59  * {@link Word#toObject()} operations. It has an impact on the pointer maps for the GC, so it must
  60  * not be scheduled or optimized away.
  61  */
  62 @NodeInfo(cycles = CYCLES_1, size = SIZE_1)
  63 public final class WordCastNode extends FixedWithNextNode implements LIRLowerable, Canonicalizable {
  64 
  65     public static final NodeClass<WordCastNode> TYPE = NodeClass.create(WordCastNode.class);
  66 
  67     @Input ValueNode input;
  68     private final boolean trackedPointer;
  69 
  70     public static WordCastNode wordToObject(ValueNode input, JavaKind wordKind) {
  71         assert input.getStackKind() == wordKind;
  72         return new WordCastNode(objectStampFor(input), input);
  73     }
  74 
  75     public static WordCastNode wordToObjectNonNull(ValueNode input, JavaKind wordKind) {
  76         assert input.getStackKind() == wordKind;
  77         return new WordCastNode(StampFactory.objectNonNull(), input);
  78     }
  79 
  80     public static WordCastNode wordToNarrowObject(ValueNode input, NarrowOopStamp stamp) {
  81         return new WordCastNode(stamp, input);
  82     }
  83 
  84     public static WordCastNode addressToWord(ValueNode input, JavaKind wordKind) {
  85         assert input.stamp(NodeView.DEFAULT) instanceof AbstractPointerStamp;
  86         return new WordCastNode(StampFactory.forKind(wordKind), input);
  87     }
  88 
  89     public static WordCastNode objectToTrackedPointer(ValueNode input, JavaKind wordKind) {
  90         assert input.stamp(NodeView.DEFAULT) instanceof ObjectStamp;
  91         return new WordCastNode(StampFactory.forKind(wordKind), input);
  92     }
  93 
  94     public static WordCastNode objectToUntrackedPointer(ValueNode input, JavaKind wordKind) {
  95         assert input.stamp(NodeView.DEFAULT) instanceof ObjectStamp;
  96         return new WordCastNode(StampFactory.forKind(wordKind), input, false);
  97     }
  98 
  99     public static WordCastNode narrowOopToUntrackedWord(ValueNode input, JavaKind wordKind) {
 100         assert input.stamp(NodeView.DEFAULT) instanceof NarrowOopStamp;
 101         return new WordCastNode(StampFactory.forKind(wordKind), input, false);
 102     }
 103 
 104     private WordCastNode(Stamp stamp, ValueNode input) {
 105         this(stamp, input, true);
 106     }
 107 
 108     protected WordCastNode(Stamp stamp, ValueNode input, boolean trackedPointer) {
 109         super(TYPE, stamp);
 110         this.input = input;
 111         this.trackedPointer = trackedPointer;
 112     }
 113 
 114     public ValueNode getInput() {
 115         return input;
 116     }
 117 
 118     private static boolean isZeroConstant(ValueNode value) {
 119         JavaConstant constant = value.asJavaConstant();
 120         return constant.getJavaKind().isNumericInteger() && constant.asLong() == 0;
 121     }
 122 
 123     private static Stamp objectStampFor(ValueNode input) {
 124         Stamp inputStamp = input.stamp(NodeView.DEFAULT);
 125         if (inputStamp instanceof AbstractPointerStamp) {
 126             AbstractPointerStamp pointerStamp = (AbstractPointerStamp) inputStamp;
 127             if (pointerStamp.alwaysNull()) {
 128                 return StampFactory.alwaysNull();
 129             } else if (pointerStamp.nonNull()) {
 130                 return StampFactory.objectNonNull();
 131             }
 132         } else if (inputStamp instanceof IntegerStamp && !((IntegerStamp) inputStamp).contains(0)) {
 133             return StampFactory.objectNonNull();
 134         } else if (input.isConstant() && isZeroConstant(input)) {
 135             return StampFactory.alwaysNull();
 136         }
 137         return StampFactory.object();
 138     }
 139 
 140     @Override
 141     public boolean inferStamp() {
 142         if (stamp instanceof AbstractPointerStamp) {
 143             AbstractPointerStamp objectStamp = (AbstractPointerStamp) stamp;
 144             if (!objectStamp.alwaysNull() && !objectStamp.nonNull()) {
 145                 Stamp newStamp = stamp;
 146                 Stamp inputStamp = input.stamp(NodeView.DEFAULT);
 147                 if (inputStamp instanceof AbstractPointerStamp) {
 148                     AbstractPointerStamp pointerStamp = (AbstractPointerStamp) inputStamp;
 149                     if (pointerStamp.alwaysNull()) {
 150                         newStamp = objectStamp.asAlwaysNull();
 151                     } else if (pointerStamp.nonNull()) {
 152                         newStamp = objectStamp.asNonNull();
 153                     }
 154                 } else if (inputStamp instanceof IntegerStamp && !((IntegerStamp) inputStamp).contains(0)) {
 155                     newStamp = objectStamp.asNonNull();
 156                 } else if (input.isConstant() && isZeroConstant(input)) {
 157                     newStamp = objectStamp.asAlwaysNull();
 158                 }
 159                 return updateStamp(newStamp);
 160             }
 161         }
 162         return false;
 163     }
 164 
 165     @Override
 166     public Node canonical(CanonicalizerTool tool) {
 167         if (tool.allUsagesAvailable() && hasNoUsages()) {
 168             /* If the cast is unused, it can be eliminated. */
 169             return input;
 170         }
 171 
 172         assert !stamp.isCompatible(input.stamp(NodeView.DEFAULT));
 173         if (input.isConstant()) {
 174             /* Null pointers are uncritical for GC, so they can be constant folded. */
 175             if (input.asJavaConstant().isNull()) {
 176                 return ConstantNode.forIntegerStamp(stamp, 0);
 177             } else if (isZeroConstant(input)) {
 178                 return ConstantNode.forConstant(stamp, ((AbstractPointerStamp) stamp).nullConstant(), tool.getMetaAccess());
 179             }
 180         }
 181 
 182         return this;
 183     }
 184 
 185     @Override
 186     public void generate(NodeLIRBuilderTool generator) {
 187         Value value = generator.operand(input);
 188         ValueKind<?> kind = generator.getLIRGeneratorTool().getLIRKind(stamp(NodeView.DEFAULT));
 189         assert kind.getPlatformKind().getSizeInBytes() == value.getPlatformKind().getSizeInBytes();
 190 
 191         if (trackedPointer && LIRKind.isValue(kind) && !LIRKind.isValue(value)) {
 192             // just change the PlatformKind, but don't drop reference information
 193             kind = value.getValueKind().changeType(kind.getPlatformKind());
 194         }
 195 
 196         if (kind.equals(value.getValueKind()) && !(value instanceof ConstantValue)) {
 197             generator.setResult(this, value);
 198         } else {
 199             AllocatableValue result = generator.getLIRGeneratorTool().newVariable(kind);
 200             if (stamp.equals(StampFactory.object())) {
 201                 generator.getLIRGeneratorTool().emitConvertZeroToNull(result, value);
 202             } else if (!trackedPointer && !((AbstractPointerStamp) input.stamp(NodeView.DEFAULT)).nonNull()) {
 203                 generator.getLIRGeneratorTool().emitConvertNullToZero(result, value);
 204             } else {
 205                 generator.getLIRGeneratorTool().emitMove(result, value);
 206             }
 207             generator.setResult(this, result);
 208         }
 209     }
 210 }