@@ -758,9 +758,8 @@ RecurrenceDescriptor::isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
758758// will either be the initial value (0) if the condition was never met, or the
759759// value of a[i] in the most recent loop iteration where the condition was met.
760760RecurrenceDescriptor::InstDesc
761- RecurrenceDescriptor::isFindPattern (RecurKind Kind, Loop *TheLoop,
762- PHINode *OrigPhi, Instruction *I,
763- ScalarEvolution &SE) {
761+ RecurrenceDescriptor::isFindPattern (Loop *TheLoop, PHINode *OrigPhi,
762+ Instruction *I, ScalarEvolution &SE) {
764763 // TODO: Support the vectorization of FindLastIV when the reduction phi is
765764 // used by more than one select instruction. This vectorization is only
766765 // performed when the SCEV of each increasing induction variable used by the
@@ -781,17 +780,6 @@ RecurrenceDescriptor::isFindPattern(RecurKind Kind, Loop *TheLoop,
781780 m_Value (NonRdxPhi)))))
782781 return InstDesc (false , I);
783782
784- if (isFindLastRecurrenceKind (Kind)) {
785- // Must be an integer scalar.
786- Type *Type = OrigPhi->getType ();
787- if (!Type->isIntegerTy ())
788- return InstDesc (false , I);
789-
790- // FIXME: Support more complex patterns, including multiple selects.
791- // The Select must be used only outside the loop and by the PHI.
792- return InstDesc (I, RecurKind::FindLast);
793- }
794-
795783 // Returns either FindFirstIV/FindLastIV, if such a pattern is found, or
796784 // std::nullopt.
797785 auto GetRecurKind = [&](Value *V) -> std::optional<RecurKind> {
@@ -805,8 +793,9 @@ RecurrenceDescriptor::isFindPattern(RecurKind Kind, Loop *TheLoop,
805793 m_SpecificLoop (TheLoop))))
806794 return std::nullopt ;
807795
808- if ((isFindFirstIVRecurrenceKind (Kind) && !SE.isKnownNegative (Step)) ||
809- (isFindLastIVRecurrenceKind (Kind) && !SE.isKnownPositive (Step)))
796+ // We must have a known positive or negative step for FindIV
797+ const bool PositiveStep = SE.isKnownPositive (Step);
798+ if (!PositiveStep && !SE.isKnownNegative (Step))
810799 return std::nullopt ;
811800
812801 // Check if the minimum (FindLast) or maximum (FindFirst) value of the
@@ -822,7 +811,7 @@ RecurrenceDescriptor::isFindPattern(RecurKind Kind, Loop *TheLoop,
822811 IsSigned ? SE.getSignedRange (AR) : SE.getUnsignedRange (AR);
823812 unsigned NumBits = Ty->getIntegerBitWidth ();
824813 ConstantRange ValidRange = ConstantRange::getEmpty (NumBits);
825- if (isFindLastIVRecurrenceKind (Kind) ) {
814+ if (PositiveStep ) {
826815 APInt Sentinel = IsSigned ? APInt::getSignedMinValue (NumBits)
827816 : APInt::getMinValue (NumBits);
828817 ValidRange = ConstantRange::getNonEmpty (Sentinel + 1 , Sentinel);
@@ -836,26 +825,22 @@ RecurrenceDescriptor::isFindPattern(RecurKind Kind, Loop *TheLoop,
836825 APInt::getMinValue (NumBits), APInt::getMaxValue (NumBits) - 1 );
837826 }
838827
839- LLVM_DEBUG (dbgs () << " LV: "
840- << (isFindLastIVRecurrenceKind (Kind) ? " FindLastIV"
841- : " FindFirstIV" )
842- << " valid range is " << ValidRange
843- << " , and the range of " << *AR << " is " << IVRange
844- << " \n " );
828+ LLVM_DEBUG (
829+ dbgs () << " LV: " << (PositiveStep ? " FindLastIV" : " FindFirstIV" )
830+ << " valid range is " << ValidRange << " , and the range of "
831+ << *AR << " is " << IVRange << " \n " );
845832
846833 // Ensure the induction variable does not wrap around by verifying that
847834 // its range is fully contained within the valid range.
848835 return ValidRange.contains (IVRange);
849836 };
850- if (isFindLastIVRecurrenceKind (Kind) ) {
837+ if (PositiveStep ) {
851838 if (CheckRange (true ))
852839 return RecurKind::FindLastIVSMax;
853840 if (CheckRange (false ))
854841 return RecurKind::FindLastIVUMax;
855842 return std::nullopt ;
856843 }
857- assert (isFindFirstIVRecurrenceKind (Kind) &&
858- " Kind must either be a FindLastIV or FindFirstIV" );
859844
860845 if (CheckRange (true ))
861846 return RecurKind::FindFirstIVSMin;
@@ -867,7 +852,8 @@ RecurrenceDescriptor::isFindPattern(RecurKind Kind, Loop *TheLoop,
867852 if (auto RK = GetRecurKind (NonRdxPhi))
868853 return InstDesc (I, *RK);
869854
870- return InstDesc (false , I);
855+ // If the recurrence is not specific to an IV, return a generic FindLast.
856+ return InstDesc (I, RecurKind::FindLast);
871857}
872858
873859RecurrenceDescriptor::InstDesc
@@ -1001,8 +987,8 @@ RecurrenceDescriptor::InstDesc RecurrenceDescriptor::isRecurrenceInstr(
1001987 Kind == RecurKind::Add || Kind == RecurKind::Mul ||
1002988 Kind == RecurKind::Sub || Kind == RecurKind::AddChainWithSubs)
1003989 return isConditionalRdxPattern (I);
1004- if (( isFindIVRecurrenceKind ( Kind) || isFindLastRecurrenceKind (Kind) ) && SE)
1005- return isFindPattern (Kind, L, OrigPhi, I, *SE);
990+ if (isFindRecurrenceKind ( Kind) && SE)
991+ return isFindPattern (L, OrigPhi, I, *SE);
1006992 [[fallthrough]];
1007993 case Instruction::FCmp:
1008994 case Instruction::ICmp:
@@ -1142,14 +1128,9 @@ bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
11421128 << " \n " );
11431129 return true ;
11441130 }
1145- if (AddReductionVar (Phi, RecurKind::FindLastIVSMax, TheLoop, FMF, RedDes, DB,
1146- AC, DT, SE)) {
1147- LLVM_DEBUG (dbgs () << " Found a FindLastIV reduction PHI." << *Phi << " \n " );
1148- return true ;
1149- }
1150- if (AddReductionVar (Phi, RecurKind::FindFirstIVSMin, TheLoop, FMF, RedDes, DB,
1151- AC, DT, SE)) {
1152- LLVM_DEBUG (dbgs () << " Found a FindFirstIV reduction PHI." << *Phi << " \n " );
1131+ if (AddReductionVar (Phi, RecurKind::FindLast, TheLoop, FMF, RedDes, DB, AC,
1132+ DT, SE)) {
1133+ LLVM_DEBUG (dbgs () << " Found a Find reduction PHI." << *Phi << " \n " );
11531134 return true ;
11541135 }
11551136 if (AddReductionVar (Phi, RecurKind::FMul, TheLoop, FMF, RedDes, DB, AC, DT,
@@ -1199,11 +1180,6 @@ bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
11991180 << " \n " );
12001181 return true ;
12011182 }
1202- if (AddReductionVar (Phi, RecurKind::FindLast, TheLoop, FMF, RedDes, DB, AC,
1203- DT, SE)) {
1204- LLVM_DEBUG (dbgs () << " Found a FindLast reduction PHI." << *Phi << " \n " );
1205- return true ;
1206- }
12071183 // Not a reduction of known type.
12081184 return false ;
12091185}
@@ -1329,7 +1305,6 @@ unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind) {
13291305 case RecurKind::FMinimumNum:
13301306 return Instruction::FCmp;
13311307 case RecurKind::FindLast:
1332- return Instruction::Select;
13331308 case RecurKind::AnyOf:
13341309 case RecurKind::FindFirstIVSMin:
13351310 case RecurKind::FindFirstIVUMin:
0 commit comments