Skip to content

Commit

Permalink
8296545: C2 Blackholes should allow load optimizations
Browse files Browse the repository at this point in the history
Reviewed-by: mdoerr
Backport-of: eab0ada3a16a432fdfd1f0b8fceca149c725451b
  • Loading branch information
shipilev committed Apr 25, 2023
1 parent d5cbf22 commit e132605
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 43 deletions.
23 changes: 23 additions & 0 deletions src/hotspot/share/opto/cfgnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "opto/narrowptrnode.hpp"
#include "opto/mulnode.hpp"
#include "opto/phaseX.hpp"
#include "opto/regalloc.hpp"
#include "opto/regmask.hpp"
#include "opto/runtime.hpp"
#include "opto/subnode.hpp"
Expand Down Expand Up @@ -2801,3 +2802,25 @@ void NeverBranchNode::format( PhaseRegAlloc *ra_, outputStream *st) const {
st->print("%s", Name());
}
#endif

#ifndef PRODUCT
void BlackholeNode::format(PhaseRegAlloc* ra, outputStream* st) const {
st->print("blackhole ");
bool first = true;
for (uint i = 0; i < req(); i++) {
Node* n = in(i);
if (n != NULL && OptoReg::is_valid(ra->get_reg_first(n))) {
if (first) {
first = false;
} else {
st->print(", ");
}
char buf[128];
ra->dump_register(n, buf);
st->print("%s", buf);
}
}
st->cr();
}
#endif

25 changes: 25 additions & 0 deletions src/hotspot/share/opto/cfgnode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class PCTableNode;
class JumpNode;
class CatchNode;
class NeverBranchNode;
class BlackholeNode;
class ProjNode;
class CProjNode;
class IfTrueNode;
Expand Down Expand Up @@ -623,4 +624,28 @@ class NeverBranchNode : public MultiBranchNode {
#endif
};

//------------------------------BlackholeNode----------------------------
// Blackhole all arguments. This node would survive through the compiler
// the effects on its arguments, and would be finally matched to nothing.
class BlackholeNode : public MultiNode {
public:
BlackholeNode(Node* ctrl) : MultiNode(1) {
init_req(TypeFunc::Control, ctrl);
}
virtual int Opcode() const;
virtual uint ideal_reg() const { return 0; } // not matched in the AD file
virtual const Type* bottom_type() const { return TypeTuple::MEMBAR; }

const RegMask &in_RegMask(uint idx) const {
// Fake the incoming arguments mask for blackholes: accept all registers
// and all stack slots. This would avoid any redundant register moves
// for blackhole inputs.
return RegMask::All;
}
#ifndef PRODUCT
virtual void format(PhaseRegAlloc* ra, outputStream* st) const;
#endif
};


