mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-02-01 05:01:59 +01:00
[InstCombine] (X != C1 && X != C2) --> (X | (C1 ^ C2)) != C2
...when C1 differs from C2 by one bit and C1 <u C2: http://rise4fun.com/Alive/Vuo And move related folds to a helper function. This reduces code duplication and will make it easier to remove the scalar-only restriction as a follow-up step. llvm-svn: 300364
This commit is contained in:
parent
c7fb97cbfb
commit
0ee9395084
@ -724,6 +724,61 @@ Value *InstCombiner::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1,
|
||||
return Builder->CreateICmp(NewPred, Input, RangeEnd);
|
||||
}
|
||||
|
||||
static Value *
|
||||
foldAndOrOfEqualityCmpsWithConstants(ICmpInst *LHS, ICmpInst *RHS,
|
||||
bool JoinedByAnd,
|
||||
InstCombiner::BuilderTy *Builder) {
|
||||
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)
|
||||
return nullptr;
|
||||
|
||||
// We only handle (X != C1 && X != C2) and (X == C1 || X == C2).
|
||||
ICmpInst::Predicate Pred = LHS->getPredicate();
|
||||
if (Pred != RHS->getPredicate())
|
||||
return nullptr;
|
||||
if (JoinedByAnd && Pred != ICmpInst::ICMP_NE)
|
||||
return nullptr;
|
||||
if (!JoinedByAnd && Pred != ICmpInst::ICMP_EQ)
|
||||
return nullptr;
|
||||
|
||||
// The larger unsigned constant goes on the right.
|
||||
if (LHSC->getValue().ugt(RHSC->getValue()))
|
||||
std::swap(LHSC, RHSC);
|
||||
|
||||
APInt Xor = LHSC->getValue() ^ RHSC->getValue();
|
||||
if (Xor.isPowerOf2()) {
|
||||
// If LHSC and RHSC differ by only one bit, then set that bit in X and
|
||||
// compare against the larger constant:
|
||||
// (X == C1 || X == C2) --> (X | (C1 ^ C2)) == C2
|
||||
// (X != C1 && X != C2) --> (X | (C1 ^ C2)) != C2
|
||||
// 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);
|
||||
}
|
||||
|
||||
// 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 (LHSC == SubOne(RHSC)) {
|
||||
// (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));
|
||||
auto NewPred = JoinedByAnd ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_ULE;
|
||||
return Builder->CreateICmp(NewPred, Add, ConstantInt::get(X->getType(), 1));
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/// Fold (icmp)&(icmp) if possible.
|
||||
Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
|
||||
ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
|
||||
@ -823,6 +878,9 @@ Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
|
||||
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) ||
|
||||
@ -877,17 +935,8 @@ Value *InstCombiner::FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS) {
|
||||
case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15
|
||||
return RHS;
|
||||
case ICmpInst::ICMP_NE:
|
||||
// Special case to 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 (LHSC == SubOne(RHSC)) {
|
||||
// (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(LHS0, ConstantExpr::getNeg(LHSC));
|
||||
return Builder->CreateICmpUGT(Add, ConstantInt::get(Add->getType(), 1));
|
||||
}
|
||||
break; // (X != 13 & X != 15) -> no change
|
||||
// Potential folds for this case should already be handled.
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ICmpInst::ICMP_ULT:
|
||||
@ -1742,6 +1791,9 @@ Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
|
||||
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) ||
|
||||
@ -1772,31 +1824,8 @@ Value *InstCombiner::FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
|
||||
default:
|
||||
llvm_unreachable("Unknown integer condition code!");
|
||||
case ICmpInst::ICMP_EQ:
|
||||
if (LHS->getOperand(0) == RHS->getOperand(0)) {
|
||||
// if LHSC and RHSC differ only by one bit:
|
||||
// (A == C1 || A == C2) -> (A | (C1 ^ C2)) == C2
|
||||
assert(LHSC->getValue().ult(RHSC->getValue()));
|
||||
|
||||
APInt Xor = LHSC->getValue() ^ RHSC->getValue();
|
||||
if (Xor.isPowerOf2()) {
|
||||
Value *C = Builder->getInt(Xor);
|
||||
Value *Or = Builder->CreateOr(LHS->getOperand(0), C);
|
||||
return Builder->CreateICmp(ICmpInst::ICMP_EQ, Or, RHSC);
|
||||
}
|
||||
}
|
||||
|
||||
// Special case to 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 (LHSC == SubOne(RHSC)) {
|
||||
// (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(LHS0, ConstantExpr::getNeg(LHSC));
|
||||
return Builder->CreateICmpULE(Add, ConstantInt::get(Add->getType(), 1));
|
||||
}
|
||||
|
||||
break; // (X == 13 | X == 15) -> no change
|
||||
// Potential folds for this case should already be handled.
|
||||
break;
|
||||
case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change
|
||||
case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change
|
||||
break;
|
||||
|
@ -59,8 +59,8 @@ define i1 @or_eq_with_one_bit_diff_constants1(i32 %x) {
|
||||
|
||||
define i1 @and_ne_with_one_bit_diff_constants1(i32 %x) {
|
||||
; CHECK-LABEL: @and_ne_with_one_bit_diff_constants1(
|
||||
; CHECK-NEXT: [[TMP1:%.*]] = and i32 %x, -2
|
||||
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i32 [[TMP1]], 50
|
||||
; CHECK-NEXT: [[TMP1:%.*]] = or i32 %x, 1
|
||||
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i32 [[TMP1]], 51
|
||||
; CHECK-NEXT: ret i1 [[TMP2]]
|
||||
;
|
||||
%cmp1 = icmp ne i32 %x, 51
|
||||
@ -85,10 +85,9 @@ define i1 @or_eq_with_one_bit_diff_constants2(i32 %x) {
|
||||
|
||||
define i1 @and_ne_with_one_bit_diff_constants2(i19 %x) {
|
||||
; CHECK-LABEL: @and_ne_with_one_bit_diff_constants2(
|
||||
; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i19 %x, 65
|
||||
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i19 %x, 193
|
||||
; CHECK-NEXT: [[AND:%.*]] = and i1 [[CMP1]], [[CMP2]]
|
||||
; CHECK-NEXT: ret i1 [[AND]]
|
||||
; CHECK-NEXT: [[TMP1:%.*]] = or i19 %x, 128
|
||||
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i19 [[TMP1]], 193
|
||||
; CHECK-NEXT: ret i1 [[TMP2]]
|
||||
;
|
||||
%cmp1 = icmp ne i19 %x, 65
|
||||
%cmp2 = icmp ne i19 %x, 193
|
||||
@ -112,10 +111,9 @@ define i1 @or_eq_with_one_bit_diff_constants3(i8 %x) {
|
||||
|
||||
define i1 @and_ne_with_one_bit_diff_constants3(i8 %x) {
|
||||
; CHECK-LABEL: @and_ne_with_one_bit_diff_constants3(
|
||||
; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i8 %x, 65
|
||||
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 %x, -63
|
||||
; CHECK-NEXT: [[AND:%.*]] = and i1 [[CMP1]], [[CMP2]]
|
||||
; CHECK-NEXT: ret i1 [[AND]]
|
||||
; CHECK-NEXT: [[TMP1:%.*]] = or i8 %x, -128
|
||||
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i8 [[TMP1]], -63
|
||||
; CHECK-NEXT: ret i1 [[TMP2]]
|
||||
;
|
||||
%cmp1 = icmp ne i8 %x, 65
|
||||
%cmp2 = icmp ne i8 %x, 193
|
||||
|
Loading…
x
Reference in New Issue
Block a user