diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index f584980d7f8a..30ff9ecbaf35 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -131,7 +131,7 @@ void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr, 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 @@ void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr, // 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); } diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index ec61d667eed0..329599c1166b 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -35,7 +35,8 @@ CodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, 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 @@ llvm::Function *CodeGenModule::getMemCpyFn() { 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; diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index bc216e4f4852..992a3bf3c226 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -59,6 +59,7 @@ class CodeGenModule { CGDebugInfo *DebugInfo; llvm::Function *MemCpyFn; + llvm::Function *MemMoveFn; llvm::Function *MemSetFn; llvm::DenseMap GlobalDeclMap; std::vector StaticDecls; @@ -101,6 +102,7 @@ public: /// 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);