PhiCreationTests.java revision 13264:48566d838608
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.graalvm.compiler.debug.DebugContext;
26import org.graalvm.compiler.nodes.StructuredGraph;
27import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
28import org.graalvm.compiler.nodes.ValuePhiNode;
29import org.junit.Assert;
30import org.junit.Test;
31
32/**
33 * In the following tests, the correct removal of redundant phis during graph building is tested.
34 */
35public class PhiCreationTests extends GraalCompilerTest {
36
37    /**
38     * Dummy method to avoid javac dead code elimination.
39     */
40    private static void test() {
41    }
42
43    @Test
44    public void test1() {
45        StructuredGraph graph = parseEager("test1Snippet", AllowAssumptions.YES);
46        Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
47    }
48
49    public static int test1Snippet(int a) {
50        if (a > 1) {
51            test();
52        }
53        return a;
54    }
55
56    @Test
57    public void test2() {
58        StructuredGraph graph = parseEager("test2Snippet", AllowAssumptions.YES);
59        Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
60    }
61
62    public static int test2Snippet(int a) {
63        while (a > 1) {
64            test();
65        }
66        return a;
67    }
68
69    @Test
70    public void test3() {
71        StructuredGraph graph = parseEager("test3Snippet", AllowAssumptions.YES);
72        DebugContext debug = graph.getDebug();
73        debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
74        Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
75    }
76
77    public static int test3Snippet(int a) {
78        while (a > 1) {
79            while (a > 1) {
80                test();
81            }
82        }
83        return a;
84    }
85
86    @Test
87    public void test4() {
88        StructuredGraph graph = parseEager("test4Snippet", AllowAssumptions.YES);
89        DebugContext debug = graph.getDebug();
90        debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
91        Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
92    }
93
94    public static int test4Snippet(int a) {
95        int b = 5;
96        while (a > 1) {
97            while (a > 1) {
98                while (a > 1) {
99                    try {
100                        test();
101                    } catch (Throwable t) {
102
103                    }
104                }
105            }
106            while (a > 1) {
107                while (a > 1) {
108                    try {
109                        test();
110                    } catch (Throwable t) {
111
112                    }
113                }
114            }
115        }
116        return a + b;
117    }
118}
119