001/*
002 * Copyright (c) 2011, 2014, 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.loop.phases.*;
028import com.oracle.graal.nodes.*;
029import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
030import com.oracle.graal.phases.common.*;
031import com.oracle.graal.phases.common.inlining.*;
032import com.oracle.graal.phases.tiers.*;
033import com.oracle.graal.virtual.phases.ea.*;
034
035/**
036 * In the following tests, the usages of local variable "a" are replaced with the integer constant
037 * 0. Then boxing elimination is applied and it is verified that the resulting graph is equal to the
038 * graph of the method that just has a "return 1" statement in it.
039 */
040public class BoxingEliminationTest extends GraalCompilerTest {
041
042    private static final Short s = 2;
043
044    @SuppressWarnings("all")
045    public static short referenceSnippet1() {
046        return 1;
047    }
048
049    @SuppressWarnings("all")
050    public static short referenceSnippet2() {
051        return 2;
052    }
053
054    public static Short boxedShort() {
055        return 1;
056    }
057
058    public static Object boxedObjectShort() {
059        return (short) 1;
060    }
061
062    public static Object boxedObjectInteger() {
063        return (int) 1;
064    }
065
066    public static Integer boxedInteger() {
067        return 2;
068    }
069
070    public static Short constantBoxedShort() {
071        return s;
072    }
073
074    @Test
075    public void test1() {
076        compareGraphs("test1Snippet", "referenceSnippet1");
077    }
078
079    @SuppressWarnings("all")
080    public static short test1Snippet() {
081        return boxedShort();
082    }
083
084    @Test
085    public void test2() {
086        compareGraphs("test2Snippet", "referenceSnippet1");
087    }
088
089    @SuppressWarnings("all")
090    public static short test2Snippet() {
091        return (Short) boxedObjectShort();
092    }
093
094    @Test
095    public void test3() {
096        compareGraphs("test3Snippet", "referenceSnippet1");
097    }
098
099    @SuppressWarnings("all")
100    public static short test3Snippet() {
101        short b = boxedShort();
102        if (b < 0) {
103            b = boxedShort();
104        }
105        return b;
106    }
107
108    @Test
109    public void test4() {
110        compareGraphs("test4Snippet", "referenceSnippet2");
111    }
112
113    @SuppressWarnings("all")
114    public static short test4Snippet() {
115        return constantBoxedShort();
116    }
117
118    @Ignore
119    @Test
120    public void testLoop() {
121        compareGraphs("testLoopSnippet", "referenceLoopSnippet", false, true);
122    }
123
124    public static int testLoopSnippet(int n, int a) {
125        Integer sum = a;
126        for (Integer i = 0; i < n; i++) {
127            sum += i;
128        }
129        return sum;
130    }
131
132    public static int referenceLoopSnippet(int n, int a) {
133        int sum = a;
134        for (int i = 0; i < n; i++) {
135            sum += i;
136        }
137        return sum;
138    }
139
140    @Test
141    public void testLoop2() {
142        compareGraphs("testLoop2Snippet", "referenceLoop2Snippet", true, true);
143    }
144
145    public static int testLoop2Snippet(int n, Integer a) {
146        Integer sum = a;
147        for (Integer i = 0; i < n; i++) {
148            sum += i;
149        }
150        return sum;
151    }
152
153    public static int referenceLoop2Snippet(int n, Integer a) {
154        Integer sum0;
155        if (n <= 0) {
156            sum0 = a;
157        } else {
158            int sum = a;
159            for (int i = 0; i < n; i++) {
160                sum += i;
161            }
162            sum0 = sum;
163        }
164        return sum0;
165    }
166
167    public static int referenceIfSnippet(int a) {
168        int result;
169        if (a < 0) {
170            result = 2;
171        } else {
172            result = 1;
173        }
174        return result;
175    }
176
177    @Test
178    public void testIf() {
179        compareGraphs("testIfSnippet", "referenceIfSnippet");
180    }
181
182    public static int testIfSnippet(int a) {
183        Integer result;
184        if (a < 0) {
185            result = boxedInteger();
186        } else {
187            result = (Integer) boxedObjectInteger();
188        }
189        return result;
190    }
191
192    @Test
193    public void testComparison() {
194        compareGraphs("testComparison1Snippet", "referenceComparisonSnippet");
195        compareGraphs("testComparison2Snippet", "referenceComparisonSnippet");
196    }
197
198    @SuppressWarnings("cast")
199    public static boolean testComparison1Snippet(int a, int b) {
200        return ((Integer) a) == b;
201    }
202
203    public static boolean testComparison2Snippet(int a, int b) {
204        Integer x = a;
205        Integer y = b;
206        return x == y;
207    }
208
209    public static boolean referenceComparisonSnippet(int a, int b) {
210        return a == b;
211    }
212
213    @Test
214    public void testLateCanonicalization() {
215        compareGraphs("testLateCanonicalizationSnippet", "referenceLateCanonicalizationSnippet");
216    }
217
218    public static boolean testLateCanonicalizationSnippet(int a) {
219        Integer x = a;
220        Integer y = 1000;
221        return x == y;
222    }
223
224    public static boolean referenceLateCanonicalizationSnippet(int a) {
225        return a == 1000;
226    }
227
228    private StructuredGraph graph;
229
230    public static Integer materializeReferenceSnippet(int a) {
231        return Integer.valueOf(a);
232    }
233
234    public static Integer materializeTest1Snippet(int a) {
235        Integer v = a;
236
237        if (v == a) {
238            return v;
239        } else {
240            return null;
241        }
242    }
243
244    @Test
245    public void materializeTest1() {
246        test("materializeTest1Snippet", 1);
247    }
248
249    public static int intTest1Snippet() {
250        return Integer.valueOf(1);
251    }
252
253    @Test
254    public void intTest1() {
255        ValueNode result = getResult("intTest1Snippet");
256        Assert.assertTrue(result.isConstant());
257        Assert.assertEquals(1, result.asJavaConstant().asInt());
258    }
259
260    public static int mergeTest1Snippet(boolean d, int a, int b) {
261        Integer v;
262        if (d) {
263            v = a;
264        } else {
265            v = b;
266        }
267        return v;
268    }
269
270    @Test
271    public void mergeTest1() {
272        processMethod("mergeTest1Snippet");
273    }
274
275    public static boolean equalsTest1Snippet(int x, int y) {
276        Integer a = x;
277        Integer b = y;
278        return a == b;
279    }
280
281    @Test
282    public void equalsTest1() {
283        processMethod("equalsTest1Snippet");
284    }
285
286    public static int loopTest1Snippet(int n, int v) {
287        Integer sum = 0;
288        for (int i = 0; i < n; i++) {
289            sum += v;
290        }
291        return sum;
292    }
293
294    @Test
295    public void loopTest1() {
296        processMethod("loopTest1Snippet");
297
298    }
299
300    final ValueNode getResult(String snippet) {
301        processMethod(snippet);
302        assertDeepEquals(1, graph.getNodes(ReturnNode.TYPE).count());
303        return graph.getNodes(ReturnNode.TYPE).first().result();
304    }
305
306    private void processMethod(final String snippet) {
307        graph = parseEager(snippet, AllowAssumptions.NO);
308        HighTierContext context = getDefaultHighTierContext();
309        new InliningPhase(new CanonicalizerPhase()).apply(graph, context);
310        new PartialEscapePhase(false, new CanonicalizerPhase()).apply(graph, context);
311    }
312
313    private void compareGraphs(final String snippet, final String referenceSnippet) {
314        compareGraphs(snippet, referenceSnippet, false, false);
315    }
316
317    private void compareGraphs(final String snippet, final String referenceSnippet, final boolean loopPeeling, final boolean excludeVirtual) {
318        graph = parseEager(snippet, AllowAssumptions.NO);
319        HighTierContext context = getDefaultHighTierContext();
320        CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
321        canonicalizer.apply(graph, context);
322        new InliningPhase(new CanonicalizerPhase()).apply(graph, context);
323        if (loopPeeling) {
324            new LoopPeelingPhase().apply(graph);
325        }
326        new DeadCodeEliminationPhase().apply(graph);
327        canonicalizer.apply(graph, context);
328        new PartialEscapePhase(false, canonicalizer).apply(graph, context);
329
330        new DeadCodeEliminationPhase().apply(graph);
331        canonicalizer.apply(graph, context);
332
333        StructuredGraph referenceGraph = parseEager(referenceSnippet, AllowAssumptions.YES);
334        new InliningPhase(new CanonicalizerPhase()).apply(referenceGraph, context);
335        new DeadCodeEliminationPhase().apply(referenceGraph);
336        new CanonicalizerPhase().apply(referenceGraph, context);
337
338        assertEquals(referenceGraph, graph, excludeVirtual, true);
339    }
340}