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