001/*
002 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.test;
024
025import com.oracle.graal.debug.*;
026import com.oracle.graal.debug.Debug.*;
027
028import org.junit.*;
029
030import com.oracle.graal.nodes.*;
031import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
032import com.oracle.graal.phases.common.*;
033import com.oracle.graal.phases.common.inlining.*;
034import com.oracle.graal.phases.tiers.*;
035
036/**
037 * In the following tests, the usages of local variable "a" are replaced with the integer constant
038 * 0. Then canonicalization is applied and it is verified that the resulting graph is equal to the
039 * graph of the method that just has a "return 1" statement in it.
040 */
041public class DegeneratedLoopsTest extends GraalCompilerTest {
042
043    private static final String REFERENCE_SNIPPET = "referenceSnippet";
044
045    @SuppressWarnings("all")
046    public static int referenceSnippet(int a) {
047        return a;
048    }
049
050    @Test
051    public void test1() {
052        test("test1Snippet");
053    }
054
055    private static class UnresolvedException extends RuntimeException {
056
057        private static final long serialVersionUID = 5215434338750728440L;
058
059        static {
060            if (true) {
061                throw new UnsupportedOperationException("this class may never be initialized");
062            }
063        }
064    }
065
066    @SuppressWarnings("all")
067    public static int test1Snippet(int a) {
068        for (;;) {
069            try {
070                test();
071                break;
072            } catch (UnresolvedException e) {
073            }
074        }
075        return a;
076    }
077
078    private static void test() {
079
080    }
081
082    private void test(final String snippet) {
083        try (Scope s = Debug.scope("DegeneratedLoopsTest", new DebugDumpScope(snippet))) {
084            StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
085            HighTierContext context = getDefaultHighTierContext();
086            new InliningPhase(new CanonicalizerPhase()).apply(graph, context);
087            new CanonicalizerPhase().apply(graph, context);
088            Debug.dump(graph, "Graph");
089            StructuredGraph referenceGraph = parseEager(REFERENCE_SNIPPET, AllowAssumptions.YES);
090            Debug.dump(referenceGraph, "ReferenceGraph");
091            assertEquals(referenceGraph, graph);
092        } catch (Throwable e) {
093            throw Debug.handle(e);
094        }
095    }
096}