[llvm] [InstCombine] Optimize GEP comparisons with constant offsets (PR #208547)
Ömer Sinan Ağacan via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 13 03:51:19 PDT 2026
================
@@ -815,14 +815,35 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
}
}
- if (Base.Ptr && CanFold(Base.LHSNW & Base.RHSNW) && !Base.isExpensive()) {
+ if (Base.Ptr && !Base.isExpensive()) {
// ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
- Type *IdxTy = DL.getIndexType(GEPLHS->getType());
- Value *L =
- EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, /*RewriteGEP=*/true);
- Value *R =
- EmitGEPOffsets(Base.RHSGEPs, Base.RHSNW, IdxTy, /*RewriteGEP=*/true);
- return NewICmp(Base.LHSNW & Base.RHSNW, L, R);
+ bool DoFold = CanFold(Base.LHSNW & Base.RHSNW);
+
+ if (!DoFold && Base.Ptr->getType()->isPointerTy()) {
+ // Without the flags, we can still fold if the offsets are constant and
+ // they cross the base's alignment boundary the same number of times, so
+ // either both arguments will wrap, or none of them will.
+ unsigned BW = DL.getIndexTypeSizeInBits(GEPLHS->getType());
+ APInt Alignment = APInt(BW, Base.Ptr->getPointerAlignment(DL).value());
+ APInt LOff(BW, 0);
+ APInt ROff(BW, 0);
+ if (GEPLHS->stripAndAccumulateConstantOffsets(
+ DL, LOff, /*AllowNonInbounds=*/true) == Base.Ptr &&
+ RHS->stripAndAccumulateConstantOffsets(
+ DL, ROff, /*AllowNonInbounds=*/true) == Base.Ptr)
+ DoFold =
+ APIntOps::RoundingSDiv(LOff, Alignment, APInt::Rounding::DOWN) ==
+ APIntOps::RoundingSDiv(ROff, Alignment, APInt::Rounding::DOWN);
+ }
+
+ if (DoFold) {
+ Type *IdxTy = DL.getIndexType(GEPLHS->getType());
+ Value *L = EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy,
+ /*RewriteGEP=*/true);
+ Value *R = EmitGEPOffsets(Base.RHSGEPs, Base.RHSNW, IdxTy,
+ /*RewriteGEP=*/true);
+ return NewICmp(Base.LHSNW & Base.RHSNW, L, R);
----------------
osa1 wrote:
This block is the same as before, just indented.
(so transformation is the same, we just do it in more cases now)
https://github.com/llvm/llvm-project/pull/208547
More information about the llvm-commits
mailing list