Skip to content

Commit 2d21068

Browse files
committed
[Docs] Add LangRef documentation for freeze instruction
Summary: - Describe the new freeze instruction - Make it explicit that branch on undef/poison is UB Reviewers: chandlerc, majnemer, efriedma, nikic, reames, jdoerfert, lebedev.ri, regehr Subscribers: fhahn, bollu, lebedev.ri, delcypher, spatel, filcab, llvm-commits, aqjune Differential Revision: https://reviews.llvm.org/D29121
1 parent 9376714 commit 2d21068

File tree

1 file changed

+81
-33
lines changed

1 file changed

+81
-33
lines changed

llvm/docs/LangRef.rst

Lines changed: 81 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,6 +3242,9 @@ the value is not necessarily consistent over time. In fact, ``%A`` and
32423242
``%C`` need to have the same semantics or the core LLVM "replace all
32433243
uses with" concept would not hold.
32443244

3245+
To ensure all uses of a given register observe the same value (even if
3246+
'``undef``'), the :ref:`freeze instruction <i_freeze>` can be used.
3247+
32453248
.. code-block:: llvm
32463249

32473250
%A = sdiv undef, %X
@@ -3335,6 +3338,8 @@ Poison value behavior is defined in terms of value *dependence*:
33353338
An instruction that *depends* on a poison value, produces a poison value
33363339
itself. A poison value may be relaxed into an
33373340
:ref:`undef value <undefvalues>`, which takes an arbitrary bit-pattern.
3341+
Propagation of poison can be stopped with the
3342+
:ref:`freeze instruction <i_freeze>`.
33383343

33393344
This means that immediate undefined behavior occurs if a poison value is
33403345
used as an instruction operand that has any values that trigger undefined
@@ -3345,9 +3350,9 @@ behavior. Notably this includes (but is not limited to):
33453350
space).
33463351
- The divisor operand of a ``udiv``, ``sdiv``, ``urem`` or ``srem``
33473352
instruction.
3348-
3349-
Additionally, undefined behavior occurs if a side effect *depends* on poison.
3350-
This includes side effects that are control dependent on a poisoned branch.
3353+
- The condition operand of a :ref:`br <i_br>` instruction.
3354+
- The callee operand of a :ref:`call <i_call>` or :ref:`invoke <i_invoke>`
3355+
instruction.
33513356

33523357
Here are some examples:
33533358

@@ -3366,40 +3371,12 @@ Here are some examples:
33663371
%narrowaddr = bitcast i32* @g to i16*
33673372
%wideaddr = bitcast i32* @g to i64*
33683373
%poison3 = load i16, i16* %narrowaddr ; Returns a poison value.
3369-
%poison4 = load i64, i64* %wideaddr ; Returns a poison value.
3374+
%poison4 = load i64, i64* %wideaddr ; Returns a poison value.
33703375

33713376
%cmp = icmp slt i32 %poison, 0 ; Returns a poison value.
3372-
br i1 %cmp, label %true, label %end ; Branch to either destination.
3373-
3374-
true:
3375-
store volatile i32 0, i32* @g ; This is control-dependent on %cmp, so
3376-
; it has undefined behavior.
3377-
br label %end
3377+
br i1 %cmp, label %end, label %end ; undefined behavior
33783378

33793379
end:
3380-
%p = phi i32 [ 0, %entry ], [ 1, %true ]
3381-
; Both edges into this PHI are
3382-
; control-dependent on %cmp, so this
3383-
; always results in a poison value.
3384-
3385-
store volatile i32 0, i32* @g ; This would depend on the store in %true
3386-
; if %cmp is true, or the store in %entry
3387-
; otherwise, so this is undefined behavior.
3388-
3389-
br i1 %cmp, label %second_true, label %second_end
3390-
; The same branch again, but this time the
3391-
; true block doesn't have side effects.
3392-
3393-
second_true:
3394-
; No side effects!
3395-
ret void
3396-
3397-
second_end:
3398-
store volatile i32 0, i32* @g ; This time, the instruction always depends
3399-
; on the store in %end. Also, it is
3400-
; control-equivalent to %end, so this is
3401-
; well-defined (ignoring earlier undefined
3402-
; behavior in this example).
34033380

34043381
.. _blockaddress:
34053382

