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.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 org.graalvm.compiler.core.common.type.Stamp;
  29 import org.graalvm.compiler.graph.IterableNodeType;
  30 import org.graalvm.compiler.graph.Node;
  31 import org.graalvm.compiler.graph.NodeClass;
  32 import org.graalvm.compiler.graph.spi.Canonicalizable;
  33 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  34 import org.graalvm.compiler.nodeinfo.NodeInfo;
  35 import org.graalvm.compiler.nodes.extended.GuardingNode;
  36 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  37 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  38 import org.graalvm.compiler.nodes.spi.ValueProxy;
  39 import org.graalvm.compiler.nodes.spi.Virtualizable;
  40 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  41 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  42 
  43 import jdk.vm.ci.meta.JavaKind;
  44 
  45 /**
  46  * A node that changes the type of its input, usually narrowing it. For example, a GuardedValueNode
  47  * is used to keep the nodes depending on guards inside a loop during speculative guard movement.
  48  *
  49  * A GuardedValueNode will only go away if its guard is null or {@link StructuredGraph#start()}.
  50  */
  51 @NodeInfo(cycles = CYCLES_0, size = SIZE_0)
  52 public final class GuardedValueNode extends FloatingGuardedNode implements LIRLowerable, Virtualizable, IterableNodeType, Canonicalizable, ValueProxy {
  53 
  54     public static final NodeClass<GuardedValueNode> TYPE = NodeClass.create(GuardedValueNode.class);
  55     @Input ValueNode object;
  56     protected final Stamp piStamp;
  57 
  58     public GuardedValueNode(ValueNode object, GuardingNode guard, Stamp stamp) {
  59         super(TYPE, computeStamp(stamp, object), guard);
  60         this.object = object;
  61         this.piStamp = stamp;
  62     }
  63 
  64     public GuardedValueNode(ValueNode object, GuardingNode guard) {
  65         this(object, guard, object.stamp());
  66     }
  67 
  68     public ValueNode object() {
  69         return object;
  70     }
  71 
  72     @Override
  73     public void generate(NodeLIRBuilderTool generator) {
  74         if (object.getStackKind() != JavaKind.Void && object.getStackKind() != JavaKind.Illegal) {
  75             generator.setResult(this, generator.operand(object));
  76         }
  77     }
  78 
  79     @Override
  80     public boolean inferStamp() {
  81         return updateStamp(computeStamp(piStamp, object()));
  82     }
  83 
  84     static Stamp computeStamp(Stamp piStamp, ValueNode object) {
  85         return piStamp.improveWith(object.stamp());
  86     }
  87 
  88     @Override
  89     public void virtualize(VirtualizerTool tool) {
  90         ValueNode alias = tool.getAlias(object());
  91         if (alias instanceof VirtualObjectNode) {
  92             tool.replaceWithVirtual((VirtualObjectNode) alias);
  93         }
  94     }
  95 
  96     @Override
  97     public Node canonical(CanonicalizerTool tool) {
  98         if (getGuard() == null) {
  99             if (stamp().equals(object().stamp())) {
 100                 return object();
 101             } else {
 102                 return new PiNode(object(), stamp());
 103             }
 104         }
 105         return this;
 106     }
 107 
 108     @Override
 109     public ValueNode getOriginalNode() {
 110         return object;
 111     }
 112 }