@@ -3033,18 +3033,36 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
3033
3033
return true ;
3034
3034
3035
3035
// Check if all incoming values are non-zero using recursion.
3036
- SimplifyQuery RecQ = Q.getWithoutCondContext ();
3037
- unsigned NewDepth = std::max (Depth, MaxAnalysisRecursionDepth - 1 );
3038
3036
return llvm::all_of (PN->operands (), [&](const Use &U) {
3039
- if (U.get () == PN)
3037
+ Value *IncValue = U.get ();
3038
+ if (IncValue == PN)
3040
3039
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);
3042
3060
// Check if the branch on the phi excludes zero.
3043
3061
ICmpInst::Predicate Pred;
3044
3062
Value *X;
3045
3063
BasicBlock *TrueSucc, *FalseSucc;
3046
3064
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)),
3048
3066
m_BasicBlock (TrueSucc), m_BasicBlock (FalseSucc)))) {
3049
3067
// Check for cases of duplicate successors.
3050
3068
if ((TrueSucc == PN->getParent ()) != (FalseSucc == PN->getParent ())) {
@@ -3056,7 +3074,7 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
3056
3074
}
3057
3075
}
3058
3076
// Finally recurse on the edge and check it directly.
3059
- return isKnownNonZero (U. get () , DemandedElts, RecQ, NewDepth);
3077
+ return isKnownNonZero (IncValue , DemandedElts, RecQ, NewDepth);
3060
3078
});
3061
3079
}
3062
3080
case Instruction::InsertElement: {
0 commit comments