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