Skip to content

Commit e132605

Browse files
committed
8296545: C2 Blackholes should allow load optimizations
Reviewed-by: mdoerr Backport-of: eab0ada3a16a432fdfd1f0b8fceca149c725451b
1 parent d5cbf22 commit e132605

File tree

8 files changed

+169
-43
lines changed

8 files changed

+169
-43
lines changed

src/hotspot/share/opto/cfgnode.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "opto/narrowptrnode.hpp"
4040
#include "opto/mulnode.hpp"
4141
#include "opto/phaseX.hpp"
42+
#include "opto/regalloc.hpp"
4243
#include "opto/regmask.hpp"
4344
#include "opto/runtime.hpp"
4445
#include "opto/subnode.hpp"
@@ -2801,3 +2802,25 @@ void NeverBranchNode::format( PhaseRegAlloc *ra_, outputStream *st) const {
28012802
st->print("%s", Name());
28022803
}
28032804
#endif
2805+
2806+
#ifndef PRODUCT
2807+
void BlackholeNode::format(PhaseRegAlloc* ra, outputStream* st) const {
2808+
st->print("blackhole ");
2809+
bool first = true;
2810+
for (uint i = 0; i < req(); i++) {
2811+
Node* n = in(i);
2812+
if (n != NULL && OptoReg::is_valid(ra->get_reg_first(n))) {
2813+
if (first) {
2814+
first = false;
2815+
} else {
2816+
st->print(", ");
2817+
}
2818+
char buf[128];
2819+
ra->dump_register(n, buf);
2820+
st->print("%s", buf);
2821+
}
2822+
}
2823+
st->cr();
2824+
}
2825+
#endif
2826+

src/hotspot/share/opto/cfgnode.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class PCTableNode;
4747
class JumpNode;
4848
class CatchNode;
4949
class NeverBranchNode;
50+
class BlackholeNode;
5051
class ProjNode;
5152
class CProjNode;
5253
class IfTrueNode;
@@ -623,4 +624,28 @@ class NeverBranchNode : public MultiBranchNode {
623624
#endif
624625
};
625626

627+
//------------------------------BlackholeNode----------------------------
628+
// Blackhole all arguments. This node would survive through the compiler
629+
// the effects on its arguments, and would be finally matched to nothing.
630+
class BlackholeNode : public MultiNode {
631+
public:
632+
BlackholeNode(Node* ctrl) : MultiNode(1) {
633+
init_req(TypeFunc::Control, ctrl);
634+
}
635+
virtual int Opcode() const;
636+
virtual uint ideal_reg() const { return 0; } // not matched in the AD file
637+
virtual const Type* bottom_type() const { return TypeTuple::MEMBAR; }
638+
639+
const RegMask &in_RegMask(uint idx) const {
640+
// Fake the incoming arguments mask for blackholes: accept all registers
641+
// and all stack slots. This would avoid any redundant register moves
642+
// for blackhole inputs.
643+
return RegMask::All;
644+
}
645+
#ifndef PRODUCT
646+
virtual void format(PhaseRegAlloc* ra, outputStream* st) const;
647+
#endif
648+
};
649+
650+
626651
#endif // SHARE_OPTO_CFGNODE_HPP

src/hotspot/share/opto/library_call.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7174,8 +7174,15 @@ bool LibraryCallKit::inline_blackhole() {
71747174
assert(callee()->is_empty(), "Should have been checked before: only empty methods here");
71757175
assert(callee()->holder()->is_loaded(), "Should have been checked before: only methods for loaded classes here");
71767176

7177+
// Blackhole node pinches only the control, not memory. This allows
7178+
// the blackhole to be pinned in the loop that computes blackholed
7179+
// values, but have no other side effects, like breaking the optimizations
7180+
// across the blackhole.
7181+
7182+
Node* bh = _gvn.transform(new BlackholeNode(control()));
7183+
set_control(_gvn.transform(new ProjNode(bh, TypeFunc::Control)));
7184+
71777185
// Bind call arguments as blackhole arguments to keep them alive
7178-
Node* bh = insert_mem_bar(Op_Blackhole);
71797186
uint nargs = callee()->arg_size();
71807187
for (uint i = 0; i < nargs; i++) {
71817188
bh->add_req(argument(i));

src/hotspot/share/opto/memnode.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3295,7 +3295,6 @@ MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) {
32953295
case Op_MemBarCPUOrder: return new MemBarCPUOrderNode(C, atp, pn);
32963296
case Op_OnSpinWait: return new OnSpinWaitNode(C, atp, pn);
32973297
case Op_Initialize: return new InitializeNode(C, atp, pn);
3298-
case Op_Blackhole: return new BlackholeNode(C, atp, pn);
32993298
default: ShouldNotReachHere(); return NULL;
33003299
}
33013300
}
@@ -3535,26 +3534,6 @@ MemBarNode* MemBarNode::leading_membar() const {
35353534
return mb;
35363535
}
35373536

3538-
#ifndef PRODUCT
3539-
void BlackholeNode::format(PhaseRegAlloc* ra, outputStream* st) const {
3540-
st->print("blackhole ");
3541-
bool first = true;
3542-
for (uint i = 0; i < req(); i++) {
3543-
Node* n = in(i);
3544-
if (n != NULL && OptoReg::is_valid(ra->get_reg_first(n))) {
3545-
if (first) {
3546-
first = false;
3547-
} else {
3548-
st->print(", ");
3549-
}
3550-
char buf[128];
3551-
ra->dump_register(n, buf);
3552-
st->print("%s", buf);
3553-
}
3554-
}
3555-
st->cr();
3556-
}
3557-
#endif
35583537

