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

Commit 5c08882

Browse files
committed
[BranchFolding] Merge debug locations from common tail instead of removing
Summary: D25742 improved the precision of debug locations for PGO by removing debug locations from common tail when tail-merging. However, if identical insturctions that are merged into a common tail have the same debug locations, there's no need to remove them. This patch creates a merged debug location of identical instructions across SameTails and assign it to the instruction in the common tail, so that the debug locations are maintained if they are same across identical instructions. Reviewers: aprantl, probinson, MatzeB, rob.lougher Reviewed By: aprantl Subscribers: andreadb, llvm-commits Differential Revision: https://reviews.llvm.org/D30226 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297805 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c7a57cd commit 5c08882

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

lib/CodeGen/BranchFolding.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "llvm/CodeGen/MachineRegisterInfo.h"
3333
#include "llvm/CodeGen/Passes.h"
3434
#include "llvm/CodeGen/TargetPassConfig.h"
35+
#include "llvm/IR/DebugInfoMetadata.h"
3536
#include "llvm/IR/Function.h"
3637
#include "llvm/Support/CommandLine.h"
3738
#include "llvm/Support/Debug.h"
@@ -753,6 +754,45 @@ bool BranchFolder::CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
753754
return true;
754755
}
755756

757+
/// MergeCommonTailDebugLocs - Create merged DebugLocs of identical instructions
758+
/// across SameTails and assign it to the instruction in common tail.
759+
void BranchFolder::MergeCommonTailDebugLocs(unsigned commonTailIndex) {
760+
MachineBasicBlock *MBB = SameTails[commonTailIndex].getBlock();
761+
762+
std::vector<MachineBasicBlock::iterator> NextCommonInsts(SameTails.size());
763+
for (unsigned int i = 0 ; i != SameTails.size() ; ++i) {
764+
if (i != commonTailIndex)
765+
NextCommonInsts[i] = SameTails[i].getTailStartPos();
766+
else {
767+
assert(SameTails[i].getTailStartPos() == MBB->begin() &&
768+
"MBB is not a common tail only block");
769+
}
770+
}
771+
772+
for (auto &MI : *MBB) {
773+
if (MI.isDebugValue())
774+
continue;
775+
DebugLoc DL = MI.getDebugLoc();
776+
for (unsigned int i = 0 ; i < NextCommonInsts.size() ; i++) {
777+
if (i == commonTailIndex)
778+
continue;
779+
780+
auto &Pos = NextCommonInsts[i];
781+
assert(Pos != SameTails[i].getBlock()->end() &&
782+
"Reached BB end within common tail");
783+
while (Pos->isDebugValue()) {
784+
++Pos;
785+
assert(Pos != SameTails[i].getBlock()->end() &&
786+
"Reached BB end within common tail");
787+
}
788+
assert(MI.isIdenticalTo(*Pos) && "Expected matching MIIs!");
789+
DL = DILocation::getMergedLocation(DL, Pos->getDebugLoc());
790+
NextCommonInsts[i] = ++Pos;
791+
}
792+
MI.setDebugLoc(DL);
793+
}
794+
}
795+
756796
static void
757797
mergeOperations(MachineBasicBlock::iterator MBBIStartPos,
758798
MachineBasicBlock &MBBCommon) {
@@ -905,10 +945,8 @@ bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
905945
// Recompute common tail MBB's edge weights and block frequency.
906946
setCommonTailEdgeWeights(*MBB);
907947

908-
// Remove the original debug location from the common tail.
909-
for (auto &MI : *MBB)
910-
if (!MI.isDebugValue())
911-
MI.setDebugLoc(DebugLoc());
948+
// Merge debug locations across identical instructions for common tail
949+
MergeCommonTailDebugLocs(commonTailIndex);
912950

913951
// MBB is common tail. Adjust all other BB's to jump to this one.
914952
// Traversal must be forwards so erases work.

lib/CodeGen/BranchFolding.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ namespace llvm {
153153
MachineBasicBlock *SuccBB,
154154
unsigned maxCommonTailLength,
155155
unsigned &commonTailIndex);
156+
void MergeCommonTailDebugLocs(unsigned commonTailIndex);
156157

157158
bool OptimizeBranches(MachineFunction &MF);
158159
bool OptimizeBlock(MachineBasicBlock *MBB);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; RUN: llc -stop-after=branch-folder < %s | FileCheck %s
2+
;
3+
; bb2 and bb3 in the IR below will be tail-merged into a single basic block.
4+
; As br instructions in bb2 and bb3 have the same debug location, make sure that
5+
; the branch instruction in the merged basic block still maintains the debug
6+
; location info.
7+
;
8+
; CHECK: [[DLOC:![0-9]+]] = !DILocation(line: 2, column: 2, scope: !{{[0-9]+}})
9+
; CHECK: TEST64rr{{.*}}%rsi, %rsi, implicit-def %eflags
10+
; CHECK-NEXT: JNE_1{{.*}}, debug-location [[DLOC]]
11+
12+
target triple = "x86_64-unknown-linux-gnu"
13+
14+
define i32 @foo(i1 %b, i8* %p) {
15+
bb1:
16+
br i1 %b, label %bb2, label %bb3
17+
18+
bb2:
19+
%a1 = icmp eq i8* %p, null
20+
br i1 %a1, label %bb4, label %bb5, !dbg !6
21+
22+
bb3:
23+
%a2 = icmp eq i8* %p, null
24+
br i1 %a2, label %bb4, label %bb5, !dbg !6
25+
26+
bb4:
27+
ret i32 1
28+
29+
bb5:
30+
ret i32 0
31+
}
32+
33+
!llvm.dbg.cu = !{!0}
34+
!llvm.module.flags = !{!2, !3}
35+
36+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1)
37+
!1 = !DIFile(filename: "foo.c", directory: "b/")
38+
!2 = !{i32 2, !"Dwarf Version", i32 4}
39+
!3 = !{i32 2, !"Debug Info Version", i32 3}
40+
!4 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 3, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !0)
41+
!5 = distinct !DILexicalBlock(scope: !4, file: !1, line: 1, column: 1)
42+
!6 = !DILocation(line: 2, column: 2, scope: !5)

0 commit comments

Comments
 (0)