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 org.graalvm.compiler.lir.LIR;
  26 import org.graalvm.compiler.lir.Variable;
  27 import org.graalvm.compiler.lir.VirtualStackSlot;
  28 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  29 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  30 import org.graalvm.compiler.lir.phases.AllocationPhase.AllocationContext;
  31 import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase.PostAllocationOptimizationContext;
  32 import org.graalvm.compiler.lir.phases.PreAllocationOptimizationPhase.PreAllocationOptimizationContext;
  33 
  34 import jdk.vm.ci.code.StackSlot;
  35 
  36 public class LIRSuites {
  37 
  38     private final LIRPhaseSuite<PreAllocationOptimizationContext> preAllocOptStage;
  39     private final LIRPhaseSuite<AllocationContext> allocStage;
  40     private final LIRPhaseSuite<PostAllocationOptimizationContext> postAllocStage;
  41     private boolean immutable;
  42 
  43     public LIRSuites(LIRPhaseSuite<PreAllocationOptimizationContext> preAllocOptStage, LIRPhaseSuite<AllocationContext> allocStage, LIRPhaseSuite<PostAllocationOptimizationContext> postAllocStage) {
  44         this.preAllocOptStage = preAllocOptStage;
  45         this.allocStage = allocStage;
  46         this.postAllocStage = postAllocStage;
  47     }
  48 
  49     public LIRSuites(LIRSuites other) {
  50         this(other.getPreAllocationOptimizationStage().copy(), other.getAllocationStage().copy(), other.getPostAllocationOptimizationStage().copy());
  51     }
  52 
  53     /**
  54      * {@link PreAllocationOptimizationPhase}s are executed between {@link LIR} generation and
  55      * register allocation.
  56      * <p>
  57      * {@link PreAllocationOptimizationPhase Implementers} can create new
  58      * {@link LIRGeneratorTool#newVariable variables}, {@link LIRGenerationResult#getFrameMap stack
  59      * slots} and {@link LIRGenerationResult#getFrameMapBuilder virtual stack slots}.
  60      */
  61     public LIRPhaseSuite<PreAllocationOptimizationContext> getPreAllocationOptimizationStage() {
  62         return preAllocOptStage;
  63     }
  64 
  65     /**
  66      * {@link AllocationPhase}s are responsible for register allocation and translating
  67      * {@link VirtualStackSlot}s into {@link StackSlot}s.
  68      * <p>
  69      * After the {@link AllocationStage} there should be no more {@link Variable}s and
  70      * {@link VirtualStackSlot}s.
  71      */
  72     public LIRPhaseSuite<AllocationContext> getAllocationStage() {
  73         return allocStage;
  74     }
  75 
  76     /**
  77      * {@link PostAllocationOptimizationPhase}s are executed after register allocation and before
  78      * machine code generation.
  79      * <p>
  80      * A {@link PostAllocationOptimizationPhase} must not introduce new {@link Variable}s,
  81      * {@link VirtualStackSlot}s or {@link StackSlot}s. Blocks might be removed from
  82      * {@link LIR#codeEmittingOrder()} by overwriting them with {@code null}.
  83      */
  84     public LIRPhaseSuite<PostAllocationOptimizationContext> getPostAllocationOptimizationStage() {
  85         return postAllocStage;
  86     }
  87 
  88     public boolean isImmutable() {
  89         return immutable;
  90     }
  91 
  92     public synchronized void setImmutable() {
  93         if (!immutable) {
  94             preAllocOptStage.setImmutable();
  95             allocStage.setImmutable();
  96             postAllocStage.setImmutable();
  97             immutable = true;
  98         }
  99     }
 100 
 101     public LIRSuites copy() {
 102         return new LIRSuites(preAllocOptStage.copy(), allocStage.copy(), postAllocStage.copy());
 103     }
 104 }