35593538
//===========================InitializeNode====================================
35603539
// SUMMARY:

src/hotspot/share/opto/memnode.hpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,26 +1343,6 @@ class OnSpinWaitNode: public MemBarNode {
13431343
virtual int Opcode() const;
13441344
};
13451345

1346-
//------------------------------BlackholeNode----------------------------
1347-
// Blackhole all arguments. This node would survive through the compiler
1348-
// the effects on its arguments, and would be finally matched to nothing.
1349-
class BlackholeNode : public MemBarNode {
1350-
public:
1351-
BlackholeNode(Compile* C, int alias_idx, Node* precedent)
1352-
: MemBarNode(C, alias_idx, precedent) {}
1353-
virtual int Opcode() const;
1354-
virtual uint ideal_reg() const { return 0; } // not matched in the AD file
1355-
const RegMask &in_RegMask(uint idx) const {
1356-
// Fake the incoming arguments mask for blackholes: accept all registers
1357-
// and all stack slots. This would avoid any redundant register moves
1358-
// for blackhole inputs.
1359-
return RegMask::All;
1360-
}
1361-
#ifndef PRODUCT
1362-
virtual void format(PhaseRegAlloc* ra, outputStream* st) const;
1363-
#endif
1364-
};
1365-
13661346
// Isolation of object setup after an AllocateNode and before next safepoint.
13671347
// (See comment in memnode.cpp near InitializeNode::InitializeNode for semantics.)
13681348
class InitializeNode: public MemBarNode {

src/hotspot/share/runtime/vmStructs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ typedef HashtableEntry<InstanceKlass*, mtClass> KlassHashtableEntry;
15981598
declare_c2_type(MemBarVolatileNode, MemBarNode) \
15991599
declare_c2_type(MemBarCPUOrderNode, MemBarNode) \
16001600
declare_c2_type(OnSpinWaitNode, MemBarNode) \
1601-
declare_c2_type(BlackholeNode, MemBarNode) \
1601+
declare_c2_type(BlackholeNode, MultiNode) \
16021602
declare_c2_type(InitializeNode, MemBarNode) \
16031603
declare_c2_type(ThreadLocalNode, Node) \
16041604
declare_c2_type(Opaque1Node, Node) \
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2022, Red Hat, Inc. 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 8296545
27+
* @requires vm.compiler2.enabled
28+
* @summary Blackholes should allow load optimizations
29+
* @library /test/lib /
30+
* @run driver compiler.c2.irTests.blackhole.BlackholeLoadOptoTest
31+
*/
32+
33+
package compiler.c2.irTests.blackhole;
34+
35+
import compiler.lib.ir_framework.*;
36+
import jdk.test.lib.Asserts;
37+
38+
public class BlackholeLoadOptoTest {
39+
40+
public static void main(String[] args) {
41+
TestFramework.runWithFlags(
42+
"-XX:+UnlockExperimentalVMOptions",
43+
"-XX:CompileThreshold=100",
44+
"-XX:-TieredCompilation",
45+
"-XX:CompileCommand=blackhole,compiler.c2.irTests.blackhole.BlackholeLoadOptoTest::blackhole",
46+
"-XX:CompileCommand=dontinline,compiler.c2.irTests.blackhole.BlackholeLoadOptoTest::dontinline"
47+
);
48+
}
49+
50+
static int x, y;
51+
52+
53+
/*
54+
* Negative test: check that dangling expressions are eliminated
55+
*/
56+
57+
@Test
58+
@IR(failOn = {IRNode.LOAD_I, IRNode.MUL_I})
59+
static void testNothing() {
60+
int r1 = x * y;
61+
int r2 = x * y;
62+
}
63+
64+
@Run(test = "testNothing")
65+
static void runNothing() {
66+
testNothing();
67+
}
68+
69+
/*
70+
* Auxiliary test: check that dontinline method does break optimizations
71+
*/
72+
73+
@Test
74+
@IR(counts = {IRNode.LOAD_I, "4"})
75+
@IR(counts = {IRNode.MUL_I, "2"})
76+
static void testDontline() {
77+
int r1 = x * y;
78+
dontinline(r1);
79+
int r2 = x * y;
80+
dontinline(r2);
81+
}
82+
83+
static void dontinline(int x) {}
84+
85+
@Run(test = "testDontline")
86+
static void runDontinline() {
87+
testDontline();
88+
}
89+
90+
/*
91+
* Positive test: check that blackhole does not break optimizations
92+
*/
93+
94+
@Test
95+
@IR(counts = {IRNode.LOAD_I, "2"})
96+
@IR(counts = {IRNode.MUL_I, "1"})
97+
static void testBlackholed() {
98+
int r1 = x * y;
99+
blackhole(r1);
100+
int r2 = x * y;
101+
blackhole(r2);
102+
}
103+
104+
static void blackhole(int x) {}
105+
106+
@Run(test = "testBlackholed")
107+
static void runBlackholed() {
108+
testBlackholed();
109+
}
110+
111+
}

test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public class IRNode {
138138

139139
public static final String CMP_U = START + "CmpU" + MID + END;
140140
public static final String CMP_I = START + "CmpI" + MID + END;
141+
public static final String MUL_I = START + "MulI" + MID + END;
141142
public static final String MUL_L = START + "MulL" + MID + END;
142143
public static final String POPCOUNT_L = START + "PopCountL" + MID + END;
143144

0 commit comments

Comments
 (0)