1 /*
   2  * Copyright (c) 2017, 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.core.test;
  26 
  27 import org.graalvm.compiler.debug.DebugContext;
  28 import org.graalvm.compiler.debug.DebugDumpScope;
  29 import org.graalvm.compiler.loop.DefaultLoopPolicies;
  30 import org.graalvm.compiler.loop.phases.LoopFullUnrollPhase;
  31 import org.graalvm.compiler.nodes.LoopBeginNode;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  34 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  35 import org.graalvm.compiler.phases.tiers.PhaseContext;
  36 import org.junit.Test;
  37 
  38 public class LoopFullUnrollTest extends GraalCompilerTest {
  39 
  40     public static int testMinToMax(int input) {
  41         int ret = 2;
  42         int current = input;
  43         for (long i = Long.MIN_VALUE; i < Long.MAX_VALUE; i++) {
  44             ret *= 2 + current;
  45             current /= 50;
  46         }
  47         return ret;
  48     }
  49 
  50     @Test
  51     public void runMinToMax() throws Throwable {
  52         test("testMinToMax", 1);
  53     }
  54 
  55     public static int testMinTo0(int input) {
  56         int ret = 2;
  57         int current = input;
  58         for (long i = Long.MIN_VALUE; i <= 0; i++) {
  59             ret *= 2 + current;
  60             current /= 50;
  61         }
  62         return ret;
  63     }
  64 
  65     @Test
  66     public void runMinTo0() throws Throwable {
  67         test("testMinTo0", 1);
  68     }
  69 
  70     public static int testNegativeTripCount(int input) {
  71         int ret = 2;
  72         int current = input;
  73         for (long i = 0; i <= -20; i++) {
  74             ret *= 2 + current;
  75             current /= 50;
  76         }
  77         return ret;
  78     }
  79 
  80     @Test
  81     public void runNegativeTripCount() throws Throwable {
  82         test("testNegativeTripCount", 0);
  83     }
  84 
  85     @SuppressWarnings("try")
  86     private void test(String snippet, int loopCount) {
  87         DebugContext debug = getDebugContext();
  88         try (DebugContext.Scope s = debug.scope(getClass().getSimpleName(), new DebugDumpScope(snippet))) {
  89             final StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO, debug);
  90 
  91             PhaseContext context = new PhaseContext(getProviders());
  92             new LoopFullUnrollPhase(new CanonicalizerPhase(), new DefaultLoopPolicies()).apply(graph, context);
  93 
  94             assertTrue(graph.getNodes().filter(LoopBeginNode.class).count() == loopCount);
  95         } catch (Throwable e) {
  96             throw debug.handle(e);
  97         }
  98     }
  99 }