001/*
002 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023
024package com.oracle.graal.compiler.common.cfg;
025
026import java.util.*;
027
028public abstract class Loop<T extends AbstractBlockBase<T>> {
029
030    private final Loop<T> parent;
031    private final List<Loop<T>> children;
032
033    private final int depth;
034    private final int index;
035    private final T header;
036    private final List<T> blocks;
037    private final List<T> exits;
038
039    protected Loop(Loop<T> parent, int index, T header) {
040        this.parent = parent;
041        if (parent != null) {
042            this.depth = parent.getDepth() + 1;
043            parent.getChildren().add(this);
044        } else {
045            this.depth = 1;
046        }
047        this.index = index;
048        this.header = header;
049        this.blocks = new ArrayList<>();
050        this.children = new ArrayList<>();
051        this.exits = new ArrayList<>();
052    }
053
054    public abstract long numBackedges();
055
056    @Override
057    public String toString() {
058        return "loop " + index + " depth " + getDepth() + (parent != null ? " outer " + parent.index : "");
059    }
060
061    public Loop<T> getParent() {
062        return parent;
063    }
064
065    public List<Loop<T>> getChildren() {
066        return children;
067    }
068
069    public int getDepth() {
070        return depth;
071    }
072
073    public int getIndex() {
074        return index;
075    }
076
077    public T getHeader() {
078        return header;
079    }
080
081    public List<T> getBlocks() {
082        return blocks;
083    }
084
085    public List<T> getExits() {
086        return exits;
087    }
088}