Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit a4c98c3

Browse files
taewookohadrian-prantl
authored andcommitted
[BranchFolding] Update debug location along with the update of branch instruction.
Summary: Currently, BranchFolder drops DebugLoc for branch instructions in some places. For example, for the test code attached, the branch instruction of 'entry' block has a DILocation of ``` !12 = !DILocation(line: 6, column: 3, scope: !11) ``` , but this information is gone when then block is lowered because BranchFolder misses it. This patch is a fix for this issue. Reviewers: qcolombet, aprantl, craig.topper, MatzeB Reviewed By: aprantl Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D29902 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@295684 91177308-0d34-0410-b5e6-96231b3b80d8 (cherry picked from commit f8bacbb)
1 parent 84091f2 commit a4c98c3

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

lib/CodeGen/BranchFolding.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ MachineBasicBlock *BranchFolder::SplitMBBAt(MachineBasicBlock &CurMBB,
388388
NewMBB->splice(NewMBB->end(), &CurMBB, BBI1, CurMBB.end());
389389

390390
// NewMBB belongs to the same loop as CurMBB.
391-
if (MLI)
391+
if (MLI)
392392
if (MachineLoop *ML = MLI->getLoopFor(&CurMBB))
393393
ML->addBasicBlockToLoop(NewMBB, MLI->getBase());
394394

@@ -436,7 +436,7 @@ static void FixTail(MachineBasicBlock *CurMBB, MachineBasicBlock *SuccBB,
436436
MachineFunction::iterator I = std::next(MachineFunction::iterator(CurMBB));
437437
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
438438
SmallVector<MachineOperand, 4> Cond;
439-
DebugLoc dl; // FIXME: this is nowhere
439+
DebugLoc dl = CurMBB->findBranchDebugLoc();
440440
if (I != MF->end() && !TII->analyzeBranch(*CurMBB, TBB, FBB, Cond, true)) {
441441
MachineBasicBlock *NextBB = &*I;
442442
if (TBB == NextBB && !Cond.empty() && !FBB) {
@@ -1043,7 +1043,7 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
10431043

10441044
// Remove the unconditional branch at the end, if any.
10451045
if (TBB && (Cond.empty() || FBB)) {
1046-
DebugLoc dl; // FIXME: this is nowhere
1046+
DebugLoc dl = PBB->findBranchDebugLoc();
10471047
TII->removeBranch(*PBB);
10481048
if (!Cond.empty())
10491049
// reinsert conditional branch only, for now
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
; RUN: llc < %s | FileCheck %s
2+
;
3+
; The test code is generated from the following source code:
4+
;
5+
; 1 extern int bar(int x);
6+
; 2
7+
; 3 int foo(int *begin, int *end) {
8+
; 4 int *i;
9+
; 5 int ret = 0;
10+
; 6 for (
11+
; 7 i = begin ;
12+
; 8 i != end ;
13+
; 9 i++)
14+
; 10 {
15+
; 11 ret += bar(*i);
16+
; 12 }
17+
; 13 return ret;
18+
; 14 }
19+
;
20+
; CHECK: # %entry
21+
; CHECK-NOT: # %for.body
22+
; CHECK: .loc 1 6 3
23+
; CHECK-NEXT: je [[BB:.LBB[^ ]+]]
24+
; CHECK: [[BB]]:{{.}}# %for.end
25+
26+
target triple = "x86_64-unknown-linux-gnu"
27+
28+
define i32 @foo(i32* readonly %begin, i32* readnone %end) !dbg !4 {
29+
entry:
30+
%cmp6 = icmp eq i32* %begin, %end, !dbg !9
31+
br i1 %cmp6, label %for.end, label %for.body.preheader, !dbg !12
32+
33+
for.body.preheader: ; preds = %entry
34+
br label %for.body, !dbg !13
35+
36+
for.body: ; preds = %for.body.preheader, %for.body
37+
%ret.08 = phi i32 [ %add, %for.body ], [ 0, %for.body.preheader ]
38+
%i.07 = phi i32* [ %incdec.ptr, %for.body ], [ %begin, %for.body.preheader ]
39+
%0 = load i32, i32* %i.07, align 4, !dbg !13, !tbaa !15
40+
%call = tail call i32 @bar(i32 %0), !dbg !19
41+
%add = add nsw i32 %call, %ret.08, !dbg !20
42+
%incdec.ptr = getelementptr inbounds i32, i32* %i.07, i64 1, !dbg !21
43+
%cmp = icmp eq i32* %incdec.ptr, %end, !dbg !9
44+
br i1 %cmp, label %for.end.loopexit, label %for.body, !dbg !12, !llvm.loop !22
45+
46+
for.end.loopexit: ; preds = %for.body
47+
br label %for.end, !dbg !24
48+
49+
for.end: ; preds = %for.end.loopexit, %entry
50+
%ret.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.end.loopexit ]
51+
ret i32 %ret.0.lcssa, !dbg !24
52+
}
53+
54+
declare i32 @bar(i32)
55+
56+
!llvm.dbg.cu = !{!0}
57+
!llvm.module.flags = !{!2, !3}
58+
59+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug)
60+
!1 = !DIFile(filename: "foo.c", directory: "b/")
61+
!2 = !{i32 2, !"Dwarf Version", i32 4}
62+
!3 = !{i32 2, !"Debug Info Version", i32 3}
63+
!4 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 3, type: !5, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !0)
64+
!5 = !DISubroutineType(types: !6)
65+
!6 = !{!7, !8, !8}
66+
!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
67+
!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64)
68+
!9 = !DILocation(line: 8, column: 9, scope: !10)
69+
!10 = distinct !DILexicalBlock(scope: !11, file: !1, line: 6, column: 3)
70+
!11 = distinct !DILexicalBlock(scope: !4, file: !1, line: 6, column: 3)
71+
!12 = !DILocation(line: 6, column: 3, scope: !11)
72+
!13 = !DILocation(line: 11, column: 18, scope: !14)
73+
!14 = distinct !DILexicalBlock(scope: !10, file: !1, line: 10, column: 3)
74+
!15 = !{!16, !16, i64 0}
75+
!16 = !{!"int", !17, i64 0}
76+
!17 = !{!"omnipotent char", !18, i64 0}
77+
!18 = !{!"Simple C/C++ TBAA"}
78+
!19 = !DILocation(line: 11, column: 14, scope: !14)
79+
!20 = !DILocation(line: 11, column: 11, scope: !14)
80+
!21 = !DILocation(line: 9, column: 8, scope: !10)
81+
!22 = distinct !{!22, !12, !23}
82+
!23 = !DILocation(line: 12, column: 3, scope: !11)
83+
!24 = !DILocation(line: 13, column: 3, scope: !4)

0 commit comments

Comments
 (0)