1 /*
   2  * Copyright (c) 2012, 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.loop;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.LoopMaxUnswitch;
  26 import static org.graalvm.compiler.core.common.GraalOptions.MaximumDesiredSize;
  27 import static org.graalvm.compiler.core.common.GraalOptions.MinimumPeelProbability;
  28 
  29 import java.util.List;
  30 
  31 import org.graalvm.compiler.debug.Debug;
  32 import org.graalvm.compiler.debug.DebugCounter;
  33 import org.graalvm.compiler.graph.Node;
  34 import org.graalvm.compiler.graph.NodeBitMap;
  35 import org.graalvm.compiler.nodes.AbstractBeginNode;
  36 import org.graalvm.compiler.nodes.ControlSplitNode;
  37 import org.graalvm.compiler.nodes.DeoptimizeNode;
  38 import org.graalvm.compiler.nodes.FixedNode;
  39 import org.graalvm.compiler.nodes.FixedWithNextNode;
  40 import org.graalvm.compiler.nodes.LoopBeginNode;
  41 import org.graalvm.compiler.nodes.MergeNode;
  42 import org.graalvm.compiler.nodes.VirtualState;
  43 import org.graalvm.compiler.nodes.VirtualState.VirtualClosure;
  44 import org.graalvm.compiler.nodes.cfg.Block;
  45 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
  46 import org.graalvm.compiler.nodes.java.TypeSwitchNode;
  47 import org.graalvm.compiler.options.Option;
  48 import org.graalvm.compiler.options.OptionType;
  49 import org.graalvm.compiler.options.OptionValue;
  50 
  51 import jdk.vm.ci.meta.MetaAccessProvider;
  52 
  53 public class DefaultLoopPolicies implements LoopPolicies {
  54     @Option(help = "", type = OptionType.Expert) public static final OptionValue<Integer> LoopUnswitchMaxIncrease = new OptionValue<>(500);
  55     @Option(help = "", type = OptionType.Expert) public static final OptionValue<Integer> LoopUnswitchTrivial = new OptionValue<>(10);
  56     @Option(help = "", type = OptionType.Expert) public static final OptionValue<Double> LoopUnswitchFrequencyBoost = new OptionValue<>(10.0);
  57 
  58     @Option(help = "", type = OptionType.Expert) public static final OptionValue<Integer> FullUnrollMaxNodes = new OptionValue<>(300);
  59     @Option(help = "", type = OptionType.Expert) public static final OptionValue<Integer> FullUnrollMaxIterations = new OptionValue<>(600);
  60     @Option(help = "", type = OptionType.Expert) public static final OptionValue<Integer> ExactFullUnrollMaxNodes = new OptionValue<>(1200);
  61 
  62     @Override
  63     public boolean shouldPeel(LoopEx loop, ControlFlowGraph cfg, MetaAccessProvider metaAccess) {
  64         LoopBeginNode loopBegin = loop.loopBegin();
  65         double entryProbability = cfg.blockFor(loopBegin.forwardEnd()).probability();
  66         if (entryProbability > MinimumPeelProbability.getValue() && loop.size() + loopBegin.graph().getNodeCount() < MaximumDesiredSize.getValue()) {
  67             // check whether we're allowed to peel this loop
  68             return loop.canDuplicateLoop();
  69         } else {
  70             return false;
  71         }
  72     }
  73 
  74     @Override
  75     public boolean shouldFullUnroll(LoopEx loop) {
  76         if (!loop.isCounted() || !loop.counted().isConstantMaxTripCount()) {
  77             return false;
  78         }
  79         CountedLoopInfo counted = loop.counted();
  80         long maxTrips = counted.constantMaxTripCount();
  81         int maxNodes = (counted.isExactTripCount() && counted.isConstantExactTripCount()) ? ExactFullUnrollMaxNodes.getValue() : FullUnrollMaxNodes.getValue();
  82         maxNodes = Math.min(maxNodes, Math.max(0, MaximumDesiredSize.getValue() - loop.loopBegin().graph().getNodeCount()));
  83         int size = Math.max(1, loop.size() - 1 - loop.loopBegin().phis().count());
  84         if (maxTrips <= FullUnrollMaxIterations.getValue() && size * (maxTrips - 1) <= maxNodes) {
  85             // check whether we're allowed to unroll this loop
  86             return loop.canDuplicateLoop();
  87         } else {
  88             return false;
  89         }
  90     }
  91 
  92     @Override
  93     public boolean shouldTryUnswitch(LoopEx loop) {
  94         LoopBeginNode loopBegin = loop.loopBegin();
  95         double loopFrequency = loopBegin.loopFrequency();
  96         if (loopFrequency <= 1.0) {
  97             return false;
  98         }
  99         return loopBegin.unswitches() <= LoopMaxUnswitch.getValue();
 100     }
 101 
 102     private static final class CountingClosure implements VirtualClosure {
 103         int count;
 104 
 105         @Override
 106         public void apply(VirtualState node) {
 107             count++;
 108         }
 109     }
 110 
 111     private static class IsolatedInitialization {
 112         static final DebugCounter UNSWITCH_SPLIT_WITH_PHIS = Debug.counter("UnswitchSplitWithPhis");
 113     }
 114 
 115     @Override
 116     public boolean shouldUnswitch(LoopEx loop, List<ControlSplitNode> controlSplits) {
 117         int phis = 0;
 118         NodeBitMap branchNodes = loop.loopBegin().graph().createNodeBitMap();
 119         for (ControlSplitNode controlSplit : controlSplits) {
 120             for (Node successor : controlSplit.successors()) {
 121                 AbstractBeginNode branch = (AbstractBeginNode) successor;
 122                 // this may count twice because of fall-through in switches
 123                 loop.nodesInLoopBranch(branchNodes, branch);
 124             }
 125             Block postDomBlock = loop.loopsData().getCFG().blockFor(controlSplit).getPostdominator();
 126             if (postDomBlock != null) {
 127                 IsolatedInitialization.UNSWITCH_SPLIT_WITH_PHIS.increment();
 128                 phis += ((MergeNode) postDomBlock.getBeginNode()).phis().count();
 129             }
 130         }
 131         int inBranchTotal = branchNodes.count();
 132 
 133         CountingClosure stateNodesCount = new CountingClosure();
 134         double loopFrequency = loop.loopBegin().loopFrequency();
 135         int maxDiff = LoopUnswitchTrivial.getValue() + (int) (LoopUnswitchFrequencyBoost.getValue() * (loopFrequency - 1.0 + phis));
 136 
 137         maxDiff = Math.min(maxDiff, LoopUnswitchMaxIncrease.getValue());
 138         int remainingGraphSpace = MaximumDesiredSize.getValue() - loop.loopBegin().graph().getNodeCount();
 139         maxDiff = Math.min(maxDiff, remainingGraphSpace);
 140 
 141         loop.loopBegin().stateAfter().applyToVirtual(stateNodesCount);
 142         int loopTotal = loop.size() - loop.loopBegin().phis().count() - stateNodesCount.count - 1;
 143         int actualDiff = (loopTotal - inBranchTotal);
 144         ControlSplitNode firstSplit = controlSplits.get(0);
 145         if (firstSplit instanceof TypeSwitchNode) {
 146             int copies = firstSplit.successors().count() - 1;
 147             for (Node succ : firstSplit.successors()) {
 148                 FixedNode current = (FixedNode) succ;
 149                 while (current instanceof FixedWithNextNode) {
 150                     current = ((FixedWithNextNode) current).next();
 151                 }
 152                 if (current instanceof DeoptimizeNode) {
 153                     copies--;
 154                 }
 155             }
 156             actualDiff = actualDiff * copies;
 157         }
 158 
 159         Debug.log("shouldUnswitch(%s, %s) : delta=%d (%.2f%% inside of branches), max=%d, f=%.2f, phis=%d -> %b", loop, controlSplits, actualDiff, (double) (inBranchTotal) / loopTotal * 100, maxDiff,
 160                         loopFrequency, phis, actualDiff <= maxDiff);
 161         if (actualDiff <= maxDiff) {
 162             // check whether we're allowed to unswitch this loop
 163             return loop.canDuplicateLoop();
 164         } else {
 165             return false;
 166         }
 167     }
 168 
 169 }