001/* 002 * Copyright (c) 2013, 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.phases; 024 025import java.util.*; 026 027import com.oracle.graal.nodes.*; 028 029/** 030 * A compiler phase that can apply an ordered collection of phases to a graph. 031 */ 032public class PhaseSuite<C> extends BasePhase<C> { 033 034 private final List<BasePhase<? super C>> phases; 035 036 public PhaseSuite() { 037 this.phases = new ArrayList<>(); 038 } 039 040 /** 041 * Add a new phase at the beginning of this suite. 042 */ 043 public final void prependPhase(BasePhase<? super C> phase) { 044 phases.add(0, phase); 045 } 046 047 /** 048 * Add a new phase at the end of this suite. 049 */ 050 public final void appendPhase(BasePhase<? super C> phase) { 051 phases.add(phase); 052 } 053 054 public final ListIterator<BasePhase<? super C>> findPhase(Class<? extends BasePhase<? super C>> phaseClass) { 055 ListIterator<BasePhase<? super C>> it = phases.listIterator(); 056 if (findNextPhase(it, phaseClass)) { 057 return it; 058 } else { 059 return null; 060 } 061 } 062 063 public static <C> boolean findNextPhase(ListIterator<BasePhase<? super C>> it, Class<? extends BasePhase<? super C>> phaseClass) { 064 while (it.hasNext()) { 065 BasePhase<? super C> phase = it.next(); 066 if (phaseClass.isInstance(phase)) { 067 return true; 068 } 069 } 070 return false; 071 } 072 073 /** 074 * Removes the first instance of the given phase class, looking recursively into inner phase 075 * suites. 076 */ 077 public boolean removePhase(Class<? extends BasePhase<? super C>> phaseClass) { 078 ListIterator<BasePhase<? super C>> it = phases.listIterator(); 079 while (it.hasNext()) { 080 BasePhase<? super C> phase = it.next(); 081 if (phaseClass.isInstance(phase)) { 082 it.remove(); 083 return true; 084 } else if (phase instanceof PhaseSuite) { 085 @SuppressWarnings("unchecked") 086 PhaseSuite<C> innerSuite = (PhaseSuite<C>) phase; 087 if (innerSuite.removePhase(phaseClass)) { 088 if (innerSuite.phases.isEmpty()) { 089 it.remove(); 090 } 091 return true; 092 } 093 } 094 } 095 return false; 096 } 097 098 @Override 099 protected void run(StructuredGraph graph, C context) { 100 for (BasePhase<? super C> phase : phases) { 101 phase.apply(graph, context); 102 } 103 } 104 105 public PhaseSuite<C> copy() { 106 PhaseSuite<C> suite = new PhaseSuite<>(); 107 suite.phases.addAll(phases); 108 return suite; 109 } 110}