[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
Tue Nov 25 09:26:50 PST 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:

After a deeper investigation, I found it does not work as well:  https://alive2.llvm.org/ce/z/8fdoTP
Now I am fine with this patch. But it should use `getUnderlyingObject` to make sure all pointers point to the same object. Then we can see if it is useful in real-world cases.

In the future, we can introduce an intrinsic like llvm.ptrdiff to preserve the same-underlying-object assumption.


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


More information about the llvm-commits mailing list