[InstCombining] Refactor checks for TryToSinkInstruction. NFC

Moved out the checks for profitability of TryToSinkInstructions
into a lambda function.
This will also allow us to easily add checks for bailing out if the
transform is not profitable.

Tests-Run: instCombine tests.
This commit is contained in:
Anna Thomas 2021-09-12 15:11:26 -04:00
parent 9d359f6c73
commit b4e787d8f4
1 changed files with 53 additions and 42 deletions

View File

@ -3813,8 +3813,15 @@ bool InstCombinerImpl::run() {
// See if we can trivially sink this instruction to its user if we can // See if we can trivially sink this instruction to its user if we can
// prove that the successor is not executed more frequently than our block. // prove that the successor is not executed more frequently than our block.
if (EnableCodeSinking) // Return the UserBlock if successful.
if (Use *SingleUse = I->getSingleUndroppableUse()) { auto getOptionalSinkBlockForInst =
[this](Instruction *I) -> Optional<BasicBlock *> {
if (!EnableCodeSinking)
return None;
Use *SingleUse = I->getSingleUndroppableUse();
if (!SingleUse)
return None;
BasicBlock *BB = I->getParent(); BasicBlock *BB = I->getParent();
Instruction *UserInst = cast<Instruction>(SingleUse->getUser()); Instruction *UserInst = cast<Instruction>(SingleUse->getUser());
BasicBlock *UserParent; BasicBlock *UserParent;
@ -3827,37 +3834,41 @@ bool InstCombinerImpl::run() {
// Try sinking to another block. If that block is unreachable, then do // Try sinking to another block. If that block is unreachable, then do
// not bother. SimplifyCFG should handle it. // not bother. SimplifyCFG should handle it.
if (UserParent != BB && DT.isReachableFromEntry(UserParent)) { if (UserParent == BB || !DT.isReachableFromEntry(UserParent))
return None;
auto *Term = UserParent->getTerminator();
// See if the user is one of our successors that has only one // See if the user is one of our successors that has only one
// predecessor, so that we don't have to split the critical edge. // predecessor, so that we don't have to split the critical edge.
bool ShouldSink = UserParent->getUniquePredecessor() == BB;
// Another option where we can sink is a block that ends with a // Another option where we can sink is a block that ends with a
// terminator that does not pass control to other block (such as // terminator that does not pass control to other block (such as
// return or unreachable). In this case: // return or unreachable). In this case:
// - I dominates the User (by SSA form); // - I dominates the User (by SSA form);
// - the User will be executed at most once. // - the User will be executed at most once.
// So sinking I down to User is always profitable or neutral. // So sinking I down to User is always profitable or neutral.
if (!ShouldSink) { if (UserParent->getUniquePredecessor() == BB ||
auto *Term = UserParent->getTerminator(); (isa<ReturnInst>(Term) || isa<UnreachableInst>(Term))) {
ShouldSink = isa<ReturnInst>(Term) || isa<UnreachableInst>(Term); assert(DT.dominates(BB, UserParent) && "Dominance relation broken?");
return UserParent;
} }
if (ShouldSink) { return None;
assert(DT.dominates(BB, UserParent) && };
"Dominance relation broken?");
auto OptBB = getOptionalSinkBlockForInst(I);
if (OptBB) {
auto *UserParent = *OptBB;
// Okay, the CFG is simple enough, try to sink this instruction. // Okay, the CFG is simple enough, try to sink this instruction.
if (TryToSinkInstruction(I, UserParent)) { if (TryToSinkInstruction(I, UserParent)) {
LLVM_DEBUG(dbgs() << "IC: Sink: " << *I << '\n'); LLVM_DEBUG(dbgs() << "IC: Sink: " << *I << '\n');
MadeIRChange = true; MadeIRChange = true;
// We'll add uses of the sunk instruction below, but since sinking // We'll add uses of the sunk instruction below, but since
// can expose opportunities for it's *operands* add them to the // sinking can expose opportunities for it's *operands* add
// worklist // them to the worklist
for (Use &U : I->operands()) for (Use &U : I->operands())
if (Instruction *OpI = dyn_cast<Instruction>(U.get())) if (Instruction *OpI = dyn_cast<Instruction>(U.get()))
Worklist.push(OpI); Worklist.push(OpI);
} }
} }
}
}
// Now that we have an instruction, try combining it to simplify it. // Now that we have an instruction, try combining it to simplify it.
Builder.SetInsertPoint(I); Builder.SetInsertPoint(I);