Skip to content

Commit e35a449

Browse files
committed
[Dominators] Teach LoopUnswitch to use the incremental API
Summary: This patch makes LoopUnswitch use new incremental API for updating dominators. It also updates SplitCriticalEdge, as it is called in LoopUnswitch. There doesn't seem to be any noticeable performance difference when bootstrapping clang with this patch. Reviewers: dberlin, davide, sanjoy, grosser, chandlerc Reviewed By: davide, grosser Subscribers: mzolotukhin, llvm-commits Differential Revision: https://reviews.llvm.org/D35528 llvm-svn: 311093
1 parent 3a622a1 commit e35a449

File tree

2 files changed

+60
-64
lines changed

2 files changed

+60
-64
lines changed

llvm/lib/Transforms/Scalar/LoopUnswitch.cpp

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ namespace {
237237
void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
238238
BasicBlock *TrueDest,
239239
BasicBlock *FalseDest,
240-
Instruction *InsertPt,
240+
BranchInst *OldBranch,
241241
TerminatorInst *TI);
242242

243243
void SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L);
@@ -518,9 +518,6 @@ bool LoopUnswitch::runOnLoop(Loop *L, LPPassManager &LPM_Ref) {
518518
Changed |= processCurrentLoop();
519519
} while(redoLoop);
520520

521-
// FIXME: Reconstruct dom info, because it is not preserved properly.
522-
if (Changed)
523-
DT->recalculate(*F);
524521
return Changed;
525522
}
526523

@@ -896,31 +893,59 @@ static Loop *CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
896893
}
897894

898895
/// Emit a conditional branch on two values if LIC == Val, branch to TrueDst,
899-
/// otherwise branch to FalseDest. Insert the code immediately before InsertPt.
896+
/// otherwise branch to FalseDest. Insert the code immediately before OldBranch
897+
/// and remove (but not erase!) it from the function.
900898
void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
901899
BasicBlock *TrueDest,
902900
BasicBlock *FalseDest,
903-
Instruction *InsertPt,
901+
BranchInst *OldBranch,
904902
TerminatorInst *TI) {
903+
assert(OldBranch->isUnconditional() && "Preheader is not split correctly");
905904
// Insert a conditional branch on LIC to the two preheaders. The original
906905
// code is the true version and the new code is the false version.
907906
Value *BranchVal = LIC;
908907
bool Swapped = false;
909908
if (!isa<ConstantInt>(Val) ||
910909
Val->getType() != Type::getInt1Ty(LIC->getContext()))
911-
BranchVal = new ICmpInst(InsertPt, ICmpInst::ICMP_EQ, LIC, Val);
910+
BranchVal = new ICmpInst(OldBranch, ICmpInst::ICMP_EQ, LIC, Val);
912911
else if (Val != ConstantInt::getTrue(Val->getContext())) {
913912
// We want to enter the new loop when the condition is true.
914913
std::swap(TrueDest, FalseDest);
915914
Swapped = true;
916915
}
917916

917+
// Old branch will be removed, so save its parent and successor to update the
918+
// DomTree.
919+
auto *OldBranchSucc = OldBranch->getSuccessor(0);
920+
auto *OldBranchParent = OldBranch->getParent();
921+
918922
// Insert the new branch.
919923
BranchInst *BI =
920-
IRBuilder<>(InsertPt).CreateCondBr(BranchVal, TrueDest, FalseDest, TI);
924+
IRBuilder<>(OldBranch).CreateCondBr(BranchVal, TrueDest, FalseDest, TI);
921925
if (Swapped)
922926
BI->swapProfMetadata();
923927

928+
// Remove the old branch so there is only one branch at the end. This is
929+
// needed to perform DomTree's internal DFS walk on the function's CFG.
930+
OldBranch->removeFromParent();
931+
932+
// Inform the DT about the new branch.
933+
if (DT) {
934+
// First, add both successors.
935+
SmallVector<DominatorTree::UpdateType, 3> Updates;
936+
if (TrueDest != OldBranchParent)
937+
Updates.push_back({DominatorTree::Insert, OldBranchParent, TrueDest});
938+
if (FalseDest != OldBranchParent)
939+
Updates.push_back({DominatorTree::Insert, OldBranchParent, FalseDest});
940+
// If both of the new successors are different from the old one, inform the
941+
// DT that the edge was deleted.
942+
if (OldBranchSucc != TrueDest && OldBranchSucc != FalseDest) {
943+
Updates.push_back({DominatorTree::Delete, OldBranchParent, OldBranchSucc});
944+
}
945+
946+
DT->applyUpdates(Updates);
947+
}
948+
924949
// If either edge is critical, split it. This helps preserve LoopSimplify
925950
// form for enclosing loops.
926951
auto Options = CriticalEdgeSplittingOptions(DT, LI).setPreserveLCSSA();
@@ -960,10 +985,14 @@ void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
960985

961986
// Okay, now we have a position to branch from and a position to branch to,
962987
// insert the new conditional branch.
963-
EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH,
964-
loopPreheader->getTerminator(), TI);
965-
LPM->deleteSimpleAnalysisValue(loopPreheader->getTerminator(), L);
966-
loopPreheader->getTerminator()->eraseFromParent();
988+
auto *OldBranch = dyn_cast<BranchInst>(loopPreheader->getTerminator());
989+
assert(OldBranch && "Failed to split the preheader");
990+
EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH, OldBranch, TI);
991+
LPM->deleteSimpleAnalysisValue(OldBranch, L);
992+
993+
// EmitPreheaderBranchOnCondition removed the OldBranch from the function.
994+
// Delete it, as it is no longer needed.
995+
delete OldBranch;
967996

