Skip to content

Commit 6488f4b

Browse files
committed
[KeyInstr][JumpThreading] Remap atoms duping bb with cond br on phi into pred
See test for details.
1 parent 272f806 commit 6488f4b

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

llvm/lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,6 +2683,9 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
26832683
// PredBB block. Evaluate PHI nodes in BB.
26842684
ValueToValueMapTy ValueMapping;
26852685

2686+
// Remember the position before the inserted instructions.
2687+
auto RItBeforeInsertPt = std::next(OldPredBranch->getReverseIterator());
2688+
26862689
BasicBlock::iterator BI = BB->begin();
26872690
for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
26882691
ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
@@ -2702,6 +2705,8 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
27022705

27032706
// Remap debug variable operands.
27042707
remapDebugVariable(ValueMapping, New);
2708+
if (const DebugLoc &DL = New->getDebugLoc())
2709+
mapAtomInstance(DL, ValueMapping);
27052710

27062711
// If this instruction can be simplified after the operands are updated,
27072712
// just use the simplified value instead. This frequently happens due to
@@ -2740,6 +2745,10 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
27402745
addPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB,
27412746
ValueMapping);
27422747

2748+
// KeyInstructions: Remap the cloned instructions' atoms only.
2749+
remapSourceAtoms(ValueMapping, std::prev(RItBeforeInsertPt)->getIterator(),
2750+
OldPredBranch->getIterator());
2751+
27432752
updateSSA(BB, PredBB, ValueMapping);
27442753

27452754
// PredBB no longer jumps to BB, remove entries in the PHI node for the edge
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
; RUN: opt %s --passes=jump-threading -S -o - -S | FileCheck %s
2+
3+
;; +-> T1 -+
4+
;; | v +-> T2
5+
;; Entry -+ Merge -+
6+
;; | ^ +-> F2
7+
;; +-> F1 -+
8+
;;
9+
;; Duplicate Merge into T1 then fold Merge into its only pred F1 (taking its name).
10+
;;
11+
;; +-> T1 -----> T2
12+
;; | \ ^
13+
;; | \ /
14+
;; | \ /
15+
;; Entry -+ +----+
16+
;; | / v
17+
;; +--> Merge -> F2
18+
;;
19+
;; Check the duplicated (into T1) instructions' atoms are remapped.
20+
21+
; CHECK: T1:
22+
; CHECK-NEXT: %v1 = call i32 @f1()
23+
; CHECK-NEXT: %cond3 = icmp eq i32 %v1, 412
24+
; CHECK-NEXT: %C1 = add i32 %v1, 1, !dbg [[G3R2:!.*]]
25+
; CHECK-NEXT: store i32 %C1, ptr %p, align 4, !dbg [[G3R1:!.*]]
26+
27+
; CHECK: Merge:
28+
; CHECK-NEXT: %v2 = call i32 @f2()
29+
; CHECK-NEXT: store i32 1, ptr %p, align 4, !dbg [[G1R1:!.*]]
30+
; CHECK-NEXT: %C = add i32 %v2, 1, !dbg [[G2R2:!.*]]
31+
; CHECK-NEXT: store i32 %C, ptr %p, align 4, !dbg [[G2R1:!.*]]
32+
33+
; CHECK: [[G3R2]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
34+
; CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
35+
; CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
36+
; CHECK: [[G2R2]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
37+
; CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
38+
39+
define i32 @test5(i1 %cond, i1 %cond2, ptr %p) !dbg !5 {
40+
br i1 %cond, label %T1, label %F1
41+
42+
T1: ; preds = %0
43+
%v1 = call i32 @f1()
44+
%cond3 = icmp eq i32 %v1, 412
45+
br label %Merge
46+
47+
F1: ; preds = %0
48+
%v2 = call i32 @f2()
49+
store i32 1, ptr %p, align 4, !dbg !8
50+
br label %Merge
51+
52+
Merge: ; preds = %F1, %T1
53+
%A = phi i1 [ %cond3, %T1 ], [ %cond2, %F1 ]
54+
%B = phi i32 [ %v1, %T1 ], [ %v2, %F1 ]
55+
%C = add i32 %B, 1, !dbg !9
56+
store i32 %C, ptr %p, align 4, !dbg !10
57+
br i1 %A, label %T2, label %F2
58+
59+
T2: ; preds = %Merge
60+
call void @f3()
61+
ret i32 %B
62+
63+
F2: ; preds = %Merge
64+
ret i32 %B
65+
}
66+
67+
declare i32 @f1()
68+
69+
declare i32 @f2()
70+
71+
declare void @f3()
72+
73+
!llvm.dbg.cu = !{!0}
74+
!llvm.debugify = !{!2, !3}
75+
!llvm.module.flags = !{!4}
76+
77+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
78+
!1 = !DIFile(filename: "test.ll", directory: "/")
79+
!2 = !{i32 12}
80+
!3 = !{i32 0}
81+
!4 = !{i32 2, !"Debug Info Version", i32 3}
82+
!5 = distinct !DISubprogram(name: "test5", linkageName: "test5", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
83+
!6 = !DISubroutineType(types: !7)
84+
!7 = !{}
85+
!8 = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 1, atomRank: 1)
86+
!9 = !DILocation(line: 2, column: 1, scope: !5, atomGroup: 2, atomRank: 2)
87+
!10 = !DILocation(line: 2, column: 1, scope: !5, atomGroup: 2, atomRank: 1)

0 commit comments

Comments
 (0)