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

via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 10 14:33:54 PDT 2024


================
@@ -4540,14 +4540,45 @@ static Instruction *foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q,
   if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A))))
     return nullptr;
 
+  CmpInst::Predicate StrictPred = ICmpInst::getStrictPredicate(Pred);
+  // 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;
----------------
goldsteinn wrote:

I might just make this `ICMP_SGE` so you can just unconditionally use `0` for the const below.
Canonicalization will clean it up for you later.

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


More information about the llvm-commits mailing list