Skip to content

[InstCombine] Preserve GEP no-wrap flags #141113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ Instruction *InstCombinerImpl::visitExtractElementInst(ExtractElementInst &EI) {

GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
GEP->getSourceElementType(), NewPtr, NewOps);
NewGEP->setIsInBounds(GEP->isInBounds());
NewGEP->setNoWrapFlags(GEP->getNoWrapFlags());
return NewGEP;
}
}
Expand Down Expand Up @@ -1982,7 +1982,7 @@ static Value *buildNew(Instruction *I, ArrayRef<Value*> NewOps,
ArrayRef<Value*> Idx = NewOps.slice(1);
return Builder.CreateGEP(cast<GEPOperator>(I)->getSourceElementType(),
Ptr, Idx, "",
cast<GEPOperator>(I)->isInBounds());
cast<GEPOperator>(I)->getNoWrapFlags());
}
}
llvm_unreachable("failed to rebuild vector instructions");
Expand Down
28 changes: 26 additions & 2 deletions llvm/test/Transforms/InstCombine/gep-vector-indices.ll
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ define ptr @vector_splat_indices_v2i64_ext0(ptr %a) {
ret ptr %res
}

define ptr @vector_splat_indices_nxv2i64_ext0(ptr %a) {
; CHECK-LABEL: @vector_splat_indices_nxv2i64_ext0(
define ptr @vector_splat_indices_nxv2i64_ext0_inbounds(ptr %a) {
; CHECK-LABEL: @vector_splat_indices_nxv2i64_ext0_inbounds(
; CHECK-NEXT: [[RES:%.*]] = getelementptr inbounds nuw i8, ptr [[A:%.*]], i64 16
; CHECK-NEXT: ret ptr [[RES]]
;
Expand All @@ -24,6 +24,30 @@ define ptr @vector_splat_indices_nxv2i64_ext0(ptr %a) {
ret ptr %res
}

define ptr @vector_splat_indices_nxv2i64_ext0_nuw(ptr %a) {
; CHECK-LABEL: @vector_splat_indices_nxv2i64_ext0_nuw(
; CHECK-NEXT: [[RES:%.*]] = getelementptr nuw i8, ptr [[A:%.*]], i64 16
; CHECK-NEXT: ret ptr [[RES]]
;
%tmp = insertelement <vscale x 2 x i64> poison, i64 4, i32 0
%splatof4 = shufflevector <vscale x 2 x i64> %tmp, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
%gep = getelementptr nuw i32, ptr %a, <vscale x 2 x i64> %splatof4
%res = extractelement <vscale x 2 x ptr> %gep, i32 0
ret ptr %res
}

define ptr @vector_splat_indices_nxv2i64_ext0_nusw(ptr %a) {
; CHECK-LABEL: @vector_splat_indices_nxv2i64_ext0_nusw(
; CHECK-NEXT: [[RES:%.*]] = getelementptr nusw nuw i8, ptr [[A:%.*]], i64 16
; CHECK-NEXT: ret ptr [[RES]]
;
%tmp = insertelement <vscale x 2 x i64> poison, i64 4, i32 0
%splatof4 = shufflevector <vscale x 2 x i64> %tmp, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
%gep = getelementptr nusw i32, ptr %a, <vscale x 2 x i64> %splatof4
%res = extractelement <vscale x 2 x ptr> %gep, i32 0
ret ptr %res
}

define ptr @vector_indices_v2i64_ext0(ptr %a, <2 x i64> %indices) {
; CHECK-LABEL: @vector_indices_v2i64_ext0(
; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i64> [[INDICES:%.*]], i64 0
Expand Down
39 changes: 39 additions & 0 deletions llvm/test/Transforms/InstCombine/vec_shuffle.ll
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,45 @@ define <1 x ptr> @shuffle_gep(ptr %x1, ptr %x2) {
ret <1 x ptr> %ret
}

define <1 x ptr> @shuffle_gep_inbounds(ptr %x1, ptr %x2) {
; CHECK-LABEL: @shuffle_gep_inbounds(
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <1 x ptr> poison, ptr [[X2:%.*]], i64 0
; CHECK-NEXT: [[RET:%.*]] = getelementptr inbounds i8, <1 x ptr> [[TMP1]], i64 5
; CHECK-NEXT: ret <1 x ptr> [[RET]]
;
%ins.1 = insertelement <2 x ptr> poison, ptr %x1, i32 0
%ins.2 = insertelement <2 x ptr> %ins.1, ptr %x2, i32 1
%gep = getelementptr inbounds i8, <2 x ptr> %ins.2, i64 5
%ret = shufflevector <2 x ptr> %gep, <2 x ptr> poison, <1 x i32> <i32 1>
ret <1 x ptr> %ret
}

define <1 x ptr> @shuffle_gep_nuw(ptr %x1, ptr %x2) {
; CHECK-LABEL: @shuffle_gep_nuw(
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <1 x ptr> poison, ptr [[X2:%.*]], i64 0
; CHECK-NEXT: [[RET:%.*]] = getelementptr nuw i8, <1 x ptr> [[TMP1]], i64 5
; CHECK-NEXT: ret <1 x ptr> [[RET]]
;
%ins.1 = insertelement <2 x ptr> poison, ptr %x1, i32 0
%ins.2 = insertelement <2 x ptr> %ins.1, ptr %x2, i32 1
%gep = getelementptr nuw i8, <2 x ptr> %ins.2, i64 5
%ret = shufflevector <2 x ptr> %gep, <2 x ptr> poison, <1 x i32> <i32 1>
ret <1 x ptr> %ret
}

define <1 x ptr> @shuffle_gep_nusw(ptr %x1, ptr %x2) {
; CHECK-LABEL: @shuffle_gep_nusw(
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <1 x ptr> poison, ptr [[X2:%.*]], i64 0
; CHECK-NEXT: [[RET:%.*]] = getelementptr nusw i8, <1 x ptr> [[TMP1]], i64 5
; CHECK-NEXT: ret <1 x ptr> [[RET]]
;
%ins.1 = insertelement <2 x ptr> poison, ptr %x1, i32 0
%ins.2 = insertelement <2 x ptr> %ins.1, ptr %x2, i32 1
%gep = getelementptr nusw i8, <2 x ptr> %ins.2, i64 5
%ret = shufflevector <2 x ptr> %gep, <2 x ptr> poison, <1 x i32> <i32 1>
ret <1 x ptr> %ret
}

; Increasing length of vector ops is not a good canonicalization.

define <3 x i32> @add_wider(i32 %y, i32 %z) {
Expand Down