[cfe-commits] r65699 - in /cfe/trunk: lib/CodeGen/CGExprAgg.cpp test/CodeGen/struct-copy.c

Chris Lattner sabre at nondot.org
Sat Feb 28 10:18:58 PST 2009


Author: lattner
Date: Sat Feb 28 12:18:58 2009
New Revision: 65699

URL: http://llvm.org/viewvc/llvm-project?rev=65699&view=rev
Log:
after going around in circles a few times, finally cave and emit structure
copies with memcpy instead of memmove.  This matches what GCC does and if it
causes a problem with a particular libc we can always fix it with a target
hook.

Added:
    cfe/trunk/test/CodeGen/struct-copy.c
Modified:
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprAgg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprAgg.cpp?rev=65699&r1=65698&r2=65699&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Sat Feb 28 12:18:58 2009
@@ -518,7 +518,16 @@
                                         llvm::Value *SrcPtr, QualType Ty) {
   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
   
-  // Aggregate assignment turns into llvm.memmove.
+  // Aggregate assignment turns into llvm.memset.  This is almost valid per
+  // C99 6.5.16.1p3, which states "If the value being stored in an object is
+  // read from another object that overlaps in anyway the storage of the first
+  // object, then the overlap shall be exact and the two objects shall have
+  // qualified or unqualified versions of a compatible type."
+  //
+  // memset is not defined if the source and destination pointers are exactly
+  // equal, but other compilers do this optimization, and almost every memcpy
+  // implementation handles this case safely.  If there is a libc that does not
+  // safely handle this, we can add a target hook.
   const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   if (DestPtr->getType() != BP)
     DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
@@ -531,7 +540,7 @@
   // FIXME: Handle variable sized types.
   const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
   
-  Builder.CreateCall4(CGM.getMemMoveFn(),
+  Builder.CreateCall4(CGM.getMemCpyFn(),
                       DestPtr, SrcPtr,
                       // TypeInfo.first describes size in bits.
                       llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),

Added: cfe/trunk/test/CodeGen/struct-copy.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/struct-copy.c?rev=65699&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/struct-copy.c (added)
+++ cfe/trunk/test/CodeGen/struct-copy.c Sat Feb 28 12:18:58 2009
@@ -0,0 +1,7 @@
+// RUN: clang -emit-llvm %s -o - | grep 'call.*llvm.memcpy'
+struct x { int a[100]; };
+
+
+void foo(struct x *P, struct x *Q) {
+  *P = *Q;
+}





More information about the cfe-commits mailing list