From d242b52d0e477171f3f6b957bafece88edb88a4f Mon Sep 17 00:00:00 2001 From: Joshua Cao Date: Wed, 17 May 2023 20:40:40 -0700 Subject: [PATCH] [SimpleLoopUnswitch] turnGuardIntoBranch use BB utils to update DT turnGuardIntoBranch() can use splitBlockAndInsertIfThen to update the DominatorTree rather than implementing it itself. --- llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp | 28 ++++------------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp index b3ebfaa..8356f86 100644 --- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -2655,8 +2655,7 @@ static BranchInst *turnSelectIntoBranch(SelectInst *SI, DominatorTree &DT, LLVM_DEBUG(dbgs() << "Turning " << *SI << " into a branch.\n"); BasicBlock *HeadBB = SI->getParent(); - DomTreeUpdater DTU = - DomTreeUpdater(DT, DomTreeUpdater::UpdateStrategy::Eager); + DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); SplitBlockAndInsertIfThen(SI->getCondition(), SI, false, SI->getMetadata(LLVMContext::MD_prof), &DTU, &LI); auto *CondBr = cast(HeadBB->getTerminator()); @@ -2709,15 +2708,10 @@ static BranchInst *turnGuardIntoBranch(IntrinsicInst *GI, Loop &L, if (MSSAU && VerifyMemorySSA) MSSAU->getMemorySSA()->verifyMemorySSA(); - // Remove all CheckBB's successors from DomTree. A block can be seen among - // successors more than once, but for DomTree it should be added only once. - SmallPtrSet Successors; - for (auto *Succ : successors(CheckBB)) - if (Successors.insert(Succ).second) - DTUpdates.push_back({DominatorTree::Delete, CheckBB, Succ}); - + DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); Instruction *DeoptBlockTerm = - SplitBlockAndInsertIfThen(GI->getArgOperand(0), GI, true); + SplitBlockAndInsertIfThen(GI->getArgOperand(0), GI, true, + GI->getMetadata(LLVMContext::MD_prof), &DTU, &LI); BranchInst *CheckBI = cast(CheckBB->getTerminator()); // SplitBlockAndInsertIfThen inserts control flow that branches to // DeoptBlockTerm if the condition is true. We want the opposite. @@ -2734,20 +2728,6 @@ static BranchInst *turnGuardIntoBranch(IntrinsicInst *GI, Loop &L, GI->moveBefore(DeoptBlockTerm); GI->setArgOperand(0, ConstantInt::getFalse(GI->getContext())); - // Add new successors of CheckBB into DomTree. - for (auto *Succ : successors(CheckBB)) - DTUpdates.push_back({DominatorTree::Insert, CheckBB, Succ}); - - // Now the blocks that used to be CheckBB's successors are GuardedBlock's - // successors. - for (auto *Succ : Successors) - DTUpdates.push_back({DominatorTree::Insert, GuardedBlock, Succ}); - - // Make proper changes to DT. - DT.applyUpdates(DTUpdates); - // Inform LI of a new loop block. - L.addBasicBlockToLoop(GuardedBlock, LI); - if (MSSAU) { MemoryDef *MD = cast(MSSAU->getMemorySSA()->getMemoryAccess(GI)); MSSAU->moveToPlace(MD, DeoptBlock, MemorySSA::BeforeTerminator); -- 2.7.4