Skip to content

Commit e565a4f

Browse files
committed
[IR] Extract helper for GEPNoWrapFlags intersection (NFC)
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.
1 parent 7ca4128 commit e565a4f

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

llvm/include/llvm/IR/GEPNoWrapFlags.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ class GEPNoWrapFlags {
7474
return GEPNoWrapFlags(Flags & ~NUWFlag);
7575
}
7676

77+
/// Given (gep (gep p, x), y), determine the nowrap flags for (gep p, x+y).
78+
GEPNoWrapFlags intersectForOffsetAdd(GEPNoWrapFlags Other) const {
79+
GEPNoWrapFlags Res = *this & Other;
80+
// Without inbounds, we could only preserve nusw if we know that x + y does
81+
// not wrap.
82+
if (!Res.isInBounds() && Res.hasNoUnsignedSignedWrap())
83+
Res = Res.withoutNoUnsignedSignedWrap();
84+
return Res;
85+
}
86+
7787
bool operator==(GEPNoWrapFlags Other) const { return Flags == Other.Flags; }
7888
bool operator!=(GEPNoWrapFlags Other) const { return !(*this == Other); }
7989

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,12 +2349,7 @@ Instruction *InstCombinerImpl::narrowMathIfNoOverflow(BinaryOperator &BO) {
23492349
/// transform.
23502350
static GEPNoWrapFlags getMergedGEPNoWrapFlags(GEPOperator &GEP1,
23512351
GEPOperator &GEP2) {
2352-
GEPNoWrapFlags NW = GEP1.getNoWrapFlags() & GEP2.getNoWrapFlags();
2353-
// Without inbounds, we could only preserve nusw if we know that x + y does
2354-
// not wrap.
2355-
if (!NW.isInBounds())
2356-
NW = NW.withoutNoUnsignedSignedWrap();
2357-
return NW;
2352+
return GEP1.getNoWrapFlags().intersectForOffsetAdd(GEP2.getNoWrapFlags());
23582353
}
23592354

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

0 commit comments

Comments
 (0)