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 package org.graalvm.compiler.word.nodes;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  27 
  28 import org.graalvm.compiler.core.common.LIRKind;
  29 import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
  30 import org.graalvm.compiler.core.common.type.ObjectStamp;
  31 import org.graalvm.compiler.core.common.type.Stamp;
  32 import org.graalvm.compiler.core.common.type.StampFactory;
  33 import org.graalvm.compiler.graph.Node;
  34 import org.graalvm.compiler.graph.NodeClass;
  35 import org.graalvm.compiler.graph.spi.Canonicalizable;
  36 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  37 import org.graalvm.compiler.nodeinfo.NodeInfo;
  38 import org.graalvm.compiler.nodes.ConstantNode;
  39 import org.graalvm.compiler.nodes.FixedWithNextNode;
  40 import org.graalvm.compiler.nodes.ValueNode;
  41 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  42 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  43 import org.graalvm.compiler.word.Word.Opcode;
  44 
  45 import jdk.vm.ci.meta.AllocatableValue;
  46 import jdk.vm.ci.meta.JavaConstant;
  47 import jdk.vm.ci.meta.JavaKind;
  48 import jdk.vm.ci.meta.Value;
  49 import jdk.vm.ci.meta.ValueKind;
  50 
  51 /**
  52  * Casts between Word and Object exposed by the {@link Opcode#FROM_ADDRESS},
  53  * {@link Opcode#OBJECT_TO_TRACKED}, {@link Opcode#OBJECT_TO_UNTRACKED} and {@link Opcode#TO_OBJECT}
  54  * operations. It has an impact on the pointer maps for the GC, so it must not be scheduled or
  55  * optimized away.
  56  */
  57 @NodeInfo(cycles = CYCLES_1, size = SIZE_1)
  58 public final class WordCastNode extends FixedWithNextNode implements LIRLowerable, Canonicalizable {
  59 
  60     public static final NodeClass<WordCastNode> TYPE = NodeClass.create(WordCastNode.class);
  61 
  62     @Input ValueNode input;
  63     public final boolean trackedPointer;
  64 
  65     public static WordCastNode wordToObject(ValueNode input, JavaKind wordKind) {
  66         assert input.getStackKind() == wordKind;
  67         return new WordCastNode(StampFactory.object(), input);
  68     }
  69 
  70     public static WordCastNode addressToWord(ValueNode input, JavaKind wordKind) {
  71         assert input.stamp() instanceof AbstractPointerStamp;
  72         return new WordCastNode(StampFactory.forKind(wordKind), input);
  73     }
  74 
  75     public static WordCastNode objectToTrackedPointer(ValueNode input, JavaKind wordKind) {
  76         assert input.stamp() instanceof ObjectStamp;
  77         return new WordCastNode(StampFactory.forKind(wordKind), input, true);
  78     }
  79 
  80     public static WordCastNode objectToUntrackedPointer(ValueNode input, JavaKind wordKind) {
  81         assert input.stamp() instanceof ObjectStamp;
  82         return new WordCastNode(StampFactory.forKind(wordKind), input, false);
  83     }
  84 
  85     protected WordCastNode(Stamp stamp, ValueNode input) {
  86         this(stamp, input, true);
  87     }
  88 
  89     protected WordCastNode(Stamp stamp, ValueNode input, boolean trackedPointer) {
  90         super(TYPE, stamp);
  91         this.input = input;
  92         this.trackedPointer = trackedPointer;
  93     }
  94 
  95     public ValueNode getInput() {
  96         return input;
  97     }
  98 
  99     @Override
 100     public Node canonical(CanonicalizerTool tool) {
 101         if (tool.allUsagesAvailable() && hasNoUsages()) {
 102             /* If the cast is unused, it can be eliminated. */
 103             return input;
 104         }
 105 
 106         assert !stamp().isCompatible(input.stamp());
 107         if (input.isConstant()) {
 108             /* Null pointers are uncritical for GC, so they can be constant folded. */
 109             if (input.asJavaConstant().isNull()) {
 110                 return ConstantNode.forIntegerStamp(stamp(), 0);
 111             } else if (input.asJavaConstant().getJavaKind().isNumericInteger() && input.asJavaConstant().asLong() == 0) {
 112                 return ConstantNode.forConstant(stamp(), JavaConstant.NULL_POINTER, tool.getMetaAccess());
 113             }
 114         }
 115 
 116         return this;
 117     }
 118 
 119     @Override
 120     public void generate(NodeLIRBuilderTool generator) {
 121         Value value = generator.operand(input);
 122         ValueKind<?> kind = generator.getLIRGeneratorTool().getLIRKind(stamp());
 123         assert kind.getPlatformKind().getSizeInBytes() == value.getPlatformKind().getSizeInBytes();
 124 
 125         if (trackedPointer && LIRKind.isValue(kind) && !LIRKind.isValue(value)) {
 126             // just change the PlatformKind, but don't drop reference information
 127             kind = value.getValueKind().changeType(kind.getPlatformKind());
 128         }
 129 
 130         if (kind.equals(value.getValueKind())) {
 131             generator.setResult(this, value);
 132         } else {
 133             AllocatableValue result = generator.getLIRGeneratorTool().newVariable(kind);
 134             generator.getLIRGeneratorTool().emitMove(result, value);
 135             generator.setResult(this, result);
 136         }
 137     }
 138 }