Skip to content

Commit d0ccb32

Browse files
committed
[InstCombine] Respect samesign flag in foldAndOrOfICmpsWithConstEq
1 parent 0019d6c commit d0ccb32

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,8 @@ Value *InstCombinerImpl::foldEqOfParts(ICmpInst *Cmp0, ICmpInst *Cmp1,
12611261
static 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))

llvm/test/Transforms/InstCombine/and-or-icmps.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3421,7 +3421,7 @@ define i1 @logical_or_icmp_eq_const_samesign(i8 %x, i8 %y) {
34213421
; CHECK-LABEL: @logical_or_icmp_eq_const_samesign(
34223422
; CHECK-NEXT: [[CMPEQ:%.*]] = icmp samesign ne i8 [[X:%.*]], 0
34233423
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i8 [[Y:%.*]], 0
3424-
; CHECK-NEXT: [[R:%.*]] = or i1 [[CMPEQ]], [[TMP1]]
3424+
; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i1 true, i1 [[CMPEQ]]
34253425
; CHECK-NEXT: ret i1 [[R]]
34263426
;
34273427
%cmp = icmp sgt i8 %x, %y

0 commit comments

Comments
 (0)