[PATCH] Omit memcpys from memory that has just started its lifetime
Björn Steinbrink
bsteinbr at gmail.com
Sat Mar 1 10:39:09 PST 2014
If there's no possible store between the call to llvm.lifetime.start and
the memcpy, the memory contents are undefined so the memcpy may be
omitted.
---
lib/Transforms/Scalar/MemCpyOptimizer.cpp | 16 ++++++++++++----
test/Transforms/MemCpyOpt/memcpy-undef.ll | 21 +++++++++++++++++++++
2 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index 83022ed..f94bc5a 100644
--- a/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -846,9 +846,9 @@ bool MemCpyOpt::processMemCpy(MemCpyInst *M) {
// The are three possible optimizations we can do for memcpy:
// a) memcpy-memcpy xform which exposes redundance for DSE.
// b) call-memcpy xform for return slot optimization.
- // c) memcpy from freshly alloca'd space copies undefined data, and we can
- // therefore eliminate the memcpy in favor of the data that was already
- // at the destination.
+ // c) memcpy from freshly alloca'd space or space that has just started its
+ // lifetime copies undefined data, and we can therefore eliminate the
+ // memcpy in favor of the data that was already at the destination.
MemDepResult DepInfo = MD->getDependency(M);
if (DepInfo.isClobber()) {
if (CallInst *C = dyn_cast<CallInst>(DepInfo.getInst())) {
@@ -869,7 +869,15 @@ bool MemCpyOpt::processMemCpy(MemCpyInst *M) {
if (MemCpyInst *MDep = dyn_cast<MemCpyInst>(SrcDepInfo.getInst()))
return processMemCpyMemCpyDependence(M, MDep, CopySize->getZExtValue());
} else if (SrcDepInfo.isDef()) {
- if (isa<AllocaInst>(SrcDepInfo.getInst())) {
+ Instruction *I = SrcDepInfo.getInst();
+ IntrinsicInst *II;
+ ConstantInt *LTSize;
+
+ if (isa<AllocaInst>(I) ||
+ ((II = dyn_cast<IntrinsicInst>(I)) &&
+ II->getIntrinsicID() == Intrinsic::lifetime_start &&
+ (LTSize = dyn_cast<ConstantInt>(II->getArgOperand(0))) &&
+ LTSize->getZExtValue() >= CopySize->getZExtValue())) {
MD->removeInstruction(M);
M->eraseFromParent();
++NumMemCpyInstr;
diff --git a/test/Transforms/MemCpyOpt/memcpy-undef.ll b/test/Transforms/MemCpyOpt/memcpy-undef.ll
index fd4965f..663b8dc 100644
--- a/test/Transforms/MemCpyOpt/memcpy-undef.ll
+++ b/test/Transforms/MemCpyOpt/memcpy-undef.ll
@@ -21,5 +21,26 @@ define i32 @test1(%struct.foo* nocapture %foobie) nounwind noinline ssp uwtable
; CHECK-NOT: call void @llvm.memcpy
}
+define void @test2(i8* sret noalias nocapture %out, i8* %in) nounwind noinline ssp uwtable {
+ call void @llvm.lifetime.start(i64 8, i8* %in)
+ call void @llvm.memcpy.p0i8.p0i8.i64(i8* %out, i8* %in, i64 8, i32 1, i1 false)
+ ret void
+
+; Check that the memcpy is removed.
+; CHECK-LABEL: @test2(
+; CHECK-NOT: call void @llvm.memcpy
+}
+
+define void @test3(i8* sret noalias nocapture %out, i8* %in) nounwind noinline ssp uwtable {
+ call void @llvm.lifetime.start(i64 4, i8* %in)
+ call void @llvm.memcpy.p0i8.p0i8.i64(i8* %out, i8* %in, i64 8, i32 1, i1 false)
+ ret void
+
+; Check that the memcpy is not removed.
+; CHECK-LABEL: @test3(
+; CHECK: call void @llvm.memcpy
+}
+
declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
+declare void @llvm.lifetime.start(i64, i8* nocapture) nounwind
--
1.9.0.rc3
More information about the llvm-commits
mailing list