[llvm] [InstCombine] simplify `icmp pred x, ~x` (PR #73990)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 12 22:21:16 PST 2023
================
@@ -7058,6 +7058,45 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
}
}
+ // Transform X s< ~X --> X s< 0
+ // X s> ~X --> X s> -1
+ // X s>= ~X --> X s> -1
+ // X s<= ~X --> X s< 0
+ // X u< ~X --> X s> -1
+ // X u> ~X --> X s< 0
+ // X u<= ~X --> X s> -1
+ // X u>= ~X --> X s< 0
+ // X == ~X --> false
+ // X != ~X --> true
+ if (match(&I, m_c_ICmp(Pred, m_Value(X), m_Not(m_Deferred(X))))) {
+ CmpInst::Predicate NewPred;
+ switch (Pred) {
+ case ICmpInst::ICMP_EQ:
+ return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
+ case ICmpInst::ICMP_NE:
+ return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
+ case ICmpInst::ICMP_UGT:
+ case ICmpInst::ICMP_UGE:
+ case ICmpInst::ICMP_ULT:
+ case ICmpInst::ICMP_ULE:
+ NewPred = CmpInst::getSwappedPredicate(
+ CmpInst::getSignedPredicate(CmpInst::getStrictPredicate(Pred)));
+ break;
+ case ICmpInst::ICMP_SGT:
+ case ICmpInst::ICMP_SGE:
+ case ICmpInst::ICMP_SLT:
+ case ICmpInst::ICMP_SLE:
+ NewPred = CmpInst::getStrictPredicate(Pred);
----------------
dtcxzyw wrote:
```suggestion
case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_ULE:
case ICmpInst::ICMP_SGT:
case ICmpInst::ICMP_SGE:
NewPred = ICmpInst::ICMP_SGT;
break;
case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_UGE:
case ICmpInst::ICMP_SLT:
case ICmpInst::ICMP_SLE:
NewPred = ICmpInst::ICMP_SLT;
```
https://github.com/llvm/llvm-project/pull/73990
More information about the llvm-commits
mailing list