[llvm] [LV] Add support for cmp reductions with decreasing IVs. (PR #140451)
Alexey Bataev via llvm-commits
llvm-commits at lists.llvm.org
Wed May 28 04:39:08 PDT 2025
================
@@ -710,21 +713,39 @@ RecurrenceDescriptor::isFindLastIVPattern(Loop *TheLoop, PHINode *OrigPhi,
return false;
const SCEV *Step = AR->getStepRecurrence(SE);
- if (!SE.isKnownPositive(Step))
+ if (Kind == RecurKind::FindFirstIVUMin ||
+ Kind == RecurKind::FindFirstIVSMin) {
+ if (!SE.isKnownNegative(Step))
+ return false;
+ } else if (!SE.isKnownPositive(Step))
return false;
const ConstantRange IVRange = SE.getSignedRange(AR);
unsigned NumBits = Ty->getIntegerBitWidth();
- // Keep the minimum value of the recurrence type as the sentinel value.
- // The maximum acceptable range for the increasing induction variable,
- // called the valid range, will be defined as
+ // Keep the minimum (FindLast) or maximum (FindFirst) value of the
+ // recurrence type as the sentinel value. The maximum acceptable range for
+ // the induction variable, called the valid range, will be defined as
// [<sentinel value> + 1, <sentinel value>)
- // where <sentinel value> is SignedMin(<recurrence type>)
+ // where <sentinel value> is SignedMin(<recurrence type>) for FindLast or
+ // UnsignedMax(<recurrence type>) for FindFirst.
// TODO: This range restriction can be lifted by adding an additional
// virtual OR reduction.
- const APInt Sentinel = APInt::getSignedMinValue(NumBits);
- const ConstantRange ValidRange =
- ConstantRange::getNonEmpty(Sentinel + 1, Sentinel);
+ const APInt Sentinel = Kind == RecurKind::FindFirstIVUMin
+ ? APInt::getMaxValue(NumBits)
+ : (Kind == RecurKind::FindFirstIVSMin
+ ? APInt::getSignedMaxValue(NumBits)
+ : APInt::getSignedMinValue(NumBits));
+ ConstantRange ValidRange = ConstantRange::getEmpty(NumBits);
+ if (Kind == RecurKind::FindFirstIVSMin)
+ ValidRange =
+ ConstantRange::getNonEmpty(APInt::getSignedMinValue(NumBits),
+ APInt::getSignedMaxValue(NumBits) - 1);
+ else {
----------------
alexey-bataev wrote:
Braces
https://github.com/llvm/llvm-project/pull/140451
More information about the llvm-commits
mailing list