diff --git a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h --- a/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h +++ b/llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h @@ -91,11 +91,15 @@ /// if BB's Pred has a branch to BB and to AnotherBB, and BB has a single /// successor Sing. In this case the branch will be updated with Sing instead of /// BB, and BB will still be merged into its predecessor and removed. -bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU = nullptr, - LoopInfo *LI = nullptr, - MemorySSAUpdater *MSSAU = nullptr, - MemoryDependenceResults *MemDep = nullptr, - bool PredecessorWithTwoSuccessors = false); +struct MergeBlockIntoPredecessorOpts { + DomTreeUpdater *DTU = nullptr; + LoopInfo *LI = nullptr; + MemorySSAUpdater *MSSAU = nullptr; + MemoryDependenceResults *MemDep = nullptr; + bool PredecessorWithTwoSuccessors = false; +}; +bool MergeBlockIntoPredecessor(BasicBlock *BB, + MergeBlockIntoPredecessorOpts Opts = {}); /// Merge block(s) sucessors, if possible. Return true if at least two /// of the blocks were merged together. diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp --- a/llvm/lib/Transforms/Scalar/GVN.cpp +++ b/llvm/lib/Transforms/Scalar/GVN.cpp @@ -2223,7 +2223,11 @@ for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) { BasicBlock *BB = &*FI++; - bool removedBlock = MergeBlockIntoPredecessor(BB, &DTU, LI, nullptr, MD); + MergeBlockIntoPredecessorOpts MergeOpts; + MergeOpts.DTU = &DTU; + MergeOpts.LI = LI; + MergeOpts.MemDep = MD; + bool removedBlock = MergeBlockIntoPredecessor(BB, MergeOpts); if (removedBlock) ++NumGVNBlocks; diff --git a/llvm/lib/Transforms/Scalar/LoopFuse.cpp b/llvm/lib/Transforms/Scalar/LoopFuse.cpp --- a/llvm/lib/Transforms/Scalar/LoopFuse.cpp +++ b/llvm/lib/Transforms/Scalar/LoopFuse.cpp @@ -1275,7 +1275,7 @@ void mergeLatch(const FusionCandidate &FC0, const FusionCandidate &FC1) { moveInstructionsToTheBeginning(*FC0.Latch, *FC1.Latch, DT, PDT, DI); if (BasicBlock *Succ = FC0.Latch->getUniqueSuccessor()) { - MergeBlockIntoPredecessor(Succ, &DTU, &LI); + MergeBlockIntoPredecessor(Succ, {&DTU, &LI}); DTU.flush(); } } diff --git a/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp b/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp --- a/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp +++ b/llvm/lib/Transforms/Scalar/LoopSimplifyCFG.cpp @@ -667,7 +667,7 @@ continue; // Merge Succ into Pred and delete it. - MergeBlockIntoPredecessor(Succ, &DTU, &LI, MSSAU); + MergeBlockIntoPredecessor(Succ, {&DTU, &LI, MSSAU}); if (MSSAU && VerifyMemorySSA) MSSAU->getMemorySSA()->verifyMemorySSA(); diff --git a/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp --- a/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp @@ -1610,7 +1610,7 @@ } // Merge the block and make the remaining analyses updates. DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); - MergeBlockIntoPredecessor(Succ, &DTU, LI, MSSAU.get()); + MergeBlockIntoPredecessor(Succ, {&DTU, LI, MSSAU.get()}); ++NumSimplify; continue; } diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp --- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -169,10 +169,14 @@ return Changed; } -bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU, - LoopInfo *LI, MemorySSAUpdater *MSSAU, - MemoryDependenceResults *MemDep, - bool PredecessorWithTwoSuccessors) { +bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, + MergeBlockIntoPredecessorOpts Opts) { + DomTreeUpdater *DTU = Opts.DTU; + LoopInfo *LI = Opts.LI; + MemorySSAUpdater *MSSAU = Opts.MSSAU; + MemoryDependenceResults *MemDep = Opts.MemDep; + bool PredecessorWithTwoSuccessors = Opts.PredecessorWithTwoSuccessors; + if (BB->hasAddressTaken()) return false; @@ -327,7 +331,7 @@ if (Dest && (!L || L->contains(Dest))) { BasicBlock *Fold = Dest->getUniquePredecessor(); (void)Fold; - if (MergeBlockIntoPredecessor(Dest, DTU, LI)) { + if (MergeBlockIntoPredecessor(Dest, {DTU, LI})) { assert(Fold == BB && "Expecting BB to be unique predecessor of the Dest block"); MergeBlocks.erase(Dest); diff --git a/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp b/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp --- a/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp @@ -578,7 +578,7 @@ // connected by an unconditional branch. This is just a cleanup so the // emitted code isn't too gross in this common case. DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); - MergeBlockIntoPredecessor(OrigHeader, &DTU, LI, MSSAU); + MergeBlockIntoPredecessor(OrigHeader, {&DTU, LI, MSSAU}); if (MSSAU && VerifyMemorySSA) MSSAU->getMemorySSA()->verifyMemorySSA(); @@ -701,9 +701,9 @@ << LastExit->getName() << "\n"); DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); - MergeBlockIntoPredecessor(Latch, &DTU, LI, MSSAU, nullptr, - /*PredecessorWithTwoSuccessors=*/true); - + MergeBlockIntoPredecessorOpts MergeOpts = {&DTU, LI, MSSAU}; + MergeOpts.PredecessorWithTwoSuccessors = true; + MergeBlockIntoPredecessor(Latch, MergeOpts); if (MSSAU && VerifyMemorySSA) MSSAU->getMemorySSA()->verifyMemorySSA(); diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp b/llvm/lib/Transforms/Utils/LoopUnroll.cpp --- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp +++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp @@ -862,7 +862,7 @@ if (Term && Term->isUnconditional()) { BasicBlock *Dest = Term->getSuccessor(0); BasicBlock *Fold = Dest->getUniquePredecessor(); - if (MergeBlockIntoPredecessor(Dest, &DTU, LI)) { + if (MergeBlockIntoPredecessor(Dest, {&DTU, LI})) { // Dest has been folded into Fold. Update our worklists accordingly. std::replace(Latches.begin(), Latches.end(), Dest, Fold); UnrolledLoopBlocks.erase(std::remove(UnrolledLoopBlocks.begin(), diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp --- a/llvm/lib/Transforms/Vectorize/VPlan.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp @@ -526,7 +526,9 @@ BranchInst::Create(VectorLatchBB, LastBB); // Merge LastBB with Latch. - bool Merged = MergeBlockIntoPredecessor(VectorLatchBB, nullptr, State->LI); + MergeBlockIntoPredecessorOpts MergeOpts; + MergeOpts.LI = State->LI; + bool Merged = MergeBlockIntoPredecessor(VectorLatchBB, MergeOpts); (void)Merged; assert(Merged && "Could not merge last basic block with latch."); VectorLatchBB = LastBB;