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.controlflow;
014    
015    import org.jikesrvm.compilers.opt.OperationNotImplementedException;
016    import org.jikesrvm.compilers.opt.OptOptions;
017    import org.jikesrvm.compilers.opt.driver.CompilerPhase;
018    import org.jikesrvm.compilers.opt.driver.OptimizingCompiler;
019    import org.jikesrvm.compilers.opt.ir.IR;
020    
021    /**
022     * Driver routine for post-dominator computation.  This phase invokes
023     * the Lengauer-Tarjan dominator calculation.
024     */
025    public final class PostDominatorsPhase extends CompilerPhase {
026    
027      /**
028       * Should we unfactor the CFG?
029       */
030      private final boolean unfactor;
031    
032      /**
033       * @param unfactor Should we unfactor the CFG before computing
034       * dominators?
035       */
036      public PostDominatorsPhase(boolean unfactor) {
037        this.unfactor = unfactor;
038      }
039    
040      /**
041       * Should this phase be performed?  This is a member of a composite
042       * phase, so always return {@code true}.  The parent composite phase will
043       * dictate.
044       * @param options controlling compiler options
045       */
046      @Override
047      public boolean shouldPerform(OptOptions options) {
048        return true;
049      }
050    
051      /**
052       * Return a string representation of this phase
053       * @return "Post-Dominators"
054       */
055      @Override
056      public String getName() {
057        return "Post-Dominators";
058      }
059    
060      /**
061       * Should the IR be printed before and/or after this phase?
062       * @param options controlling compiler options
063       * @param before query control
064       * @return {@code false}
065       */
066      @Override
067      public boolean printingEnabled(OptOptions options, boolean before) {
068        return false;
069      }
070    
071      /**
072       * Main driver for the post-dominator calculation.
073       */
074      @Override
075      public void perform(IR ir) {
076        try {
077          // reset flags in case an exception is thrown inside "perform"
078          // and it doesn't return normally
079          ir.HIRInfo.postDominatorsAreComputed = false;
080    
081          // compute post-dominators,
082          // leaves info in scratch object of basic blocks
083          LTDominators.perform(ir, false, unfactor);
084    
085          // create the dominator tree, relies on dominator info being
086          // in scratch object of basic blocks
087          DominatorTree.perform(ir, false);
088    
089          // computation completed, so set flag
090          ir.HIRInfo.postDominatorsAreComputed = true;
091    
092        } catch (OperationNotImplementedException e) {
093          OptOptions options = ir.options;
094          if (options.PRINT_POST_DOMINATORS) {
095            OptimizingCompiler.report(e.getMessage());
096          }
097        }
098      }
099    }