-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[SimplifyCFG] Speculatively execute empty BBs with multiple predecessors #120905
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3506,6 +3506,97 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI, | |
return true; | ||
} | ||
|
||
/// Speculate a conditional basic block flattening the CFG. | ||
/// Compared to speculativelyExecuteBB, it allows \p ThenBB to have multiple | ||
/// predecessors other than the current BB. An illustration of this transform is | ||
/// turning this IR: | ||
/// \code | ||
/// BB: | ||
/// %cmp = icmp ult %x, %y | ||
/// br i1 %cmp, label %EndBB, label %ThenBB | ||
/// ThenBB: | ||
/// br label %EndBB | ||
/// EndBB: | ||
/// %phi = phi i1 [ true, %ThenBB ], [ false, %BB ], [ false, %OtherBB ] | ||
/// ... | ||
/// \endcode | ||
/// | ||
/// Into this IR: | ||
/// \code | ||
/// BB: | ||
/// %cmp = icmp ult %x, %y | ||
/// %sel = select i1 %cmp, i1 false, i1 true | ||
/// br label %EndBB | ||
/// ThenBB: | ||
/// br label %EndBB | ||
/// EndBB: | ||
/// %phi = phi i1 [ true, %ThenBB ], [ %sel, %BB ], [ false, %OtherBB ] | ||
/// ... | ||
/// \endcode | ||
/// \returns true if the branch edge is removed. | ||
static bool speculativelyExecuteEmptyBB(BranchInst *BI, bool Invert, | ||
DomTreeUpdater *DTU, | ||
const TargetTransformInfo &TTI) { | ||
BasicBlock *BB = BI->getParent(); | ||
BasicBlock *ThenBB = BI->getSuccessor(Invert); | ||
BasicBlock *EndBB = BI->getSuccessor(!Invert); | ||
|
||
BranchInst *SuccBI = dyn_cast<BranchInst>(ThenBB->getTerminator()); | ||
if (!SuccBI || !SuccBI->isUnconditional() || SuccBI->getSuccessor(0) != EndBB) | ||
return false; | ||
if (&ThenBB->front() != SuccBI) | ||
return false; | ||
if (!isProfitableToSpeculate(BI, Invert, TTI)) | ||
return false; | ||
|
||
InstructionCost Budget = | ||
PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic; | ||
InstructionCost Cost = 0; | ||
unsigned SpeculatedInstructions = 0; | ||
if (!validateAndCostRequiredSelects(BB, ThenBB, EndBB, SpeculatedInstructions, | ||
Cost, TTI) || | ||
Cost > Budget) | ||
return false; | ||
|
||
LLVM_DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *ThenBB << "\n";); | ||
|
||
// Insert selects and rewrite the PHI operands. | ||
Value *BrCond = BI->getCondition(); | ||
IRBuilder<NoFolder> Builder(BI); | ||
for (PHINode &PN : EndBB->phis()) { | ||
unsigned OrigI = PN.getBasicBlockIndex(BB); | ||
unsigned ThenI = PN.getBasicBlockIndex(ThenBB); | ||
Value *OrigV = PN.getIncomingValue(OrigI); | ||
Value *ThenV = PN.getIncomingValue(ThenI); | ||
|
||
// Skip PHIs which are trivial. | ||
if (OrigV == ThenV) | ||
continue; | ||
|
||
// Create a select whose true value is the speculatively executed value and | ||
// false value is the pre-existing value. Swap them if the branch | ||
// destinations were inverted. | ||
Value *TrueV = ThenV, *FalseV = OrigV; | ||
if (Invert) | ||
std::swap(TrueV, FalseV); | ||
Value *V = Builder.CreateSelect(BrCond, TrueV, FalseV, "spec.select", BI); | ||
PN.setIncomingValue(OrigI, V); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. We only break the branch edge |
||
} | ||
|
||
// Modify CFG | ||
ThenBB->removePredecessor(BB); | ||
BranchInst *NewBI = Builder.CreateBr(EndBB); | ||
// Transfer the metadata to the new branch instruction. | ||
NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg, | ||
LLVMContext::MD_annotation}); | ||
BI->eraseFromParent(); | ||
if (DTU) | ||
DTU->applyUpdates({{DominatorTree::Delete, BB, ThenBB}}); | ||
|
||
++NumSpeculations; | ||
return true; | ||
dtcxzyw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Return true if we can thread a branch across this block. | ||
static bool blockIsSimpleEnoughToThreadThrough(BasicBlock *BB) { | ||
int Size = 0; | ||
|
@@ -8125,6 +8216,13 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) { | |
return requestResimplify(); | ||
} | ||
|
||
if (Options.SpeculateBlocks) { | ||
if (speculativelyExecuteEmptyBB(BI, /*Invert=*/false, DTU, TTI)) | ||
return true; | ||
if (speculativelyExecuteEmptyBB(BI, /*Invert=*/true, DTU, TTI)) | ||
return true; | ||
} | ||
|
||
// If this is a branch on something for which we know the constant value in | ||
// predecessors (e.g. a phi node in the current block), thread control | ||
// through this block. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the phi iteration in
validateAndCostRequiredSelects
early exit once the budget threshold is reached?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it doesn't help :( https://llvm-compile-time-tracker.com/compare.php?from=9423961f259f49007dc9cdea7344094a6fdf1afb&to=b2d0fe1be968cc2c3d78bd087c6530af8ff0f772&stat=instructions:u