From: Amy Huang Date: Thu, 20 Jun 2019 17:15:21 +0000 (+0000) Subject: Store a pointer to the return value in a static alloca and let the debugger use that X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4730a1bd73f78e598c042c070e928d443108a988;p=clang Store a pointer to the return value in a static alloca and let the debugger use that as the variable address for NRVO variables. Subscribers: hiraditya, cfe-commits, llvm-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D63361 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@363952 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp index a53ca8f524..dbe582cd15 100644 --- a/lib/CodeGen/CGDebugInfo.cpp +++ b/lib/CodeGen/CGDebugInfo.cpp @@ -3835,7 +3835,8 @@ CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD, llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage, llvm::Optional ArgNo, - CGBuilderTy &Builder) { + CGBuilderTy &Builder, + const bool UsePointerValue) { assert(DebugKind >= codegenoptions::LimitedDebugInfo); assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); if (VD->hasAttr()) @@ -3940,6 +3941,16 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, } } + // Clang stores the sret pointer provided by the caller in a static alloca. + // Use DW_OP_deref to tell the debugger to load the pointer and treat it as + // the address of the variable. + if (UsePointerValue) { + assert(std::find(Expr.begin(), Expr.end(), llvm::dwarf::DW_OP_deref) == + Expr.end() && + "Debug info already contains DW_OP_deref."); + Expr.push_back(llvm::dwarf::DW_OP_deref); + } + // Create the descriptor for the variable. auto *D = ArgNo ? DBuilder.createParameterVariable( Scope, Name, *ArgNo, Unit, Line, Ty, @@ -3958,9 +3969,10 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::DILocalVariable * CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, llvm::Value *Storage, - CGBuilderTy &Builder) { + CGBuilderTy &Builder, + const bool UsePointerValue) { assert(DebugKind >= codegenoptions::LimitedDebugInfo); - return EmitDeclare(VD, Storage, llvm::None, Builder); + return EmitDeclare(VD, Storage, llvm::None, Builder, UsePointerValue); } void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) { diff --git a/lib/CodeGen/CGDebugInfo.h b/lib/CodeGen/CGDebugInfo.h index 054df01d97..f0c67cdecb 100644 --- a/lib/CodeGen/CGDebugInfo.h +++ b/lib/CodeGen/CGDebugInfo.h @@ -422,9 +422,10 @@ public: /// declaration. /// Returns a pointer to the DILocalVariable associated with the /// llvm.dbg.declare, or nullptr otherwise. - llvm::DILocalVariable *EmitDeclareOfAutoVariable(const VarDecl *Decl, - llvm::Value *AI, - CGBuilderTy &Builder); + llvm::DILocalVariable * + EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI, + CGBuilderTy &Builder, + const bool UsePointerValue = false); /// Emit call to \c llvm.dbg.label for an label. void EmitLabel(const LabelDecl *D, CGBuilderTy &Builder); @@ -507,7 +508,8 @@ private: /// llvm.dbg.declare, or nullptr otherwise. llvm::DILocalVariable *EmitDeclare(const VarDecl *decl, llvm::Value *AI, llvm::Optional ArgNo, - CGBuilderTy &Builder); + CGBuilderTy &Builder, + const bool UsePointerValue = false); struct BlockByRefType { /// The wrapper struct used inside the __block_literal struct. diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index 233212c76b..c4e0d89c83 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -1403,12 +1403,11 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { getLangOpts().OpenMP ? CGM.getOpenMPRuntime().getAddressOfLocalVariable(*this, &D) : Address::invalid(); + bool NRVO = getLangOpts().ElideConstructors && D.isNRVOVariable(); + if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) { address = OpenMPLocalAddr; } else if (Ty->isConstantSizeType()) { - bool NRVO = getLangOpts().ElideConstructors && - D.isNRVOVariable(); - // If this value is an array or struct with a statically determinable // constant initializer, there are optimizations we can do. // @@ -1561,8 +1560,16 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) { // Emit debug info for local var declaration. if (EmitDebugInfo && HaveInsertPoint()) { + Address DebugAddr = address; + bool UsePointerValue = NRVO && ReturnValuePointer.isValid(); DI->setLocation(D.getLocation()); - (void)DI->EmitDeclareOfAutoVariable(&D, address.getPointer(), Builder); + + // If NRVO, use a pointer to the return address. + if (UsePointerValue) + DebugAddr = ReturnValuePointer; + + (void)DI->EmitDeclareOfAutoVariable(&D, DebugAddr.getPointer(), Builder, + UsePointerValue); } if (D.hasAttr() && HaveInsertPoint()) diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp index b9573f71a3..581bbe5efa 100644 --- a/lib/CodeGen/CodeGenFunction.cpp +++ b/lib/CodeGen/CodeGenFunction.cpp @@ -895,6 +895,13 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, if (CurFnInfo->getReturnInfo().isSRetAfterThis()) ++AI; ReturnValue = Address(&*AI, CurFnInfo->getReturnInfo().getIndirectAlign()); + if (!CurFnInfo->getReturnInfo().getIndirectByVal()) { + ReturnValuePointer = + CreateDefaultAlignTempAlloca(Int8PtrTy, "result.ptr"); + Builder.CreateStore(Builder.CreatePointerBitCastOrAddrSpaceCast( + ReturnValue.getPointer(), Int8PtrTy), + ReturnValuePointer); + } } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::InAlloca && !hasScalarEvaluationKind(CurFnInfo->getReturnType())) { // Load the sret pointer from the argument struct and return into that. @@ -902,6 +909,7 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, llvm::Function::arg_iterator EI = CurFn->arg_end(); --EI; llvm::Value *Addr = Builder.CreateStructGEP(nullptr, &*EI, Idx); + ReturnValuePointer = Address(Addr, getPointerAlign()); Addr = Builder.CreateAlignedLoad(Addr, getPointerAlign(), "agg.result"); ReturnValue = Address(Addr, getNaturalTypeAlignment(RetTy)); } else { diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index b274f45867..e95a07ce0e 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -327,6 +327,10 @@ public: /// value. This is invalid iff the function has no return value. Address ReturnValue = Address::invalid(); + /// ReturnValuePointer - The temporary alloca to hold a pointer to sret. + /// This is invalid if sret is not in use. + Address ReturnValuePointer = Address::invalid(); + /// Return true if a label was seen in the current scope. bool hasLabelBeenSeenInCurrentScope() const { if (CurLexicalScope) diff --git a/test/CodeGen/arm64-microsoft-arguments.cpp b/test/CodeGen/arm64-microsoft-arguments.cpp index 356bd8c974..bca7cc94b3 100644 --- a/test/CodeGen/arm64-microsoft-arguments.cpp +++ b/test/CodeGen/arm64-microsoft-arguments.cpp @@ -43,7 +43,7 @@ S3 f3() { // Pass and return aggregate (of size < 16 bytes) with non-trivial destructor. // Passed directly but returned indirectly. // CHECK: define {{.*}} void {{.*}}f4{{.*}}(%struct.S4* inreg noalias sret %agg.result) -// CHECK: call void {{.*}}func4{{.*}}(%struct.S4* inreg sret %agg.result, [2 x i64] %4) +// CHECK: call void {{.*}}func4{{.*}}(%struct.S4* inreg sret %agg.result, [2 x i64] %5) struct S4 { int a[3]; ~S4(); @@ -57,7 +57,7 @@ S4 f4() { // Pass and return from instance method called from instance method. // CHECK: define {{.*}} void @{{.*}}bar@Q1{{.*}}(%class.Q1* %this, %class.P1* inreg noalias sret %agg.result) -// CHECK: call void {{.*}}foo@P1{{.*}}(%class.P1* %ref.tmp, %class.P1* inreg sret %agg.result, i8 %0) +// CHECK: call void {{.*}}foo@P1{{.*}}(%class.P1* %ref.tmp, %class.P1* inreg sret %agg.result, i8 %1) class P1 { public: diff --git a/test/CodeGenCXX/conditional-gnu-ext.cpp b/test/CodeGenCXX/conditional-gnu-ext.cpp index b073e0cc66..613dd65ee7 100644 --- a/test/CodeGenCXX/conditional-gnu-ext.cpp +++ b/test/CodeGenCXX/conditional-gnu-ext.cpp @@ -79,7 +79,7 @@ namespace test3 { B test0(B &x) { // CHECK-LABEL: define void @_ZN5test35test0ERNS_1BE( // CHECK: [[X:%.*]] = alloca [[B:%.*]]*, - // CHECK-NEXT: store [[B]]* {{%.*}}, [[B]]** [[X]] + // CHECK: store [[B]]* {{%.*}}, [[B]]** [[X]] // CHECK-NEXT: [[T0:%.*]] = load [[B]]*, [[B]]** [[X]] // CHECK-NEXT: [[BOOL:%.*]] = call zeroext i1 @_ZN5test31BcvbEv([[B]]* [[T0]]) // CHECK-NEXT: br i1 [[BOOL]] @@ -94,7 +94,7 @@ namespace test3 { B test1() { // CHECK-LABEL: define void @_ZN5test35test1Ev( // CHECK: [[TEMP:%.*]] = alloca [[B]], - // CHECK-NEXT: call void @_ZN5test312test1_helperEv([[B]]* sret [[TEMP]]) + // CHECK: call void @_ZN5test312test1_helperEv([[B]]* sret [[TEMP]]) // CHECK-NEXT: [[BOOL:%.*]] = call zeroext i1 @_ZN5test31BcvbEv([[B]]* [[TEMP]]) // CHECK-NEXT: br i1 [[BOOL]] // CHECK: call void @_ZN5test31BC1ERKS0_([[B]]* [[RESULT:%.*]], [[B]]* dereferenceable({{[0-9]+}}) [[TEMP]]) @@ -111,7 +111,7 @@ namespace test3 { A test2(B &x) { // CHECK-LABEL: define void @_ZN5test35test2ERNS_1BE( // CHECK: [[X:%.*]] = alloca [[B]]*, - // CHECK-NEXT: store [[B]]* {{%.*}}, [[B]]** [[X]] + // CHECK: store [[B]]* {{%.*}}, [[B]]** [[X]] // CHECK-NEXT: [[T0:%.*]] = load [[B]]*, [[B]]** [[X]] // CHECK-NEXT: [[BOOL:%.*]] = call zeroext i1 @_ZN5test31BcvbEv([[B]]* [[T0]]) // CHECK-NEXT: br i1 [[BOOL]] @@ -126,7 +126,7 @@ namespace test3 { A test3() { // CHECK-LABEL: define void @_ZN5test35test3Ev( // CHECK: [[TEMP:%.*]] = alloca [[B]], - // CHECK-NEXT: call void @_ZN5test312test3_helperEv([[B]]* sret [[TEMP]]) + // CHECK: call void @_ZN5test312test3_helperEv([[B]]* sret [[TEMP]]) // CHECK-NEXT: [[BOOL:%.*]] = call zeroext i1 @_ZN5test31BcvbEv([[B]]* [[TEMP]]) // CHECK-NEXT: br i1 [[BOOL]] // CHECK: call void @_ZN5test31BcvNS_1AEEv([[A]]* sret [[RESULT:%.*]], [[B]]* [[TEMP]]) diff --git a/test/CodeGenCXX/debug-info-nrvo.cpp b/test/CodeGenCXX/debug-info-nrvo.cpp new file mode 100644 index 0000000000..a03571aeac --- /dev/null +++ b/test/CodeGenCXX/debug-info-nrvo.cpp @@ -0,0 +1,28 @@ +// RUN: %clangxx -target x86_64-unknown-unknown -g %s -emit-llvm -S -o - | FileCheck %s +// RUN: %clangxx -target x86_64-unknown-unknown -g -fno-elide-constructors %s -emit-llvm -S -o - | FileCheck %s -check-prefix=NOELIDE +struct Foo { + Foo() = default; + Foo(Foo &&other) { x = other.x; } + int x; +}; +void some_function(int); +Foo getFoo() { + Foo foo; + foo.x = 41; + some_function(foo.x); + return foo; +} + +int main() { + Foo bar = getFoo(); + return bar.x; +} + +// Check that NRVO variables are stored as a pointer with deref if they are +// stored in the return register. + +// CHECK: %result.ptr = alloca i8*, align 8 +// CHECK: call void @llvm.dbg.declare(metadata i8** %result.ptr, +// CHECK-SAME: metadata !DIExpression(DW_OP_deref) +// NOELIDE: call void @llvm.dbg.declare(metadata %struct.Foo* %foo, +// NOELIDE-SAME: metadata !DIExpression() diff --git a/test/CodeGenCXX/lambda-expressions.cpp b/test/CodeGenCXX/lambda-expressions.cpp index 2c625daaf8..566132ad64 100644 --- a/test/CodeGenCXX/lambda-expressions.cpp +++ b/test/CodeGenCXX/lambda-expressions.cpp @@ -195,7 +195,6 @@ namespace pr28595 { // CHECK-NEXT: ret i32 // CHECK-LABEL: define internal void @"_ZZ1hvEN4$_118__invokeEv"(%struct.A* noalias sret %agg.result) {{.*}} { -// CHECK-NOT: = // CHECK: call void @"_ZZ1hvENK4$_11clEv"(%struct.A* sret %agg.result, // CHECK-NEXT: ret void struct A { ~A(); }; diff --git a/test/CodeGenObjC/objc-non-trivial-struct-nrvo.m b/test/CodeGenObjC/objc-non-trivial-struct-nrvo.m index 798aba2489..53ff433989 100644 --- a/test/CodeGenObjC/objc-non-trivial-struct-nrvo.m +++ b/test/CodeGenObjC/objc-non-trivial-struct-nrvo.m @@ -38,7 +38,6 @@ Trivial testTrivial(void) { void func1(TrivialBig *); // CHECK: define void @testTrivialBig(%[[STRUCT_TRIVIALBIG]]* noalias sret %[[AGG_RESULT:.*]]) -// CHECK-NOT: alloca // CHECK: call void @func1(%[[STRUCT_TRIVIALBIG]]* %[[AGG_RESULT]]) // CHECK-NEXT: ret void