Skip to content

Commit eae4f56

Browse files
committed
[SROA] Fix phi gep unfolding with an alloca not in entry block
Fixes a crash reported in #83494.
1 parent a331937 commit eae4f56

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,10 +4062,14 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
40624062
bool unfoldGEPPhi(GetElementPtrInst &GEPI) {
40634063
// To prevent infinitely expanding recursive phis, bail if the GEP pointer
40644064
// operand (looking through the phi if it is the phi we want to unfold) is
4065-
// an instruction besides an alloca.
4065+
// an instruction besides a static alloca.
40664066
PHINode *Phi = dyn_cast<PHINode>(GEPI.getPointerOperand());
40674067
auto IsInvalidPointerOperand = [](Value *V) {
4068-
return isa<Instruction>(V) && !isa<AllocaInst>(V);
4068+
if (!isa<Instruction>(V))
4069+
return false;
4070+
if (auto *AI = dyn_cast<AllocaInst>(V))
4071+
return !AI->isStaticAlloca();
4072+
return true;
40694073
};
40704074
if (Phi) {
40714075
if (any_of(Phi->operands(), IsInvalidPointerOperand))

llvm/test/Transforms/SROA/phi-gep.ll

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,41 @@ bb3:
638638
ret i1 %icmp
639639
}
640640

641+
define i32 @test_phi_mem2reg_alloca_not_in_entry_block(i1 %arg) {
642+
; CHECK-LABEL: @test_phi_mem2reg_alloca_not_in_entry_block(
643+
; CHECK-NEXT: bb:
644+
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca i64, align 8
645+
; CHECK-NEXT: store i64 123, ptr [[ALLOCA]], align 4
646+
; CHECK-NEXT: br label [[BB2:%.*]]
647+
; CHECK: bb2:
648+
; CHECK-NEXT: [[ALLOCA2:%.*]] = alloca i64, align 8
649+
; CHECK-NEXT: store i64 124, ptr [[ALLOCA]], align 4
650+
; CHECK-NEXT: br i1 [[ARG:%.*]], label [[BB3:%.*]], label [[BB4:%.*]]
651+
; CHECK: bb3:
652+
; CHECK-NEXT: br label [[BB4]]
653+
; CHECK: bb4:
654+
; CHECK-NEXT: [[PHI:%.*]] = phi ptr [ [[ALLOCA]], [[BB2]] ], [ [[ALLOCA2]], [[BB3]] ]
655+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[PHI]], i64 1
656+
; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
657+
; CHECK-NEXT: ret i32 [[LOAD]]
658+
;
659+
bb:
660+
%alloca = alloca i64
661+
store i64 123, ptr %alloca
662+
br label %bb2
663+
bb2:
664+
%alloca2 = alloca i64
665+
store i64 124, ptr %alloca
666+
br i1 %arg, label %bb3, label %bb4
667+
bb3:
668+
br label %bb4
669+
bb4:
670+
%phi = phi ptr [ %alloca, %bb2 ], [ %alloca2, %bb3 ]
671+
%gep = getelementptr i32, ptr %phi, i64 1
672+
%load = load i32, ptr %gep
673+
ret i32 %load
674+
}
675+
641676
define i64 @test_unfold_phi_duplicate_phi_entry(ptr %arg, i8 %arg1, i1 %arg2) {
642677
; CHECK-LABEL: @test_unfold_phi_duplicate_phi_entry(
643678
; CHECK-NEXT: bb:

0 commit comments

Comments
 (0)