From 129ab6c6c5ea835f0ebc798e3f019f47af205201 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Thu, 23 Oct 2014 19:12:43 +0000 Subject: [PATCH] DebugInfo: Simplify/tidy/correct global variable decl/def emission handling. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This fixes a bug (introduced by fixing the IR emitted from Clang where the definition of a static member would be scoped within the class, rather than within its lexical decl context) where the definition of a static variable would be placed inside a class. It also improves source fidelity by scoping static class member definitions inside the lexical decl context in which tehy are written (eg: namespace n { class foo { static int i; } int foo::i; } - the definition of 'i' will be within the namespace 'n' in the DWARF output now). Lastly, and the original goal, this reduces debug info size slightly (and makes debug info easier to read, etc) by placing the definitions of non-member global variables within their namespace, rather than using a separate namespace-scoped declaration along with a definition at global scope. Based on patches and discussion with Frédéric. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220497 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp | 77 +++++++------------ .../DebugInfo/X86/debug-info-static-member.ll | 24 +++--- test/DebugInfo/X86/gnu-public-names.ll | 39 ++++------ 3 files changed, 53 insertions(+), 87 deletions(-) diff --git a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp index 89d0df28e6b..e14ac5ac7f6 100644 --- a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ b/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -105,28 +105,23 @@ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(DIGlobalVariable GV) { DIScope GVContext = GV.getContext(); DIType GTy = DD->resolve(GV.getType()); - // If this is a static data member definition, some attributes belong - // to the declaration DIE. - DIE *VariableDIE = nullptr; - bool IsStaticMember = false; - DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration(); - if (SDMDecl.Verify()) { - assert(SDMDecl.isStaticMember() && "Expected static member decl"); - // We need the declaration DIE that is in the static member's class. - VariableDIE = getOrCreateStaticMemberDIE(SDMDecl); - IsStaticMember = true; - } - - // If this is not a static data member definition, create the variable - // DIE and add the initial set of attributes to it. - if (!VariableDIE) { - // Construct the context before querying for the existence of the DIE in - // case such construction creates the DIE. - DIE *ContextDIE = getOrCreateContextDIE(GVContext); + // Construct the context before querying for the existence of the DIE in + // case such construction creates the DIE. + DIE *ContextDIE = getOrCreateContextDIE(GVContext); - // Add to map. - VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV); + // Add to map. + DIE *VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV); + DIScope DeclContext; + if (DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration()) { + DeclContext = resolve(SDMDecl.getContext()); + assert(SDMDecl.isStaticMember() && "Expected static member decl"); + assert(GV.isDefinition()); + // We need the declaration DIE that is in the static member's class. + DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl); + addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE); + } else { + DeclContext = GV.getContext(); // Add name and type. addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); addType(*VariableDIE, GTy); @@ -139,9 +134,11 @@ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(DIGlobalVariable GV) { addSourceLine(*VariableDIE, GV); } + if (!GV.isDefinition()) + addFlag(*VariableDIE, dwarf::DW_AT_declaration); + // Add location. bool addToAccelTable = false; - DIE *VariableSpecDIE = nullptr; bool isGlobalVariable = GV.getGlobal() != nullptr; if (isGlobalVariable) { addToAccelTable = true; @@ -172,41 +169,21 @@ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(DIGlobalVariable GV) { DD->addArangeLabel(SymbolCU(this, Sym)); addOpAddress(*Loc, Sym); } - // A static member's declaration is already flagged as such. - if (!SDMDecl.Verify() && !GV.isDefinition()) - addFlag(*VariableDIE, dwarf::DW_AT_declaration); - // Do not create specification DIE if context is either compile unit - // or a subprogram. - if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() && - !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) { - // Create specification DIE. - VariableSpecDIE = &createAndAddDIE(dwarf::DW_TAG_variable, UnitDie); - addDIEEntry(*VariableSpecDIE, dwarf::DW_AT_specification, *VariableDIE); - addBlock(*VariableSpecDIE, dwarf::DW_AT_location, Loc); - // A static member's declaration is already flagged as such. - if (!SDMDecl.Verify()) - addFlag(*VariableDIE, dwarf::DW_AT_declaration); - } else { - addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); - } + + addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); // Add the linkage name. StringRef LinkageName = GV.getLinkageName(); if (!LinkageName.empty()) // From DWARF4: DIEs to which DW_AT_linkage_name may apply include: // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and // TAG_variable. - addString(IsStaticMember && VariableSpecDIE ? *VariableSpecDIE - : *VariableDIE, + addString(*VariableDIE, DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name : dwarf::DW_AT_MIPS_linkage_name, GlobalValue::getRealLinkageName(LinkageName)); } else if (const ConstantInt *CI = dyn_cast_or_null(GV.getConstant())) { - // AT_const_value was added when the static member was created. To avoid - // emitting AT_const_value multiple times, we only add AT_const_value when - // it is not a static member. - if (!IsStaticMember) - addConstantValue(*VariableDIE, CI, GTy); + addConstantValue(*VariableDIE, CI, GTy); } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV.getConstant())) { addToAccelTable = true; // GV is a merged global. @@ -223,19 +200,17 @@ DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(DIGlobalVariable GV) { addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); } - DIE *ResultDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE; - if (addToAccelTable) { - DD->addAccelName(GV.getName(), *ResultDIE); + DD->addAccelName(GV.getName(), *VariableDIE); // If the linkage name is different than the name, go ahead and output // that as well into the name table. if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) - DD->addAccelName(GV.getLinkageName(), *ResultDIE); + DD->addAccelName(GV.getLinkageName(), *VariableDIE); } - addGlobalName(GV.getName(), *ResultDIE, GV.getContext()); - return ResultDIE; + addGlobalName(GV.getName(), *VariableDIE, DeclContext); + return VariableDIE; } void DwarfCompileUnit::addRange(RangeSpan Range) { diff --git a/test/DebugInfo/X86/debug-info-static-member.ll b/test/DebugInfo/X86/debug-info-static-member.ll index 9f1cbacf3a6..37fe99710c3 100644 --- a/test/DebugInfo/X86/debug-info-static-member.ll +++ b/test/DebugInfo/X86/debug-info-static-member.ll @@ -1,5 +1,5 @@ ; RUN: llc %s -o %t -filetype=obj -O0 -mtriple=x86_64-unknown-linux-gnu -dwarf-version=4 -; RUN: llvm-dwarfdump -debug-dump=info %t | FileCheck %s -check-prefix=PRESENT +; RUN: llvm-dwarfdump -debug-dump=info %t | FileCheck %s -check-prefix=PRESENT ; RUN: llvm-dwarfdump -debug-dump=info %t | FileCheck %s -check-prefix=ABSENT ; RUN: llc %s -o %t -filetype=obj -O0 -mtriple=x86_64-apple-darwin -dwarf-version=4 ; RUN: llvm-dwarfdump -debug-dump=info %t | FileCheck %s -check-prefix=DARWINP @@ -68,7 +68,7 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) nounwind readnone !8 = metadata !{metadata !9} !9 = metadata !{metadata !"0x24\00int\000\0032\0032\000\000\005", null, null} ; [ DW_TAG_base_type ] [int] [line 0, size 32, align 32, offset 0, enc DW_ATE_signed] !10 = metadata !{metadata !12, metadata !27, metadata !28} -!12 = metadata !{metadata !"0x34\00a\00a\00_ZN1C1aE\0014\000\001", metadata !13, metadata !6, metadata !9, i32* @_ZN1C1aE, metadata !15} ; [ DW_TAG_variable ] [a] [line 14] [def] +!12 = metadata !{metadata !"0x34\00a\00a\00_ZN1C1aE\0014\000\001", null, metadata !6, metadata !9, i32* @_ZN1C1aE, metadata !15} ; [ DW_TAG_variable ] [a] [line 14] [def] !13 = metadata !{metadata !"0x2\00C\001\0032\0032\000\000\000", metadata !33, null, null, metadata !14, null, null, null} ; [ DW_TAG_class_type ] [C] [line 1, size 32, align 32, offset 0] [def] [from ] !14 = metadata !{metadata !15, metadata !16, metadata !19, metadata !20, metadata !23, metadata !24, metadata !26} !15 = metadata !{metadata !"0xd\00a\003\000\000\000\004097", metadata !33, metadata !13, metadata !9, null} ; [ DW_TAG_member ] [a] [line 3, size 0, align 0, offset 0] [private] [static] [from int] @@ -83,8 +83,8 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) nounwind readnone !24 = metadata !{metadata !"0xd\00const_c\0010\000\000\000\004099", metadata !33, metadata !13, metadata !25, i32 18} ; [ DW_TAG_member ] [const_c] [line 10, size 0, align 0, offset 0] [static] [from ] !25 = metadata !{metadata !"0x26\00\000\000\000\000\000", null, null, metadata !9} ; [ DW_TAG_const_type ] [line 0, size 0, align 0, offset 0] [from int] !26 = metadata !{metadata !"0xd\00d\0011\0032\0032\000\003", metadata !33, metadata !13, metadata !9} ; [ DW_TAG_member ] [d] [line 11, size 32, align 32, offset 0] [from int] -!27 = metadata !{metadata !"0x34\00b\00b\00_ZN1C1bE\0015\000\001", metadata !13, metadata !6, metadata !9, i32* @_ZN1C1bE, metadata !19} ; [ DW_TAG_variable ] [b] [line 15] [def] -!28 = metadata !{metadata !"0x34\00c\00c\00_ZN1C1cE\0016\000\001", metadata !13, metadata !6, metadata !9, i32* @_ZN1C1cE, metadata !23} ; [ DW_TAG_variable ] [c] [line 16] [def] +!27 = metadata !{metadata !"0x34\00b\00b\00_ZN1C1bE\0015\000\001", null, metadata !6, metadata !9, i32* @_ZN1C1bE, metadata !19} ; [ DW_TAG_variable ] [b] [line 15] [def] +!28 = metadata !{metadata !"0x34\00c\00c\00_ZN1C1cE\0016\000\001", null, metadata !6, metadata !9, i32* @_ZN1C1cE, metadata !23} ; [ DW_TAG_variable ] [c] [line 16] [def] !29 = metadata !{metadata !"0x100\00instance_C\0020\000", metadata !5, metadata !6, metadata !13} ; [ DW_TAG_auto_variable ] [instance_C] [line 20] !30 = metadata !{i32 20, i32 0, metadata !5, null} !31 = metadata !{i32 21, i32 0, metadata !5, null} @@ -95,6 +95,10 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) nounwind readnone ; (for variables) or DW_AT_const_value (for constants). ; ; PRESENT: .debug_info contents: +; PRESENT: DW_TAG_variable +; PRESENT-NEXT: DW_AT_specification {{.*}} "a" +; PRESENT-NEXT: DW_AT_location +; PRESENT-NEXT: DW_AT_linkage_name {{.*}} "_ZN1C1aE" ; PRESENT: DW_TAG_class_type ; PRESENT-NEXT: DW_AT_name {{.*}} "C" ; PRESENT: DW_TAG_member @@ -131,10 +135,6 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) nounwind readnone ; PRESENT: NULL ; Definitions point back to their declarations, and have a location. ; PRESENT: DW_TAG_variable -; PRESENT-NEXT: DW_AT_specification {{.*}} "a" -; PRESENT-NEXT: DW_AT_location -; PRESENT-NEXT: DW_AT_linkage_name {{.*}} "_ZN1C1aE" -; PRESENT: DW_TAG_variable ; PRESENT-NEXT: DW_AT_specification {{.*}} "b" ; PRESENT-NEXT: DW_AT_location ; PRESENT-NEXT: DW_AT_linkage_name {{.*}} "_ZN1C1bE" @@ -145,6 +145,10 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) nounwind readnone ; For Darwin gdb: ; DARWINP: .debug_info contents: +; DARWINP: DW_TAG_variable +; DARWINP-NEXT: DW_AT_specification {{.*}} "a" +; DARWINP-NEXT: DW_AT_location +; DARWINP-NEXT: DW_AT_linkage_name {{.*}} "_ZN1C1aE" ; DARWINP: DW_TAG_class_type ; DARWINP-NEXT: DW_AT_name {{.*}} "C" ; DARWINP: DW_TAG_member @@ -181,10 +185,6 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata) nounwind readnone ; DARWINP: NULL ; Definitions point back to their declarations, and have a location. ; DARWINP: DW_TAG_variable -; DARWINP-NEXT: DW_AT_specification {{.*}} "a" -; DARWINP-NEXT: DW_AT_location -; DARWINP-NEXT: DW_AT_linkage_name {{.*}} "_ZN1C1aE" -; DARWINP: DW_TAG_variable ; DARWINP-NEXT: DW_AT_specification {{.*}} "b" ; DARWINP-NEXT: DW_AT_location ; DARWINP-NEXT: DW_AT_linkage_name {{.*}} "_ZN1C1bE" diff --git a/test/DebugInfo/X86/gnu-public-names.ll b/test/DebugInfo/X86/gnu-public-names.ll index 72ae5b29346..1696288eed5 100644 --- a/test/DebugInfo/X86/gnu-public-names.ll +++ b/test/DebugInfo/X86/gnu-public-names.ll @@ -49,6 +49,9 @@ ; CHECK: DW_AT_GNU_pubnames [DW_FORM_flag_present] (true) ; CHECK-NOT: DW_AT_GNU_pubtypes [ +; CHECK: [[STATIC_MEM_VAR:0x[0-9a-f]+]]: DW_TAG_variable +; CHECK-NEXT: DW_AT_specification {{.*}} "static_member_variable" + ; CHECK: [[C:0x[0-9a-f]+]]: DW_TAG_structure_type ; CHECK-NEXT: DW_AT_name {{.*}} "C" @@ -66,20 +69,23 @@ ; CHECK: [[INT:0x[0-9a-f]+]]: DW_TAG_base_type ; CHECK-NEXT: DW_AT_name {{.*}} "int" -; CHECK: [[STATIC_MEM_VAR:0x[0-9a-f]+]]: DW_TAG_variable -; CHECK-NEXT: DW_AT_specification {{.*}} "static_member_variable" - ; CHECK: [[GLOB_VAR:0x[0-9a-f]+]]: DW_TAG_variable ; CHECK-NEXT: DW_AT_name {{.*}} "global_variable" ; CHECK: [[NS:0x[0-9a-f]+]]: DW_TAG_namespace ; CHECK-NEXT: DW_AT_name {{.*}} "ns" -; CHECK: DW_TAG_variable +; CHECK: [[GLOB_NS_VAR:0x[0-9a-f]+]]: DW_TAG_variable ; CHECK-NEXT: DW_AT_name {{.*}} "global_namespace_variable" +; CHECK-NOT: DW_AT_specification +; CHECK: DW_AT_location +; CHECK-NOT: DW_AT_specification -; CHECK: DW_TAG_variable +; CHECK: [[D_VAR:0x[0-9a-f]+]]: DW_TAG_variable ; CHECK-NEXT: DW_AT_name {{.*}} "d" +; CHECK-NOT: DW_AT_specification +; CHECK: DW_AT_location +; CHECK-NOT: DW_AT_specification ; CHECK: [[D:0x[0-9a-f]+]]: DW_TAG_structure_type ; CHECK-NEXT: DW_AT_name {{.*}} "D" @@ -90,12 +96,6 @@ ; CHECK-NOT: DW_TAG ; CHECK: DW_AT_name {{.*}} "global_namespace_function" -; CHECK: [[GLOB_NS_VAR:0x[0-9a-f]+]]: DW_TAG_variable -; CHECK-NEXT: DW_AT_specification {{.*}} "_ZN2ns25global_namespace_variableE" - -; CHECK: [[D_VAR:0x[0-9a-f]+]]: DW_TAG_variable -; CHECK-NEXT: DW_AT_specification {{.*}} "_ZN2ns1dE" - ; CHECK: DW_TAG_subprogram ; CHECK-NOT: DW_TAG ; CHECK: DW_AT_name {{.*}} "f3" @@ -116,7 +116,7 @@ ; CHECK: [[OUTER_ANON:.*]]: DW_TAG_namespace ; CHECK-NOT: {{DW_TAG|NULL}} ; CHECK-NOT: DW_AT_name -; CHECK: DW_TAG_variable +; CHECK: [[OUTER_ANON_C:.*]]: DW_TAG_variable ; CHECK-NOT: DW_TAG ; CHECK: DW_AT_name {{.*}} "c" ; CHECK-NOT: {{DW_TAG|NULL}} @@ -129,9 +129,6 @@ ; CHECK-NOT: {{DW_TAG|NULL}} ; CHECK: NULL ; CHECK-NOT: {{DW_TAG|NULL}} -; CHECK: [[OUTER_ANON_C:.*]]: DW_TAG_variable -; CHECK-NOT: DW_TAG -; CHECK-NEXT: DW_AT_specification {{.*}} "_ZN5outer12_GLOBAL__N_11cE" ; CHECK: [[ANON:.*]]: DW_TAG_namespace ; CHECK-NOT: DW_AT_name @@ -139,24 +136,18 @@ ; CHECK-NOT: DW_TAG ; CHECK: DW_AT_name {{.*}} "inner" ; CHECK-NOT: {{DW_TAG|NULL}} -; CHECK: DW_TAG_variable +; CHECK: [[ANON_INNER_B:.*]]: DW_TAG_variable ; CHECK-NOT: DW_TAG ; CHECK: DW_AT_name {{.*}} "b" ; CHECK-NOT: {{DW_TAG|NULL}} ; CHECK: NULL ; CHECK-NOT: {{DW_TAG|NULL}} -; CHECK: DW_TAG_variable +; CHECK: [[ANON_I:.*]]: DW_TAG_variable ; CHECK-NOT: DW_TAG ; CHECK: DW_AT_name {{.*}} "i" ; CHECK-NOT: {{DW_TAG|NULL}} ; CHECK: NULL ; CHECK-NOT: {{DW_TAG|NULL}} -; CHECK: [[ANON_INNER_B:.*]]: DW_TAG_variable -; CHECK-NOT: DW_TAG -; CHECK-NEXT: DW_AT_specification {{.*}} "_ZN12_GLOBAL__N_15inner1bE" -; CHECK: [[ANON_I:.*]]: DW_TAG_variable -; CHECK-NOT: DW_TAG -; CHECK-NEXT: DW_AT_specification {{.*}} "_ZN12_GLOBAL__N_11iE" ; CHECK: [[MEM_FUNC:0x[0-9a-f]+]]: DW_TAG_subprogram ; CHECK-NOT: DW_TAG @@ -312,7 +303,7 @@ attributes #1 = { nounwind readnone } !30 = metadata !{metadata !"0xf\00\000\0064\0064\000\000", null, null, metadata !7} ; [ DW_TAG_pointer_type ] [line 0, size 64, align 64, offset 0] [from int] !31 = metadata !{metadata !"0x2e\00f7\00f7\00_Z2f7v\0054\000\001\000\006\00256\000\0054", metadata !1, metadata !23, metadata !13, null, i32 ()* @_Z2f7v, null, null, metadata !2} ; [ DW_TAG_subprogram ] [line 54] [def] [f7] !32 = metadata !{metadata !33, metadata !34, metadata !35, metadata !36, metadata !37, metadata !38, metadata !41, metadata !44} -!33 = metadata !{metadata !"0x34\00static_member_variable\00static_member_variable\00_ZN1C22static_member_variableE\007\000\001", metadata !4, metadata !23, metadata !7, i32* @_ZN1C22static_member_variableE, metadata !6} ; [ DW_TAG_variable ] [static_member_variable] [line 7] [def] +!33 = metadata !{metadata !"0x34\00static_member_variable\00static_member_variable\00_ZN1C22static_member_variableE\007\000\001", null, metadata !23, metadata !7, i32* @_ZN1C22static_member_variableE, metadata !6} ; [ DW_TAG_variable ] [static_member_variable] [line 7] [def] !34 = metadata !{metadata !"0x34\00global_variable\00global_variable\00\0017\000\001", null, metadata !23, metadata !"_ZTS1C", %struct.C* @global_variable, null} ; [ DW_TAG_variable ] [global_variable] [line 17] [def] !35 = metadata !{metadata !"0x34\00global_namespace_variable\00global_namespace_variable\00_ZN2ns25global_namespace_variableE\0027\000\001", metadata !16, metadata !23, metadata !7, i32* @_ZN2ns25global_namespace_variableE, null} ; [ DW_TAG_variable ] [global_namespace_variable] [line 27] [def] !36 = metadata !{metadata !"0x34\00d\00d\00_ZN2ns1dE\0030\000\001", metadata !16, metadata !23, metadata !"_ZTSN2ns1DE", %"struct.ns::D"* @_ZN2ns1dE, null} ; [ DW_TAG_variable ] [d] [line 30] [def] -- 2.34.1