Skip to content

Commit e14551b

Browse files
committed
[SimplifyCFG] Keep !dgb metadata of moved instruction, if they match.
Currently SimplifyCFG drops the debug locations of 'bonus' instructions. Such instructions are moved before the first branch. The reason for the current behavior is that this could lead to surprising debug stepping, if the block that's folded is dead. In case the first branch and the instructions to be folded have the same debug location, this shouldn't be an issue and we can keep the debug location. Reviewed By: vsk Differential Revision: https://reviews.llvm.org/D93662
1 parent ac5390c commit e14551b

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

llvm/docs/HowToUpdateDebugInfo.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ Examples of transformations for which this rule *does not* apply include:
5959
* LICM. E.g., if an instruction is moved from the loop body to the preheader,
6060
the rule for :ref:`dropping locations<WhenToDropLocation>` applies.
6161

62+
In addition to the rule above, a transformation should also preserve the debug
63+
location of an instruction that is moved between basic blocks, if the
64+
destination block already contains an instruction with an identical debug
65+
location.
66+
67+
Examples of transformations that should follow this rule include:
68+
69+
* Moving instructions between basic blocks. For example, if instruction ``I1``
70+
in ``BB1`` is moved before ``I2`` in ``BB2``, the source location of ``I1``
71+
can be preserved if it has the same source location as ``I2``.
72+
6273
.. _WhenToMergeLocation:
6374

6475
When to merge instruction locations

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3037,10 +3037,13 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, DomTreeUpdater *DTU,
30373037
if (&BonusInst == Cond)
30383038
CondInPred = NewBonusInst;
30393039

3040-
// When we fold the bonus instructions we want to make sure we
3041-
// reset their debug locations in order to avoid stepping on dead
3042-
// code caused by folding dead branches.
3043-
NewBonusInst->setDebugLoc(DebugLoc());
3040+
if (PBI->getDebugLoc() != NewBonusInst->getDebugLoc()) {
3041+
// Unless the instruction has the same !dbg location as the original
3042+
// branch, drop it. When we fold the bonus instructions we want to make
3043+
// sure we reset their debug locations in order to avoid stepping on
3044+
// dead code caused by folding dead branches.
3045+
NewBonusInst->setDebugLoc(DebugLoc());
3046+
}
30443047

30453048
RemapInstruction(NewBonusInst, VMap,
30463049
RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);

llvm/test/Transforms/SimplifyCFG/fold-debug-location.ll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
; RUN: opt -S -simplifycfg -simplifycfg-require-and-preserve-domtree=1 < %s | FileCheck %s --match-full-lines
22

33
; Make sure we reset the debug location when folding instructions.
4+
; CHECK-LABEL: define {{.+}} @patatino({{.+}}
45
; CHECK: [[VAL:%.*]] = and i32 %c2, %k
56
; CHECK-NEXT: [[VAL2:%.*]] icmp eq i32 [[VAL]], 0
67

@@ -24,6 +25,34 @@ define i32 @patatino(i32 %k, i32 %c1, i32 %c2) !dbg !6 {
2425
ret i32 undef, !dbg !16
2526
}
2627

28+
; All instructions involved in folding have the same !dbg location. Make sure
29+
; they are preserved.
30+
define void @dbg_all_equal(i32 %k, i32 %c1, i32 %c2) !dbg !17 {
31+
; CHECK-LABEL: define {{.+}} @dbg_all_equal({{.+}}
32+
; CHECK-NEXT: [[A1:%[a-z0-9]+]] = and i32 %c1, %k, !dbg [[DBG:![0-9]+]]
33+
; CHECK-NEXT: [[C1:%[a-z0-9]+]] = icmp eq i32 [[A1]], 0, !dbg [[DBG]]
34+
; CHECK-NEXT: [[A2:%[a-z0-9]+]] = and i32 %c2, %k, !dbg [[DBG]]
35+
; CHECK-NEXT: [[C2:%[a-z0-9]+]] = icmp eq i32 [[A2]], 0, !dbg [[DBG]]
36+
; CHECK-NEXT: [[OR:%[.a-z0-9]+]] = or i1 [[C1]], [[C2]], !dbg [[DBG]]
37+
; CHECK-NEXT: br i1 [[OR]], label {{.+}}, label {{.+}}, !dbg [[DBG]]
38+
;
39+
%1 = and i32 %c1, %k, !dbg !18
40+
%2 = icmp eq i32 %1, 0, !dbg !18
41+
br i1 %2, label %8, label %3, !dbg !18
42+
43+
3:
44+
%4 = and i32 %c2, %k, !dbg !18
45+
%5 = icmp eq i32 %4, 0, !dbg !18
46+
br i1 %5, label %8, label %6, !dbg !18
47+
48+
6:
49+
%7 = tail call i32 (...) @bar(), !dbg !18
50+
br label %8, !dbg !18
51+
52+
8:
53+
ret void
54+
}
55+
2756
!llvm.dbg.cu = !{!0}
2857
!llvm.debugify = !{!3, !4}
2958
!llvm.module.flags = !{!5}
@@ -45,3 +74,5 @@ define i32 @patatino(i32 %k, i32 %c1, i32 %c2) !dbg !6 {
4574
!14 = !DILocation(line: 7, column: 1, scope: !6)
4675
!15 = !DILocation(line: 8, column: 1, scope: !6)
4776
!16 = !DILocation(line: 9, column: 1, scope: !6)
77+
!17 = distinct !DISubprogram(name: "dbg_all_equal", linkageName: "dbg_all_equal", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
78+
!18 = !DILocation(line: 10, column: 10, scope: !17)

0 commit comments

Comments
 (0)