[llvm] [InstCombinePHI] Enable gep arg fold transform with constant indices (PR #174981)

Jessica Del via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 8 06:07:13 PST 2026


https://github.com/OutOfCache created https://github.com/llvm/llvm-project/pull/174981

https://alive2.llvm.org/ce/z/DzO5Ow

The fold changes a phi node with getelementptrs as incoming values to a phi node with offsets, while creating a new getelementptr after the phi and the phi value as offset:

```llvm
; before
%old.phi = phi ptr [ %gep0, %bb0], [ %gep1, %bb1]

; after
%new.phi = phi i64 [ %off0, %bb0], [ %off1, %bb1 ]
%new.gep = getelementptr i8, ptr %p, i64 %new.phi
```
This transform should not happen if we index into a struct, since it creates a new gep with variable index and structs need constant indices.

Previously, this was prevented by disabling the transform for any constant indices. Now, the change should apply to getelementptrs with constant indices, if the indexed type is not a struct.

Do not transform if the offset is constant for some of the geps and variable for others, as suggested here https://github.com/llvm/llvm-project/issues/169919

>From 1a89e2622330d07626287cb47cf937618ecdf43e Mon Sep 17 00:00:00 2001
From: Jessica Del <Jessica.Del at amd.com>
Date: Thu, 8 Jan 2026 09:33:18 +0100
Subject: [PATCH 1/5] [InstCombinePHI][NFC] Prevent gep canonicalization in
 testcase

GEP canonicalization unintentionally prevents the faulty behavior which
was tested.

InstCombine prevents the transformation of a gep, which indexes a struct,
into a gep with a variable index, since that is not valid LLVM IR.
It does so by checking if the gep only has constant indices.

The original test checks this scenario, even for constant vector
indices. Without the constant check, the test is supposed to crash.

However, after the getelementptr is canonicalized into a `gep i8, ptr %ptr,
i64`, the test case can actually be transformed without an issue.

In a followup change, we want to allow the transformation of geps with
constant indices, if they do not index into a struct.
Therefore, the constant index check needs to be changed accordingly to
properly check the indexed type and whether it is a struct or not.

Since canonicalization loses the type information, we modified the test
so it would still trigger a crash, by avoiding the canonicalization.
This is done by introducing a variable index and visiting the phi before
the geps (so they would not be canonicalized or split).
---
 .../fold-phi-arg-gep-to-phi-negative.ll       | 36 +++++++++++--------
 1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi-negative.ll b/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi-negative.ll
index c5f9dc1861704..4278e60bd2d44 100644
--- a/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi-negative.ll
+++ b/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi-negative.ll
@@ -3,37 +3,45 @@
 
 %foo = type { i16, i16, i16 }
 
-define <16 x ptr> @test(i1 %tobool) {
+define <16 x ptr> @test(i1 %tobool, i1 %end, <16 x i64> %varidx) {
 ; CHECK-LABEL: define <16 x ptr> @test(
-; CHECK-SAME: i1 [[TOBOOL:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-SAME: i1 [[TOBOOL:%.*]], i1 [[END:%.*]], <16 x i64> [[VARIDX:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
 ; CHECK-NEXT:    [[LANE_0:%.*]] = alloca [[FOO:%.*]], align 16
 ; CHECK-NEXT:    [[LANE_15:%.*]] = insertelement <16 x ptr> poison, ptr [[LANE_0]], i64 0
-; CHECK-NEXT:    br i1 [[TOBOOL]], label %[[F1:.*]], label %[[F0:.*]]
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[VEC_PHI:%.*]] = phi <16 x ptr> [ [[MM_VECTORGEP11:%.*]], %[[F0:.*]] ], [ [[MM_VECTORGEP22:%.*]], %[[F1:.*]] ], [ zeroinitializer, %[[ENTRY]] ]
+; CHECK-NEXT:    br i1 [[TOBOOL]], label %[[F1]], label %[[F0]]
 ; CHECK:       [[F0]]:
-; CHECK-NEXT:    [[MM_VECTORGEP1:%.*]] = getelementptr inbounds i8, <16 x ptr> [[LANE_15]], i64 2
-; CHECK-NEXT:    br label %[[MERGE:.*]]
+; CHECK-NEXT:    [[MM_VECTORGEP11_SPLIT:%.*]] = getelementptr inbounds [[FOO]], <16 x ptr> [[LANE_15]], <16 x i64> [[VARIDX]]
+; CHECK-NEXT:    [[MM_VECTORGEP11]] = getelementptr inbounds i8, <16 x ptr> [[MM_VECTORGEP11_SPLIT]], i64 2
+; CHECK-NEXT:    br i1 [[END]], label %[[LOOP]], label %[[MERGE:.*]]
 ; CHECK:       [[F1]]:
-; CHECK-NEXT:    [[MM_VECTORGEP22:%.*]] = getelementptr inbounds i8, <16 x ptr> [[LANE_15]], i64 4
-; CHECK-NEXT:    br label %[[MERGE]]
+; CHECK-NEXT:    [[MM_VECTORGEP22_SPLIT:%.*]] = getelementptr inbounds [[FOO]], <16 x ptr> [[LANE_15]], <16 x i64> [[VARIDX]]
+; CHECK-NEXT:    [[MM_VECTORGEP22]] = getelementptr inbounds i8, <16 x ptr> [[MM_VECTORGEP22_SPLIT]], i64 4
+; CHECK-NEXT:    br i1 [[END]], label %[[LOOP]], label %[[MERGE]]
 ; CHECK:       [[MERGE]]:
-; CHECK-NEXT:    [[VEC_PHI:%.*]] = phi <16 x ptr> [ [[MM_VECTORGEP1]], %[[F0]] ], [ [[MM_VECTORGEP22]], %[[F1]] ]
 ; CHECK-NEXT:    ret <16 x ptr> [[VEC_PHI]]
 ;
 entry:
   %lane.0 = alloca %foo, align 16
   %lane.15 = insertelement <16 x ptr> poison, ptr %lane.0, i64 0
-  %mm_vectorGEP = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> zeroinitializer, <16 x i32> splat (i32 1)
-  %mm_vectorGEP2 = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> zeroinitializer, <16 x i32> splat (i32 2)
+  br label %loop
+
+loop:
+  %vec.phi = phi <16 x ptr> [ %mm_vectorGEP1, %f0], [ %mm_vectorGEP2, %f1 ], [ zeroinitializer, %entry]
+  %mm_vectorGEP1 = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> %varidx, <16 x i32> splat (i32 1)
+  %mm_vectorGEP2 = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> %varidx, <16 x i32> splat (i32 2)
   br i1 %tobool, label %f1, label %f0
 
 f0:
-  br label %merge
+  br i1 %end, label %loop, label %merge
 
 f1:
-  br label %merge
+  br i1 %end, label %loop, label %merge
 
 merge:
-  %vec.phi = phi <16 x ptr> [ %mm_vectorGEP, %f0], [ %mm_vectorGEP2, %f1 ]
   ret <16 x ptr> %vec.phi
 }
+

>From a212d6c4256134831b1a31323d6480bda634f8dd Mon Sep 17 00:00:00 2001
From: Jessica Del <Jessica.Del at amd.com>
Date: Tue, 16 Dec 2025 22:38:39 +0100
Subject: [PATCH 2/5] [InstCombine] Add tests for gep arg folding

---
 .../fold-phi-arg-gep-to-phi-negative.ll       |  47 ----
 .../InstCombine/fold-phi-arg-gep-to-phi.ll    | 202 ++++++++++++++++++
 2 files changed, 202 insertions(+), 47 deletions(-)
 delete mode 100644 llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi-negative.ll
 create mode 100644 llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi.ll

diff --git a/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi-negative.ll b/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi-negative.ll
deleted file mode 100644
index 4278e60bd2d44..0000000000000
--- a/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi-negative.ll
+++ /dev/null
@@ -1,47 +0,0 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
-; RUN: opt < %s -passes=instcombine -S | FileCheck %s
-
-%foo = type { i16, i16, i16 }
-
-define <16 x ptr> @test(i1 %tobool, i1 %end, <16 x i64> %varidx) {
-; CHECK-LABEL: define <16 x ptr> @test(
-; CHECK-SAME: i1 [[TOBOOL:%.*]], i1 [[END:%.*]], <16 x i64> [[VARIDX:%.*]]) {
-; CHECK-NEXT:  [[ENTRY:.*]]:
-; CHECK-NEXT:    [[LANE_0:%.*]] = alloca [[FOO:%.*]], align 16
-; CHECK-NEXT:    [[LANE_15:%.*]] = insertelement <16 x ptr> poison, ptr [[LANE_0]], i64 0
-; CHECK-NEXT:    br label %[[LOOP:.*]]
-; CHECK:       [[LOOP]]:
-; CHECK-NEXT:    [[VEC_PHI:%.*]] = phi <16 x ptr> [ [[MM_VECTORGEP11:%.*]], %[[F0:.*]] ], [ [[MM_VECTORGEP22:%.*]], %[[F1:.*]] ], [ zeroinitializer, %[[ENTRY]] ]
-; CHECK-NEXT:    br i1 [[TOBOOL]], label %[[F1]], label %[[F0]]
-; CHECK:       [[F0]]:
-; CHECK-NEXT:    [[MM_VECTORGEP11_SPLIT:%.*]] = getelementptr inbounds [[FOO]], <16 x ptr> [[LANE_15]], <16 x i64> [[VARIDX]]
-; CHECK-NEXT:    [[MM_VECTORGEP11]] = getelementptr inbounds i8, <16 x ptr> [[MM_VECTORGEP11_SPLIT]], i64 2
-; CHECK-NEXT:    br i1 [[END]], label %[[LOOP]], label %[[MERGE:.*]]
-; CHECK:       [[F1]]:
-; CHECK-NEXT:    [[MM_VECTORGEP22_SPLIT:%.*]] = getelementptr inbounds [[FOO]], <16 x ptr> [[LANE_15]], <16 x i64> [[VARIDX]]
-; CHECK-NEXT:    [[MM_VECTORGEP22]] = getelementptr inbounds i8, <16 x ptr> [[MM_VECTORGEP22_SPLIT]], i64 4
-; CHECK-NEXT:    br i1 [[END]], label %[[LOOP]], label %[[MERGE]]
-; CHECK:       [[MERGE]]:
-; CHECK-NEXT:    ret <16 x ptr> [[VEC_PHI]]
-;
-entry:
-  %lane.0 = alloca %foo, align 16
-  %lane.15 = insertelement <16 x ptr> poison, ptr %lane.0, i64 0
-  br label %loop
-
-loop:
-  %vec.phi = phi <16 x ptr> [ %mm_vectorGEP1, %f0], [ %mm_vectorGEP2, %f1 ], [ zeroinitializer, %entry]
-  %mm_vectorGEP1 = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> %varidx, <16 x i32> splat (i32 1)
-  %mm_vectorGEP2 = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> %varidx, <16 x i32> splat (i32 2)
-  br i1 %tobool, label %f1, label %f0
-
-f0:
-  br i1 %end, label %loop, label %merge
-
-f1:
-  br i1 %end, label %loop, label %merge
-
-merge:
-  ret <16 x ptr> %vec.phi
-}
-
diff --git a/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi.ll b/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi.ll
new file mode 100644
index 0000000000000..41f0bbac2e30b
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi.ll
@@ -0,0 +1,202 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+%foo = type { i16, i16, i16 }
+
+define <16 x ptr> @test(i1 %tobool, i1 %end, <16 x i64> %varidx) {
+; CHECK-LABEL: define <16 x ptr> @test(
+; CHECK-SAME: i1 [[TOBOOL:%.*]], i1 [[END:%.*]], <16 x i64> [[VARIDX:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[LANE_0:%.*]] = alloca [[FOO:%.*]], align 16
+; CHECK-NEXT:    [[LANE_15:%.*]] = insertelement <16 x ptr> poison, ptr [[LANE_0]], i64 0
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[VEC_PHI:%.*]] = phi <16 x ptr> [ [[MM_VECTORGEP11:%.*]], %[[F0:.*]] ], [ [[MM_VECTORGEP22:%.*]], %[[F1:.*]] ], [ zeroinitializer, %[[ENTRY]] ]
+; CHECK-NEXT:    br i1 [[TOBOOL]], label %[[F1]], label %[[F0]]
+; CHECK:       [[F0]]:
+; CHECK-NEXT:    [[MM_VECTORGEP11_SPLIT:%.*]] = getelementptr inbounds [[FOO]], <16 x ptr> [[LANE_15]], <16 x i64> [[VARIDX]]
+; CHECK-NEXT:    [[MM_VECTORGEP11]] = getelementptr inbounds i8, <16 x ptr> [[MM_VECTORGEP11_SPLIT]], i64 2
+; CHECK-NEXT:    br i1 [[END]], label %[[LOOP]], label %[[MERGE:.*]]
+; CHECK:       [[F1]]:
+; CHECK-NEXT:    [[MM_VECTORGEP22_SPLIT:%.*]] = getelementptr inbounds [[FOO]], <16 x ptr> [[LANE_15]], <16 x i64> [[VARIDX]]
+; CHECK-NEXT:    [[MM_VECTORGEP22]] = getelementptr inbounds i8, <16 x ptr> [[MM_VECTORGEP22_SPLIT]], i64 4
+; CHECK-NEXT:    br i1 [[END]], label %[[LOOP]], label %[[MERGE]]
+; CHECK:       [[MERGE]]:
+; CHECK-NEXT:    ret <16 x ptr> [[VEC_PHI]]
+;
+entry:
+  %lane.0 = alloca %foo, align 16
+  %lane.15 = insertelement <16 x ptr> poison, ptr %lane.0, i64 0
+  br label %loop
+
+loop:
+  %vec.phi = phi <16 x ptr> [ %mm_vectorGEP1, %f0], [ %mm_vectorGEP2, %f1 ], [ zeroinitializer, %entry]
+  %mm_vectorGEP1 = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> %varidx, <16 x i32> splat (i32 1)
+  %mm_vectorGEP2 = getelementptr inbounds %foo, <16 x ptr> %lane.15, <16 x i64> %varidx, <16 x i32> splat (i32 2)
+  br i1 %tobool, label %f1, label %f0
+
+f0:
+  br i1 %end, label %loop, label %merge
+
+f1:
+  br i1 %end, label %loop, label %merge
+
+merge:
+  ret <16 x ptr> %vec.phi
+}
+
+define ptr @gep_constant_offset(i1 %cond, ptr %ptr) {
+; CHECK-LABEL: define ptr @gep_constant_offset(
+; CHECK-SAME: i1 [[COND:%.*]], ptr [[PTR:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br i1 [[COND]], label %[[F1:.*]], label %[[F0:.*]]
+; CHECK:       [[F0]]:
+; CHECK-NEXT:    [[GEP0:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 16
+; CHECK-NEXT:    br label %[[MERGE:.*]]
+; CHECK:       [[F1]]:
+; CHECK-NEXT:    [[GEP1:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 32
+; CHECK-NEXT:    br label %[[MERGE]]
+; CHECK:       [[MERGE]]:
+; CHECK-NEXT:    [[PHI:%.*]] = phi ptr [ [[GEP0]], %[[F0]] ], [ [[GEP1]], %[[F1]] ]
+; CHECK-NEXT:    ret ptr [[PHI]]
+;
+entry:
+  %gep0 = getelementptr inbounds i8, ptr %ptr, i64 16
+  %gep1 = getelementptr inbounds i8, ptr %ptr, i64 32
+  br i1 %cond, label %f1, label %f0
+
+f0:
+  br label %merge
+
+f1:
+  br label %merge
+
+merge:
+  %phi = phi ptr [ %gep0, %f0], [ %gep1, %f1 ]
+  ret ptr %phi
+}
+
+define ptr @gep_variable_offset(i1 %cond, ptr %ptr, i64 %off0, i64 %off1) {
+; CHECK-LABEL: define ptr @gep_variable_offset(
+; CHECK-SAME: i1 [[COND:%.*]], ptr [[PTR:%.*]], i64 [[OFF0:%.*]], i64 [[OFF1:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br i1 [[COND]], label %[[F1:.*]], label %[[F0:.*]]
+; CHECK:       [[F0]]:
+; CHECK-NEXT:    br label %[[MERGE:.*]]
+; CHECK:       [[F1]]:
+; CHECK-NEXT:    br label %[[MERGE]]
+; CHECK:       [[MERGE]]:
+; CHECK-NEXT:    [[OFF0_PN:%.*]] = phi i64 [ [[OFF0]], %[[F0]] ], [ [[OFF1]], %[[F1]] ]
+; CHECK-NEXT:    [[PHI:%.*]] = getelementptr inbounds i8, ptr [[PTR]], i64 [[OFF0_PN]]
+; CHECK-NEXT:    ret ptr [[PHI]]
+;
+entry:
+  %gep0 = getelementptr inbounds i8, ptr %ptr, i64 %off0
+  %gep1 = getelementptr inbounds i8, ptr %ptr, i64 %off1
+  br i1 %cond, label %f1, label %f0
+
+f0:
+  br label %merge
+
+f1:
+  br label %merge
+
+merge:
+  %phi = phi ptr [ %gep0, %f0], [ %gep1, %f1 ]
+  ret ptr %phi
+}
+
+define ptr @gep_mixed_offset_1(i1 %cond, ptr %ptr, i64 %off1) {
+; CHECK-LABEL: define ptr @gep_mixed_offset_1(
+; CHECK-SAME: i1 [[COND:%.*]], ptr [[PTR:%.*]], i64 [[OFF1:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br i1 [[COND]], label %[[F1:.*]], label %[[F0:.*]]
+; CHECK:       [[F0]]:
+; CHECK-NEXT:    [[GEP0:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 16
+; CHECK-NEXT:    br label %[[MERGE:.*]]
+; CHECK:       [[F1]]:
+; CHECK-NEXT:    [[GEP1:%.*]] = getelementptr inbounds i8, ptr [[PTR]], i64 [[OFF1]]
+; CHECK-NEXT:    br label %[[MERGE]]
+; CHECK:       [[MERGE]]:
+; CHECK-NEXT:    [[PHI:%.*]] = phi ptr [ [[GEP0]], %[[F0]] ], [ [[GEP1]], %[[F1]] ]
+; CHECK-NEXT:    ret ptr [[PHI]]
+;
+entry:
+  %gep0 = getelementptr inbounds i8, ptr %ptr, i64 16
+  %gep1 = getelementptr inbounds i8, ptr %ptr, i64 %off1
+  br i1 %cond, label %f1, label %f0
+
+f0:
+  br label %merge
+
+f1:
+  br label %merge
+
+merge:
+  %phi = phi ptr [ %gep0, %f0], [ %gep1, %f1 ]
+  ret ptr %phi
+}
+
+define ptr @gep_mixed_offset_2(i1 %cond, ptr %ptr, i64 %off0) {
+; CHECK-LABEL: define ptr @gep_mixed_offset_2(
+; CHECK-SAME: i1 [[COND:%.*]], ptr [[PTR:%.*]], i64 [[OFF0:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br i1 [[COND]], label %[[F1:.*]], label %[[F0:.*]]
+; CHECK:       [[F0]]:
+; CHECK-NEXT:    [[GEP0:%.*]] = getelementptr inbounds i8, ptr [[PTR]], i64 [[OFF0]]
+; CHECK-NEXT:    br label %[[MERGE:.*]]
+; CHECK:       [[F1]]:
+; CHECK-NEXT:    [[GEP1:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 32
+; CHECK-NEXT:    br label %[[MERGE]]
+; CHECK:       [[MERGE]]:
+; CHECK-NEXT:    [[PHI:%.*]] = phi ptr [ [[GEP0]], %[[F0]] ], [ [[GEP1]], %[[F1]] ]
+; CHECK-NEXT:    ret ptr [[PHI]]
+;
+entry:
+  %gep0 = getelementptr inbounds i8, ptr %ptr, i64 %off0
+  %gep1 = getelementptr inbounds i8, ptr %ptr, i64 32
+  br i1 %cond, label %f1, label %f0
+
+f0:
+  br label %merge
+
+f1:
+  br label %merge
+
+merge:
+  %phi = phi ptr [ %gep0, %f0], [ %gep1, %f1 ]
+  ret ptr %phi
+}
+
+define ptr @struct_constant_offset(i1 %cond, %foo %ptr) {
+; CHECK-LABEL: define ptr @struct_constant_offset(
+; CHECK-SAME: i1 [[COND:%.*]], [[FOO:%.*]] [[PTR:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[LANE_0:%.*]] = alloca [[FOO]], align 16
+; CHECK-NEXT:    br i1 [[COND]], label %[[F1:.*]], label %[[F0:.*]]
+; CHECK:       [[F0]]:
+; CHECK-NEXT:    [[GEP0:%.*]] = getelementptr inbounds nuw i8, ptr [[LANE_0]], i64 6
+; CHECK-NEXT:    br label %[[MERGE:.*]]
+; CHECK:       [[F1]]:
+; CHECK-NEXT:    [[GEP1:%.*]] = getelementptr inbounds nuw i8, ptr [[LANE_0]], i64 12
+; CHECK-NEXT:    br label %[[MERGE]]
+; CHECK:       [[MERGE]]:
+; CHECK-NEXT:    [[PHI:%.*]] = phi ptr [ [[GEP0]], %[[F0]] ], [ [[GEP1]], %[[F1]] ]
+; CHECK-NEXT:    ret ptr [[PHI]]
+;
+entry:
+  %lane.0 = alloca %foo, align 16
+  %gep0 = getelementptr inbounds %foo, ptr %lane.0, i64 1
+  %gep1 = getelementptr inbounds %foo, ptr %lane.0, i64 2
+  br i1 %cond, label %f1, label %f0
+
+f0:
+  br label %merge
+
+f1:
+  br label %merge
+
+merge:
+  %phi = phi ptr [ %gep0, %f0], [ %gep1, %f1 ]
+  ret ptr %phi
+}

>From fdd10a8d9200a128135484272868c3ebaa14b1a6 Mon Sep 17 00:00:00 2001
From: Jessica Del <Jessica.Del at amd.com>
Date: Wed, 17 Dec 2025 10:12:42 +0100
Subject: [PATCH 3/5] [InstCombine] Allow folding if only one operand is
 variable

Allow the transform if a phi is needed for the same operand.
---
 llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
index ba1865a2b5469..e0e77e061c6f5 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -587,7 +587,7 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
       // also requires a PHI, we'd be introducing more PHIs than we're
       // eliminating, which increases register pressure on entry to the PHI's
       // block.
-      if (NeededPhi)
+      if (NeededPhi && FixedOperands[Op] != nullptr)
         return nullptr;
 
       FixedOperands[Op] = nullptr; // Needs a PHI.

>From 45e50d0d9c2ad3427e7d2061de485d9c9d8ebd44 Mon Sep 17 00:00:00 2001
From: Jessica Del <Jessica.Del at amd.com>
Date: Wed, 7 Jan 2026 17:55:50 +0100
Subject: [PATCH 4/5] [InstCombinePHI] Prevent GEP arg folding for mixed
 offsets

Prevent folding of gep args into a phi if some of the geps have a
constant offset, while others have a variable offset.
---
 llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
index e0e77e061c6f5..32850ff120b41 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -579,6 +579,16 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
           isa<Constant>(GEP->getOperand(Op)))
         return nullptr;
 
+      // Don't merge if there is a mixture of constant and variable indeces for
+      // the same operand. If all the indeces are constant, the chance is higher
+      // that we can create a lookup table. Otherwise, we could pessimize the
+      // path.
+      if ((isa<Constant>(FirstInst->getOperand(Op)) &&
+           !isa<Constant>(GEP->getOperand(Op))) ||
+          (!isa<Constant>(FirstInst->getOperand(Op)) &&
+           isa<Constant>(GEP->getOperand(Op))))
+        return nullptr;
+
       if (FirstInst->getOperand(Op)->getType() !=
           GEP->getOperand(Op)->getType())
         return nullptr;

>From 4733b823d15b7fb0c07715f37cefb8df05ce68f3 Mon Sep 17 00:00:00 2001
From: Jessica Del <Jessica.Del at amd.com>
Date: Wed, 7 Jan 2026 18:41:39 +0100
Subject: [PATCH 5/5] [InstCombinePHI] Allow gep arg folding for constant
 non-struct indices

Allow folding of a gep with constant indices, as long as it does not
index a struct.

Change the check from one based on constant indices to one based on the
indexed type.
---
 .../Transforms/InstCombine/InstCombinePHI.cpp    | 16 +++++++++-------
 .../InstCombine/InstructionCombining.cpp         |  2 +-
 .../InstCombine/fold-phi-arg-gep-to-phi.ll       |  5 ++---
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
index 32850ff120b41..bf59cdd6f0d43 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -552,6 +552,7 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
   // Scan to see if all operands are the same opcode, and all have one user.
   for (Value *V : drop_begin(PN.incoming_values())) {
     GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V);
+    SmallVector<Value *, 16> GEPIndices;
     if (!GEP || !GEP->hasOneUser() ||
         GEP->getSourceElementType() != FirstInst->getSourceElementType() ||
         GEP->getNumOperands() != FirstInst->getNumOperands())
@@ -570,13 +571,14 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
       if (FirstInst->getOperand(Op) == GEP->getOperand(Op))
         continue;
 
-      // Don't merge two GEPs when two operands differ (introducing phi nodes)
-      // if one of the PHIs has a constant for the index.  The index may be
-      // substantially cheaper to compute for the constants, so making it a
-      // variable index could pessimize the path.  This also handles the case
-      // for struct indices, which must always be constant.
-      if (isa<Constant>(FirstInst->getOperand(Op)) ||
-          isa<Constant>(GEP->getOperand(Op)))
+      // Don't merge two GEPs if the GEP indices a struct, because struct
+      // indices must be constant.
+
+      if (Op > 0) // skip base pointer for indeces
+        GEPIndices.push_back(GEP->getOperand(Op));
+
+      if (GEP->getIndexedType(GEP->getSourceElementType(), GEPIndices)
+              ->isStructTy())
         return nullptr;
 
       // Don't merge if there is a mixture of constant and variable indeces for
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 49504b20fa1ec..d381c9984366b 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3104,7 +3104,7 @@ static bool shouldCanonicalizeGEPToPtrAdd(GetElementPtrInst &GEP) {
 
   // gep (gep %p, C1), %x, C2 is expanded so the two constants can
   // possibly be merged together.
-  auto PtrOpGep = dyn_cast<GEPOperator>(PtrOp);
+  auto *PtrOpGep = dyn_cast<GEPOperator>(PtrOp);
   return PtrOpGep && PtrOpGep->hasAllConstantIndices() &&
          any_of(GEP.indices(), [](Value *V) {
            const APInt *C;
diff --git a/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi.ll b/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi.ll
index 41f0bbac2e30b..a1b989c3ec476 100644
--- a/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi.ll
+++ b/llvm/test/Transforms/InstCombine/fold-phi-arg-gep-to-phi.ll
@@ -51,13 +51,12 @@ define ptr @gep_constant_offset(i1 %cond, ptr %ptr) {
 ; CHECK-NEXT:  [[ENTRY:.*:]]
 ; CHECK-NEXT:    br i1 [[COND]], label %[[F1:.*]], label %[[F0:.*]]
 ; CHECK:       [[F0]]:
-; CHECK-NEXT:    [[GEP0:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 16
 ; CHECK-NEXT:    br label %[[MERGE:.*]]
 ; CHECK:       [[F1]]:
-; CHECK-NEXT:    [[GEP1:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 32
 ; CHECK-NEXT:    br label %[[MERGE]]
 ; CHECK:       [[MERGE]]:
-; CHECK-NEXT:    [[PHI:%.*]] = phi ptr [ [[GEP0]], %[[F0]] ], [ [[GEP1]], %[[F1]] ]
+; CHECK-NEXT:    [[DOTPN:%.*]] = phi i64 [ 16, %[[F0]] ], [ 32, %[[F1]] ]
+; CHECK-NEXT:    [[PHI:%.*]] = getelementptr inbounds nuw i8, ptr [[PTR]], i64 [[DOTPN]]
 ; CHECK-NEXT:    ret ptr [[PHI]]
 ;
 entry:



More information about the llvm-commits mailing list