[llvm] [InstCombine] Only ignore first zero index during GEP canonicalization (PR #180764)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 10 08:08:24 PST 2026


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/180764

When canonicalizing GEPs to have a single index, only ignore the first leading zero index. This fixes a hole in the canonicalization in case there was a struct zero offset, which can't be removed by the the leading zero stripping.

>From 136d1bbd29400ebca2094fec8c6dbf49678aed80 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 10 Feb 2026 16:57:33 +0100
Subject: [PATCH] [InstCombine] Only ignore first zero index during GEP
 canonicalization

When canonicalizing GEPs to have a single index, only ignore the
first leading zero index. This fixes a hole in the canonicalization
in case there was a struct zero offset, which can't be removed by
the the leading zero stripping.
---
 .../InstCombine/InstructionCombining.cpp      |  3 ++-
 llvm/test/Transforms/InstCombine/strlen-7.ll  |  2 +-
 .../LoopVectorize/PowerPC/vsx-tsvc-s173.ll    | 20 +++++++++----------
 .../X86/consecutive-ptr-uniforms.ll           |  2 +-
 4 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index fd699381c22fa..afabdf66e2ede 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3464,8 +3464,9 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
 
   bool SeenNonZeroIndex = false;
   for (auto [IdxNum, Idx] : enumerate(Indices)) {
+    // Ignore one leading zero index.
     auto *C = dyn_cast<Constant>(Idx);
-    if (C && C->isNullValue())
+    if (C && C->isNullValue() && IdxNum == 0)
       continue;
 
     if (!SeenNonZeroIndex) {
diff --git a/llvm/test/Transforms/InstCombine/strlen-7.ll b/llvm/test/Transforms/InstCombine/strlen-7.ll
index a878ece075c5f..b63c181b6f43b 100644
--- a/llvm/test/Transforms/InstCombine/strlen-7.ll
+++ b/llvm/test/Transforms/InstCombine/strlen-7.ll
@@ -167,7 +167,7 @@ define void @fold_strlen_A(ptr %plen) {
 
 define void @fold_strlen_A_pI(ptr %plen, i64 %I) {
 ; CHECK-LABEL: @fold_strlen_A_pI(
-; CHECK-NEXT:    [[PA0A:%.*]] = getelementptr [[STRUCT_A:%.*]], ptr @a, i64 0, i32 0, i64 [[I:%.*]]
+; CHECK-NEXT:    [[PA0A:%.*]] = getelementptr i8, ptr @a, i64 [[I:%.*]]
 ; CHECK-NEXT:    [[LENA0A:%.*]] = call i64 @strlen(ptr noundef nonnull dereferenceable(1) [[PA0A]])
 ; CHECK-NEXT:    store i64 [[LENA0A]], ptr [[PLEN:%.*]], align 4
 ; CHECK-NEXT:    [[PA0B:%.*]] = getelementptr i8, ptr getelementptr inbounds nuw (i8, ptr @a, i64 4), i64 [[I]]
diff --git a/llvm/test/Transforms/LoopVectorize/PowerPC/vsx-tsvc-s173.ll b/llvm/test/Transforms/LoopVectorize/PowerPC/vsx-tsvc-s173.ll
index 48a9bdc12a104..372dfbc2e3cd2 100644
--- a/llvm/test/Transforms/LoopVectorize/PowerPC/vsx-tsvc-s173.ll
+++ b/llvm/test/Transforms/LoopVectorize/PowerPC/vsx-tsvc-s173.ll
@@ -24,7 +24,7 @@ define signext i32 @s173() #0 {
 ; CHECK-NEXT:    br label %[[VECTOR_BODY:.*]]
 ; CHECK:       [[VECTOR_BODY]]:
 ; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
-; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds [[STRUCT_GLOBALDATA:%.*]], ptr @global_data, i64 0, i32 0, i64 [[INDEX]]
+; CHECK-NEXT:    [[TMP1:%.*]] = getelementptr inbounds float, ptr @global_data, i64 [[INDEX]]
 ; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP1]], i64 16
 ; CHECK-NEXT:    [[TMP3:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP1]], i64 32
 ; CHECK-NEXT:    [[TMP4:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP1]], i64 48
@@ -64,15 +64,15 @@ define signext i32 @s173() #0 {
 ; CHECK-NEXT:    [[TMP22:%.*]] = fadd <4 x float> [[WIDE_LOAD5]], [[WIDE_LOAD13]]
 ; CHECK-NEXT:    [[TMP23:%.*]] = fadd <4 x float> [[WIDE_LOAD6]], [[WIDE_LOAD14]]
 ; CHECK-NEXT:    [[TMP24:%.*]] = fadd <4 x float> [[WIDE_LOAD7]], [[WIDE_LOAD15]]
-; CHECK-NEXT:    [[TMP25:%.*]] = add nsw i64 [[INDEX]], 16000
-; CHECK-NEXT:    [[TMP26:%.*]] = getelementptr inbounds [[STRUCT_GLOBALDATA]], ptr @global_data, i64 0, i32 0, i64 [[TMP25]]
-; CHECK-NEXT:    [[TMP27:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP26]], i64 16
-; CHECK-NEXT:    [[TMP28:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP26]], i64 32
-; CHECK-NEXT:    [[TMP29:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP26]], i64 48
-; CHECK-NEXT:    [[TMP30:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP26]], i64 64
-; CHECK-NEXT:    [[TMP31:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP26]], i64 80
-; CHECK-NEXT:    [[TMP32:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP26]], i64 96
-; CHECK-NEXT:    [[TMP33:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP26]], i64 112
+; CHECK-NEXT:    [[TMP25:%.*]] = getelementptr float, ptr @global_data, i64 [[INDEX]]
+; CHECK-NEXT:    [[TMP26:%.*]] = getelementptr i8, ptr [[TMP25]], i64 64000
+; CHECK-NEXT:    [[TMP27:%.*]] = getelementptr i8, ptr [[TMP25]], i64 64016
+; CHECK-NEXT:    [[TMP28:%.*]] = getelementptr i8, ptr [[TMP25]], i64 64032
+; CHECK-NEXT:    [[TMP29:%.*]] = getelementptr i8, ptr [[TMP25]], i64 64048
+; CHECK-NEXT:    [[TMP30:%.*]] = getelementptr i8, ptr [[TMP25]], i64 64064
+; CHECK-NEXT:    [[TMP31:%.*]] = getelementptr i8, ptr [[TMP25]], i64 64080
+; CHECK-NEXT:    [[TMP32:%.*]] = getelementptr i8, ptr [[TMP25]], i64 64096
+; CHECK-NEXT:    [[TMP33:%.*]] = getelementptr i8, ptr [[TMP25]], i64 64112
 ; CHECK-NEXT:    store <4 x float> [[TMP17]], ptr [[TMP26]], align 4
 ; CHECK-NEXT:    store <4 x float> [[TMP18]], ptr [[TMP27]], align 4
 ; CHECK-NEXT:    store <4 x float> [[TMP19]], ptr [[TMP28]], align 4
diff --git a/llvm/test/Transforms/LoopVectorize/X86/consecutive-ptr-uniforms.ll b/llvm/test/Transforms/LoopVectorize/X86/consecutive-ptr-uniforms.ll
index 69505eb176f50..7bea9e07d7224 100644
--- a/llvm/test/Transforms/LoopVectorize/X86/consecutive-ptr-uniforms.ll
+++ b/llvm/test/Transforms/LoopVectorize/X86/consecutive-ptr-uniforms.ll
@@ -41,7 +41,7 @@ define void @PR31671(float %x, ptr %d) #0 {
 ; CHECK-NEXT:    [[WIDE_VEC:%.*]] = load <80 x float>, ptr [[TMP0]], align 4
 ; CHECK-NEXT:    [[STRIDED_VEC:%.*]] = shufflevector <80 x float> [[WIDE_VEC]], <80 x float> poison, <16 x i32> <i32 0, i32 5, i32 10, i32 15, i32 20, i32 25, i32 30, i32 35, i32 40, i32 45, i32 50, i32 55, i32 60, i32 65, i32 70, i32 75>
 ; CHECK-NEXT:    [[TMP1:%.*]] = fmul <16 x float> [[BROADCAST_SPLAT]], [[STRIDED_VEC]]
-; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds [[DATA:%.*]], ptr [[D]], i64 0, i32 0, <16 x i64> [[VEC_IND]]
+; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr inbounds float, ptr [[D]], <16 x i64> [[VEC_IND]]
 ; CHECK-NEXT:    [[TMP3:%.*]] = extractelement <16 x ptr> [[TMP2]], i64 0
 ; CHECK-NEXT:    [[WIDE_VEC1:%.*]] = load <80 x float>, ptr [[TMP3]], align 4
 ; CHECK-NEXT:    [[STRIDED_VEC2:%.*]] = shufflevector <80 x float> [[WIDE_VEC1]], <80 x float> poison, <16 x i32> <i32 0, i32 5, i32 10, i32 15, i32 20, i32 25, i32 30, i32 35, i32 40, i32 45, i32 50, i32 55, i32 60, i32 65, i32 70, i32 75>



More information about the llvm-commits mailing list