Skip to content

Commit cb8690f

Browse files
authored
[RemoveDIs] Handle DPValues in LowerDbgDeclare (#73504)
The tests will become "live" once #74090 lands (see for more info).
1 parent 7de592b commit cb8690f

13 files changed

+43
-17
lines changed

Diff for: llvm/lib/Transforms/Utils/Local.cpp

+30-17
Original file line numberDiff line numberDiff line change
@@ -1724,16 +1724,19 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
17241724
SI->getIterator());
17251725
}
17261726

1727+
namespace llvm {
17271728
// RemoveDIs: duplicate the getDebugValueLoc method using DPValues instead of
1728-
// dbg.value intrinsics.
1729-
static DebugLoc getDebugValueLocDPV(DPValue *DPV) {
1729+
// dbg.value intrinsics. In llvm namespace so that it overloads the
1730+
// DbgVariableIntrinsic version.
1731+
static DebugLoc getDebugValueLoc(DPValue *DPV) {
17301732
// Original dbg.declare must have a location.
17311733
const DebugLoc &DeclareLoc = DPV->getDebugLoc();
17321734
MDNode *Scope = DeclareLoc.getScope();
17331735
DILocation *InlinedAt = DeclareLoc.getInlinedAt();
17341736
// Produce an unknown location with the correct scope / inlinedAt fields.
17351737
return DILocation::get(DPV->getContext(), 0, 0, Scope, InlinedAt);
17361738
}
1739+
} // namespace llvm
17371740

17381741
/// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
17391742
/// that has an associated llvm.dbg.declare intrinsic.
@@ -1770,7 +1773,7 @@ void llvm::ConvertDebugDeclareToDebugValue(DPValue *DPV, StoreInst *SI,
17701773
auto *DIExpr = DPV->getExpression();
17711774
Value *DV = SI->getValueOperand();
17721775

1773-
DebugLoc NewLoc = getDebugValueLocDPV(DPV);
1776+
DebugLoc NewLoc = getDebugValueLoc(DPV);
17741777

17751778
if (!valueCoversEntireFragment(DV->getType(), DPV)) {
17761779
// FIXME: If storing to a part of the variable described by the dbg.declare,
@@ -1842,7 +1845,7 @@ void llvm::ConvertDebugDeclareToDebugValue(DPValue *DPV, LoadInst *LI,
18421845
return;
18431846
}
18441847

1845-
DebugLoc NewLoc = getDebugValueLocDPV(DPV);
1848+
DebugLoc NewLoc = getDebugValueLoc(DPV);
18461849

18471850
// We are now tracking the loaded value instead of the address. In the
18481851
// future if multi-location support is added to the IR, it might be
@@ -1887,7 +1890,7 @@ void llvm::ConvertDebugDeclareToDebugValue(DPValue *DPV, PHINode *APN,
18871890
BasicBlock *BB = APN->getParent();
18881891
auto InsertionPt = BB->getFirstInsertionPt();
18891892

1890-
DebugLoc NewLoc = getDebugValueLocDPV(DPV);
1893+
DebugLoc NewLoc = getDebugValueLoc(DPV);
18911894

18921895
// The block may be a catchswitch block, which does not have a valid
18931896
// insertion point.
@@ -1903,25 +1906,32 @@ bool llvm::LowerDbgDeclare(Function &F) {
19031906
bool Changed = false;
19041907
DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
19051908
SmallVector<DbgDeclareInst *, 4> Dbgs;
1906-
for (auto &FI : F)
1907-
for (Instruction &BI : FI)
1908-
if (auto DDI = dyn_cast<DbgDeclareInst>(&BI))
1909+
SmallVector<DPValue *> DPVs;
1910+
for (auto &FI : F) {
1911+
for (Instruction &BI : FI) {
1912+
if (auto *DDI = dyn_cast<DbgDeclareInst>(&BI))
19091913
Dbgs.push_back(DDI);
1914+
for (DPValue &DPV : BI.getDbgValueRange()) {
1915+
if (DPV.getType() == DPValue::LocationType::Declare)
1916+
DPVs.push_back(&DPV);
1917+
}
1918+
}
1919+
}
19101920

1911-
if (Dbgs.empty())
1921+
if (Dbgs.empty() && DPVs.empty())
19121922
return Changed;
19131923

1914-
for (auto &I : Dbgs) {
1915-
DbgDeclareInst *DDI = I;
1916-
AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress());
1924+
auto LowerOne = [&](auto *DDI) {
1925+
AllocaInst *AI =
1926+
dyn_cast_or_null<AllocaInst>(DDI->getVariableLocationOp(0));
19171927
// If this is an alloca for a scalar variable, insert a dbg.value
19181928
// at each load and store to the alloca and erase the dbg.declare.
19191929
// The dbg.values allow tracking a variable even if it is not
19201930
// stored on the stack, while the dbg.declare can only describe
19211931
// the stack slot (and at a lexical-scope granularity). Later
19221932
// passes will attempt to elide the stack slot.
19231933
if (!AI || isArray(AI) || isStructure(AI))
1924-
continue;
1934+
return;
19251935

19261936
// A volatile load/store means that the alloca can't be elided anyway.
19271937
if (llvm::any_of(AI->users(), [](User *U) -> bool {
@@ -1931,7 +1941,7 @@ bool llvm::LowerDbgDeclare(Function &F) {
19311941
return SI->isVolatile();
19321942
return false;
19331943
}))
1934-
continue;
1944+
return;
19351945

19361946
SmallVector<const Value *, 8> WorkList;
19371947
WorkList.push_back(AI);
@@ -1963,11 +1973,14 @@ bool llvm::LowerDbgDeclare(Function &F) {
19631973
}
19641974
DDI->eraseFromParent();
19651975
Changed = true;
1966-
}
1976+
};
1977+
1978+
for_each(Dbgs, LowerOne);
1979+
for_each(DPVs, LowerOne);
19671980

19681981
if (Changed)
1969-
for (BasicBlock &BB : F)
1970-
RemoveRedundantDbgInstrs(&BB);
1982+
for (BasicBlock &BB : F)
1983+
RemoveRedundantDbgInstrs(&BB);
19711984

19721985
return Changed;
19731986
}

Diff for: llvm/test/DebugInfo/ARM/lowerbdgdeclare_vla.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -passes=instcombine %s -S | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators -passes=instcombine %s -S | FileCheck %s
23
;
34
; Generate me from:
45
; clang -cc1 -triple thumbv7-apple-ios7.0.0 -S -target-abi apcs-gnu -gdwarf-2 -Os test.c -o test.ll -emit-llvm

Diff for: llvm/test/DebugInfo/Generic/dbg-value-lower-linenos.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt < %s -S -passes=mem2reg,instcombine | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators < %s -S -passes=mem2reg,instcombine | FileCheck %s
23

34
; The '%bar' alloca will be promoted to an SSA register by mem2reg: test that
45
; zero line number are assigned to the dbg.value intrinsics that are inserted

Diff for: llvm/test/DebugInfo/X86/array2.ll

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
; }
1414
;
1515
; RUN: opt %s -O2 -S -o - | FileCheck %s
16+
; RUN: opt --try-experimental-debuginfo-iterators %s -O2 -S -o - | FileCheck %s
1617
; Test that we correctly lower dbg.declares for arrays.
1718
;
1819
; CHECK: define i32 @main

Diff for: llvm/test/DebugInfo/X86/formal_parameter.ll

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ target triple = "x86_64-apple-macosx10.9.0"
1515
; RUN: opt %s -O2 -S -o %t
1616
; RUN: cat %t | FileCheck --check-prefix=LOWERING %s
1717
; RUN: llc -filetype=obj %t -o - | llvm-dwarfdump -debug-info - | FileCheck %s
18+
; RUN: llc --try-experimental-debuginfo-iterators -filetype=obj %t -o - | llvm-dwarfdump -debug-info - | FileCheck %s
1819
; Test that we only emit only one DW_AT_formal_parameter "map" for this function.
1920
; rdar://problem/14874886
2021
;

Diff for: llvm/test/DebugInfo/X86/instcombine-instrinsics.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt %s -O2 -S -o - | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators %s -O2 -S -o - | FileCheck %s
23
; Verify that we emit the same intrinsic at most once.
34
; rdar://problem/13056109
45
;

Diff for: llvm/test/DebugInfo/duplicate_dbgvalue.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -passes=instcombine -S -o - < %s | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators -passes=instcombine -S -o - < %s | FileCheck %s
23

34
; CHECK-LABEL: %3 = load i32, ptr %i1_311
45
; CHECK: call void @llvm.dbg.value(metadata i32 %3

Diff for: llvm/test/DebugInfo/verify-di-preserve.ll

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
; RUN: opt %s -verify-debuginfo-preserve -passes=instcombine -disable-output 2>&1 | FileCheck --check-prefix=VERIFY %s
2+
; RUN: opt --try-experimental-debuginfo-iterators %s -verify-debuginfo-preserve -passes=instcombine -disable-output 2>&1 | FileCheck --check-prefix=VERIFY %s
23

34
; VERIFY: CheckModuleDebugify (original debuginfo):
45

56
; RUN: opt %s -verify-each-debuginfo-preserve -O2 -disable-output 2>&1 | FileCheck --check-prefix=VERIFY-EACH %s
7+
; RUN: opt %s --try-experimental-debuginfo-iterators -verify-each-debuginfo-preserve -O2 -disable-output 2>&1 | FileCheck --check-prefix=VERIFY-EACH %s
68

79
; VERIFY-EACH: DeadArgumentEliminationPass
810
; VERIFY-EACH: GlobalDCEPass

Diff for: llvm/test/Transforms/InstCombine/dbg-scalable-store-fixed-frag.ll

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
; RUN: opt < %s -passes='instcombine' -S | FileCheck %s
33
; RUN: opt < %s -passes='instcombine' -S --try-experimental-debuginfo-iterators | FileCheck %s
44

5+
56
define i32 @foo(<vscale x 2 x i32> %x) {
67
; CHECK-LABEL: @foo(
78
; CHECK-NEXT: entry:

Diff for: llvm/test/Transforms/InstCombine/dbg-simplify-alloca-size.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -S --passes=instcombine %s | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators -S --passes=instcombine %s | FileCheck %s
23

34
; https://github.com/llvm/llvm-project/issues/56807
45
declare void @foo(ptr %pixels)

Diff for: llvm/test/Transforms/InstCombine/debuginfo-scalable-typesize.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators -passes=instcombine -S < %s | FileCheck %s
23

34
; This test is defending against a TypeSize message raised in the method
45
; `valueCoversEntireFragment` in Local.cpp because of an implicit cast from

Diff for: llvm/test/Transforms/InstCombine/lifetime-no-null-opt.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
; RUN: opt --try-experimental-debuginfo-iterators < %s -passes=instcombine -S | FileCheck %s
34

45
declare void @llvm.dbg.declare(metadata, metadata, metadata)
56
declare void @llvm.lifetime.start.p0(i64, ptr nocapture)

Diff for: llvm/test/Transforms/InstCombine/lifetime.ll

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
; RUN: opt --try-experimental-debuginfo-iterators < %s -passes=instcombine -S | FileCheck %s
34

45
declare void @llvm.dbg.declare(metadata, metadata, metadata)
56
declare void @llvm.lifetime.start.p0(i64, ptr nocapture)

0 commit comments

Comments
 (0)