@@ -580,6 +580,43 @@ static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
580580 return true ;
581581}
582582
583+ static void breakSelfRecursivePHI (const Use *U, const PHINode *PHI,
584+ Value *&ValOut, Instruction *&CtxIOut,
585+ unsigned *DepthInOut = nullptr ) {
586+ ValOut = U->get ();
587+ if (ValOut == PHI)
588+ return ;
589+ CtxIOut = PHI->getIncomingBlock (*U)->getTerminator ();
590+ Value *V;
591+ // If the Use is a select of this phi, compute analysis on other arm to be
592+ // recusion.
593+ // TODO: FMin/FMax
594+ if (match (ValOut, m_Select (m_Value (), m_Specific (PHI), m_Value (V))) ||
595+ match (ValOut, m_Select (m_Value (), m_Value (V), m_Specific (PHI))) ||
596+ match (ValOut, m_c_MaxOrMin (m_Value (V), m_Specific (PHI)))) {
597+ ValOut = V;
598+ return ;
599+ }
600+
601+ if (DepthInOut)
602+ *DepthInOut = MaxAnalysisRecursionDepth - 1 ;
603+
604+ // Same for select, if this phi is 2-operand phi, compute analysis on other
605+ // incoming value to break recursion.
606+ // TODO: We could handle any number of incoming edges as long as we only have
607+ // two unique values.
608+ if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
609+ IncPhi && IncPhi->getNumIncomingValues () == 2 ) {
610+ for (int Idx = 0 ; Idx < 2 ; ++Idx) {
611+ if (IncPhi->getIncomingValue (Idx) == PHI) {
612+ ValOut = IncPhi->getIncomingValue (1 - Idx);
613+ CtxIOut = IncPhi->getIncomingBlock (1 - Idx)->getTerminator ();
614+ return ;
615+ }
616+ }
617+ }
618+ }
619+
583620static bool isKnownNonZeroFromAssume (const Value *V, const SimplifyQuery &Q) {
584621 // Use of assumptions is context-sensitive. If we don't have a context, we
585622 // cannot use them!
@@ -1561,33 +1598,20 @@ static void computeKnownBitsFromOperator(const Operator *I,
15611598
15621599 Known.Zero .setAllBits ();
15631600 Known.One .setAllBits ();
1564- for (unsigned u = 0 , e = P->getNumIncomingValues (); u < e; ++u) {
1565- Value *IncValue = P->getIncomingValue (u);
1601+ for (const Use &U : P->operands ()) {
1602+ Value *IncValue;
1603+ Instruction *CxtI;
1604+ unsigned IncDepth = Depth + 1 ;
1605+ breakSelfRecursivePHI (&U, P, IncValue, CxtI, &IncDepth);
15661606 // Skip direct self references.
1567- if (IncValue == P) continue ;
1568-
1569- // Recurse, but cap the recursion to one level, because we don't
1570- // want to waste time spinning around in loops.
1571- // TODO: See if we can base recursion limiter on number of incoming phi
1572- // edges so we don't overly clamp analysis.
1573- unsigned IncDepth = MaxAnalysisRecursionDepth - 1 ;
1574-
1575- // If the Use is a select of this phi, use the knownbit of the other
1576- // operand to break the recursion.
1577- if (auto *SI = dyn_cast<SelectInst>(IncValue)) {
1578- if (SI->getTrueValue () == P || SI->getFalseValue () == P) {
1579- IncValue = SI->getTrueValue () == P ? SI->getFalseValue ()
1580- : SI->getTrueValue ();
1581- IncDepth = Depth + 1 ;
1582- }
1583- }
1607+ if (IncValue == P)
1608+ continue ;
15841609
15851610 // Change the context instruction to the "edge" that flows into the
15861611 // phi. This is important because that is where the value is actually
15871612 // "evaluated" even though it is used later somewhere else. (see also
15881613 // D69571).
1589- SimplifyQuery RecQ = Q.getWithoutCondContext ();
1590- RecQ.CxtI = P->getIncomingBlock (u)->getTerminator ();
1614+ SimplifyQuery RecQ = Q.getWithoutCondContext ().getWithInstruction (CxtI);
15911615
15921616 Known2 = KnownBits (BitWidth);
15931617 computeKnownBits (IncValue, DemandedElts, Known2, IncDepth, RecQ);
@@ -6008,30 +6032,13 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
60086032 bool First = true ;
60096033
60106034 for (const Use &U : P->operands ()) {
6011- Value *IncValue = U.get ();
6035+ Value *IncValue;
6036+ Instruction *CxtI;
6037+ breakSelfRecursivePHI (&U, P, IncValue, CxtI);
60126038 // Skip direct self references.
60136039 if (IncValue == P)
60146040 continue ;
60156041
6016- Instruction *CxtI = P->getIncomingBlock (U)->getTerminator ();
6017-
6018- // If the Use is a select of this phi, use the fp class of the other
6019- // operand to break the recursion. Same around 2-operand phi nodes
6020- Value *V;
6021- if (match (IncValue, m_Select (m_Value (), m_Specific (P), m_Value (V))) ||
6022- match (IncValue, m_Select (m_Value (), m_Value (V), m_Specific (P)))) {
6023- IncValue = V;
6024- } else if (auto *IncPhi = dyn_cast<PHINode>(IncValue);
6025- IncPhi && IncPhi->getNumIncomingValues () == 2 ) {
6026- for (int Idx = 0 ; Idx < 2 ; ++Idx) {
6027- if (IncPhi->getIncomingValue (Idx) == P) {
6028- IncValue = IncPhi->getIncomingValue (1 - Idx);
6029- CxtI = IncPhi->getIncomingBlock (1 - Idx)->getTerminator ();
6030- break ;
6031- }
6032- }
6033- }
6034-
60356042 KnownFPClass KnownSrc;
60366043 // Recurse, but cap the recursion to two levels, because we don't want
60376044 // to waste time spinning around in loops. We need at least depth 2 to
0 commit comments