CompareCanonicalizerTest3.java revision 12995:5e441a7ec5e3
1/*
2 * Copyright (c) 2012, 2014, 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.nodes.StructuredGraph;
26import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
27import org.graalvm.compiler.nodes.ValueNode;
28import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
29import org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin;
30import org.graalvm.compiler.phases.common.CanonicalizerPhase;
31import org.graalvm.compiler.phases.tiers.PhaseContext;
32import org.junit.Ignore;
33import org.junit.Test;
34
35import jdk.vm.ci.meta.ResolvedJavaMethod;
36
37public class CompareCanonicalizerTest3 extends GraalCompilerTest {
38
39    @SuppressWarnings("unused") private static int sink0;
40    @SuppressWarnings("unused") private static int sink1;
41
42    @Test
43    public void test00() {
44        assertCanonicallyEqual("integerTestCanonicalization00", "referenceSnippet00");
45    }
46
47    public static void integerTestCanonicalization00(char a) {
48        if (a - 1 < a) {
49            sink1 = 0;
50        } else {
51            sink0 = -1;
52        }
53    }
54
55    @SuppressWarnings("unused")
56    public static void referenceSnippet00(char a) {
57        sink1 = 0;
58    }
59
60    @Ignore("Needs better stamp support for unsigned ranges")
61    @Test
62    public void test01() {
63        assertCanonicallyEqual("integerTestCanonicalization01", "referenceSnippet01");
64    }
65
66    public static void integerTestCanonicalization01(char a) {
67        if (Integer.compareUnsigned(a - 1, a) < 0) {
68            sink1 = 0;
69        } else {
70            sink0 = -1;
71        }
72    }
73
74    public static void referenceSnippet01(char a) {
75        if (a != 0) {
76            sink1 = 0;
77        } else {
78            sink0 = -1;
79        }
80    }
81
82    @Ignore("Needs better stamp support for unsigned ranges")
83    @Test
84    public void test1() {
85        assertCanonicallyEqual("integerTestCanonicalization1", "referenceSnippet1");
86    }
87
88    public static void integerTestCanonicalization1(char a) {
89        if (Integer.compareUnsigned(a - 2, a) < 0) {
90            sink1 = 0;
91        } else {
92            sink0 = -1;
93        }
94    }
95
96    public static void referenceSnippet1(char a) {
97        if (Integer.compareUnsigned(a, 2) >= 0) {
98            sink1 = 0;
99        } else {
100            sink0 = -1;
101        }
102    }
103
104    @Test
105    public void test2() {
106        assertCanonicallyEqual("integerTestCanonicalization2", "referenceSnippet2");
107    }
108
109    public static void integerTestCanonicalization2(int a) {
110        if (a - 1 < a) {
111            sink1 = 0;
112        } else {
113            sink0 = -1;
114        }
115    }
116
117    public static void referenceSnippet2(int a) {
118        if (a != Integer.MIN_VALUE) {
119            sink1 = 0;
120        } else {
121            sink0 = -1;
122        }
123    }
124
125    @Test
126    public void test3() {
127        assertCanonicallyEqual("integerTestCanonicalization3", "referenceSnippet3");
128    }
129
130    public static void integerTestCanonicalization3(int a) {
131        if (a - 2 < a) {
132            sink1 = 0;
133        } else {
134            sink0 = -1;
135        }
136    }
137
138    public static void referenceSnippet3(int a) {
139        if (a >= Integer.MIN_VALUE + 2) {
140            sink1 = 0;
141        } else {
142            sink0 = -1;
143        }
144    }
145
146    @Test
147    public void test4() {
148        assertCanonicallyEqual("integerTestCanonicalization4", "referenceSnippet4");
149    }
150
151    public static void integerTestCanonicalization4(int a) {
152        if (a + 1 < a) {
153            sink1 = 0;
154        } else {
155            sink0 = -1;
156        }
157    }
158
159    public static void referenceSnippet4(int a) {
160        if (a == Integer.MAX_VALUE) {
161            sink1 = 0;
162        } else {
163            sink0 = -1;
164        }
165    }
166
167    @Test
168    public void test5() {
169        assertCanonicallyEqual("integerTestCanonicalization5", "referenceSnippet5");
170    }
171
172    public static void integerTestCanonicalization5(int a) {
173        if (a + 2 < a) {
174            sink1 = 0;
175        } else {
176            sink0 = -1;
177        }
178    }
179
180    public static void referenceSnippet5(int a) {
181        if (a > Integer.MAX_VALUE - 2) {
182            sink1 = 0;
183        } else {
184            sink0 = -1;
185        }
186    }
187
188    @Test
189    public void test6() {
190        assertCanonicallyEqual("integerTestCanonicalization6", "referenceSnippet6");
191    }
192
193    public static void integerTestCanonicalization6(int a) {
194        if (a < a + 1) {
195            sink1 = 0;
196        } else {
197            sink0 = -1;
198        }
199    }
200
201    public static void referenceSnippet6(int a) {
202        if (a != Integer.MAX_VALUE) {
203            sink1 = 0;
204        } else {
205            sink0 = -1;
206        }
207    }
208
209    @Test
210    public void test7() {
211        assertCanonicallyEqual("integerTestCanonicalization7", "referenceSnippet7");
212    }
213
214    public static void integerTestCanonicalization7(int a) {
215        if (a < a + 2) {
216            sink1 = 0;
217        } else {
218            sink0 = -1;
219        }
220    }
221
222    public static void referenceSnippet7(int a) {
223        if (a <= Integer.MAX_VALUE - 2) {
224            sink1 = 0;
225        } else {
226            sink0 = -1;
227        }
228    }
229
230    protected void assertCanonicallyEqual(String snippet, String reference) {
231        StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
232        PhaseContext context = new PhaseContext(getProviders());
233        CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
234        canonicalizer.apply(graph, context);
235        canonicalizer.apply(graph, context);
236        StructuredGraph referenceGraph = parseEager(reference, AllowAssumptions.YES);
237        canonicalizer.apply(referenceGraph, context);
238        canonicalizer.apply(referenceGraph, context);
239        assertEquals(referenceGraph, graph, true, true);
240    }
241
242    @Override
243    protected InlineInvokePlugin.InlineInfo bytecodeParserShouldInlineInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) {
244        return InlineInvokePlugin.InlineInfo.createStandardInlineInfo(method);
245    }
246}
247