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 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import java.util.ArrayList;
  28 import java.util.Arrays;
  29 import java.util.List;
  30 
  31 import org.junit.Test;
  32 
  33 public class ArrayLengthProviderTest extends GraalCompilerTest {
  34 
  35     public static Object test0Snippet(ArrayList<?> list, boolean a) {
  36         while (true) {
  37             Object[] array = toArray(list);
  38             if (array.length < 1) {
  39                 return null;
  40             }
  41             if (array[0] instanceof String || a) {
  42                 /*
  43                  * This code is outside of the loop. Accessing the array reqires a ValueProxyNode.
  44                  * When the simplification of the ArrayLengthNode replaces the length access with
  45                  * the ArrayList.size used to create the array, then the value needs to have a
  46                  * ValueProxyNode too. In addition, the two parts of the if-condition actually lead
  47                  * to two separate loop exits, with two separate proxy nodes. A ValuePhiNode is
  48                  * present originally for the array, and the array length simplification needs to
  49                  * create a new ValuePhiNode for the two newly introduced ValueProxyNode.
  50                  */
  51                 if (array.length < 1) {
  52                     return null;
  53                 }
  54                 return array[0];
  55             }
  56         }
  57     }
  58 
  59     public static Object test1Snippet(ArrayList<?> list, boolean a, boolean b) {
  60         while (true) {
  61             Object[] array = toArray(list);
  62             if (a || b) {
  63                 if (array.length < 1) {
  64                     return null;
  65                 }
  66                 return array[0];
  67             }
  68         }
  69     }
  70 
  71     public static Object[] toArray(List<?> list) {
  72         return new Object[list.size()];
  73     }
  74 
  75     @Test
  76     public void test0() {
  77         test("test0Snippet", new ArrayList<>(Arrays.asList("a", "b")), true);
  78     }
  79 
  80     @Test
  81     public void test1() {
  82         test("test1Snippet", new ArrayList<>(Arrays.asList("a", "b")), true, true);
  83     }
  84 }