[llvm] [InstCombinePHI] Enable gep arg fold transform with constant indices (PR #174981)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 23 11:03:26 PST 2026


================
@@ -566,16 +567,24 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
       AllBasePointersAreAllocas = false;
 
     // Compare the operand lists.
+    gep_type_iterator TypeIter = gep_type_begin(GEP);
     for (unsigned Op = 0, E = FirstInst->getNumOperands(); Op != E; ++Op) {
       if (FirstInst->getOperand(Op) == GEP->getOperand(Op))
         continue;
 
-      // Don't merge two GEPs when two operands differ (introducing phi nodes)
-      // if one of the PHIs has a constant for the index.  The index may be
-      // substantially cheaper to compute for the constants, so making it a
-      // variable index could pessimize the path.  This also handles the case
-      // for struct indices, which must always be constant.
-      if (isa<Constant>(FirstInst->getOperand(Op)) ||
+      // Don't merge two GEPs if the GEP indices a struct, because struct
+      // indices must be constant.
+      if (Op > 0) { // skip pointer operand
+        if (TypeIter.isStruct())
+          return nullptr;
+        ++TypeIter;
----------------
dtcxzyw wrote:

> The iterator starts at op + 1, so at the indices if I understand correctly, while the for-loop starts at operand 0.

Yes. TBH I don't think the base pointer should share the same merging logic with indices :(


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


More information about the llvm-commits mailing list