< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.phases.common/src/org/graalvm/compiler/phases/common/ProfileCompiledMethodsPhase.java

Print this page
rev 52509 : [mq]: graal


  81  *
  82  * Additionally, there's a second counter that's only increased for code sections without invokes.
  83  */
  84 public class ProfileCompiledMethodsPhase extends Phase {
  85 
  86     private static final String GROUP_NAME = "~profiled weight";
  87     private static final String GROUP_NAME_WITHOUT = "~profiled weight (invoke-free sections)";
  88     private static final String GROUP_NAME_INVOKES = "~profiled invokes";
  89 
  90     private static final boolean WITH_SECTION_HEADER = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_SECTION_HEADER", "false"));
  91     private static final boolean WITH_INVOKE_FREE_SECTIONS = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_FREE_SECTIONS", "false"));
  92     private static final boolean WITH_INVOKES = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_INVOKES", "true"));
  93 
  94     @Override
  95     protected void run(StructuredGraph graph) {
  96         SchedulePhase schedule = new SchedulePhase(graph.getOptions());
  97         schedule.apply(graph, false);
  98 
  99         ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, true, true);
 100         for (Loop<Block> loop : cfg.getLoops()) {
 101             double loopProbability = cfg.blockFor(loop.getHeader().getBeginNode()).probability();
 102             if (loopProbability > (1D / Integer.MAX_VALUE)) {
 103                 addSectionCounters(loop.getHeader().getBeginNode(), loop.getBlocks(), loop.getChildren(), graph.getLastSchedule(), cfg);
 104             }
 105         }
 106         // don't put the counter increase directly after the start (problems with OSR)
 107         FixedWithNextNode current = graph.start();
 108         while (current.next() instanceof FixedWithNextNode) {
 109             current = (FixedWithNextNode) current.next();
 110         }
 111         addSectionCounters(current, Arrays.asList(cfg.getBlocks()), cfg.getLoops(), graph.getLastSchedule(), cfg);
 112 
 113         if (WITH_INVOKES) {
 114             for (Node node : graph.getNodes()) {
 115                 if (node instanceof Invoke) {
 116                     Invoke invoke = (Invoke) node;
 117                     DynamicCounterNode.addCounterBefore(GROUP_NAME_INVOKES, invoke.callTarget().targetName(), 1, true, invoke.asNode());
 118 
 119                 }
 120             }
 121         }
 122     }
 123 
 124     private static void addSectionCounters(FixedWithNextNode start, Collection<Block> sectionBlocks, Collection<Loop<Block>> childLoops, ScheduleResult schedule, ControlFlowGraph cfg) {
 125         HashSet<Block> blocks = new HashSet<>(sectionBlocks);
 126         for (Loop<Block> loop : childLoops) {
 127             blocks.removeAll(loop.getBlocks());
 128         }
 129         double weight = getSectionWeight(schedule, blocks) / cfg.blockFor(start).probability();
 130         DynamicCounterNode.addCounterBefore(GROUP_NAME, sectionHead(start), (long) weight, true, start.next());
 131         if (WITH_INVOKE_FREE_SECTIONS && !hasInvoke(blocks)) {
 132             DynamicCounterNode.addCounterBefore(GROUP_NAME_WITHOUT, sectionHead(start), (long) weight, true, start.next());
 133         }
 134     }
 135 
 136     private static String sectionHead(Node node) {
 137         if (WITH_SECTION_HEADER) {
 138             return node.toString();
 139         } else {
 140             return "";
 141         }
 142     }
 143 
 144     private static double getSectionWeight(ScheduleResult schedule, Collection<Block> blocks) {
 145         double count = 0;
 146         for (Block block : blocks) {
 147             double blockProbability = block.probability();
 148             for (Node node : schedule.getBlockToNodesMap().get(block)) {
 149                 count += blockProbability * getNodeWeight(node);
 150             }
 151         }
 152         return count;
 153     }
 154 
 155     private static double getNodeWeight(Node node) {
 156         if (node instanceof AbstractMergeNode) {
 157             return ((AbstractMergeNode) node).phiPredecessorCount();
 158         } else if (node instanceof AbstractBeginNode || node instanceof AbstractEndNode || node instanceof MonitorIdNode || node instanceof ConstantNode || node instanceof ParameterNode ||
 159                         node instanceof CallTargetNode || node instanceof ValueProxy || node instanceof VirtualObjectNode || node instanceof ReinterpretNode) {
 160             return 0;
 161         } else if (node instanceof AccessMonitorNode) {
 162             return 10;
 163         } else if (node instanceof Access) {
 164             return 2;
 165         } else if (node instanceof LogicNode || node instanceof ConvertNode || node instanceof NotNode) {
 166             return 1;
 167         } else if (node instanceof IntegerDivRemNode || node instanceof FloatDivNode || node instanceof RemNode) {




  81  *
  82  * Additionally, there's a second counter that's only increased for code sections without invokes.
  83  */
  84 public class ProfileCompiledMethodsPhase extends Phase {
  85 
  86     private static final String GROUP_NAME = "~profiled weight";
  87     private static final String GROUP_NAME_WITHOUT = "~profiled weight (invoke-free sections)";
  88     private static final String GROUP_NAME_INVOKES = "~profiled invokes";
  89 
  90     private static final boolean WITH_SECTION_HEADER = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_SECTION_HEADER", "false"));
  91     private static final boolean WITH_INVOKE_FREE_SECTIONS = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_FREE_SECTIONS", "false"));
  92     private static final boolean WITH_INVOKES = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_INVOKES", "true"));
  93 
  94     @Override
  95     protected void run(StructuredGraph graph) {
  96         SchedulePhase schedule = new SchedulePhase(graph.getOptions());
  97         schedule.apply(graph, false);
  98 
  99         ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, true, true);
 100         for (Loop<Block> loop : cfg.getLoops()) {
 101             double loopProbability = cfg.blockFor(loop.getHeader().getBeginNode()).getRelativeFrequency();
 102             if (loopProbability > (1D / Integer.MAX_VALUE)) {
 103                 addSectionCounters(loop.getHeader().getBeginNode(), loop.getBlocks(), loop.getChildren(), graph.getLastSchedule(), cfg);
 104             }
 105         }
 106         // don't put the counter increase directly after the start (problems with OSR)
 107         FixedWithNextNode current = graph.start();
 108         while (current.next() instanceof FixedWithNextNode) {
 109             current = (FixedWithNextNode) current.next();
 110         }
 111         addSectionCounters(current, Arrays.asList(cfg.getBlocks()), cfg.getLoops(), graph.getLastSchedule(), cfg);
 112 
 113         if (WITH_INVOKES) {
 114             for (Node node : graph.getNodes()) {
 115                 if (node instanceof Invoke) {
 116                     Invoke invoke = (Invoke) node;
 117                     DynamicCounterNode.addCounterBefore(GROUP_NAME_INVOKES, invoke.callTarget().targetName(), 1, true, invoke.asNode());
 118 
 119                 }
 120             }
 121         }
 122     }
 123 
 124     private static void addSectionCounters(FixedWithNextNode start, Collection<Block> sectionBlocks, Collection<Loop<Block>> childLoops, ScheduleResult schedule, ControlFlowGraph cfg) {
 125         HashSet<Block> blocks = new HashSet<>(sectionBlocks);
 126         for (Loop<Block> loop : childLoops) {
 127             blocks.removeAll(loop.getBlocks());
 128         }
 129         long increment = DynamicCounterNode.clampIncrement((long) (getSectionWeight(schedule, blocks) / cfg.blockFor(start).getRelativeFrequency()));
 130         DynamicCounterNode.addCounterBefore(GROUP_NAME, sectionHead(start), increment, true, start.next());
 131         if (WITH_INVOKE_FREE_SECTIONS && !hasInvoke(blocks)) {
 132             DynamicCounterNode.addCounterBefore(GROUP_NAME_WITHOUT, sectionHead(start), increment, true, start.next());
 133         }
 134     }
 135 
 136     private static String sectionHead(Node node) {
 137         if (WITH_SECTION_HEADER) {
 138             return node.toString();
 139         } else {
 140             return "";
 141         }
 142     }
 143 
 144     private static double getSectionWeight(ScheduleResult schedule, Collection<Block> blocks) {
 145         double count = 0;
 146         for (Block block : blocks) {
 147             double blockProbability = block.getRelativeFrequency();
 148             for (Node node : schedule.getBlockToNodesMap().get(block)) {
 149                 count += blockProbability * getNodeWeight(node);
 150             }
 151         }
 152         return count;
 153     }
 154 
 155     private static double getNodeWeight(Node node) {
 156         if (node instanceof AbstractMergeNode) {
 157             return ((AbstractMergeNode) node).phiPredecessorCount();
 158         } else if (node instanceof AbstractBeginNode || node instanceof AbstractEndNode || node instanceof MonitorIdNode || node instanceof ConstantNode || node instanceof ParameterNode ||
 159                         node instanceof CallTargetNode || node instanceof ValueProxy || node instanceof VirtualObjectNode || node instanceof ReinterpretNode) {
 160             return 0;
 161         } else if (node instanceof AccessMonitorNode) {
 162             return 10;
 163         } else if (node instanceof Access) {
 164             return 2;
 165         } else if (node instanceof LogicNode || node instanceof ConvertNode || node instanceof NotNode) {
 166             return 1;
 167         } else if (node instanceof IntegerDivRemNode || node instanceof FloatDivNode || node instanceof RemNode) {


< prev index next >