1 /*
   2  * Copyright (c) 2012, 2018, 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.nodes;
  26 
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
  29 
  30 import jdk.vm.ci.meta.JavaKind;
  31 import jdk.vm.ci.meta.ResolvedJavaMethod;
  32 import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
  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.core.common.type.TypeReference;
  37 import org.graalvm.compiler.graph.IterableNodeType;
  38 import org.graalvm.compiler.graph.Node;
  39 import org.graalvm.compiler.graph.NodeClass;
  40 import org.graalvm.compiler.graph.spi.Canonicalizable;
  41 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  42 import org.graalvm.compiler.nodeinfo.NodeInfo;
  43 import org.graalvm.compiler.nodes.extended.GuardingNode;
  44 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
  45 import org.graalvm.compiler.nodes.memory.ReadNode;
  46 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  47 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  48 import org.graalvm.compiler.nodes.spi.ValueProxy;
  49 import org.graalvm.compiler.nodes.spi.Virtualizable;
  50 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  51 import org.graalvm.compiler.nodes.type.StampTool;
  52 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  53 
  54 //JaCoCo Exclude
  55 
  56 import jdk.vm.ci.meta.ResolvedJavaType;
  57 
  58 /**
  59  * A node that changes the type of its input, usually narrowing it. For example, a {@link PiNode}
  60  * refines the type of a receiver during type-guarded inlining to be the type tested by the guard.
  61  *
  62  * In contrast to a {@link GuardedValueNode}, a {@link PiNode} is useless as soon as the type of its
  63  * input is as narrow or narrower than the {@link PiNode}'s type. The {@link PiNode}, and therefore
  64  * also the scheduling restriction enforced by the guard, will go away.
  65  */
  66 @NodeInfo(cycles = CYCLES_0, size = SIZE_0)
  67 public class PiNode extends FloatingGuardedNode implements LIRLowerable, Virtualizable, IterableNodeType, Canonicalizable, ValueProxy {
  68 
  69     public static final NodeClass<PiNode> TYPE = NodeClass.create(PiNode.class);
  70     @Input ValueNode object;
  71     protected Stamp piStamp;
  72 
  73     public ValueNode object() {
  74         return object;
  75     }
  76 
  77     protected PiNode(NodeClass<? extends PiNode> c, ValueNode object, Stamp stamp, GuardingNode guard) {
  78         super(c, stamp, guard);
  79         this.object = object;
  80         this.piStamp = stamp;
  81         assert piStamp.isCompatible(object.stamp(NodeView.DEFAULT)) : "Object stamp not compatible to piStamp";
  82         inferStamp();
  83     }
  84 
  85     public PiNode(ValueNode object, Stamp stamp) {
  86         this(object, stamp, null);
  87     }
  88 
  89     public PiNode(ValueNode object, Stamp stamp, ValueNode guard) {
  90         this(TYPE, object, stamp, (GuardingNode) guard);
  91     }
  92 
  93     public PiNode(ValueNode object, ValueNode guard) {
  94         this(object, AbstractPointerStamp.pointerNonNull(object.stamp(NodeView.DEFAULT)), guard);
  95     }
  96 
  97     public PiNode(ValueNode object, ResolvedJavaType toType, boolean exactType, boolean nonNull) {
  98         this(object, StampFactory.object(exactType ? TypeReference.createExactTrusted(toType) : TypeReference.createWithoutAssumptions(toType),
  99                         nonNull || StampTool.isPointerNonNull(object.stamp(NodeView.DEFAULT))));
 100     }
 101 
 102     public static ValueNode create(ValueNode object, Stamp stamp) {
 103         ValueNode value = canonical(object, stamp, null);
 104         if (value != null) {
 105             return value;
 106         }
 107         return new PiNode(object, stamp);
 108     }
 109 
 110     public static ValueNode create(ValueNode object, Stamp stamp, ValueNode guard) {
 111         ValueNode value = canonical(object, stamp, (GuardingNode) guard);
 112         if (value != null) {
 113             return value;
 114         }
 115         return new PiNode(object, stamp, guard);
 116     }
 117 
 118     public static ValueNode create(ValueNode object, ValueNode guard) {
 119         Stamp stamp = AbstractPointerStamp.pointerNonNull(object.stamp(NodeView.DEFAULT));
 120         ValueNode value = canonical(object, stamp, (GuardingNode) guard);
 121         if (value != null) {
 122             return value;
 123         }
 124         return new PiNode(object, stamp, guard);
 125     }
 126 
 127     @SuppressWarnings("unused")
 128     public static boolean intrinsify(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode object, ValueNode guard) {
 129         Stamp stamp = AbstractPointerStamp.pointerNonNull(object.stamp(NodeView.DEFAULT));
 130         ValueNode value = canonical(object, stamp, (GuardingNode) guard);
 131         if (value == null) {
 132             value = new PiNode(object, stamp, guard);
 133         }
 134         b.push(JavaKind.Object, b.append(value));
 135         return true;
 136     }
 137 
 138     @SuppressWarnings("unused")
 139     public static boolean intrinsify(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode object, ResolvedJavaType toType, boolean exactType, boolean nonNull) {
 140         Stamp stamp = StampFactory.object(exactType ? TypeReference.createExactTrusted(toType) : TypeReference.createWithoutAssumptions(toType),
 141                         nonNull || StampTool.isPointerNonNull(object.stamp(NodeView.DEFAULT)));
 142         ValueNode value = canonical(object, stamp, null);
 143         if (value == null) {
 144             value = new PiNode(object, stamp);
 145         }
 146         b.push(JavaKind.Object, b.append(value));
 147         return true;
 148     }
 149 
 150     public final Stamp piStamp() {
 151         return piStamp;
 152     }
 153 
 154     public void strengthenPiStamp(Stamp newPiStamp) {
 155         assert this.piStamp.join(newPiStamp).equals(newPiStamp) : "stamp can only improve";
 156         this.piStamp = newPiStamp;
 157     }
 158 
 159     @Override
 160     public void generate(NodeLIRBuilderTool generator) {
 161         if (generator.hasOperand(object)) {
 162             generator.setResult(this, generator.operand(object));
 163         }
 164     }
 165 
 166     @Override
 167     public boolean inferStamp() {
 168         return updateStamp(computeStamp());
 169     }
 170 
 171     private Stamp computeStamp() {
 172         return piStamp.improveWith(object().stamp(NodeView.DEFAULT));
 173     }
 174 
 175     @Override
 176     public void virtualize(VirtualizerTool tool) {
 177         ValueNode alias = tool.getAlias(object());
 178         if (alias instanceof VirtualObjectNode) {
 179             VirtualObjectNode virtual = (VirtualObjectNode) alias;
 180             if (StampTool.typeOrNull(this) != null && StampTool.typeOrNull(this).isAssignableFrom(virtual.type())) {
 181                 tool.replaceWithVirtual(virtual);
 182             }
 183         }
 184     }
 185 
 186     public static ValueNode canonical(ValueNode object, Stamp stamp, GuardingNode guard) {
 187         // Use most up to date stamp.
 188         Stamp computedStamp = stamp.improveWith(object.stamp(NodeView.DEFAULT));
 189 
 190         // The pi node does not give any additional information => skip it.
 191         if (computedStamp.equals(object.stamp(NodeView.DEFAULT))) {
 192             return object;
 193         }
 194 
 195         if (guard == null) {
 196             // Try to merge the pi node with a load node.
 197             if (object instanceof ReadNode && !object.hasMoreThanOneUsage()) {
 198                 ReadNode readNode = (ReadNode) object;
 199                 readNode.setStamp(readNode.stamp(NodeView.DEFAULT).improveWith(stamp));
 200                 return readNode;
 201             }
 202         } else {
 203             for (Node n : guard.asNode().usages()) {
 204                 if (n instanceof PiNode) {
 205                     PiNode otherPi = (PiNode) n;
 206                     if (object == otherPi.object() && computedStamp.equals(otherPi.stamp(NodeView.DEFAULT))) {
 207                         /*
 208                          * Two PiNodes with the same guard and same result, so return the one with
 209                          * the more precise piStamp.
 210                          */
 211                         Stamp newStamp = stamp.join(otherPi.piStamp);
 212                         if (newStamp.equals(otherPi.piStamp)) {
 213                             return otherPi;
 214                         }
 215                     }
 216                 }
 217             }
 218         }
 219         return null;
 220     }
 221 
 222     @Override
 223     public Node canonical(CanonicalizerTool tool) {
 224         Node value = canonical(object(), stamp(NodeView.DEFAULT), getGuard());
 225         if (value != null) {
 226             return value;
 227         }
 228         return this;
 229     }
 230 
 231     @Override
 232     public ValueNode getOriginalNode() {
 233         return object;
 234     }
 235 
 236     public void setOriginalNode(ValueNode newNode) {
 237         this.updateUsages(object, newNode);
 238         this.object = newNode;
 239         assert piStamp.isCompatible(object.stamp(NodeView.DEFAULT)) : "New object stamp not compatible to piStamp";
 240     }
 241 
 242     /**
 243      * Casts an object to have an exact, non-null stamp representing {@link Class}.
 244      */
 245     public static Class<?> asNonNullClass(Object object) {
 246         return asNonNullClassIntrinsic(object, Class.class, true, true);
 247     }
 248 
 249     /**
 250      * Casts an object to have an exact, non-null stamp representing {@link Class}.
 251      */
 252     public static Class<?> asNonNullObject(Object object) {
 253         return asNonNullClassIntrinsic(object, Object.class, false, true);
 254     }
 255 
 256     @NodeIntrinsic(PiNode.class)
 257     private static native Class<?> asNonNullClassIntrinsic(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull);
 258 
 259     /**
 260      * Changes the stamp of an object inside a snippet to be the stamp of the node replaced by the
 261      * snippet.
 262      */
 263     @NodeIntrinsic(PiNode.Placeholder.class)
 264     public static native Object piCastToSnippetReplaceeStamp(Object object);
 265 
 266     /**
 267      * Changes the stamp of an object and ensures the newly stamped value is non-null and does not
 268      * float above a given guard.
 269      */
 270     @NodeIntrinsic
 271     public static native Object piCastNonNull(Object object, GuardingNode guard);
 272 
 273     /**
 274      * Changes the stamp of an object and ensures the newly stamped value is non-null and does not
 275      * float above a given guard.
 276      */
 277     @NodeIntrinsic
 278     public static native Class<?> piCastNonNullClass(Class<?> type, GuardingNode guard);
 279 
 280     /**
 281      * Changes the stamp of an object to represent a given type and to indicate that the object is
 282      * not null.
 283      */
 284     public static Object piCastNonNull(Object object, @ConstantNodeParameter Class<?> toType) {
 285         return piCast(object, toType, false, true);
 286     }
 287 
 288     @NodeIntrinsic
 289     public static native Object piCast(Object object, @ConstantNodeParameter Class<?> toType, @ConstantNodeParameter boolean exactType, @ConstantNodeParameter boolean nonNull);
 290 
 291     /**
 292      * A placeholder node in a snippet that will be replaced with a {@link PiNode} when the snippet
 293      * is instantiated.
 294      */
 295     @NodeInfo(cycles = CYCLES_0, size = SIZE_0)
 296     public static class Placeholder extends FloatingGuardedNode {
 297 
 298         public static final NodeClass<Placeholder> TYPE = NodeClass.create(Placeholder.class);
 299         @Input ValueNode object;
 300 
 301         public ValueNode object() {
 302             return object;
 303         }
 304 
 305         protected Placeholder(NodeClass<? extends Placeholder> c, ValueNode object) {
 306             super(c, PlaceholderStamp.SINGLETON, null);
 307             this.object = object;
 308         }
 309 
 310         public Placeholder(ValueNode object) {
 311             this(TYPE, object);
 312         }
 313 
 314         /**
 315          * Replaces this node with a {@link PiNode} during snippet instantiation.
 316          *
 317          * @param snippetReplaceeStamp the stamp of the node being replace by the snippet
 318          */
 319         public void makeReplacement(Stamp snippetReplaceeStamp) {
 320             ValueNode value = graph().maybeAddOrUnique(PiNode.create(object(), snippetReplaceeStamp, null));
 321             replaceAndDelete(value);
 322         }
 323     }
 324 
 325     /**
 326      * A stamp for {@link Placeholder} nodes which are only used in snippets. It is replaced by an
 327      * actual stamp when the snippet is instantiated.
 328      */
 329     public static final class PlaceholderStamp extends ObjectStamp {
 330         private static final PlaceholderStamp SINGLETON = new PlaceholderStamp();
 331 
 332         public static PlaceholderStamp singleton() {
 333             return SINGLETON;
 334         }
 335 
 336         private PlaceholderStamp() {
 337             super(null, false, false, false);
 338         }
 339 
 340         @Override
 341         public int hashCode() {
 342             return System.identityHashCode(this);
 343         }
 344 
 345         @Override
 346         public boolean equals(Object obj) {
 347             return this == obj;
 348         }
 349 
 350         @Override
 351         public String toString() {
 352             return "PlaceholderStamp";
 353         }
 354     }
 355 }