1 /*
   2  * Copyright (c) 2012, 2012, 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.phases;
  24 
  25 import java.util.Iterator;
  26 import java.util.List;
  27 
  28 import org.graalvm.compiler.debug.Debug;
  29 import org.graalvm.compiler.debug.DebugCounter;
  30 import org.graalvm.compiler.graph.Node;
  31 import org.graalvm.compiler.loop.LoopEx;
  32 import org.graalvm.compiler.loop.LoopPolicies;
  33 import org.graalvm.compiler.loop.LoopsData;
  34 import org.graalvm.compiler.nodes.AbstractBeginNode;
  35 import org.graalvm.compiler.nodes.ControlSplitNode;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 
  38 public class LoopUnswitchingPhase extends ContextlessLoopPhase<LoopPolicies> {
  39     private static final DebugCounter UNSWITCHED = Debug.counter("Unswitched");
  40     private static final DebugCounter UNSWITCH_CANDIDATES = Debug.counter("UnswitchCandidates");
  41     private static final DebugCounter UNSWITCH_EARLY_REJECTS = Debug.counter("UnswitchEarlyRejects");
  42 
  43     public LoopUnswitchingPhase(LoopPolicies policies) {
  44         super(policies);
  45     }
  46 
  47     @Override
  48     protected void run(StructuredGraph graph) {
  49         if (graph.hasLoops()) {
  50             boolean unswitched;
  51             do {
  52                 unswitched = false;
  53                 final LoopsData dataUnswitch = new LoopsData(graph);
  54                 for (LoopEx loop : dataUnswitch.outerFirst()) {
  55                     if (getPolicies().shouldTryUnswitch(loop)) {
  56                         List<ControlSplitNode> controlSplits = LoopTransformations.findUnswitchable(loop);
  57                         if (controlSplits != null) {
  58                             UNSWITCH_CANDIDATES.increment();
  59                             if (getPolicies().shouldUnswitch(loop, controlSplits)) {
  60                                 if (Debug.isLogEnabled()) {
  61                                     logUnswitch(loop, controlSplits);
  62                                 }
  63                                 LoopTransformations.unswitch(loop, controlSplits);
  64                                 Debug.dump(Debug.INFO_LOG_LEVEL, graph, "After unswitch %s", controlSplits);
  65                                 UNSWITCHED.increment();
  66                                 unswitched = true;
  67                                 break;
  68                             }
  69                         }
  70                     } else {
  71                         UNSWITCH_EARLY_REJECTS.increment();
  72                     }
  73                 }
  74             } while (unswitched);
  75         }
  76     }
  77 
  78     private static void logUnswitch(LoopEx loop, List<ControlSplitNode> controlSplits) {
  79         StringBuilder sb = new StringBuilder("Unswitching ");
  80         sb.append(loop).append(" at ");
  81         for (ControlSplitNode controlSplit : controlSplits) {
  82             sb.append(controlSplit).append(" [");
  83             Iterator<Node> it = controlSplit.successors().iterator();
  84             while (it.hasNext()) {
  85                 sb.append(controlSplit.probability((AbstractBeginNode) it.next()));
  86                 if (it.hasNext()) {
  87                     sb.append(", ");
  88                 }
  89             }
  90             sb.append("]");
  91         }
  92         Debug.log("%s", sb);
  93     }
  94 
  95     @Override
  96     public float codeSizeIncrease() {
  97         return 10.0f;
  98     }
  99 }