Index: lib/Transforms/IPO/GlobalOpt.cpp =================================================================== --- lib/Transforms/IPO/GlobalOpt.cpp +++ lib/Transforms/IPO/GlobalOpt.cpp @@ -1570,6 +1570,12 @@ GV->getThreadLocalMode(), GV->getType()->getAddressSpace()); NewGV->copyAttributesFrom(GV); + + SmallVector GVs; + GV->getDebugInfo(GVs); + for(auto *GV : GVs) + NewGV->addDebugInfo(GV); + GV->getParent()->getGlobalList().insert(GV->getIterator(), NewGV); Constant *InitVal = GV->getInitializer(); Index: test/Transforms/GlobalOpt/static-global-boolean-dwarf.ll =================================================================== --- /dev/null +++ test/Transforms/GlobalOpt/static-global-boolean-dwarf.ll @@ -0,0 +1,132 @@ +; RUN: opt -f %s -globalopt -o %t +; llvm-dwarfdump %t | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@foo = internal global i32 0, align 4, !dbg !0 + +; Function Attrs: noinline nounwind optnone uwtable +define i32 @get_foo() #0 !dbg !15 { +entry: + %0 = load i32, i32* @foo, align 4, !dbg !19 + ret i32 %0, !dbg !20 +} + +; Function Attrs: noinline nounwind optnone uwtable +define void @init_boolean(i32 %arg) #0 !dbg !21 { +entry: + %arg.addr = alloca i32, align 4 + store i32 %arg, i32* %arg.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %arg.addr, metadata !25, metadata !26), !dbg !27 + %0 = load i32, i32* %arg.addr, align 4, !dbg !28 + %cmp = icmp eq i32 %0, 1, !dbg !30 + br i1 %cmp, label %if.then, label %if.else, !dbg !31 + +if.then: ; preds = %entry + store i32 1, i32* @foo, align 4, !dbg !32 + %1 = load i32, i32* %arg.addr, align 4, !dbg !34 + %inc = add nsw i32 %1, 1, !dbg !34 + store i32 %inc, i32* %arg.addr, align 4, !dbg !34 + br label %if.end, !dbg !35 + +if.else: ; preds = %entry + store i32 0, i32* @foo, align 4, !dbg !36 + %2 = load i32, i32* %arg.addr, align 4, !dbg !38 + %dec = add nsw i32 %2, -1, !dbg !38 + store i32 %dec, i32* %arg.addr, align 4, !dbg !38 + br label %if.end + +if.end: ; preds = %if.else, %if.then + ret void, !dbg !39 +} + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +; Function Attrs: noinline nounwind optnone uwtable +define i32 @main(i32 %argc, i8** %argv) #0 !dbg !40 { +entry: + %retval = alloca i32, align 4 + %argc.addr = alloca i32, align 4 + %argv.addr = alloca i8**, align 8 + store i32 0, i32* %retval, align 4 + store i32 %argc, i32* %argc.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %argc.addr, metadata !46, metadata !26), !dbg !47 + store i8** %argv, i8*** %argv.addr, align 8 + call void @llvm.dbg.declare(metadata i8*** %argv.addr, metadata !48, metadata !26), !dbg !49 + %0 = load i32, i32* %argc.addr, align 4, !dbg !50 + %cmp = icmp eq i32 %0, 2, !dbg !51 + %conv = zext i1 %cmp to i32, !dbg !51 + call void @init_boolean(i32 %conv), !dbg !52 + ret i32 0, !dbg !53 +} + +attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone speculatable } + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!11, !12, !13} +!llvm.ident = !{!14} + +!0 = !DIGlobalVariableExpression(var: !1) +!1 = distinct !DIGlobalVariable(name: "foo", scope: !2, file: !3, line: 9, type: !10, isLocal: true, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 5.0.0 (http://llvm.org/git/clang.git b8d6b711721ae52eb05d4d39837e494c07e8b78f) (http://llvm.org/git/llvm.git c5512b8ed3ebeea87dffa4a49d2b339b29391ceb)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !9) +!3 = !DIFile(filename: "static-global-boolean-dwarf.c", directory: "/home/rtrk/Programs/git_llvm-4.0/build") +!4 = !{!5} +!5 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "myboolean", file: !3, line: 5, size: 32, elements: !6) +!6 = !{!7, !8} +!7 = !DIEnumerator(name: "TRUE", value: 0) +!8 = !DIEnumerator(name: "FALSE", value: 1) +!9 = !{!0} +!10 = !DIDerivedType(tag: DW_TAG_typedef, name: "boolean", file: !3, line: 6, baseType: !5) +!11 = !{i32 2, !"Dwarf Version", i32 4} +!12 = !{i32 2, !"Debug Info Version", i32 3} +!13 = !{i32 1, !"wchar_size", i32 4} +!14 = !{!"clang version 5.0.0 (http://llvm.org/git/clang.git b8d6b711721ae52eb05d4d39837e494c07e8b78f) (http://llvm.org/git/llvm.git c5512b8ed3ebeea87dffa4a49d2b339b29391ceb)"} +!15 = distinct !DISubprogram(name: "get_foo", scope: !3, file: !3, line: 11, type: !16, isLocal: false, isDefinition: true, scopeLine: 11, flags: DIFlagPrototyped, isOptimized: false, unit: !2, variables: !18) +!16 = !DISubroutineType(types: !17) +!17 = !{!10} +!18 = !{} +!19 = !DILocation(line: 11, column: 32, scope: !15) +!20 = !DILocation(line: 11, column: 24, scope: !15) +!21 = distinct !DISubprogram(name: "init_boolean", scope: !3, file: !3, line: 14, type: !22, isLocal: false, isDefinition: true, scopeLine: 14, flags: DIFlagPrototyped, isOptimized: false, unit: !2, variables: !18) +!22 = !DISubroutineType(types: !23) +!23 = !{null, !24} +!24 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!25 = !DILocalVariable(name: "arg", arg: 1, scope: !21, file: !3, line: 14, type: !24) +!26 = !DIExpression() +!27 = !DILocation(line: 14, column: 18, scope: !21) +!28 = !DILocation(line: 15, column: 6, scope: !29) +!29 = distinct !DILexicalBlock(scope: !21, file: !3, line: 15, column: 6) +!30 = !DILocation(line: 15, column: 10, scope: !29) +!31 = !DILocation(line: 15, column: 6, scope: !21) +!32 = !DILocation(line: 16, column: 9, scope: !33) +!33 = distinct !DILexicalBlock(scope: !29, file: !3, line: 15, column: 15) +!34 = !DILocation(line: 17, column: 8, scope: !33) +!35 = !DILocation(line: 18, column: 3, scope: !33) +!36 = !DILocation(line: 19, column: 9, scope: !37) +!37 = distinct !DILexicalBlock(scope: !29, file: !3, line: 18, column: 10) +!38 = !DILocation(line: 20, column: 8, scope: !37) +!39 = !DILocation(line: 22, column: 1, scope: !21) +!40 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 25, type: !41, isLocal: false, isDefinition: true, scopeLine: 26, flags: DIFlagPrototyped, isOptimized: false, unit: !2, variables: !18) +!41 = !DISubroutineType(types: !42) +!42 = !{!24, !24, !43} +!43 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !44, size: 64) +!44 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !45, size: 64) +!45 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char) +!46 = !DILocalVariable(name: "argc", arg: 1, scope: !40, file: !3, line: 25, type: !24) +!47 = !DILocation(line: 25, column: 14, scope: !40) +!48 = !DILocalVariable(name: "argv", arg: 2, scope: !40, file: !3, line: 25, type: !43) +!49 = !DILocation(line: 25, column: 26, scope: !40) +!50 = !DILocation(line: 27, column: 16, scope: !40) +!51 = !DILocation(line: 27, column: 21, scope: !40) +!52 = !DILocation(line: 27, column: 3, scope: !40) +!53 = !DILocation(line: 28, column: 3, scope: !40) + + +; CHECK: DW_AT_name [DW_FORM_strp] {{.*}} "foo" +; CHECK-NEXT: DW_AT_type +; CHECK-NEXT: DW_AT_decl_file +; CHECK-NEXT: DW_AT_decl_line +; CHECK-NEXT: DW_AT_location