Skip to content

Commit 8571397

Browse files
gbaraldigiordano
authored andcommitted
Backport llvm@5c3beb7 (#22)
to avoid non integral addresspace issues
1 parent 8a2c8f5 commit 8571397

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,13 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
14271427
eraseInstruction(M);
14281428
return true;
14291429
}
1430+
// If the size is zero, remove the memcpy. This also prevents infinite loops
1431+
// in processMemSetMemCpyDependence, which is a no-op for zero-length memcpys.
1432+
1433+
MemoryUseOrDef *MA = MSSA->getMemoryAccess(M);
1434+
if (!MA)
1435+
// Degenerate case: memcpy marked as not accessing memory.
1436+
return false;
14301437

14311438
// If copying from a constant, try to turn the memcpy into a memset.
14321439
if (auto *GV = dyn_cast<GlobalVariable>(M->getSource()))
@@ -1436,8 +1443,7 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
14361443
IRBuilder<> Builder(M);
14371444
Instruction *NewM = Builder.CreateMemSet(
14381445
M->getRawDest(), ByteVal, M->getLength(), M->getDestAlign(), false);
1439-
auto *LastDef =
1440-
cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(M));
1446+
auto *LastDef = cast<MemoryDef>(MA);
14411447
auto *NewAccess =
14421448
MSSAU->createMemoryAccessAfter(NewM, LastDef, LastDef);
14431449
MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/true);
@@ -1448,7 +1454,6 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
14481454
}
14491455

14501456
BatchAAResults BAA(*AA);
1451-
MemoryUseOrDef *MA = MSSA->getMemoryAccess(M);
14521457
// FIXME: Not using getClobberingMemoryAccess() here due to PR54682.
14531458
MemoryAccess *AnyClobber = MA->getDefiningAccess();
14541459
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)