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.adaptive.recompilation.instrumentation;
014    
015    import org.jikesrvm.adaptive.controller.Controller;
016    import org.jikesrvm.adaptive.database.AOSDatabase;
017    import org.jikesrvm.adaptive.measurements.instrumentation.Instrumentation;
018    import org.jikesrvm.adaptive.measurements.instrumentation.MethodInvocationCounterData;
019    import org.jikesrvm.compilers.opt.OptOptions;
020    import org.jikesrvm.compilers.opt.driver.CompilerPhase;
021    import org.jikesrvm.compilers.opt.ir.BasicBlock;
022    import org.jikesrvm.compilers.opt.ir.IR;
023    import 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     */
037    public 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() { return "InsertMethodInvocationCounters"; }
057    
058      /**
059       * Insert basic block counters
060       *
061       * @param ir the governing IR
062       */
063      @Override
064      public final void perform(IR ir) {
065        // Don't insert counters in uninterruptible or
066        // save volatile methods, or when instrumentation is disabled
067        if (!ir.method.isInterruptible() ||
068            !Instrumentation.instrumentationEnabled() ||
069            ir.method.getDeclaringClass().hasSaveVolatileAnnotation()) {
070          return;
071        }
072    
073        BasicBlock firstBB = ir.cfg.entry();
074    
075        MethodInvocationCounterData data = AOSDatabase.methodInvocationCounterData;
076    
077        int cmid = ir.compiledMethod.getId();
078    
079        // Create a dummy instruction that is later converted into an
080        // increment of the appropriate CounterArray element.
081        Instruction c = data.createEventCounterInstruction(cmid);
082    
083        // Insert it at the beginning of the basic block
084        firstBB.prependInstructionRespectingPrologue(c);
085      }
086    }