@@ -50,7 +50,8 @@ bool RecurrenceDescriptor::isIntegerRecurrenceKind(RecurKind Kind) {
5050 case RecurKind::UMax:
5151 case RecurKind::UMin:
5252 case RecurKind::AnyOf:
53- case RecurKind::FindLastIV:
53+ case RecurKind::FindLastIVSMax:
54+ case RecurKind::FindLastIVUMax:
5455 return true ;
5556 }
5657 return false ;
@@ -700,47 +701,59 @@ RecurrenceDescriptor::isFindLastIVPattern(Loop *TheLoop, PHINode *OrigPhi,
700701 m_Value (NonRdxPhi)))))
701702 return InstDesc (false , I);
702703
703- auto IsIncreasingLoopInduction = [&](Value *V) {
704+ // Returns a non-nullopt boolean indicating the signedness of the recurrence
705+ // when a valid FindLastIV pattern is found.
706+ auto GetRecurKind = [&](Value *V) -> std::optional<RecurKind> {
704707 Type *Ty = V->getType ();
705708 if (!SE.isSCEVable (Ty))
706- return false ;
709+ return std:: nullopt ;
707710
708711 auto *AR = dyn_cast<SCEVAddRecExpr>(SE.getSCEV (V));
709712 if (!AR || AR->getLoop () != TheLoop)
710- return false ;
713+ return std:: nullopt ;
711714
712715 const SCEV *Step = AR->getStepRecurrence (SE);
713716 if (!SE.isKnownPositive (Step))
714- return false ;
717+ return std:: nullopt ;
715718
716- const ConstantRange IVRange = SE.getSignedRange (AR);
717- unsigned NumBits = Ty->getIntegerBitWidth ();
718719 // Keep the minimum value of the recurrence type as the sentinel value.
719720 // The maximum acceptable range for the increasing induction variable,
720721 // called the valid range, will be defined as
721722 // [<sentinel value> + 1, <sentinel value>)
722- // where <sentinel value> is SignedMin (<recurrence type>)
723+ // where <sentinel value> is [Signed|Unsigned]Min (<recurrence type>)
723724 // TODO: This range restriction can be lifted by adding an additional
724725 // virtual OR reduction.
725- const APInt Sentinel = APInt::getSignedMinValue (NumBits);
726- const ConstantRange ValidRange =
727- ConstantRange::getNonEmpty (Sentinel + 1 , Sentinel);
728- LLVM_DEBUG (dbgs () << " LV: FindLastIV valid range is " << ValidRange
729- << " , and the signed range of " << *AR << " is "
730- << IVRange << " \n " );
731- // Ensure the induction variable does not wrap around by verifying that its
732- // range is fully contained within the valid range.
733- return ValidRange.contains (IVRange);
726+ auto CheckRange = [&](bool IsSigned) {
727+ const ConstantRange IVRange =
728+ IsSigned ? SE.getSignedRange (AR) : SE.getUnsignedRange (AR);
729+ unsigned NumBits = Ty->getIntegerBitWidth ();
730+ const APInt Sentinel = IsSigned ? APInt::getSignedMinValue (NumBits)
731+ : APInt::getMinValue (NumBits);
732+ const ConstantRange ValidRange =
733+ ConstantRange::getNonEmpty (Sentinel + 1 , Sentinel);
734+ LLVM_DEBUG (dbgs () << " LV: FindLastIV valid range is " << ValidRange
735+ << " , and the range of " << *AR << " is " << IVRange
736+ << " \n " );
737+
738+ // Ensure the induction variable does not wrap around by verifying that
739+ // its range is fully contained within the valid range.
740+ return ValidRange.contains (IVRange);
741+ };
742+ if (CheckRange (true ))
743+ return RecurKind::FindLastIVSMax;
744+ if (CheckRange (false ))
745+ return RecurKind::FindLastIVUMax;
746+ return std::nullopt ;
734747 };
735748
736749 // We are looking for selects of the form:
737750 // select(cmp(), phi, increasing_loop_induction) or
738751 // select(cmp(), increasing_loop_induction, phi)
739752 // TODO: Support for monotonically decreasing induction variable
740- if (! IsIncreasingLoopInduction (NonRdxPhi))
741- return InstDesc (false , I );
753+ if (auto RK = GetRecurKind (NonRdxPhi))
754+ return InstDesc (I, *RK );
742755
743- return InstDesc (I, RecurKind::FindLastIV );
756+ return InstDesc (false , I );
744757}
745758
746759RecurrenceDescriptor::InstDesc
@@ -985,8 +998,8 @@ bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
985998 << " \n " );
986999 return true ;
9871000 }
988- if (AddReductionVar (Phi, RecurKind::FindLastIV , TheLoop, FMF, RedDes, DB, AC ,
989- DT, SE)) {
1001+ if (AddReductionVar (Phi, RecurKind::FindLastIVSMax , TheLoop, FMF, RedDes, DB,
1002+ AC, DT, SE)) {
9901003 LLVM_DEBUG (dbgs () << " Found a FindLastIV reduction PHI." << *Phi << " \n " );
9911004 return true ;
9921005 }
@@ -1137,7 +1150,8 @@ unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind) {
11371150 case RecurKind::Mul:
11381151 return Instruction::Mul;
11391152 case RecurKind::AnyOf:
1140- case RecurKind::FindLastIV:
1153+ case RecurKind::FindLastIVSMax:
1154+ case RecurKind::FindLastIVUMax:
11411155 case RecurKind::Or:
11421156 return Instruction::Or;
11431157 case RecurKind::And:
0 commit comments