[llvm] [SROA] Unfold gep of index phi (PR #83087)

Artem Belevich via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 26 17:54:16 PST 2024


================
@@ -4023,64 +4023,88 @@ class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
     Visited.insert(NSelI);
     enqueueUsers(*NSelI);
 
-    LLVM_DEBUG(dbgs() << "\n          to: " << *NTrue
-                      << "\n              " << *NFalse
-                      << "\n              " << *NSel << '\n');
+    LLVM_DEBUG(dbgs() << "\n          to: " << *NTrue << "\n              "
+                      << *NFalse << "\n              " << *NSel << '\n');
 
     return true;
   }
 
-  // Fold gep (phi ptr1, ptr2) => phi gep(ptr1), gep(ptr2)
+  // Fold gep (phi ptr1, ptr2), idx
+  //   => phi ((gep ptr1, idx), (gep ptr2, idx))
+  // and  gep ptr, (phi idx1, idx2)
+  //   => phi ((gep ptr, idx1), (gep ptr, idx2))
   bool foldGEPPhi(GetElementPtrInst &GEPI) {
-    if (!GEPI.hasAllConstantIndices())
+    // Check whether the GEP has exactly one phi operand and all indices
+    // will become constant after the transform.
----------------
Artem-B wrote:

single phi-only will do for now.

That said, it would give us a bit more flexibility, if we would still expand one phi at a time, but would do that for GEPs with up to N phis we could convert that way.

The folding itself will work pretty much as is, and up-fron thecks on the number of phis we can handles guarantees that the recursion will end, as all expended GEPs will have one less expandable phi.

Instead of bailing out on the first unaceptable phi, we can count acceptable ones, and only expand the last one, if we've found no more than N. With N=1 we'll have the behavior identical to the current version of the algorithm, but if we add a CLI option to control N, we can then experiment on real world code and see whether N>1 buys us anything in practice.

Up to you.

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


More information about the llvm-commits mailing list