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