Skip to content

Commit 48c1f60

Browse files
committed
8263227: C2: inconsistent spilling due to dead nodes in exception block
Eliminate dead nodes created by call-catch cleanup even if they have multiple projections. Assert that definitions dominate uses and projections are scheduled next to their parent nodes. Reviewed-by: roland Backport-of: d81b046
1 parent dcd1044 commit 48c1f60

File tree

4 files changed

+115
-8
lines changed

4 files changed

+115
-8
lines changed

src/hotspot/share/opto/block.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,12 +1218,18 @@ void PhaseCFG::verify() const {
12181218
for (uint k = 0; k < n->req(); k++) {
12191219
Node *def = n->in(k);
12201220
if (def && def != n) {
1221-
assert(get_block_for_node(def) || def->is_Con(), "must have block; constants for debug info ok");
1222-
// Verify that instructions in the block is in correct order.
1221+
Block* def_block = get_block_for_node(def);
1222+
assert(def_block || def->is_Con(), "must have block; constants for debug info ok");
1223+
// Verify that all definitions dominate their uses (except for virtual
1224+
// instructions merging multiple definitions).
1225+
assert(n->is_Root() || n->is_Region() || n->is_Phi() || n->is_MachMerge() ||
1226+
def_block->dominates(block),
1227+
"uses must be dominated by definitions");
1228+
// Verify that instructions in the block are in correct order.
12231229
// Uses must follow their definition if they are at the same block.
12241230
// Mostly done to check that MachSpillCopy nodes are placed correctly
12251231
// when CreateEx node is moved in build_ifg_physical().
1226-
if (get_block_for_node(def) == block && !(block->head()->is_Loop() && n->is_Phi()) &&
1232+
if (def_block == block && !(block->head()->is_Loop() && n->is_Phi()) &&
12271233
// See (+++) comment in reg_split.cpp
12281234
!(n->jvms() != NULL && n->jvms()->is_monitor_use(k))) {
12291235
bool is_loop = false;
@@ -1239,6 +1245,14 @@ void PhaseCFG::verify() const {
12391245
}
12401246
}
12411247
}
1248+
if (n->is_Proj()) {
1249+
assert(j >= 1, "a projection cannot be the first instruction in a block");
1250+
Node* pred = block->get_node(j - 1);
1251+
Node* parent = n->in(0);
1252+
assert(parent != NULL, "projections must have a parent");
1253+
assert(pred == parent || (pred->is_Proj() && pred->in(0) == parent),
1254+
"projections must follow their parents or other sibling projections");
1255+
}
12421256
}
12431257

12441258
j = block->end_idx();

src/hotspot/share/opto/lcm.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,14 +1390,40 @@ void PhaseCFG::call_catch_cleanup(Block* block) {
13901390
}
13911391

13921392
// If the successor blocks have a CreateEx node, move it back to the top
1393-
for(uint i4 = 0; i4 < block->_num_succs; i4++ ) {
1393+
for (uint i4 = 0; i4 < block->_num_succs; i4++) {
13941394
Block *sb = block->_succs[i4];
13951395
uint new_cnt = end - beg;
1396-
// Remove any newly created, but dead, nodes.
1397-
for( uint j = new_cnt; j > 0; j-- ) {
1396+
// Remove any newly created, but dead, nodes by traversing their schedule
1397+
// backwards. Here, a dead node is a node whose only outputs (if any) are
1398+
// unused projections.
1399+
for (uint j = new_cnt; j > 0; j--) {
13981400
Node *n = sb->get_node(j);
1399-
if (n->outcnt() == 0 &&
1400-
(!n->is_Proj() || n->as_Proj()->in(0)->outcnt() == 1) ){
1401+
// Individual projections are examined together with all siblings when
1402+
// their parent is visited.
1403+
if (n->is_Proj()) {
1404+
continue;
1405+
}
1406+
bool dead = true;
1407+
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1408+
Node* out = n->fast_out(i);
1409+
// n is live if it has a non-projection output or a used projection.
1410+
if (!out->is_Proj() || out->outcnt() > 0) {
1411+
dead = false;
1412+
break;
1413+
}
1414+
}
1415+
if (dead) {
1416+
// n's only outputs (if any) are unused projections scheduled next to n
1417+
// (see PhaseCFG::select()). Remove these projections backwards.
1418+
for (uint k = j + n->outcnt(); k > j; k--) {
1419+
Node* proj = sb->get_node(k);
1420+
assert(proj->is_Proj() && proj->in(0) == n,
1421+
"projection should correspond to dead node");
1422+
proj->disconnect_inputs(NULL, C);
1423+
sb->remove_node(k);
1424+
new_cnt--;
1425+
}
1426+
// Now remove the node itself.
14011427
n->disconnect_inputs(NULL, C);
14021428
sb->remove_node(j);
14031429
new_cnt--;

src/hotspot/share/opto/reg_split.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ void PhaseChaitin::insert_proj( Block *b, uint i, Node *spill, uint maxlrg ) {
114114
// Do not insert between a call and his Catch
115115
if( b->get_node(i)->is_Catch() ) {
116116
// Put the instruction at the top of the fall-thru block.
117+
// This assumes that the instruction is not used in the other exception
118+
// blocks. Global code motion is responsible for maintaining this invariant.
117119
// Find the fall-thru projection
118120
while( 1 ) {
119121
const CatchProjNode *cp = b->get_node(++i)->as_CatchProj();
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2021, 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+
package compiler.exceptions;
25+
26+
/**
27+
* @test
28+
* @bug 8263227
29+
* @summary Tests that users of return values from exception-throwing method
30+
* calls are not duplicated in the call's exception path. The second
31+
* run with a variable seed is added for test robustness.
32+
* @library /test/lib /
33+
* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions
34+
* -XX:+UnlockDiagnosticVMOptions
35+
* -Xbatch -XX:+StressGCM -XX:StressSeed=0
36+
* -XX:+VerifyRegisterAllocator
37+
* -XX:CompileCommand=dontinline,java.lang.Integer::*
38+
* compiler.exceptions.TestSpilling
39+
* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions
40+
* -XX:+UnlockDiagnosticVMOptions
41+
* -Xbatch -XX:+StressGCM
42+
* -XX:+VerifyRegisterAllocator
43+
* -XX:CompileCommand=dontinline,java.lang.Integer::*
44+
* compiler.exceptions.TestSpilling
45+
*/
46+
47+
public class TestSpilling {
48+
49+
public static void test() {
50+
int a = Integer.valueOf(42).intValue();
51+
// After global code motion, the logic below should only be placed in
52+
// the fall-through path of java.lang.Integer::intValue(). Otherwise,
53+
// live range splitting might create uses without reaching definitions
54+
// if 'a' is spilled.
55+
int b = (((a & 0x0000F000)) + 1);
56+
int c = a / b + ((a % b > 0) ? 1 : 0);
57+
}
58+
59+
public static void main(String[] args) {
60+
for (int i = 0; i < 10_000; i++) {
61+
test();
62+
}
63+
}
64+
65+
}

0 commit comments

Comments
 (0)