MergeCanonicalizerTest.java revision 12657:6ef01bd40ce2
1243730Srwatson/*
2243730Srwatson * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3243730Srwatson * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4243730Srwatson *
5243730Srwatson * This code is free software; you can redistribute it and/or modify it
6243730Srwatson * under the terms of the GNU General Public License version 2 only, as
7243730Srwatson * published by the Free Software Foundation.
8243730Srwatson *
9243730Srwatson * This code is distributed in the hope that it will be useful, but WITHOUT
10243730Srwatson * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11243730Srwatson * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12243730Srwatson * version 2 for more details (a copy is included in the LICENSE file that
13243730Srwatson * accompanied this code).
14243730Srwatson *
15243730Srwatson * You should have received a copy of the GNU General Public License version
16243730Srwatson * 2 along with this work; if not, write to the Free Software Foundation,
17243730Srwatson * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18243730Srwatson *
19243730Srwatson * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20243730Srwatson * or visit www.oracle.com if you need additional information or have any
21243730Srwatson * questions.
22243730Srwatson */
23243730Srwatsonpackage org.graalvm.compiler.core.test;
24243730Srwatson
25243730Srwatsonimport org.junit.Test;
26243730Srwatson
27243730Srwatsonimport org.graalvm.compiler.debug.Debug;
28243730Srwatsonimport org.graalvm.compiler.nodes.ReturnNode;
29243730Srwatsonimport org.graalvm.compiler.nodes.StructuredGraph;
30243730Srwatsonimport org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
31243730Srwatsonimport org.graalvm.compiler.phases.common.CanonicalizerPhase;
32243730Srwatsonimport org.graalvm.compiler.phases.tiers.PhaseContext;
33243730Srwatson
34243730Srwatsonpublic class MergeCanonicalizerTest extends GraalCompilerTest {
35243730Srwatson
36243730Srwatson    public static int staticField;
37243730Srwatson
38243730Srwatson    private int field;
39243730Srwatson
40243730Srwatson    @Test
41243730Srwatson    public void testSplitReturn() {
42243730Srwatson        test("testSplitReturnSnippet", 2);
43243730Srwatson        testReturnCount("testSplitReturnSnippet", 2);
44243730Srwatson    }
45243730Srwatson
46243730Srwatson    public int testSplitReturnSnippet(int b) {
47243730Srwatson        int v;
48243730Srwatson        if (b < 0) {
49243730Srwatson            staticField = 1;
50243730Srwatson            v = 10;
51243730Srwatson        } else {
52243730Srwatson            staticField = 2;
53243730Srwatson            v = 20;
54243730Srwatson        }
55243730Srwatson        int i = field;
56243730Srwatson        i = field + i;
57243730Srwatson        return v;
58243730Srwatson    }
59243730Srwatson
60243730Srwatson    private void testReturnCount(String snippet, int returnCount) {
61243730Srwatson        StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
62243730Srwatson        new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
63243730Srwatson        new CanonicalizerPhase().apply(graph, new PhaseContext(getProviders()));
64243730Srwatson        Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "Graph");
65243730Srwatson        assertDeepEquals(returnCount, graph.getNodes(ReturnNode.TYPE).count());
66243730Srwatson    }
67243730Srwatson}
68243730Srwatson