@@ -715,36 +715,6 @@ RecurrenceDescriptor::isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
715715 return InstDesc (I, RecurKind::AnyOf);
716716}
717717
718- bool RecurrenceDescriptor::isValidIVRangeForFindIV (const SCEVAddRecExpr *AR,
719- bool IsSigned,
720- bool IsFindFirstIV,
721- ScalarEvolution &SE) {
722- const ConstantRange IVRange =
723- IsSigned ? SE.getSignedRange (AR) : SE.getUnsignedRange (AR);
724- unsigned NumBits = AR->getType ()->getIntegerBitWidth ();
725- ConstantRange ValidRange = ConstantRange::getEmpty (NumBits);
726-
727- if (IsFindFirstIV) {
728- if (IsSigned)
729- ValidRange =
730- ConstantRange::getNonEmpty (APInt::getSignedMinValue (NumBits),
731- APInt::getSignedMaxValue (NumBits) - 1 );
732- else
733- ValidRange = ConstantRange::getNonEmpty (APInt::getMinValue (NumBits),
734- APInt::getMaxValue (NumBits) - 1 );
735- } else {
736- APInt Sentinel = IsSigned ? APInt::getSignedMinValue (NumBits)
737- : APInt::getMinValue (NumBits);
738- ValidRange = ConstantRange::getNonEmpty (Sentinel + 1 , Sentinel);
739- }
740-
741- LLVM_DEBUG (dbgs () << " LV: " << (IsFindFirstIV ? " FindFirstIV" : " FindLastIV" )
742- << " valid range is " << ValidRange << " , and the range of "
743- << *AR << " is " << IVRange << " \n " );
744-
745- return ValidRange.contains (IVRange);
746- }
747-
748718// We are looking for loops that do something like this:
749719// int r = 0;
750720// for (int i = 0; i < n; i++) {
@@ -822,24 +792,49 @@ RecurrenceDescriptor::isFindIVPattern(RecurKind Kind, Loop *TheLoop,
822792 // [Signed|Unsigned]Max(<recurrence type>) for FindFirstIV.
823793 // TODO: This range restriction can be lifted by adding an additional
824794 // virtual OR reduction.
825- bool IsFindFirstIV = isFindFirstIVRecurrenceKind (Kind);
795+ auto CheckRange = [&](bool IsSigned) {
796+ const ConstantRange IVRange =
797+ IsSigned ? SE.getSignedRange (AR) : SE.getUnsignedRange (AR);
798+ unsigned NumBits = Ty->getIntegerBitWidth ();
799+ ConstantRange ValidRange = ConstantRange::getEmpty (NumBits);
800+ if (isFindLastIVRecurrenceKind (Kind)) {
801+ APInt Sentinel = IsSigned ? APInt::getSignedMinValue (NumBits)
802+ : APInt::getMinValue (NumBits);
803+ ValidRange = ConstantRange::getNonEmpty (Sentinel + 1 , Sentinel);
804+ } else {
805+ if (IsSigned)
806+ ValidRange =
807+ ConstantRange::getNonEmpty (APInt::getSignedMinValue (NumBits),
808+ APInt::getSignedMaxValue (NumBits) - 1 );
809+ else
810+ ValidRange = ConstantRange::getNonEmpty (
811+ APInt::getMinValue (NumBits), APInt::getMaxValue (NumBits) - 1 );
812+ }
813+
814+ LLVM_DEBUG (dbgs () << " LV: "
815+ << (isFindLastIVRecurrenceKind (Kind) ? " FindLastIV"
816+ : " FindFirstIV" )
817+ << " valid range is " << ValidRange
818+ << " , and the range of " << *AR << " is " << IVRange
819+ << " \n " );
820+
821+ // Ensure the induction variable does not wrap around by verifying that
822+ // its range is fully contained within the valid range.
823+ return ValidRange.contains (IVRange);
824+ };
826825 if (isFindLastIVRecurrenceKind (Kind)) {
827- if (RecurrenceDescriptor::isValidIVRangeForFindIV (
828- cast<SCEVAddRecExpr>(AR), /* IsSigned=*/ true , IsFindFirstIV, SE))
826+ if (CheckRange (true ))
829827 return RecurKind::FindLastIVSMax;
830- if (RecurrenceDescriptor::isValidIVRangeForFindIV (
831- cast<SCEVAddRecExpr>(AR), /* IsSigned=*/ false , IsFindFirstIV, SE))
828+ if (CheckRange (false ))
832829 return RecurKind::FindLastIVUMax;
833830 return std::nullopt ;
834831 }
835832 assert (isFindFirstIVRecurrenceKind (Kind) &&
836833 " Kind must either be a FindLastIV or FindFirstIV" );
837834
838- if (RecurrenceDescriptor::isValidIVRangeForFindIV (
839- cast<SCEVAddRecExpr>(AR), /* IsSigned=*/ true , IsFindFirstIV, SE))
835+ if (CheckRange (true ))
840836 return RecurKind::FindFirstIVSMin;
841- if (RecurrenceDescriptor::isValidIVRangeForFindIV (
842- cast<SCEVAddRecExpr>(AR), /* IsSigned=*/ false , IsFindFirstIV, SE))
837+ if (CheckRange (false ))
843838 return RecurKind::FindFirstIVUMin;
844839 return std::nullopt ;
845840 };
0 commit comments