foldAndOrOfEqualityCmpsWithConstants(ICmpInst *LHS, ICmpInst *RHS,
bool JoinedByAnd,
InstCombiner::BuilderTy *Builder) {
- Value *X = LHS->getOperand(0); if (X != RHS->getOperand(0))
+ Value *X = LHS->getOperand(0);
+ if (X != RHS->getOperand(0))
return nullptr;
- // FIXME: This should use m_APInt and work with splat vector constants.
- auto *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1));
- auto *RHSC = dyn_cast<ConstantInt>(RHS->getOperand(1));
- if (!LHSC || !RHSC)
+ const APInt *C1, *C2;
+ if (!match(LHS->getOperand(1), m_APInt(C1)) ||
+ !match(RHS->getOperand(1), m_APInt(C2)))
return nullptr;
// We only handle (X != C1 && X != C2) and (X == C1 || X == C2).
return nullptr;
// The larger unsigned constant goes on the right.
- if (LHSC->getValue().ugt(RHSC->getValue()))
- std::swap(LHSC, RHSC);
+ if (C1->ugt(*C2))
+ std::swap(C1, C2);
- APInt Xor = LHSC->getValue() ^ RHSC->getValue();
+ APInt Xor = *C1 ^ *C2;
if (Xor.isPowerOf2()) {
// If LHSC and RHSC differ by only one bit, then set that bit in X and
// compare against the larger constant:
// We choose an 'or' with a Pow2 constant rather than the inverse mask with
// 'and' because that may lead to smaller codegen from a smaller constant.
Value *Or = Builder->CreateOr(X, ConstantInt::get(X->getType(), Xor));
- return Builder->CreateICmp(Pred, Or, RHSC);
+ return Builder->CreateICmp(Pred, Or, ConstantInt::get(X->getType(), *C2));
}
// Special case: get the ordering right when the values wrap around zero.
// Ie, we assumed the constants were unsigned when swapping earlier.
- if (LHSC->getValue() == 0 && RHSC->getValue().isAllOnesValue())
- std::swap(LHSC, RHSC);
+ if (*C1 == 0 && C2->isAllOnesValue())
+ std::swap(C1, C2);
- if (LHSC == SubOne(RHSC)) {
+ if (*C1 == *C2 - 1) {
// (X == 13 || X == 14) --> X - 13 <=u 1
// (X != 13 && X != 14) --> X - 13 >u 1
// An 'add' is the canonical IR form, so favor that over a 'sub'.
- Value *Add = Builder->CreateAdd(X, ConstantExpr::getNeg(LHSC));
+ Value *Add = Builder->CreateAdd(X, ConstantInt::get(X->getType(), -(*C1)));
auto NewPred = JoinedByAnd ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_ULE;
return Builder->CreateICmp(NewPred, Add, ConstantInt::get(X->getType(), 1));
}
if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/false))
return V;
+ if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, true, Builder))
+ return V;
+
// This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0);
ConstantInt *LHSC = dyn_cast<ConstantInt>(LHS->getOperand(1));
if (!PredicatesFoldable(PredL, PredR))
return nullptr;
- if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, true, Builder))
- return V;
-
// Ensure that the larger constant is on the RHS.
bool ShouldSwap;
if (CmpInst::isSigned(PredL) ||
if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/true))
return V;
+ if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, false, Builder))
+ return V;
+
// This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
if (!LHSC || !RHSC)
return nullptr;
if (!PredicatesFoldable(PredL, PredR))
return nullptr;
- if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, false, Builder))
- return V;
-
// Ensure that the larger constant is on the RHS.
bool ShouldSwap;
if (CmpInst::isSigned(PredL) ||
ret i1 %and
}
-; FIXME: Vectors with splat constants get the same folds.
+; Vectors with splat constants get the same folds.
define <2 x i1> @or_eq_with_one_bit_diff_constants2_splatvec(<2 x i32> %x) {
; CHECK-LABEL: @or_eq_with_one_bit_diff_constants2_splatvec(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp eq <2 x i32> %x, <i32 97, i32 97>
-; CHECK-NEXT: [[CMP2:%.*]] = icmp eq <2 x i32> %x, <i32 65, i32 65>
-; CHECK-NEXT: [[OR:%.*]] = or <2 x i1> [[CMP1]], [[CMP2]]
-; CHECK-NEXT: ret <2 x i1> [[OR]]
+; CHECK-NEXT: [[TMP1:%.*]] = or <2 x i32> %x, <i32 32, i32 32>
+; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i32> [[TMP1]], <i32 97, i32 97>
+; CHECK-NEXT: ret <2 x i1> [[TMP2]]
;
%cmp1 = icmp eq <2 x i32> %x, <i32 97, i32 97>
%cmp2 = icmp eq <2 x i32> %x, <i32 65, i32 65>
define <2 x i1> @and_ne_with_diff_one_splatvec(<2 x i32> %x) {
; CHECK-LABEL: @and_ne_with_diff_one_splatvec(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp ne <2 x i32> %x, <i32 40, i32 40>
-; CHECK-NEXT: [[CMP2:%.*]] = icmp ne <2 x i32> %x, <i32 39, i32 39>
-; CHECK-NEXT: [[AND:%.*]] = and <2 x i1> [[CMP1]], [[CMP2]]
-; CHECK-NEXT: ret <2 x i1> [[AND]]
+; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i32> %x, <i32 -39, i32 -39>
+; CHECK-NEXT: [[TMP2:%.*]] = icmp ugt <2 x i32> [[TMP1]], <i32 1, i32 1>
+; CHECK-NEXT: ret <2 x i1> [[TMP2]]
;
%cmp1 = icmp ne <2 x i32> %x, <i32 40, i32 40>
%cmp2 = icmp ne <2 x i32> %x, <i32 39, i32 39>