Skip to content

Commit d98cb81

Browse files
committed
Handle successor's PHI node correctly when flattening CFG merges two if-regions
Summary: FlattenCFG merges two 'if' basicblocks by inserting one basicblock to another basicblock. The inserted basicblock can have a successor that contains a PHI node whoes incoming basicblock is the inserted basicblock. Since the existing code does not handle it, it becomes a badref. if (cond1) statement if (cond2) statement successor - contains PHI node whose predecessor is cond2 --> if (cond1 || cond2) statement (BB for cond2 was deleted) successor - contains PHI node whose predecessor is cond2 --> bad ref! Author: Jaebaek Seo Reviewers: asbirlea, kuhar, tstellar, chandlerc, davide, dexonsmith Reviewed By: kuhar Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D68032 llvm-svn: 372989
1 parent df3af00 commit d98cb81

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

llvm/lib/Transforms/Utils/FlattenCFG.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FlattenCFGOpt {
6767
/// Before:
6868
/// ......
6969
/// %cmp10 = fcmp une float %tmp1, %tmp2
70-
/// br i1 %cmp1, label %if.then, label %lor.rhs
70+
/// br i1 %cmp10, label %if.then, label %lor.rhs
7171
///
7272
/// lor.rhs:
7373
/// ......
@@ -453,6 +453,16 @@ bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
453453
PBI->replaceUsesOfWith(CC, NC);
454454
Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
455455

456+
// Handle PHI node to replace its predecessors to FirstEntryBlock.
457+
for (BasicBlock *Succ : successors(PBI)) {
458+
for (PHINode &Phi : Succ->phis()) {
459+
for (unsigned i = 0, e = Phi.getNumIncomingValues(); i != e; ++i) {
460+
if (Phi.getIncomingBlock(i) == SecondEntryBlock)
461+
Phi.setIncomingBlock(i, FirstEntryBlock);
462+
}
463+
}
464+
}
465+
456466
// Remove IfTrue1
457467
if (IfTrue1 != FirstEntryBlock) {
458468
IfTrue1->dropAllReferences();

llvm/test/Transforms/Util/flattencfg.ll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,33 @@ bb0: ; preds = %bb1, %entry
5454
br i1 %1, label %bb4, label %bb3
5555
}
5656

57+
; CHECK-LABEL: @test_not_crash3
58+
; CHECK-NEXT: entry:
59+
; CHECK-NEXT: %a_eq_0 = icmp eq i32 %a, 0
60+
; CHECK-NEXT: %a_eq_1 = icmp eq i32 %a, 1
61+
; CHECK-NEXT: [[COND:%[a-z0-9]+]] = or i1 %a_eq_0, %a_eq_1
62+
; CHECK-NEXT: br i1 [[COND]], label %bb2, label %bb3
63+
; CHECK: bb2:
64+
; CHECK-NEXT: br label %bb3
65+
; CHECK: bb3:
66+
; CHECK-NEXT: %check_badref = phi i32 [ 17, %entry ], [ 11, %bb2 ]
67+
; CHECK-NEXT: ret void
68+
define void @test_not_crash3(i32 %a) #0 {
69+
entry:
70+
%a_eq_0 = icmp eq i32 %a, 0
71+
br i1 %a_eq_0, label %bb0, label %bb1
72+
73+
bb0: ; preds = %entry
74+
br label %bb1
75+
76+
bb1: ; preds = %bb0, %entry
77+
%a_eq_1 = icmp eq i32 %a, 1
78+
br i1 %a_eq_1, label %bb2, label %bb3
79+
80+
bb2: ; preds = %bb1
81+
br label %bb3
82+
83+
bb3: ; preds = %bb2, %bb1
84+
%check_badref = phi i32 [ 17, %bb1 ], [ 11, %bb2 ]
85+
ret void
86+
}

0 commit comments

Comments
 (0)