From 5b477be72a4eafe4ddd927589b8b4d962d379a3f Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Fri, 9 Mar 2018 00:45:04 +0000 Subject: [PATCH] LowerDbgDeclare: ignore dbg.declares for allocas with volatile access There is no point in lowering a dbg.declare describing an alloca that has volatile loads or stores as users, since the alloca cannot be elided. Lowering the dbg.declare will result in larger debug info that may also have worse coverage than just describing the alloca. rdar://problem/34496278 llvm-svn: 327092 --- llvm/lib/Transforms/Utils/Local.cpp | 45 +++++++----- llvm/test/DebugInfo/Generic/volatile-alloca.ll | 83 ++++++++++++++++++++++ .../Transforms/Util/simplify-dbg-declare-load.ll | 8 ++- 3 files changed, 116 insertions(+), 20 deletions(-) create mode 100644 llvm/test/DebugInfo/Generic/volatile-alloca.ll diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index 1bfa1be..cd96847c 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -1322,25 +1322,36 @@ bool llvm::LowerDbgDeclare(Function &F) { // stored on the stack, while the dbg.declare can only describe // the stack slot (and at a lexical-scope granularity). Later // passes will attempt to elide the stack slot. - if (AI && !isArray(AI)) { - for (auto &AIUse : AI->uses()) { - User *U = AIUse.getUser(); - if (StoreInst *SI = dyn_cast(U)) { - if (AIUse.getOperandNo() == 1) - ConvertDebugDeclareToDebugValue(DDI, SI, DIB); - } else if (LoadInst *LI = dyn_cast(U)) { - ConvertDebugDeclareToDebugValue(DDI, LI, DIB); - } else if (CallInst *CI = dyn_cast(U)) { - // This is a call by-value or some other instruction that - // takes a pointer to the variable. Insert a *value* - // intrinsic that describes the alloca. - DIB.insertDbgValueIntrinsic(AI, DDI->getVariable(), - DDI->getExpression(), DDI->getDebugLoc(), - CI); - } + if (!AI || isArray(AI)) + continue; + + // A volatile load/store means that the alloca can't be elided anyway. + if (llvm::any_of(AI->users(), [](User *U) -> bool { + if (LoadInst *LI = dyn_cast(U)) + return LI->isVolatile(); + if (StoreInst *SI = dyn_cast(U)) + return SI->isVolatile(); + return false; + })) + continue; + + for (auto &AIUse : AI->uses()) { + User *U = AIUse.getUser(); + if (StoreInst *SI = dyn_cast(U)) { + if (AIUse.getOperandNo() == 1) + ConvertDebugDeclareToDebugValue(DDI, SI, DIB); + } else if (LoadInst *LI = dyn_cast(U)) { + ConvertDebugDeclareToDebugValue(DDI, LI, DIB); + } else if (CallInst *CI = dyn_cast(U)) { + // This is a call by-value or some other instruction that + // takes a pointer to the variable. Insert a *value* + // intrinsic that describes the alloca. + DIB.insertDbgValueIntrinsic(AI, DDI->getVariable(), + DDI->getExpression(), DDI->getDebugLoc(), + CI); } - DDI->eraseFromParent(); } + DDI->eraseFromParent(); } return true; } diff --git a/llvm/test/DebugInfo/Generic/volatile-alloca.ll b/llvm/test/DebugInfo/Generic/volatile-alloca.ll new file mode 100644 index 0000000..438d886 --- /dev/null +++ b/llvm/test/DebugInfo/Generic/volatile-alloca.ll @@ -0,0 +1,83 @@ +; RUN: opt -mem2reg -instcombine %s -o - -S | FileCheck %s +; +; Test that a dbg.declare describing am alloca with volatile +; load/stores is not lowered into a dbg.value, since the alloca won't +; be elided anyway. +; +; Generated from: +; +; unsigned long long g(); +; void h(unsigned long long); +; void f() { +; volatile unsigned long long v = g(); +; if (v == 0) +; g(); +; h(v); +; } + +; CHECK: alloca i64 +; CHECK-NOT: call void @llvm.dbg.value +; CHECK: call void @llvm.dbg.declare +; CHECK-NOT: call void @llvm.dbg.value + +source_filename = "volatile.c" + +; Function Attrs: nounwind optsize ssp uwtable +define void @f() local_unnamed_addr #0 !dbg !8 { + %1 = alloca i64, align 8 + %2 = bitcast i64* %1 to i8*, !dbg !15 + call void @llvm.lifetime.start.p0i8(i64 8, i8* %2), !dbg !15 + call void @llvm.dbg.declare(metadata i64* %1, metadata !12, metadata !DIExpression()), !dbg !15 + %3 = call i64 (...) @g() #4, !dbg !15 + store volatile i64 %3, i64* %1, align 8, !dbg !15 + %4 = load volatile i64, i64* %1, align 8, !dbg !15 + %5 = icmp eq i64 %4, 0, !dbg !15 + br i1 %5, label %6, label %8, !dbg !15 + +;