@@ -237,7 +237,7 @@ namespace {
237
237
void EmitPreheaderBranchOnCondition (Value *LIC, Constant *Val,
238
238
BasicBlock *TrueDest,
239
239
BasicBlock *FalseDest,
240
- Instruction *InsertPt ,
240
+ BranchInst *OldBranch ,
241
241
TerminatorInst *TI);
242
242
243
243
void SimplifyCode (std::vector<Instruction*> &Worklist, Loop *L);
@@ -518,9 +518,6 @@ bool LoopUnswitch::runOnLoop(Loop *L, LPPassManager &LPM_Ref) {
518
518
Changed |= processCurrentLoop ();
519
519
} while (redoLoop);
520
520
521
- // FIXME: Reconstruct dom info, because it is not preserved properly.
522
- if (Changed)
523
- DT->recalculate (*F);
524
521
return Changed;
525
522
}
526
523
@@ -896,31 +893,59 @@ static Loop *CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
896
893
}
897
894
898
895
// / 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.
900
898
void LoopUnswitch::EmitPreheaderBranchOnCondition (Value *LIC, Constant *Val,
901
899
BasicBlock *TrueDest,
902
900
BasicBlock *FalseDest,
903
- Instruction *InsertPt ,
901
+ BranchInst *OldBranch ,
904
902
TerminatorInst *TI) {
903
+ assert (OldBranch->isUnconditional () && " Preheader is not split correctly" );
905
904
// Insert a conditional branch on LIC to the two preheaders. The original
906
905
// code is the true version and the new code is the false version.
907
906
Value *BranchVal = LIC;
908
907
bool Swapped = false ;
909
908
if (!isa<ConstantInt>(Val) ||
910
909
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);
912
911
else if (Val != ConstantInt::getTrue (Val->getContext ())) {
913
912
// We want to enter the new loop when the condition is true.
914
913
std::swap (TrueDest, FalseDest);
915
914
Swapped = true ;
916
915
}
917
916
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
+
918
922
// Insert the new branch.
919
923
BranchInst *BI =
920
- IRBuilder<>(InsertPt ).CreateCondBr (BranchVal, TrueDest, FalseDest, TI);
924
+ IRBuilder<>(OldBranch ).CreateCondBr (BranchVal, TrueDest, FalseDest, TI);
921
925
if (Swapped)
922
926
BI->swapProfMetadata ();
923
927
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
+
924
949
// If either edge is critical, split it. This helps preserve LoopSimplify
925
950
// form for enclosing loops.
926
951
auto Options = CriticalEdgeSplittingOptions (DT, LI).setPreserveLCSSA ();
@@ -960,10 +985,14 @@ void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
960
985
961
986
// Okay, now we have a position to branch from and a position to branch to,
962
987
// 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;
967
996
968
997
// We need to reprocess this loop, it could be unswitched again.
969
998
redoLoop = true ;
@@ -1278,7 +1307,10 @@ void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
1278
1307
EmitPreheaderBranchOnCondition (LIC, Val, NewBlocks[0 ], LoopBlocks[0 ], OldBR,
1279
1308
TI);
1280
1309
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;
1282
1314
1283
1315
LoopProcessWorklist.push_back (NewLoop);
1284
1316
redoLoop = true ;
0 commit comments