Skip to content

[CodeMoverUtils] Enhance CodeMoverUtils to sink an entire BB #87857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions llvm/lib/Transforms/Utils/CodeMoverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,22 @@ bool llvm::isSafeToMoveBefore(Instruction &I, Instruction &InsertPoint,

if (isReachedBefore(&I, &InsertPoint, &DT, PDT))
for (const Use &U : I.uses())
if (auto *UserInst = dyn_cast<Instruction>(U.getUser()))
if (UserInst != &InsertPoint && !DT.dominates(&InsertPoint, U))
if (auto *UserInst = dyn_cast<Instruction>(U.getUser())) {
// If InsertPoint is in a BB that comes after I, then we cannot move if
// I is used in the terminator of the current BB.
if (I.getParent() == InsertPoint.getParent() &&
UserInst == I.getParent()->getTerminator())
return false;
if (UserInst != &InsertPoint && !DT.dominates(&InsertPoint, U)) {
// If UserInst is an instruction that appears later in the same BB as
// I, then it is okay to move since I will still be available when
// UserInst is executed.
if (CheckForEntireBlock && I.getParent() == UserInst->getParent() &&
DT.dominates(&I, UserInst))
continue;
return false;
}
}
if (isReachedBefore(&InsertPoint, &I, &DT, PDT))
for (const Value *Op : I.operands())
if (auto *OpInst = dyn_cast<Instruction>(Op)) {
Expand Down
5 changes: 5 additions & 0 deletions llvm/unittests/Transforms/Utils/CodeMoverUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,11 @@ TEST(CodeMoverUtils, IsSafeToMoveTest4) {
// Can move as %add2 and %sub2 are control flow equivalent,
// although %add2 does not strictly dominate %sub2.
EXPECT_TRUE(isSafeToMoveBefore(*SubInst2, *AddInst2, DT, &PDT, &DI));

BasicBlock *BB0 = getBasicBlockByName(F, "if.then.first");
BasicBlock *BB1 = getBasicBlockByName(F, "if.then.second");
EXPECT_TRUE(
isSafeToMoveBefore(*BB0, *BB1->getTerminator(), DT, &PDT, &DI));
});
}

Expand Down