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 */
013package org.jikesrvm.compilers.opt.ssa;
014
015import java.lang.reflect.Constructor;
016
017import org.jikesrvm.compilers.opt.DefUse;
018import org.jikesrvm.compilers.opt.ExpressionFolding;
019import org.jikesrvm.compilers.opt.OptOptions;
020import org.jikesrvm.compilers.opt.Simple;
021import org.jikesrvm.compilers.opt.controlflow.DominanceFrontier;
022import org.jikesrvm.compilers.opt.controlflow.DominatorsPhase;
023import org.jikesrvm.compilers.opt.driver.CompilerPhase;
024import org.jikesrvm.compilers.opt.driver.OptimizationPlanAtomicElement;
025import org.jikesrvm.compilers.opt.driver.OptimizationPlanCompositeElement;
026import org.jikesrvm.compilers.opt.driver.OptimizationPlanElement;
027import 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 */
033public 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  @Override
051  public boolean shouldPerform(OptOptions options) {
052    return options.SSA;
053  }
054
055  /**
056   * This class drives expression folding.
057   */
058  private static class FoldingDriver extends CompilerPhase {
059
060    /**
061     * Return this instance of this phase. This phase contains no
062     * per-compilation instance fields.
063     * @param ir not used
064     * @return this
065     */
066    @Override
067    public CompilerPhase newExecution(IR ir) {
068      return this;
069    }
070
071    @Override
072    public final boolean shouldPerform(OptOptions options) {
073      return options.SSA && options.SSA_EXPRESSION_FOLDING;
074    }
075
076    @Override
077    public final String getName() {
078      return "SSA Expression Folding";
079    }
080
081    /**
082     * Execute expression folding.
083     */
084    @Override
085    public final void perform(IR ir) {
086      DefUse.computeDU(ir);
087      ExpressionFolding.perform(ir);
088    }
089  }
090
091  /**
092   * This class sets up the IR state prior to entering SSA.
093   */
094  public static class TuneUpPreparation extends CompilerPhase {
095
096    /**
097     * Compiler phases necessary to re-build dominators and dominance
098     * frontier
099     */
100    private final CompilerPhase dominators, frontier;
101
102    public TuneUpPreparation() {
103      dominators = new DominatorsPhase(true);
104      frontier = new DominanceFrontier();
105    }
106
107    /**
108     * Constructor for this compiler phase
109     */
110    private static final Constructor<CompilerPhase> constructor =
111        getCompilerPhaseConstructor(TuneUpPreparation.class);
112
113    /**
114     * Get a constructor object for this compiler phase
115     * @return compiler phase constructor
116     */
117    @Override
118    public Constructor<CompilerPhase> getClassConstructor() {
119      return constructor;
120    }
121
122    @Override
123    public final boolean shouldPerform(OptOptions options) {
124      return options.SSA;
125    }
126
127    @Override
128    public final String getName() {
129      return "SSA Tune UpPreparation";
130    }
131
132    /**
133     * register in the IR the SSA properties we need for simple scalar
134     * optimizations
135     */
136    @Override
137    public final void perform(IR ir) {
138      ir.desiredSSAOptions = new SSAOptions();
139      ir.desiredSSAOptions.setScalarsOnly(true);
140      ir.desiredSSAOptions.setBackwards(false);
141      ir.desiredSSAOptions.setInsertUsePhis(false);
142      if (!ir.HIRInfo.dominatorsAreComputed) {
143        dominators.perform(ir);
144        frontier.perform(ir);
145      }
146    }
147  }
148}