Skip to content

Commit 9649c20

Browse files
committed
[InstCombine] Drop debug loc in TryToSinkInstruction (reland)
Summary: The advice in HowToUpdateDebugInfo.rst is to "... preserve the debug location of an instruction if the instruction either remains in its basic block, or if its basic block is folded into a predecessor that branches unconditionally". TryToSinkInstruction doesn't seem to satisfy the criteria as it's sinking an instruction to some successor block. Preserving the debug loc can make single-stepping appear to go backwards, or make a breakpoint hit on that location happen "too late" (since single-stepping from that breakpoint can cause the function to return unexpectedly). So, drop the debug location. This was reverted in ee36206 because it removed source locations from inlinable calls, breaking a verifier rule. I've added an exception for calls because the alternative (setting a line 0 location) is not better. I tested the updated patch by completing a stage2 RelWithDebInfo build. Reviewers: aprantl, davide Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D82487
1 parent 2b00cac commit 9649c20

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,6 +3355,12 @@ static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
33553355
I->moveBefore(&*InsertPos);
33563356
++NumSunkInst;
33573357

3358+
// Drop the debug loc of non-inlinable instructions. This prevents
3359+
// single-stepping from going backwards. See HowToUpdateDebugInfo.rst for
3360+
// the full rationale.
3361+
if (!isa<CallBase>(I))
3362+
I->setDebugLoc(DebugLoc());
3363+
33583364
// Also sink all related debug uses from the source basic block. Otherwise we
33593365
// get debug use before the def. Attempt to salvage debug uses first, to
33603366
// maximise the range variables have location for. If we cannot salvage, then
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; RUN: opt -debugify -debugify-level=locations -instcombine -S < %s | FileCheck %s
2+
3+
; CHECK-LABEL: @test1(
4+
; CHECK: [[phi:%.*]] = phi i32
5+
; CHECK-NEXT: [[add:%.*]] = add i32 {{.*}}, 1{{$}}
6+
; CHECK-NEXT: add i32 [[phi]], [[add]], !dbg
7+
define i32 @test1(i32 %0, i1 %1) {
8+
%3 = add i32 %0, 1
9+
br i1 %1, label %4, label %5
10+
11+
4: ; preds = %2
12+
br label %6
13+
14+
5: ; preds = %2
15+
br label %6
16+
17+
6: ; preds = %5, %4
18+
%7 = phi i32 [ 0, %4 ], [ 1, %5 ]
19+
%8 = add i32 %7, %3
20+
ret i32 %8
21+
}
22+
23+
; Function Attrs: nounwind readnone
24+
declare i32 @external(i32) #0
25+
26+
; CHECK-LABEL: @test2(
27+
; CHECK: [[phi:%.*]] = phi i32
28+
; CHECK-NEXT: [[add:%.*]] = call i32 @external(i32 {{.*}}), !dbg
29+
; CHECK-NEXT: add i32 [[phi]], [[add]], !dbg
30+
define i32 @test2(i32 %0, i1 %1) {
31+
%3 = call i32 @external(i32 %0)
32+
br i1 %1, label %4, label %5
33+
34+
4: ; preds = %2
35+
br label %6
36+
37+
5: ; preds = %2
38+
br label %6
39+
40+
6: ; preds = %5, %4
41+
%7 = phi i32 [ 0, %4 ], [ 1, %5 ]
42+
%8 = add i32 %7, %3
43+
ret i32 %8
44+
}
45+
46+
attributes #0 = { nounwind readnone }

0 commit comments

Comments
 (0)