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.loop; 24 25 import java.util.ArrayDeque; 26 import java.util.Collections; 27 import java.util.Deque; 28 import java.util.Iterator; 29 30 import org.graalvm.compiler.debug.GraalError; 31 import org.graalvm.compiler.graph.Graph; 32 import org.graalvm.compiler.graph.Graph.DuplicationReplacement; 33 import org.graalvm.compiler.graph.Node; 34 import org.graalvm.compiler.graph.NodeBitMap; 35 import org.graalvm.compiler.graph.iterators.NodeIterable; 36 import org.graalvm.compiler.nodes.AbstractBeginNode; 37 import org.graalvm.compiler.nodes.EndNode; 38 import org.graalvm.compiler.nodes.FixedNode; 39 import org.graalvm.compiler.nodes.FrameState; 40 import org.graalvm.compiler.nodes.GuardNode; 41 import org.graalvm.compiler.nodes.GuardPhiNode; 42 import org.graalvm.compiler.nodes.GuardProxyNode; 43 import org.graalvm.compiler.nodes.Invoke; 44 import org.graalvm.compiler.nodes.LoopExitNode; 45 import org.graalvm.compiler.nodes.MergeNode; 46 import org.graalvm.compiler.nodes.PhiNode; 47 import org.graalvm.compiler.nodes.ProxyNode; 48 import org.graalvm.compiler.nodes.StructuredGraph; 49 import org.graalvm.compiler.nodes.ValueNode; 50 import org.graalvm.compiler.nodes.ValuePhiNode; 51 import org.graalvm.compiler.nodes.ValueProxyNode; 52 import org.graalvm.compiler.nodes.VirtualState; 53 import org.graalvm.compiler.nodes.cfg.Block; 54 import org.graalvm.compiler.nodes.java.MonitorEnterNode; 55 import org.graalvm.compiler.nodes.spi.NodeWithState; 56 import org.graalvm.compiler.nodes.virtual.CommitAllocationNode; 57 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 58 import org.graalvm.util.EconomicMap; 59 60 import jdk.vm.ci.meta.TriState; 61 62 public abstract class LoopFragment { 63 64 private final LoopEx loop; 65 private final LoopFragment original; 66 protected NodeBitMap nodes; 67 protected boolean nodesReady; 68 private EconomicMap<Node, Node> duplicationMap; 69 70 public LoopFragment(LoopEx loop) { 71 this(loop, null); 72 this.nodesReady = true; 73 } 74 75 public LoopFragment(LoopEx loop, LoopFragment original) { 76 this.loop = loop; 77 this.original = original; 78 this.nodesReady = false; 79 } 80 81 public LoopEx loop() { 82 return loop; 83 } 84 85 public abstract LoopFragment duplicate(); 86 87 public abstract void insertBefore(LoopEx l); 88 89 public void disconnect() { 90 // TODO (gd) possibly abstract 91 } 92 93 public boolean contains(Node n) { 94 return nodes().isMarkedAndGrow(n); 95 } 96 97 @SuppressWarnings("unchecked") 98 public <New extends Node, Old extends New> New getDuplicatedNode(Old n) { 99 assert isDuplicate(); 100 return (New) duplicationMap.get(n); 101 } 155 public Node replacement(Node o) { 156 Node r1 = dataFix.replacement(o); 157 if (r1 != o) { 158 assert cfgFix.replacement(o) == o; 159 return r1; 160 } 161 Node r2 = cfgFix.replacement(o); 162 if (r2 != o) { 163 return r2; 164 } 165 return o; 166 } 167 }; 168 } else { 169 dr = null; 170 } 171 beforeDuplication(); 172 NodeIterable<Node> nodesIterable = original().nodes(); 173 duplicationMap = graph().addDuplicates(nodesIterable, graph(), nodesIterable.count(), dr); 174 finishDuplication(); 175 nodesReady = true; 176 } else { 177 // TODO (gd) apply fix ? 178 } 179 } 180 181 protected static NodeBitMap computeNodes(Graph graph, Iterable<AbstractBeginNode> blocks) { 182 return computeNodes(graph, blocks, Collections.emptyList()); 183 } 184 185 protected static NodeBitMap computeNodes(Graph graph, Iterable<AbstractBeginNode> blocks, Iterable<AbstractBeginNode> earlyExits) { 186 final NodeBitMap nodes = graph.createNodeBitMap(); 187 computeNodes(nodes, graph, blocks, earlyExits); 188 return nodes; 189 } 190 191 protected static void computeNodes(NodeBitMap nodes, Graph graph, Iterable<AbstractBeginNode> blocks, Iterable<AbstractBeginNode> earlyExits) { 192 for (AbstractBeginNode b : blocks) { 193 if (b.isDeleted()) { 194 continue; | 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.loop; 24 25 import jdk.vm.ci.meta.TriState; 26 import org.graalvm.compiler.debug.GraalError; 27 import org.graalvm.compiler.graph.Graph; 28 import org.graalvm.compiler.graph.Graph.DuplicationReplacement; 29 import org.graalvm.compiler.graph.Node; 30 import org.graalvm.compiler.graph.NodeBitMap; 31 import org.graalvm.compiler.graph.iterators.NodeIterable; 32 import org.graalvm.compiler.nodes.AbstractBeginNode; 33 import org.graalvm.compiler.nodes.EndNode; 34 import org.graalvm.compiler.nodes.FixedNode; 35 import org.graalvm.compiler.nodes.FrameState; 36 import org.graalvm.compiler.nodes.GuardNode; 37 import org.graalvm.compiler.nodes.GuardPhiNode; 38 import org.graalvm.compiler.nodes.GuardProxyNode; 39 import org.graalvm.compiler.nodes.Invoke; 40 import org.graalvm.compiler.nodes.LoopExitNode; 41 import org.graalvm.compiler.nodes.MergeNode; 42 import org.graalvm.compiler.nodes.PhiNode; 43 import org.graalvm.compiler.nodes.ProxyNode; 44 import org.graalvm.compiler.nodes.StructuredGraph; 45 import org.graalvm.compiler.nodes.ValueNode; 46 import org.graalvm.compiler.nodes.ValuePhiNode; 47 import org.graalvm.compiler.nodes.ValueProxyNode; 48 import org.graalvm.compiler.nodes.VirtualState; 49 import org.graalvm.compiler.nodes.cfg.Block; 50 import org.graalvm.compiler.nodes.java.MonitorEnterNode; 51 import org.graalvm.compiler.nodes.spi.NodeWithState; 52 import org.graalvm.compiler.nodes.virtual.CommitAllocationNode; 53 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 54 import org.graalvm.util.EconomicMap; 55 56 import java.util.ArrayDeque; 57 import java.util.Collections; 58 import java.util.Deque; 59 import java.util.Iterator; 60 61 public abstract class LoopFragment { 62 63 private final LoopEx loop; 64 private final LoopFragment original; 65 protected NodeBitMap nodes; 66 protected boolean nodesReady; 67 private EconomicMap<Node, Node> duplicationMap; 68 69 public LoopFragment(LoopEx loop) { 70 this(loop, null); 71 this.nodesReady = true; 72 } 73 74 public LoopFragment(LoopEx loop, LoopFragment original) { 75 this.loop = loop; 76 this.original = original; 77 this.nodesReady = false; 78 } 79 80 /** 81 * Return the original LoopEx for this fragment. For duplicated fragments this returns null. 82 */ 83 protected LoopEx loop() { 84 return loop; 85 } 86 87 public abstract LoopFragment duplicate(); 88 89 public abstract void insertBefore(LoopEx l); 90 91 public void disconnect() { 92 // TODO (gd) possibly abstract 93 } 94 95 public boolean contains(Node n) { 96 return nodes().isMarkedAndGrow(n); 97 } 98 99 @SuppressWarnings("unchecked") 100 public <New extends Node, Old extends New> New getDuplicatedNode(Old n) { 101 assert isDuplicate(); 102 return (New) duplicationMap.get(n); 103 } 157 public Node replacement(Node o) { 158 Node r1 = dataFix.replacement(o); 159 if (r1 != o) { 160 assert cfgFix.replacement(o) == o; 161 return r1; 162 } 163 Node r2 = cfgFix.replacement(o); 164 if (r2 != o) { 165 return r2; 166 } 167 return o; 168 } 169 }; 170 } else { 171 dr = null; 172 } 173 beforeDuplication(); 174 NodeIterable<Node> nodesIterable = original().nodes(); 175 duplicationMap = graph().addDuplicates(nodesIterable, graph(), nodesIterable.count(), dr); 176 finishDuplication(); 177 nodes = new NodeBitMap(graph()); 178 nodes.markAll(duplicationMap.getValues()); 179 nodesReady = true; 180 } else { 181 // TODO (gd) apply fix ? 182 } 183 } 184 185 protected static NodeBitMap computeNodes(Graph graph, Iterable<AbstractBeginNode> blocks) { 186 return computeNodes(graph, blocks, Collections.emptyList()); 187 } 188 189 protected static NodeBitMap computeNodes(Graph graph, Iterable<AbstractBeginNode> blocks, Iterable<AbstractBeginNode> earlyExits) { 190 final NodeBitMap nodes = graph.createNodeBitMap(); 191 computeNodes(nodes, graph, blocks, earlyExits); 192 return nodes; 193 } 194 195 protected static void computeNodes(NodeBitMap nodes, Graph graph, Iterable<AbstractBeginNode> blocks, Iterable<AbstractBeginNode> earlyExits) { 196 for (AbstractBeginNode b : blocks) { 197 if (b.isDeleted()) { 198 continue; |