[IR] CmpInst: Add getFlippedSignednessPredicate()

And refactor a few places to use it
This commit is contained in:
Roman Lebedev 2020-11-06 11:14:03 +03:00
parent d4f70d6454
commit 8d0fdd36a3
No known key found for this signature in database
GPG Key ID: 083C3EBB4A1689E0
3 changed files with 28 additions and 8 deletions

View File

@ -942,6 +942,18 @@ public:
return getUnsignedPredicate(getPredicate()); return getUnsignedPredicate(getPredicate());
} }
/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
/// @returns the unsigned version of the signed predicate pred or
/// the signed version of the signed predicate pred.
static Predicate getFlippedSignednessPredicate(Predicate pred);
/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
/// @returns the unsigned version of the signed predicate pred or
/// the signed version of the signed predicate pred.
Predicate getFlippedSignednessPredicate() {
return getFlippedSignednessPredicate(getPredicate());
}
/// This is just a convenience. /// This is just a convenience.
/// Determine if this is true when both operands are the same. /// Determine if this is true when both operands are the same.
bool isTrueWhenEqual() const { bool isTrueWhenEqual() const {

View File

@ -3898,6 +3898,18 @@ bool CmpInst::isSigned(Predicate predicate) {
} }
} }
CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) {
assert(CmpInst::isRelational(pred) &&
"Call only with non-equality predicates!");
if (isSigned(pred))
return getUnsignedPredicate(pred);
if (isUnsigned(pred))
return getSignedPredicate(pred);
llvm_unreachable("Unknown predicate!");
}
bool CmpInst::isOrdered(Predicate predicate) { bool CmpInst::isOrdered(Predicate predicate) {
switch (predicate) { switch (predicate) {
default: return false; default: return false;

View File

@ -1614,15 +1614,13 @@ Instruction *InstCombinerImpl::foldICmpXorConstant(ICmpInst &Cmp,
if (Xor->hasOneUse()) { if (Xor->hasOneUse()) {
// (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask)) // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
if (!Cmp.isEquality() && XorC->isSignMask()) { if (!Cmp.isEquality() && XorC->isSignMask()) {
Pred = Cmp.isSigned() ? Cmp.getUnsignedPredicate() Pred = Cmp.getFlippedSignednessPredicate();
: Cmp.getSignedPredicate();
return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC)); return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
} }
// (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask)) // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask))
if (!Cmp.isEquality() && XorC->isMaxSignedValue()) { if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
Pred = Cmp.isSigned() ? Cmp.getUnsignedPredicate() Pred = Cmp.getFlippedSignednessPredicate();
: Cmp.getSignedPredicate();
Pred = Cmp.getSwappedPredicate(Pred); Pred = Cmp.getSwappedPredicate(Pred);
return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC)); return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), C ^ *XorC));
} }
@ -4053,15 +4051,13 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
if (match(BO0->getOperand(1), m_APInt(C))) { if (match(BO0->getOperand(1), m_APInt(C))) {
// icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
if (C->isSignMask()) { if (C->isSignMask()) {
ICmpInst::Predicate NewPred = ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
I.isSigned() ? I.getUnsignedPredicate() : I.getSignedPredicate();
return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0)); return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
} }
// icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) { if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
ICmpInst::Predicate NewPred = ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
I.isSigned() ? I.getUnsignedPredicate() : I.getSignedPredicate();
NewPred = I.getSwappedPredicate(NewPred); NewPred = I.getSwappedPredicate(NewPred);
return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0)); return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
} }