001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.jikesrvm.compilers.opt.ssa;
014    
015    import java.lang.reflect.Constructor;
016    
017    import org.jikesrvm.compilers.opt.DefUse;
018    import org.jikesrvm.compilers.opt.ExpressionFolding;
019    import org.jikesrvm.compilers.opt.OptOptions;
020    import org.jikesrvm.compilers.opt.Simple;
021    import org.jikesrvm.compilers.opt.controlflow.DominanceFrontier;
022    import org.jikesrvm.compilers.opt.controlflow.DominatorsPhase;
023    import org.jikesrvm.compilers.opt.driver.CompilerPhase;
024    import org.jikesrvm.compilers.opt.driver.OptimizationPlanAtomicElement;
025    import org.jikesrvm.compilers.opt.driver.OptimizationPlanCompositeElement;
026    import org.jikesrvm.compilers.opt.driver.OptimizationPlanElement;
027    import org.jikesrvm.compilers.opt.ir.IR;
028    
029    /**
030     * This phase puts the IR in SSA form and performs a set of simple
031     * optimizations to clean up.
032     */
033    public final class SSATuneUp extends OptimizationPlanCompositeElement {
034    
035      /**
036       * Build this phase as a composite of others.
037       */
038      public SSATuneUp() {
039        super("SSA Tune Up", new OptimizationPlanElement[]{
040            // 1. Set up IR state to control SSA translation as needed
041            new OptimizationPlanAtomicElement(new TuneUpPreparation()),
042            // 2. Get the desired SSA form
043            new OptimizationPlanAtomicElement(new EnterSSA()),
044            // 3. Perform simple optimizations
045            new OptimizationPlanAtomicElement(new Simple(1, true, true, false, false)),
046            // 4. Perform expression simplification
047            new OptimizationPlanAtomicElement(new FoldingDriver())});
048      }
049    
050      public boolean shouldPerform(OptOptions options) {
051        return options.SSA;
052      }
053    
054      /**
055       * This class drives expression folding.
056       */
057      private static class FoldingDriver extends CompilerPhase {
058    
059        /**
060         * Return this instance of this phase. This phase contains no
061         * per-compilation instance fields.
062         * @param ir not used
063         * @return this
064         */
065        public CompilerPhase newExecution(IR ir) {
066          return this;
067        }
068    
069        public final boolean shouldPerform(OptOptions options) {
070          return options.SSA && options.SSA_EXPRESSION_FOLDING;
071        }
072    
073        public final String getName() {
074          return "SSA Expression Folding";
075        }
076    
077        /**
078         * Execute expression folding.
079         */
080        public final void perform(IR ir) {
081          DefUse.computeDU(ir);
082          ExpressionFolding.perform(ir);
083        }
084      }
085    
086      /**
087       * This class sets up the IR state prior to entering SSA.
088       */
089      public static class TuneUpPreparation extends CompilerPhase {
090    
091        /**
092         * Compiler phases necessary to re-build dominators and dominance
093         * frontier
094         */
095        private final CompilerPhase dominators, frontier;
096    
097        public TuneUpPreparation() {
098          dominators = new DominatorsPhase(true);
099          frontier = new DominanceFrontier();
100        }
101    
102        /**
103         * Constructor for this compiler phase
104         */
105        private static final Constructor<CompilerPhase> constructor =
106            getCompilerPhaseConstructor(TuneUpPreparation.class);
107    
108        /**
109         * Get a constructor object for this compiler phase
110         * @return compiler phase constructor
111         */
112        public Constructor<CompilerPhase> getClassConstructor() {
113          return constructor;
114        }
115    
116        public final boolean shouldPerform(OptOptions options) {
117          return options.SSA;
118        }
119    
120        public final String getName() {
121          return "SSA Tune UpPreparation";
122        }
123    
124        /**
125         * register in the IR the SSA properties we need for simple scalar
126         * optimizations
127         */
128        public final void perform(IR ir) {
129          ir.desiredSSAOptions = new SSAOptions();
130          ir.desiredSSAOptions.setScalarsOnly(true);
131          ir.desiredSSAOptions.setBackwards(false);
132          ir.desiredSSAOptions.setInsertUsePhis(false);
133          if (!ir.HIRInfo.dominatorsAreComputed) {
134            dominators.perform(ir);
135            frontier.perform(ir);
136          }
137        }
138      }
139    }