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