@@ -11184,6 +11184,8 @@ Syntax:
1118411184
1118511185 <result> = getelementptr <ty>, ptr <ptrval>{, <ty> <idx>}*
1118611186 <result> = getelementptr inbounds <ty>, ptr <ptrval>{, <ty> <idx>}*
11187+ <result> = getelementptr nusw <ty>, ptr <ptrval>{, <ty> <idx>}*
11188+ <result> = getelementptr nuw <ty>, ptr <ptrval>{, <ty> <idx>}*
1118711189 <result> = getelementptr inrange(S,E) <ty>, ptr <ptrval>{, <ty> <idx>}*
1118811190 <result> = getelementptr <ty>, <N x ptr> <ptrval>, <vector index type> <idx>
1118911191
@@ -11299,27 +11301,47 @@ memory though, even if it happens to point into allocated storage. See the
1129911301:ref:`Pointer Aliasing Rules <pointeraliasing>` section for more
1130011302information.
1130111303
11302- If the ``inbounds`` keyword is present, the result value of a
11303- ``getelementptr`` with any non-zero indices is a
11304- :ref:`poison value <poisonvalues>` if one of the following rules is violated:
11305-
11306- * The base pointer has an *in bounds* address of an allocated object, which
11304+ The ``getelementptr`` instruction may have a number of attributes that impose
11305+ additional rules. If any of the rules are violated, the result value is a
11306+ :ref:`poison value <poisonvalues>`. In cases where the base is a vector of
11307+ pointers, the attributes apply to each computation element-wise.
11308+
11309+ For ``nusw`` (no unsigned signed wrap):
11310+
11311+ * If the type of an index is larger than the pointer index type, the
11312+ truncation to the pointer index type preserves the signed value
11313+ (``trunc nsw``).
11314+ * The multiplication of an index by the type size does not wrap the pointer
11315+ index type in a signed sense (``mul nsw``).
11316+ * The successive addition of each offset (without adding the base address)
11317+ does not wrap the pointer index type in a signed sense (``add nsw``).
11318+ * The successive addition of the current address, truncated to the pointer
11319+ index type and interpreted as an unsigned number, and each offset,
11320+ interpreted as a signed number, does not wrap the pointer index type.
11321+
11322+ For ``nuw`` (no unsigned wrap):
11323+
11324+ * If the type of an index is larger than the pointer index type, the
11325+ truncation to the pointer index type preserves the unsigned value
11326+ (``trunc nuw``).
11327+ * The multiplication of an index by the type size does not wrap the pointer
11328+ index type in an unsigned sense (``mul nuw``).
11329+ * The successive addition of each offset (without adding the base address)
11330+ does not wrap the pointer index type in an unsigned sense (``add nuw``).
11331+ * The successive addition of the current address, truncated to the pointer
11332+ index type and interpreted as an unsigned number, and each offset, also
11333+ interpreted as an unsigned number, does not wrap the pointer index type
11334+ (``add nuw``).
11335+
11336+ For ``inbounds`` all rules of the ``nusw`` attribute apply. Additionally,
11337+ if the ``getelementptr`` has any non-zero indices, the following rules apply:
11338+
11339+ * The base pointer has an *in bounds* address of an allocated object, which
1130711340 means that it points into an allocated object, or to its end. Note that the
1130811341 object does not have to be live anymore; being in-bounds of a deallocated
1130911342 object is sufficient.
11310- * If the type of an index is larger than the pointer index type, the
11311- truncation to the pointer index type preserves the signed value.
11312- * The multiplication of an index by the type size does not wrap the pointer
11313- index type in a signed sense (``nsw``).
11314- * The successive addition of each offset (without adding the base address) does
11315- not wrap the pointer index type in a signed sense (``nsw``).
11316- * The successive addition of the current address, interpreted as an unsigned
11317- number, and each offset, interpreted as a signed number, does not wrap the
11318- unsigned address space and remains *in bounds* of the allocated object.
11319- As a corollary, if the added offset is non-negative, the addition does not
11320- wrap in an unsigned sense (``nuw``).
11321- * In cases where the base is a vector of pointers, the ``inbounds`` keyword
11322- applies to each of the computations element-wise.
11343+ * During the successive addition of offsets to the address, the resulting
11344+ pointer must remain *in bounds* of the allocated object at each step.
1132311345
1132411346Note that ``getelementptr`` with all-zero indices is always considered to be
1132511347``inbounds``, even if the base pointer does not point to an allocated object.
@@ -11330,6 +11352,10 @@ These rules are based on the assumption that no allocated object may cross
1133011352the unsigned address space boundary, and no allocated object may be larger
1133111353than half the pointer index type space.
1133211354
11355+ If ``inbounds`` is present on a ``getelementptr`` instruction, the ``nusw``
11356+ attribute will be automatically set as well. For this reason, the ``nusw``
11357+ will also not be printed in textual IR if ``inbounds`` is already present.
11358+
1133311359If the ``inrange(Start, End)`` attribute is present, loading from or
1133411360storing to any pointer derived from the ``getelementptr`` has undefined
1133511361behavior if the load or store would access memory outside the half-open range
0 commit comments