Skip to content

Commit

Permalink
[IR] Extract helper for GEPNoWrapFlags intersection (NFC)
Browse files Browse the repository at this point in the history
When combining two geps into one by adding the offsets, we have
to take some care when intersecting the flags, because nusw flags
cannot be straightforwardly preserved.

Add a helper for this on GEPNoWrapFlags so we won't have to repeat
this logic in various places.
  • Loading branch information
nikic authored and Sterling-Augustine committed Oct 3, 2024
1 parent 9c86093 commit 32e1cf0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 10 additions & 0 deletions llvm/include/llvm/IR/GEPNoWrapFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ class GEPNoWrapFlags {
return GEPNoWrapFlags(Flags & ~NUWFlag);
}

/// Given (gep (gep p, x), y), determine the nowrap flags for (gep p, x+y).
GEPNoWrapFlags intersectForOffsetAdd(GEPNoWrapFlags Other) const {
GEPNoWrapFlags Res = *this & Other;
// Without inbounds, we could only preserve nusw if we know that x + y does
// not wrap.
if (!Res.isInBounds() && Res.hasNoUnsignedSignedWrap())
Res = Res.withoutNoUnsignedSignedWrap();
return Res;
}

bool operator==(GEPNoWrapFlags Other) const { return Flags == Other.Flags; }
bool operator!=(GEPNoWrapFlags Other) const { return !(*this == Other); }

Expand Down
7 changes: 1 addition & 6 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2349,12 +2349,7 @@ Instruction *InstCombinerImpl::narrowMathIfNoOverflow(BinaryOperator &BO) {
/// transform.
static GEPNoWrapFlags getMergedGEPNoWrapFlags(GEPOperator &GEP1,
GEPOperator &GEP2) {
GEPNoWrapFlags NW = GEP1.getNoWrapFlags() & GEP2.getNoWrapFlags();
// Without inbounds, we could only preserve nusw if we know that x + y does
// not wrap.
if (!NW.isInBounds())
NW = NW.withoutNoUnsignedSignedWrap();
return NW;
return GEP1.getNoWrapFlags().intersectForOffsetAdd(GEP2.getNoWrapFlags());
}

/// Thread a GEP operation with constant indices through the constant true/false
Expand Down

0 comments on commit 32e1cf0

Please sign in to comment.