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.*;
026
027import org.junit.*;
028
029import com.oracle.graal.nodes.*;
030import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
031
032/**
033 * In the following tests, the correct removal of redundant phis during graph building is tested.
034 */
035public class PhiCreationTests extends GraalCompilerTest {
036
037    /**
038     * Dummy method to avoid javac dead code elimination.
039     */
040    private static void test() {
041    }
042
043    @Test
044    public void test1() {
045        StructuredGraph graph = parseEager("test1Snippet", AllowAssumptions.YES);
046        Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
047    }
048
049    public static int test1Snippet(int a) {
050        if (a > 1) {
051            test();
052        }
053        return a;
054    }
055
056    @Test
057    public void test2() {
058        StructuredGraph graph = parseEager("test2Snippet", AllowAssumptions.YES);
059        Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
060    }
061
062    public static int test2Snippet(int a) {
063        while (a > 1) {
064            test();
065        }
066        return a;
067    }
068
069    @Test
070    public void test3() {
071        StructuredGraph graph = parseEager("test3Snippet", AllowAssumptions.YES);
072        Debug.dump(graph, "Graph");
073        Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
074    }
075
076    public static int test3Snippet(int a) {
077        while (a > 1) {
078            while (a > 1) {
079                test();
080            }
081        }
082        return a;
083    }
084
085    @Test
086    public void test4() {
087        StructuredGraph graph = parseEager("test4Snippet", AllowAssumptions.YES);
088        Debug.dump(graph, "Graph");
089        Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
090    }
091
092    public static int test4Snippet(int a) {
093        int b = 5;
094        while (a > 1) {
095            while (a > 1) {
096                while (a > 1) {
097                    try {
098                        test();
099                    } catch (Throwable t) {
100
101                    }
102                }
103            }
104            while (a > 1) {
105                while (a > 1) {
106                    try {
107                        test();
108                    } catch (Throwable t) {
109
110                    }
111                }
112            }
113        }
114        return a + b;
115    }
116}