1 /*
   2  * Copyright (c) 2012, 2018, 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 
  24 
  25 package org.graalvm.compiler.loop.phases;
  26 
  27 import org.graalvm.compiler.core.common.GraalOptions;
  28 import org.graalvm.compiler.debug.CounterKey;
  29 import org.graalvm.compiler.debug.DebugContext;
  30 import org.graalvm.compiler.loop.LoopEx;
  31 import org.graalvm.compiler.loop.LoopPolicies;
  32 import org.graalvm.compiler.loop.LoopsData;
  33 import org.graalvm.compiler.nodes.StructuredGraph;
  34 import org.graalvm.compiler.nodes.spi.CoreProviders;
  35 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  36 
  37 public class LoopFullUnrollPhase extends LoopPhase<LoopPolicies> {
  38 
  39     private static final CounterKey FULLY_UNROLLED_LOOPS = DebugContext.counter("FullUnrolls");
  40     private final CanonicalizerPhase canonicalizer;
  41 
  42     public LoopFullUnrollPhase(CanonicalizerPhase canonicalizer, LoopPolicies policies) {
  43         super(policies);
  44         this.canonicalizer = canonicalizer;
  45     }
  46 
  47     @Override
  48     protected void run(StructuredGraph graph, CoreProviders context) {
  49         if (GraalOptions.FullUnroll.getValue(graph.getOptions())) {
  50             DebugContext debug = graph.getDebug();
  51             if (graph.hasLoops()) {
  52                 boolean peeled;
  53                 do {
  54                     peeled = false;
  55                     final LoopsData dataCounted = new LoopsData(graph);
  56                     dataCounted.detectedCountedLoops();
  57                     for (LoopEx loop : dataCounted.countedLoops()) {
  58                         if (getPolicies().shouldFullUnroll(loop)) {
  59                             debug.log("FullUnroll %s", loop);
  60                             LoopTransformations.fullUnroll(loop, context, canonicalizer);
  61                             FULLY_UNROLLED_LOOPS.increment(debug);
  62                             debug.dump(DebugContext.DETAILED_LEVEL, graph, "FullUnroll %s", loop);
  63                             peeled = true;
  64                             break;
  65                         }
  66                     }
  67                     dataCounted.deleteUnusedNodes();
  68                 } while (peeled);
  69             }
  70         }
  71     }
  72 
  73     @Override
  74     public boolean checkContract() {
  75         return false;
  76     }
  77 }