[clang] [llvm] [Clang] Add __builtin_assume_dereferenceable to encode deref assumption. (PR #121789)

Erich Keane via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 7 11:11:30 PST 2025


================
@@ -2761,6 +2761,41 @@ etc.).
 
 Query for this feature with ``__has_builtin(__builtin_assume_separate_storage)``.
 
+``__builtin_assume_dereferenceable``
+-------------------------------------
+
+``__builtin_assume_derefernceable`` is used to provide the optimizer with the
+knowledge that the pointer argument P is dereferenceable up to the specified
+number of bytes.
+
+**Syntax**:
+
+.. code-block:: c++
+
+    __builtin_assume_dereferenceable(const void *, size_t)
+
+**Example of Use**:
+
+.. code-block:: c++
+
+  int foo(int *x, int y) {
+      __builtin_assume_dereferenceable(x, 4);
+      int z = 0;
+      if (y == 1) {
+        // The optimizer may execute the load of x unconditionally.
+        z = *x;
+        }
+      return z;
+  }
+
+**Description**:
+
+The arguments to this function provide a start pointer ``P`` and a size ``S``.
+``P`` must be non-null and ``S`` at least 1. The optimizer may assume that
----------------
erichkeane wrote:

> In IR, dereferenceable and nonnull are separate, if the builtin assumes both, make 2 operand bundles and the IR passes should pick it up.

Is it?  What is the difference with `dereferenceable_or_null` then?  It seems to me that `dereferenceable` implies `nonnull`.

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


More information about the llvm-commits mailing list