[llvm] r183606 - Fix a potential bug in r183584.

Shuxin Yang shuxin.llvm at gmail.com
Fri Jun 7 21:56:05 PDT 2013


Author: shuxin_yang
Date: Fri Jun  7 23:56:05 2013
New Revision: 183606

URL: http://llvm.org/viewvc/llvm-project?rev=183606&view=rev
Log:
  Fix a potential bug in r183584.

  r183584 tries to derive some info from the code *AFTER* a call and apply
these derived info to the code *BEFORE* the call, which is not always safe
as the call in question may never return, and in this case, the derived
info is invalid.
  
  Thank Duncan for pointing out this potential bug.

rdar://14073661 

Modified:
    llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp
    llvm/trunk/test/Transforms/MemCpyOpt/memcpy.ll

Modified: llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp?rev=183606&r1=183605&r2=183606&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp Fri Jun  7 23:56:05 2013
@@ -626,10 +626,14 @@ bool MemCpyOpt::performCallSlotOptzn(Ins
       return false;
 
     Type *StructTy = cast<PointerType>(A->getType())->getElementType();
-    // If StructTy is an opaque type, it should have at least <cpyLen> bytes,
-    // as implified by the copy-instruction.
-    uint64_t destSize = StructTy->isSized() ?
-                        TD->getTypeAllocSize(StructTy) : cpyLen;
+    if (!StructTy->isSized()) {
+      // The call may never return and hence the copy-instruction may never
+      // be executed, and therefore it's not safe to say "the destination
+      // has at least <cpyLen> bytes, as implied by the copy-instruction",
+      return false;
+    }
+
+    uint64_t destSize = TD->getTypeAllocSize(StructTy);
     if (destSize < srcSize)
       return false;
   } else {

Modified: llvm/trunk/test/Transforms/MemCpyOpt/memcpy.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/MemCpyOpt/memcpy.ll?rev=183606&r1=183605&r2=183606&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/MemCpyOpt/memcpy.ll (original)
+++ llvm/trunk/test/Transforms/MemCpyOpt/memcpy.ll Fri Jun  7 23:56:05 2013
@@ -185,28 +185,6 @@ define void @test10(%opaque* noalias noc
   ret void
 }
 
-; Test11 is similar to test10 except that the instruction "store i32 %y, i32* %a"
-; before the call-site is deleted. MemCopyOpt is able to optimize this snippet into
-;
-;  %x1 = bitcast %opaque* %x to i32*
-;  call void @foo(i32* noalias nocapture %x1)
-;  ret void
-; 
-
-define void @test11(%opaque* noalias nocapture sret %x, i32 %y) {
-; CHECK: test11
-; CHECK: %x1 = bitcast %opaque* %x to i32*
-; CHECK: call void @foo(i32* noalias nocapture %x1)
-; CHECK: ret void
-
-  %a = alloca i32, align 4
-  call void @foo(i32* noalias nocapture %a)
-  %c = load i32* %a
-  %d = bitcast %opaque* %x to i32*
-  store i32 %c, i32* %d
-  ret void
-}
-
 declare void @f1(%struct.big* sret)
 declare void @f2(%struct.big*)
 





More information about the llvm-commits mailing list