Index: lib/CodeGen/AsmPrinter/DwarfExpression.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfExpression.cpp +++ lib/CodeGen/AsmPrinter/DwarfExpression.cpp @@ -337,6 +337,7 @@ break; case dwarf::DW_OP_plus: case dwarf::DW_OP_minus: + case dwarf::DW_OP_mul: emitOp(Op->getOp()); break; case dwarf::DW_OP_deref: { Index: lib/IR/DebugInfoMetadata.cpp =================================================================== --- lib/IR/DebugInfoMetadata.cpp +++ lib/IR/DebugInfoMetadata.cpp @@ -643,6 +643,7 @@ case dwarf::DW_OP_plus_uconst: case dwarf::DW_OP_plus: case dwarf::DW_OP_minus: + case dwarf::DW_OP_mul: case dwarf::DW_OP_deref: case dwarf::DW_OP_xderef: break; Index: lib/Transforms/IPO/GlobalOpt.cpp =================================================================== --- lib/Transforms/IPO/GlobalOpt.cpp +++ lib/Transforms/IPO/GlobalOpt.cpp @@ -37,6 +37,7 @@ #include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" #include "llvm/IR/ValueHandle.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/Pass.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" @@ -1602,12 +1603,47 @@ assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) && "No reason to shrink to bool!"); + SmallVector GVs; + GV->getDebugInfo(GVs); + // If initialized to zero and storing one into the global, we can use a cast // instead of a select to synthesize the desired value. bool IsOneZero = false; - if (ConstantInt *CI = dyn_cast(OtherVal)) + if (ConstantInt *CI = dyn_cast(OtherVal)){ IsOneZero = InitVal->isNullValue() && CI->isOne(); + ConstantInt *CIInit = dyn_cast(GV->getInitializer()); + uint64_t ValInit = CIInit->getZExtValue(); + uint64_t ValOther = CI->getZExtValue(); + uint64_t ValMinus = ValOther - ValInit; + + for(auto *GVe : GVs){ + DIGlobalVariable *DGV = GVe->getVariable(); + DIExpression *E = GVe->getExpression(); + + // val * (ValOther - ValInit) + ValInit: + // DW_OP_deref DW_OP_constu + // DW_OP_mul DW_OP_constu DW_OP_plus DW_OP_stack_value + E = DIExpression::get(NewGV->getContext(), + {dwarf::DW_OP_deref, + dwarf::DW_OP_constu, + ValMinus, + dwarf::DW_OP_mul, + dwarf::DW_OP_constu, + ValInit, + dwarf::DW_OP_plus, + dwarf::DW_OP_stack_value}); + DIGlobalVariableExpression *DGVE = + DIGlobalVariableExpression::get(NewGV->getContext(), DGV, E); + NewGV->addDebugInfo(DGVE); + } + } else { + // FIXME: This will only emit address for debugger on which will + // be written only 0 or 1. + for(auto *GV : GVs) + NewGV->addDebugInfo(GV); + } + while (!GV->use_empty()) { Instruction *UI = cast(GV->user_back()); if (StoreInst *SI = dyn_cast(UI)) { Index: test/Transforms/GlobalOpt/integer-bool-dwarf.ll =================================================================== --- /dev/null +++ test/Transforms/GlobalOpt/integer-bool-dwarf.ll @@ -0,0 +1,88 @@ +;RUN: opt -globalopt -f %s | llc -filetype=obj -o -| llvm-dwarfdump - | FileCheck %s + +source_filename = "integer-bool-dwarf.c" +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 void @set1() #0 !dbg !11 { +entry: + store i32 111, i32* @foo, align 4, !dbg !14 + ret void, !dbg !15 +} + +; Function Attrs: noinline nounwind optnone uwtable +define void @set2() #0 !dbg !16 { +entry: + store i32 0, i32* @foo, align 4, !dbg !17 + ret void, !dbg !18 +} + +; Function Attrs: noinline nounwind optnone uwtable +define i32 @get() #0 !dbg !19 { +entry: + %A = alloca i32, align 4 + %C = alloca i32, align 4 + call void @llvm.dbg.declare(metadata i32* %A, metadata !22, metadata !23), !dbg !24 + %0 = load i32, i32* @foo, align 4, !dbg !25 + store i32 %0, i32* %A, align 4, !dbg !24 + call void @llvm.dbg.declare(metadata i32* %C, metadata !26, metadata !23), !dbg !27 + %1 = load i32, i32* %A, align 4, !dbg !28 + %cmp = icmp slt i32 %1, 2, !dbg !29 + %conv = zext i1 %cmp to i32, !dbg !29 + store i32 %conv, i32* %C, align 4, !dbg !27 + %2 = load i32, i32* %C, align 4, !dbg !30 + ret i32 %2, !dbg !31 +} + +; Function Attrs: nounwind readnone speculatable +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +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 = !{!7, !8, !9} +!llvm.ident = !{!10} + +!0 = !DIGlobalVariableExpression(var: !1) +!1 = distinct !DIGlobalVariable(name: "foo", scope: !2, file: !3, line: 1, type: !6, 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 05db9a628f6f6c8a1a077165b386350690831bbf)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5) +!3 = !DIFile(filename: "integer-bool-dwarf.c", directory: "/home/rtrk/Programs/git_llvm-4.0/test/Transforms/GlobalOpt") +!4 = !{} +!5 = !{!0} +!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!7 = !{i32 2, !"Dwarf Version", i32 4} +!8 = !{i32 2, !"Debug Info Version", i32 3} +!9 = !{i32 1, !"wchar_size", i32 4} +!10 = !{!"clang version 5.0.0 (http://llvm.org/git/clang.git b8d6b711721ae52eb05d4d39837e494c07e8b78f) (http://llvm.org/git/llvm.git 05db9a628f6f6c8a1a077165b386350690831bbf)"} +!11 = distinct !DISubprogram(name: "set1", scope: !3, file: !3, line: 3, type: !12, isLocal: false, isDefinition: true, scopeLine: 4, isOptimized: false, unit: !2, variables: !4) +!12 = !DISubroutineType(types: !13) +!13 = !{null} +!14 = !DILocation(line: 5, column: 13, scope: !11) +!15 = !DILocation(line: 6, column: 1, scope: !11) +!16 = distinct !DISubprogram(name: "set2", scope: !3, file: !3, line: 8, type: !12, isLocal: false, isDefinition: true, scopeLine: 9, isOptimized: false, unit: !2, variables: !4) +!17 = !DILocation(line: 10, column: 13, scope: !16) +!18 = !DILocation(line: 11, column: 1, scope: !16) +!19 = distinct !DISubprogram(name: "get", scope: !3, file: !3, line: 13, type: !20, isLocal: false, isDefinition: true, scopeLine: 14, isOptimized: false, unit: !2, variables: !4) +!20 = !DISubroutineType(types: !21) +!21 = !{!6} +!22 = !DILocalVariable(name: "A", scope: !19, file: !3, line: 15, type: !6) +!23 = !DIExpression() +!24 = !DILocation(line: 15, column: 13, scope: !19) +!25 = !DILocation(line: 15, column: 17, scope: !19) +!26 = !DILocalVariable(name: "C", scope: !19, file: !3, line: 16, type: !6) +!27 = !DILocation(line: 16, column: 13, scope: !19) +!28 = !DILocation(line: 16, column: 18, scope: !19) +!29 = !DILocation(line: 16, column: 19, scope: !19) +!30 = !DILocation(line: 17, column: 16, scope: !19) +!31 = !DILocation(line: 17, column: 9, scope: !19) + +;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 [DW_FORM_exprloc] (<0xe> 03 {{.*}} 06 10 6f 1e 9f ) +; DW_OP_addr DW_OP_deref DW_OP_constu <111> DW_OP_mul DW_OP_stack_value