Skip to content

Commit 3ae0901

Browse files
committed
wip debug location tail merging
Fixes #94050
1 parent a74348c commit 3ae0901

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,14 @@ static unsigned EstimateRuntime(MachineBasicBlock::iterator I,
455455
// with a conditional branch to the next block, optimize by reversing the
456456
// test and conditionally branching to SuccMBB instead.
457457
static void FixTail(MachineBasicBlock *CurMBB, MachineBasicBlock *SuccBB,
458-
const TargetInstrInfo *TII) {
458+
const TargetInstrInfo *TII, const DebugLoc &BranchDL) {
459459
MachineFunction *MF = CurMBB->getParent();
460460
MachineFunction::iterator I = std::next(MachineFunction::iterator(CurMBB));
461461
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
462462
SmallVector<MachineOperand, 4> Cond;
463463
DebugLoc dl = CurMBB->findBranchDebugLoc();
464+
if (!dl)
465+
dl = BranchDL;
464466
if (I != MF->end() && !TII->analyzeBranch(*CurMBB, TBB, FBB, Cond, true)) {
465467
MachineBasicBlock *NextBB = &*I;
466468
if (TBB == NextBB && !Cond.empty() && !FBB) {
@@ -686,15 +688,16 @@ unsigned BranchFolder::ComputeSameTails(unsigned CurHash,
686688

687689
void BranchFolder::RemoveBlocksWithHash(unsigned CurHash,
688690
MachineBasicBlock *SuccBB,
689-
MachineBasicBlock *PredBB) {
691+
MachineBasicBlock *PredBB,
692+
const DebugLoc &BranchDL) {
690693
MPIterator CurMPIter, B;
691694
for (CurMPIter = std::prev(MergePotentials.end()),
692695
B = MergePotentials.begin();
693696
CurMPIter->getHash() == CurHash; --CurMPIter) {
694697
// Put the unconditional branch back, if we need one.
695698
MachineBasicBlock *CurMBB = CurMPIter->getBlock();
696699
if (SuccBB && CurMBB != PredBB)
697-
FixTail(CurMBB, SuccBB, TII);
700+
FixTail(CurMBB, SuccBB, TII, BranchDL);
698701
if (CurMPIter == B)
699702
break;
700703
}
@@ -908,6 +911,7 @@ bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
908911
// Walk through equivalence sets looking for actual exact matches.
909912
while (MergePotentials.size() > 1) {
910913
unsigned CurHash = MergePotentials.back().getHash();
914+
const DebugLoc &BranchDL = MergePotentials.back().getBranchDebugLoc();
911915

912916
// Build SameTails, identifying the set of blocks with this hash code
913917
// and with the maximum number of instructions in common.
@@ -918,7 +922,7 @@ bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
918922
// If we didn't find any pair that has at least MinCommonTailLength
919923
// instructions in common, remove all blocks with this hash code and retry.
920924
if (SameTails.empty()) {
921-
RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
925+
RemoveBlocksWithHash(CurHash, SuccBB, PredBB, BranchDL);
922926
continue;
923927
}
924928

@@ -965,7 +969,7 @@ bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB,
965969
// Split a block so that one does.
966970
if (!CreateCommonTailOnlyBlock(PredBB, SuccBB,
967971
maxCommonTailLength, commonTailIndex)) {
968-
RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
972+
RemoveBlocksWithHash(CurHash, SuccBB, PredBB, BranchDL);
969973
continue;
970974
}
971975
}
@@ -1013,7 +1017,8 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
10131017
if (MergePotentials.size() == TailMergeThreshold)
10141018
break;
10151019
if (!TriedMerging.count(&MBB) && MBB.succ_empty())
1016-
MergePotentials.push_back(MergePotentialsElt(HashEndOfMBB(MBB), &MBB));
1020+
MergePotentials.push_back(MergePotentialsElt(HashEndOfMBB(MBB), &MBB,
1021+
MBB.findBranchDebugLoc()));
10171022
}
10181023

10191024
// If this is a large problem, avoid visiting the same basic blocks
@@ -1115,16 +1120,17 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
11151120
}
11161121

11171122
// Remove the unconditional branch at the end, if any.
1123+
DebugLoc dl = PBB->findBranchDebugLoc();
11181124
if (TBB && (Cond.empty() || FBB)) {
1119-
DebugLoc dl = PBB->findBranchDebugLoc();
11201125
TII->removeBranch(*PBB);
11211126
if (!Cond.empty())
11221127
// reinsert conditional branch only, for now
11231128
TII->insertBranch(*PBB, (TBB == IBB) ? FBB : TBB, nullptr,
11241129
NewCond, dl);
11251130
}
11261131

1127-
MergePotentials.push_back(MergePotentialsElt(HashEndOfMBB(*PBB), PBB));
1132+
MergePotentials.push_back(
1133+
MergePotentialsElt(HashEndOfMBB(*PBB), PBB, dl));
11281134
}
11291135
}
11301136

@@ -1142,7 +1148,8 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
11421148
PredBB = &*std::prev(I); // this may have been changed in TryTailMergeBlocks
11431149
if (MergePotentials.size() == 1 &&
11441150
MergePotentials.begin()->getBlock() != PredBB)
1145-
FixTail(MergePotentials.begin()->getBlock(), IBB, TII);
1151+
FixTail(MergePotentials.begin()->getBlock(), IBB, TII,
1152+
MergePotentials.begin()->getBranchDebugLoc());
11461153
}
11471154

11481155
return MadeChange;

llvm/lib/CodeGen/BranchFolding.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ class TargetRegisterInfo;
5050
class MergePotentialsElt {
5151
unsigned Hash;
5252
MachineBasicBlock *Block;
53+
DebugLoc BranchDebugLoc;
5354

5455
public:
55-
MergePotentialsElt(unsigned h, MachineBasicBlock *b)
56-
: Hash(h), Block(b) {}
56+
MergePotentialsElt(unsigned h, MachineBasicBlock *b, DebugLoc bdl)
57+
: Hash(h), Block(b), BranchDebugLoc(std::move(bdl)) {}
5758

5859
unsigned getHash() const { return Hash; }
5960
MachineBasicBlock *getBlock() const { return Block; }
@@ -62,6 +63,8 @@ class TargetRegisterInfo;
6263
Block = MBB;
6364
}
6465

66+
const DebugLoc &getBranchDebugLoc() { return BranchDebugLoc; }
67+
6568
bool operator<(const MergePotentialsElt &) const;
6669
};
6770

