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 org.junit.*;
026
027import com.oracle.graal.graph.*;
028import com.oracle.graal.nodes.*;
029import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
030import com.oracle.graal.phases.common.*;
031import com.oracle.graal.phases.tiers.*;
032
033public class ReassociateAndCanonicalTest extends GraalCompilerTest {
034
035    public static int rnd = (int) (Math.random() * 100);
036
037    @Test
038    public void test1() {
039        test("test1Snippet", "ref1Snippet");
040    }
041
042    public static int test1Snippet() {
043        return 1 + (rnd + 2);
044    }
045
046    public static int ref1Snippet() {
047        return rnd + 3;
048    }
049
050    @Test
051    public void test2() {
052        test("test2Snippet", "ref2Snippet");
053    }
054
055    public static int test2Snippet() {
056        return rnd + 3;
057    }
058
059    public static int ref2Snippet() {
060        return (rnd + 2) + 1;
061    }
062
063    @Test
064    public void test3() {
065        test("test3Snippet", "ref3Snippet");
066    }
067
068    public static int test3Snippet() {
069        return rnd + 3;
070    }
071
072    public static int ref3Snippet() {
073        return 1 + (2 + rnd);
074    }
075
076    @Test
077    public void test4() {
078        test("test4Snippet", "ref4Snippet");
079    }
080
081    public static int test4Snippet() {
082        return rnd + 3;
083    }
084
085    public static int ref4Snippet() {
086        return (2 + rnd) + 1;
087    }
088
089    @Test
090    public void test5() {
091        test("test5Snippet", "ref5Snippet");
092    }
093
094    public static int test5Snippet() {
095        return -1 - rnd;
096    }
097
098    public static int ref5Snippet() {
099        return 1 - (rnd + 2);
100    }
101
102    @Test
103    public void test6() {
104        test("test6Snippet", "ref6Snippet");
105    }
106
107    public static int test6Snippet() {
108        return rnd + 1;
109    }
110
111    public static int ref6Snippet() {
112        return (rnd + 2) - 1;
113    }
114
115    @Test
116    public void test7() {
117        test("test7Snippet", "ref7Snippet");
118    }
119
120    public static int test7Snippet() {
121        return -1 - rnd;
122    }
123
124    public static int ref7Snippet() {
125        return 1 - (2 + rnd);
126    }
127
128    @Test
129    public void test8() {
130        test("test8Snippet", "ref8Snippet");
131    }
132
133    public static int test8Snippet() {
134        return rnd + 1;
135    }
136
137    public static int ref8Snippet() {
138        return (2 + rnd) - 1;
139    }
140
141    @Test
142    public void test9() {
143        test("test9Snippet", "ref9Snippet");
144    }
145
146    public static int test9Snippet() {
147        return rnd - 1;
148    }
149
150    public static int ref9Snippet() {
151        return 1 + (rnd - 2);
152    }
153
154    @Test
155    public void test10() {
156        test("test10Snippet", "ref10Snippet");
157    }
158
159    public static int test10Snippet() {
160        return rnd - 1;
161    }
162
163    public static int ref10Snippet() {
164        return (rnd - 2) + 1;
165    }
166
167    @Test
168    public void test11() {
169        test("test11Snippet", "ref11Snippet");
170    }
171
172    public static int test11Snippet() {
173        return -rnd + 3;
174    }
175
176    public static int ref11Snippet() {
177        return 1 + (2 - rnd);
178    }
179
180    @Test
181    public void test12() {
182        test("test12Snippet", "ref12Snippet");
183    }
184
185    public static int test12Snippet() {
186        return -rnd + 3;
187    }
188
189    public static int ref12Snippet() {
190        return (2 - rnd) + 1;
191    }
192
193    @Test
194    public void test13() {
195        test("test13Snippet", "ref13Snippet");
196    }
197
198    public static int test13Snippet() {
199        return -rnd + 3;
200    }
201
202    public static int ref13Snippet() {
203        return 1 - (rnd - 2);
204    }
205
206    @Test
207    public void test14() {
208        test("test14Snippet", "ref14Snippet");
209    }
210
211    public static int test14Snippet() {
212        return rnd - 3;
213    }
214
215    public static int ref14Snippet() {
216        return (rnd - 2) - 1;
217    }
218
219    @Test
220    public void test15() {
221        test("test15Snippet", "ref15Snippet");
222    }
223
224    public static int test15Snippet() {
225        return rnd + -1;
226    }
227
228    public static int ref15Snippet() {
229        return 1 - (2 - rnd);
230    }
231
232    @Test
233    public void test16() {
234        test("test16Snippet", "ref16Snippet");
235    }
236
237    public static int test16Snippet() {
238        return -rnd + 1;
239    }
240
241    public static int ref16Snippet() {
242        return (2 - rnd) - 1;
243    }
244
245    private <T extends Node & IterableNodeType> void test(String test, String ref) {
246        StructuredGraph testGraph = parseEager(test, AllowAssumptions.NO);
247        new CanonicalizerPhase().apply(testGraph, new PhaseContext(getProviders()));
248        StructuredGraph refGraph = parseEager(ref, AllowAssumptions.NO);
249        new CanonicalizerPhase().apply(refGraph, new PhaseContext(getProviders()));
250        assertEquals(testGraph, refGraph);
251    }
252}