Index: include/llvm/Analysis/MemorySSAUpdater.h =================================================================== --- include/llvm/Analysis/MemorySSAUpdater.h +++ include/llvm/Analysis/MemorySSAUpdater.h @@ -99,7 +99,7 @@ /// load a /// Where a mayalias b, *does* require RenameUses be set to true. void insertDef(MemoryDef *Def, bool RenameUses = false); - void insertUse(MemoryUse *Use); + void insertUse(MemoryUse *Use, bool RenameUses = false); /// Update the MemoryPhi in `To` following an edge deletion between `From` and /// `To`. If `To` becomes unreachable, a call to removeBlocks should be made. void removeEdge(BasicBlock *From, BasicBlock *To); Index: lib/Analysis/MemorySSAUpdater.cpp =================================================================== --- lib/Analysis/MemorySSAUpdater.cpp +++ lib/Analysis/MemorySSAUpdater.cpp @@ -218,7 +218,7 @@ return recursePhi(Same); } -void MemorySSAUpdater::insertUse(MemoryUse *MU) { +void MemorySSAUpdater::insertUse(MemoryUse *MU, bool RenameUses) { InsertedPHIs.clear(); MU->setDefiningAccess(getPreviousDef(MU)); // Unlike for defs, there is no extra work to do. Because uses do not create @@ -229,6 +229,33 @@ // // 2. There is no def below us, and therefore, there is no extra renaming work // to do. + + // Above rule 1. is broken by having unreachable blocks, where the unnecessary + // Phis were optimized out. Adding the Use may re-insert those Phis. Hence, + // when inserting Uses outside of the MSSA creation process, and new Phis + // were added, rename all uses if we are asked. This only happens in the + // presence of unreachable blocks. + if (RenameUses && InsertedPHIs.size()) { + SmallPtrSet Visited; + BasicBlock *StartBlock = MU->getBlock(); + + if (auto *Defs = MSSA->getWritableBlockDefs(StartBlock)) { + MemoryAccess *FirstDef = &*Defs->begin(); + // Convert to incoming value if it's a memorydef. A phi *is* already an + // incoming value. + if (auto *MD = dyn_cast(FirstDef)) + FirstDef = MD->getDefiningAccess(); + + MSSA->renamePass(MU->getBlock(), FirstDef, Visited); + // We just inserted a phi into this block, so the incoming value will + // become the phi anyway, so it does not matter what we pass. + for (auto &MP : InsertedPHIs) { + MemoryPhi *Phi = dyn_cast_or_null(MP); + if (Phi) + MSSA->renamePass(Phi->getBlock(), nullptr, Visited); + } + } + } } // Set every incoming edge {BB, MP->getBlock()} of MemoryPhi MP to NewDef. Index: lib/Transforms/Scalar/LICM.cpp =================================================================== --- lib/Transforms/Scalar/LICM.cpp +++ lib/Transforms/Scalar/LICM.cpp @@ -1392,7 +1392,7 @@ MSSAU->insertDef(MemDef, /*RenameUses=*/true); else { auto *MemUse = cast(NewMemAcc); - MSSAU->insertUse(MemUse); + MSSAU->insertUse(MemUse, /*RenameUses=*/true); } } } @@ -2119,9 +2119,11 @@ PreheaderLoadMemoryAccess = MSSAU->createMemoryAccessInBB( PreheaderLoad, nullptr, PreheaderLoad->getParent(), MemorySSA::End); MemoryUse *NewMemUse = cast(PreheaderLoadMemoryAccess); - MSSAU->insertUse(NewMemUse); + MSSAU->insertUse(NewMemUse, /*RenameUses=*/true); } + if (MSSAU && VerifyMemorySSA) + MSSAU->getMemorySSA()->verifyMemorySSA(); // Rewrite all the loads in the loop and remember all the definitions from // stores in the loop. Promoter.run(LoopUses); Index: test/Analysis/MemorySSA/PR42940.ll =================================================================== --- /dev/null +++ test/Analysis/MemorySSA/PR42940.ll @@ -0,0 +1,36 @@ +; RUN: opt -licm -enable-mssa-loop-dependency -verify-memoryssa -S %s | FileCheck %s +; REQUIRES: asserts + +target datalayout = "E-m:e-i1:8:16-i8:8:16-i64:64-f128:64-v128:64-a:8:16-n32:64" +target triple = "s390x-ibm-linux" + +@g_77 = external dso_local global i16, align 2 + +; CHECK-LABEL: @f1() +define void @f1() { +entry: + store i16 undef, i16* @g_77, align 2 + br label %loop_pre + +unreachablelabel: ; No predecessors + br label %loop_pre + +loop_pre: + br label %for.cond.header + +for.cond.header: + store i32 0, i32* undef, align 4 + br i1 undef, label %for.body, label %for.end + +for.body: + %tmp1 = load volatile i16, i16* undef, align 2 + br label %for.end + +for.end: + br i1 undef, label %func.exit, label %for.cond.header + +func.exit: + ret void + +} +