[llvm] [InstCombine] Optimize icmp(sub(a, c), sub(b, c)) to icmp(a, b) if a, b, and c are pointers (PR #161698)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 2 10:38:08 PDT 2025


================
@@ -0,0 +1,109 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define ptr @icmp_ptrdiff_gt(ptr %0, ptr %1, ptr %2) {
----------------
dtcxzyw wrote:

Unfortunately it doesn't work in LLVM: https://alive2.llvm.org/ce/z/AYPSer

Can we set nsw for pointer difference here (we use sdiv to compute the number of elements, so it is meaningless if the sub signed wraps)?
See also https://github.com/llvm/llvm-project/blob/d68f0c2e1c1fafea983f92233e8ef9bcfe2a410a/clang/lib/CodeGen/CGExprScalar.cpp#L4695-L4744



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


More information about the llvm-commits mailing list