Skip to content

Commit 74c3025

Browse files
authored
[KeyInstr][SimplifyCFG] Remap atoms after duplication for threading (#133484)
Given the same branch condition in `a` and `c` SimplifyCFG converts: +> b -+ | v --> a --> c --> e --> | ^ +> d -+ into: +--> bcd ---+ | v --> a --> c --> e --> Remap source atoms on instructions duplicated from `c` into `bcd`. RFC: https://discourse.llvm.org/t/rfc-improving-is-stmt-placement-for-better-interactive-debugging/82668
1 parent 0d0eed4 commit 74c3025

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,7 +3590,7 @@ foldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI, DomTreeUpdater *DTU,
35903590
// instructions into EdgeBB. We know that there will be no uses of the
35913591
// cloned instructions outside of EdgeBB.
35923592
BasicBlock::iterator InsertPt = EdgeBB->getFirstInsertionPt();
3593-
DenseMap<Value *, Value *> TranslateMap; // Track translated values.
3593+
ValueToValueMapTy TranslateMap; // Track translated values.
35943594
TranslateMap[Cond] = CB;
35953595

35963596
// RemoveDIs: track instructions that we optimise away while folding, so
@@ -3610,11 +3610,11 @@ foldCondBranchOnValueKnownInPredecessorImpl(BranchInst *BI, DomTreeUpdater *DTU,
36103610
N->setName(BBI->getName() + ".c");
36113611

36123612
// Update operands due to translation.
3613-
for (Use &Op : N->operands()) {
3614-
DenseMap<Value *, Value *>::iterator PI = TranslateMap.find(Op);
3615-
if (PI != TranslateMap.end())
3616-
Op = PI->second;
3617-
}
3613+
// Key Instructions: Remap all the atom groups.
3614+
if (const DebugLoc &DL = BBI->getDebugLoc())
3615+
mapAtomInstance(DL, TranslateMap);
3616+
RemapInstruction(N, TranslateMap,
3617+
RF_IgnoreMissingLocals | RF_NoModuleLevelChanges);
36183618

36193619
// Check for trivial simplification.
36203620
if (Value *V = simplifyInstruction(N, {DL, nullptr, nullptr, AC})) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; RUN: opt %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S \
2+
; RUN: | FileCheck %s
3+
4+
;; Generated using:
5+
;; opt -passes=debugify --debugify-atoms --debugify-level=locations \
6+
;; llvm/test/Transforms/SimplifyCFG/debug-info-thread-phi.ll
7+
;; With unused/untested metadata nodes removed.
8+
9+
;; Check the duplicated store gets distinct atom info in each branch.
10+
11+
; CHECK-LABEL: @bar(
12+
; CHECK: if.then:
13+
; CHECK: store i32 1{{.*}}, !dbg [[DBG1:!.*]]
14+
; CHECK: if.end.1.critedge:
15+
; CHECK: store i32 1{{.*}}, !dbg [[DBG2:!.*]]
16+
; CHECK: [[DBG1]] = !DILocation(line: 1{{.*}}, atomGroup: 1
17+
; CHECK: [[DBG2]] = !DILocation(line: 1{{.*}}, atomGroup: 2
18+
19+
define void @bar(i32 %aa) !dbg !5 {
20+
entry:
21+
%aa.addr = alloca i32, align 4
22+
%bb = alloca i32, align 4
23+
store i32 %aa, ptr %aa.addr, align 4
24+
store i32 0, ptr %bb, align 4
25+
%tobool = icmp ne i32 %aa, 0
26+
br i1 %tobool, label %if.then, label %if.end
27+
28+
if.then: ; preds = %entry
29+
call void @foo()
30+
br label %if.end
31+
32+
if.end: ; preds = %if.then, %entry
33+
store i32 1, ptr %bb, align 4, !dbg !8
34+
br i1 %tobool, label %if.then.1, label %if.end.1
35+
36+
if.then.1: ; preds = %if.end
37+
call void @foo()
38+
br label %if.end.1
39+
40+
if.end.1: ; preds = %if.then.1, %if.end
41+
store i32 2, ptr %bb, align 4
42+
br label %for.end
43+
44+
for.end: ; preds = %if.end.1
45+
ret void
46+
}
47+
48+
declare void @foo()
49+
50+
!llvm.dbg.cu = !{!0}
51+
!llvm.debugify = !{!2, !3}
52+
!llvm.module.flags = !{!4}
53+
54+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
55+
!1 = !DIFile(filename: "llvm/test/Transforms/SimplifyCFG/debug-info-thread-phi.ll", directory: "/")
56+
!2 = !{i32 15}
57+
!3 = !{i32 0}
58+
!4 = !{i32 2, !"Debug Info Version", i32 3}
59+
!5 = distinct !DISubprogram(name: "bar", linkageName: "bar", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
60+
!6 = !DISubroutineType(types: !7)
61+
!7 = !{}
62+
!8 = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 1, atomRank: 1)

0 commit comments

Comments
 (0)