1 ========================================
2 Machine IR (MIR) Format Reference Manual
3 ========================================
9 This is a work in progress.
14 This document is a reference manual for the Machine IR (MIR) serialization
15 format. MIR is a human readable serialization format that is used to represent
16 LLVM's :ref:`machine specific intermediate representation
17 <machine code representation>`.
19 The MIR serialization format is designed to be used for testing the code
20 generation passes in LLVM.
25 The MIR serialization format uses a YAML container. YAML is a standard
26 data serialization language, and the full YAML language spec can be read at
28 <http://www.yaml.org/spec/1.2/spec.html#Introduction>`_.
30 A MIR file is split up into a series of `YAML documents`_. The first document
31 can contain an optional embedded LLVM IR module, and the rest of the documents
32 contain the serialized machine functions.
34 .. _YAML documents: http://www.yaml.org/spec/1.2/spec.html#id2800132
39 You can use the MIR format for testing in two different ways:
41 - You can write MIR tests that invoke a single code generation pass using the
42 ``run-pass`` option in llc.
44 - You can use llc's ``stop-after`` option with existing or new LLVM assembly
45 tests and check the MIR output of a specific code generation pass.
47 Testing Individual Code Generation Passes
48 -----------------------------------------
50 The ``run-pass`` option in llc allows you to create MIR tests that invoke
51 just a single code generation pass. When this option is used, llc will parse
52 an input MIR file, run the specified code generation pass, and print the
53 resulting MIR to the standard output stream.
55 You can generate an input MIR file for the test by using the ``stop-after``
56 option in llc. For example, if you would like to write a test for the
57 post register allocation pseudo instruction expansion pass, you can specify
58 the machine copy propagation pass in the ``stop-after`` option, as it runs
59 just before the pass that we are trying to test:
61 ``llc -stop-after machine-cp bug-trigger.ll > test.mir``
63 After generating the input MIR file, you'll have to add a run line that uses
64 the ``-run-pass`` option to it. In order to test the post register allocation
65 pseudo instruction expansion pass on X86-64, a run line like the one shown
68 ``# RUN: llc -run-pass postrapseudos -march=x86-64 %s -o /dev/null | FileCheck %s``
70 The MIR files are target dependent, so they have to be placed in the target
71 specific test directories. They also need to specify a target triple or a
72 target architecture either in the run line or in the embedded LLVM IR module.
77 Currently the MIR format has several limitations in terms of which state it
80 - The target-specific state in the target-specific ``MachineFunctionInfo``
81 subclasses isn't serialized at the moment.
83 - The target-specific ``MachineConstantPoolValue`` subclasses (in the ARM and
84 SystemZ backends) aren't serialized at the moment.
86 - The ``MCSymbol`` machine operands are only printed, they can't be parsed.
88 - A lot of the state in ``MachineModuleInfo`` isn't serialized - only the CFI
89 instructions and the variable debug information from MMI is serialized right
92 These limitations impose restrictions on what you can test with the MIR format.
93 For now, tests that would like to test some behaviour that depends on the state
94 of certain ``MCSymbol`` operands or the exception handling state in MMI, can't
95 use the MIR format. As well as that, tests that test some behaviour that
96 depends on the state of the target specific ``MachineFunctionInfo`` or
97 ``MachineConstantPoolValue`` subclasses can't use the MIR format at the moment.
107 When the first YAML document contains a `YAML block literal string`_, the MIR
108 parser will treat this string as an LLVM assembly language string that
109 represents an embedded LLVM IR module.
110 Here is an example of a YAML document that contains an LLVM module:
115 define i32 @inc(i32* %x) {
117 %0 = load i32, i32* %x
119 store i32 %1, i32* %x
124 .. _YAML block literal string: http://www.yaml.org/spec/1.2/spec.html#id2795688
129 The remaining YAML documents contain the machine functions. This is an example
130 of such YAML document:
136 tracksRegLiveness: true
143 %eax = MOV32rm %rdi, 1, _, 0, _
144 %eax = INC32r killed %eax, implicit-def dead %eflags
145 MOV32mr killed %rdi, 1, _, 0, _, %eax
149 The document above consists of attributes that represent the various
150 properties and data structures in a machine function.
152 The attribute ``name`` is required, and its value should be identical to the
153 name of a function that this machine function is based on.
155 The attribute ``body`` is a `YAML block literal string`_. Its value represents
156 the function's machine basic blocks and their machine instructions.
158 Machine Instructions Format Reference
159 =====================================
161 The machine basic blocks and their instructions are represented using a custom,
162 human readable serialization language. This language is used in the
163 `YAML block literal string`_ that corresponds to the machine function's body.
165 A source string that uses this language contains a list of machine basic
166 blocks, which are described in the section below.
171 A machine basic block is defined in a single block definition source construct
172 that contains the block's ID.
173 The example below defines two blocks that have an ID of zero and one:
182 A machine basic block can also have a name. It should be specified after the ID
183 in the block's definition:
187 bb.0.entry: ; This block's name is "entry"
190 The block's name should be identical to the name of the IR block that this
191 machine block is based on.
196 The machine basic blocks are identified by their ID numbers. Individual
197 blocks are referenced using the following syntax:
213 The machine basic block's successors have to be specified before any of the
219 successors: %bb.1.then, %bb.2.else
226 The branch weights can be specified in brackets after the successor blocks.
227 The example below defines a block that has two successors with branch weights
233 successors: %bb.1.then(32), %bb.2.else(16)
240 The machine basic block's live in registers have to be specified before any of
248 The list of live in registers and successors can be empty. The language also
249 allows multiple live in register and successor lists - they are combined into
250 one list by the parser.
252 Miscellaneous Attributes
253 ^^^^^^^^^^^^^^^^^^^^^^^^
255 The attributes ``IsAddressTaken``, ``IsLandingPad`` and ``Alignment`` can be
256 specified in brackets after the block's definition:
260 bb.0.entry (address-taken):
264 bb.3(landing-pad, align 4):
267 .. TODO: Describe the way the reference to an unnamed LLVM IR block can be
273 A machine instruction is composed of a name,
274 :ref:`machine operands <machine-operands>`,
275 :ref:`instruction flags <instruction-flags>`, and machine memory operands.
277 The instruction's name is usually specified before the operands. The example
278 below shows an instance of the X86 ``RETQ`` instruction with a single machine
285 However, if the machine instruction has one or more explicitly defined register
286 operands, the instruction's name has to be specified after them. The example
287 below shows an instance of the AArch64 ``LDPXpost`` instruction with three
288 defined register operands:
292 %sp, %fp, %lr = LDPXpost %sp, 2
294 The instruction names are serialized using the exact definitions from the
295 target's ``*InstrInfo.td`` files, and they are case sensitive. This means that
296 similar instruction names like ``TSTri`` and ``tSTRi`` represent different
297 machine instructions.
299 .. _instruction-flags:
304 The flag ``frame-setup`` can be specified before the instruction's name:
308 %fp = frame-setup ADDXri %sp, 0, 0
315 Registers are one of the key primitives in the machine instructions
316 serialization language. They are primarly used in the
317 :ref:`register machine operands <register-operands>`,
318 but they can also be used in a number of other places, like the
319 :ref:`basic block's live in list <bb-liveins>`.
321 The physical registers are identified by their name. They use the following
328 The example below shows three X86 physical registers:
336 The virtual registers are identified by their ID number. They use the following
349 The null registers are represented using an underscore ('``_``'). They can also be
350 represented using a '``%noreg``' named register, although the former syntax
353 .. _machine-operands:
358 There are seventeen different kinds of machine operands, and all of them, except
359 the ``MCSymbol`` operand, can be serialized. The ``MCSymbol`` operands are
360 just printed out - they can't be parsed back yet.
365 The immediate machine operands are untyped, 64-bit signed integers. The
366 example below shows an instance of the X86 ``MOV32ri`` instruction that has an
367 immediate machine operand ``-42``:
373 .. TODO: Describe the CIMM (Rare) and FPIMM immediate operands.
375 .. _register-operands:
380 The :ref:`register <registers>` primitive is used to represent the register
381 machine operands. The register operands can also have optional
382 :ref:`register flags <register-flags>`,
383 :ref:`a subregister index <subregister-indices>`,
384 and a reference to the tied register operand.
385 The full syntax of a register operand is shown below:
389 [<flags>] <register> [ :<subregister-idx-name> ] [ (tied-def <tied-op>) ]
391 This example shows an instance of the X86 ``XOR32rr`` instruction that has
392 5 register operands with different register flags:
396 dead %eax = XOR32rr undef %eax, undef %eax, implicit-def dead %eflags, implicit-def %al
403 The table below shows all of the possible register flags along with the
404 corresponding internal ``llvm::RegState`` representation:
413 - ``RegState::Implicit``
416 - ``RegState::ImplicitDefine``
419 - ``RegState::Define``
428 - ``RegState::Undef``
431 - ``RegState::InternalRead``
433 * - ``early-clobber``
434 - ``RegState::EarlyClobber``
437 - ``RegState::Debug``
439 .. _subregister-indices:
444 The register machine operands can reference a portion of a register by using
445 the subregister indices. The example below shows an instance of the ``COPY``
446 pseudo instruction that uses the X86 ``sub_8bit`` subregister index to copy 8
447 lower bits from the 32-bit virtual register 0 to the 8-bit virtual register 1:
451 %1 = COPY %0:sub_8bit
453 The names of the subregister indices are target specific, and are typically
454 defined in the target's ``*RegisterInfo.td`` file.
456 Global Value Operands
457 ^^^^^^^^^^^^^^^^^^^^^
459 The global value machine operands reference the global values from the
460 :ref:`embedded LLVM IR module <embedded-module>`.
461 The example below shows an instance of the X86 ``MOV64rm`` instruction that has
462 a global value operand named ``G``:
466 %rax = MOV64rm %rip, 1, _, @G, _
468 The named global values are represented using an identifier with the '@' prefix.
469 If the identifier doesn't match the regular expression
470 `[-a-zA-Z$._][-a-zA-Z$._0-9]*`, then this identifier must be quoted.
472 The unnamed global values are represented using an unsigned numeric value with
473 the '@' prefix, like in the following examples: ``@0``, ``@989``.
475 .. TODO: Describe the parsers default behaviour when optional YAML attributes
477 .. TODO: Describe the syntax for the bundled instructions.
478 .. TODO: Describe the syntax for virtual register YAML definitions.
479 .. TODO: Describe the machine function's YAML flag attributes.
480 .. TODO: Describe the syntax for the external symbol and register
481 mask machine operands.
482 .. TODO: Describe the frame information YAML mapping.
483 .. TODO: Describe the syntax of the stack object machine operands and their
485 .. TODO: Describe the syntax of the constant pool machine operands and their
487 .. TODO: Describe the syntax of the jump table machine operands and their
489 .. TODO: Describe the syntax of the block address machine operands.
490 .. TODO: Describe the syntax of the CFI index machine operands.
491 .. TODO: Describe the syntax of the metadata machine operands, and the
492 instructions debug location attribute.
493 .. TODO: Describe the syntax of the target index machine operands.
494 .. TODO: Describe the syntax of the register live out machine operands.
495 .. TODO: Describe the syntax of the machine memory operands.