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.driver;
014
015import org.jikesrvm.VM;
016import org.jikesrvm.compilers.opt.OptOptions;
017import org.jikesrvm.compilers.opt.ir.IR;
018
019/**
020 * An element in the opt compiler's optimization plan.
021 * <p>
022 * NOTE: Instances of subclasses of this class are
023 *       held in OptimizationPlanner.masterPlan
024 *       and thus represent global state.
025 *       It is therefore incorrect for any per-compilation
026 *       state to be stored in an instance field of
027 *       one of these objects.
028 * <p>
029 * TODO: refactor the optimization plan elements and compiler phases
030 */
031public abstract class OptimizationPlanElement {
032
033  /**
034   * Determine, possibly by consulting the passed options object,
035   * if this optimization plan element should be performed.
036   *
037   * @param options The Options object for the current compilation.
038   * @return {@code true} if the plan element should be performed.
039   */
040  public abstract boolean shouldPerform(OptOptions options);
041
042  /**
043   * Do the work represented by this element in the optimization plan.
044   * The assumption is that the work will modify the IR in some way.
045   *
046   * @param ir The IR object to work with.
047   */
048  public abstract void perform(IR ir);
049
050  /**
051   * @return a String which is the name of the phase.
052   */
053  public abstract String getName();
054
055  /**
056   * This method is called to initialize the optimization plan support
057   *  measuring compilation.
058   */
059  public abstract void initializeForMeasureCompilation();
060
061  /**
062   * Generate (to the sysWrite stream) a report of the
063   * time spent performing this element of the optimization plan.
064   *
065   * @param indent Number of spaces to indent report.
066   * @param timeCol Column number of time portion of report.
067   * @param totalTime Total opt compilation time in seconds.
068   */
069  public abstract void reportStats(int indent, int timeCol, double totalTime);
070
071  /**
072   * Report the elapsed time spent in the PlanElement
073   * @return time spend in the plan (in ms)
074   */
075  public abstract double elapsedTime();
076
077  /**
078   * Helper function for <code> reportStats </code>
079   *
080   * @param time time spent for executing an element
081   * @param totalTime total time spent on executing all the elements
082   */
083  protected void prettyPrintTime(double time, double totalTime) {
084    int t = (int) time;
085    if (t < 1000000) {
086      VM.sysWrite(" ");
087    }
088    if (t < 100000) {
089      VM.sysWrite(" ");
090    }
091    if (t < 10000) {
092      VM.sysWrite(" ");
093    }
094    if (t < 1000) {
095      VM.sysWrite(" ");
096    }
097    if (t < 100) {
098      VM.sysWrite(" ");
099    }
100    if (t < 10) {
101      VM.sysWrite(" ");
102    }
103    VM.sysWrite(t);
104    if (time / totalTime > 0.10) {
105      VM.sysWrite("    ");
106    } else {
107      VM.sysWrite("     ");
108    }
109    VM.sysWrite(time / totalTime * 100, 2);
110    VM.sysWrite("%");
111  }
112}