|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 | + * @test |
| 26 | + * @bug 8251544 |
| 27 | + * @summary A dead data loop in dying code is not correctly removed resulting in unremovable data nodes. |
| 28 | + * @requires vm.compiler2.enabled |
| 29 | + * @library /test/lib / |
| 30 | + * @modules java.base/jdk.internal.misc |
| 31 | + * java.management |
| 32 | + * java.base/jdk.internal.access |
| 33 | + * java.base/jdk.internal.reflect |
| 34 | + * |
| 35 | + * @build sun.hotspot.WhiteBox |
| 36 | + * @run driver ClassFileInstaller sun.hotspot.WhiteBox |
| 37 | + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-TieredCompilation -Xbatch |
| 38 | + * compiler.c2.TestDeadDataLoopIGVN |
| 39 | + */ |
| 40 | + |
| 41 | +package compiler.c2; |
| 42 | + |
| 43 | +import java.lang.reflect.Method; |
| 44 | +import java.util.HashMap; |
| 45 | +import java.util.List; |
| 46 | +import java.util.LinkedList; |
| 47 | +import java.util.ArrayList; |
| 48 | +import java.util.Vector; |
| 49 | +import java.util.ListIterator; |
| 50 | +import jdk.internal.access.SharedSecrets; |
| 51 | +import jdk.internal.misc.Unsafe; |
| 52 | +import jdk.internal.reflect.ConstantPool; |
| 53 | +import java.net.URL; |
| 54 | +import java.net.URLClassLoader; |
| 55 | +import java.lang.reflect.Executable; |
| 56 | + |
| 57 | +import compiler.whitebox.CompilerWhiteBoxTest; |
| 58 | +import sun.hotspot.WhiteBox; |
| 59 | + |
| 60 | +public class TestDeadDataLoopIGVN { |
| 61 | + |
| 62 | + private static final WhiteBox WB = WhiteBox.getWhiteBox(); |
| 63 | + private static final int TIERED_STOP_AT_LEVEL = WB.getIntxVMFlag("TieredStopAtLevel").intValue(); |
| 64 | + private static final Unsafe UNSAFE = Unsafe.getUnsafe(); |
| 65 | + |
| 66 | + // The original test only failed with CTW due to different inlining and virtual call decisions compared to an |
| 67 | + // execution with -Xcomp. This test adapts the behavior of CTW and compiles the methods with the Whitebox API |
| 68 | + // in order to reproduce the bug. |
| 69 | + public static void main(String[] strArr) throws Exception { |
| 70 | + // Required to get the same inlining/virtual call decisions as for CTW |
| 71 | + callSomeMethods(new ArrayList<String>()); |
| 72 | + callSomeMethods(new Vector<String>()); |
| 73 | + |
| 74 | + if (TIERED_STOP_AT_LEVEL != CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION) { |
| 75 | + throw new RuntimeException("Sanity check if C2 is available"); |
| 76 | + } |
| 77 | + |
| 78 | + // To trigger the assertion, we only need to compile Test |
| 79 | + compileClass(Test.class); |
| 80 | + } |
| 81 | + |
| 82 | + private static void callSomeMethods(List<String> list) { |
| 83 | + list.add("bla"); |
| 84 | + list.add("foo"); |
| 85 | + ListIterator<String> it = list.listIterator(); |
| 86 | + it.hasNext(); |
| 87 | + for (String s : list) { |
| 88 | + s.charAt(0); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Adaptation from CTW to compile and deoptimize the same methods |
| 93 | + private static void compileClass(Class<?> aClass) throws Exception { |
| 94 | + aClass = Class.forName(aClass.getCanonicalName(), true, aClass.getClassLoader()); |
| 95 | + ConstantPool constantPool = SharedSecrets.getJavaLangAccess().getConstantPool(aClass); |
| 96 | + preloadClasses(constantPool); |
| 97 | + UNSAFE.ensureClassInitialized(aClass); |
| 98 | + WB.enqueueInitializerForCompilation(aClass, 4); // Level 4 for C2 |
| 99 | + |
| 100 | + for (Executable method : aClass.getDeclaredConstructors()) { |
| 101 | + WB.deoptimizeMethod(method); |
| 102 | + WB.enqueueMethodForCompilation(method, 4); |
| 103 | + WB.deoptimizeMethod(method); |
| 104 | + } |
| 105 | + |
| 106 | + for (Executable method : aClass.getDeclaredMethods()) { |
| 107 | + WB.deoptimizeMethod(method); |
| 108 | + WB.enqueueMethodForCompilation(method, 4); |
| 109 | + WB.deoptimizeMethod(method); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static void preloadClasses(ConstantPool constantPool) throws Exception { |
| 114 | + for (int i = 0, n = constantPool.getSize(); i < n; ++i) { |
| 115 | + try { |
| 116 | + constantPool.getClassAt(i); |
| 117 | + } catch (IllegalArgumentException ignore) { |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// The actual class that failed by executing it with CTW |
| 124 | +class Test { |
| 125 | + |
| 126 | + public static A a = new A(); |
| 127 | + |
| 128 | + Test() { |
| 129 | + LinkedList<A> l = new LinkedList<A>(); |
| 130 | + for (int i = 0; i < 34; i++) { |
| 131 | + A instance = new A(); |
| 132 | + instance.id = i; |
| 133 | + l.add(instance); |
| 134 | + } |
| 135 | + test(l, 34); |
| 136 | + } |
| 137 | + |
| 138 | + public void test(LinkedList<A> list, int max) { |
| 139 | + Integer[] numbers = new Integer[max + 1]; |
| 140 | + A[] numbers2 = new A[max + 1]; |
| 141 | + int n = 0; |
| 142 | + ListIterator<A> it = list.listIterator(); |
| 143 | + while (it.hasNext()) { |
| 144 | + A b = it.next(); |
| 145 | + numbers[b.get()] = n; |
| 146 | + numbers2[n] = b; |
| 147 | + n++; |
| 148 | + } |
| 149 | + |
| 150 | + Integer[] iArr = new Integer[max + 1]; |
| 151 | + |
| 152 | + A a = getA(); |
| 153 | + Integer x = numbers[a.get()]; |
| 154 | + iArr[x] = x; |
| 155 | + |
| 156 | + boolean flag = true; |
| 157 | + while (flag) { |
| 158 | + flag = false; |
| 159 | + it = list.listIterator(34); |
| 160 | + while (it.hasPrevious()) { |
| 161 | + A b = it.previous(); |
| 162 | + if (b == a) { |
| 163 | + continue; |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + HashMap<A, A> map = new HashMap<A, A>(); |
| 169 | + for (Integer i = 0; i < max - 34; i++) { |
| 170 | + map.put(numbers2[i], numbers2[iArr[i]]); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public A getA() { |
| 175 | + return a; |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +// Helper class |
| 180 | +class A { |
| 181 | + int id; |
| 182 | + |
| 183 | + public int get() { |
| 184 | + return id; |
| 185 | + } |
| 186 | +} |
0 commit comments