From 7257500c3d4df6e6596d01ffd316949e540e065e Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Tue, 10 Dec 2024 16:40:12 +0100 Subject: [PATCH 1/7] try to fix --- src/hotspot/share/opto/loopPredicate.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hotspot/share/opto/loopPredicate.cpp b/src/hotspot/share/opto/loopPredicate.cpp index 9340f7e097846..a305230edaf44 100644 --- a/src/hotspot/share/opto/loopPredicate.cpp +++ b/src/hotspot/share/opto/loopPredicate.cpp @@ -469,7 +469,7 @@ class Invariance : public StackObj { // loop, it was marked invariant but n is only invariant if // it depends only on that test. Otherwise, unless that test // is out of the loop, it's not invariant. - if (n->is_CFG() || n->depends_only_on_test() || n->in(0) == nullptr || !_phase->is_member(_lpt, n->in(0))) { + if (n->is_CFG() || (n->depends_only_on_test() && _phase->igvn().no_dependent_zero_check(n)) || n->in(0) == nullptr || !_phase->is_member(_lpt, n->in(0))) { _invariant.set(n->_idx); // I am a invariant too } } From cb906016f5f843adb3596e65386b9671829a68e6 Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Wed, 11 Dec 2024 09:37:10 +0100 Subject: [PATCH 2/7] Add tests --- .../TestLoopPredicationDivZeroCheck.java | 53 +++++++++++++++++ .../TestLoopPredicationDivZeroCheck2.java | 59 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java create mode 100644 test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java diff --git a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java new file mode 100644 index 0000000000000..ea85b2d2ac7fa --- /dev/null +++ b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @bug 8331717 + * @summary C2: Crash with SIGFPE + * + * @run main/othervm -XX:CompileCommand=compileonly,*TestLoopPredicationDivZeroCheck*::* -XX:-TieredCompilation -Xbatch TestLoopPredicationDivZeroCheck + */ + +public class TestLoopPredicationDivZeroCheck { + static int iArr[] = new int[100]; + + public static void main(String[] strArr) { + for (int i = 0; i < 10000; i++) { + test(); + } + } + + static void test() { + int i1 = 0; + + for (int i4 : iArr) { + i4 = i1; + try { + iArr[0] = 1 / i4; // Also reproduces with % + i4 = iArr[2 / i4]; // Also reproduces with % + } catch (ArithmeticException a_e) { + } + } + } +} diff --git a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java new file mode 100644 index 0000000000000..edda7d5994aeb --- /dev/null +++ b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @bug 8331717 + * @summary C2: Crash with SIGFPE + * + * @run main/othervm -XX:CompileCommand=compileonly,*TestLoopPredicationDivZeroCheck2*::* -XX:-TieredCompilation -Xbatch TestLoopPredicationDivZeroCheck2 + */ + +public class TestLoopPredicationDivZeroCheck2 { + static volatile long lFld; + static int iFld; + + public static void main(String[] strArr) { + test(); + } + + static void test() { + int x = 0; + int y = iFld; + long lArr[] = new long[400]; + boolean bArr[] = new boolean[400]; + for (int i = 0; i < 10000; i++) { + for (int j = 1; j < 13; j++) { + for (int k = 1; k < 2; k++) { + lFld = 0; + lArr[1] = 7; + try { + x = 3 / y; + } catch (ArithmeticException a_e) { + } + bArr[x / 30] = true; + } + } + } + } +} From 2e1926f030ad52991329ac158eaff825a05fe0ef Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Wed, 11 Dec 2024 14:26:59 +0100 Subject: [PATCH 3/7] Describe tests --- .../loopopts/TestLoopPredicationDivZeroCheck.java | 11 +++++++++-- .../loopopts/TestLoopPredicationDivZeroCheck2.java | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java index ea85b2d2ac7fa..2e8913dc84f39 100644 --- a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java +++ b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java @@ -29,6 +29,13 @@ * @run main/othervm -XX:CompileCommand=compileonly,*TestLoopPredicationDivZeroCheck*::* -XX:-TieredCompilation -Xbatch TestLoopPredicationDivZeroCheck */ +/* + * The division 2 / i4 requires a non-zero check. As the result is an array access, it will be the input to a range + * check. Loop predication will try to move the range check and the division to right before the loop as the division + * appears to be invariant (i4 is always 0). However, the division is not truly invariant as it requires the zero + * check for i4 that can throw an exception. The bug fixed 8331717 caused the division to still be moved before the + * for loop with the range check. + */ public class TestLoopPredicationDivZeroCheck { static int iArr[] = new int[100]; @@ -44,8 +51,8 @@ static void test() { for (int i4 : iArr) { i4 = i1; try { - iArr[0] = 1 / i4; // Also reproduces with % - i4 = iArr[2 / i4]; // Also reproduces with % + iArr[0] = 1 / i4; + i4 = iArr[2 / i4]; } catch (ArithmeticException a_e) { } } diff --git a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java index edda7d5994aeb..5868fbf0b56cc 100644 --- a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java +++ b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java @@ -29,6 +29,10 @@ * @run main/othervm -XX:CompileCommand=compileonly,*TestLoopPredicationDivZeroCheck2*::* -XX:-TieredCompilation -Xbatch TestLoopPredicationDivZeroCheck2 */ +/* + * Loop predication will try to move 3 / y (input to the range check for bArr[x / 30]) before outside loop but it may + * not as y must be zero-checked. See TestLoopPredicationDivZeroCheck for a more detailed explanation. + */ public class TestLoopPredicationDivZeroCheck2 { static volatile long lFld; static int iFld; From 9d4dca493fd1e6a3094c8863266a2b0a43dc2abc Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Wed, 11 Dec 2024 14:27:45 +0100 Subject: [PATCH 4/7] Fix copyright --- .../compiler/loopopts/TestLoopPredicationDivZeroCheck.java | 2 +- .../compiler/loopopts/TestLoopPredicationDivZeroCheck2.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java index 2e8913dc84f39..d9b27a1583efa 100644 --- a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java +++ b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it diff --git a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java index 5868fbf0b56cc..1ec49cf6c11ba 100644 --- a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java +++ b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it From 032fae33cd6d1dc0ade67fa2d497347c0f5ffaf7 Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Wed, 11 Dec 2024 14:28:31 +0100 Subject: [PATCH 5/7] Update TestLoopPredicationDivZeroCheck2.java --- .../compiler/loopopts/TestLoopPredicationDivZeroCheck2.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java index 1ec49cf6c11ba..9f226e5c6d415 100644 --- a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java +++ b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java @@ -30,8 +30,8 @@ */ /* - * Loop predication will try to move 3 / y (input to the range check for bArr[x / 30]) before outside loop but it may - * not as y must be zero-checked. See TestLoopPredicationDivZeroCheck for a more detailed explanation. + * Loop predication will try to move 3 / y (input to the range check for bArr[x / 30]) before its containing for loop + * but it may not as y must be zero-checked. See TestLoopPredicationDivZeroCheck for a more detailed explanation. */ public class TestLoopPredicationDivZeroCheck2 { static volatile long lFld; From 50146ee8c373ccfa1b25a189859a3eb9f9d91eba Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Wed, 11 Dec 2024 14:46:16 +0100 Subject: [PATCH 6/7] Fix typo --- .../compiler/loopopts/TestLoopPredicationDivZeroCheck.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java index d9b27a1583efa..e4196424021ba 100644 --- a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java +++ b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java @@ -33,7 +33,7 @@ * The division 2 / i4 requires a non-zero check. As the result is an array access, it will be the input to a range * check. Loop predication will try to move the range check and the division to right before the loop as the division * appears to be invariant (i4 is always 0). However, the division is not truly invariant as it requires the zero - * check for i4 that can throw an exception. The bug fixed 8331717 caused the division to still be moved before the + * check for i4 that can throw an exception. The bug fixed in 8331717 caused the division to still be moved before the * for loop with the range check. */ public class TestLoopPredicationDivZeroCheck { From 8da7cb585a422fde0d6b0926d0c11f26bf5ba6db Mon Sep 17 00:00:00 2001 From: theoweidmannoracle Date: Thu, 12 Dec 2024 09:43:13 +0100 Subject: [PATCH 7/7] Combine test files --- .../TestLoopPredicationDivZeroCheck.java | 43 ++++++++++--- .../TestLoopPredicationDivZeroCheck2.java | 63 ------------------- 2 files changed, 36 insertions(+), 70 deletions(-) delete mode 100644 test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java diff --git a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java index e4196424021ba..82935475aa0a5 100644 --- a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java +++ b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck.java @@ -29,22 +29,27 @@ * @run main/othervm -XX:CompileCommand=compileonly,*TestLoopPredicationDivZeroCheck*::* -XX:-TieredCompilation -Xbatch TestLoopPredicationDivZeroCheck */ -/* - * The division 2 / i4 requires a non-zero check. As the result is an array access, it will be the input to a range - * check. Loop predication will try to move the range check and the division to right before the loop as the division - * appears to be invariant (i4 is always 0). However, the division is not truly invariant as it requires the zero - * check for i4 that can throw an exception. The bug fixed in 8331717 caused the division to still be moved before the - * for loop with the range check. - */ public class TestLoopPredicationDivZeroCheck { static int iArr[] = new int[100]; + static volatile long lFld; + static int iFld; public static void main(String[] strArr) { for (int i = 0; i < 10000; i++) { test(); } + for (int i = 0; i < 10000; i++) { + test2(); + } } + /* + * The division 2 / i4 requires a non-zero check. As the result is an array access, it will be the input to a range + * check. Loop predication will try to move the range check and the division to right before the loop as the division + * appears to be invariant (i4 is always 0). However, the division is not truly invariant as it requires the zero + * check for i4 that can throw an exception. The bug fixed in 8331717 caused the division to still be moved before the + * for loop with the range check. + */ static void test() { int i1 = 0; @@ -57,4 +62,28 @@ static void test() { } } } + + /* + * Loop predication will try to move 3 / y (input to the range check for bArr[x / 30]) before its containing for loop + * but it may not as y must be zero-checked. The same problem as above occurred before the fix in 8331717. + */ + static void test2() { + int x = 0; + int y = iFld; + long lArr[] = new long[400]; + boolean bArr[] = new boolean[400]; + for (int i = 0; i < 10000; i++) { + for (int j = 1; j < 13; j++) { + for (int k = 1; k < 2; k++) { + lFld = 0; + lArr[1] = 7; + try { + x = 3 / y; + } catch (ArithmeticException a_e) { + } + bArr[x / 30] = true; + } + } + } + } } diff --git a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java b/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java deleted file mode 100644 index 9f226e5c6d415..0000000000000 --- a/test/hotspot/jtreg/compiler/loopopts/TestLoopPredicationDivZeroCheck2.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/** - * @test - * @bug 8331717 - * @summary C2: Crash with SIGFPE - * - * @run main/othervm -XX:CompileCommand=compileonly,*TestLoopPredicationDivZeroCheck2*::* -XX:-TieredCompilation -Xbatch TestLoopPredicationDivZeroCheck2 - */ - -/* - * Loop predication will try to move 3 / y (input to the range check for bArr[x / 30]) before its containing for loop - * but it may not as y must be zero-checked. See TestLoopPredicationDivZeroCheck for a more detailed explanation. - */ -public class TestLoopPredicationDivZeroCheck2 { - static volatile long lFld; - static int iFld; - - public static void main(String[] strArr) { - test(); - } - - static void test() { - int x = 0; - int y = iFld; - long lArr[] = new long[400]; - boolean bArr[] = new boolean[400]; - for (int i = 0; i < 10000; i++) { - for (int j = 1; j < 13; j++) { - for (int k = 1; k < 2; k++) { - lFld = 0; - lArr[1] = 7; - try { - x = 3 / y; - } catch (ArithmeticException a_e) { - } - bArr[x / 30] = true; - } - } - } - } -}