[llvm] [InstCombine] fold icmp with add/sub instructions having the same operands (PR #143241)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 16 00:49:59 PDT 2025


================
@@ -7728,6 +7728,37 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
     }
   }
 
+  // icmp slt (sub nsw x, y), (add nsw x, y)  -->  icmp sgt y, 0
+  // icmp sgt (sub nsw x, y), (add nsw x, y)  -->  icmp slt y, 0
+  // icmp sle (sub nsw x, y), (add nsw x, y)  -->  icmp sge y, 0
+  // icmp sge (sub nsw x, y), (add nsw x, y)  -->  icmp sle y, 0
+  // icmp ult (sub nuw x, y), (add nuw x, y)  -->  icmp ugt y, 0
+  // icmp ugt (sub nuw x, y), (add nuw x, y)  -->  icmp ult y, 0
+  // icmp ule (sub nuw x, y), (add nuw x, y)  -->  icmp uge y, 0
+  // icmp uge (sub nuw x, y), (add nuw x, y)  -->  icmp ule y, 0
+  // icmp eq (sub nsw/nuw x, y), (add nsw/nuw x, y)   -->  icmp eq y, 0
+  // icmp ne (sub nsw/nuw x, y), (add nsw/nuw x, y)   -->  icmp ne y, 0
----------------
nikic wrote:

```suggestion
  // icmp slt (sub nsw x, y), (add nsw x, y)  -->  icmp sgt y, 0
  // icmp ult (sub nuw x, y), (add nuw x, y)  -->  icmp ugt y, 0
  // icmp eq (sub nsw/nuw x, y), (add nsw/nuw x, y)   -->  icmp eq y, 0
```
The full list is a bit excessive, I think something like this should be clear enough?

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


More information about the llvm-commits mailing list