[llvm] [InstCombine] Lower multi-dimensional GEP to ptradd (PR #150383)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 24 00:49:53 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Usha Gupta (usha1830)

<details>
<summary>Changes</summary>


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`




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


1 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+9) 


``````````diff
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);

``````````

</details>


https://github.com/llvm/llvm-project/pull/150383


More information about the llvm-commits mailing list