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.adaptive.recompilation.instrumentation;
014
015import org.jikesrvm.adaptive.controller.Controller;
016import org.jikesrvm.adaptive.database.AOSDatabase;
017import org.jikesrvm.adaptive.measurements.instrumentation.Instrumentation;
018import org.jikesrvm.adaptive.measurements.instrumentation.MethodInvocationCounterData;
019import org.jikesrvm.compilers.opt.OptOptions;
020import org.jikesrvm.compilers.opt.driver.CompilerPhase;
021import org.jikesrvm.compilers.opt.ir.BasicBlock;
022import org.jikesrvm.compilers.opt.ir.IR;
023import org.jikesrvm.compilers.opt.ir.Instruction;
024
025/**
026 * An CompilerPhase that inserts a method invocation counter on the first
027 * basic block of the method.  It uses a
028 * InstrumentedEventCounterManager to obtain the space to put the
029 * counters.
030 * <p>
031 * Note: one counter data, (MethodInvocationCounterData) is shared
032 * across all methods, and is initialized at boot time.  This is
033 * unlike other kinds of instrumentation (such as basic block
034 * counters) where a separate data object is maintained for each
035 * method.
036 */
037public class InsertMethodInvocationCounter extends CompilerPhase {
038
039  /**
040   * Return this instance of this phase. This phase contains no
041   * per-compilation instance fields.
042   * @param ir not used
043   * @return this
044   */
045  @Override
046  public CompilerPhase newExecution(IR ir) {
047    return this;
048  }
049
050  @Override
051  public final boolean shouldPerform(OptOptions options) {
052    return Controller.options.INSERT_METHOD_COUNTERS_OPT;
053  }
054
055  @Override
056  public final String getName() {
057    return "InsertMethodInvocationCounters";
058  }
059
060  /**
061   * Insert basic block counters
062   *
063   * @param ir the governing IR
064   */
065  @Override
066  public final void perform(IR ir) {
067    // Don't insert counters in uninterruptible or
068    // save volatile methods, or when instrumentation is disabled
069    if (!ir.method.isInterruptible() ||
070        !Instrumentation.instrumentationEnabled() ||
071        ir.method.getDeclaringClass().hasSaveVolatileAnnotation()) {
072      return;
073    }
074
075    BasicBlock firstBB = ir.cfg.entry();
076
077    MethodInvocationCounterData data = AOSDatabase.methodInvocationCounterData;
078
079    int cmid = ir.compiledMethod.getId();
080
081    // Create a dummy instruction that is later converted into an
082    // increment of the appropriate CounterArray element.
083    Instruction c = data.createEventCounterInstruction(cmid);
084
085    // Insert it at the beginning of the basic block
086    firstBB.prependInstructionRespectingPrologue(c);
087  }
088}