1 /*
2  * Copyright (c) 2011, 2019, 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  */
23 
24 
25 package org.graalvm.compiler.core.test;
26 
27 import org.graalvm.compiler.debug.DebugContext;
28 import org.graalvm.compiler.nodes.FrameState;
29 import org.graalvm.compiler.nodes.StructuredGraph;
30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
31 import org.graalvm.compiler.nodes.util.GraphUtil;
32 import org.junit.Test;
33 
34 public class PushThroughIfTest extends GraalCompilerTest {
35 
36     public int field1;
37     public int field2;
38 
testSnippet(boolean b)39     public int testSnippet(boolean b) {
40         int i;
41         if (b) {
42             i = field1;
43         } else {
44             i = field1;
45         }
46         return i + field2;
47     }
48 
49     @SuppressWarnings("unused")
referenceSnippet(boolean b)50     public int referenceSnippet(boolean b) {
51         return field1 + field2;
52     }
53 
54     @Test
test1()55     public void test1() {
56         test("testSnippet", "referenceSnippet");
57     }
58 
test(String snippet, String reference)59     private void test(String snippet, String reference) {
60         StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
61         DebugContext debug = graph.getDebug();
62         debug.dump(DebugContext.BASIC_LEVEL, graph, "Graph");
63         for (FrameState fs : graph.getNodes(FrameState.TYPE).snapshot()) {
64             fs.replaceAtUsages(null);
65             GraphUtil.killWithUnusedFloatingInputs(fs);
66         }
67         createCanonicalizerPhase().apply(graph, getProviders());
68         createCanonicalizerPhase().apply(graph, getProviders());
69 
70         StructuredGraph referenceGraph = parseEager(reference, AllowAssumptions.YES);
71         for (FrameState fs : referenceGraph.getNodes(FrameState.TYPE).snapshot()) {
72             fs.replaceAtUsages(null);
73             GraphUtil.killWithUnusedFloatingInputs(fs);
74         }
75         createCanonicalizerPhase().apply(referenceGraph, getProviders());
76         assertEquals(referenceGraph, graph);
77     }
78 }
79