@@ -3575,41 +3575,6 @@ static Value *foldOrOfInversions(BinaryOperator &I,
35753575 return nullptr ;
35763576}
35773577
3578- // Optimize patterns where an OR operation combines a select-based zero check
3579- // with its condition value. This handles both scalar and vector types.
3580- //
3581- // Given:
3582- // (X == 0 ? Y : 0) | X --> X == 0 ? Y : X
3583- // X | (X == 0 ? Y : 0) --> X == 0 ? Y : X
3584- //
3585- // Also handles cases where X might be wrapped in zero/sign extensions.
3586- static Instruction *foldOrOfSelectZero (BinaryOperator &BO, Value *Op0,
3587- Value *Op1) {
3588- CmpPredicate Pred;
3589- Value *X, *Y;
3590-
3591- // Check both operand orders to handle commutative OR
3592- for (Value *SelVal : {Op0, Op1}) {
3593- // The other operand in the OR operation (potentially X or extended X)
3594- Value *Other = (SelVal == Op0) ? Op1 : Op0;
3595-
3596- // Attempt to match the select pattern:
3597- // select(icmp eq X, 0), Y, 0
3598- // Where X might be:
3599- // - Original value
3600- // - Zero extended value (zext)
3601- // - Sign extended value (sext)
3602- if (match (SelVal, m_Select (m_c_ICmp (Pred, m_Value (X), m_Zero ()), m_Value (Y),
3603- m_Zero ())) &&
3604- Pred == ICmpInst::ICMP_EQ &&
3605- match (Other, m_ZExtOrSExtOrSelf (m_Specific (X)))) {
3606- return SelectInst::Create (cast<SelectInst>(SelVal)->getCondition (), Y,
3607- Other);
3608- }
3609- }
3610- return nullptr ;
3611- }
3612-
36133578// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
36143579// here. We should standardize that construct where it is needed or choose some
36153580// other way to ensure that commutated variants of patterns are not missed.
@@ -3692,11 +3657,15 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
36923657 /* NSW=*/ true , /* NUW=*/ true ))
36933658 return R;
36943659 }
3695-
3660+
36963661 // (X == 0 ? Y : 0) | X -> X == 0 ? Y : X
36973662 // X | (X == 0 ? Y : 0) -> X == 0 ? Y : X
3698- if (Instruction *R = foldOrOfSelectZero (I, Op0, Op1))
3699- return R;
3663+ for (Value *Op : {Op0, Op1}) {
3664+ if (auto *SI = dyn_cast<SelectInst>(Op)) {
3665+ if (auto *R = FoldOpIntoSelect (I, SI, /* FoldWithMultiUse */ false ))
3666+ return R;
3667+ }
3668+ }
37003669
37013670 Value *X, *Y;
37023671 const APInt *CV;
@@ -5078,3 +5047,4 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
50785047
50795048 return nullptr ;
50805049}
5050+
0 commit comments