Skip to content

Commit 738fcbe

Browse files
committed
[SROA] Preserve all GEP flags during speculation
Unlikely to matter in practice, as these GEPs are typically promoted away.
1 parent 4f54b91 commit 738fcbe

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,15 +3981,15 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
39813981
SmallVector<Value *> FalseOps = GetNewOps(False);
39823982

39833983
IRB.SetInsertPoint(&GEPI);
3984-
bool IsInBounds = GEPI.isInBounds();
3984+
GEPNoWrapFlags NW = GEPI.getNoWrapFlags();
39853985

39863986
Type *Ty = GEPI.getSourceElementType();
39873987
Value *NTrue = IRB.CreateGEP(Ty, TrueOps[0], ArrayRef(TrueOps).drop_front(),
3988-
True->getName() + ".sroa.gep", IsInBounds);
3988+
True->getName() + ".sroa.gep", NW);
39893989

39903990
Value *NFalse =
39913991
IRB.CreateGEP(Ty, FalseOps[0], ArrayRef(FalseOps).drop_front(),
3992-
False->getName() + ".sroa.gep", IsInBounds);
3992+
False->getName() + ".sroa.gep", NW);
39933993

39943994
Value *NSel = IRB.CreateSelect(Sel->getCondition(), NTrue, NFalse,
39953995
Sel->getName() + ".sroa.sel");
@@ -4069,7 +4069,6 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
40694069
PHINode *NewPhi = IRB.CreatePHI(GEPI.getType(), Phi->getNumIncomingValues(),
40704070
Phi->getName() + ".sroa.phi");
40714071

4072-
bool IsInBounds = GEPI.isInBounds();
40734072
Type *SourceTy = GEPI.getSourceElementType();
40744073
// We only handle arguments, constants, and static allocas here, so we can
40754074
// insert GEPs at the end of the entry block.
@@ -4084,7 +4083,7 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
40844083
SmallVector<Value *> NewOps = GetNewOps(Op);
40854084
NewGEP =
40864085
IRB.CreateGEP(SourceTy, NewOps[0], ArrayRef(NewOps).drop_front(),
4087-
Phi->getName() + ".sroa.gep", IsInBounds);
4086+
Phi->getName() + ".sroa.gep", GEPI.getNoWrapFlags());
40884087
}
40894088
NewPhi->addIncoming(NewGEP, BB);
40904089
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,35 @@ exit:
363363
ret void
364364
}
365365

366+
define void @test_sroa_gep_phi_select_same_block_nuw(i1 %c1, i1 %c2, ptr %ptr) {
367+
; CHECK-LABEL: @test_sroa_gep_phi_select_same_block_nuw(
368+
; CHECK-NEXT: entry:
369+
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [[PAIR:%.*]], align 8
370+
; CHECK-NEXT: br label [[WHILE_BODY:%.*]]
371+
; CHECK: while.body:
372+
; CHECK-NEXT: [[PHI:%.*]] = phi ptr [ [[ALLOCA]], [[ENTRY:%.*]] ], [ [[SELECT:%.*]], [[WHILE_BODY]] ]
373+
; CHECK-NEXT: [[SELECT]] = select i1 [[C1:%.*]], ptr [[PHI]], ptr [[PTR:%.*]]
374+
; CHECK-NEXT: [[PHI_SROA_GEP:%.*]] = getelementptr nuw [[PAIR]], ptr [[PHI]], i64 1
375+
; CHECK-NEXT: [[PTR_SROA_GEP:%.*]] = getelementptr nuw [[PAIR]], ptr [[PTR]], i64 1
376+
; CHECK-NEXT: [[SELECT_SROA_SEL:%.*]] = select i1 [[C1]], ptr [[PHI_SROA_GEP]], ptr [[PTR_SROA_GEP]]
377+
; CHECK-NEXT: br i1 [[C2:%.*]], label [[EXIT:%.*]], label [[WHILE_BODY]]
378+
; CHECK: exit:
379+
; CHECK-NEXT: ret void
380+
;
381+
entry:
382+
%alloca = alloca %pair, align 8
383+
br label %while.body
384+
385+
while.body:
386+
%phi = phi ptr [ %alloca, %entry ], [ %select, %while.body ]
387+
%select = select i1 %c1, ptr %phi, ptr %ptr
388+
%gep = getelementptr nuw %pair, ptr %select, i64 1
389+
br i1 %c2, label %exit, label %while.body
390+
391+
exit:
392+
ret void
393+
}
394+
366395
define i32 @test_sroa_gep_cast_phi_gep(i1 %cond) {
367396
; CHECK-LABEL: @test_sroa_gep_cast_phi_gep(
368397
; CHECK-NEXT: entry:

0 commit comments

Comments
 (0)