1 /*
   2  * Copyright (c) 2011, 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 java.util.Iterator;
  26 import java.util.NoSuchElementException;
  27 
  28 import org.graalvm.compiler.core.common.type.Stamp;
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 import org.graalvm.compiler.graph.IterableNodeType;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.graph.iterators.NodeIterable;
  34 import org.graalvm.compiler.nodeinfo.InputType;
  35 import org.graalvm.compiler.nodeinfo.NodeInfo;
  36 import org.graalvm.compiler.nodes.extended.AnchoringNode;
  37 import org.graalvm.compiler.nodes.extended.GuardingNode;
  38 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  39 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  40 
  41 @NodeInfo(allowedUsageTypes = {InputType.Guard, InputType.Anchor})
  42 public abstract class AbstractBeginNode extends FixedWithNextNode implements LIRLowerable, GuardingNode, AnchoringNode, IterableNodeType {
  43 
  44     public static final NodeClass<AbstractBeginNode> TYPE = NodeClass.create(AbstractBeginNode.class);
  45 
  46     protected AbstractBeginNode(NodeClass<? extends AbstractBeginNode> c) {
  47         this(c, StampFactory.forVoid());
  48     }
  49 
  50     protected AbstractBeginNode(NodeClass<? extends AbstractBeginNode> c, Stamp stamp) {
  51         super(c, stamp);
  52     }
  53 
  54     public static AbstractBeginNode prevBegin(FixedNode from) {
  55         Node next = from;
  56         while (next != null) {
  57             if (next instanceof AbstractBeginNode) {
  58                 AbstractBeginNode begin = (AbstractBeginNode) next;
  59                 return begin;
  60             }
  61             next = next.predecessor();
  62         }
  63         return null;
  64     }
  65 
  66     private void evacuateGuards(FixedNode evacuateFrom) {
  67         if (!hasNoUsages()) {
  68             AbstractBeginNode prevBegin = prevBegin(evacuateFrom);
  69             assert prevBegin != null;
  70             for (Node anchored : anchored().snapshot()) {
  71                 anchored.replaceFirstInput(this, prevBegin);
  72             }
  73         }
  74     }
  75 
  76     public void prepareDelete() {
  77         prepareDelete((FixedNode) predecessor());
  78     }
  79 
  80     public void prepareDelete(FixedNode evacuateFrom) {
  81         evacuateGuards(evacuateFrom);
  82     }
  83 
  84     @Override
  85     public boolean verify() {
  86         assertTrue(predecessor() != null || this == graph().start() || this instanceof AbstractMergeNode, "begin nodes must be connected");
  87         return super.verify();
  88     }
  89 
  90     @Override
  91     public void generate(NodeLIRBuilderTool gen) {
  92         // nop
  93     }
  94 
  95     public NodeIterable<GuardNode> guards() {
  96         return usages().filter(GuardNode.class);
  97     }
  98 
  99     public NodeIterable<Node> anchored() {
 100         return usages();
 101     }
 102 
 103     public NodeIterable<FixedNode> getBlockNodes() {
 104         return new NodeIterable<FixedNode>() {
 105 
 106             @Override
 107             public Iterator<FixedNode> iterator() {
 108                 return new BlockNodeIterator(AbstractBeginNode.this);
 109             }
 110         };
 111     }
 112 
 113     private class BlockNodeIterator implements Iterator<FixedNode> {
 114 
 115         private FixedNode current;
 116 
 117         BlockNodeIterator(FixedNode next) {
 118             this.current = next;
 119         }
 120 
 121         @Override
 122         public boolean hasNext() {
 123             return current != null;
 124         }
 125 
 126         @Override
 127         public FixedNode next() {
 128             FixedNode ret = current;
 129             if (ret == null) {
 130                 throw new NoSuchElementException();
 131             }
 132             if (!(current instanceof FixedWithNextNode) || (current instanceof AbstractBeginNode && current != AbstractBeginNode.this)) {
 133                 current = null;
 134             } else {
 135                 current = ((FixedWithNextNode) current).next();
 136             }
 137             return ret;
 138         }
 139 
 140         @Override
 141         public void remove() {
 142             throw new UnsupportedOperationException();
 143         }
 144     }
 145 }