1 /* 2 * Copyright (c) 2012, 2012, 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.loop; 24 25 import java.util.ArrayList; 26 import java.util.Collection; 27 import java.util.Collections; 28 import java.util.Comparator; 29 import java.util.LinkedList; 30 import java.util.List; 31 import java.util.Map; 32 33 import org.graalvm.compiler.core.common.CollectionsFactory; 34 import org.graalvm.compiler.core.common.cfg.Loop; 35 import org.graalvm.compiler.debug.Debug; 36 import org.graalvm.compiler.debug.Debug.Scope; 37 import org.graalvm.compiler.graph.Node; 38 import org.graalvm.compiler.nodes.LoopBeginNode; 39 import org.graalvm.compiler.nodes.StructuredGraph; 40 import org.graalvm.compiler.nodes.ValueNode; 41 import org.graalvm.compiler.nodes.cfg.Block; 42 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; 43 44 public class LoopsData { 45 46 private Map<Loop<Block>, LoopEx> loopToEx = CollectionsFactory.newIdentityMap(); 47 private Map<LoopBeginNode, LoopEx> loopBeginToEx = Node.newIdentityMap(); 48 private ControlFlowGraph cfg; 49 50 @SuppressWarnings("try") 51 public LoopsData(final StructuredGraph graph) { 52 try (Scope s = Debug.scope("ControlFlowGraph")) { 53 cfg = ControlFlowGraph.compute(graph, true, true, true, true); 54 } catch (Throwable e) { 55 throw Debug.handle(e); 56 } 57 58 for (Loop<Block> loop : cfg.getLoops()) { 59 LoopEx ex = new LoopEx(loop, this); 60 loopToEx.put(loop, ex); 61 loopBeginToEx.put(ex.loopBegin(), ex); 62 } 63 } 64 65 public LoopEx loop(Loop<?> loop) { 66 return loopToEx.get(loop); 67 } 68 69 public LoopEx loop(LoopBeginNode loopBegin) { 70 return loopBeginToEx.get(loopBegin); 71 } 72 73 public Collection<LoopEx> loops() { 74 return loopToEx.values(); 75 } 76 77 public List<LoopEx> outerFirst() { 78 ArrayList<LoopEx> loops = new ArrayList<>(loops()); 79 Collections.sort(loops, new Comparator<LoopEx>() { 80 81 @Override 82 public int compare(LoopEx o1, LoopEx o2) { 83 return o1.loop().getDepth() - o2.loop().getDepth(); 84 } 85 }); 86 return loops; 87 } 88 89 public List<LoopEx> innerFirst() { 90 ArrayList<LoopEx> loops = new ArrayList<>(loops()); 91 Collections.sort(loops, new Comparator<LoopEx>() { 92 93 @Override 94 public int compare(LoopEx o1, LoopEx o2) { 95 return o2.loop().getDepth() - o1.loop().getDepth(); 96 } 97 }); 98 return loops; 99 } 100 101 public Collection<LoopEx> countedLoops() { 102 List<LoopEx> counted = new LinkedList<>(); 103 for (LoopEx loop : loops()) { 104 if (loop.isCounted()) { 105 counted.add(loop); 106 } 107 } 108 return counted; 109 } 110 111 public void detectedCountedLoops() { 112 for (LoopEx loop : loops()) { 113 loop.detectCounted(); 114 } 115 } 116 117 public ControlFlowGraph getCFG() { 118 return cfg; 119 } 120 121 public InductionVariable getInductionVariable(ValueNode value) { 122 InductionVariable match = null; 123 for (LoopEx loop : loops()) { 124 InductionVariable iv = loop.getInductionVariables().get(value); 125 if (iv != null) { 126 if (match != null) { 127 return null; 128 } 129 match = iv; 130 } 131 } 132 return match; 133 } 134 135 /** 136 * Deletes any nodes created within the scope of this object that have no usages. 137 */ 138 public void deleteUnusedNodes() { 139 for (LoopEx loop : loops()) { 140 loop.deleteUnusedNodes(); 141 } 142 } 143 }