Skip to content

Commit a5b2e43

Browse files
dtcxzywtstellar
authored andcommitted
[FlattenCFG] Fix the miscompilation where phi nodes exist in the merge point (#81987)
When there are phi nodes in the merge point of the if-region, we cannot do the merge. Alive2: https://alive2.llvm.org/ce/z/DbgEan Fixes #70900. (cherry picked from commit f920b74)
1 parent 3aea3d2 commit a5b2e43

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

llvm/lib/Transforms/Utils/FlattenCFG.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Block1, BasicBlock *Block2,
407407
/// form, by inverting the condition and the branch successors. The same
408408
/// approach goes for the opposite case.
409409
bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
410+
// We cannot merge the if-region if the merge point has phi nodes.
411+
if (isa<PHINode>(BB->front()))
412+
return false;
413+
410414
BasicBlock *IfTrue2, *IfFalse2;
411415
BranchInst *DomBI2 = GetIfCondition(BB, IfTrue2, IfFalse2);
412416
if (!DomBI2)
@@ -493,16 +497,6 @@ bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
493497
PBI->replaceUsesOfWith(PBI->getCondition(), NC);
494498
Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
495499

496-
// Handle PHI node to replace its predecessors to FirstEntryBlock.
497-
for (BasicBlock *Succ : successors(PBI)) {
498-
for (PHINode &Phi : Succ->phis()) {
499-
for (unsigned i = 0, e = Phi.getNumIncomingValues(); i != e; ++i) {
500-
if (Phi.getIncomingBlock(i) == SecondEntryBlock)
501-
Phi.setIncomingBlock(i, FirstEntryBlock);
502-
}
503-
}
504-
}
505-
506500
// Remove IfTrue1
507501
if (IfTrue1 != FirstEntryBlock) {
508502
IfTrue1->dropAllReferences();

llvm/test/Transforms/Util/flattencfg.ll

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,16 @@ define void @test_not_crash3(i32 %a) #0 {
7777
; CHECK-SAME: (i32 [[A:%.*]]) {
7878
; CHECK-NEXT: entry:
7979
; CHECK-NEXT: [[A_EQ_0:%.*]] = icmp eq i32 [[A]], 0
80+
; CHECK-NEXT: br i1 [[A_EQ_0]], label [[BB0:%.*]], label [[BB1:%.*]]
81+
; CHECK: bb0:
82+
; CHECK-NEXT: br label [[BB1]]
83+
; CHECK: bb1:
8084
; CHECK-NEXT: [[A_EQ_1:%.*]] = icmp eq i32 [[A]], 1
81-
; CHECK-NEXT: [[TMP0:%.*]] = or i1 [[A_EQ_0]], [[A_EQ_1]]
82-
; CHECK-NEXT: br i1 [[TMP0]], label [[BB2:%.*]], label [[BB3:%.*]]
85+
; CHECK-NEXT: br i1 [[A_EQ_1]], label [[BB2:%.*]], label [[BB3:%.*]]
8386
; CHECK: bb2:
8487
; CHECK-NEXT: br label [[BB3]]
8588
; CHECK: bb3:
86-
; CHECK-NEXT: [[CHECK_BADREF:%.*]] = phi i32 [ 17, [[ENTRY:%.*]] ], [ 11, [[BB2]] ]
89+
; CHECK-NEXT: [[CHECK_BADREF:%.*]] = phi i32 [ 17, [[BB1]] ], [ 11, [[BB2]] ]
8790
; CHECK-NEXT: ret void
8891
;
8992
entry:
@@ -278,9 +281,9 @@ define i1 @test_cond_multi_use(i32 %x, i32 %y, i32 %z) {
278281
; CHECK-NEXT: entry.x:
279282
; CHECK-NEXT: [[CMP_X:%.*]] = icmp ne i32 [[X]], 0
280283
; CHECK-NEXT: [[CMP_Y:%.*]] = icmp eq i32 [[Y]], 0
281-
; CHECK-NEXT: [[TMP0:%.*]] = xor i1 [[CMP_Y]], true
282-
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[CMP_X]], [[TMP0]]
283-
; CHECK-NEXT: br i1 [[TMP1]], label [[IF_THEN_Y:%.*]], label [[EXIT:%.*]]
284+
; CHECK-NEXT: [[CMP_Y_NOT:%.*]] = xor i1 [[CMP_Y]], true
285+
; CHECK-NEXT: [[TMP0:%.*]] = or i1 [[CMP_X]], [[CMP_Y_NOT]]
286+
; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN_Y:%.*]], label [[EXIT:%.*]]
284287
; CHECK: if.then.y:
285288
; CHECK-NEXT: store i32 [[Z]], ptr @g, align 4
286289
; CHECK-NEXT: br label [[EXIT]]

0 commit comments

Comments
 (0)