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     *
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      public CompilerPhase newExecution(IR ir) {
046        return this;
047      }
048    
049      public final boolean shouldPerform(OptOptions options) {
050        return Controller.options.INSERT_METHOD_COUNTERS_OPT;
051      }
052    
053      public final String getName() { return "InsertMethodInvocationCounters"; }
054    
055      /**
056       * Insert basic block counters
057       *
058       * @param ir the governing IR
059       */
060      public final void perform(IR ir) {
061        // Don't insert counters in uninterruptible or
062        // save volatile methods, or when instrumentation is disabled
063        if (!ir.method.isInterruptible() ||
064            !Instrumentation.instrumentationEnabled() ||
065            ir.method.getDeclaringClass().hasSaveVolatileAnnotation()) {
066          return;
067        }
068    
069        BasicBlock firstBB = ir.cfg.entry();
070    
071        MethodInvocationCounterData data = AOSDatabase.methodInvocationCounterData;
072    
073        int cmid = ir.compiledMethod.getId();
074    
075        // Create a dummy instruction that is later converted into an
076        // increment of the appropriate CounterArray element.
077        Instruction c = data.createEventCounterInstruction(cmid);
078    
079        // Insert it at the beginning of the basic block
080        firstBB.prependInstructionRespectingPrologue(c);
081      }
082    }