Skip to content

Commit bfd2c21

Browse files
committed
[IR][LoopRotate] avoid leaving phi with no operands (PR48296)
https://llvm.org/PR48296 shows an example where we delete all of the operands of a phi without actually deleting the phi, and that is currently considered invalid IR. The reduced test included here would crash for that reason. A suggested follow-up is to loosen the assert to allow 0-operand phis in unreachable blocks. Differential Revision: https://reviews.llvm.org/D92247
1 parent 234a529 commit bfd2c21

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

llvm/include/llvm/IR/BasicBlock.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
387387
/// Update PHI nodes in this BasicBlock before removal of predecessor \p Pred.
388388
/// Note that this function does not actually remove the predecessor.
389389
///
390-
/// If \p KeepOneInputPHIs is true then don't remove PHIs that are left with
391-
/// zero or one incoming values, and don't simplify PHIs with all incoming
392-
/// values the same.
390+
/// If \p KeepOneInputPHIs is true, then don't remove PHIs that are left with
391+
/// one incoming value and don't simplify PHIs with all incoming values the
392+
/// same.
393393
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs = false);
394394

395395
bool canSplitPredecessors() const;

llvm/lib/IR/BasicBlock.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ void BasicBlock::removePredecessor(BasicBlock *Pred,
330330

331331
unsigned NumPreds = cast<PHINode>(front()).getNumIncomingValues();
332332
for (PHINode &Phi : make_early_inc_range(phis())) {
333-
Phi.removeIncomingValue(Pred, !KeepOneInputPHIs);
333+
Phi.removeIncomingValue(Pred);
334334
if (KeepOneInputPHIs)
335335
continue;
336336
// If we have a single predecessor, removeIncomingValue erased the PHI
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -S -lcssa -loop-rotate < %s | FileCheck %s
3+
4+
define void @PR48296(i1 %cond) {
5+
; CHECK-LABEL: @PR48296(
6+
; CHECK-NEXT: entry:
7+
; CHECK-NEXT: br label [[LOOP:%.*]]
8+
; CHECK: loop:
9+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[INC:%.*]], label [[LOOP_BACKEDGE:%.*]]
10+
; CHECK: loop.backedge:
11+
; CHECK-NEXT: br label [[LOOP]]
12+
; CHECK: dead:
13+
; CHECK-NEXT: unreachable
14+
; CHECK: inc:
15+
; CHECK-NEXT: br label [[LOOP_BACKEDGE]]
16+
; CHECK: return:
17+
; CHECK-NEXT: ret void
18+
;
19+
entry:
20+
br label %loop
21+
22+
loop:
23+
br i1 %cond, label %inc, label %loop
24+
25+
dead: ; No predecessors!
26+
br i1 %cond, label %inc, label %return
27+
28+
inc:
29+
br label %loop
30+
31+
return:
32+
%r = phi i32 [ undef, %dead ]
33+
ret void
34+
}

0 commit comments

Comments
 (0)