Skip to content

Commit 5c3beb7

Browse files
committed
[MemCpyOpt] Handle memcpy marked as memory(none)
Fixes #71183.
1 parent 68fbc8e commit 5c3beb7

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,11 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
16671667
return true;
16681668
}
16691669

1670+
MemoryUseOrDef *MA = MSSA->getMemoryAccess(M);
1671+
if (!MA)
1672+
// Degenerate case: memcpy marked as not accessing memory.
1673+
return false;
1674+
16701675
// If copying from a constant, try to turn the memcpy into a memset.
16711676
if (auto *GV = dyn_cast<GlobalVariable>(M->getSource()))
16721677
if (GV->isConstant() && GV->hasDefinitiveInitializer())
@@ -1675,8 +1680,7 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
16751680
IRBuilder<> Builder(M);
16761681
Instruction *NewM = Builder.CreateMemSet(
16771682
M->getRawDest(), ByteVal, M->getLength(), M->getDestAlign(), false);
1678-
auto *LastDef =
1679-
cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(M));
1683+
auto *LastDef = cast<MemoryDef>(MA);
16801684
auto *NewAccess =
16811685
MSSAU->createMemoryAccessAfter(NewM, nullptr, LastDef);
16821686
MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
@@ -1687,7 +1691,6 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
16871691
}
16881692

16891693
BatchAAResults BAA(*AA);
1690-
MemoryUseOrDef *MA = MSSA->getMemoryAccess(M);
16911694
// FIXME: Not using getClobberingMemoryAccess() here due to PR54682.
16921695
MemoryAccess *AnyClobber = MA->getDefiningAccess();
16931696
MemoryLocation DestLoc = MemoryLocation::getForDest(M);

llvm/test/Transforms/MemCpyOpt/memcpy.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,15 @@ define void @byval_param_noalias_metadata(ptr align 4 byval(i32) %ptr) {
723723
ret void
724724
}
725725

726+
define void @memcpy_memory_none(ptr %p, ptr %p2, i64 %size) {
727+
; CHECK-LABEL: @memcpy_memory_none(
728+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr [[P:%.*]], ptr [[P2:%.*]], i64 [[SIZE:%.*]], i1 false) #[[ATTR6:[0-9]+]]
729+
; CHECK-NEXT: ret void
730+
;
731+
call void @llvm.memcpy.p0.p0.i64(ptr %p, ptr %p2, i64 %size, i1 false) memory(none)
732+
ret void
733+
}
734+
726735
!0 = !{!0}
727736
!1 = !{!1, !0}
728737
!2 = !{!1}

0 commit comments

Comments
 (0)