Skip to content

Commit eab0ada

Browse files
committed
8296545: C2 Blackholes should allow load optimizations
Reviewed-by: kvn, vlivanov
1 parent dea2161 commit eab0ada

File tree

7 files changed

+168
-43
lines changed

7 files changed

+168
-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"
@@ -2814,3 +2815,25 @@ void NeverBranchNode::format( PhaseRegAlloc *ra_, outputStream *st) const {
28142815
st->print("%s", Name());
28152816
}
28162817
#endif
2818+
2819+
#ifndef PRODUCT
2820+
void BlackholeNode::format(PhaseRegAlloc* ra, outputStream* st) const {
2821+
st->print("blackhole ");
2822+
bool first = true;
2823+
for (uint i = 0; i < req(); i++) {
2824+
Node* n = in(i);
2825+
if (n != NULL && OptoReg::is_valid(ra->get_reg_first(n))) {
2826+
if (first) {
2827+
first = false;
2828+
} else {
2829+
st->print(", ");
2830+
}
2831+
char buf[128];
2832+
ra->dump_register(n, buf);
2833+
st->print("%s", buf);
2834+
}
2835+
}
2836+
st->cr();
2837+
}
2838+
#endif
2839+

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;
@@ -604,4 +605,28 @@ class NeverBranchNode : public MultiBranchNode {
604605
#endif
605606
};
606607

608+
//------------------------------BlackholeNode----------------------------
609+
// Blackhole all arguments. This node would survive through the compiler
610+
// the effects on its arguments, and would be finally matched to nothing.
611+
class BlackholeNode : public MultiNode {
612+
public:
613+
BlackholeNode(Node* ctrl) : MultiNode(1) {
614+
init_req(TypeFunc::Control, ctrl);
615+
}
616+
virtual int Opcode() const;
617+
virtual uint ideal_reg() const { return 0; } // not matched in the AD file
618+
virtual const Type* bottom_type() const { return TypeTuple::MEMBAR; }
619+
620+
const RegMask &in_RegMask(uint idx) const {
621+
// Fake the incoming arguments mask for blackholes: accept all registers
622+
// and all stack slots. This would avoid any redundant register moves
623+
// for blackhole inputs.
624+
return RegMask::All;
625+
}
626+
#ifndef PRODUCT
627+
virtual void format(PhaseRegAlloc* ra, outputStream* st) const;
628+
#endif
629+
};
630+
631+
607632
#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
@@ -7841,8 +7841,15 @@ bool LibraryCallKit::inline_blackhole() {
78417841
assert(callee()->is_empty(), "Should have been checked before: only empty methods here");
78427842
assert(callee()->holder()->is_loaded(), "Should have been checked before: only methods for loaded classes here");
78437843

7844+
// Blackhole node pinches only the control, not memory. This allows
7845+
// the blackhole to be pinned in the loop that computes blackholed
7846+
// values, but have no other side effects, like breaking the optimizations
7847+
// across the blackhole.
7848+
7849+
Node* bh = _gvn.transform(new BlackholeNode(control()));
7850+
set_control(_gvn.transform(new ProjNode(bh, TypeFunc::Control)));
7851+
78447852
// Bind call arguments as blackhole arguments to keep them alive
7845-
Node* bh = insert_mem_bar(Op_Blackhole);
78467853
uint nargs = callee()->arg_size();
78477854
for (uint i = 0; i < nargs; i++) {
78487855
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
@@ -3261,7 +3261,6 @@ MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) {
32613261
case Op_MemBarCPUOrder: return new MemBarCPUOrderNode(C, atp, pn);
32623262
case Op_OnSpinWait: return new OnSpinWaitNode(C, atp, pn);
32633263
case Op_Initialize: return new InitializeNode(C, atp, pn);
3264-
case Op_Blackhole: return new BlackholeNode(C, atp, pn);
32653264
default: ShouldNotReachHere(); return NULL;
32663265
}
32673266
}
@@ -3501,26 +3500,6 @@ MemBarNode* MemBarNode::leading_membar() const {
35013500
return mb;
35023501
}
35033502

3504-
#ifndef PRODUCT
3505-
void BlackholeNode::format(PhaseRegAlloc* ra, outputStream* st) const {
3506-
st->print("blackhole ");
3507-
bool first = true;
3508-
for (uint i = 0; i < req(); i++) {
3509-
Node* n = in(i);
3510-
if (n != NULL && OptoReg::is_valid(ra->get_reg_first(n))) {
3511-
if (first) {
3512-
first = false;
3513-
} else {
3514-
st->print(", ");
3515-
}
3516-
char buf[128];
3517-
ra->dump_register(n, buf);
3518-
st->print("%s", buf);
3519-
}
3520-
}
3521-
st->cr();
3522-
}
3523-
#endif
35243503

35253504
//===========================InitializeNode====================================
35263505
// SUMMARY:

src/hotspot/share/opto/memnode.hpp

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

1299-
//------------------------------BlackholeNode----------------------------
1300-
// Blackhole all arguments. This node would survive through the compiler
1301-
// the effects on its arguments, and would be finally matched to nothing.
1302-
class BlackholeNode : public MemBarNode {
1303-
public:
1304-
BlackholeNode(Compile* C, int alias_idx, Node* precedent)
1305-
: MemBarNode(C, alias_idx, precedent) {}
1306-
virtual int Opcode() const;
1307-
virtual uint ideal_reg() const { return 0; } // not matched in the AD file
1308-
const RegMask &in_RegMask(uint idx) const {
1309-
// Fake the incoming arguments mask for blackholes: accept all registers
1310-
// and all stack slots. This would avoid any redundant register moves
1311-
// for blackhole inputs.
1312-
return RegMask::All;
1313-
}
1314-
#ifndef PRODUCT
1315-
virtual void format(PhaseRegAlloc* ra, outputStream* st) const;
1316-
#endif
1317-
};
1318-
13191299
// Isolation of object setup after an AllocateNode and before next safepoint.
13201300
// (See comment in memnode.cpp near InitializeNode::InitializeNode for semantics.)
13211301
class InitializeNode: public MemBarNode {

src/hotspot/share/runtime/vmStructs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,7 @@
15651565
declare_c2_type(MemBarVolatileNode, MemBarNode) \
15661566
declare_c2_type(MemBarCPUOrderNode, MemBarNode) \
15671567
declare_c2_type(OnSpinWaitNode, MemBarNode) \
1568-
declare_c2_type(BlackholeNode, MemBarNode) \
1568+
declare_c2_type(BlackholeNode, MultiNode) \
15691569
declare_c2_type(InitializeNode, MemBarNode) \
15701570
declare_c2_type(ThreadLocalNode, Node) \
15711571
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+
}

0 commit comments

Comments
 (0)