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