[llvm] [InstCombine] Add limit for expansion of gep chains (PR #147065)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 4 23:40:02 PDT 2025


================
@@ -2144,13 +2144,35 @@ CommonPointerBase CommonPointerBase::compute(Value *LHS, Value *RHS) {
   return Base;
 }
 
+bool CommonPointerBase::isExpensive() const {
+  bool SeenConst = false;
+  unsigned NumGEPs = 0;
+  auto ProcessGEPs = [&SeenConst, &NumGEPs](ArrayRef<GEPOperator *> GEPs) {
+    bool SeenMultiUse = false;
+    for (GEPOperator *GEP : GEPs) {
+      // Only count GEPs after the first multi-use GEP. For the first one,
+      // we will directly reuse the offset.
----------------
dtcxzyw wrote:

I am confused why the offset of the first multi-use GEP can be reused. See the following case (or `multiple_geps_multi_use_below_limit`):
```
%gep1 = gep %p, %idx1
%gep2 = gep %gep1, %idx2 (multiuse)
```
Currently `EmitGEPOffsets` left `%gep2` as is, instead of rewriting `%gep2` to `gep %p, (%idx1 + %idx2)`.


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


More information about the llvm-commits mailing list