Skip to content

Commit e0374fb

Browse files
Ben MuddSLTozer
authored andcommitted
[DebugInfo] Make debug intrinsics to track cloned values in JumpThreading
This patch causes debug value intrinsics outside of cloned blocks in the Jump Threading pass to correctly point towards any derived values. If it cannot, it kills them. Reviewed By: probinson, StephenTozer Differential Revision: https://reviews.llvm.org/D140404
1 parent 0ece205 commit e0374fb

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

llvm/include/llvm/Transforms/Utils/SSAUpdater.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ template <typename T> class SSAUpdaterTraits;
2828
class Type;
2929
class Use;
3030
class Value;
31+
class DbgValueInst;
3132

3233
/// Helper class for SSA formation on a set of values defined in
3334
/// multiple blocks.
@@ -114,6 +115,15 @@ class SSAUpdater {
114115
/// be below it.
115116
void RewriteUse(Use &U);
116117

118+
/// Rewrite debug value intrinsics to conform to a new SSA form.
119+
///
120+
/// This will scout out all the debug value instrinsics associated with
121+
/// the instruction. Anything outside of its block will have its
122+
/// value set to the new SSA value if available, and undef if not.
123+
void UpdateDebugValues(Instruction *I);
124+
void UpdateDebugValues(Instruction *I,
125+
SmallVectorImpl<DbgValueInst *> &DbgValues);
126+
117127
/// Rewrite a use like \c RewriteUse but handling in-block definitions.
118128
///
119129
/// This version of the method can rewrite uses in the same block as
@@ -123,6 +133,7 @@ class SSAUpdater {
123133

124134
private:
125135
Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
136+
void UpdateDebugValue(Instruction *I, DbgValueInst *DbgValue);
126137
};
127138

128139
/// Helper class for promoting a collection of loads and stores into SSA

llvm/lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/IR/ConstantRange.h"
4141
#include "llvm/IR/Constants.h"
4242
#include "llvm/IR/DataLayout.h"
43+
#include "llvm/IR/DebugInfo.h"
4344
#include "llvm/IR/Dominators.h"
4445
#include "llvm/IR/Function.h"
4546
#include "llvm/IR/InstrTypes.h"
@@ -2038,6 +2039,7 @@ void JumpThreadingPass::updateSSA(
20382039
// PHI insertion, of which we are prepared to do, clean these up now.
20392040
SSAUpdater SSAUpdate;
20402041
SmallVector<Use *, 16> UsesToRename;
2042+
SmallVector<DbgValueInst *, 4> DbgValues;
20412043

20422044
for (Instruction &I : *BB) {
20432045
// Scan all uses of this instruction to see if it is used outside of its
@@ -2053,8 +2055,16 @@ void JumpThreadingPass::updateSSA(
20532055
UsesToRename.push_back(&U);
20542056
}
20552057

2058+
// Find debug values outside of the block
2059+
findDbgValues(DbgValues, &I);
2060+
DbgValues.erase(remove_if(DbgValues,
2061+
[&](const DbgValueInst *DbgVal) {
2062+
return DbgVal->getParent() == BB;
2063+
}),
2064+
DbgValues.end());
2065+
20562066
// If there are no uses outside the block, we're done with this instruction.
2057-
if (UsesToRename.empty())
2067+
if (UsesToRename.empty() && DbgValues.empty())
20582068
continue;
20592069
LLVM_DEBUG(dbgs() << "JT: Renaming non-local uses of: " << I << "\n");
20602070

@@ -2067,6 +2077,11 @@ void JumpThreadingPass::updateSSA(
20672077

20682078
while (!UsesToRename.empty())
20692079
SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
2080+
if (!DbgValues.empty()) {
2081+
SSAUpdate.UpdateDebugValues(&I, DbgValues);
2082+
DbgValues.clear();
2083+
}
2084+
20702085
LLVM_DEBUG(dbgs() << "\n");
20712086
}
20722087
}

llvm/lib/Transforms/Utils/SSAUpdater.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/IR/BasicBlock.h"
2020
#include "llvm/IR/CFG.h"
2121
#include "llvm/IR/Constants.h"
22+
#include "llvm/IR/DebugInfo.h"
2223
#include "llvm/IR/DebugLoc.h"
2324
#include "llvm/IR/Instruction.h"
2425
#include "llvm/IR/Instructions.h"
@@ -195,6 +196,33 @@ void SSAUpdater::RewriteUse(Use &U) {
195196
U.set(V);
196197
}
197198

199+
void SSAUpdater::UpdateDebugValues(Instruction *I) {
200+
SmallVector<DbgValueInst *, 4> DbgValues;
201+
llvm::findDbgValues(DbgValues, I);
202+
for (auto &DbgValue : DbgValues) {
203+
if (DbgValue->getParent() == I->getParent())
204+
continue;
205+
UpdateDebugValue(I, DbgValue);
206+
}
207+
}
208+
209+
void SSAUpdater::UpdateDebugValues(Instruction *I,
210+
SmallVectorImpl<DbgValueInst *> &DbgValues) {
211+
for (auto &DbgValue : DbgValues) {
212+
UpdateDebugValue(I, DbgValue);
213+
}
214+
}
215+
216+
void SSAUpdater::UpdateDebugValue(Instruction *I, DbgValueInst *DbgValue) {
217+
BasicBlock *UserBB = DbgValue->getParent();
218+
if (HasValueForBlock(UserBB)) {
219+
Value *NewVal = GetValueInMiddleOfBlock(UserBB);
220+
DbgValue->replaceVariableLocationOp(I, NewVal);
221+
}
222+
else
223+
DbgValue->setKillLocation();
224+
}
225+
198226
void SSAUpdater::RewriteUseAfterInsertions(Use &U) {
199227
Instruction *User = cast<Instruction>(U.getUser());
200228

llvm/test/Transforms/JumpThreading/thread-debug-info.ll

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@a = global i32 0, align 4
44
; Test that the llvm.dbg.value calls in a threaded block are correctly updated to
55
; target the locals in their threaded block, and not the unthreaded one.
6-
define void @test2(i32 %cond1, i32 %cond2) {
6+
define void @test1(i32 %cond1, i32 %cond2) {
77
; CHECK: [[globalptr:@.*]] = global i32 0, align 4
88
; CHECK: bb.cond2:
99
; CHECK: call void @llvm.dbg.value(metadata ptr null, metadata ![[DBG1ptr:[0-9]+]], metadata !DIExpression()), !dbg ![[DBG2ptr:[0-9]+]]
@@ -49,6 +49,48 @@ exit: ; preds = %bb.f4, %bb.f3, %bb.
4949
ret void, !dbg !29
5050
}
5151

52+
; This is testing for debug value instrinsics outside of the threaded block pointing to a value
53+
; inside to correctly take any new definitions.
54+
define void @test2(i32 %cond1, i32 %cond2) !dbg !5 {
55+
; CHECK: bb.f3
56+
; CHECK: call void @llvm.dbg.value(metadata ptr @a, metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}}
57+
; CHECK: bb.f4
58+
; CHECK-NEXT: [[PTR3:%.*]] = phi ptr [ null, %bb.cond2 ]
59+
; CHECK-NEXT: call void @llvm.dbg.value(metadata ptr [[PTR3]], metadata !{{[0-9]+}}, metadata !DIExpression()), !dbg !{{[0-9]+}}
60+
entry:
61+
%tobool = icmp eq i32 %cond1, 0, !dbg !15
62+
br i1 %tobool, label %bb.cond2, label %bb.f1, !dbg !16
63+
64+
bb.f1: ; preds = %entry
65+
call void @f1(), !dbg !17
66+
br label %bb.cond2, !dbg !18
67+
68+
bb.cond2: ; preds = %bb.f1, %entry
69+
%ptr = phi ptr [ null, %bb.f1 ], [ @a, %entry ], !dbg !19
70+
%tobool1 = icmp eq i32 %cond2, 0, !dbg !20
71+
br i1 %tobool1, label %bb.file, label %bb.f2, !dbg !21
72+
73+
bb.f2: ; preds = %bb.cond2
74+
call void @f2(), !dbg !22
75+
br label %exit, !dbg !23
76+
77+
bb.file: ; preds = %bb.cond2
78+
%cmp = icmp eq ptr %ptr, null, !dbg !24
79+
call void @llvm.dbg.value(metadata ptr %ptr, metadata !14, metadata !DIExpression()), !dbg !21
80+
br i1 %cmp, label %bb.f4, label %bb.f3, !dbg !25
81+
82+
bb.f3: ; preds = %bb.file
83+
call void @f3(), !dbg !26
84+
br label %exit, !dbg !27
85+
86+
bb.f4: ; preds = %bb.file
87+
call void @f4(), !dbg !28
88+
br label %exit, !dbg !29
89+
90+
exit: ; preds = %bb.f4, %bb.f3, %bb.f2
91+
ret void, !dbg !29
92+
}
93+
5294
declare void @f1()
5395

5496
declare void @f2()
@@ -95,4 +137,4 @@ attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memo
95137
!26 = !DILocation(line: 12, column: 1, scope: !5)
96138
!27 = !DILocation(line: 13, column: 1, scope: !5)
97139
!28 = !DILocation(line: 14, column: 1, scope: !5)
98-
!29 = !DILocation(line: 15, column: 1, scope: !5)
140+
!29 = !DILocation(line: 15, column: 1, scope: !5)

0 commit comments

Comments
 (0)