diff --git a/llvm/lib/Analysis/MemorySSAUpdater.cpp b/llvm/lib/Analysis/MemorySSAUpdater.cpp --- a/llvm/lib/Analysis/MemorySSAUpdater.cpp +++ b/llvm/lib/Analysis/MemorySSAUpdater.cpp @@ -727,6 +727,33 @@ } }; + auto RemoveDeadMemoryAccess = [&](BasicBlock *BB) { + // If globals-aa is enabled, because of the deletion of memory instructions, + // there may be call instruction that is not in ModOrRefSet but is a + // MemoryUseOrDef. This causes the crash in the process of clone uses and + // defs. + BasicBlock *NewBlock = cast_or_null(VMap.lookup(BB)); + if (!NewBlock) + return; + const MemorySSA::AccessList *BA = MSSA->getBlockAccesses(BB); + if (!BA) + return; + SmallVector ToRemove; + for (const MemoryAccess &MA : *BA) { + if (const MemoryUseOrDef *MUD = dyn_cast(&MA)) { + Instruction *Inst = MUD->getMemoryInst(); + if (auto *Call = dyn_cast(Inst)) { + ModRefInfo ModRef = MSSA->AA->getModRefInfo(Call, None); + if (!isModOrRefSet(ModRef)) + ToRemove.push_back(Inst); + } + } + } + for (auto *Inst : ToRemove) { + removeMemoryAccess(Inst); + } + }; + auto ProcessBlock = [&](BasicBlock *BB) { BasicBlock *NewBlock = cast_or_null(VMap.lookup(BB)); if (!NewBlock) @@ -744,6 +771,9 @@ cloneUsesAndDefs(BB, NewBlock, VMap, MPhiMap); }; + for (auto *BB : llvm::concat(LoopBlocks, ExitBlocks)) + RemoveDeadMemoryAccess(BB); + for (auto *BB : llvm::concat(LoopBlocks, ExitBlocks)) ProcessBlock(BB); diff --git a/llvm/test/Transforms/SimpleLoopUnswitch/pr58719.ll b/llvm/test/Transforms/SimpleLoopUnswitch/pr58719.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/SimpleLoopUnswitch/pr58719.ll @@ -0,0 +1,19 @@ +; RUN: opt -passes="require,cgscc(instcombine),recompute-globalsaa,function(loop-mssa(simple-loop-unswitch),print)" -disable-output < %s 2>&1 | FileCheck %s +define void @f() { +entry: + %0 = load i16, ptr null, align 1 + ret void +} + +define void @g(i1 %tobool.not) { +entry: + br label %for.cond + +for.cond: ; preds = %if.then, %for.cond, %entry + br i1 %tobool.not, label %if.then, label %for.cond + +if.then: ; preds = %for.cond +; CHECK-NOT: MemoryUse(liveOnEntry) + call void @f() + br label %for.cond +}