#endif // SHARE_OPTO_CFGNODE_HPP
9 changes: 8 additions & 1 deletion src/hotspot/share/opto/library_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7174,8 +7174,15 @@ bool LibraryCallKit::inline_blackhole() {
assert(callee()->is_empty(), "Should have been checked before: only empty methods here");
assert(callee()->holder()->is_loaded(), "Should have been checked before: only methods for loaded classes here");

// Blackhole node pinches only the control, not memory. This allows
// the blackhole to be pinned in the loop that computes blackholed
// values, but have no other side effects, like breaking the optimizations
// across the blackhole.

Node* bh = _gvn.transform(new BlackholeNode(control()));
set_control(_gvn.transform(new ProjNode(bh, TypeFunc::Control)));

// Bind call arguments as blackhole arguments to keep them alive
Node* bh = insert_mem_bar(Op_Blackhole);
uint nargs = callee()->arg_size();
for (uint i = 0; i < nargs; i++) {
bh->add_req(argument(i));
Expand Down
21 changes: 0 additions & 21 deletions src/hotspot/share/opto/memnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3295,7 +3295,6 @@ MemBarNode* MemBarNode::make(Compile* C, int opcode, int atp, Node* pn) {
case Op_MemBarCPUOrder: return new MemBarCPUOrderNode(C, atp, pn);
case Op_OnSpinWait: return new OnSpinWaitNode(C, atp, pn);
case Op_Initialize: return new InitializeNode(C, atp, pn);
case Op_Blackhole: return new BlackholeNode(C, atp, pn);
default: ShouldNotReachHere(); return NULL;
}
}
Expand Down Expand Up @@ -3535,26 +3534,6 @@ MemBarNode* MemBarNode::leading_membar() const {
return mb;
}

#ifndef PRODUCT
void BlackholeNode::format(PhaseRegAlloc* ra, outputStream* st) const {
st->print("blackhole ");
bool first = true;
for (uint i = 0; i < req(); i++) {
Node* n = in(i);
if (n != NULL && OptoReg::is_valid(ra->get_reg_first(n))) {
if (first) {
first = false;
} else {
st->print(", ");
}
char buf[128];
ra->dump_register(n, buf);
st->print("%s", buf);
}
}
st->cr();
}
#endif

//===========================InitializeNode====================================
// SUMMARY:
Expand Down
20 changes: 0 additions & 20 deletions src/hotspot/share/opto/memnode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,26 +1343,6 @@ class OnSpinWaitNode: public MemBarNode {
virtual int Opcode() const;
};

//------------------------------BlackholeNode----------------------------
// Blackhole all arguments. This node would survive through the compiler
// the effects on its arguments, and would be finally matched to nothing.
class BlackholeNode : public MemBarNode {
public:
BlackholeNode(Compile* C, int alias_idx, Node* precedent)
: MemBarNode(C, alias_idx, precedent) {}
virtual int Opcode() const;
virtual uint ideal_reg() const { return 0; } // not matched in the AD file
const RegMask &in_RegMask(uint idx) const {
// Fake the incoming arguments mask for blackholes: accept all registers
// and all stack slots. This would avoid any redundant register moves
// for blackhole inputs.
return RegMask::All;
}
#ifndef PRODUCT
virtual void format(PhaseRegAlloc* ra, outputStream* st) const;
#endif
};

// Isolation of object setup after an AllocateNode and before next safepoint.
// (See comment in memnode.cpp near InitializeNode::InitializeNode for semantics.)
class InitializeNode: public MemBarNode {
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/runtime/vmStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,7 +1598,7 @@ typedef HashtableEntry<InstanceKlass*, mtClass> KlassHashtableEntry;
declare_c2_type(MemBarVolatileNode, MemBarNode) \
declare_c2_type(MemBarCPUOrderNode, MemBarNode) \
declare_c2_type(OnSpinWaitNode, MemBarNode) \
declare_c2_type(BlackholeNode, MemBarNode) \
declare_c2_type(BlackholeNode, MultiNode) \
declare_c2_type(InitializeNode, MemBarNode) \
declare_c2_type(ThreadLocalNode, Node) \
declare_c2_type(Opaque1Node, Node) \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2022, Red Hat, Inc. 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 8296545
* @requires vm.compiler2.enabled
* @summary Blackholes should allow load optimizations
* @library /test/lib /
* @run driver compiler.c2.irTests.blackhole.BlackholeLoadOptoTest
*/

package compiler.c2.irTests.blackhole;

import compiler.lib.ir_framework.*;
import jdk.test.lib.Asserts;

public class BlackholeLoadOptoTest {

public static void main(String[] args) {
TestFramework.runWithFlags(
"-XX:+UnlockExperimentalVMOptions",
"-XX:CompileThreshold=100",
"-XX:-TieredCompilation",
"-XX:CompileCommand=blackhole,compiler.c2.irTests.blackhole.BlackholeLoadOptoTest::blackhole",
"-XX:CompileCommand=dontinline,compiler.c2.irTests.blackhole.BlackholeLoadOptoTest::dontinline"
);
}

static int x, y;


/*
* Negative test: check that dangling expressions are eliminated
*/

@Test
@IR(failOn = {IRNode.LOAD_I, IRNode.MUL_I})
static void testNothing() {
int r1 = x * y;
int r2 = x * y;
}

@Run(test = "testNothing")
static void runNothing() {
testNothing();
}

/*
* Auxiliary test: check that dontinline method does break optimizations
*/

@Test
@IR(counts = {IRNode.LOAD_I, "4"})
@IR(counts = {IRNode.MUL_I, "2"})
static void testDontline() {
int r1 = x * y;
dontinline(r1);
int r2 = x * y;
dontinline(r2);
}

static void dontinline(int x) {}

@Run(test = "testDontline")
static void runDontinline() {
testDontline();
}

/*
* Positive test: check that blackhole does not break optimizations
*/

@Test
@IR(counts = {IRNode.LOAD_I, "2"})
@IR(counts = {IRNode.MUL_I, "1"})
static void testBlackholed() {
int r1 = x * y;
blackhole(r1);
int r2 = x * y;
blackhole(r2);
}

static void blackhole(int x) {}

@Run(test = "testBlackholed")
static void runBlackholed() {
testBlackholed();
}

}
1 change: 1 addition & 0 deletions test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public class IRNode {

public static final String CMP_U = START + "CmpU" + MID + END;
public static final String CMP_I = START + "CmpI" + MID + END;
public static final String MUL_I = START + "MulI" + MID + END;
public static final String MUL_L = START + "MulL" + MID + END;
public static final String POPCOUNT_L = START + "PopCountL" + MID + END;

Expand Down

1 comment on commit e132605

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.