@@ -54,8 +54,10 @@ bool RecurrenceDescriptor::isIntegerRecurrenceKind(RecurKind Kind) {
5454 case RecurKind::UMin:
5555 case RecurKind::IAnyOf:
5656 case RecurKind::FAnyOf:
57- case RecurKind::IFindLastIV:
58- case RecurKind::FFindLastIV:
57+ case RecurKind::IFindLastIncIV:
58+ case RecurKind::FFindLastIncIV:
59+ case RecurKind::IFindLastDecIV:
60+ case RecurKind::FFindLastDecIV:
5961 return true ;
6062 }
6163 return false ;
@@ -664,6 +666,8 @@ RecurrenceDescriptor::isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
664666 : RecurKind::FAnyOf);
665667}
666668
669+ enum class LoopInductionDirection { None, Increasing, Decreasing };
670+
667671// We are looking for loops that do something like this:
668672// int r = 0;
669673// for (int i = 0; i < n; i++) {
@@ -711,47 +715,65 @@ RecurrenceDescriptor::isFindLastIVPattern(Loop *Loop, PHINode *OrigPhi,
711715 else
712716 return InstDesc (false , I);
713717
714- auto IsIncreasingLoopInduction = [&SE, &Loop](Value *V) {
715- if (!SE)
716- return false ;
717-
718+ auto GetLoopInduction = [&SE, &Loop](Value *V) {
718719 Type *Ty = V->getType ();
719- if (!SE->isSCEVable (Ty))
720- return false ;
720+ if (!SE || !SE ->isSCEVable (Ty))
721+ return LoopInductionDirection::None ;
721722
722723 auto *AR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV (V));
723724 if (!AR)
724- return false ;
725-
726- const SCEV *Step = AR->getStepRecurrence (*SE);
727- if (!SE->isKnownPositive (Step))
728- return false ;
725+ return LoopInductionDirection::None;
729726
730727 const ConstantRange IVRange = SE->getSignedRange (AR);
731728 unsigned NumBits = Ty->getIntegerBitWidth ();
732- // Keep the minmum value of the recurrence type as the sentinel value.
733- // The maximum acceptable range for the increasing induction variable,
734- // called the valid range, will be defined as
735- // [<sentinel value> + 1, SignedMin(<recurrence type>))
736- // TODO: This range restriction can be lifted by adding an additional
737- // virtual OR reduction.
738- const APInt Sentinel = APInt::getSignedMinValue (NumBits);
739- const ConstantRange ValidRange = ConstantRange::getNonEmpty (
740- Sentinel + 1 , APInt::getSignedMinValue (NumBits));
741- LLVM_DEBUG (dbgs () << " LV: FindLastIV valid range is " << ValidRange
742- << " , and the signed range of " << *AR << " is "
743- << IVRange << " \n " );
744- return ValidRange.contains (IVRange);
729+ const SCEV *Step = AR->getStepRecurrence (*SE);
730+
731+ if (SE->isKnownPositive (Step)) {
732+ // For increasing IV, keep the minimum value of the recurrence type as the
733+ // sentinel value. The maximum acceptable range will be defined as
734+ // [<sentinel value> + 1, <sentinel value>)
735+ // TODO: This range restriction can be lifted by adding an additional
736+ // virtual OR reduction.
737+ const APInt Sentinel = APInt::getSignedMinValue (NumBits);
738+ const ConstantRange ValidRange =
739+ ConstantRange::getNonEmpty (Sentinel + 1 , Sentinel);
740+ LLVM_DEBUG (dbgs () << " LV: FindLastIncIV valid range is " << ValidRange
741+ << " , and the signed range of " << *AR << " is "
742+ << IVRange << " \n " );
743+ if (ValidRange.contains (IVRange))
744+ return LoopInductionDirection::Increasing;
745+ } else if (SE->isKnownNegative (Step)) {
746+ // For decreasing IV, keep the maximum value of the recurrence type as the
747+ // sentinel value. The maximum acceptable range will be defined as
748+ // [<sentinel value> + 1, <sentinel value>)
749+ const APInt Sentinel = APInt::getSignedMaxValue (NumBits);
750+ const ConstantRange ValidRange =
751+ ConstantRange::getNonEmpty (Sentinel + 1 , Sentinel);
752+ LLVM_DEBUG (dbgs () << " LV: FindLastDecIV valid range is " << ValidRange
753+ << " , and the signed range of " << *AR << " is "
754+ << IVRange << " \n " );
755+ if (ValidRange.contains (IVRange))
756+ return LoopInductionDirection::Decreasing;
757+ }
758+ return LoopInductionDirection::None;
745759 };
746760
747761 // We are looking for selects of the form:
748762 // select(cmp(), phi, loop_induction) or
749763 // select(cmp(), loop_induction, phi)
750- if (!IsIncreasingLoopInduction (NonRdxPhi))
751- return InstDesc (false , I);
752-
753- return InstDesc (I, isa<ICmpInst>(I->getOperand (0 )) ? RecurKind::IFindLastIV
754- : RecurKind::FFindLastIV);
764+ switch (GetLoopInduction (NonRdxPhi)) {
765+ case LoopInductionDirection::None:
766+ break ;
767+ case LoopInductionDirection::Increasing:
768+ return InstDesc (I, isa<ICmpInst>(I->getOperand (0 ))
769+ ? RecurKind::IFindLastIncIV
770+ : RecurKind::FFindLastIncIV);
771+ case LoopInductionDirection::Decreasing:
772+ return InstDesc (I, isa<ICmpInst>(I->getOperand (0 ))
773+ ? RecurKind::IFindLastDecIV
774+ : RecurKind::FFindLastDecIV);
775+ }
776+ return InstDesc (false , I);
755777}
756778
757779RecurrenceDescriptor::InstDesc
@@ -995,8 +1017,8 @@ bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
9951017 << *Phi << " \n " );
9961018 return true ;
9971019 }
998- if (AddReductionVar (Phi, RecurKind::IFindLastIV , TheLoop, FMF, RedDes, DB, AC ,
999- DT, SE)) {
1020+ if (AddReductionVar (Phi, RecurKind::IFindLastIncIV , TheLoop, FMF, RedDes, DB,
1021+ AC, DT, SE)) {
10001022 LLVM_DEBUG (dbgs () << " Found a FindLastIV reduction PHI." << *Phi << " \n " );
10011023 return true ;
10021024 }
@@ -1189,9 +1211,12 @@ Value *RecurrenceDescriptor::getRecurrenceIdentity(RecurKind K, Type *Tp,
11891211 case RecurKind::FAnyOf:
11901212 return getRecurrenceStartValue ();
11911213 break ;
1192- case RecurKind::IFindLastIV :
1193- case RecurKind::FFindLastIV :
1214+ case RecurKind::IFindLastIncIV :
1215+ case RecurKind::FFindLastIncIV :
11941216 return getRecurrenceIdentity (RecurKind::SMax, Tp, FMF);
1217+ case RecurKind::IFindLastDecIV:
1218+ case RecurKind::FFindLastDecIV:
1219+ return getRecurrenceIdentity (RecurKind::SMin, Tp, FMF);
11951220 default :
11961221 llvm_unreachable (" Unknown recurrence kind" );
11971222 }
@@ -1219,14 +1244,16 @@ unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind) {
12191244 case RecurKind::UMax:
12201245 case RecurKind::UMin:
12211246 case RecurKind::IAnyOf:
1222- case RecurKind::IFindLastIV:
1247+ case RecurKind::IFindLastIncIV:
1248+ case RecurKind::IFindLastDecIV:
12231249 return Instruction::ICmp;
12241250 case RecurKind::FMax:
12251251 case RecurKind::FMin:
12261252 case RecurKind::FMaximum:
12271253 case RecurKind::FMinimum:
12281254 case RecurKind::FAnyOf:
1229- case RecurKind::FFindLastIV:
1255+ case RecurKind::FFindLastIncIV:
1256+ case RecurKind::FFindLastDecIV:
12301257 return Instruction::FCmp;
12311258 default :
12321259 llvm_unreachable (" Unknown recurrence operation" );
0 commit comments