001/*
002 * Copyright (c) 2015, 2015, 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 */
023package com.oracle.graal.lir.phases;
024
025import java.util.*;
026
027import jdk.internal.jvmci.code.*;
028
029import com.oracle.graal.compiler.common.cfg.*;
030import com.oracle.graal.lir.gen.*;
031
032public class LIRPhaseSuite<C> extends LIRPhase<C> {
033    private final List<LIRPhase<C>> phases;
034
035    public LIRPhaseSuite() {
036        phases = new ArrayList<>();
037    }
038
039    /**
040     * Add a new phase at the beginning of this suite.
041     */
042    public final void prependPhase(LIRPhase<C> phase) {
043        phases.add(0, phase);
044    }
045
046    /**
047     * Add a new phase at the end of this suite.
048     */
049    public final void appendPhase(LIRPhase<C> phase) {
050        phases.add(phase);
051    }
052
053    public final ListIterator<LIRPhase<C>> findPhase(Class<? extends LIRPhase<C>> phaseClass) {
054        ListIterator<LIRPhase<C>> it = phases.listIterator();
055        if (findNextPhase(it, phaseClass)) {
056            return it;
057        } else {
058            return null;
059        }
060    }
061
062    public static <C> boolean findNextPhase(ListIterator<LIRPhase<C>> it, Class<? extends LIRPhase<C>> phaseClass) {
063        while (it.hasNext()) {
064            LIRPhase<C> phase = it.next();
065            if (phaseClass.isInstance(phase)) {
066                return true;
067            }
068        }
069        return false;
070    }
071
072    @Override
073    protected <B extends AbstractBlockBase<B>> void run(TargetDescription target, LIRGenerationResult lirGenRes, List<B> codeEmittingOrder, List<B> linearScanOrder, C context) {
074        for (LIRPhase<C> phase : phases) {
075            phase.apply(target, lirGenRes, codeEmittingOrder, linearScanOrder, context);
076        }
077    }
078
079    public LIRPhaseSuite<C> copy() {
080        LIRPhaseSuite<C> suite = new LIRPhaseSuite<>();
081        suite.phases.addAll(phases);
082        return suite;
083    }
084}