[cfe-commits] r51566 - in /cfe/trunk/lib/CodeGen: CGExprAgg.cpp CodeGenModule.cpp CodeGenModule.h

Eli Friedman eli.friedman at gmail.com
Mon May 26 05:59:40 PDT 2008


Author: efriedma
Date: Mon May 26 07:59:39 2008
New Revision: 51566

URL: http://llvm.org/viewvc/llvm-project?rev=51566&view=rev
Log:
Emit memmove, not memcpy, for structure copies; this is unfortunately 
required for correctness in cases of copying a struct to itself or to 
an overlapping struct (itself for cases like *a = *a, and overlapping 
is possible with unions).

Hopefully, this won't end up being a perf issue; LLVM *should* be able 
to optimize memmove to memcpy in a lot of cases, and for small copies 
the generated code *should* be mostly comparable. (In reality, LLVM 
is currently horrible at optimizing memmove, but that's a bug, not a 
fundamental issue.)

gcc currently generates wrong code; that's 
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32667.


Modified:
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Mon May 26 07:59:39 2008
@@ -131,7 +131,7 @@
                                        llvm::Value *SrcPtr, QualType Ty) {
   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
   
-  // Aggregate assignment turns into llvm.memcpy.
+  // Aggregate assignment turns into llvm.memmove.
   const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
   if (DestPtr->getType() != BP)
     DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
@@ -144,14 +144,14 @@
   // FIXME: Handle variable sized types.
   const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
   
-  llvm::Value *MemCpyOps[4] = {
+  llvm::Value *MemMoveOps[4] = {
     DestPtr, SrcPtr,
     // TypeInfo.first describes size in bits.
     llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
     llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
   };
   
-  Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
+  Builder.CreateCall(CGF.CGM.getMemMoveFn(), MemMoveOps, MemMoveOps+4);
 }
 
 

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Mon May 26 07:59:39 2008
@@ -35,7 +35,8 @@
                              llvm::Module &M, const llvm::TargetData &TD,
                              Diagnostic &diags, bool GenerateDebugInfo)
   : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags),
-    Types(C, M, TD), MemCpyFn(0), MemSetFn(0), CFConstantStringClassRef(0) {
+    Types(C, M, TD), MemCpyFn(0), MemMoveFn(0), MemSetFn(0),
+    CFConstantStringClassRef(0) {
   //TODO: Make this selectable at runtime
   Runtime = CreateObjCRuntime(M,
       getTypes().ConvertType(getContext().IntTy),
@@ -589,6 +590,17 @@
   return MemCpyFn = getIntrinsic(IID);
 }
 
+llvm::Function *CodeGenModule::getMemMoveFn() {
+  if (MemMoveFn) return MemMoveFn;
+  llvm::Intrinsic::ID IID;
+  switch (Context.Target.getPointerWidth(0)) {
+  default: assert(0 && "Unknown ptr width");
+  case 32: IID = llvm::Intrinsic::memmove_i32; break;
+  case 64: IID = llvm::Intrinsic::memmove_i64; break;
+  }
+  return MemMoveFn = getIntrinsic(IID);
+}
+
 llvm::Function *CodeGenModule::getMemSetFn() {
   if (MemSetFn) return MemSetFn;
   llvm::Intrinsic::ID IID;

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=51566&r1=51565&r2=51566&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Mon May 26 07:59:39 2008
@@ -59,6 +59,7 @@
   CGDebugInfo *DebugInfo;
 
   llvm::Function *MemCpyFn;
+  llvm::Function *MemMoveFn;
   llvm::Function *MemSetFn;
   llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
   std::vector<const NamedDecl*> StaticDecls;
@@ -101,6 +102,7 @@
   /// array containing the literal.  The result is pointer to array type.
   llvm::Constant *GetAddrOfConstantString(const std::string& str);
   llvm::Function *getMemCpyFn();
+  llvm::Function *getMemMoveFn();
   llvm::Function *getMemSetFn();
   llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0, 
                                unsigned NumTys = 0);





More information about the cfe-commits mailing list