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