1 /*
2  * Copyright (c) 2015, 2018, 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 
25 package org.graalvm.compiler.lir.phases;
26 
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.ListIterator;
31 
32 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
33 
34 import jdk.vm.ci.code.TargetDescription;
35 
36 public class LIRPhaseSuite<C> extends LIRPhase<C> {
37     private List<LIRPhase<C>> phases;
38     private boolean immutable;
39 
LIRPhaseSuite()40     public LIRPhaseSuite() {
41         phases = new ArrayList<>();
42     }
43 
44     /**
45      * Gets an unmodifiable view on the phases in this suite.
46      */
getPhases()47     public List<LIRPhase<C>> getPhases() {
48         return Collections.unmodifiableList(phases);
49     }
50 
51     /**
52      * Add a new phase at the beginning of this suite.
53      */
prependPhase(LIRPhase<C> phase)54     public final void prependPhase(LIRPhase<C> phase) {
55         phases.add(0, phase);
56     }
57 
58     /**
59      * Add a new phase at the end of this suite.
60      */
appendPhase(LIRPhase<C> phase)61     public final void appendPhase(LIRPhase<C> phase) {
62         phases.add(phase);
63     }
64 
findPhase(Class<? extends LIRPhase<C>> phaseClass)65     public final ListIterator<LIRPhase<C>> findPhase(Class<? extends LIRPhase<C>> phaseClass) {
66         ListIterator<LIRPhase<C>> it = phases.listIterator();
67         if (findNextPhase(it, phaseClass)) {
68             return it;
69         } else {
70             return null;
71         }
72     }
73 
findPhaseInstance(Class<T> phaseClass)74     public final <T extends LIRPhase<C>> T findPhaseInstance(Class<T> phaseClass) {
75         ListIterator<LIRPhase<C>> it = phases.listIterator();
76         while (it.hasNext()) {
77             LIRPhase<C> phase = it.next();
78             if (phaseClass.isInstance(phase)) {
79                 return phaseClass.cast(phase);
80             }
81         }
82         return null;
83     }
84 
findNextPhase(ListIterator<LIRPhase<C>> it, Class<? extends LIRPhase<C>> phaseClass)85     public static <C> boolean findNextPhase(ListIterator<LIRPhase<C>> it, Class<? extends LIRPhase<C>> phaseClass) {
86         while (it.hasNext()) {
87             LIRPhase<C> phase = it.next();
88             if (phaseClass.isInstance(phase)) {
89                 return true;
90             }
91         }
92         return false;
93     }
94 
95     @Override
run(TargetDescription target, LIRGenerationResult lirGenRes, C context)96     protected final void run(TargetDescription target, LIRGenerationResult lirGenRes, C context) {
97         for (LIRPhase<C> phase : phases) {
98             phase.apply(target, lirGenRes, context);
99         }
100     }
101 
copy()102     public LIRPhaseSuite<C> copy() {
103         LIRPhaseSuite<C> suite = new LIRPhaseSuite<>();
104         suite.phases.addAll(phases);
105         return suite;
106     }
107 
isImmutable()108     public boolean isImmutable() {
109         return immutable;
110     }
111 
setImmutable()112     public synchronized void setImmutable() {
113         if (!immutable) {
114             phases = Collections.unmodifiableList(phases);
115             immutable = true;
116         }
117     }
118 }
119