1 /*
   2  * Copyright (c) 2015, 2015, 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.Test;
  26 
  27 /**
  28  * Collection of tests for
  29  * {@link org.graalvm.compiler.phases.common.DominatorConditionalEliminationPhase} including those
  30  * that triggered bugs in this phase.
  31  */
  32 public class ConditionalEliminationTest3 extends ConditionalEliminationTestBase {
  33 
  34     private static final String REFERENCE_SNIPPET = "referenceSnippet";
  35 
  36     @SuppressWarnings("all")
  37     public static int referenceSnippet(int a, int b) {
  38         int sum = 0;
  39         outer: for (int i = 0;; ++i) {
  40             if (b > 100) {
  41                 inner: for (int j = 0;; ++j) {
  42                     ++sum;
  43                     if (sum == 100) {
  44                         break inner;
  45                     }
  46                     if (sum == 1000 && b < 1000) {
  47                         break outer;
  48                     }
  49                 }
  50             }
  51         }
  52         return sum;
  53     }
  54 
  55     @Test
  56     public void test1() {
  57         testConditionalElimination("test1Snippet", REFERENCE_SNIPPET);
  58     }
  59 
  60     @SuppressWarnings("all")
  61     public static int test1Snippet(int a, int b) {
  62         int sum = 0;
  63         outer: for (int i = 0;; ++i) {
  64             if (b > 100) {
  65                 inner: for (int j = 0;; ++j) {
  66                     ++sum;
  67                     if (sum == 100) {
  68                         break inner;
  69                     }
  70                     if (sum == 1000 && b < 1000) {
  71                         break outer;
  72                     }
  73                 }
  74             }
  75         }
  76         if (b >= 1000) {
  77             return 5;
  78         }
  79         return sum;
  80     }
  81 
  82     @Test
  83     public void test2() {
  84         testConditionalElimination("test2Snippet", REFERENCE_SNIPPET);
  85     }
  86 
  87     @SuppressWarnings("all")
  88     public static int test2Snippet(int a, int b) {
  89         int sum = 0;
  90         outer: for (int i = 0;; ++i) {
  91             if (b > 100) {
  92                 inner: for (int j = 0;; ++j) {
  93                     ++sum;
  94                     if (sum == 100) {
  95                         break inner;
  96                     }
  97                     if (sum == 1000 && b < 1000) {
  98                         break outer;
  99                     }
 100                 }
 101                 if (sum != 100) {
 102                     return 42;
 103                 }
 104             }
 105         }
 106         return sum;
 107     }
 108 }