[clang] [llvm] [Clang] Add __builtin_assume_dereferenceable to encode deref assumption. (PR #121789)
Erich Keane via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 8 06:05:38 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.
----------------
erichkeane wrote:
A better description as to WHY it can do so unconditionally would be nice. I originally assumed it was making some sort of assumption about the value of 'y' and was unclear why that would be the case.
Here's a side note: Does this builtin ALSO imply the opposite? A `__builtin_assume_dereferenceable` states that the 1st 4 bytes are dereferenceable (so only index 0?), but does that mean it is NOT `derferenceable` beyond that? For example, do we NOW know that:
`x[3]` is UB? I should hope not...
https://github.com/llvm/llvm-project/pull/121789
More information about the llvm-commits
mailing list