Skip to content

Commit 8e73162

Browse files
committed
[BasicBlockUtils] Never emit PHIs that are known to be undef
There are (rare) cases we used to emit a PHI node with only `undef` inputs, that's not helpful.
1 parent f2a3e2b commit 8e73162

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,22 +1909,29 @@ static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,
19091909
auto NewPhi =
19101910
PHINode::Create(Phi->getType(), Incoming.size(),
19111911
Phi->getName() + ".moved", FirstGuardBlock->begin());
1912+
bool AllUndef = true;
19121913
for (auto *In : Incoming) {
19131914
Value *V = UndefValue::get(Phi->getType());
19141915
if (In == Out) {
19151916
V = NewPhi;
19161917
} else if (Phi->getBasicBlockIndex(In) != -1) {
19171918
V = Phi->removeIncomingValue(In, false);
1919+
AllUndef &= isa<UndefValue>(V);
19181920
}
19191921
NewPhi->addIncoming(V, In);
19201922
}
19211923
assert(NewPhi->getNumIncomingValues() == Incoming.size());
1924+
Value *NewV = NewPhi;
1925+
if (AllUndef) {
1926+
NewPhi->eraseFromParent();
1927+
NewV = UndefValue::get(Phi->getType());
1928+
}
19221929
if (Phi->getNumOperands() == 0) {
1923-
Phi->replaceAllUsesWith(NewPhi);
1930+
Phi->replaceAllUsesWith(NewV);
19241931
I = Phi->eraseFromParent();
19251932
continue;
19261933
}
1927-
Phi->addIncoming(NewPhi, GuardBlock);
1934+
Phi->addIncoming(NewV, GuardBlock);
19281935
++I;
19291936
}
19301937
}

llvm/test/Transforms/UnifyLoopExits/undef-phis.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ define fastcc void @undef_phi(i64 %i5247, i1 %i4530, i1 %i4936.not) {
2323
; CHECK-NEXT: store volatile [2 x i32] [[I5293]], ptr addrspace(5) null, align 4
2424
; CHECK-NEXT: ret void
2525
; CHECK: [[LOOP_EXIT_GUARD]]:
26-
; CHECK-NEXT: [[DOTMOVED]] = phi i32 [ [[TMP0]], %[[MBB4321]] ], [ [[DOTMOVED_MOVED:%.*]], %[[LOOP_EXIT_GUARD1]] ]
26+
; CHECK-NEXT: [[DOTMOVED]] = phi i32 [ [[TMP0]], %[[MBB4321]] ], [ undef, %[[LOOP_EXIT_GUARD1]] ]
2727
; CHECK-NEXT: [[GUARD_MBB4531:%.*]] = phi i1 [ false, %[[MBB4321]] ], [ [[GUARD_MBB4531_MOVED:%.*]], %[[LOOP_EXIT_GUARD1]] ]
2828
; CHECK-NEXT: br i1 [[GUARD_MBB4531]], label %[[MBB4531]], label %[[MBB5291]]
2929
; CHECK: [[LOOP_EXIT_GUARD1]]:
3030
; CHECK-NEXT: [[GUARD_MBB4531_MOVED]] = phi i1 [ true, %[[MBB4454]] ], [ undef, %[[MBB4535]] ]
31-
; CHECK-NEXT: [[DOTMOVED_MOVED]] = phi i32 [ poison, %[[MBB4454]] ], [ undef, %[[MBB4535]] ]
3231
; CHECK-NEXT: [[GUARD_LOOP_EXIT_GUARD:%.*]] = phi i1 [ true, %[[MBB4454]] ], [ false, %[[MBB4535]] ]
3332
; CHECK-NEXT: br i1 [[GUARD_LOOP_EXIT_GUARD]], label %[[LOOP_EXIT_GUARD]], label %[[MBB4321]]
3433
;

0 commit comments

Comments
 (0)