[llvm-branch-commits] [llvm] [InstCombine] Improve inbounds preservation for ADD+GEP -> GEP+GEP (PR #135155)
Björn Pettersson via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Apr 11 04:02:40 PDT 2025
================
@@ -3086,13 +3086,51 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
if (GEPType->isVectorTy())
return nullptr;
+ if (!GEP.isInBounds()) {
+ unsigned IdxWidth =
+ DL.getIndexSizeInBits(PtrOp->getType()->getPointerAddressSpace());
+ APInt BasePtrOffset(IdxWidth, 0);
+ Value *UnderlyingPtrOp =
+ PtrOp->stripAndAccumulateInBoundsConstantOffsets(DL, BasePtrOffset);
+ bool CanBeNull, CanBeFreed;
+ uint64_t DerefBytes = UnderlyingPtrOp->getPointerDereferenceableBytes(
+ DL, CanBeNull, CanBeFreed);
+ if (!CanBeNull && !CanBeFreed && DerefBytes != 0) {
+ if (GEP.accumulateConstantOffset(DL, BasePtrOffset) &&
+ BasePtrOffset.isNonNegative()) {
+ APInt AllocSize(IdxWidth, DerefBytes);
+ if (BasePtrOffset.ule(AllocSize)) {
+ return GetElementPtrInst::CreateInBounds(
+ GEP.getSourceElementType(), PtrOp, Indices, GEP.getName());
+ }
+ }
+ }
+ }
+
+ // nusw + nneg -> nuw
+ if (GEP.hasNoUnsignedSignedWrap() && !GEP.hasNoUnsignedWrap() &&
+ all_of(GEP.indices(), [&](Value *Idx) {
+ return isKnownNonNegative(Idx, SQ.getWithInstruction(&GEP));
+ })) {
+ GEP.setNoWrapFlags(GEP.getNoWrapFlags() | GEPNoWrapFlags::noUnsignedWrap());
+ return &GEP;
+ }
+
+ // These rewrites is trying to preserve inbounds/nuw attributes. So we want to
+ // do this after having tried to derive "nuw" above.
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) {
- SimplifyQuery Q = SQ.getWithInstruction(&GEP);
- return GEP.isInBounds() && AddIsNSW && isKnownNonNegative(Idx1, Q) &&
- isKnownNonNegative(Idx2, Q);
+ auto GetPreservedNoWrapFlags = [&](bool AddIsNUW, Value *Idx1,
+ Value *Idx2) {
----------------
bjope wrote:
Yes. Done.
https://github.com/llvm/llvm-project/pull/135155
More information about the llvm-branch-commits
mailing list