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.nodes.StructuredGraph;
  30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  31 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  32 import org.graalvm.compiler.phases.tiers.HighTierContext;
  33 import org.junit.Test;
  34 
  35 /**
  36  * In the following tests, the usages of local variable "a" are replaced with the integer constant
  37  * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the
  38  * graph of the method that just has a "return 1" statement in it.
  39  */
  40 public class DegeneratedLoopsTest extends GraalCompilerTest {
  41 
  42     private static final String REFERENCE_SNIPPET = "referenceSnippet";
  43 
  44     @SuppressWarnings("all")
  45     public static int referenceSnippet(int a) {
  46         return a;
  47     }
  48 
  49     @Test
  50     public void test1() {
  51         test("test1Snippet");
  52     }
  53 
  54     private static class UnresolvedException extends RuntimeException {
  55 
  56         private static final long serialVersionUID = 5215434338750728440L;
  57 
  58         static {
  59             if (true) {
  60                 throw new UnsupportedOperationException("this class may never be initialized");
  61             }
  62         }
  63     }
  64 
  65     @SuppressWarnings("all")
  66     public static int test1Snippet(int a) {
  67         for (;;) {
  68             try {
  69                 test();
  70                 break;
  71             } catch (UnresolvedException e) {
  72             }
  73         }
  74         return a;
  75     }
  76 
  77     private static void test() {
  78 
  79     }
  80 
  81     @SuppressWarnings("try")
  82     private void test(final String snippet) {
  83         DebugContext debug = getDebugContext();
  84         try (DebugContext.Scope s = debug.scope("DegeneratedLoopsTest", new DebugDumpScope(snippet))) {
  85             StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
  86             HighTierContext context = getDefaultHighTierContext();
  87             createInliningPhase().apply(graph, context);
  88             new CanonicalizerPhase().apply(graph, context);
  89             debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
  90             StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.YES);
  91             debug.dump(DebugContext.BASIC_LEVEL, referenceGraph, "ReferenceGraph");
  92             assertEquals(referenceGraph, graph);
  93         } catch (Throwable e) {
  94             throw debug.handle(e);
  95         }
  96     }
  97 }