[llvm] [InstCombine] Lower multi-dimensional GEP to ptradd (PR #150383)
Usha Gupta via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 24 00:49:22 PDT 2025
https://github.com/usha1830 created https://github.com/llvm/llvm-project/pull/150383
This change will help canonicalize multi-dimensional array geps with exactly one variable index and all other indices constant into a flat, single-index gep over the element type. This would cover cases such as:
`%gep = getelementptr [9 x [9 x [9 x i32]]], ptr @arr, i64 0, i64 %i, i64 2, i64 3`
>From fbd143dc0ba7a90821de634a5787027f59a9babc Mon Sep 17 00:00:00 2001
From: Usha Gupta <usha.gupta at arm.com>
Date: Thu, 24 Jul 2025 07:26:14 +0000
Subject: [PATCH] [InstCombine] Lower multi-dimensional GEP to ptradd
---
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index e2a9255ca9c6e..9b148e523b7a7 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2995,6 +2995,15 @@ static bool shouldCanonicalizeGEPToPtrAdd(GetElementPtrInst &GEP) {
m_Shl(m_Value(), m_ConstantInt())))))
return true;
+ // Flatten multidimensional GEPs with one variable index.
+ unsigned NumVarIndices = 0;
+ for (unsigned i = 1; i < GEP.getNumOperands(); ++i) {
+ if (!isa<ConstantInt>(GEP.getOperand(i)))
+ ++NumVarIndices;
+ }
+ if (NumVarIndices == 1)
+ return true;
+
// gep (gep %p, C1), %x, C2 is expanded so the two constants can
// possibly be merged together.
auto PtrOpGep = dyn_cast<GEPOperator>(PtrOp);
More information about the llvm-commits
mailing list