1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.lir.profiling;
  24 
  25 import java.util.ArrayList;
  26 
  27 import org.graalvm.compiler.core.common.LIRKind;
  28 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  29 import org.graalvm.compiler.lir.ConstantValue;
  30 import org.graalvm.compiler.lir.LIR;
  31 import org.graalvm.compiler.lir.LIRInsertionBuffer;
  32 import org.graalvm.compiler.lir.LIRInstruction;
  33 import org.graalvm.compiler.lir.StandardOp.BlockEndOp;
  34 import org.graalvm.compiler.lir.StandardOp.LabelOp;
  35 import org.graalvm.compiler.lir.gen.DiagnosticLIRGeneratorTool;
  36 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  37 import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase;
  38 
  39 import jdk.vm.ci.code.TargetDescription;
  40 import jdk.vm.ci.meta.JavaConstant;
  41 import jdk.vm.ci.meta.JavaKind;
  42 
  43 public class MethodProfilingPhase extends PostAllocationOptimizationPhase {
  44     public static final String INVOCATION_GROUP = "METHOD_INVOCATION_COUNTER";
  45     public static final String ITERATION_GROUP = "METHOD_ITERATION_COUNTER";
  46 
  47     @Override
  48     protected void run(TargetDescription target, LIRGenerationResult lirGenRes, PostAllocationOptimizationContext context) {
  49         new Analyzer(target, lirGenRes.getCompilationUnitName(), lirGenRes.getLIR(), context.diagnosticLirGenTool).run();
  50     }
  51 
  52     private class Analyzer {
  53         private final LIR lir;
  54         private final DiagnosticLIRGeneratorTool diagnosticLirGenTool;
  55         private final LIRInsertionBuffer buffer;
  56         private final String compilationUnitName;
  57         private final ConstantValue increment;
  58 
  59         Analyzer(TargetDescription target, String compilationUnitName, LIR lir, DiagnosticLIRGeneratorTool diagnosticLirGenTool) {
  60             this.lir = lir;
  61             this.compilationUnitName = compilationUnitName;
  62             this.diagnosticLirGenTool = diagnosticLirGenTool;
  63             this.buffer = new LIRInsertionBuffer();
  64             this.increment = new ConstantValue(LIRKind.fromJavaKind(target.arch, JavaKind.Int), JavaConstant.INT_1);
  65         }
  66 
  67         public void run() {
  68             // insert counter at method entry
  69             doBlock(lir.getControlFlowGraph().getStartBlock(), INVOCATION_GROUP);
  70             for (AbstractBlockBase<?> block : lir.getControlFlowGraph().getBlocks()) {
  71                 if (block.isLoopHeader()) {
  72                     // insert counter at loop header
  73                     doBlock(block, ITERATION_GROUP);
  74                 }
  75             }
  76         }
  77 
  78         public void doBlock(AbstractBlockBase<?> block, String group) {
  79             ArrayList<LIRInstruction> instructions = lir.getLIRforBlock(block);
  80             assert instructions.size() >= 2 : "Malformed block: " + block + ", " + instructions;
  81             assert instructions.get(instructions.size() - 1) instanceof BlockEndOp : "Not a BlockEndOp: " + instructions.get(instructions.size() - 1);
  82             assert !(instructions.get(instructions.size() - 2) instanceof BlockEndOp) : "Is a BlockEndOp: " + instructions.get(instructions.size() - 2);
  83             assert instructions.get(0) instanceof LabelOp : "Not a LabelOp: " + instructions.get(0);
  84             assert !(instructions.get(1) instanceof LabelOp) : "Is a LabelOp: " + instructions.get(1);
  85 
  86             LIRInstruction op = diagnosticLirGenTool.createBenchmarkCounter(compilationUnitName, group, increment);
  87             buffer.init(instructions);
  88             buffer.append(1, op);
  89             buffer.finish();
  90         }
  91     }
  92 
  93 }