[llvm] [InstCombine] simplify `x (comp) ~x` (PR #73990)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 1 17:12:49 PST 2023


================
@@ -7035,6 +7035,54 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
       return new ICmpInst(I.getSwappedPredicate(Pred), Builder.CreateXor(X, Y),
                           Z);
 
+    // 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 u<  (SIGNBIT(X))
+    //           X u>  ~X  -->   X u>  (SIGNBIT(X))
+    //           X u<= ~X  -->   X u<  (SIGNBIT(X))
+    //           X u>= ~X  -->   X u>  (SIGNBIT(X))
+    //           X ==  ~X  -->   false
+    //           X !=  ~X  -->   true
+    if (match(&I, m_c_ICmp(Pred, m_Value(X), m_Not(m_Deferred(X))))) {
+      // if `~X (comp) X`, replace it with `X (Swapped-comp) ~X`.
+      // ex) ~X s< X  -->  X s> ~X
+      if (match(I.getOperand(0), m_Not(m_Specific(X))))
+        Pred = I.getSwappedPredicate();
----------------
dtcxzyw wrote:

```suggestion
```
Don't do this. `m_c_ICmp` will swap the predicate if operands are commuted.

https://github.com/llvm/llvm-project/blob/fc74db466b0d2b87d2013d5e24be137f0d8b6f0a/llvm/include/llvm/IR/PatternMatch.h#L1359-L1384
https://github.com/llvm/llvm-project/blob/fc74db466b0d2b87d2013d5e24be137f0d8b6f0a/llvm/include/llvm/IR/PatternMatch.h#L2269-L2276


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


More information about the llvm-commits mailing list