src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.loop/src/org/graalvm/compiler/loop/LoopsData.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.loop/src/org/graalvm/compiler/loop

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.loop/src/org/graalvm/compiler/loop/LoopsData.java

Print this page


   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() {


   1 /*
   2  * Copyright (c) 2012, 2017, 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.LinkedList;
  28 import java.util.List;

  29 

  30 import org.graalvm.compiler.core.common.cfg.Loop;
  31 import org.graalvm.compiler.debug.Debug;
  32 import org.graalvm.compiler.debug.Debug.Scope;

  33 import org.graalvm.compiler.nodes.LoopBeginNode;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.ValueNode;
  36 import org.graalvm.compiler.nodes.cfg.Block;
  37 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
  38 import org.graalvm.util.Equivalence;
  39 import org.graalvm.util.EconomicMap;
  40 import org.graalvm.util.EconomicSet;
  41 
  42 public class LoopsData {
  43     private final EconomicMap<LoopBeginNode, LoopEx> loopBeginToEx = EconomicMap.create(Equivalence.IDENTITY);
  44     private final ControlFlowGraph cfg;
  45     private final List<LoopEx> loops;

  46 
  47     @SuppressWarnings("try")
  48     public LoopsData(final StructuredGraph graph) {
  49         try (Scope s = Debug.scope("ControlFlowGraph")) {
  50             cfg = ControlFlowGraph.compute(graph, true, true, true, true);
  51         } catch (Throwable e) {
  52             throw Debug.handle(e);
  53         }
  54         assert checkLoopOrder(cfg.getLoops());
  55         loops = new ArrayList<>(cfg.getLoops().size());
  56         for (Loop<Block> loop : cfg.getLoops()) {
  57             LoopEx ex = new LoopEx(loop, this);
  58             loops.add(ex);
  59             loopBeginToEx.put(ex.loopBegin(), ex);
  60         }
  61     }
  62 
  63     /**
  64      * Checks that loops are ordered such that outer loops appear first.
  65      */
  66     private static boolean checkLoopOrder(Iterable<Loop<Block>> loops) {
  67         EconomicSet<Loop<Block>> seen = EconomicSet.create(Equivalence.IDENTITY);
  68         for (Loop<Block> loop : loops) {
  69             if (loop.getParent() != null && !seen.contains(loop.getParent())) {
  70                 return false;
  71             }
  72             seen.add(loop);


  73         }
  74         return true;


  75     }
  76 
  77     public LoopEx loop(Loop<Block> loop) {
  78         return loopBeginToEx.get((LoopBeginNode) loop.getHeader().getBeginNode());
  79     }
  80 
  81     public LoopEx loop(LoopBeginNode loopBegin) {
  82         return loopBeginToEx.get(loopBegin);

  83     }
  84 
  85     public List<LoopEx> loops() {
  86         return loops;
  87     }
  88 
  89     public List<LoopEx> outerFirst() {








  90         return loops;
  91     }
  92 
  93     public Collection<LoopEx> countedLoops() {
  94         List<LoopEx> counted = new LinkedList<>();
  95         for (LoopEx loop : loops()) {
  96             if (loop.isCounted()) {
  97                 counted.add(loop);
  98             }
  99         }
 100         return counted;
 101     }
 102 
 103     public void detectedCountedLoops() {
 104         for (LoopEx loop : loops()) {
 105             loop.detectCounted();
 106         }
 107     }
 108 
 109     public ControlFlowGraph getCFG() {


src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.loop/src/org/graalvm/compiler/loop/LoopsData.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File