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