1 /* 2 * Copyright (c) 2011, 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.junit.Test; 26 27 import org.graalvm.compiler.debug.Debug; 28 import org.graalvm.compiler.debug.Debug.Scope; 29 import org.graalvm.compiler.debug.DebugDumpScope; 30 import org.graalvm.compiler.loop.DefaultLoopPolicies; 31 import org.graalvm.compiler.loop.phases.LoopUnswitchingPhase; 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 37 public class LoopUnswitchTest extends GraalCompilerTest { 38 39 public static int referenceSnippet1(int a) { 40 int sum = 0; 41 if (a > 2) { 42 for (int i = 0; i < 1000; i++) { 43 sum += 2; 44 } 45 } else { 46 for (int i = 0; i < 1000; i++) { 47 sum += a; 48 } 49 } 50 return sum; 51 } 52 53 public static int test1Snippet(int a) { 54 int sum = 0; 55 for (int i = 0; i < 1000; i++) { 56 if (a > 2) { 57 sum += 2; 58 } else { 59 sum += a; 60 } 61 } 62 return sum; 63 } 64 65 public static int referenceSnippet2(int a) { 66 int sum = 0; 67 switch (a) { 68 case 0: 69 for (int i = 0; i < 1000; i++) { 70 sum += System.currentTimeMillis(); 71 } 72 break; 73 case 1: 74 for (int i = 0; i < 1000; i++) { 75 sum += 1; 76 sum += 5; 77 } 78 break; 79 case 55: 80 for (int i = 0; i < 1000; i++) { 81 sum += 5; 82 } 83 break; 84 default: 85 for (int i = 0; i < 1000; i++) { 86 // nothing 87 } 88 break; 89 } 90 return sum; 91 } 92 93 @SuppressWarnings("fallthrough") 94 public static int test2Snippet(int a) { 95 int sum = 0; 96 for (int i = 0; i < 1000; i++) { 97 switch (a) { 98 case 0: 99 sum += System.currentTimeMillis(); 100 break; 101 case 1: 102 sum += 1; 103 // fall through 104 case 55: 105 sum += 5; 106 break; 107 default: 108 // nothing 109 break; 110 } 111 } 112 return sum; 113 } 114 115 @Test 116 public void test1() { 117 test("test1Snippet", "referenceSnippet1"); 118 } 119 120 @Test 121 public void test2() { 122 test("test2Snippet", "referenceSnippet2"); 123 } 124 125 @SuppressWarnings("try") 126 private void test(String snippet, String referenceSnippet) { 127 final StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO); 128 final StructuredGraph referenceGraph = parseEager(referenceSnippet, AllowAssumptions.NO); 129 130 new LoopUnswitchingPhase(new DefaultLoopPolicies()).apply(graph); 131 132 // Framestates create comparison problems 133 graph.clearAllStateAfter(); 134 referenceGraph.clearAllStateAfter(); 135 136 new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders())); 137 new CanonicalizerPhase().apply(referenceGraph, new PhaseContext(getProviders())); 138 try (Scope s = Debug.scope("Test", new DebugDumpScope("Test:" + snippet))) { 139 assertEquals(referenceGraph, graph); 140 } catch (Throwable e) { 141 throw Debug.handle(e); 142 } 143 } 144 }