[llvm] [InstCombine] Fold icmp of gep chain with base (PR #144065)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 27 08:54:40 PDT 2025


================
@@ -708,10 +708,12 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
     return I;
   };
 
-  Value *PtrBase = GEPLHS->getOperand(0);
-  if (PtrBase == RHS && CanFold(GEPLHS->getNoWrapFlags())) {
+  CommonPointerBase Base = CommonPointerBase::compute(GEPLHS, RHS);
+  if (Base.Ptr == RHS && CanFold(Base.LHSNW)) {
     // ((gep Ptr, OFFSET) cmp Ptr)   ---> (OFFSET cmp 0).
-    Value *Offset = EmitGEPOffset(GEPLHS);
+    Type *IdxTy = DL.getIndexType(GEPLHS->getType());
+    Value *Offset =
+        EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEPs=*/false);
----------------
nikic wrote:

Created https://github.com/llvm/llvm-project/pull/146100 for just this change.

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


More information about the llvm-commits mailing list