Index: lib/CodeGen/CGDebugInfo.cpp =================================================================== --- lib/CodeGen/CGDebugInfo.cpp +++ lib/CodeGen/CGDebugInfo.cpp @@ -3466,17 +3466,17 @@ // functions there won't be an implicit param at arg1 and // otherwise it is 'self' or 'this'. if (isa(VD) && ArgNo && *ArgNo == 1) - Flags |= llvm::DINode::FlagObjectPointer; - if (auto *Arg = dyn_cast(Storage)) - if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() && - !VD->getType()->isPointerType()) - Expr.push_back(llvm::dwarf::DW_OP_deref); + Flags |= llvm::DINode::FlagObjectPointer; + // Note: Older versions of clang used to emit byval references with an extra + // DW_OP_deref, because they referenced the IR arg directly instead of + // referencing an alloca. Newer versions of LLVM don't treat allocas + // differently from other function arguments when used in a dbg.declare. auto *Scope = cast(LexicalBlockStack.back()); - StringRef Name = VD->getName(); if (!Name.empty()) { if (VD->hasAttr()) { + // Here, we need an offset *into* the alloca. CharUnits offset = CharUnits::fromQuantity(32); Expr.push_back(llvm::dwarf::DW_OP_plus); // offset of __forwarding field @@ -3488,22 +3488,7 @@ // offset of x field offset = CGM.getContext().toCharUnitsFromBits(XOffset); Expr.push_back(offset.getQuantity()); - - // Create the descriptor for the variable. - auto *D = ArgNo - ? DBuilder.createParameterVariable(Scope, VD->getName(), - *ArgNo, Unit, Line, Ty) - : DBuilder.createAutoVariable(Scope, VD->getName(), Unit, - Line, Ty, Align); - - // Insert an llvm.dbg.declare into the current block. - DBuilder.insertDeclare( - Storage, D, DBuilder.createExpression(Expr), - llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt), - Builder.GetInsertBlock()); - return; - } else if (isa(VD->getType())) - Expr.push_back(llvm::dwarf::DW_OP_deref); + } } else if (const auto *RT = dyn_cast(VD->getType())) { // If VD is an anonymous union then Storage represents value for // all union fields. @@ -3606,8 +3591,7 @@ ->getElementOffset(blockInfo.getCapture(VD).getIndex())); SmallVector addr; - if (isa(Storage)) - addr.push_back(llvm::dwarf::DW_OP_deref); + addr.push_back(llvm::dwarf::DW_OP_deref); addr.push_back(llvm::dwarf::DW_OP_plus); addr.push_back(offset.getQuantity()); if (isByRef) { @@ -3633,12 +3617,11 @@ // Insert an llvm.dbg.declare into the current block. auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt); + auto *Expr = DBuilder.createExpression(addr); if (InsertPoint) - DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL, - InsertPoint); + DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint); else - DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL, - Builder.GetInsertBlock()); + DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock()); } void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, Index: test/CodeGen/debug-info-vla.c =================================================================== --- test/CodeGen/debug-info-vla.c +++ test/CodeGen/debug-info-vla.c @@ -4,8 +4,8 @@ { // CHECK: dbg.declare // CHECK: dbg.declare({{.*}}, metadata ![[VAR:.*]], metadata ![[EXPR:.*]]) -// CHECK: ![[VAR]] = !DILocalVariable(name: "vla",{{.*}} line: [[@LINE+2]] -// CHECK: ![[EXPR]] = !DIExpression(DW_OP_deref) +// CHECK: ![[EXPR]] = !DIExpression() +// CHECK: ![[VAR]] = !DILocalVariable(name: "vla",{{.*}} line: [[@LINE+1]] int vla[s]; int i; for (i = 0; i < s; i++) { Index: test/CodeGenCXX/debug-info.cpp =================================================================== --- test/CodeGenCXX/debug-info.cpp +++ test/CodeGenCXX/debug-info.cpp @@ -21,6 +21,7 @@ // CHECK: ![[INCTYPE]] = !DICompositeType(tag: DW_TAG_structure_type, name: "incomplete" // CHECK-SAME: DIFlagFwdDecl +// CHECK: ![[EXPR]] = !DIExpression() template struct Identity { typedef T Type; @@ -117,7 +118,6 @@ // For some reason function arguments ended up down here // CHECK: ![[F]] = !DILocalVariable(name: "f", arg: 1, scope: ![[FUNC]] // CHECK-SAME: type: ![[FOO]] -// CHECK: ![[EXPR]] = !DIExpression(DW_OP_deref) foo func(foo f) { return f; // reference 'f' for now because otherwise we hit another bug } Index: test/CodeGenOpenCL/amdgpu-debug-info-variable-expression.cl =================================================================== --- test/CodeGenOpenCL/amdgpu-debug-info-variable-expression.cl +++ test/CodeGenOpenCL/amdgpu-debug-info-variable-expression.cl @@ -1,6 +1,5 @@ // RUN: %clang -cl-std=CL2.0 -emit-llvm -g -O0 -S -target amdgcn-amd-amdhsa -mcpu=fiji -o - %s | FileCheck %s -// CHECK-DAG: ![[NONE:[0-9]+]] = !DIExpression() // CHECK-DAG: ![[LOCAL:[0-9]+]] = !DIExpression(DW_OP_constu, 2, DW_OP_swap, DW_OP_xderef) // CHECK-DAG: ![[PRIVATE:[0-9]+]] = !DIExpression(DW_OP_constu, 1, DW_OP_swap, DW_OP_xderef) @@ -82,7 +81,7 @@ int *FuncVar4 = Tmp1; // CHECK-DAG: ![[FUNCVAR5:[0-9]+]] = !DILocalVariable(name: "FuncVar5", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}}) - // CHECK-DAG: call void @llvm.dbg.declare(metadata i32 addrspace(1)** {{.*}}, metadata ![[FUNCVAR5]], metadata ![[NONE]]), !dbg !{{[0-9]+}} + // CHECK-DAG: call void @llvm.dbg.declare(metadata i32 addrspace(1)** {{.*}}, metadata ![[FUNCVAR5]], metadata ![[NONE:[0-9]+]]), !dbg !{{[0-9]+}} global int *constant FuncVar5 = KernelArg0; // CHECK-DAG: ![[FUNCVAR6:[0-9]+]] = !DILocalVariable(name: "FuncVar6", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: !{{[0-9]+}}) // CHECK-DAG: call void @llvm.dbg.declare(metadata i32 addrspace(2)** {{.*}}, metadata ![[FUNCVAR6]], metadata ![[NONE]]), !dbg !{{[0-9]+}}