< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.common/src/org/graalvm/compiler/core/common/cfg/Loop.java

Print this page




   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 
  26 package org.graalvm.compiler.core.common.cfg;
  27 


  28 import java.util.ArrayList;

  29 import java.util.List;
  30 
  31 public abstract class Loop<T extends AbstractBlockBase<T>> {
  32 
  33     private final Loop<T> parent;
  34     private final List<Loop<T>> children;
  35 
  36     private final int depth;
  37     private final int index;
  38     private final T header;
  39     private final List<T> blocks;
  40     private final List<T> exits;






  41 
  42     protected Loop(Loop<T> parent, int index, T header) {
  43         this.parent = parent;
  44         if (parent != null) {
  45             this.depth = parent.getDepth() + 1;
  46         } else {
  47             this.depth = 1;
  48         }
  49         this.index = index;
  50         this.header = header;
  51         this.blocks = new ArrayList<>();
  52         this.children = new ArrayList<>();
  53         this.exits = new ArrayList<>();

  54     }
  55 
  56     public abstract long numBackedges();
  57 
  58     @Override
  59     public String toString() {
  60         return "loop " + index + " depth " + getDepth() + (parent != null ? " outer " + parent.index : "");
  61     }
  62 
  63     public Loop<T> getParent() {
  64         return parent;
  65     }
  66 
  67     public List<Loop<T>> getChildren() {
  68         return children;
  69     }
  70 
  71     public int getDepth() {
  72         return depth;
  73     }
  74 
  75     public int getIndex() {
  76         return index;
  77     }
  78 
  79     public T getHeader() {
  80         return header;
  81     }
  82 
  83     public List<T> getBlocks() {
  84         return blocks;
  85     }
  86 
  87     public List<T> getExits() {














































  88         return exits;
  89     }
  90 
  91     public void addExit(T t) {
  92         exits.add(t);





























  93     }
  94 
  95     /**
  96      * Determines if one loop is a transitive parent of another loop.
  97      *
  98      * @param childLoop The loop for which parentLoop might be a transitive parent loop.
  99      * @param parentLoop The loop which might be a transitive parent loop of child loop.
 100      * @return {@code true} if parentLoop is a (transitive) parent loop of childLoop, {@code false}
 101      *         otherwise
 102      */
 103     public static <T extends AbstractBlockBase<T>> boolean transitiveParentLoop(Loop<T> childLoop, Loop<T> parentLoop) {
 104         Loop<T> curr = childLoop;
 105         while (curr != null) {
 106             if (curr == parentLoop) {
 107                 return true;
 108             }
 109             curr = curr.getParent();
 110         }
 111         return false;
 112     }
 113 
 114     @Override
 115     public int hashCode() {
 116         return index + depth * 31;





 117     }
 118 }


   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 
  26 package org.graalvm.compiler.core.common.cfg;
  27 
  28 import static org.graalvm.compiler.core.common.cfg.AbstractBlockBase.BLOCK_ID_COMPARATOR;
  29 
  30 import java.util.ArrayList;
  31 import java.util.Collections;
  32 import java.util.List;
  33 
  34 public abstract class Loop<T extends AbstractBlockBase<T>> {
  35 
  36     private final Loop<T> parent;
  37     private final ArrayList<Loop<T>> children;
  38 
  39     private final int depth;
  40     private final int index;
  41     private final T header;
  42     private final ArrayList<T> blocks;
  43     private final ArrayList<T> exits;
  44     /**
  45      * Natural exits, ignoring LoopExitNodes.
  46      *
  47      * @see #getNaturalExits()
  48      */
  49     private final ArrayList<T> naturalExits;
  50 
  51     protected Loop(Loop<T> parent, int index, T header) {
  52         this.parent = parent;
  53         if (parent != null) {
  54             this.depth = parent.getDepth() + 1;
  55         } else {
  56             this.depth = 1;
  57         }
  58         this.index = index;
  59         this.header = header;
  60         this.blocks = new ArrayList<>();
  61         this.children = new ArrayList<>();
  62         this.exits = new ArrayList<>();
  63         this.naturalExits = new ArrayList<>();
  64     }
  65 
  66     public abstract long numBackedges();
  67 
  68     @Override
  69     public String toString() {
  70         return "loop " + index + " depth " + getDepth() + (parent != null ? " outer " + parent.index : "");
  71     }
  72 
  73     public Loop<T> getParent() {
  74         return parent;
  75     }
  76 
  77     public List<Loop<T>> getChildren() {
  78         return children;
  79     }
  80 
  81     public int getDepth() {
  82         return depth;
  83     }
  84 
  85     public int getIndex() {
  86         return index;
  87     }
  88 
  89     public T getHeader() {
  90         return header;
  91     }
  92 
  93     public List<T> getBlocks() {
  94         return blocks;
  95     }
  96 
  97     /**
  98      * Returns the loop exits.
  99      *
 100      * This might be a conservative set: before framestate assignment it matches the LoopExitNodes
 101      * even if earlier blocks could be considered as exits. After framestate assignments, this is
 102      * the same as {@link #getNaturalExits()}.
 103      *
 104      * <p>
 105      * LoopExitNodes are inserted in the control-flow during parsing and are natural exits: they are
 106      * the earliest block at which we are guaranteed to have exited the loop. However, after some
 107      * transformations of the graph, the natural exit might go up but the LoopExitNodes are not
 108      * updated.
 109      * </p>
 110      *
 111      * <p>
 112      * For example in:
 113      *
 114      * <pre>
 115      * for (int i = 0; i < N; i++) {
 116      *     if (c) {
 117      *         // Block 1
 118      *         if (dummy) {
 119      *             // ...
 120      *         } else {
 121      *             // ...
 122      *         }
 123      *         if (b) {
 124      *             continue;
 125      *         } else {
 126      *             // Block 2
 127      *             // LoopExitNode
 128      *             break;
 129      *         }
 130      *     }
 131      * }
 132      * </pre>
 133      *
 134      * After parsing, the natural exits match the LoopExitNode: Block 2 is a natural exit and has a
 135      * LoopExitNode. If the {@code b} condition gets canonicalized to {@code false}, the natural
 136      * exit moves to Block 1 while the LoopExitNode remains in Block 2. In such a scenario,
 137      * {@code getLoopExits()} will contain block 2 while {@link #getNaturalExits()} will contain
 138      * block 1.
 139      *
 140      *
 141      * @see #getNaturalExits()
 142      */
 143     public List<T> getLoopExits() {
 144         return exits;
 145     }
 146 
 147     public boolean isLoopExit(T block) {
 148         assert isSorted(exits);
 149         return Collections.binarySearch(exits, block, BLOCK_ID_COMPARATOR) >= 0;
 150     }
 151 
 152     /**
 153      * Returns the natural exit points: these are the earliest block that are guaranteed to never
 154      * reach a back-edge.
 155      *
 156      * This can not be used in the context of preserving or using loop-closed form.
 157      *
 158      * @see #getLoopExits()
 159      */
 160     public ArrayList<T> getNaturalExits() {
 161         return naturalExits;
 162     }
 163 
 164     public boolean isNaturalExit(T block) {
 165         assert isSorted(naturalExits);
 166         return Collections.binarySearch(naturalExits, block, BLOCK_ID_COMPARATOR) >= 0;
 167     }
 168 
 169     private static <T extends AbstractBlockBase<T>> boolean isSorted(List<T> list) {
 170         int lastId = Integer.MIN_VALUE;
 171         for (AbstractBlockBase<?> block : list) {
 172             if (block.getId() < lastId) {
 173                 return false;
 174             }
 175             lastId = block.getId();
 176         }
 177         return true;
 178     }
 179 
 180     /**
 181      * Determines if one loop is a transitive parent of another loop.
 182      *
 183      * @param childLoop The loop for which parentLoop might be a transitive parent loop.
 184      * @param parentLoop The loop which might be a transitive parent loop of child loop.
 185      * @return {@code true} if parentLoop is a (transitive) parent loop of childLoop, {@code false}
 186      *         otherwise
 187      */
 188     public static <T extends AbstractBlockBase<T>> boolean transitiveParentLoop(Loop<T> childLoop, Loop<T> parentLoop) {
 189         Loop<T> curr = childLoop;
 190         while (curr != null) {
 191             if (curr == parentLoop) {
 192                 return true;
 193             }
 194             curr = curr.getParent();
 195         }
 196         return false;
 197     }
 198 
 199     @Override
 200     public int hashCode() {
 201         return index + depth * 31;
 202     }
 203 
 204     @Override
 205     public boolean equals(Object obj) {
 206         return super.equals(obj);
 207     }
 208 }
< prev index next >