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;
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 }
|
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.DebugContext;
26 import org.graalvm.compiler.debug.DebugDumpScope;
27 import org.graalvm.compiler.loop.DefaultLoopPolicies;
28 import org.graalvm.compiler.loop.phases.LoopFullUnrollPhase;
29 import org.graalvm.compiler.nodes.LoopBeginNode;
30 import org.graalvm.compiler.nodes.StructuredGraph;
31 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
32 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
33 import org.graalvm.compiler.phases.tiers.PhaseContext;
34 import org.junit.Test;
35
36 public class LoopFullUnrollTest extends GraalCompilerTest {
37
38 public static int testMinToMax(int input) {
39 int ret = 2;
40 int current = input;
41 for (long i = Long.MIN_VALUE; i < Long.MAX_VALUE; i++) {
42 ret *= 2 + current;
43 current /= 50;
44 }
45 return ret;
65 test("testMinTo0", 1);
66 }
67
68 public static int testNegativeTripCount(int input) {
69 int ret = 2;
70 int current = input;
71 for (long i = 0; i <= -20; i++) {
72 ret *= 2 + current;
73 current /= 50;
74 }
75 return ret;
76 }
77
78 @Test
79 public void runNegativeTripCount() throws Throwable {
80 test("testNegativeTripCount", 0);
81 }
82
83 @SuppressWarnings("try")
84 private void test(String snippet, int loopCount) {
85 DebugContext debug = getDebugContext();
86 try (DebugContext.Scope s = debug.scope(getClass().getSimpleName(), new DebugDumpScope(snippet))) {
87 final StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO, debug);
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 }
|