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