1 /*
   2  * Copyright (c) 2011, 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.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.LoopUnswitchingPhase;
  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.junit.Test;
  35 
  36 public class LoopUnswitchTest extends GraalCompilerTest {
  37 
  38     public static int referenceSnippet1(int a) {
  39         int sum = 0;
  40         if (a > 2) {
  41             for (int i = 0; i < 1000; i++) {
  42                 sum += 2;
  43             }
  44         } else {
  45             for (int i = 0; i < 1000; i++) {
  46                 sum += a;
  47             }
  48         }
  49         return sum;
  50     }
  51 
  52     public static int test1Snippet(int a) {
  53         int sum = 0;
  54         for (int i = 0; i < 1000; i++) {
  55             if (a > 2) {
  56                 sum += 2;
  57             } else {
  58                 sum += a;
  59             }
  60         }
  61         return sum;
  62     }
  63 
  64     public static int referenceSnippet2(int a) {
  65         int sum = 0;
  66         switch (a) {
  67             case 0:
  68                 for (int i = 0; i < 1000; i++) {
  69                     sum += System.currentTimeMillis();
  70                 }
  71                 break;
  72             case 1:
  73                 for (int i = 0; i < 1000; i++) {
  74                     sum += 1;
  75                     sum += 5;
  76                 }
  77                 break;
  78             case 55:
  79                 for (int i = 0; i < 1000; i++) {
  80                     sum += 5;
  81                 }
  82                 break;
  83             default:
  84                 for (int i = 0; i < 1000; i++) {
  85                     // nothing
  86                 }
  87                 break;
  88         }
  89         return sum;
  90     }
  91 
  92     @SuppressWarnings("fallthrough")
  93     public static int test2Snippet(int a) {
  94         int sum = 0;
  95         for (int i = 0; i < 1000; i++) {
  96             switch (a) {
  97                 case 0:
  98                     sum += System.currentTimeMillis();
  99                     break;
 100                 case 1:
 101                     sum += 1;
 102                     // fall through
 103                 case 55:
 104                     sum += 5;
 105                     break;
 106                 default:
 107                     // nothing
 108                     break;
 109             }
 110         }
 111         return sum;
 112     }
 113 
 114     @Test
 115     public void test1() {
 116         test("test1Snippet", "referenceSnippet1");
 117     }
 118 
 119     @Test
 120     public void test2() {
 121         test("test2Snippet", "referenceSnippet2");
 122     }
 123 
 124     @SuppressWarnings("try")
 125     private void test(String snippet, String referenceSnippet) {
 126         DebugContext debug = getDebugContext();
 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, getProviders());
 137         new CanonicalizerPhase().apply(referenceGraph, getProviders());
 138         try (DebugContext.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 }