@@ -162,8 +165,9 @@ class TargetRegisterInfo;
162165

163166
/// Remove all blocks with hash CurHash from MergePotentials, restoring
164167
/// branches at ends of blocks as appropriate.
165-
void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
166-
MachineBasicBlock* PredBB);
168+
void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock *SuccBB,
169+
MachineBasicBlock *PredBB,
170+
const DebugLoc &BranchDL);
167171

168172
/// None of the blocks to be tail-merged consist only of the common tail.
169173
/// Create a block that does by splitting one.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -enable-tail-merge=1 -stop-after=branch-folder | FileCheck %s
2+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
3+
target triple = "x86_64-grtev4-linux-gnu"
4+
5+
define i32 @main(i1 %0) {
6+
entry:
7+
br i1 %0, label %1, label %2
8+
9+
1: ; preds = %entry
10+
store i64 1, ptr null, align 1
11+
; CHECK: JMP_1 %bb.3, debug-location !3
12+
br label %3, !dbg !3
13+
14+
2: ; preds = %entry
15+
store i64 0, ptr null, align 1
16+
br label %3
17+
18+
3: ; preds = %2, %1
19+
ret i32 0
20+
}
21+
22+
!llvm.dbg.cu = !{!0}
23+
!llvm.module.flags = !{!2}
24+
25+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, nameTableKind: None)
26+
!1 = !DIFile(filename: "foo.c", directory: "/tmp", checksumkind: CSK_MD5, checksum: "2d07c91bb9d9c2fa4eee31a1aeed20e3")
27+
!2 = !{i32 2, !"Debug Info Version", i32 3}
28+
!3 = !DILocation(line: 17, column: 3, scope: !4)
29+
!4 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 6, type: !5, scopeLine: 6, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
30+
!5 = !DISubroutineType(types: !6)
31+
!6 = !{}

0 commit comments

Comments
 (0)