@@ -3242,6 +3242,9 @@ the value is not necessarily consistent over time. In fact, ``%A`` and
3242
3242
``%C`` need to have the same semantics or the core LLVM "replace all
3243
3243
uses with" concept would not hold.
3244
3244
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
+
3245
3248
.. code-block:: llvm
3246
3249
3247
3250
%A = sdiv undef, %X
@@ -3335,6 +3338,8 @@ Poison value behavior is defined in terms of value *dependence*:
3335
3338
An instruction that *depends* on a poison value, produces a poison value
3336
3339
itself. A poison value may be relaxed into an
3337
3340
: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>`.
3338
3343
3339
3344
This means that immediate undefined behavior occurs if a poison value is
3340
3345
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):
3345
3350
space).
3346
3351
- The divisor operand of a ``udiv``, ``sdiv``, ``urem`` or ``srem``
3347
3352
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 .
3351
3356
3352
3357
Here are some examples:
3353
3358
@@ -3366,40 +3371,12 @@ Here are some examples:
3366
3371
%narrowaddr = bitcast i32* @g to i16*
3367
3372
%wideaddr = bitcast i32* @g to i64*
3368
3373
%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.
3370
3375
3371
3376
%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
3378
3378
3379
3379
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).
3403
3380
3404
3381
.. _blockaddress:
3405
3382
@@ -6878,6 +6855,7 @@ Upon execution of a conditional '``br``' instruction, the '``i1``'
6878
6855
argument is evaluated. If the value is ``true``, control flows to the
6879
6856
'``iftrue``' ``label`` argument. If "cond" is ``false``, control flows
6880
6857
to the '``iffalse``' ``label`` argument.
6858
+ If '``cond``' is ``poison``, this instruction has undefined behavior.
6881
6859
6882
6860
Example:
6883
6861
""""""""
@@ -6928,6 +6906,7 @@ When the '``switch``' instruction is executed, this table is searched
6928
6906
for the given value. If the value is found, control flow is transferred
6929
6907
to the corresponding destination; otherwise, control flow is transferred
6930
6908
to the default destination.
6909
+ If '``value``' is ``poison``, this instruction has undefined behavior.
6931
6910
6932
6911
Implementation:
6933
6912
"""""""""""""""
@@ -6992,6 +6971,7 @@ Control transfers to the block specified in the address argument. All
6992
6971
possible destination blocks must be listed in the label list, otherwise
6993
6972
this instruction has undefined behavior. This implies that jumps to
6994
6973
labels defined in other functions have undefined behavior as well.
6974
+ If '``address``' is ``poison``, this instruction has undefined behavior.
6995
6975
6996
6976
Implementation:
6997
6977
"""""""""""""""
@@ -10218,6 +10198,74 @@ Example:
10218
10198
10219
10199
%X = select i1 true, i8 17, i8 42 ; yields i8:17
10220
10200
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
+
10221
10269
.. _i_call:
10222
10270
10223
10271
'``call``' Instruction
0 commit comments