[llvm] [InstCombine] Modify zero-indexed GEPs in place rather than cloning (PR #185053)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 9 14:51:56 PDT 2026


================
@@ -995,6 +995,15 @@ static Instruction *replaceGEPIdxWithZero(InstCombinerImpl &IC, Value *Ptr,
       NewGEPI->setOperand(Idx,
         ConstantInt::get(GEPI->getOperand(Idx)->getType(), 0));
       IC.InsertNewInstBefore(NewGEPI, GEPI->getIterator());
+      // If the memory instruction is guaranteed to execute whenever the GEP
+      // does, the dereference proves the index is unconditionally zero.
+      // Replace the GEP for all users so they all benefit.
+      if (GEPI->getParent() == MemI.getParent() &&
+          isGuaranteedToTransferExecutionToSuccessor(GEPI->getIterator(),
+                                                     MemI.getIterator())) {
+        IC.replaceInstUsesWith(*GEPI, NewGEPI);
+        IC.eraseInstFromFunction(*GEPI);
----------------
nikic wrote:

Ah yes, you're right. Usually the replaceOperand() call would add it to the worklist, but here we RAUW before we get there. In that case the explicit erase is fine.

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


More information about the llvm-commits mailing list