hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.word/src/org/graalvm/compiler/word/WordCastNode.java
changeset 46459 7d4e637d3f21
equal deleted inserted replaced
46458:3c12af929e7d 46459:7d4e637d3f21
       
     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;
       
    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.lir.ConstantValue;
       
    38 import org.graalvm.compiler.nodeinfo.NodeInfo;
       
    39 import org.graalvm.compiler.nodes.ConstantNode;
       
    40 import org.graalvm.compiler.nodes.FixedWithNextNode;
       
    41 import org.graalvm.compiler.nodes.ValueNode;
       
    42 import org.graalvm.compiler.nodes.spi.LIRLowerable;
       
    43 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
       
    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 Word#fromAddress},
       
    53  * {@link Word#objectToTrackedPointer}, {@link Word#objectToUntrackedPointer} and
       
    54  * {@link Word#toObject()} operations. It has an impact on the pointer maps for the GC, so it must
       
    55  * not be scheduled or 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 wordToObjectNonNull(ValueNode input, JavaKind wordKind) {
       
    71         assert input.getStackKind() == wordKind;
       
    72         return new WordCastNode(StampFactory.objectNonNull(), input);
       
    73     }
       
    74 
       
    75     public static WordCastNode addressToWord(ValueNode input, JavaKind wordKind) {
       
    76         assert input.stamp() instanceof AbstractPointerStamp;
       
    77         return new WordCastNode(StampFactory.forKind(wordKind), input);
       
    78     }
       
    79 
       
    80     public static WordCastNode objectToTrackedPointer(ValueNode input, JavaKind wordKind) {
       
    81         assert input.stamp() instanceof ObjectStamp;
       
    82         return new WordCastNode(StampFactory.forKind(wordKind), input, true);
       
    83     }
       
    84 
       
    85     public static WordCastNode objectToUntrackedPointer(ValueNode input, JavaKind wordKind) {
       
    86         assert input.stamp() instanceof ObjectStamp;
       
    87         return new WordCastNode(StampFactory.forKind(wordKind), input, false);
       
    88     }
       
    89 
       
    90     protected WordCastNode(Stamp stamp, ValueNode input) {
       
    91         this(stamp, input, true);
       
    92     }
       
    93 
       
    94     protected WordCastNode(Stamp stamp, ValueNode input, boolean trackedPointer) {
       
    95         super(TYPE, stamp);
       
    96         this.input = input;
       
    97         this.trackedPointer = trackedPointer;
       
    98     }
       
    99 
       
   100     public ValueNode getInput() {
       
   101         return input;
       
   102     }
       
   103 
       
   104     @Override
       
   105     public Node canonical(CanonicalizerTool tool) {
       
   106         if (tool.allUsagesAvailable() && hasNoUsages()) {
       
   107             /* If the cast is unused, it can be eliminated. */
       
   108             return input;
       
   109         }
       
   110 
       
   111         assert !stamp().isCompatible(input.stamp());
       
   112         if (input.isConstant()) {
       
   113             /* Null pointers are uncritical for GC, so they can be constant folded. */
       
   114             if (input.asJavaConstant().isNull()) {
       
   115                 return ConstantNode.forIntegerStamp(stamp(), 0);
       
   116             } else if (input.asJavaConstant().getJavaKind().isNumericInteger() && input.asJavaConstant().asLong() == 0) {
       
   117                 return ConstantNode.forConstant(stamp(), JavaConstant.NULL_POINTER, tool.getMetaAccess());
       
   118             }
       
   119         }
       
   120 
       
   121         return this;
       
   122     }
       
   123 
       
   124     @Override
       
   125     public void generate(NodeLIRBuilderTool generator) {
       
   126         Value value = generator.operand(input);
       
   127         ValueKind<?> kind = generator.getLIRGeneratorTool().getLIRKind(stamp());
       
   128         assert kind.getPlatformKind().getSizeInBytes() == value.getPlatformKind().getSizeInBytes();
       
   129 
       
   130         if (trackedPointer && LIRKind.isValue(kind) && !LIRKind.isValue(value)) {
       
   131             // just change the PlatformKind, but don't drop reference information
       
   132             kind = value.getValueKind().changeType(kind.getPlatformKind());
       
   133         }
       
   134 
       
   135         if (kind.equals(value.getValueKind()) && !(value instanceof ConstantValue)) {
       
   136             generator.setResult(this, value);
       
   137         } else {
       
   138             AllocatableValue result = generator.getLIRGeneratorTool().newVariable(kind);
       
   139             generator.getLIRGeneratorTool().emitMove(result, value);
       
   140             generator.setResult(this, result);
       
   141         }
       
   142     }
       
   143 }