Index: llvm/docs/LangRef.rst =================================================================== --- llvm/docs/LangRef.rst +++ llvm/docs/LangRef.rst @@ -601,11 +601,29 @@ representation; that is, the integral representation may be target dependent or unstable (not backed by a fixed integer). -``inttoptr`` and ``ptrtoint`` instructions converting integers to non-integral -pointer types or vice versa are implementation defined, and subject to likely -future revision in semantics. Vector versions of said instructions are as well. -Users of non-integral-pointer types are advised not to design around current -semantics as they may very well change in the nearish future. +``inttoptr`` and ``ptrtoint`` instructions have the same semantics as for +integral (i.e. normal) pointers in that they convert integers to and from +corresponding pointer types, but there are additional implications to be +aware of. Because the bit-representation of a non-integral pointer may +not be stable, two identical casts of the same operand may or may not +return the same value. Said differently, the conversion to or from the +non-integral type depends on environmental state in an implementation +defined manner. + +If the frontend wishes to observe a *particular* value following a cast, the +generated IR must fence with the underlying environment in an implementation +defined manner. (In practice, this tends to require ``noinline`` routines for +such operations.) + +From the perspective of the optimizer, ``inttoptr`` and ``ptrtoint`` for +non-integral types are analogous to ones on integral types with one +key exception: the optimizer may not, in general, insert new dynamic +occurrences of such casts. If a new cast is inserted, the optimizer would +need to either ensure that a) all possible values are valid, or b) +appropriate fencing is inserted. Since the appropriate fencing is +implementation defined, the optimizer can't do the latter. The former is +challenging as many commonly expected properties, such as +``ptrtoint(v)-ptrtoint(v) == 0``, don't hold for non-integral types. .. _globalvars: