001/*
002 * Copyright (c) 2009, 2012, 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.jtt.optimize;
024
025import org.junit.*;
026
027import com.oracle.graal.jtt.*;
028
029/*
030 * Tests value numbering of integer operations.
031 */
032public class VN_Loop01 extends JTTTest {
033
034    private static boolean cond1 = true;
035    private static boolean cond2 = true;
036
037    public static int test(int arg) {
038        if (arg == 0) {
039            return test1(arg);
040        }
041        if (arg == 1) {
042            return test2(arg);
043        }
044        if (arg == 2) {
045            return test3(arg);
046        }
047        if (arg == 3) {
048            return test4(arg);
049        }
050        return 0;
051    }
052
053    public static int test1(int x) {
054        int c = 3;
055        int t = x + c;
056        while (cond1) {
057            if (cond2) {
058                int u = x + c; // GVN should recognize u == t
059                return t + u;
060            }
061        }
062        return 3; // GVN should recognize 3 == 3
063    }
064
065    public static int test2(int x) {
066        int c = 3;
067        while (cond1) {
068            int t = x + c;
069            if (cond2) {
070                int u = x + c; // GVN should recognize u == t
071                return t + u;
072            }
073        }
074        return 3;
075    }
076
077    public static int test3(int x) {
078        int c = 3;
079        int t = x + c;
080        while (cond1) {
081            if (cond2) {
082                int u = x + c; // GVN should recognize u == t
083                return t + u;
084            }
085            int u = x + c; // GVN should recognize u == t
086            return t + u;
087        }
088        return 3; // GVN should recognize 3 == 3
089    }
090
091    public static int test4(int x) {
092        int c = 3;
093        int t = x + c;
094        while (cond1) {
095            if (!cond2) {
096                int u = x + c;
097                return t + u;
098            }
099            int u = x + c;
100            return t + u;
101        }
102        return 3;
103    }
104
105    @Test
106    public void run0() throws Throwable {
107        runTest("test", 0);
108    }
109
110    @Test
111    public void run1() throws Throwable {
112        runTest("test", 1);
113    }
114
115    @Test
116    public void run2() throws Throwable {
117        runTest("test", 2);
118    }
119
120    @Test
121    public void run3() throws Throwable {
122        runTest("test", 3);
123    }
124
125    @Test
126    public void run4() throws Throwable {
127        runTest("test", 4);
128    }
129
130}