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 true.  The parent composite phase will
043       * dictate.
044       * @param options controlling compiler options
045       */
046      public boolean shouldPerform(OptOptions options) {
047        return true;
048      }
049    
050      /**
051       * Return a string representation of this phase
052       * @return "Post-Dominators"
053       */
054      public String getName() {
055        return "Post-Dominators";
056      }
057    
058      /**
059       * Should the IR be printed before and/or after this phase?
060       * @param options controlling compiler options
061       * @param before query control
062       * @return true or false
063       */
064      public boolean printingEnabled(OptOptions options, boolean before) {
065        return false;
066      }
067    
068      /**
069       * Main driver for the post-dominator calculation.
070       */
071      public void perform(IR ir) {
072        try {
073          // reset flags in case an exception is thrown inside "perform"
074          // and it doesn't return normally
075          ir.HIRInfo.postDominatorsAreComputed = false;
076    
077          // compute post-dominators,
078          // leaves info in scratch object of basic blocks
079          LTDominators.perform(ir, false, unfactor);
080    
081          // create the dominator tree, relies on dominator info being
082          // in scratch object of basic blocks
083          DominatorTree.perform(ir, false);
084    
085          // computation completed, so set flag
086          ir.HIRInfo.postDominatorsAreComputed = true;
087    
088        } catch (OperationNotImplementedException e) {
089          OptOptions options = ir.options;
090          if (options.PRINT_POST_DOMINATORS) {
091            OptimizingCompiler.report(e.getMessage());
092          }
093        }
094      }
095    }