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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 5 00:20: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.
----------------
nikic wrote:

For `%gep2` we will just use `%idx2`, which is free. When adding `%gep1` we generate `%idx1 + %idx2` which adds cost.

And yes, we could also rewrite the GEP, in which case we would only have to count all but the first multi-use GEPs and could ignore one-use GEPs entirely.

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


More information about the llvm-commits mailing list