001/*
002 * Copyright (c) 2015, 2015, 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 org.junit.*;
026
027import com.oracle.graal.api.directives.*;
028
029/**
030 * Collection of tests for
031 * {@link com.oracle.graal.phases.common.DominatorConditionalEliminationPhase} including those that
032 * triggered bugs in this phase.
033 */
034public class ConditionalEliminationTest1 extends ConditionalEliminationTestBase {
035
036    private static final String REFERENCE_SNIPPET = "referenceSnippet";
037
038    @SuppressWarnings("all")
039    public static int referenceSnippet(int a) {
040        if (a == 0) {
041            return 1;
042        }
043        return 0;
044    }
045
046    @Test
047    public void test1() {
048        testConditionalElimination("test1Snippet", REFERENCE_SNIPPET);
049    }
050
051    @SuppressWarnings("all")
052    public static int test1Snippet(int a) {
053        if (a == 0) {
054            if (a == 5) {
055                return 100;
056            }
057            if (a > 100) {
058                if (a == 0) {
059                    return 200;
060                }
061            }
062            if (a != 2) {
063                return 1;
064            }
065        }
066        return 0;
067    }
068
069    @Test
070    public void test2() {
071        testConditionalElimination("test2Snippet", REFERENCE_SNIPPET);
072    }
073
074    @SuppressWarnings("all")
075    public static int test2Snippet(int a) {
076        if (a == 0) {
077            if (a > 100) {
078                if (a == 0) {
079                    return 200;
080                }
081            }
082            if (a != 2) {
083                return 1;
084            }
085        }
086        return 0;
087    }
088
089    @Test
090    public void test3() {
091        testConditionalElimination("test3Snippet", REFERENCE_SNIPPET);
092    }
093
094    @SuppressWarnings("all")
095    public static int test3Snippet(int a) {
096        if (a == 0) {
097            if (a < 1) {
098                if (a < 2) {
099                    if (a < 3) {
100                        if (a > -1) {
101                            if (a > -2) {
102                                if (a > -3) {
103                                    if (a == 1) {
104                                        return 42;
105                                    } else {
106                                        return 1;
107                                    }
108                                }
109                            }
110                        }
111                    }
112                }
113            }
114        }
115        return 0;
116    }
117
118    @SuppressWarnings("all")
119    public static int test4Snippet(int a, int b) {
120        if (b < 1) {
121            GraalDirectives.controlFlowAnchor();
122            if (b < 0) {
123                return 1;
124            }
125        }
126        return 0;
127    }
128
129    @Test
130    public void test4() {
131        testConditionalElimination("test4Snippet", "test4Snippet");
132    }
133
134    @SuppressWarnings("all")
135    public static int test5Snippet(int a, int b) {
136        if ((b & 3) == 0) {
137            GraalDirectives.controlFlowAnchor();
138            if ((b & 7) == 0) {
139                GraalDirectives.controlFlowAnchor();
140                return 1;
141            }
142        } else {
143            GraalDirectives.controlFlowAnchor();
144            if ((b & 1) == 0) {
145                GraalDirectives.controlFlowAnchor();
146                return 2;
147            }
148        }
149        return 0;
150    }
151
152    @Test
153    public void test5() {
154        testConditionalElimination("test5Snippet", "test5Snippet");
155    }
156}