forked from OSchip/llvm-project
[IR] CmpInst: Add getFlippedSignednessPredicate()
And refactor a few places to use it
This commit is contained in:
parent
d4f70d6454
commit
8d0fdd36a3
|
@ -942,6 +942,18 @@ public:
|
|||
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.
|
||||
/// Determine if this is true when both operands are the same.
|
||||
bool isTrueWhenEqual() const {
|
||||
|
|
|
@ -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) {
|
||||
switch (predicate) {
|
||||
default: return false;
|
||||
|
|
|
@ -1614,15 +1614,13 @@ Instruction *InstCombinerImpl::foldICmpXorConstant(ICmpInst &Cmp,
|
|||
if (Xor->hasOneUse()) {
|
||||
// (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask))
|
||||
if (!Cmp.isEquality() && XorC->isSignMask()) {
|
||||
Pred = Cmp.isSigned() ? Cmp.getUnsignedPredicate()
|
||||
: Cmp.getSignedPredicate();
|
||||
Pred = Cmp.getFlippedSignednessPredicate();
|
||||
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))
|
||||
if (!Cmp.isEquality() && XorC->isMaxSignedValue()) {
|
||||
Pred = Cmp.isSigned() ? Cmp.getUnsignedPredicate()
|
||||
: Cmp.getSignedPredicate();
|
||||
Pred = Cmp.getFlippedSignednessPredicate();
|
||||
Pred = Cmp.getSwappedPredicate(Pred);
|
||||
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))) {
|
||||
// icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
|
||||
if (C->isSignMask()) {
|
||||
ICmpInst::Predicate NewPred =
|
||||
I.isSigned() ? I.getUnsignedPredicate() : I.getSignedPredicate();
|
||||
ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
|
||||
return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
|
||||
}
|
||||
|
||||
// icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b
|
||||
if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) {
|
||||
ICmpInst::Predicate NewPred =
|
||||
I.isSigned() ? I.getUnsignedPredicate() : I.getSignedPredicate();
|
||||
ICmpInst::Predicate NewPred = I.getFlippedSignednessPredicate();
|
||||
NewPred = I.getSwappedPredicate(NewPred);
|
||||
return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue