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 24 package org.graalvm.compiler.core.common.cfg; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 public abstract class Loop<T extends AbstractBlockBase<T>> { 30 31 private final Loop<T> parent; 32 private final List<Loop<T>> children; 33 34 private final int depth; 35 private final int index; 36 private final T header; 37 private final List<T> blocks; 38 private final List<T> exits; 39 40 protected Loop(Loop<T> parent, int index, T header) { 41 this.parent = parent; 42 if (parent != null) { 43 this.depth = parent.getDepth() + 1; 44 } else { 45 this.depth = 1; 46 } 47 this.index = index; 48 this.header = header; 49 this.blocks = new ArrayList<>(); 50 this.children = new ArrayList<>(); 51 this.exits = new ArrayList<>(); 52 } 53 54 public abstract long numBackedges(); 55 56 @Override 57 public String toString() { 58 return "loop " + index + " depth " + getDepth() + (parent != null ? " outer " + parent.index : ""); 59 } 60 61 public Loop<T> getParent() { 62 return parent; 63 } 64 65 public List<Loop<T>> getChildren() { 66 return children; 67 } 68 69 public int getDepth() { 70 return depth; 71 } 72 73 public int getIndex() { 74 return index; 75 } 76 77 public T getHeader() { 78 return header; 79 } 80 81 public List<T> getBlocks() { 82 return blocks; 83 } 84 85 public List<T> getExits() { 86 return exits; 87 } 88 89 public void addExit(T t) { 90 exits.add(t); 91 } 92 93 /** 94 * Determines if one loop is a transitive parent of another loop. 95 * 96 * @param childLoop The loop for which parentLoop might be a transitive parent loop. 97 * @param parentLoop The loop which might be a transitive parent loop of child loop. 98 * @return {@code true} if parentLoop is a (transitive) parent loop of childLoop, {@code false} 99 * otherwise 100 */ 101 public static <T extends AbstractBlockBase<T>> boolean transitiveParentLoop(Loop<T> childLoop, Loop<T> parentLoop) { 102 Loop<T> curr = childLoop; 103 while (curr != null) { 104 if (curr == parentLoop) { 105 return true; 106 } 107 curr = curr.getParent(); 108 } 109 return false; 110 } 111 112 @Override 113 public int hashCode() { 114 return index + depth * 31; 115 } 116 }