From 210a29de7bcc6dcf73fec98efe38e2e1fac83c50 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Fri, 27 Apr 2018 21:41:36 +0000 Subject: [PATCH] Fix a bug in GlobalOpt's handling of DIExpressions. This patch adds support for fragment expressions TryToShrinkGlobalToBoolean() which were previously just dropped. Thanks to Reid Kleckner for providing me a reproducer! llvm-svn: 331086 --- llvm/include/llvm/IR/DebugInfoMetadata.h | 6 +++--- llvm/lib/IR/DebugInfoMetadata.cpp | 8 ++++---- llvm/lib/Transforms/IPO/GlobalOpt.cpp | 14 +++++--------- llvm/lib/Transforms/Utils/Local.cpp | 4 ++-- llvm/test/Transforms/GlobalOpt/integer-bool-dwarf.ll | 8 ++++++-- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h index 749d475..fea3f5a 100644 --- a/llvm/include/llvm/IR/DebugInfoMetadata.h +++ b/llvm/include/llvm/IR/DebugInfoMetadata.h @@ -2402,9 +2402,9 @@ public: /// Prepend \p DIExpr with the given opcodes and optionally turn it into a /// stack value. - static DIExpression *doPrepend(const DIExpression *DIExpr, - SmallVectorImpl &Ops, - bool StackValue = false); + static DIExpression *prependOpcodes(const DIExpression *DIExpr, + SmallVectorImpl &Ops, + bool StackValue = false); /// Create a DIExpression to describe one part of an aggregate variable that /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp b/llvm/lib/IR/DebugInfoMetadata.cpp index fe57aa7..f1ad72d 100644 --- a/llvm/lib/IR/DebugInfoMetadata.cpp +++ b/llvm/lib/IR/DebugInfoMetadata.cpp @@ -785,12 +785,12 @@ DIExpression *DIExpression::prepend(const DIExpression *Expr, bool DerefBefore, if (DerefAfter) Ops.push_back(dwarf::DW_OP_deref); - return doPrepend(Expr, Ops, StackValue); + return prependOpcodes(Expr, Ops, StackValue); } -DIExpression *DIExpression::doPrepend(const DIExpression *Expr, - SmallVectorImpl &Ops, - bool StackValue) { +DIExpression *DIExpression::prependOpcodes(const DIExpression *Expr, + SmallVectorImpl &Ops, + bool StackValue) { if (Expr) for (auto Op : Expr->expr_ops()) { // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment. diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp index 96a1f4d..1ae5dde 100644 --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -1668,15 +1668,11 @@ static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) { // 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}); + SmallVector Ops = { + dwarf::DW_OP_deref, dwarf::DW_OP_constu, ValMinus, + dwarf::DW_OP_mul, dwarf::DW_OP_constu, ValInit, + dwarf::DW_OP_plus}; + E = DIExpression::prependOpcodes(E, Ops, DIExpression::WithStackValue); DIGlobalVariableExpression *DGVE = DIGlobalVariableExpression::get(NewGV->getContext(), DGV, E); NewGV->addDebugInfo(DGVE); diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index edb01d1..2f7d414 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -1522,8 +1522,8 @@ void llvm::salvageDebugInfo(Instruction &I) { auto doSalvage = [&](DbgInfoIntrinsic *DII, SmallVectorImpl &Ops) { auto *DIExpr = DII->getExpression(); - DIExpr = DIExpression::doPrepend(DIExpr, Ops, - DIExpression::WithStackValue); + DIExpr = + DIExpression::prependOpcodes(DIExpr, Ops, DIExpression::WithStackValue); DII->setOperand(0, wrapMD(I.getOperand(0))); DII->setOperand(2, MetadataAsValue::get(I.getContext(), DIExpr)); DEBUG(dbgs() << "SALVAGE: " << *DII << '\n'); diff --git a/llvm/test/Transforms/GlobalOpt/integer-bool-dwarf.ll b/llvm/test/Transforms/GlobalOpt/integer-bool-dwarf.ll index 5ee9897..3b4460a 100644 --- a/llvm/test/Transforms/GlobalOpt/integer-bool-dwarf.ll +++ b/llvm/test/Transforms/GlobalOpt/integer-bool-dwarf.ll @@ -1,6 +1,10 @@ ;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)) +;CHECK: @foo = internal unnamed_addr global i1 false, align 4, !dbg ![[VAR:.*]] +;CHECK: ![[VAR]] = !DIGlobalVariableExpression(var: !1, expr: +;CHECK-SAME: !DIExpression(DW_OP_deref, DW_OP_constu, 111, DW_OP_mul, +;CHECK-SAME: DW_OP_constu, 0, DW_OP_plus, DW_OP_stack_value, +;CHECK-SAME: DW_OP_LLVM_fragment, 0, 1)) @foo = internal global i32 0, align 4, !dbg !0 @@ -31,7 +35,7 @@ attributes #0 = { noinline nounwind optnone uwtable } !llvm.module.flags = !{!7, !8, !9} !llvm.ident = !{!10} -!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression(DW_OP_LLVM_fragment, 0, 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 6.0.0 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5) !3 = !DIFile(filename: "integer-bool-dwarf.c", directory: "/") -- 2.7.4