[clang] [llvm] [IR] Add getelementptr nusw and nuw flags (PR #90824)

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Thu May 2 18:06:38 PDT 2024


================
@@ -11295,27 +11297,46 @@ memory though, even if it happens to point into allocated storage. See the
 :ref:`Pointer Aliasing Rules <pointeraliasing>` section for more
 information.
 
-If the ``inbounds`` keyword is present, the result value of a
-``getelementptr`` with any non-zero indices is a
-:ref:`poison value <poisonvalues>` if one of the following rules is violated:
-
-*  The base pointer has an *in bounds* address of an allocated object, which
+The ``getelementptr`` instruction may have a number of attributes that impose
+additional rules. If any of the rules are violated, the result value is a
+:ref:`poison value <poisonvalues>`. In cases where the base is a vector of
+pointers, the attributes apply to each computation element-wise.
+
+For ``nusw`` (no unsigned signed wrap):
+
+ * If the type of an index is larger than the pointer index type, the
+   truncation to the pointer index type preserves the signed value
+   (``trunc nsw``).
+ * The multiplication of an index by the type size does not wrap the pointer
+   index type in a signed sense (``mul nsw``).
+ * The successive addition of each offset (without adding the base address)
+   does not wrap the pointer index type in a signed sense (``add nsw``).
+ * The successive addition of the current address, truncated to the index type
+   and interpreted as an unsigned number, and each offset, interpreted as
+   a signed number, does not wrap the index type.
----------------
nikic wrote:

> missing '(`add nsw`)' at the end of this?

No, because the base is unsigned here. This would be `add nusw` if it existed.

> Also maybe im a bit confused, but should this not be 'pointer index type'?

Yeah, I've updated this and other places to use "pointer index type".

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


More information about the cfe-commits mailing list