Index: lib/CodeGen/AsmPrinter/DwarfCompileUnit.h =================================================================== --- lib/CodeGen/AsmPrinter/DwarfCompileUnit.h +++ lib/CodeGen/AsmPrinter/DwarfCompileUnit.h @@ -79,7 +79,8 @@ void applyStmtList(DIE &D); /// getOrCreateGlobalVariableDIE - get or create global variable DIE. - DIE *getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV); + DIE *getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV, + DIE *ContextDIE = nullptr); /// addLabelAddress - Add a dwarf label attribute data and value using /// either DW_FORM_addr or DW_FORM_GNU_addr_index. Index: lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -98,7 +98,7 @@ /// getOrCreateGlobalVariableDIE - get or create global variable DIE. DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( - const DIGlobalVariable *GV) { + const DIGlobalVariable *GV, DIE *ContextDIE) { // Check for pre-existence. if (DIE *Die = getDIE(GV)) return Die; @@ -110,7 +110,8 @@ // Construct the context before querying for the existence of the DIE in // case such construction creates the DIE. - DIE *ContextDIE = getOrCreateContextDIE(GVContext); + if (ContextDIE == nullptr) + ContextDIE = getOrCreateContextDIE(GVContext); // Add to map. DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); @@ -344,9 +345,14 @@ constructImportedEntityDIE(cast(E.second))); } + auto GVRangeForScope = DD->findGlobalVariablesForScope(DS); + auto RTRangeForScope = DD->findRetainedTypesForScope(DS); + // If there are only other scopes as children, put them directly in the // parent instead, as this scope would serve no purpose. - if (Children.size() == ChildScopeCount) { + if (Children.size() == ChildScopeCount && + GVRangeForScope.begin() == GVRangeForScope.end() && + RTRangeForScope.begin() == RTRangeForScope.end()) { FinalChildren.insert(FinalChildren.end(), std::make_move_iterator(Children.begin()), std::make_move_iterator(Children.end())); @@ -354,6 +360,12 @@ } ScopeDIE = constructLexicalScopeDIE(Scope); assert(ScopeDIE && "Scope DIE should not be null."); + + for (const auto &GV : GVRangeForScope) + getOrCreateGlobalVariableDIE(cast(GV.second), ScopeDIE.get()); + + for (const auto &RT : RTRangeForScope) + getOrCreateTypeDIE(cast(RT.second), ScopeDIE.get()); } // Add children Index: lib/CodeGen/AsmPrinter/DwarfDebug.h =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.h +++ lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -261,10 +261,14 @@ // Holders for the various debug information flags that we might need to // have exposed. See accessor functions below for description. - // Holder for imported entities. typedef SmallVector, 32> - ImportedEntityMap; + ImportedEntityMap, GlobalVariableMap, RetainedTypeMap; + // Holder for imported entities. ImportedEntityMap ScopesWithImportedEntities; + // Holder for global variables. + GlobalVariableMap ScopesWithGlovalVariables; + // Holder for retained types. + RetainedTypeMap ScopesWithRetainedTypes; // Map from MDNodes for user-defined types to the type units that describe // them. @@ -605,6 +609,22 @@ less_first())); } + iterator_range + findGlobalVariablesForScope(const MDNode *Scope) const { + return make_range(std::equal_range( + ScopesWithGlovalVariables.begin(), ScopesWithGlovalVariables.end(), + std::pair(Scope, nullptr), + less_first())); + } + + iterator_range + findRetainedTypesForScope(const MDNode *Scope) const { + return make_range(std::equal_range( + ScopesWithRetainedTypes.begin(), ScopesWithRetainedTypes.end(), + std::pair(Scope, nullptr), + less_first())); + } + /// \brief A helper function to check whether the DIE for a given Scope is /// going to be null. bool isLexicalScopeDIENull(LexicalScope *Scope); Index: lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -455,8 +455,15 @@ // within the same scope, e.g. { namespace A = base; namespace B = A; } std::stable_sort(ScopesWithImportedEntities.begin(), ScopesWithImportedEntities.end(), less_first()); - for (auto *GV : CUNode->getGlobalVariables()) - CU.getOrCreateGlobalVariableDIE(GV); + for (auto *GV : CUNode->getGlobalVariables()) { + if (CU.getOrCreateContextDIE(GV->getScope())) + CU.getOrCreateGlobalVariableDIE(GV); + else + ScopesWithGlovalVariables.push_back(std::make_pair(GV->getScope(), GV)); + } + // Stable sort to preserve the order of appearance of global variables. + std::stable_sort(ScopesWithGlovalVariables.begin(), + ScopesWithGlovalVariables.end(), less_first()); for (auto *SP : CUNode->getSubprograms()) SPMap.insert(std::make_pair(SP, &CU)); for (auto *Ty : CUNode->getEnumTypes()) { @@ -467,8 +474,16 @@ for (auto *Ty : CUNode->getRetainedTypes()) { // The retained types array by design contains pointers to // MDNodes rather than DIRefs. Unique them here. - CU.getOrCreateTypeDIE(cast(resolve(Ty->getRef()))); + DIType *RT = cast(resolve(Ty->getRef())); + auto *Context = resolve(Ty->getScope()); + if (CU.getOrCreateContextDIE(Context)) + CU.getOrCreateTypeDIE(RT); + else + ScopesWithRetainedTypes.push_back(std::make_pair(Context, RT)); } + // Stable sort to preserve the order of appearance of retained types. + std::stable_sort(ScopesWithRetainedTypes.begin(), + ScopesWithRetainedTypes.end(), less_first()); // Emit imported_modules last so that the relevant context is already // available. for (auto *IE : CUNode->getImportedEntities()) Index: lib/CodeGen/AsmPrinter/DwarfUnit.h =================================================================== --- lib/CodeGen/AsmPrinter/DwarfUnit.h +++ lib/CodeGen/AsmPrinter/DwarfUnit.h @@ -304,7 +304,7 @@ bool Minimal = false); /// \brief Find existing DIE or create new DIE for the given type. - DIE *getOrCreateTypeDIE(const MDNode *N); + DIE *getOrCreateTypeDIE(const MDNode *N, DIE *ContextDIE = nullptr); /// \brief Get context owner's DIE. DIE *createTypeDIE(const DICompositeType *Ty); Index: lib/CodeGen/AsmPrinter/DwarfUnit.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -733,7 +733,7 @@ return &TyDIE; } -DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) { +DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode, DIE *ContextDIE) { if (!TyNode) return nullptr; @@ -741,19 +741,21 @@ assert(Ty == resolve(Ty->getRef()) && "type was not uniqued, possible ODR violation."); + if (DIE *TyDIE = getDIE(Ty)) + return TyDIE; + // DW_TAG_restrict_type is not supported in DWARF2 if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2) - return getOrCreateTypeDIE(resolve(cast(Ty)->getBaseType())); + return getOrCreateTypeDIE(resolve(cast(Ty)->getBaseType()), + ContextDIE); // Construct the context before querying for the existence of the DIE in case // such construction creates the DIE. auto *Context = resolve(Ty->getScope()); - DIE *ContextDIE = getOrCreateContextDIE(Context); + if (ContextDIE == nullptr) + ContextDIE = getOrCreateContextDIE(Context); assert(ContextDIE); - if (DIE *TyDIE = getDIE(Ty)) - return TyDIE; - // Create new type. DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty); Index: test/DebugInfo/LB-class.ll =================================================================== --- test/DebugInfo/LB-class.ll +++ test/DebugInfo/LB-class.ll @@ -0,0 +1,164 @@ +; RUN: llc -mtriple=x86_64-unknown-linux-gnu -filetype=obj -O0 < %s | llvm-dwarfdump -debug-dump=info - | FileCheck %s + +;; This test was generated by running following command: +;; clang -cc1 -O0 -g -emit-llvm class.cpp +;; Where class.cpp: +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;int foo(int x) { +;; if(x) { +;; class X { +;; public: +;; char z; +;; X(int y) : z(y) {} +;; }; +;; { +;; X a(x); +;; return a.z; +;; } +;; } +;; return 0; +;;} +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +; CHECK: DW_TAG_subprogram +; CHECK-NOT: DW_TAG +; CHECK: DW_AT_name {{.*}} "foo" +; CHECK-NOT: NULL +; CHECK: DW_TAG_lexical_block +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_TAG_class +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_AT_name {{.*}} "X" +; CHECK-NOT: {{NULL}} +; CHECK: DW_TAG_subprogram +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_AT_name {{.*}} "X" +; CHECK: NULL +; CHECK: NULL +; CHECK: DW_TAG_lexical_block +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_TAG_variable +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_AT_name {{.*}} "a" + + +%class.X = type { i8 } + +; Function Attrs: nounwind +define i32 @_Z3fooi(i32 %x) #0 { +entry: + %retval = alloca i32, align 4 + %x.addr = alloca i32, align 4 + %a = alloca %class.X, align 1 + store i32 %x, i32* %x.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %x.addr, metadata !24, metadata !25), !dbg !26 + %0 = load i32, i32* %x.addr, align 4, !dbg !27 + %tobool = icmp ne i32 %0, 0, !dbg !27 + br i1 %tobool, label %if.then, label %if.end, !dbg !28 + +if.then: ; preds = %entry + call void @llvm.dbg.declare(metadata %class.X* %a, metadata !29, metadata !25), !dbg !31 + %1 = load i32, i32* %x.addr, align 4, !dbg !31 + call void @_ZZ3fooiEN1XC1Ei(%class.X* %a, i32 %1), !dbg !31 + %z = getelementptr inbounds %class.X, %class.X* %a, i32 0, i32 0, !dbg !32 + %2 = load i8, i8* %z, align 1, !dbg !32 + %conv = sext i8 %2 to i32, !dbg !32 + store i32 %conv, i32* %retval, !dbg !32 + br label %return, !dbg !32 + +if.end: ; preds = %entry + store i32 0, i32* %retval, !dbg !33 + br label %return, !dbg !33 + +return: ; preds = %if.end, %if.then + %3 = load i32, i32* %retval, !dbg !34 + ret i32 %3, !dbg !34 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +; Function Attrs: nounwind +define internal void @_ZZ3fooiEN1XC1Ei(%class.X* %this, i32 %y) unnamed_addr #0 align 2 { +entry: + %this.addr = alloca %class.X*, align 8 + %y.addr = alloca i32, align 4 + store %class.X* %this, %class.X** %this.addr, align 8 + call void @llvm.dbg.declare(metadata %class.X** %this.addr, metadata !35, metadata !25), !dbg !37 + store i32 %y, i32* %y.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %y.addr, metadata !38, metadata !25), !dbg !39 + %this1 = load %class.X*, %class.X** %this.addr + %0 = load i32, i32* %y.addr, align 4, !dbg !39 + call void @_ZZ3fooiEN1XC2Ei(%class.X* %this1, i32 %0), !dbg !39 + ret void, !dbg !39 +} + +; Function Attrs: nounwind +define internal void @_ZZ3fooiEN1XC2Ei(%class.X* %this, i32 %y) unnamed_addr #0 align 2 { +entry: + %this.addr = alloca %class.X*, align 8 + %y.addr = alloca i32, align 4 + store %class.X* %this, %class.X** %this.addr, align 8 + call void @llvm.dbg.declare(metadata %class.X** %this.addr, metadata !40, metadata !25), !dbg !41 + store i32 %y, i32* %y.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %y.addr, metadata !42, metadata !25), !dbg !43 + %this1 = load %class.X*, %class.X** %this.addr + %z = getelementptr inbounds %class.X, %class.X* %this1, i32 0, i32 0, !dbg !43 + %0 = load i32, i32* %y.addr, align 4, !dbg !43 + %conv = trunc i32 %0 to i8, !dbg !43 + store i8 %conv, i8* %z, align 1, !dbg !43 + ret void, !dbg !43 +} + +attributes #0 = { nounwind } +attributes #1 = { nounwind readnone } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!21, !22} +!llvm.ident = !{!23} + +!0 = !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.7.0 (trunk 237245)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, retainedTypes: !3, subprograms: !18, globals: !2, imports: !2) +!1 = !DIFile(filename: "class.cpp", directory: "/") +!2 = !{} +!3 = !{!4} +!4 = !DICompositeType(tag: DW_TAG_class_type, name: "X", scope: !5, file: !1, line: 3, size: 8, align: 8, elements: !11) +!5 = distinct !DILexicalBlock(scope: !6, file: !1, line: 2) +!6 = distinct !DILexicalBlock(scope: !7, file: !1, line: 2) +!7 = !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, function: i32 (i32)* @_Z3fooi, variables: !2) +!8 = !DISubroutineType(types: !9) +!9 = !{!10, !10} +!10 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!11 = !{!12, !14} +!12 = !DIDerivedType(tag: DW_TAG_member, name: "z", scope: !4, file: !1, line: 5, baseType: !13, size: 8, align: 8, flags: DIFlagPublic) +!13 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char) +!14 = !DISubprogram(name: "X", scope: !4, file: !1, line: 6, type: !15, isLocal: false, isDefinition: false, scopeLine: 6, flags: DIFlagPublic | DIFlagPrototyped, isOptimized: false) +!15 = !DISubroutineType(types: !16) +!16 = !{null, !17, !10} +!17 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !4, size: 64, align: 64, flags: DIFlagArtificial | DIFlagObjectPointer) +!18 = !{!7, !19, !20} +!19 = !DISubprogram(name: "X", linkageName: "_ZZ3fooiEN1XC1Ei", scope: !4, file: !1, line: 6, type: !15, isLocal: true, isDefinition: true, scopeLine: 6, flags: DIFlagPrototyped, isOptimized: false, function: void (%class.X*, i32)* @_ZZ3fooiEN1XC1Ei, declaration: !14, variables: !2) +!20 = !DISubprogram(name: "X", linkageName: "_ZZ3fooiEN1XC2Ei", scope: !4, file: !1, line: 6, type: !15, isLocal: true, isDefinition: true, scopeLine: 6, flags: DIFlagPrototyped, isOptimized: false, function: void (%class.X*, i32)* @_ZZ3fooiEN1XC2Ei, declaration: !14, variables: !2) +!21 = !{i32 2, !"Dwarf Version", i32 4} +!22 = !{i32 2, !"Debug Info Version", i32 3} +!23 = !{!"clang version 3.7.0 (trunk 237245)"} +!24 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "x", arg: 1, scope: !7, file: !1, line: 1, type: !10) +!25 = !DIExpression() +!26 = !DILocation(line: 1, scope: !7) +!27 = !DILocation(line: 2, scope: !6) +!28 = !DILocation(line: 2, scope: !7) +!29 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "a", scope: !30, file: !1, line: 9, type: !4) +!30 = distinct !DILexicalBlock(scope: !5, file: !1, line: 8) +!31 = !DILocation(line: 9, scope: !30) +!32 = !DILocation(line: 10, scope: !30) +!33 = !DILocation(line: 13, scope: !7) +!34 = !DILocation(line: 14, scope: !7) +!35 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "this", arg: 1, scope: !19, type: !36, flags: DIFlagArtificial | DIFlagObjectPointer) +!36 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !4, size: 64, align: 64) +!37 = !DILocation(line: 0, scope: !19) +!38 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "y", arg: 2, scope: !19, file: !1, line: 6, type: !10) +!39 = !DILocation(line: 6, scope: !19) +!40 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "this", arg: 1, scope: !20, type: !36, flags: DIFlagArtificial | DIFlagObjectPointer) +!41 = !DILocation(line: 0, scope: !20) +!42 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "y", arg: 2, scope: !20, file: !1, line: 6, type: !10) +!43 = !DILocation(line: 6, scope: !20) Index: test/DebugInfo/LB-static.ll =================================================================== --- test/DebugInfo/LB-static.ll +++ test/DebugInfo/LB-static.ll @@ -0,0 +1,106 @@ +; RUN: llc -mtriple=x86_64-unknown-linux-gnu -filetype=obj -O0 < %s | llvm-dwarfdump -debug-dump=info - | FileCheck %s + +;; This test was generated by running following command: +;; clang -cc1 -O0 -g -emit-llvm static.cpp +;; Where static.cpp: +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;int foo(int x) { +;; if(x) { +;; static int bar = 0; +;; { +;; int a = bar++; +;; return a; +;; } +;; } +;; return 0; +;;} +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +; CHECK: DW_TAG_subprogram +; CHECK-NOT: DW_TAG +; CHECK: DW_AT_name {{.*}} "foo" +; CHECK-NOT: NULL +; CHECK: DW_TAG_lexical_block +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_TAG_variable +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_AT_name {{.*}} "bar" +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_TAG_lexical_block +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_TAG_variable +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_AT_name {{.*}} "a" +; CHECK: NULL + + +@_ZZ3fooiE3bar = internal global i32 0, align 4 + +; Function Attrs: nounwind +define i32 @_Z3fooi(i32 %x) #0 { +entry: + %retval = alloca i32, align 4 + %x.addr = alloca i32, align 4 + %a = alloca i32, align 4 + store i32 %x, i32* %x.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %x.addr, metadata !15, metadata !16), !dbg !17 + %0 = load i32, i32* %x.addr, align 4, !dbg !18 + %tobool = icmp ne i32 %0, 0, !dbg !18 + br i1 %tobool, label %if.then, label %if.end, !dbg !19 + +if.then: ; preds = %entry + call void @llvm.dbg.declare(metadata i32* %a, metadata !20, metadata !16), !dbg !22 + %1 = load i32, i32* @_ZZ3fooiE3bar, align 4, !dbg !22 + %inc = add nsw i32 %1, 1, !dbg !22 + store i32 %inc, i32* @_ZZ3fooiE3bar, align 4, !dbg !22 + store i32 %1, i32* %a, align 4, !dbg !22 + %2 = load i32, i32* %a, align 4, !dbg !23 + store i32 %2, i32* %retval, !dbg !23 + br label %return, !dbg !23 + +if.end: ; preds = %entry + store i32 0, i32* %retval, !dbg !24 + br label %return, !dbg !24 + +return: ; preds = %if.end, %if.then + %3 = load i32, i32* %retval, !dbg !25 + ret i32 %3, !dbg !25 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { nounwind } +attributes #1 = { nounwind readnone } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!12, !13} +!llvm.ident = !{!14} + +!0 = !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.7.0 (trunk 237245)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, retainedTypes: !2, subprograms: !3, globals: !8, imports: !2) +!1 = !DIFile(filename: "static.cpp", directory: "/") +!2 = !{} +!3 = !{!4} +!4 = !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, function: i32 (i32)* @_Z3fooi, variables: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{!7, !7} +!7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!8 = !{!9} +!9 = !DIGlobalVariable(name: "bar", scope: !10, file: !1, line: 3, type: !7, isLocal: true, isDefinition: true, variable: i32* @_ZZ3fooiE3bar) +!10 = distinct !DILexicalBlock(scope: !11, file: !1, line: 2) +!11 = distinct !DILexicalBlock(scope: !4, file: !1, line: 2) +!12 = !{i32 2, !"Dwarf Version", i32 4} +!13 = !{i32 2, !"Debug Info Version", i32 3} +!14 = !{!"clang version 3.7.0 (trunk 237245)"} +!15 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "x", arg: 1, scope: !4, file: !1, line: 1, type: !7) +!16 = !DIExpression() +!17 = !DILocation(line: 1, scope: !4) +!18 = !DILocation(line: 2, scope: !11) +!19 = !DILocation(line: 2, scope: !4) +!20 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "a", scope: !21, file: !1, line: 5, type: !7) +!21 = distinct !DILexicalBlock(scope: !10, file: !1, line: 4) +!22 = !DILocation(line: 5, scope: !21) +!23 = !DILocation(line: 6, scope: !21) +!24 = !DILocation(line: 9, scope: !4) +!25 = !DILocation(line: 10, scope: !4) Index: test/DebugInfo/LB-typedef.ll =================================================================== --- test/DebugInfo/LB-typedef.ll +++ test/DebugInfo/LB-typedef.ll @@ -0,0 +1,105 @@ +; RUN: llc -mtriple=x86_64-unknown-linux-gnu -filetype=obj -O0 < %s | llvm-dwarfdump -debug-dump=info - | FileCheck %s + +;; This test was generated by running following command: +;; clang -cc1 -O0 -g -emit-llvm typedef.cpp +;; Where typedef.cpp +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;int foo(int x) { +;; if(x) { +;; typedef char X; +;; { +;; X a = x; +;; return a; +;; } +;; } +;; return 0; +;;} +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +; CHECK: DW_TAG_subprogram +; CHECK-NOT: DW_TAG +; CHECK: DW_AT_name {{.*}} "foo" +; CHECK-NOT: NULL +; CHECK: DW_TAG_lexical_block +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_TAG_typedef +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_AT_name {{.*}} "X" +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_TAG_lexical_block +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_TAG_variable +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_AT_name {{.*}} "a" +; CHECK: NULL + + +; Function Attrs: nounwind +define i32 @_Z3fooi(i32 %x) #0 { +entry: + %retval = alloca i32, align 4 + %x.addr = alloca i32, align 4 + %a = alloca i8, align 1 + store i32 %x, i32* %x.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %x.addr, metadata !16, metadata !17), !dbg !18 + %0 = load i32, i32* %x.addr, align 4, !dbg !19 + %tobool = icmp ne i32 %0, 0, !dbg !19 + br i1 %tobool, label %if.then, label %if.end, !dbg !20 + +if.then: ; preds = %entry + call void @llvm.dbg.declare(metadata i8* %a, metadata !21, metadata !17), !dbg !23 + %1 = load i32, i32* %x.addr, align 4, !dbg !23 + %conv = trunc i32 %1 to i8, !dbg !23 + store i8 %conv, i8* %a, align 1, !dbg !23 + %2 = load i8, i8* %a, align 1, !dbg !24 + %conv1 = sext i8 %2 to i32, !dbg !24 + store i32 %conv1, i32* %retval, !dbg !24 + br label %return, !dbg !24 + +if.end: ; preds = %entry + store i32 0, i32* %retval, !dbg !25 + br label %return, !dbg !25 + +return: ; preds = %if.end, %if.then + %3 = load i32, i32* %retval, !dbg !26 + ret i32 %3, !dbg !26 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { nounwind } +attributes #1 = { nounwind readnone } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!13, !14} +!llvm.ident = !{!15} + +!0 = !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.7.0 (trunk 237245)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, retainedTypes: !3, subprograms: !12, globals: !2, imports: !2) +!1 = !DIFile(filename: "typedef.cpp", directory: "/") +!2 = !{} +!3 = !{!4} +!4 = !DIDerivedType(tag: DW_TAG_typedef, name: "X", scope: !5, file: !1, line: 3, baseType: !11) +!5 = distinct !DILexicalBlock(scope: !6, file: !1, line: 2) +!6 = distinct !DILexicalBlock(scope: !7, file: !1, line: 2) +!7 = !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, function: i32 (i32)* @_Z3fooi, variables: !2) +!8 = !DISubroutineType(types: !9) +!9 = !{!10, !10} +!10 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!11 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char) +!12 = !{!7} +!13 = !{i32 2, !"Dwarf Version", i32 4} +!14 = !{i32 2, !"Debug Info Version", i32 3} +!15 = !{!"clang version 3.7.0 (trunk 237245)"} +!16 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "x", arg: 1, scope: !7, file: !1, line: 1, type: !10) +!17 = !DIExpression() +!18 = !DILocation(line: 1, scope: !7) +!19 = !DILocation(line: 2, scope: !6) +!20 = !DILocation(line: 2, scope: !7) +!21 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "a", scope: !22, file: !1, line: 5, type: !4) +!22 = distinct !DILexicalBlock(scope: !5, file: !1, line: 4) +!23 = !DILocation(line: 5, scope: !22) +!24 = !DILocation(line: 6, scope: !22) +!25 = !DILocation(line: 9, scope: !7) +!26 = !DILocation(line: 10, scope: !7) Index: test/DebugInfo/useless_lexical_scope.ll =================================================================== --- test/DebugInfo/useless_lexical_scope.ll +++ test/DebugInfo/useless_lexical_scope.ll @@ -0,0 +1,112 @@ +; RUN: llc -mtriple=x86_64-unknown-linux-gnu -filetype=obj -O0 < %s | llvm-dwarfdump -debug-dump=info - | FileCheck %s + +;; This test was generated by running following command: +;; clang -cc1 -O0 -g -emit-llvm foo.cpp +;; Where foo.cpp +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;int foo(int X) +;;{ +;; if (X) +;; { +;; if (X % 2 == 0) +;; { +;; int Y = X++; +;; return Y; +;; } +;; } +;; return 0; +;;} +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +; CHECK: DW_TAG_subprogram +; CHECK-NOT: DW_TAG +; CHECK: DW_AT_name {{.*}} "foo" +; CHECK-NOT: NULL +; CHECK: DW_TAG_lexical_block +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_TAG_variable +; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_AT_name {{.*}} "Y" +; CHECK-NOT: {{DW_TAG}} +; CHECK: NULL + + +; Function Attrs: nounwind +define i32 @_Z3fooi(i32 %X) #0 { +entry: + %retval = alloca i32, align 4 + %X.addr = alloca i32, align 4 + %Y = alloca i32, align 4 + store i32 %X, i32* %X.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %X.addr, metadata !11, metadata !12), !dbg !13 + %0 = load i32, i32* %X.addr, align 4, !dbg !14 + %tobool = icmp ne i32 %0, 0, !dbg !14 + br i1 %tobool, label %if.then, label %if.end.2, !dbg !16 + +if.then: ; preds = %entry + %1 = load i32, i32* %X.addr, align 4, !dbg !17 + %rem = srem i32 %1, 2, !dbg !17 + %cmp = icmp eq i32 %rem, 0, !dbg !17 + br i1 %cmp, label %if.then.1, label %if.end, !dbg !20 + +if.then.1: ; preds = %if.then + call void @llvm.dbg.declare(metadata i32* %Y, metadata !21, metadata !12), !dbg !23 + %2 = load i32, i32* %X.addr, align 4, !dbg !23 + %inc = add nsw i32 %2, 1, !dbg !23 + store i32 %inc, i32* %X.addr, align 4, !dbg !23 + store i32 %2, i32* %Y, align 4, !dbg !23 + %3 = load i32, i32* %Y, align 4, !dbg !24 + store i32 %3, i32* %retval, !dbg !24 + br label %return, !dbg !24 + +if.end: ; preds = %if.then + br label %if.end.2, !dbg !25 + +if.end.2: ; preds = %if.end, %entry + store i32 0, i32* %retval, !dbg !26 + br label %return, !dbg !26 + +return: ; preds = %if.end.2, %if.then.1 + %4 = load i32, i32* %retval, !dbg !27 + ret i32 %4, !dbg !27 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { nounwind } +attributes #1 = { nounwind readnone } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!8, !9} +!llvm.ident = !{!10} + +!0 = !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.7.0 (trunk 237245)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, retainedTypes: !2, subprograms: !3, globals: !2, imports: !2) +!1 = !DIFile(filename: "foo.cpp", directory: "/") +!2 = !{} +!3 = !{!4} +!4 = !DISubprogram(name: "foo", linkageName: "_Z3fooi", scope: !1, file: !1, line: 1, type: !5, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: false, function: i32 (i32)* @_Z3fooi, variables: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{!7, !7} +!7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!8 = !{i32 2, !"Dwarf Version", i32 4} +!9 = !{i32 2, !"Debug Info Version", i32 3} +!10 = !{!"clang version 3.7.0 (trunk 237245)"} +!11 = !DILocalVariable(tag: DW_TAG_arg_variable, name: "X", arg: 1, scope: !4, file: !1, line: 1, type: !7) +!12 = !DIExpression() +!13 = !DILocation(line: 1, scope: !4) +!14 = !DILocation(line: 3, scope: !15) +!15 = distinct !DILexicalBlock(scope: !4, file: !1, line: 3) +!16 = !DILocation(line: 3, scope: !4) +!17 = !DILocation(line: 5, scope: !18) +!18 = distinct !DILexicalBlock(scope: !19, file: !1, line: 5) +!19 = distinct !DILexicalBlock(scope: !15, file: !1, line: 4) +!20 = !DILocation(line: 5, scope: !19) +!21 = !DILocalVariable(tag: DW_TAG_auto_variable, name: "Y", scope: !22, file: !1, line: 7, type: !7) +!22 = distinct !DILexicalBlock(scope: !18, file: !1, line: 6) +!23 = !DILocation(line: 7, scope: !22) +!24 = !DILocation(line: 8, scope: !22) +!25 = !DILocation(line: 10, scope: !19) +!26 = !DILocation(line: 11, scope: !4) +!27 = !DILocation(line: 12, scope: !4)