[llvm] [InstCombine] simplify `icmp pred x, ~x` (PR #73990)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 9 00:50:47 PST 2024


dtcxzyw wrote:

> @dtcxzyw How about moving my code inside the foldICmpXorXX function? Is it okay if I do that? if moved, then I think that no need tests for commuative.
> 
> ```
> static Instruction *foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q,
>                                   InstCombinerImpl &IC) {
>   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A;
>   // Normalize xor operand as operand 0.
>   CmpInst::Predicate Pred = I.getPredicate();
>   CmpInst::Predicate StrictPred = ICmpInst::getStrictPredicate(Pred);
>   if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) {
>     std::swap(Op0, Op1);
>     Pred = ICmpInst::getSwappedPredicate(Pred);
>     StrictPred = ICmpInst::getStrictPredicate(Pred);
>   }
>   if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))
>     return nullptr;
> 
>   // Transform
>   // X s< ~X, X s<= ~X, X u> ~X, X u>= ~X
>   // --> X s< 0
>   // X s> ~X, X s>= ~X, X u< ~X, X u<= ~X
>   // --> X s> -1
>   // X ==  ~X  -->   false
>   // X !=  ~X  -->   true
>   if (match(Op0, m_Not(m_Specific(Op1)))) {
>     CmpInst::Predicate NewPred;
>     switch (StrictPred) {
>     case ICmpInst::ICMP_EQ:
>       return IC.replaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
>     case ICmpInst::ICMP_NE:
>       return IC.replaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
>     case ICmpInst::ICMP_SLT:
>     case ICmpInst::ICMP_UGT:
>       NewPred = ICmpInst::ICMP_SGT;
>       break;
>     case ICmpInst::ICMP_SGT:
>     case ICmpInst::ICMP_ULT:
>       NewPred = ICmpInst::ICMP_SLT;
>       break;
>     default:
>       llvm_unreachable("not a valid predicate");
>     }
>     Constant *Const = NewPred == ICmpInst::ICMP_SLT
>                           ? Constant::getNullValue(Op1->getType())
>                           : Constant::getAllOnesValue(Op1->getType());
>     return new ICmpInst(NewPred, Op1, Const);
>   }
> ```

Yeah, it makes sense. Please keep the commutative tests.


https://github.com/llvm/llvm-project/pull/73990


More information about the llvm-commits mailing list