@@ -6878,6 +6855,7 @@ Upon execution of a conditional '``br``' instruction, the '``i1``'
68786855
argument is evaluated. If the value is ``true``, control flows to the
68796856
'``iftrue``' ``label`` argument. If "cond" is ``false``, control flows
68806857
to the '``iffalse``' ``label`` argument.
6858+
If '``cond``' is ``poison``, this instruction has undefined behavior.
68816859

68826860
Example:
68836861
""""""""
@@ -6928,6 +6906,7 @@ When the '``switch``' instruction is executed, this table is searched
69286906
for the given value. If the value is found, control flow is transferred
69296907
to the corresponding destination; otherwise, control flow is transferred
69306908
to the default destination.
6909+
If '``value``' is ``poison``, this instruction has undefined behavior.
69316910

69326911
Implementation:
69336912
"""""""""""""""
@@ -6992,6 +6971,7 @@ Control transfers to the block specified in the address argument. All
69926971
possible destination blocks must be listed in the label list, otherwise
69936972
this instruction has undefined behavior. This implies that jumps to
69946973
labels defined in other functions have undefined behavior as well.
6974+
If '``address``' is ``poison``, this instruction has undefined behavior.
69956975

69966976
Implementation:
69976977
"""""""""""""""
@@ -10218,6 +10198,74 @@ Example:
1021810198

1021910199
%X = select i1 true, i8 17, i8 42 ; yields i8:17
1022010200

10201+
10202+
.. _i_freeze:
10203+
10204+
'``freeze``' Instruction
10205+
^^^^^^^^^^^^^^^^^^^^^^^^
10206+
10207+
Syntax:
10208+
"""""""
10209+
10210+
::
10211+
10212+
<result> = freeze ty <val> ; yields ty:result
10213+
10214+
Overview:
10215+
"""""""""
10216+
10217+
The '``freeze``' instruction is used to stop propagation of
10218+
:ref:`undef <undefvalues>` and :ref:`poison <poisonvalues>` values.
10219+
10220+
Arguments:
10221+
""""""""""
10222+
10223+
The '``freeze``' instruction takes a single argument.
10224+
10225+
Semantics:
10226+
""""""""""
10227+
10228+
If the argument is ``undef`` or ``poison``, '``freeze``' returns an
10229+
arbitrary, but fixed, value of type '``ty``'.
10230+
Otherwise, this instruction is a no-op and returns the input argument.
10231+
All uses of a value returned by the same '``freeze``' instruction are
10232+
guaranteed to always observe the same value, while different '``freeze``'
10233+
instructions may yield different values.
10234+
10235+
While ``undef`` and ``poison`` pointers can be frozen, the result is a
10236+
non-dereferenceable pointer. See the
10237+
:ref:`Pointer Aliasing Rules <pointeraliasing>` section for more information.
10238+
10239+
10240+
Example:
10241+
""""""""
10242+
10243+
.. code-block:: llvm
10244+
10245+
%w = i32 undef
10246+
%x = freeze i32 %w
10247+
%y = add i32 %w, %w ; undef
10248+
%z = add i32 %x, %x ; even number because all uses of %x observe
10249+
; the same value
10250+
%x2 = freeze i32 %w
10251+
%cmp = icmp eq i32 %x, %x2 ; can be true or false
10252+
10253+
; example with vectors
10254+
%v = <2 x i32> <i32 undef, i32 poison>
10255+
%a = extractelement <2 x i32> %v, i32 0 ; undef
10256+
%b = extractelement <2 x i32> %v, i32 1 ; poison
10257+
%add = add i32 %a, %a ; undef
10258+
10259+
%v.fr = freeze <2 x i32> %v ; element-wise freeze
10260+
%d = extractelement <2 x i32> %v.fr, i32 0 ; not undef
10261+
%add.f = add i32 %d, %d ; even number
10262+
10263+
; branching on frozen value
10264+
%poison = add nsw i1 %k, undef ; poison
10265+
%c = freeze i1 %poison
10266+
br i1 %c, label %foo, label %bar ; non-deterministic branch to %foo or %bar
10267+
10268+
1022110269
.. _i_call:
1022210270

1022310271
'``call``' Instruction

0 commit comments

Comments
 (0)