@@ -1261,7 +1261,8 @@ Value *InstCombinerImpl::foldEqOfParts(ICmpInst *Cmp0, ICmpInst *Cmp1,
12611261static Value *foldAndOrOfICmpsWithConstEq (ICmpInst *Cmp0, ICmpInst *Cmp1,
12621262 bool IsAnd, bool IsLogical,
12631263 InstCombiner::BuilderTy &Builder,
1264- const SimplifyQuery &Q) {
1264+ const SimplifyQuery &Q,
1265+ bool SwapOpOrder) {
12651266 // Match an equality compare with a non-poison constant as Cmp0.
12661267 // Also, give up if the compare can be constant-folded to avoid looping.
12671268 ICmpInst::Predicate Pred0;
@@ -1295,9 +1296,14 @@ static Value *foldAndOrOfICmpsWithConstEq(ICmpInst *Cmp0, ICmpInst *Cmp1,
12951296 return nullptr ;
12961297 SubstituteCmp = Builder.CreateICmp (Pred1, Y, C);
12971298 }
1298- if (IsLogical)
1299- return IsAnd ? Builder.CreateLogicalAnd (Cmp0, SubstituteCmp)
1300- : Builder.CreateLogicalOr (Cmp0, SubstituteCmp);
1299+ if (IsLogical) {
1300+ Value *LHS = Cmp0;
1301+ Value *RHS = SubstituteCmp;
1302+ if (SwapOpOrder)
1303+ std::swap (LHS, RHS);
1304+ return IsAnd ? Builder.CreateLogicalAnd (LHS, RHS)
1305+ : Builder.CreateLogicalOr (LHS, RHS);
1306+ }
13011307 return Builder.CreateBinOp (IsAnd ? Instruction::And : Instruction::Or, Cmp0,
13021308 SubstituteCmp);
13031309}
@@ -3363,13 +3369,16 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
33633369 /* IsLogical*/ false , Builder))
33643370 return V;
33653371
3366- if (Value *V =
3367- foldAndOrOfICmpsWithConstEq (LHS, RHS, IsAnd, IsLogical, Builder, Q))
3372+ if (Value *V = foldAndOrOfICmpsWithConstEq (LHS, RHS, IsAnd, IsLogical,
3373+ Builder, Q, /* SwapOpOrder= */ false ))
33683374 return V;
33693375 // We can convert this case to bitwise and, because both operands are used
3370- // on the LHS, and as such poison from both will propagate.
3376+ // on the LHS, and as such poison from both will propagate (unless RHS has
3377+ // samesign flag).
33713378 if (Value *V = foldAndOrOfICmpsWithConstEq (RHS, LHS, IsAnd,
3372- /* IsLogical*/ false , Builder, Q))
3379+ /* IsLogical=*/ IsLogical &&
3380+ RHS->hasSameSign (),
3381+ Builder, Q, /* SwapOpOrder=*/ true ))
33733382 return V;
33743383
33753384 if (Value *V = foldIsPowerOf2OrZero (LHS, RHS, IsAnd, Builder, *this ))
0 commit comments