Treat lifetime.start'd memory like we treat freshly alloca'd memory. Patch by Björn...
authorNick Lewycky <nicholas@mxc.ca>
Wed, 26 Mar 2014 23:45:15 +0000 (23:45 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Wed, 26 Mar 2014 23:45:15 +0000 (23:45 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204876 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/MemCpyOptimizer.cpp
test/Transforms/MemCpyOpt/memcpy-undef.ll

index 143ba38ec98f521311b2b9c52c734a15ed116699..2603c969c5acf6333d8517de533b9d66c0cf979d 100644 (file)
@@ -851,9 +851,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())) {
@@ -874,7 +874,19 @@ 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();
+    bool hasUndefContents = false;
+
+    if (isa<AllocaInst>(I)) {
+      hasUndefContents = true;
+    } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
+      if (II->getIntrinsicID() == Intrinsic::lifetime_start)
+        if (ConstantInt *LTSize = dyn_cast<ConstantInt>(II->getArgOperand(0)))
+          if (LTSize->getZExtValue() >= CopySize->getZExtValue())
+            hasUndefContents = true;
+    }
+
+    if (hasUndefContents) {
       MD->removeInstruction(M);
       M->eraseFromParent();
       ++NumMemCpyInstr;
index fd4965f4dc5bc27c79e0b7a45292e86ac10d93a1..663b8dcfd3df59a39fddadaea5d1982d636d427e 100644 (file)
@@ -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