Index: include/llvm/Analysis/MemorySSAUpdater.h =================================================================== --- include/llvm/Analysis/MemorySSAUpdater.h +++ include/llvm/Analysis/MemorySSAUpdater.h @@ -172,9 +172,9 @@ /// If New is the only predecessor, move Old's Phi, if present, to New. /// Otherwise, add a new Phi in New with appropriate incoming values, and /// update the incoming values in Old's Phi node too, if present. - void - wireOldPredecessorsToNewImmediatePredecessor(BasicBlock *Old, BasicBlock *New, - ArrayRef Preds); + void wireOldPredecessorsToNewImmediatePredecessor( + BasicBlock *Old, BasicBlock *New, ArrayRef Preds, + bool IdenticalEdgesWereMerged = true); // The below are utility functions. Other than creation of accesses to pass // to insertDef, and removeAccess to remove accesses, you should generally // not attempt to update memoryssa yourself. It is very non-trivial to get Index: lib/Analysis/MemorySSAUpdater.cpp =================================================================== --- lib/Analysis/MemorySSAUpdater.cpp +++ lib/Analysis/MemorySSAUpdater.cpp @@ -1010,7 +1010,8 @@ } void MemorySSAUpdater::wireOldPredecessorsToNewImmediatePredecessor( - BasicBlock *Old, BasicBlock *New, ArrayRef Preds) { + BasicBlock *Old, BasicBlock *New, ArrayRef Preds, + bool IdenticalEdgesWereMerged) { assert(!MSSA->getWritableBlockAccesses(New) && "Access list should be null for a new block."); MemoryPhi *Phi = MSSA->getMemoryAccess(Old); @@ -1028,6 +1029,8 @@ Phi->unorderedDeleteIncomingIf([&](MemoryAccess *MA, BasicBlock *B) { if (PredsSet.count(B)) { NewPhi->addIncoming(MA, B); + if (!IdenticalEdgesWereMerged) + PredsSet.erase(B); return true; } return false; Index: lib/Transforms/Utils/BreakCriticalEdges.cpp =================================================================== --- lib/Transforms/Utils/BreakCriticalEdges.cpp +++ lib/Transforms/Utils/BreakCriticalEdges.cpp @@ -200,8 +200,10 @@ auto *DT = Options.DT; auto *LI = Options.LI; auto *MSSAU = Options.MSSAU; - if (MSSAU) - MSSAU->wireOldPredecessorsToNewImmediatePredecessor(DestBB, NewBB, {TIBB}); + if (MSSAU) { + MSSAU->wireOldPredecessorsToNewImmediatePredecessor( + DestBB, NewBB, {TIBB}, Options.MergeIdenticalEdges); + } if (!DT && !LI) return NewBB;