@@ -249,17 +249,17 @@ hasRequiredFastMathFlags(FPMathOperator *FPOp, RecurKind &RK,
249249 return {collectMinMaxFMF (FPOp)};
250250}
251251
252- static std::optional< RecurrenceDescriptor>
253- getMultiUseMinMax (PHINode *Phi, Loop *TheLoop, FastMathFlags FuncFMF,
254- ScalarEvolution *SE) {
252+ static RecurrenceDescriptor getMinMaxRecurrence (PHINode *Phi, Loop *TheLoop,
253+ FastMathFlags FuncFMF,
254+ ScalarEvolution *SE) {
255255 if (Phi->getNumIncomingValues () != 2 ||
256256 Phi->getParent () != TheLoop->getHeader ())
257- return std:: nullopt ;
257+ return {} ;
258258
259259 Type *Ty = Phi->getType ();
260260 BasicBlock *Latch = TheLoop->getLoopLatch ();
261261 if ((!Ty->isIntegerTy () && !Ty->isFloatingPointTy ()) || !Latch)
262- return std:: nullopt ;
262+ return {} ;
263263
264264 auto Matches = [](Value *V, Value *&A, Value *&B) -> RecurKind {
265265 if (match (V, m_UMin (m_Value (A), m_Value (B))))
@@ -300,7 +300,7 @@ getMultiUseMinMax(PHINode *Phi, Loop *TheLoop, FastMathFlags FuncFMF,
300300 continue ;
301301 auto *I = dyn_cast<Instruction>(Cur);
302302 if (!I || !TheLoop->contains (I))
303- return std:: nullopt ;
303+ return {} ;
304304 if (auto *PN = dyn_cast<PHINode>(I)) {
305305 if (PN != Phi)
306306 append_range (WorkList, PN->operands ());
@@ -309,7 +309,7 @@ getMultiUseMinMax(PHINode *Phi, Loop *TheLoop, FastMathFlags FuncFMF,
309309 Value *A, *B;
310310 RecurKind CurRK = Matches (Cur, A, B);
311311 if (CurRK == RecurKind::None || (RK != RecurKind::None && CurRK != RK))
312- return std:: nullopt ;
312+ return {} ;
313313
314314 RK = CurRK;
315315 // For floating point recurrences, check we have the required fast-math
@@ -319,7 +319,7 @@ getMultiUseMinMax(PHINode *Phi, Loop *TheLoop, FastMathFlags FuncFMF,
319319 hasRequiredFastMathFlags (cast<FPMathOperator>(Cur), RK, FuncFMF))
320320 FMF &= *CurFMF;
321321 else
322- return std:: nullopt ;
322+ return {} ;
323323 }
324324
325325 Chain.insert (I);
@@ -337,7 +337,7 @@ getMultiUseMinMax(PHINode *Phi, Loop *TheLoop, FastMathFlags FuncFMF,
337337 bool AMatches = IA && TheLoop->contains (IA) && Matches (A, X, Y) == RK;
338338 bool BMatches = IB && TheLoop->contains (IB) && Matches (B, X, Y) == RK;
339339 if (AMatches == BMatches) // Both or neither match
340- return std:: nullopt ;
340+ return {} ;
341341 WorkList.push_back (AMatches ? A : B);
342342 }
343343
@@ -350,29 +350,30 @@ getMultiUseMinMax(PHINode *Phi, Loop *TheLoop, FastMathFlags FuncFMF,
350350 for (Use &U : RdxNext->uses ()) {
351351 auto *User = cast<Instruction>(U.getUser ());
352352 if (!TheLoop->contains (User->getParent ())) {
353- if (++IncOut > 1 )
354- return std::nullopt ;
353+ if (IncOut > 0 )
354+ return {};
355+ IncOut++;
355356 } else if (auto *SI = dyn_cast<StoreInst>(User)) {
356357 const SCEV *Ptr = SE->getSCEV (SI->getPointerOperand ());
357358 if (U.getOperandNo () == SI->getPointerOperandIndex () ||
358359 !SE->isLoopInvariant (Ptr, TheLoop) ||
359360 (IntermediateStore &&
360361 SE->getSCEV (IntermediateStore->getPointerOperand ()) != Ptr))
361- return std:: nullopt ;
362+ return {} ;
362363 // Keep the store that appears last in the block, as it will be the final
363364 // reduction value.
364365 if (!IntermediateStore || IntermediateStore->comesBefore (SI))
365366 IntermediateStore = SI;
366367 } else if (Phi != User)
367- return std:: nullopt ;
368+ return {} ;
368369 }
369370
370371 // All ops on the chain from Phi to RdxNext must only be used by instructions
371372 // in the chain.
372373 for (Value *Op : Chain)
373374 if (Op != RdxNext &&
374375 any_of (Op->users (), [&Chain](User *U) { return !Chain.contains (U); }))
375- return std:: nullopt ;
376+ return {} ;
376377
377378 SmallPtrSet<Instruction *, 4 > Casts;
378379 return RecurrenceDescriptor (
@@ -1090,9 +1091,13 @@ bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
10901091 LLVM_DEBUG (dbgs () << " Found a XOR reduction PHI." << *Phi << " \n " );
10911092 return true ;
10921093 }
1093- if (auto RD = getMultiUseMinMax (Phi, TheLoop, FMF, SE)) {
1094+ auto RD = getMinMaxRecurrence (Phi, TheLoop, FMF, SE);
1095+ if (RD.getRecurrenceKind () != RecurKind::None) {
1096+ assert (
1097+ RecurrenceDescriptor::isMinMaxRecurrenceKind (RD.getRecurrenceKind ()) &&
1098+ " must return a min/max recurrence kind" );
10941099 LLVM_DEBUG (dbgs () << " Found a min/max reduction PHI." << *Phi << " \n " );
1095- RedDes = * RD;
1100+ RedDes = RD;
10961101 return true ;
10971102 }
10981103 if (AddReductionVar (Phi, RecurKind::AnyOf, TheLoop, FMF, RedDes, DB, AC, DT,
0 commit comments