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

Eli Friedman via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 3 13:38:51 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) {
----------------
efriedma-quic wrote:

Right, the pointers have to point to the same object, and the size of an object is limited.  (From the C standard: "If the result is not representable in an object of that type, the behavior is undefined."  From LangRef: "the size of all allocated objects must be non-negative and not exceed the largest signed integer that fits into the index type.")

That said, because people with security-sensitive codebases are very sensitive to undefined behavior, we're trying to be conservative with new forms of undefined behavior: there should be a sanitizer to detect the behavior, and there should be a flag to disable it.  Which we can do here, I guess, but I'm not sure it's worth the trouble.

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


More information about the llvm-commits mailing list