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

via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 22 07:27:55 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;
----------------
ParkHanbum wrote:

I'm tried it. 
I tried it, but I noticed that somewhere in Pass it changes it back. What do you think?
```
IC: Old =   %cmp = icmp slt i8 %not, %x
    New =   <badref> = icmp sge i8 %x, 0
ADD:   %cmp = icmp sge i8 %x, 0
IC: ERASE   %1 = icmp slt i8 %not, %x
ADD DEFERRED:   %not = xor i8 %x, -1
IC: ERASE   %not = xor i8 %x, -1
IC: Visiting:   %cmp = icmp sge i8 %x, 0
IC: Old =   %cmp = icmp sge i8 %x, 0
    New =   <badref> = icmp sgt i8 %x, -1

```

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


More information about the llvm-commits mailing list