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