From 55d484802f3e27930317739efc5f5956b78aac25 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Thu, 26 May 2011 00:10:27 +0000 Subject: [PATCH] Skip extra copy from aggregate where it isn't necessary; rdar://problem/8139919 . This shouldn't make much of a difference at -O3, but should substantially reduce the number of generated memcpy's at -O0. Originally r130717, but was backed out due to an ObjC regression. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@132102 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGCall.cpp | 13 +++++++++++++ lib/CodeGen/CGCall.h | 9 +++++---- test/CodeGen/byval-memcpy-elim.c | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 test/CodeGen/byval-memcpy-elim.c diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp index 4c9f3d467a..712ae89a48 100644 --- a/lib/CodeGen/CGCall.cpp +++ b/lib/CodeGen/CGCall.cpp @@ -1189,6 +1189,15 @@ void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E, return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0), type); + if (hasAggregateLLVMType(type) && isa(E) && + cast(E)->getCastKind() == CK_LValueToRValue) { + LValue L = EmitLValue(cast(E)->getSubExpr()); + assert(L.isSimple()); + args.add(RValue::getAggregate(L.getAddress(), L.isVolatileQualified()), + type, /*NeedsCopy*/true); + return; + } + args.add(EmitAnyExprToTemp(E), type); } @@ -1254,6 +1263,10 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, Alignment, I->Ty); else StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); + } else if (I->NeedsCopy && !ArgInfo.getIndirectByVal()) { + Args.push_back(CreateMemTemp(I->Ty)); + EmitAggregateCopy(Args.back(), RV.getAggregateAddr(), I->Ty, + RV.isVolatileQualified()); } else { Args.push_back(RV.getAggregateAddr()); } diff --git a/lib/CodeGen/CGCall.h b/lib/CodeGen/CGCall.h index 3f600c04e5..160a62eab3 100644 --- a/lib/CodeGen/CGCall.h +++ b/lib/CodeGen/CGCall.h @@ -47,8 +47,9 @@ namespace CodeGen { struct CallArg { RValue RV; QualType Ty; - CallArg(RValue rv, QualType ty) - : RV(rv), Ty(ty) + bool NeedsCopy; + CallArg(RValue rv, QualType ty, bool needscopy) + : RV(rv), Ty(ty), NeedsCopy(needscopy) { } }; @@ -57,8 +58,8 @@ namespace CodeGen { class CallArgList : public llvm::SmallVector { public: - void add(RValue rvalue, QualType type) { - push_back(CallArg(rvalue, type)); + void add(RValue rvalue, QualType type, bool needscopy = false) { + push_back(CallArg(rvalue, type, needscopy)); } }; diff --git a/test/CodeGen/byval-memcpy-elim.c b/test/CodeGen/byval-memcpy-elim.c new file mode 100644 index 0000000000..8aa08fb07b --- /dev/null +++ b/test/CodeGen/byval-memcpy-elim.c @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -emit-llvm -triple x86_64-apple-darwin10 < %s | FileCheck %s + +struct Test1S { + long NumDecls; + long X; + long Y; +}; +struct Test2S { + long NumDecls; + long X; +}; + +// Make sure we don't generate extra memcpy for lvalues +void test1a(struct Test1S, struct Test2S); +// CHECK: define void @test1( +// CHECK-NOT: memcpy +// CHECK: call void @test1a +void test1(struct Test1S *A, struct Test2S *B) { + test1a(*A, *B); +} -- 2.40.0