[llvm-branch-commits] [llvm] [InstCombine] Improve inbounds preservation for ADD+GEP -> GEP+GEP (PR #135155)
Nikita Popov via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Apr 10 03:54:49 PDT 2025
================
@@ -3087,12 +3087,22 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
return nullptr;
if (GEP.getNumIndices() == 1) {
- // We can only preserve inbounds if the original gep is inbounds, the add
- // is nsw, and the add operands are non-negative.
- auto CanPreserveInBounds = [&](bool AddIsNSW, Value *Idx1, Value *Idx2) {
+ auto CanPreserveNoWrapFlags = [&](bool AddIsNSW, bool AddIsNUW, Value *Idx1,
+ Value *Idx2) {
+ // Preserve "inbounds nuw" if the original gep is "inbounds nuw",
+ // and the add is "nuw".
+ if (GEP.isInBounds() && GEP.hasNoUnsignedWrap() && AddIsNUW)
+ return GEPNoWrapFlags::inBounds() | GEPNoWrapFlags::noUnsignedWrap();
+ // Preserve "inbounds" if the original gep is "inbounds", the add
+ // is "nsw", and the add operands are non-negative.
SimplifyQuery Q = SQ.getWithInstruction(&GEP);
- return GEP.isInBounds() && AddIsNSW && isKnownNonNegative(Idx1, Q) &&
- isKnownNonNegative(Idx2, Q);
+ if (GEP.isInBounds() && AddIsNSW && isKnownNonNegative(Idx1, Q) &&
+ isKnownNonNegative(Idx2, Q))
+ return GEPNoWrapFlags::inBounds();
----------------
nikic wrote:
Is it actually still necessary to explicitly handle this case? If we have an add nsw with nonneg operands, I think we should infer nuw on both add and gep and can then use the new code path?
https://github.com/llvm/llvm-project/pull/135155
More information about the llvm-branch-commits
mailing list