xref: /openbsd-src/gnu/llvm/llvm/docs/GlobalISel/GMIR.rst (revision 73471bf04ceb096474c7f0fa83b1b65c70a787a1)
109467b48Spatrick.. _gmir:
209467b48Spatrick
309467b48SpatrickGeneric Machine IR
409467b48Spatrick==================
509467b48Spatrick
609467b48Spatrick.. contents::
709467b48Spatrick   :local:
809467b48Spatrick
909467b48SpatrickGeneric MIR (gMIR) is an intermediate representation that shares the same data
1009467b48Spatrickstructures as :doc:`MachineIR (MIR) <../MIRLangRef>` but has more relaxed
1109467b48Spatrickconstraints. As the compilation pipeline proceeds, these constraints are
1209467b48Spatrickgradually tightened until gMIR has become MIR.
1309467b48Spatrick
1409467b48SpatrickThe rest of this document will assume that you are familiar with the concepts
1509467b48Spatrickin :doc:`MachineIR (MIR) <../MIRLangRef>` and will highlight the differences
1609467b48Spatrickbetween MIR and gMIR.
1709467b48Spatrick
1809467b48Spatrick.. _gmir-instructions:
1909467b48Spatrick
2009467b48SpatrickGeneric Machine Instructions
2109467b48Spatrick----------------------------
2209467b48Spatrick
2309467b48Spatrick.. note::
2409467b48Spatrick
2509467b48Spatrick  This section expands on :ref:`mir-instructions` from the MIR Language
2609467b48Spatrick  Reference.
2709467b48Spatrick
2809467b48SpatrickWhereas MIR deals largely in Target Instructions and only has a small set of
2909467b48Spatricktarget independent opcodes such as ``COPY``, ``PHI``, and ``REG_SEQUENCE``,
3009467b48SpatrickgMIR defines a rich collection of ``Generic Opcodes`` which are target
3109467b48Spatrickindependent and describe operations which are typically supported by targets.
3209467b48SpatrickOne example is ``G_ADD`` which is the generic opcode for an integer addition.
3309467b48SpatrickMore information on each of the generic opcodes can be found at
3409467b48Spatrick:doc:`GenericOpcode`.
3509467b48Spatrick
3609467b48SpatrickThe ``MachineIRBuilder`` class wraps the ``MachineInstrBuilder`` and provides
3709467b48Spatricka convenient way to create these generic instructions.
3809467b48Spatrick
3909467b48Spatrick.. _gmir-gvregs:
4009467b48Spatrick
4109467b48SpatrickGeneric Virtual Registers
4209467b48Spatrick-------------------------
4309467b48Spatrick
4409467b48Spatrick.. note::
4509467b48Spatrick
4609467b48Spatrick  This section expands on :ref:`mir-registers` from the MIR Language
4709467b48Spatrick  Reference.
4809467b48Spatrick
4909467b48SpatrickGeneric virtual registers are like virtual registers but they are not assigned a
5009467b48SpatrickRegister Class constraint. Instead, generic virtual registers have less strict
5109467b48Spatrickconstraints starting with a :ref:`gmir-llt` and then further constrained to a
5209467b48Spatrick:ref:`gmir-regbank`. Eventually they will be constrained to a register class
5309467b48Spatrickat which point they become normal virtual registers.
5409467b48Spatrick
5509467b48SpatrickGeneric virtual registers can be used with all the virtual register API's
5609467b48Spatrickprovided by ``MachineRegisterInfo``. In particular, the def-use chain API's can
5709467b48Spatrickbe used without needing to distinguish them from non-generic virtual registers.
5809467b48Spatrick
5909467b48SpatrickFor simplicity, most generic instructions only accept virtual registers (both
6009467b48Spatrickgeneric and non-generic). There are some exceptions to this but in general:
6109467b48Spatrick
6209467b48Spatrick* instead of immediates, they use a generic virtual register defined by an
6309467b48Spatrick  instruction that materializes the immediate value (see
6409467b48Spatrick  :ref:`irtranslator-constants`). Typically this is a G_CONSTANT or a
6509467b48Spatrick  G_FCONSTANT. One example of an exception to this rule is G_SEXT_INREG where
6609467b48Spatrick  having an immediate is mandatory.
6709467b48Spatrick* instead of physical register, they use a generic virtual register that is
6809467b48Spatrick  either defined by a ``COPY`` from the physical register or used by a ``COPY``
6909467b48Spatrick  that defines the physical register.
7009467b48Spatrick
7109467b48Spatrick.. admonition:: Historical Note
7209467b48Spatrick
7309467b48Spatrick  We started with an alternative representation, where MRI tracks a size for
7409467b48Spatrick  each generic virtual register, and instructions have lists of types.
7509467b48Spatrick  That had two flaws: the type and size are redundant, and there was no generic
7609467b48Spatrick  way of getting a given operand's type (as there was no 1:1 mapping between
7709467b48Spatrick  instruction types and operands).
7809467b48Spatrick  We considered putting the type in some variant of MCInstrDesc instead:
79097a140dSpatrick  See `PR26576 <https://llvm.org/PR26576>`_: [GlobalISel] Generic MachineInstrs
8009467b48Spatrick  need a type but this increases the memory footprint of the related objects
8109467b48Spatrick
8209467b48Spatrick.. _gmir-regbank:
8309467b48Spatrick
8409467b48SpatrickRegister Bank
8509467b48Spatrick-------------
8609467b48Spatrick
8709467b48SpatrickA Register Bank is a set of register classes defined by the target. This
8809467b48Spatrickdefinition is rather loose so let's talk about what they can achieve.
8909467b48Spatrick
9009467b48SpatrickSuppose we have a processor that has two register files, A and B. These are
9109467b48Spatrickequal in every way and support the same instructions for the same cost. They're
9209467b48Spatrickjust physically stored apart and each instruction can only access registers from
9309467b48SpatrickA or B but never a mix of the two. If we want to perform an operation on data
9409467b48Spatrickthat's in split between the two register files, we must first copy all the data
9509467b48Spatrickinto a single register file.
9609467b48Spatrick
9709467b48SpatrickGiven a processor like this, we would benefit from clustering related data
9809467b48Spatricktogether into one register file so that we minimize the cost of copying data
9909467b48Spatrickback and forth to satisfy the (possibly conflicting) requirements of all the
10009467b48Spatrickinstructions. Register Banks are a means to constrain the register allocator to
10109467b48Spatrickuse a particular register file for a virtual register.
10209467b48Spatrick
10309467b48SpatrickIn practice, register files A and B are rarely equal. They can typically store
10409467b48Spatrickthe same data but there's usually some restrictions on what operations you can
10509467b48Spatrickdo on each register file. A fairly common pattern is for one of them to be
10609467b48Spatrickaccessible to integer operations and the other accessible to floating point
107*73471bf0Spatrickoperations. To accommodate this, let's rename A and B to GPR (general purpose
10809467b48Spatrickregisters) and FPR (floating point registers).
10909467b48Spatrick
11009467b48SpatrickWe now have some additional constraints that limit us. An operation like G_FMUL
11109467b48Spatrickhas to happen in FPR and G_ADD has to happen in GPR. However, even though this
11209467b48Spatrickprescribes a lot of the assignments we still have some freedom. A G_LOAD can
11309467b48Spatrickhappen in both GPR and FPR, and which we want depends on who is going to consume
11409467b48Spatrickthe loaded data. Similarly, G_FNEG can happen in both GPR and FPR. If we assign
11509467b48Spatrickit to FPR, then we'll use floating point negation. However, if we assign it to
11609467b48SpatrickGPR then we can equivalently G_XOR the sign bit with 1 to invert it.
11709467b48Spatrick
11809467b48SpatrickIn summary, Register Banks are a means of disambiguating between seemingly
11909467b48Spatrickequivalent choices based on some analysis of the differences when each choice
12009467b48Spatrickis applied in a given context.
12109467b48Spatrick
12209467b48SpatrickTo give some concrete examples:
12309467b48Spatrick
12409467b48SpatrickAArch64
12509467b48Spatrick
12609467b48Spatrick  AArch64 has three main banks. GPR for integer operations, FPR for floating
12709467b48Spatrick  point and also for the NEON vector instruction set. The third is CCR and
12809467b48Spatrick  describes the condition code register used for predication.
12909467b48Spatrick
13009467b48SpatrickMIPS
13109467b48Spatrick
13209467b48Spatrick  MIPS has five main banks of which many programs only really use one or two.
13309467b48Spatrick  GPR is the general purpose bank for integer operations. FGR or CP1 is for
13409467b48Spatrick  the floating point operations as well as the MSA vector instructions and a
13509467b48Spatrick  few other application specific extensions. CP0 is for system registers and
13609467b48Spatrick  few programs will use it. CP2 and CP3 are for any application specific
13709467b48Spatrick  coprocessors that may be present in the chip. Arguably, there is also a sixth
13809467b48Spatrick  for the LO and HI registers but these are only used for the result of a few
13909467b48Spatrick  operations and it's of questionable value to model distinctly from GPR.
14009467b48Spatrick
14109467b48SpatrickX86
14209467b48Spatrick
14309467b48Spatrick  X86 can be seen as having 3 main banks: general-purpose, x87, and
14409467b48Spatrick  vector (which could be further split into a bank per domain for single vs
14509467b48Spatrick  double precision instructions). It also looks like there's arguably a few
14609467b48Spatrick  more potential banks such as one for the AVX512 Mask Registers.
14709467b48Spatrick
14809467b48SpatrickRegister banks are described by a target-provided API,
14909467b48Spatrick:ref:`RegisterBankInfo <api-registerbankinfo>`.
15009467b48Spatrick
15109467b48Spatrick.. _gmir-llt:
15209467b48Spatrick
15309467b48SpatrickLow Level Type
15409467b48Spatrick--------------
15509467b48Spatrick
15609467b48SpatrickAdditionally, every generic virtual register has a type, represented by an
15709467b48Spatrickinstance of the ``LLT`` class.
15809467b48Spatrick
15909467b48SpatrickLike ``EVT``/``MVT``/``Type``, it has no distinction between unsigned and signed
16009467b48Spatrickinteger types.  Furthermore, it also has no distinction between integer and
16109467b48Spatrickfloating-point types: it mainly conveys absolutely necessary information, such
16209467b48Spatrickas size and number of vector lanes:
16309467b48Spatrick
16409467b48Spatrick* ``sN`` for scalars
16509467b48Spatrick* ``pN`` for pointers
16609467b48Spatrick* ``<N x sM>`` for vectors
16709467b48Spatrick
16809467b48Spatrick``LLT`` is intended to replace the usage of ``EVT`` in SelectionDAG.
16909467b48Spatrick
17009467b48SpatrickHere are some LLT examples and their ``EVT`` and ``Type`` equivalents:
17109467b48Spatrick
17209467b48Spatrick   =============  =========  ======================================
17309467b48Spatrick   LLT            EVT        IR Type
17409467b48Spatrick   =============  =========  ======================================
17509467b48Spatrick   ``s1``         ``i1``     ``i1``
17609467b48Spatrick   ``s8``         ``i8``     ``i8``
17709467b48Spatrick   ``s32``        ``i32``    ``i32``
17809467b48Spatrick   ``s32``        ``f32``    ``float``
17909467b48Spatrick   ``s17``        ``i17``    ``i17``
18009467b48Spatrick   ``s16``        N/A        ``{i8, i8}`` [#abi-dependent]_
18109467b48Spatrick   ``s32``        N/A        ``[4 x i8]`` [#abi-dependent]_
18209467b48Spatrick   ``p0``         ``iPTR``   ``i8*``, ``i32*``, ``%opaque*``
18309467b48Spatrick   ``p2``         ``iPTR``   ``i8 addrspace(2)*``
18409467b48Spatrick   ``<4 x s32>``  ``v4f32``  ``<4 x float>``
18509467b48Spatrick   ``s64``        ``v1f64``  ``<1 x double>``
18609467b48Spatrick   ``<3 x s32>``  ``v3i32``  ``<3 x i32>``
18709467b48Spatrick   =============  =========  ======================================
18809467b48Spatrick
18909467b48Spatrick
19009467b48SpatrickRationale: instructions already encode a specific interpretation of types
19109467b48Spatrick(e.g., ``add`` vs. ``fadd``, or ``sdiv`` vs. ``udiv``).  Also encoding that
19209467b48Spatrickinformation in the type system requires introducing bitcast with no real
19309467b48Spatrickadvantage for the selector.
19409467b48Spatrick
19509467b48SpatrickPointer types are distinguished by address space.  This matches IR, as opposed
19609467b48Spatrickto SelectionDAG where address space is an attribute on operations.
19709467b48SpatrickThis representation better supports pointers having different sizes depending
19809467b48Spatrickon their addressspace.
19909467b48Spatrick
20009467b48Spatrick.. note::
20109467b48Spatrick
20209467b48Spatrick  .. caution::
20309467b48Spatrick
20409467b48Spatrick    Is this still true? I thought we'd removed the 1-element vector concept.
20509467b48Spatrick    Hypothetically, it could be distinct from a scalar but I think we failed to
20609467b48Spatrick    find a real occurrence.
20709467b48Spatrick
20809467b48Spatrick  Currently, LLT requires at least 2 elements in vectors, but some targets have
20909467b48Spatrick  the concept of a '1-element vector'.  Representing them as their underlying
21009467b48Spatrick  scalar type is a nice simplification.
21109467b48Spatrick
21209467b48Spatrick.. rubric:: Footnotes
21309467b48Spatrick
21409467b48Spatrick.. [#abi-dependent] This mapping is ABI dependent. Here we've assumed no additional padding is required.
21509467b48Spatrick
21609467b48SpatrickGeneric Opcode Reference
21709467b48Spatrick------------------------
21809467b48Spatrick
21909467b48SpatrickThe Generic Opcodes that are available are described at :doc:`GenericOpcode`.
220