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.List; 26 27 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase; 28 import org.graalvm.compiler.core.common.cfg.BlockMap; 29 import org.graalvm.compiler.lir.LIR; 30 import org.graalvm.compiler.lir.LIRInstruction; 31 import org.graalvm.compiler.lir.StandardOp.BlockEndOp; 32 import org.graalvm.compiler.lir.StandardOp.LabelOp; 33 import org.graalvm.compiler.lir.StandardOp.MoveOp; 34 35 public final class MoveProfiler { 36 37 public static BlockMap<MoveStatistics> profile(LIR lir) { 38 MoveProfiler profiler = new MoveProfiler(lir); 39 profiler.run(); 40 return profiler.blockMap; 41 } 42 43 static class MoveStatistics { 44 45 private final int[] cnt; 46 47 MoveStatistics() { 48 cnt = new int[MoveType.values().length]; 49 50 } 51 52 public void add(MoveType moveType) { 53 cnt[moveType.ordinal()]++; 54 } 55 56 public int get(MoveType moveType) { 57 return cnt[moveType.ordinal()]; 58 } 59 60 public void add(MoveType moveType, int value) { 61 cnt[moveType.ordinal()] += value; 62 } 63 } 64 65 private final LIR lir; 66 private final BlockMap<MoveStatistics> blockMap; 67 68 private MoveProfiler(LIR lir) { 69 this.lir = lir; 70 blockMap = new BlockMap<>(lir.getControlFlowGraph()); 71 } 72 73 private void run() { 74 for (AbstractBlockBase<?> block : lir.getControlFlowGraph().getBlocks()) { 75 doBlock(block); 76 } 77 } 78 79 private void doBlock(AbstractBlockBase<?> block) { 80 List<LIRInstruction> instructions = lir.getLIRforBlock(block); 81 assert instructions.size() >= 2 : "Malformed block: " + block + ", " + instructions; 82 assert instructions.get(instructions.size() - 1) instanceof BlockEndOp : "Not a BlockEndOp: " + instructions.get(instructions.size() - 1); 83 assert !(instructions.get(instructions.size() - 2) instanceof BlockEndOp) : "Is a BlockEndOp: " + instructions.get(instructions.size() - 2); 84 assert instructions.get(0) instanceof LabelOp : "Not a LabelOp: " + instructions.get(0); 85 assert !(instructions.get(1) instanceof LabelOp) : "Is a LabelOp: " + instructions.get(1); 86 87 MoveStatistics stats = null; 88 // analysis phase 89 for (LIRInstruction inst : instructions) { 90 if (inst instanceof MoveOp) { 91 if (stats == null) { 92 stats = new MoveStatistics(); 93 blockMap.put(block, stats); 94 } 95 stats.add(MoveType.get((MoveOp) inst)); 96 } 97 } 98 } 99 100 }