968997
// We need to reprocess this loop, it could be unswitched again.
969998
redoLoop = true;
@@ -1278,7 +1307,10 @@ void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
12781307
EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR,
12791308
TI);
12801309
LPM->deleteSimpleAnalysisValue(OldBR, L);
1281-
OldBR->eraseFromParent();
1310+
1311+
// The OldBr was replaced by a new one and removed (but not erased) by
1312+
// EmitPreheaderBranchOnCondition. It is no longer needed, so delete it.
1313+
delete OldBR;
12821314

12831315
LoopProcessWorklist.push_back(NewLoop);
12841316
redoLoop = true;

llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -198,59 +198,23 @@ llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
198198
if (!DT && !LI)
199199
return NewBB;
200200

201-
// Now update analysis information. Since the only predecessor of NewBB is
202-
// the TIBB, TIBB clearly dominates NewBB. TIBB usually doesn't dominate
203-
// anything, as there are other successors of DestBB. However, if all other
204-
// predecessors of DestBB are already dominated by DestBB (e.g. DestBB is a
205-
// loop header) then NewBB dominates DestBB.
206-
SmallVector<BasicBlock*, 8> OtherPreds;
207-
208-
// If there is a PHI in the block, loop over predecessors with it, which is
209-
// faster than iterating pred_begin/end.
210-
if (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
211-
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
212-
if (PN->getIncomingBlock(i) != NewBB)
213-
OtherPreds.push_back(PN->getIncomingBlock(i));
214-
} else {
215-
for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB);
216-
I != E; ++I) {
217-
BasicBlock *P = *I;
218-
if (P != NewBB)
219-
OtherPreds.push_back(P);
220-
}
221-
}
222-
223-
bool NewBBDominatesDestBB = true;
224-
225-
// Should we update DominatorTree information?
226201
if (DT) {
227-
DomTreeNode *TINode = DT->getNode(TIBB);
228-
229-
// The new block is not the immediate dominator for any other nodes, but
230-
// TINode is the immediate dominator for the new node.
202+
// Update the DominatorTree.
203+
// ---> NewBB -----\
204+
// / V
205+
// TIBB -------\\------> DestBB
231206
//
232-
if (TINode) { // Don't break unreachable code!
233-
DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB);
234-
DomTreeNode *DestBBNode = nullptr;
235-
236-
// If NewBBDominatesDestBB hasn't been computed yet, do so with DT.
237-
if (!OtherPreds.empty()) {
238-
DestBBNode = DT->getNode(DestBB);
239-
while (!OtherPreds.empty() && NewBBDominatesDestBB) {
240-
if (DomTreeNode *OPNode = DT->getNode(OtherPreds.back()))
241-
NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode);
242-
OtherPreds.pop_back();
243-
}
244-
OtherPreds.clear();
245-
}
246-
247-
// If NewBBDominatesDestBB, then NewBB dominates DestBB, otherwise it
248-
// doesn't dominate anything.
249-
if (NewBBDominatesDestBB) {
250-
if (!DestBBNode) DestBBNode = DT->getNode(DestBB);
251-
DT->changeImmediateDominator(DestBBNode, NewBBNode);
252-
}
253-
}
207+
// First, inform the DT about the new path from TIBB to DestBB via NewBB,
208+
// then delete the old edge from TIBB to DestBB. By doing this in that order
209+
// DestBB stays reachable in the DT the whole time and its subtree doesn't
210+
// get disconnected.
211+
SmallVector<DominatorTree::UpdateType, 3> Updates;
212+
Updates.push_back({DominatorTree::Insert, TIBB, NewBB});
213+
Updates.push_back({DominatorTree::Insert, NewBB, DestBB});
214+
if (llvm::find(successors(TIBB), DestBB) == succ_end(TIBB))
215+
Updates.push_back({DominatorTree::Delete, TIBB, DestBB});
216+
217+
DT->applyUpdates(Updates);
254218
}
255219

256220
// Update LoopInfo if it is around.

0 commit comments

Comments
 (0)