From 8fdca6a8738c1ad7091137688ee48c9e623b75bb Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 9 Dec 2010 07:45:45 +0000 Subject: [PATCH] enhance memcpyopt to zap memcpy's that have the same src/dst. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121362 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/MemCpyOptimizer.cpp | 17 +++++++++++++---- test/Transforms/MemCpyOpt/memcpy.ll | 9 +++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/lib/Transforms/Scalar/MemCpyOptimizer.cpp index 6f93e326ba1..4c487e0a345 100644 --- a/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ b/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -762,6 +762,14 @@ bool MemCpyOpt::processMemCpy(MemCpyInst *M) { ConstantInt *CopySize = dyn_cast(M->getLength()); if (CopySize == 0 || M->isVolatile()) return false; + // If the source and destination of the memcpy are the same, then zap it. + if (M->getSource() == M->getDest()) { + MD->removeInstruction(M); + M->eraseFromParent(); + return false; + } + + // The are two possible optimizations we can do for memcpy: // a) memcpy-memcpy xform which exposes redundance for DSE. // b) call-memcpy xform for return slot optimization. @@ -773,10 +781,11 @@ bool MemCpyOpt::processMemCpy(MemCpyInst *M) { return processMemCpyMemCpyDependence(M, MDep, CopySize->getZExtValue()); if (CallInst *C = dyn_cast(DepInfo.getInst())) { - bool changed = performCallSlotOptzn(M, M->getDest(), M->getSource(), - CopySize->getZExtValue(), C); - if (changed) M->eraseFromParent(); - return changed; + if (performCallSlotOptzn(M, M->getDest(), M->getSource(), + CopySize->getZExtValue(), C)) { + M->eraseFromParent(); + return true; + } } return false; } diff --git a/test/Transforms/MemCpyOpt/memcpy.ll b/test/Transforms/MemCpyOpt/memcpy.ll index 16b80a628a3..b387d32a7d5 100644 --- a/test/Transforms/MemCpyOpt/memcpy.ll +++ b/test/Transforms/MemCpyOpt/memcpy.ll @@ -100,3 +100,12 @@ entry: ; CHECK: store i8 4 ; CHECK: call void @test5a(%struct.S* byval align 16 %y) } + +;; Noop memcpy should be zapped. +define void @test6(i8 *%P) { + call void @llvm.memcpy.p0i8.p0i8.i64(i8* %P, i8* %P, i64 8, i32 4, i1 false) + ret void +; CHECK: @test6 +; CHECK-NEXT: ret void +} +