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