Skip to content

Commit 9e6625d

Browse files
committed
[InstCombine] Preserve all gep flags in another select of gep fold
1 parent 6467b49 commit 9e6625d

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3712,9 +3712,8 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
37123712
std::swap(NewT, NewF);
37133713
Value *NewSI =
37143714
Builder.CreateSelect(CondVal, NewT, NewF, SI.getName() + ".idx", &SI);
3715-
if (Gep->isInBounds())
3716-
return GetElementPtrInst::CreateInBounds(ElementType, Ptr, {NewSI});
3717-
return GetElementPtrInst::Create(ElementType, Ptr, {NewSI});
3715+
return GetElementPtrInst::Create(ElementType, Ptr, NewSI,
3716+
Gep->getNoWrapFlags());
37183717
};
37193718
if (auto *TrueGep = dyn_cast<GetElementPtrInst>(TrueVal))
37203719
if (auto *NewGep = SelectGepWithBase(TrueGep, FalseVal, false))

llvm/test/Transforms/InstCombine/select-gep.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ define ptr @test2a(ptr %p, i64 %x, i64 %y) {
8484
ret ptr %select
8585
}
8686

87+
define ptr @test2a_nusw(ptr %p, i64 %x, i64 %y) {
88+
; CHECK-LABEL: @test2a_nusw(
89+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
90+
; CHECK-NEXT: [[SELECT_IDX:%.*]] = select i1 [[CMP]], i64 [[X]], i64 0
91+
; CHECK-NEXT: [[SELECT:%.*]] = getelementptr nusw i32, ptr [[P:%.*]], i64 [[SELECT_IDX]]
92+
; CHECK-NEXT: ret ptr [[SELECT]]
93+
;
94+
%gep = getelementptr nusw i32, ptr %p, i64 %x
95+
%cmp = icmp ugt i64 %x, %y
96+
%select = select i1 %cmp, ptr %gep, ptr %p
97+
ret ptr %select
98+
}
99+
100+
define ptr @test2a_nuw(ptr %p, i64 %x, i64 %y) {
101+
; CHECK-LABEL: @test2a_nuw(
102+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
103+
; CHECK-NEXT: [[SELECT_IDX:%.*]] = select i1 [[CMP]], i64 [[X]], i64 0
104+
; CHECK-NEXT: [[SELECT:%.*]] = getelementptr nuw i32, ptr [[P:%.*]], i64 [[SELECT_IDX]]
105+
; CHECK-NEXT: ret ptr [[SELECT]]
106+
;
107+
%gep = getelementptr nuw i32, ptr %p, i64 %x
108+
%cmp = icmp ugt i64 %x, %y
109+
%select = select i1 %cmp, ptr %gep, ptr %p
110+
ret ptr %select
111+
}
112+
87113
; PR50183
88114
define ptr @test2b(ptr %p, i64 %x, i64 %y) {
89115
; CHECK-LABEL: @test2b(

0 commit comments

Comments
 (0)