1 /*
   2  * Copyright (c) 2011, 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.extended;
  24 
  25 import static org.graalvm.compiler.nodeinfo.InputType.Anchor;
  26 import static org.graalvm.compiler.nodeinfo.InputType.Guard;
  27 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
  28 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
  29 
  30 import org.graalvm.compiler.core.common.type.StampFactory;
  31 import org.graalvm.compiler.graph.NodeClass;
  32 import org.graalvm.compiler.graph.spi.Simplifiable;
  33 import org.graalvm.compiler.graph.spi.SimplifierTool;
  34 import org.graalvm.compiler.nodeinfo.NodeInfo;
  35 import org.graalvm.compiler.nodes.AbstractBeginNode;
  36 import org.graalvm.compiler.nodes.FixedNode;
  37 import org.graalvm.compiler.nodes.FixedWithNextNode;
  38 import org.graalvm.compiler.nodes.GuardNode;
  39 import org.graalvm.compiler.nodes.ValueNode;
  40 import org.graalvm.compiler.nodes.calc.IsNullNode;
  41 import org.graalvm.compiler.nodes.memory.FixedAccessNode;
  42 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  43 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  44 import org.graalvm.compiler.nodes.spi.Virtualizable;
  45 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  46 import org.graalvm.compiler.nodes.util.GraphUtil;
  47 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  48 
  49 /**
  50  * The ValueAnchor instruction keeps non-CFG (floating) nodes above a certain point in the graph.
  51  */
  52 @NodeInfo(allowedUsageTypes = {Anchor, Guard}, cycles = CYCLES_0, size = SIZE_0)
  53 public final class ValueAnchorNode extends FixedWithNextNode implements LIRLowerable, Simplifiable, Virtualizable, AnchoringNode, GuardingNode {
  54 
  55     public static final NodeClass<ValueAnchorNode> TYPE = NodeClass.create(ValueAnchorNode.class);
  56     @OptionalInput(Guard) ValueNode anchored;
  57 
  58     public ValueAnchorNode(ValueNode value) {
  59         super(TYPE, StampFactory.forVoid());
  60         this.anchored = value;
  61     }
  62 
  63     @Override
  64     public void generate(NodeLIRBuilderTool gen) {
  65         // Nothing to emit, since this node is used for structural purposes only.
  66     }
  67 
  68     public ValueNode getAnchoredNode() {
  69         return anchored;
  70     }
  71 
  72     @Override
  73     public void simplify(SimplifierTool tool) {
  74         while (next() instanceof ValueAnchorNode) {
  75             ValueAnchorNode nextAnchor = (ValueAnchorNode) next();
  76             if (nextAnchor.anchored == anchored || nextAnchor.anchored == null) {
  77                 // two anchors for the same anchored -> coalesce
  78                 // nothing anchored on the next anchor -> coalesce
  79                 nextAnchor.replaceAtUsages(this);
  80                 GraphUtil.removeFixedWithUnusedInputs(nextAnchor);
  81             } else {
  82                 break;
  83             }
  84         }
  85         if (tool.allUsagesAvailable() && hasNoUsages() && next() instanceof FixedAccessNode) {
  86             FixedAccessNode currentNext = (FixedAccessNode) next();
  87             if (currentNext.getGuard() == anchored) {
  88                 GraphUtil.removeFixedWithUnusedInputs(this);
  89                 return;
  90             } else if (currentNext.getGuard() == null && anchored instanceof GuardNode && ((GuardNode) anchored).getCondition() instanceof IsNullNode) {
  91                 // coalesce null check guards into subsequent read/write
  92                 currentNext.setGuard((GuardingNode) anchored);
  93                 tool.addToWorkList(next());
  94                 return;
  95             }
  96         }
  97 
  98         if (anchored != null && (anchored.isConstant() || anchored instanceof FixedNode)) {
  99             // anchoring fixed nodes and constants is useless
 100             removeAnchoredNode();
 101         }
 102 
 103         if (anchored == null && hasNoUsages()) {
 104             // anchor is not necessary any more => remove.
 105             GraphUtil.removeFixedWithUnusedInputs(this);
 106         }
 107     }
 108 
 109     @Override
 110     public void virtualize(VirtualizerTool tool) {
 111         if (anchored == null || anchored instanceof AbstractBeginNode) {
 112             tool.delete();
 113         } else {
 114             ValueNode alias = tool.getAlias(anchored);
 115             if (alias instanceof VirtualObjectNode) {
 116                 tool.delete();
 117             }
 118         }
 119     }
 120 
 121     public void removeAnchoredNode() {
 122         this.updateUsages(anchored, null);
 123         this.anchored = null;
 124     }
 125 }