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

Conversation

artagnon
Copy link
Contributor

No description provided.

@artagnon artagnon requested a review from nikic as a code owner May 22, 2025 18:01
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels May 22, 2025
@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Ramkumar Ramachandra (artagnon)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/141113.diff

3 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp (+2-2)
  • (modified) llvm/test/Transforms/InstCombine/gep-vector-indices.ll (+26-2)
  • (modified) llvm/test/Transforms/InstCombine/vec_shuffle.ll (+39)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
index f6423cb40492e..900946e9b335b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -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;
         }
       }
@@ -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");
diff --git a/llvm/test/Transforms/InstCombine/gep-vector-indices.ll b/llvm/test/Transforms/InstCombine/gep-vector-indices.ll
index 660ce317a0f64..ce584e0400cd5 100644
--- a/llvm/test/Transforms/InstCombine/gep-vector-indices.ll
+++ b/llvm/test/Transforms/InstCombine/gep-vector-indices.ll
@@ -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]]
 ;
@@ -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
diff --git a/llvm/test/Transforms/InstCombine/vec_shuffle.ll b/llvm/test/Transforms/InstCombine/vec_shuffle.ll
index fa34a42714c46..53f9df91d6af8 100644
--- a/llvm/test/Transforms/InstCombine/vec_shuffle.ll
+++ b/llvm/test/Transforms/InstCombine/vec_shuffle.ll
@@ -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) {

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@artagnon artagnon merged commit f398f2a into llvm:main May 22, 2025
14 checks passed
@artagnon artagnon deleted the ic-preserve-gepnw branch May 22, 2025 21:48
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
ajaden-codes pushed a commit to Jaddyen/llvm-project that referenced this pull request Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants