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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 27 04:18:00 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);
----------------
dtcxzyw wrote:

```suggestion
        EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEPs=*/true);
```
Rewrite GEPs to avoid duplicating the arithmetic: https://github.com/dtcxzyw/llvm-opt-benchmark/pull/2510

We can land this later together with https://github.com/llvm/llvm-project/blob/5fdcb35aaa4e01f001780f2dcda38e16395d7d18/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp#L2156-L2159



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


More information about the llvm-commits mailing list