blob: f6ee6ccd0506b859ca2c23880e943f60c8a658fe [file] [log] [blame]
Alex Lorenz3d311772015-08-06 22:55:19 +00001========================================
2Machine IR (MIR) Format Reference Manual
3========================================
4
5.. contents::
6 :local:
7
8.. warning::
9 This is a work in progress.
10
11Introduction
12============
13
14This document is a reference manual for the Machine IR (MIR) serialization
15format. MIR is a human readable serialization format that is used to represent
16LLVM's :ref:`machine specific intermediate representation
17<machine code representation>`.
18
19The MIR serialization format is designed to be used for testing the code
20generation passes in LLVM.
21
22Overview
23========
24
25The MIR serialization format uses a YAML container. YAML is a standard
26data serialization language, and the full YAML language spec can be read at
27`yaml.org
28<http://www.yaml.org/spec/1.2/spec.html#Introduction>`_.
29
30A MIR file is split up into a series of `YAML documents`_. The first document
31can contain an optional embedded LLVM IR module, and the rest of the documents
32contain the serialized machine functions.
33
34.. _YAML documents: http://www.yaml.org/spec/1.2/spec.html#id2800132
35
Alex Lorenzea788c42015-08-21 22:58:33 +000036MIR Testing Guide
37=================
38
39You can use the MIR format for testing in two different ways:
40
41- You can write MIR tests that invoke a single code generation pass using the
42 ``run-pass`` option in llc.
43
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.
46
47Testing Individual Code Generation Passes
48-----------------------------------------
49
50The ``run-pass`` option in llc allows you to create MIR tests that invoke
51just a single code generation pass. When this option is used, llc will parse
52an input MIR file, run the specified code generation pass, and print the
53resulting MIR to the standard output stream.
54
55You can generate an input MIR file for the test by using the ``stop-after``
56option in llc. For example, if you would like to write a test for the
57post register allocation pseudo instruction expansion pass, you can specify
58the machine copy propagation pass in the ``stop-after`` option, as it runs
59just before the pass that we are trying to test:
60
61 ``llc -stop-after machine-cp bug-trigger.ll > test.mir``
62
63After generating the input MIR file, you'll have to add a run line that uses
64the ``-run-pass`` option to it. In order to test the post register allocation
65pseudo instruction expansion pass on X86-64, a run line like the one shown
66below can be used:
67
68 ``# RUN: llc -run-pass postrapseudos -march=x86-64 %s -o /dev/null | FileCheck %s``
69
70The MIR files are target dependent, so they have to be placed in the target
71specific test directories. They also need to specify a target triple or a
72target architecture either in the run line or in the embedded LLVM IR module.
73
74Limitations
75-----------
76
77Currently the MIR format has several limitations in terms of which state it
78can serialize:
79
80- The target-specific state in the target-specific ``MachineFunctionInfo``
81 subclasses isn't serialized at the moment.
82
83- The target-specific ``MachineConstantPoolValue`` subclasses (in the ARM and
84 SystemZ backends) aren't serialized at the moment.
85
86- The ``MCSymbol`` machine operands are only printed, they can't be parsed.
87
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
90 now.
91
92These limitations impose restrictions on what you can test with the MIR format.
93For now, tests that would like to test some behaviour that depends on the state
94of certain ``MCSymbol`` operands or the exception handling state in MMI, can't
95use the MIR format. As well as that, tests that test some behaviour that
96depends on the state of the target specific ``MachineFunctionInfo`` or
97``MachineConstantPoolValue`` subclasses can't use the MIR format at the moment.
98
Alex Lorenz3d311772015-08-06 22:55:19 +000099High Level Structure
100====================
101
Alex Lorenzd4990eb2015-09-08 11:38:16 +0000102.. _embedded-module:
103
Alex Lorenz3d311772015-08-06 22:55:19 +0000104Embedded Module
105---------------
106
107When the first YAML document contains a `YAML block literal string`_, the MIR
108parser will treat this string as an LLVM assembly language string that
109represents an embedded LLVM IR module.
110Here is an example of a YAML document that contains an LLVM module:
111
112.. code-block:: llvm
113
Alex Lorenz3d311772015-08-06 22:55:19 +0000114 define i32 @inc(i32* %x) {
115 entry:
116 %0 = load i32, i32* %x
117 %1 = add i32 %0, 1
118 store i32 %1, i32* %x
119 ret i32 %1
120 }
Alex Lorenz3d311772015-08-06 22:55:19 +0000121
122.. _YAML block literal string: http://www.yaml.org/spec/1.2/spec.html#id2795688
123
124Machine Functions
125-----------------
126
127The remaining YAML documents contain the machine functions. This is an example
128of such YAML document:
129
Renato Golin124f2592016-07-20 12:16:38 +0000130.. code-block:: text
Alex Lorenz3d311772015-08-06 22:55:19 +0000131
132 ---
133 name: inc
134 tracksRegLiveness: true
135 liveins:
136 - { reg: '%rdi' }
Alex Lorenz98461672015-08-14 00:36:10 +0000137 body: |
138 bb.0.entry:
139 liveins: %rdi
140
141 %eax = MOV32rm %rdi, 1, _, 0, _
142 %eax = INC32r killed %eax, implicit-def dead %eflags
143 MOV32mr killed %rdi, 1, _, 0, _, %eax
144 RETQ %eax
Alex Lorenz3d311772015-08-06 22:55:19 +0000145 ...
146
147The document above consists of attributes that represent the various
148properties and data structures in a machine function.
149
150The attribute ``name`` is required, and its value should be identical to the
151name of a function that this machine function is based on.
152
Alex Lorenz98461672015-08-14 00:36:10 +0000153The attribute ``body`` is a `YAML block literal string`_. Its value represents
154the function's machine basic blocks and their machine instructions.
Alex Lorenz3d311772015-08-06 22:55:19 +0000155
Alex Lorenz3a4a60c2015-08-15 01:06:06 +0000156Machine Instructions Format Reference
157=====================================
158
159The machine basic blocks and their instructions are represented using a custom,
160human readable serialization language. This language is used in the
161`YAML block literal string`_ that corresponds to the machine function's body.
162
163A source string that uses this language contains a list of machine basic
164blocks, which are described in the section below.
165
166Machine Basic Blocks
167--------------------
168
169A machine basic block is defined in a single block definition source construct
170that contains the block's ID.
171The example below defines two blocks that have an ID of zero and one:
172
Renato Golin124f2592016-07-20 12:16:38 +0000173.. code-block:: text
Alex Lorenz3a4a60c2015-08-15 01:06:06 +0000174
175 bb.0:
176 <instructions>
177 bb.1:
178 <instructions>
179
180A machine basic block can also have a name. It should be specified after the ID
181in the block's definition:
182
Renato Golin124f2592016-07-20 12:16:38 +0000183.. code-block:: text
Alex Lorenz3a4a60c2015-08-15 01:06:06 +0000184
185 bb.0.entry: ; This block's name is "entry"
186 <instructions>
187
188The block's name should be identical to the name of the IR block that this
189machine block is based on.
190
191Block References
192^^^^^^^^^^^^^^^^
193
194The machine basic blocks are identified by their ID numbers. Individual
195blocks are referenced using the following syntax:
196
Renato Golin124f2592016-07-20 12:16:38 +0000197.. code-block:: text
Alex Lorenz3a4a60c2015-08-15 01:06:06 +0000198
199 %bb.<id>[.<name>]
200
201Examples:
202
203.. code-block:: llvm
204
205 %bb.0
206 %bb.1.then
207
208Successors
209^^^^^^^^^^
210
211The machine basic block's successors have to be specified before any of the
212instructions:
213
Renato Golin124f2592016-07-20 12:16:38 +0000214.. code-block:: text
Alex Lorenz3a4a60c2015-08-15 01:06:06 +0000215
216 bb.0.entry:
217 successors: %bb.1.then, %bb.2.else
218 <instructions>
219 bb.1.then:
220 <instructions>
221 bb.2.else:
222 <instructions>
223
224The branch weights can be specified in brackets after the successor blocks.
225The example below defines a block that has two successors with branch weights
226of 32 and 16:
227
Renato Golin124f2592016-07-20 12:16:38 +0000228.. code-block:: text
Alex Lorenz3a4a60c2015-08-15 01:06:06 +0000229
230 bb.0.entry:
231 successors: %bb.1.then(32), %bb.2.else(16)
232
Alex Lorenzb981d372015-08-21 21:17:01 +0000233.. _bb-liveins:
234
Alex Lorenz3a4a60c2015-08-15 01:06:06 +0000235Live In Registers
236^^^^^^^^^^^^^^^^^
237
238The machine basic block's live in registers have to be specified before any of
239the instructions:
240
Renato Golin124f2592016-07-20 12:16:38 +0000241.. code-block:: text
Alex Lorenz3a4a60c2015-08-15 01:06:06 +0000242
243 bb.0.entry:
244 liveins: %edi, %esi
245
246The list of live in registers and successors can be empty. The language also
247allows multiple live in register and successor lists - they are combined into
248one list by the parser.
249
250Miscellaneous Attributes
251^^^^^^^^^^^^^^^^^^^^^^^^
252
253The attributes ``IsAddressTaken``, ``IsLandingPad`` and ``Alignment`` can be
254specified in brackets after the block's definition:
255
Renato Golin124f2592016-07-20 12:16:38 +0000256.. code-block:: text
Alex Lorenz3a4a60c2015-08-15 01:06:06 +0000257
258 bb.0.entry (address-taken):
259 <instructions>
260 bb.2.else (align 4):
261 <instructions>
262 bb.3(landing-pad, align 4):
263 <instructions>
264
265.. TODO: Describe the way the reference to an unnamed LLVM IR block can be
266 preserved.
267
Alex Lorenz8eadc3f2015-08-21 17:26:38 +0000268Machine Instructions
269--------------------
270
Alex Lorenzb981d372015-08-21 21:17:01 +0000271A machine instruction is composed of a name,
272:ref:`machine operands <machine-operands>`,
Alex Lorenz8eadc3f2015-08-21 17:26:38 +0000273:ref:`instruction flags <instruction-flags>`, and machine memory operands.
274
275The instruction's name is usually specified before the operands. The example
276below shows an instance of the X86 ``RETQ`` instruction with a single machine
277operand:
278
Renato Golin124f2592016-07-20 12:16:38 +0000279.. code-block:: text
Alex Lorenz8eadc3f2015-08-21 17:26:38 +0000280
281 RETQ %eax
282
283However, if the machine instruction has one or more explicitly defined register
284operands, the instruction's name has to be specified after them. The example
285below shows an instance of the AArch64 ``LDPXpost`` instruction with three
286defined register operands:
287
Renato Golin124f2592016-07-20 12:16:38 +0000288.. code-block:: text
Alex Lorenz8eadc3f2015-08-21 17:26:38 +0000289
290 %sp, %fp, %lr = LDPXpost %sp, 2
291
292The instruction names are serialized using the exact definitions from the
293target's ``*InstrInfo.td`` files, and they are case sensitive. This means that
294similar instruction names like ``TSTri`` and ``tSTRi`` represent different
295machine instructions.
296
297.. _instruction-flags:
298
299Instruction Flags
300^^^^^^^^^^^^^^^^^
301
302The flag ``frame-setup`` can be specified before the instruction's name:
303
Renato Golin124f2592016-07-20 12:16:38 +0000304.. code-block:: text
Alex Lorenz8eadc3f2015-08-21 17:26:38 +0000305
306 %fp = frame-setup ADDXri %sp, 0, 0
307
Alex Lorenzb981d372015-08-21 21:17:01 +0000308.. _registers:
309
310Registers
311---------
312
313Registers are one of the key primitives in the machine instructions
314serialization language. They are primarly used in the
315:ref:`register machine operands <register-operands>`,
316but they can also be used in a number of other places, like the
317:ref:`basic block's live in list <bb-liveins>`.
318
319The physical registers are identified by their name. They use the following
320syntax:
321
Renato Golin124f2592016-07-20 12:16:38 +0000322.. code-block:: text
Alex Lorenzb981d372015-08-21 21:17:01 +0000323
324 %<name>
325
326The example below shows three X86 physical registers:
327
Renato Golin124f2592016-07-20 12:16:38 +0000328.. code-block:: text
Alex Lorenzb981d372015-08-21 21:17:01 +0000329
330 %eax
331 %r15
332 %eflags
333
334The virtual registers are identified by their ID number. They use the following
335syntax:
336
Renato Golin124f2592016-07-20 12:16:38 +0000337.. code-block:: text
Alex Lorenzb981d372015-08-21 21:17:01 +0000338
339 %<id>
340
341Example:
342
Renato Golin124f2592016-07-20 12:16:38 +0000343.. code-block:: text
Alex Lorenzb981d372015-08-21 21:17:01 +0000344
345 %0
346
347The null registers are represented using an underscore ('``_``'). They can also be
348represented using a '``%noreg``' named register, although the former syntax
349is preferred.
350
351.. _machine-operands:
352
353Machine Operands
354----------------
355
356There are seventeen different kinds of machine operands, and all of them, except
357the ``MCSymbol`` operand, can be serialized. The ``MCSymbol`` operands are
358just printed out - they can't be parsed back yet.
359
360Immediate Operands
361^^^^^^^^^^^^^^^^^^
362
363The immediate machine operands are untyped, 64-bit signed integers. The
364example below shows an instance of the X86 ``MOV32ri`` instruction that has an
365immediate machine operand ``-42``:
366
Renato Golin124f2592016-07-20 12:16:38 +0000367.. code-block:: text
Alex Lorenzb981d372015-08-21 21:17:01 +0000368
369 %eax = MOV32ri -42
370
371.. TODO: Describe the CIMM (Rare) and FPIMM immediate operands.
372
373.. _register-operands:
374
375Register Operands
376^^^^^^^^^^^^^^^^^
377
378The :ref:`register <registers>` primitive is used to represent the register
379machine operands. The register operands can also have optional
380:ref:`register flags <register-flags>`,
Alex Lorenz37e02622015-09-08 11:39:47 +0000381:ref:`a subregister index <subregister-indices>`,
382and a reference to the tied register operand.
Alex Lorenzb981d372015-08-21 21:17:01 +0000383The full syntax of a register operand is shown below:
384
Renato Golin124f2592016-07-20 12:16:38 +0000385.. code-block:: text
Alex Lorenzb981d372015-08-21 21:17:01 +0000386
387 [<flags>] <register> [ :<subregister-idx-name> ] [ (tied-def <tied-op>) ]
388
389This example shows an instance of the X86 ``XOR32rr`` instruction that has
3905 register operands with different register flags:
391
Renato Golin124f2592016-07-20 12:16:38 +0000392.. code-block:: text
Alex Lorenzb981d372015-08-21 21:17:01 +0000393
394 dead %eax = XOR32rr undef %eax, undef %eax, implicit-def dead %eflags, implicit-def %al
395
396.. _register-flags:
397
398Register Flags
399~~~~~~~~~~~~~~
400
401The table below shows all of the possible register flags along with the
402corresponding internal ``llvm::RegState`` representation:
403
404.. list-table::
405 :header-rows: 1
406
407 * - Flag
408 - Internal Value
409
410 * - ``implicit``
411 - ``RegState::Implicit``
412
413 * - ``implicit-def``
414 - ``RegState::ImplicitDefine``
415
416 * - ``def``
417 - ``RegState::Define``
418
419 * - ``dead``
420 - ``RegState::Dead``
421
422 * - ``killed``
423 - ``RegState::Kill``
424
425 * - ``undef``
426 - ``RegState::Undef``
427
428 * - ``internal``
429 - ``RegState::InternalRead``
430
431 * - ``early-clobber``
432 - ``RegState::EarlyClobber``
433
434 * - ``debug-use``
435 - ``RegState::Debug``
Alex Lorenz3a4a60c2015-08-15 01:06:06 +0000436
Alex Lorenz37e02622015-09-08 11:39:47 +0000437.. _subregister-indices:
438
439Subregister Indices
440~~~~~~~~~~~~~~~~~~~
441
442The register machine operands can reference a portion of a register by using
443the subregister indices. The example below shows an instance of the ``COPY``
444pseudo instruction that uses the X86 ``sub_8bit`` subregister index to copy 8
445lower bits from the 32-bit virtual register 0 to the 8-bit virtual register 1:
446
Renato Golin124f2592016-07-20 12:16:38 +0000447.. code-block:: text
Alex Lorenz37e02622015-09-08 11:39:47 +0000448
449 %1 = COPY %0:sub_8bit
450
451The names of the subregister indices are target specific, and are typically
452defined in the target's ``*RegisterInfo.td`` file.
453
Alex Lorenzd4990eb2015-09-08 11:38:16 +0000454Global Value Operands
455^^^^^^^^^^^^^^^^^^^^^
456
457The global value machine operands reference the global values from the
458:ref:`embedded LLVM IR module <embedded-module>`.
459The example below shows an instance of the X86 ``MOV64rm`` instruction that has
460a global value operand named ``G``:
461
Renato Golin124f2592016-07-20 12:16:38 +0000462.. code-block:: text
Alex Lorenzd4990eb2015-09-08 11:38:16 +0000463
464 %rax = MOV64rm %rip, 1, _, @G, _
465
466The named global values are represented using an identifier with the '@' prefix.
467If the identifier doesn't match the regular expression
468`[-a-zA-Z$._][-a-zA-Z$._0-9]*`, then this identifier must be quoted.
469
470The unnamed global values are represented using an unsigned numeric value with
471the '@' prefix, like in the following examples: ``@0``, ``@989``.
472
Alex Lorenz3d311772015-08-06 22:55:19 +0000473.. TODO: Describe the parsers default behaviour when optional YAML attributes
474 are missing.
Alex Lorenz8eadc3f2015-08-21 17:26:38 +0000475.. TODO: Describe the syntax for the bundled instructions.
Alex Lorenzb981d372015-08-21 21:17:01 +0000476.. TODO: Describe the syntax for virtual register YAML definitions.
Alex Lorenz3d311772015-08-06 22:55:19 +0000477.. TODO: Describe the machine function's YAML flag attributes.
Alex Lorenzd4990eb2015-09-08 11:38:16 +0000478.. TODO: Describe the syntax for the external symbol and register
Alex Lorenz3d311772015-08-06 22:55:19 +0000479 mask machine operands.
480.. TODO: Describe the frame information YAML mapping.
481.. TODO: Describe the syntax of the stack object machine operands and their
482 YAML definitions.
483.. TODO: Describe the syntax of the constant pool machine operands and their
484 YAML definitions.
485.. TODO: Describe the syntax of the jump table machine operands and their
486 YAML definitions.
487.. TODO: Describe the syntax of the block address machine operands.
488.. TODO: Describe the syntax of the CFI index machine operands.
489.. TODO: Describe the syntax of the metadata machine operands, and the
490 instructions debug location attribute.
491.. TODO: Describe the syntax of the target index machine operands.
492.. TODO: Describe the syntax of the register live out machine operands.
493.. TODO: Describe the syntax of the machine memory operands.