diff options
| -rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp | 1 | ||||
| -rw-r--r-- | llvm/lib/IR/DebugInfoMetadata.cpp | 1 | ||||
| -rw-r--r-- | llvm/lib/Transforms/IPO/GlobalOpt.cpp | 48 | ||||
| -rw-r--r-- | llvm/test/Transforms/GlobalOpt/integer-bool-dwarf.ll | 57 | ||||
| -rw-r--r-- | llvm/test/Transforms/GlobalOpt/shrink-address-to-bool.ll | 46 | 
5 files changed, 152 insertions, 1 deletions
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp index 4538bb3ff02..429269d36d8 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp @@ -338,6 +338,7 @@ void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor,        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: diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp b/llvm/lib/IR/DebugInfoMetadata.cpp index 1ef43e60b54..645bba1652d 100644 --- a/llvm/lib/IR/DebugInfoMetadata.cpp +++ b/llvm/lib/IR/DebugInfoMetadata.cpp @@ -644,6 +644,7 @@ bool DIExpression::isValid() const {      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; diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp index ee8fdaebbda..e31bbc7fe57 100644 --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -36,6 +36,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" @@ -1603,12 +1604,57 @@ static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {    assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&           "No reason to shrink to bool!"); +  SmallVector<DIGlobalVariableExpression *, 1> 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<ConstantInt>(OtherVal)) +  bool EmitOneOrZero = true; +  if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)){      IsOneZero = InitVal->isNullValue() && CI->isOne(); +    if (ConstantInt *CIInit = dyn_cast<ConstantInt>(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(); + +        // It is expected that the address of global optimized variable is on +        // top of the stack. After optimization, value of that variable will +        // be ether 0 for initial value or 1 for other value. The following +        // expression should return constant integer value depending on the +        // value at global object address: +        // val * (ValOther - ValInit) + ValInit: +        // DW_OP_deref DW_OP_constu <ValMinus> +        // DW_OP_mul DW_OP_constu <ValInit> 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); +     } +     EmitOneOrZero = false; +    } +  } + +  if (EmitOneOrZero) { +     // 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<Instruction>(GV->user_back());      if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { diff --git a/llvm/test/Transforms/GlobalOpt/integer-bool-dwarf.ll b/llvm/test/Transforms/GlobalOpt/integer-bool-dwarf.ll new file mode 100644 index 00000000000..5ee98977f77 --- /dev/null +++ b/llvm/test/Transforms/GlobalOpt/integer-bool-dwarf.ll @@ -0,0 +1,57 @@ +;RUN: opt -S -globalopt -f %s | FileCheck %s + +;CHECK: !0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression(DW_OP_deref, DW_OP_constu, 111, DW_OP_mul, DW_OP_constu, 0, DW_OP_plus, DW_OP_stack_value)) + +@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: +  %0 = load i32, i32* @foo, align 4, !dbg !22 +  ret i32 %0, !dbg !23 +} + +attributes #0 = { noinline nounwind optnone uwtable } + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!7, !8, !9} +!llvm.ident = !{!10} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!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 6.0.0 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5) +!3 = !DIFile(filename: "integer-bool-dwarf.c", directory: "/") +!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 6.0.0 "} +!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: 7, 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: 7, 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 = !DILocation(line: 15, column: 10, scope: !19) +!23 = !DILocation(line: 15, column: 3, scope: !19) diff --git a/llvm/test/Transforms/GlobalOpt/shrink-address-to-bool.ll b/llvm/test/Transforms/GlobalOpt/shrink-address-to-bool.ll new file mode 100644 index 00000000000..c4e4b98faa5 --- /dev/null +++ b/llvm/test/Transforms/GlobalOpt/shrink-address-to-bool.ll @@ -0,0 +1,46 @@ +;RUN: opt -S -globalopt -f %s | FileCheck %s + +;CHECK: @foo = {{.*}}, !dbg !0 +@foo = global i64 ptrtoint ([1 x i64]* @baa to i64), align 8, !dbg !0 +@baa = common global [1 x i64] zeroinitializer, align 8, !dbg !6 + +; Function Attrs: noinline nounwind optnone uwtable +define void @fun() #0 !dbg !16 { +entry: +  %0 = load i64, i64* @foo, align 8, !dbg !19 +  %1 = inttoptr i64 %0 to i64*, !dbg !19 +  %cmp = icmp ugt i64* getelementptr inbounds ([1 x i64], [1 x i64]* @baa, i32 0, i32 0), %1, !dbg !20 +  %conv = zext i1 %cmp to i32, !dbg !20 +  store i64 0, i64* @foo, align 8, !dbg !21 +  ret void, !dbg !22 +} + +attributes #0 = { noinline nounwind optnone uwtable } + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!12, !13, !14} +!llvm.ident = !{!15} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "foo", scope: !2, file: !3, line: 2, type: !9, isLocal: false, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 6.0.0 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5) +!3 = !DIFile(filename: "shrink-address-to-bool.c", directory: "/") +!4 = !{} +!5 = !{!0, !6} +!6 = !DIGlobalVariableExpression(var: !7, expr: !DIExpression()) +!7 = distinct !DIGlobalVariable(name: "baa", scope: !2, file: !3, line: 1, type: !8, isLocal: false, isDefinition: true) +!8 = !DICompositeType(tag: DW_TAG_array_type, baseType: !9, size: 64, elements: !10) +!9 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed) +!10 = !{!11} +!11 = !DISubrange(count: 1) +!12 = !{i32 2, !"Dwarf Version", i32 4} +!13 = !{i32 2, !"Debug Info Version", i32 3} +!14 = !{i32 1, !"wchar_size", i32 4} +!15 = !{!"clang version 6.0.0 "} +!16 = distinct !DISubprogram(name: "fun", scope: !3, file: !3, line: 4, type: !17, isLocal: false, isDefinition: true, scopeLine: 4, isOptimized: false, unit: !2, variables: !4) +!17 = !DISubroutineType(types: !18) +!18 = !{null} +!19 = !DILocation(line: 5, column: 9, scope: !16) +!20 = !DILocation(line: 5, column: 7, scope: !16) +!21 = !DILocation(line: 6, column: 7, scope: !16) +!22 = !DILocation(line: 7, column: 1, scope: !16)  | 

