Skip to content

Commit 4c8ce5d

Browse files
committed
[InstCombine] Preserve all flags in select of gep fold
Preserve the flag intersection.
1 parent 1a364b4 commit 4c8ce5d

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,8 @@ Instruction *InstCombinerImpl::foldSelectOpOp(SelectInst &SI, Instruction *TI,
486486
if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) {
487487
auto *FGEP = cast<GetElementPtrInst>(FI);
488488
Type *ElementType = TGEP->getSourceElementType();
489-
return TGEP->isInBounds() && FGEP->isInBounds()
490-
? GetElementPtrInst::CreateInBounds(ElementType, Op0, {Op1})
491-
: GetElementPtrInst::Create(ElementType, Op0, {Op1});
489+
return GetElementPtrInst::Create(
490+
ElementType, Op0, Op1, TGEP->getNoWrapFlags() & FGEP->getNoWrapFlags());
492491
}
493492
llvm_unreachable("Expected BinaryOperator or GEP");
494493
return nullptr;

llvm/test/Transforms/InstCombine/opaque-ptr.ll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,30 @@ define ptr @select_of_gep(i1 %c, ptr %p) {
639639
ret ptr %s
640640
}
641641

642+
define ptr @select_of_gep_flags_1(i1 %c, ptr %p) {
643+
; CHECK-LABEL: @select_of_gep_flags_1(
644+
; CHECK-NEXT: [[S_V:%.*]] = select i1 [[C:%.*]], i64 4, i64 8
645+
; CHECK-NEXT: [[S:%.*]] = getelementptr nusw i8, ptr [[P:%.*]], i64 [[S_V]]
646+
; CHECK-NEXT: ret ptr [[S]]
647+
;
648+
%gep1 = getelementptr inbounds i32, ptr %p, i64 1
649+
%gep2 = getelementptr nusw nuw i32, ptr %p, i64 2
650+
%s = select i1 %c, ptr %gep1, ptr %gep2
651+
ret ptr %s
652+
}
653+
654+
define ptr @select_of_gep_flags_2(i1 %c, ptr %p) {
655+
; CHECK-LABEL: @select_of_gep_flags_2(
656+
; CHECK-NEXT: [[S_V:%.*]] = select i1 [[C:%.*]], i64 4, i64 8
657+
; CHECK-NEXT: [[S:%.*]] = getelementptr nuw i8, ptr [[P:%.*]], i64 [[S_V]]
658+
; CHECK-NEXT: ret ptr [[S]]
659+
;
660+
%gep1 = getelementptr nuw i32, ptr %p, i64 1
661+
%gep2 = getelementptr nusw nuw i32, ptr %p, i64 2
662+
%s = select i1 %c, ptr %gep1, ptr %gep2
663+
ret ptr %s
664+
}
665+
642666
define ptr @select_of_gep_different_type(i1 %c, ptr %p) {
643667
; CHECK-LABEL: @select_of_gep_different_type(
644668
; CHECK-NEXT: [[S_V:%.*]] = select i1 [[C:%.*]], i64 4, i64 16

0 commit comments

Comments
 (0)