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 package org.graalvm.compiler.core.test;
  24 
  25 import org.junit.Assert;
  26 import org.junit.Test;
  27 
  28 import org.graalvm.compiler.debug.Debug;
  29 import org.graalvm.compiler.nodes.StructuredGraph;
  30 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  31 import org.graalvm.compiler.nodes.ValuePhiNode;
  32 
  33 /**
  34  * In the following tests, the correct removal of redundant phis during graph building is tested.
  35  */
  36 public class PhiCreationTests extends GraalCompilerTest {
  37 
  38     /**
  39      * Dummy method to avoid javac dead code elimination.
  40      */
  41     private static void test() {
  42     }
  43 
  44     @Test
  45     public void test1() {
  46         StructuredGraph graph = parseEager("test1Snippet", AllowAssumptions.YES);
  47         Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
  48     }
  49 
  50     public static int test1Snippet(int a) {
  51         if (a > 1) {
  52             test();
  53         }
  54         return a;
  55     }
  56 
  57     @Test
  58     public void test2() {
  59         StructuredGraph graph = parseEager("test2Snippet", AllowAssumptions.YES);
  60         Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
  61     }
  62 
  63     public static int test2Snippet(int a) {
  64         while (a > 1) {
  65             test();
  66         }
  67         return a;
  68     }
  69 
  70     @Test
  71     public void test3() {
  72         StructuredGraph graph = parseEager("test3Snippet", AllowAssumptions.YES);
  73         Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "Graph");
  74         Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
  75     }
  76 
  77     public static int test3Snippet(int a) {
  78         while (a > 1) {
  79             while (a > 1) {
  80                 test();
  81             }
  82         }
  83         return a;
  84     }
  85 
  86     @Test
  87     public void test4() {
  88         StructuredGraph graph = parseEager("test4Snippet", AllowAssumptions.YES);
  89         Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "Graph");
  90         Assert.assertFalse(graph.getNodes().filter(ValuePhiNode.class).iterator().hasNext());
  91     }
  92 
  93     public static int test4Snippet(int a) {
  94         int b = 5;
  95         while (a > 1) {
  96             while (a > 1) {
  97                 while (a > 1) {
  98                     try {
  99                         test();
 100                     } catch (Throwable t) {
 101 
 102                     }
 103                 }
 104             }
 105             while (a > 1) {
 106                 while (a > 1) {
 107                     try {
 108                         test();
 109                     } catch (Throwable t) {
 110 
 111                     }
 112                 }
 113             }
 114         }
 115         return a + b;
 116     }
 117 }