@@ -3032,18 +3032,36 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
30323032 return true ;
30333033
30343034 // Check if all incoming values are non-zero using recursion.
3035- SimplifyQuery RecQ = Q.getWithoutCondContext ();
3036- unsigned NewDepth = std::max (Depth, MaxAnalysisRecursionDepth - 1 );
30373035 return llvm::all_of (PN->operands (), [&](const Use &U) {
3038- if (U.get () == PN)
3036+ Value *IncValue = U.get ();
3037+ if (IncValue == PN)
30393038 return true ;
3040- RecQ.CxtI = PN->getIncomingBlock (U)->getTerminator ();
3039+
3040+ Instruction *CxtI = PN->getIncomingBlock (U)->getTerminator ();
3041+ unsigned NewDepth = std::max (Depth, MaxAnalysisRecursionDepth - 1 );
3042+ if (auto *SI = dyn_cast<SelectInst>(IncValue)) {
3043+ if (SI->getTrueValue () == PN || SI->getFalseValue () == PN) {
3044+ IncValue = SI->getTrueValue () == PN ? SI->getFalseValue ()
3045+ : SI->getTrueValue ();
3046+ NewDepth = Depth;
3047+ }
3048+ } else if (auto *IncPhi = dyn_cast<PHINode>(IncValue);
3049+ IncPhi && IncPhi->getNumIncomingValues () == 2 ) {
3050+ for (int Idx = 0 ; Idx < 2 ; ++Idx) {
3051+ if (IncPhi->getIncomingValue (Idx) == PN) {
3052+ IncValue = IncPhi->getIncomingValue (1 - Idx);
3053+ CxtI = IncPhi->getIncomingBlock (1 - Idx)->getTerminator ();
3054+ break ;
3055+ }
3056+ }
3057+ }
3058+ SimplifyQuery RecQ = Q.getWithoutCondContext ().getWithInstruction (CxtI);
30413059 // Check if the branch on the phi excludes zero.
30423060 ICmpInst::Predicate Pred;
30433061 Value *X;
30443062 BasicBlock *TrueSucc, *FalseSucc;
30453063 if (match (RecQ.CxtI ,
3046- m_Br (m_c_ICmp (Pred, m_Specific (U. get () ), m_Value (X)),
3064+ m_Br (m_c_ICmp (Pred, m_Specific (IncValue ), m_Value (X)),
30473065 m_BasicBlock (TrueSucc), m_BasicBlock (FalseSucc)))) {
30483066 // Check for cases of duplicate successors.
30493067 if ((TrueSucc == PN->getParent ()) != (FalseSucc == PN->getParent ())) {
@@ -3055,7 +3073,7 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
30553073 }
30563074 }
30573075 // Finally recurse on the edge and check it directly.
3058- return isKnownNonZero (U. get () , DemandedElts, RecQ, NewDepth);
3076+ return isKnownNonZero (IncValue , DemandedElts, RecQ, NewDepth);
30593077 });
30603078 }
30613079 case Instruction::InsertElement: {
0 commit comments