Skip to content

Commit 29b6392

Browse files
committedJun 17, 2024
8334228: C2 SuperWord: fix JDK-24 regression in VPointer::cmp_for_sort after JDK-8325155
Reviewed-by: chagedorn, kvn
1 parent 31e8deb commit 29b6392

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed
 

‎src/hotspot/share/opto/vectorization.cpp

+15-14
Original file line numberDiff line numberDiff line change
@@ -807,24 +807,26 @@ void VPointer::maybe_add_to_invar(Node* new_invar, bool negate) {
807807
_invar = register_if_new(add);
808808
}
809809

810+
// We use two comparisons, because a subtraction could underflow.
811+
#define RETURN_CMP_VALUE_IF_NOT_EQUAL(a, b) \
812+
if (a < b) { return -1; } \
813+
if (a > b) { return 1; }
814+
810815
// To be in the same group, two VPointers must be the same,
811816
// except for the offset.
812817
int VPointer::cmp_for_sort_by_group(const VPointer** p1, const VPointer** p2) {
813818
const VPointer* a = *p1;
814819
const VPointer* b = *p2;
815820

816-
int cmp_base = a->base()->_idx - b->base()->_idx;
817-
if (cmp_base != 0) { return cmp_base; }
818-
819-
int cmp_opcode = a->mem()->Opcode() - b->mem()->Opcode();
820-
if (cmp_opcode != 0) { return cmp_opcode; }
821+
RETURN_CMP_VALUE_IF_NOT_EQUAL(a->base()->_idx, b->base()->_idx);
822+
RETURN_CMP_VALUE_IF_NOT_EQUAL(a->mem()->Opcode(), b->mem()->Opcode());
823+
RETURN_CMP_VALUE_IF_NOT_EQUAL(a->scale_in_bytes(), b->scale_in_bytes());
821824

822-
int cmp_scale = a->scale_in_bytes() - b->scale_in_bytes();
823-
if (cmp_scale != 0) { return cmp_scale; }
825+
int a_inva_idx = a->invar() == nullptr ? 0 : a->invar()->_idx;
826+
int b_inva_idx = b->invar() == nullptr ? 0 : b->invar()->_idx;
827+
RETURN_CMP_VALUE_IF_NOT_EQUAL(a_inva_idx, b_inva_idx);
824828

825-
int cmp_invar = (a->invar() == nullptr ? 0 : a->invar()->_idx) -
826-
(b->invar() == nullptr ? 0 : b->invar()->_idx);
827-
return cmp_invar;
829+
return 0; // equal
828830
}
829831

830832
// We compare by group, then by offset, and finally by node idx.
@@ -835,10 +837,9 @@ int VPointer::cmp_for_sort(const VPointer** p1, const VPointer** p2) {
835837
const VPointer* a = *p1;
836838
const VPointer* b = *p2;
837839

838-
int cmp_offset = a->offset_in_bytes() - b->offset_in_bytes();
839-
if (cmp_offset != 0) { return cmp_offset; }
840-
841-
return a->mem()->_idx - b->mem()->_idx;
840+
RETURN_CMP_VALUE_IF_NOT_EQUAL(a->offset_in_bytes(), b->offset_in_bytes());
841+
RETURN_CMP_VALUE_IF_NOT_EQUAL(a->mem()->_idx, b->mem()->_idx);
842+
return 0; // equal
842843
}
843844

844845
#ifndef PRODUCT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2024, 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 8334228
27+
* @summary Test sorting of VPointer by offset, when subtraction of two offsets can overflow.
28+
* @run main/othervm -XX:CompileCommand=compileonly,compiler.vectorization.TestOffsetSorting::test -Xcomp compiler.vectorization.TestOffsetSorting
29+
* @run main compiler.vectorization.TestOffsetSorting
30+
*/
31+
32+
package compiler.vectorization;
33+
34+
public class TestOffsetSorting {
35+
static int RANGE = 10_000;
36+
37+
public static void main(String[] args) {
38+
int[] a = new int[RANGE];
39+
for (int i = 0; i < 10_000; i++) {
40+
try {
41+
test(a, 0);
42+
throw new RuntimeException("test should go out-of-bounds");
43+
} catch (ArrayIndexOutOfBoundsException e) {
44+
}
45+
}
46+
}
47+
48+
static void test(int[] a, int invar) {
49+
int large = (1 << 28) + (1 << 20);
50+
for (int i = 0; i < 1_000; i++) {
51+
a[i + invar - large] = 42;
52+
a[i + invar + large] = 42;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)
Failed to load comments.