Skip to content

Commit 958c0d7

Browse files
committed
[KeyInstr][SimplifyCFG] Remap atoms when folding br to common succ into pred
SimplifyCFG folds `b` into `a`. +-----------+ | v --> a --> b --> c --> d --> | ^ +-----------------+ Remap source atoms in `b` so that the duplicated instructions are analysed independently to determine is_stmt positions. This is necessary as the contents of `b` may be folded into multiple preds in this way. Add multi-pred test.
1 parent c89fa2b commit 958c0d7

File tree

3 files changed

+116
-7
lines changed

3 files changed

+116
-7
lines changed

llvm/include/llvm/IR/DebugLoc.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ namespace llvm {
7676
LLVMContext &Ctx,
7777
DenseMap<const MDNode *, MDNode *> &Cache);
7878

79+
/// Return true if the source locations match, ignoring isImplicitCode and
80+
/// source atom info.
81+
bool isSameSourceLocation(const DebugLoc &Other) const {
82+
if (get() == Other.get())
83+
return true;
84+
return ((bool)*this == (bool)Other) && getLine() == Other.getLine() &&
85+
getCol() == Other.getCol() && getScope() == Other.getScope() &&
86+
getInlinedAt() == Other.getInlinedAt();
87+
}
88+
7989
unsigned getLine() const;
8090
unsigned getCol() const;
8191
MDNode *getScope() const;

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "llvm/Support/MathExtras.h"
7474
#include "llvm/Support/raw_ostream.h"
7575
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
76+
#include "llvm/Transforms/Utils/Cloning.h"
7677
#include "llvm/Transforms/Utils/Local.h"
7778
#include "llvm/Transforms/Utils/LockstepReverseIterator.h"
7879
#include "llvm/Transforms/Utils/ValueMapper.h"
@@ -1129,13 +1130,17 @@ static void cloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(
11291130

11301131
Instruction *NewBonusInst = BonusInst.clone();
11311132

1132-
if (!isa<DbgInfoIntrinsic>(BonusInst) &&
1133-
PTI->getDebugLoc() != NewBonusInst->getDebugLoc()) {
1134-
// Unless the instruction has the same !dbg location as the original
1135-
// branch, drop it. When we fold the bonus instructions we want to make
1136-
// sure we reset their debug locations in order to avoid stepping on
1137-
// dead code caused by folding dead branches.
1138-
NewBonusInst->setDebugLoc(DebugLoc());
1133+
if (!isa<DbgInfoIntrinsic>(BonusInst)) {
1134+
if (!NewBonusInst->getDebugLoc().isSameSourceLocation(
1135+
PTI->getDebugLoc())) {
1136+
// Unless the instruction has the same !dbg location as the original
1137+
// branch, drop it. When we fold the bonus instructions we want to make
1138+
// sure we reset their debug locations in order to avoid stepping on
1139+
// dead code caused by folding dead branches.
1140+
NewBonusInst->setDebugLoc(DebugLoc());
1141+
} else if (const DebugLoc &DL = NewBonusInst->getDebugLoc()) {
1142+
mapAtomInstance(DL, VMap);
1143+
}
11391144
}
11401145

11411146
RemapInstruction(NewBonusInst, VMap,
@@ -1182,6 +1187,19 @@ static void cloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(
11821187
U.set(NewBonusInst);
11831188
}
11841189
}
1190+
1191+
// Key Instructions: We may have propagated atom info into the pred. If the
1192+
// pred's terminator already has atom info do nothing as merging would drop
1193+
// one atom group anyway. If it doesn't, propagte the remapped atom group
1194+
// from BB's terminator.
1195+
if (auto &PredDL = PredBlock->getTerminator()->getDebugLoc()) {
1196+
auto &DL = BB->getTerminator()->getDebugLoc();
1197+
if (!PredDL->getAtomGroup() && DL && DL->getAtomGroup() &&
1198+
PredDL.isSameSourceLocation(DL)) {
1199+
PredBlock->getTerminator()->setDebugLoc(DL);
1200+
RemapSourceAtom(PredBlock->getTerminator(), VMap);
1201+
}
1202+
}
11851203
}
11861204

11871205
bool SimplifyCFGOpt::performValueComparisonIntoPredecessorFolding(
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt %s -S -passes=simplifycfg -bonus-inst-threshold=2 | FileCheck %s
3+
4+
;; Block d gets folded into preds b and c. Check the cloned instructions get
5+
;; remapped DILocation atomGroup numbers in each of the preds. Additionally
6+
;; check that the branches each inherit the atomGroup of the folded branch.
7+
8+
declare i32 @g(...)
9+
define void @f(i1 %c0, i1 %c1, i1 %c2, i32 %x, i32 %y) !dbg !17 {
10+
; CHECK-LABEL: define void @f(
11+
; CHECK-SAME: i1 [[C0:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]]) !dbg [[DBG6:![0-9]+]] {
12+
; CHECK-NEXT: [[A:.*:]]
13+
; CHECK-NEXT: br i1 [[C0]], label %[[B:.*]], label %[[C:.*]]
14+
; CHECK: [[B]]:
15+
; CHECK-NEXT: [[AND_OLD:%.*]] = and i32 [[X]], [[Y]], !dbg [[DBG8:![0-9]+]]
16+
; CHECK-NEXT: [[CMP_OLD:%.*]] = icmp eq i32 [[AND_OLD]], 0, !dbg [[DBG9:![0-9]+]]
17+
; CHECK-NEXT: [[OR_COND1:%.*]] = select i1 [[C1]], i1 true, i1 [[CMP_OLD]], !dbg [[DBG10:![0-9]+]]
18+
; CHECK-NEXT: br i1 [[OR_COND1]], label %[[F:.*]], label %[[E:.*]], !dbg [[DBG11:![0-9]+]]
19+
; CHECK: [[C]]:
20+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[X]], [[Y]], !dbg [[DBG12:![0-9]+]]
21+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0, !dbg [[DBG13:![0-9]+]]
22+
; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[C2]], i1 true, i1 [[CMP]], !dbg [[DBG10]]
23+
; CHECK-NEXT: br i1 [[OR_COND]], label %[[F]], label %[[E]], !dbg [[DBG14:![0-9]+]]
24+
; CHECK: [[E]]:
25+
; CHECK-NEXT: [[TMP0:%.*]] = tail call i32 (...) @g()
26+
; CHECK-NEXT: br label %[[F]]
27+
; CHECK: [[F]]:
28+
; CHECK-NEXT: ret void
29+
;
30+
a:
31+
br i1 %c0, label %b, label %c
32+
33+
b:
34+
br i1 %c1, label %f, label %d, !dbg !18
35+
36+
c:
37+
br i1 %c2, label %f, label %d, !dbg !18
38+
39+
d:
40+
%and = and i32 %x, %y, !dbg !19
41+
%cmp = icmp eq i32 %and, 0, !dbg !20
42+
br i1 %cmp, label %f, label %e, !dbg !21
43+
44+
e:
45+
%7 = tail call i32 (...) @g()
46+
br label %f
47+
48+
f:
49+
ret void
50+
}
51+
52+
!llvm.dbg.cu = !{!0}
53+
!llvm.debugify = !{!3, !4}
54+
!llvm.module.flags = !{!5}
55+
56+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
57+
!1 = !DIFile(filename: "a.ll", directory: "/")
58+
!2 = !{}
59+
!3 = !{i32 9}
60+
!4 = !{i32 0}
61+
!5 = !{i32 2, !"Debug Info Version", i32 3}
62+
!7 = !DISubroutineType(types: !2)
63+
!17 = distinct !DISubprogram(name: "f", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
64+
!18 = !DILocation(line: 10, column: 10, scope: !17)
65+
!19 = !DILocation(line: 10, column: 10, scope: !17, atomGroup: 1, atomRank: 2)
66+
!20 = !DILocation(line: 10, column: 10, scope: !17, atomGroup: 2, atomRank: 2)
67+
!21 = !DILocation(line: 10, column: 10, scope: !17, atomGroup: 2, atomRank: 1)
68+
;.
69+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C, file: [[META1:![0-9]+]], producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: [[META2:![0-9]+]])
70+
; CHECK: [[META1]] = !DIFile(filename: "a.ll", directory: {{.*}})
71+
; CHECK: [[META2]] = !{}
72+
; CHECK: [[DBG6]] = distinct !DISubprogram(name: "f", scope: null, file: [[META1]], line: 1, type: [[META7:![0-9]+]], scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META2]])
73+
; CHECK: [[META7]] = !DISubroutineType(types: [[META2]])
74+
; CHECK: [[DBG8]] = !DILocation(line: 10, column: 10, scope: [[DBG6]], atomGroup: 5, atomRank: 2)
75+
; CHECK: [[DBG9]] = !DILocation(line: 10, column: 10, scope: [[DBG6]], atomGroup: 6, atomRank: 2)
76+
; CHECK: [[DBG10]] = !DILocation(line: 10, column: 10, scope: [[DBG6]])
77+
; CHECK: [[DBG11]] = !DILocation(line: 10, column: 10, scope: [[DBG6]], atomGroup: 6, atomRank: 1)
78+
; CHECK: [[DBG12]] = !DILocation(line: 10, column: 10, scope: [[DBG6]], atomGroup: 3, atomRank: 2)
79+
; CHECK: [[DBG13]] = !DILocation(line: 10, column: 10, scope: [[DBG6]], atomGroup: 4, atomRank: 2)
80+
; CHECK: [[DBG14]] = !DILocation(line: 10, column: 10, scope: [[DBG6]], atomGroup: 4, atomRank: 1)
81+
;.

0 commit comments

Comments
 (0)