1 /* 2 * Copyright (c) 2012, 2019, 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.InputType.Association; 28 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0; 29 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0; 30 31 import org.graalvm.compiler.graph.IterableNodeType; 32 import org.graalvm.compiler.graph.Node; 33 import org.graalvm.compiler.graph.NodeClass; 34 import org.graalvm.compiler.graph.iterators.NodeIterable; 35 import org.graalvm.compiler.graph.spi.Simplifiable; 36 import org.graalvm.compiler.graph.spi.SimplifierTool; 37 import org.graalvm.compiler.nodeinfo.NodeInfo; 38 import org.graalvm.compiler.nodes.util.GraphUtil; 39 40 @NodeInfo(allowedUsageTypes = {Association}, cycles = CYCLES_0, size = SIZE_0) 41 public final class LoopExitNode extends BeginStateSplitNode implements IterableNodeType, Simplifiable { 42 43 public static final NodeClass<LoopExitNode> TYPE = NodeClass.create(LoopExitNode.class); 44 45 /* 46 * The declared type of the field cannot be LoopBeginNode, because loop explosion during partial 47 * evaluation can temporarily assign a non-loop begin. This node will then be deleted shortly 48 * after - but we still must not have type system violations for that short amount of time. 49 */ 50 @Input(Association) AbstractBeginNode loopBegin; 51 52 public LoopExitNode(LoopBeginNode loop) { 53 super(TYPE); 54 assert loop != null; 55 loopBegin = loop; 56 } 57 58 public LoopBeginNode loopBegin() { 59 return (LoopBeginNode) loopBegin; 60 } 61 62 @Override 63 public NodeIterable<Node> anchored() { 64 return super.anchored().filter(n -> { 65 if (n instanceof ProxyNode) { 66 ProxyNode proxyNode = (ProxyNode) n; 67 return proxyNode.proxyPoint() != this; 68 } 69 return true; 70 }); 71 } 72 73 @Override 74 public void prepareDelete(FixedNode evacuateFrom) { 75 removeProxies(); 76 super.prepareDelete(evacuateFrom); 77 } 78 79 public void removeProxies() { 80 if (this.hasUsages()) { 81 outer: while (true) { 82 for (ProxyNode vpn : proxies().snapshot()) { 83 ValueNode value = vpn.value(); 84 vpn.replaceAtUsagesAndDelete(value); 85 if (value == this) { 86 // Guard proxy could have this input as value. 87 continue outer; 88 } 89 } 90 break; 91 } 92 } 93 } 94 95 @SuppressWarnings({"unchecked", "rawtypes"}) 96 public NodeIterable<ProxyNode> proxies() { 97 return (NodeIterable) usages().filter(n -> { 98 if (n instanceof ProxyNode) { 99 ProxyNode proxyNode = (ProxyNode) n; 100 return proxyNode.proxyPoint() == this; 101 } 102 return false; 103 }); 104 } 105 106 public void removeExit() { 107 this.removeProxies(); 108 FrameState loopStateAfter = this.stateAfter(); 109 graph().replaceFixedWithFixed(this, graph().add(new BeginNode())); 110 if (loopStateAfter != null) { 111 GraphUtil.tryKillUnused(loopStateAfter); 112 } 113 } 114 115 @Override 116 public void simplify(SimplifierTool tool) { 117 Node prev = this.predecessor(); 118 while (tool.allUsagesAvailable() && prev instanceof BeginNode && prev.hasNoUsages()) { 119 AbstractBeginNode begin = (AbstractBeginNode) prev; 120 this.setNodeSourcePosition(begin.getNodeSourcePosition()); 121 prev = prev.predecessor(); 122 graph().removeFixed(begin); 123 } 124 } 125 }