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.classloader.NormalMethod;
016import org.jikesrvm.classloader.TypeReference;
017import org.jikesrvm.compilers.opt.OptOptions;
018import org.jikesrvm.compilers.opt.inlining.DefaultInlineOracle;
019import org.jikesrvm.compilers.opt.inlining.InlineOracle;
020import org.jikesrvm.compilers.opt.ir.IR;
021
022/*
023 * An instance of this class acts instructs the optimizing
024 * compiler how to compile the specified method.
025 */
026public final class CompilationPlan {
027  /**
028   * The method to be compiled.
029   */
030  public final NormalMethod method;
031
032  public NormalMethod getMethod() {
033    return method;
034  }
035
036  /**
037   * The specialized parameters to use in place of those defined in method.
038   */
039  public final TypeReference[] params;
040
041  /**
042   * The OptimizationPlanElements to be invoked during compilation.
043   */
044  public final OptimizationPlanElement[] optimizationPlan;
045  /**
046   * The instrumentation plan for the method.
047   */
048  public final InstrumentationPlan instrumentationPlan;
049  /**
050   * The oracle to be consulted for all inlining decisions.
051   */
052  public InlineOracle inlinePlan;
053  /**
054   * The Options object that contains misc compilation control data
055   */
056  public final OptOptions options;
057
058  /**
059   * Whether this compilation is for analysis only?
060   */
061  public boolean analyzeOnly;
062
063  public boolean irGeneration;
064
065  /**
066   * Construct a compilation plan
067   *
068   * @param m    The NormalMethod representing the source method to be compiled
069   * @param pms  The specialized parameters to use in place of those defined in method
070   * @param op   The optimization plan to be executed on m
071   * @param mp   The instrumentation plan to be executed on m
072   * @param opts The Options to be used for compiling m
073   */
074  public CompilationPlan(NormalMethod m, TypeReference[] pms, OptimizationPlanElement[] op, InstrumentationPlan mp,
075                             OptOptions opts) {
076    method = m;
077    params = pms;
078    inlinePlan = new DefaultInlineOracle();
079    optimizationPlan = op;
080    instrumentationPlan = mp;
081    options = opts;
082  }
083
084  /**
085   * Construct a compilation plan
086   *
087   * @param m    The NormalMethod representing the source method to be compiled
088   * @param op   The optimization plan to be executed on m
089   * @param mp   The instrumentation plan to be executed on m
090   * @param opts The Options to be used for compiling m
091   */
092  public CompilationPlan(NormalMethod m, OptimizationPlanElement[] op, InstrumentationPlan mp,
093                             OptOptions opts) {
094    this(m, null, op, mp, opts);
095  }
096
097  /**
098   * Construct a compilation plan
099   * @param m    The NormalMethod representing the source method to be compiled
100   * @param op   A single optimization pass to execute on m
101   * @param mp   The instrumentation plan to be executed on m
102   * @param opts The Options to be used for compiling m
103   */
104  public CompilationPlan(NormalMethod m, OptimizationPlanElement op, InstrumentationPlan mp,
105                             OptOptions opts) {
106    this(m, new OptimizationPlanElement[]{op}, mp, opts);
107  }
108
109  public void setInlineOracle(InlineOracle o) {
110    inlinePlan = o;
111  }
112
113  /**
114   * Execute a compilation plan by executing each element
115   * in the optimization plan.
116   *
117   * @return the IR created by the execution of the optimization plan
118   */
119  public IR execute() {
120    IR ir = new IR(method, this);
121
122    // If there is instrumentation to perform, do some initialization
123    if (instrumentationPlan != null) {
124      instrumentationPlan.initInstrumentation(method);
125    }
126    for (OptimizationPlanElement element : optimizationPlan) {
127      element.perform(ir);
128    }
129    // If instrumentation has occured, perform some
130    // cleanup/finalization.  NOTE: This code won't execute when
131    // compilation fails with an exception.  TODO: Figure out
132    // whether this matters.
133    if (instrumentationPlan != null) {
134      instrumentationPlan.finalizeInstrumentation(method);
135    }
136
137    return ir;
138  }
139}