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