Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 1 | ======================================== |
| 2 | Machine IR (MIR) Format Reference Manual |
| 3 | ======================================== |
| 4 | |
| 5 | .. contents:: |
| 6 | :local: |
| 7 | |
| 8 | .. warning:: |
| 9 | This is a work in progress. |
| 10 | |
| 11 | Introduction |
| 12 | ============ |
| 13 | |
| 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>`. |
| 18 | |
| 19 | The MIR serialization format is designed to be used for testing the code |
| 20 | generation passes in LLVM. |
| 21 | |
| 22 | Overview |
| 23 | ======== |
| 24 | |
| 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 |
| 27 | `yaml.org |
| 28 | <http://www.yaml.org/spec/1.2/spec.html#Introduction>`_. |
| 29 | |
| 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. |
| 33 | |
| 34 | .. _YAML documents: http://www.yaml.org/spec/1.2/spec.html#id2800132 |
| 35 | |
Alex Lorenz | ea788c4 | 2015-08-21 22:58:33 +0000 | [diff] [blame] | 36 | MIR Testing Guide |
| 37 | ================= |
| 38 | |
| 39 | You 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 |
Matthias Braun | e6185b7 | 2017-04-13 22:14:45 +0000 | [diff] [blame] | 42 | ``-run-pass`` option in llc. |
Alex Lorenz | ea788c4 | 2015-08-21 22:58:33 +0000 | [diff] [blame] | 43 | |
Matthias Braun | e6185b7 | 2017-04-13 22:14:45 +0000 | [diff] [blame] | 44 | - You can use llc's ``-stop-after`` option with existing or new LLVM assembly |
Alex Lorenz | ea788c4 | 2015-08-21 22:58:33 +0000 | [diff] [blame] | 45 | tests and check the MIR output of a specific code generation pass. |
| 46 | |
| 47 | Testing Individual Code Generation Passes |
| 48 | ----------------------------------------- |
| 49 | |
Matthias Braun | e6185b7 | 2017-04-13 22:14:45 +0000 | [diff] [blame] | 50 | The ``-run-pass`` option in llc allows you to create MIR tests that invoke just |
| 51 | a single code generation pass. When this option is used, llc will parse an |
| 52 | input MIR file, run the specified code generation pass(es), and output the |
| 53 | resulting MIR code. |
Alex Lorenz | ea788c4 | 2015-08-21 22:58:33 +0000 | [diff] [blame] | 54 | |
Matthias Braun | e6185b7 | 2017-04-13 22:14:45 +0000 | [diff] [blame] | 55 | You can generate an input MIR file for the test by using the ``-stop-after`` or |
| 56 | ``-stop-before`` option in llc. For example, if you would like to write a test |
| 57 | for the post register allocation pseudo instruction expansion pass, you can |
| 58 | specify the machine copy propagation pass in the ``-stop-after`` option, as it |
| 59 | runs just before the pass that we are trying to test: |
Alex Lorenz | ea788c4 | 2015-08-21 22:58:33 +0000 | [diff] [blame] | 60 | |
Matthias Braun | e6185b7 | 2017-04-13 22:14:45 +0000 | [diff] [blame] | 61 | ``llc -stop-after=machine-cp bug-trigger.ll > test.mir`` |
Alex Lorenz | ea788c4 | 2015-08-21 22:58:33 +0000 | [diff] [blame] | 62 | |
| 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 |
| 66 | below can be used: |
| 67 | |
Matthias Braun | e6185b7 | 2017-04-13 22:14:45 +0000 | [diff] [blame] | 68 | ``# RUN: llc -o - %s -mtriple=x86_64-- -run-pass=postrapseudos | FileCheck %s`` |
Alex Lorenz | ea788c4 | 2015-08-21 22:58:33 +0000 | [diff] [blame] | 69 | |
| 70 | The MIR files are target dependent, so they have to be placed in the target |
Matthias Braun | e6185b7 | 2017-04-13 22:14:45 +0000 | [diff] [blame] | 71 | specific test directories (``lib/CodeGen/TARGETNAME``). They also need to |
| 72 | specify a target triple or a target architecture either in the run line or in |
| 73 | the embedded LLVM IR module. |
Alex Lorenz | ea788c4 | 2015-08-21 22:58:33 +0000 | [diff] [blame] | 74 | |
Matthias Braun | 836c383 | 2017-04-13 23:45:14 +0000 | [diff] [blame] | 75 | Simplifying MIR files |
| 76 | ^^^^^^^^^^^^^^^^^^^^^ |
| 77 | |
| 78 | The MIR code coming out of ``-stop-after``/``-stop-before`` is very verbose; |
| 79 | Tests are more accessible and future proof when simplified: |
| 80 | |
Matthias Braun | 8940114 | 2017-05-05 21:09:30 +0000 | [diff] [blame] | 81 | - Use the ``-simplify-mir`` option with llc. |
| 82 | |
Matthias Braun | 836c383 | 2017-04-13 23:45:14 +0000 | [diff] [blame] | 83 | - Machine function attributes often have default values or the test works just |
| 84 | as well with default values. Typical candidates for this are: `alignment:`, |
| 85 | `exposesReturnsTwice`, `legalized`, `regBankSelected`, `selected`. |
| 86 | The whole `frameInfo` section is often unnecessary if there is no special |
| 87 | frame usage in the function. `tracksRegLiveness` on the other hand is often |
| 88 | necessary for some passes that care about block livein lists. |
| 89 | |
| 90 | - The (global) `liveins:` list is typically only interesting for early |
| 91 | instruction selection passes and can be removed when testing later passes. |
| 92 | The per-block `liveins:` on the other hand are necessary if |
| 93 | `tracksRegLiveness` is true. |
| 94 | |
| 95 | - Branch probability data in block `successors:` lists can be dropped if the |
| 96 | test doesn't depend on it. Example: |
| 97 | `successors: %bb.1(0x40000000), %bb.2(0x40000000)` can be replaced with |
| 98 | `successors: %bb.1, %bb.2`. |
| 99 | |
| 100 | - MIR code contains a whole IR module. This is necessary because there are |
| 101 | no equivalents in MIR for global variables, references to external functions, |
| 102 | function attributes, metadata, debug info. Instead some MIR data references |
| 103 | the IR constructs. You can often remove them if the test doesn't depend on |
| 104 | them. |
| 105 | |
| 106 | - Alias Analysis is performed on IR values. These are referenced by memory |
| 107 | operands in MIR. Example: `:: (load 8 from %ir.foobar, !alias.scope !9)`. |
| 108 | If the test doesn't depend on (good) alias analysis the references can be |
| 109 | dropped: `:: (load 8)` |
| 110 | |
| 111 | - MIR blocks can reference IR blocks for debug printing, profile information |
| 112 | or debug locations. Example: `bb.42.myblock` in MIR references the IR block |
| 113 | `myblock`. It is usually possible to drop the `.myblock` reference and simply |
| 114 | use `bb.42`. |
| 115 | |
| 116 | - If there are no memory operands or blocks referencing the IR then the |
| 117 | IR function can be replaced by a parameterless dummy function like |
| 118 | `define @func() { ret void }`. |
| 119 | |
| 120 | - It is possible to drop the whole IR section of the MIR file if it only |
| 121 | contains dummy functions (see above). The .mir loader will create the |
| 122 | IR functions automatically in this case. |
| 123 | |
Francis Visoiu Mistrih | 3c99371 | 2017-12-14 10:03:23 +0000 | [diff] [blame] | 124 | .. _limitations: |
| 125 | |
Alex Lorenz | ea788c4 | 2015-08-21 22:58:33 +0000 | [diff] [blame] | 126 | Limitations |
| 127 | ----------- |
| 128 | |
| 129 | Currently the MIR format has several limitations in terms of which state it |
| 130 | can serialize: |
| 131 | |
| 132 | - The target-specific state in the target-specific ``MachineFunctionInfo`` |
| 133 | subclasses isn't serialized at the moment. |
| 134 | |
| 135 | - The target-specific ``MachineConstantPoolValue`` subclasses (in the ARM and |
| 136 | SystemZ backends) aren't serialized at the moment. |
| 137 | |
| 138 | - The ``MCSymbol`` machine operands are only printed, they can't be parsed. |
| 139 | |
| 140 | - A lot of the state in ``MachineModuleInfo`` isn't serialized - only the CFI |
| 141 | instructions and the variable debug information from MMI is serialized right |
| 142 | now. |
| 143 | |
| 144 | These limitations impose restrictions on what you can test with the MIR format. |
| 145 | For now, tests that would like to test some behaviour that depends on the state |
| 146 | of certain ``MCSymbol`` operands or the exception handling state in MMI, can't |
| 147 | use the MIR format. As well as that, tests that test some behaviour that |
| 148 | depends on the state of the target specific ``MachineFunctionInfo`` or |
| 149 | ``MachineConstantPoolValue`` subclasses can't use the MIR format at the moment. |
| 150 | |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 151 | High Level Structure |
| 152 | ==================== |
| 153 | |
Alex Lorenz | d4990eb | 2015-09-08 11:38:16 +0000 | [diff] [blame] | 154 | .. _embedded-module: |
| 155 | |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 156 | Embedded Module |
| 157 | --------------- |
| 158 | |
| 159 | When the first YAML document contains a `YAML block literal string`_, the MIR |
| 160 | parser will treat this string as an LLVM assembly language string that |
| 161 | represents an embedded LLVM IR module. |
| 162 | Here is an example of a YAML document that contains an LLVM module: |
| 163 | |
| 164 | .. code-block:: llvm |
| 165 | |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 166 | define i32 @inc(i32* %x) { |
| 167 | entry: |
| 168 | %0 = load i32, i32* %x |
| 169 | %1 = add i32 %0, 1 |
| 170 | store i32 %1, i32* %x |
| 171 | ret i32 %1 |
| 172 | } |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 173 | |
| 174 | .. _YAML block literal string: http://www.yaml.org/spec/1.2/spec.html#id2795688 |
| 175 | |
| 176 | Machine Functions |
| 177 | ----------------- |
| 178 | |
| 179 | The remaining YAML documents contain the machine functions. This is an example |
| 180 | of such YAML document: |
| 181 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 182 | .. code-block:: text |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 183 | |
| 184 | --- |
| 185 | name: inc |
| 186 | tracksRegLiveness: true |
| 187 | liveins: |
| 188 | - { reg: '%rdi' } |
Alex Lorenz | 9846167 | 2015-08-14 00:36:10 +0000 | [diff] [blame] | 189 | body: | |
| 190 | bb.0.entry: |
| 191 | liveins: %rdi |
| 192 | |
| 193 | %eax = MOV32rm %rdi, 1, _, 0, _ |
| 194 | %eax = INC32r killed %eax, implicit-def dead %eflags |
| 195 | MOV32mr killed %rdi, 1, _, 0, _, %eax |
| 196 | RETQ %eax |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 197 | ... |
| 198 | |
| 199 | The document above consists of attributes that represent the various |
| 200 | properties and data structures in a machine function. |
| 201 | |
| 202 | The attribute ``name`` is required, and its value should be identical to the |
| 203 | name of a function that this machine function is based on. |
| 204 | |
Alex Lorenz | 9846167 | 2015-08-14 00:36:10 +0000 | [diff] [blame] | 205 | The attribute ``body`` is a `YAML block literal string`_. Its value represents |
| 206 | the function's machine basic blocks and their machine instructions. |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 207 | |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 208 | Machine Instructions Format Reference |
| 209 | ===================================== |
| 210 | |
| 211 | The machine basic blocks and their instructions are represented using a custom, |
| 212 | human readable serialization language. This language is used in the |
| 213 | `YAML block literal string`_ that corresponds to the machine function's body. |
| 214 | |
| 215 | A source string that uses this language contains a list of machine basic |
| 216 | blocks, which are described in the section below. |
| 217 | |
| 218 | Machine Basic Blocks |
| 219 | -------------------- |
| 220 | |
| 221 | A machine basic block is defined in a single block definition source construct |
| 222 | that contains the block's ID. |
| 223 | The example below defines two blocks that have an ID of zero and one: |
| 224 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 225 | .. code-block:: text |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 226 | |
| 227 | bb.0: |
| 228 | <instructions> |
| 229 | bb.1: |
| 230 | <instructions> |
| 231 | |
| 232 | A machine basic block can also have a name. It should be specified after the ID |
| 233 | in the block's definition: |
| 234 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 235 | .. code-block:: text |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 236 | |
| 237 | bb.0.entry: ; This block's name is "entry" |
| 238 | <instructions> |
| 239 | |
| 240 | The block's name should be identical to the name of the IR block that this |
| 241 | machine block is based on. |
| 242 | |
Francis Visoiu Mistrih | b41dbbe | 2017-12-13 10:30:59 +0000 | [diff] [blame] | 243 | .. _block-references: |
| 244 | |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 245 | Block References |
| 246 | ^^^^^^^^^^^^^^^^ |
| 247 | |
| 248 | The machine basic blocks are identified by their ID numbers. Individual |
| 249 | blocks are referenced using the following syntax: |
| 250 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 251 | .. code-block:: text |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 252 | |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 253 | %bb.<id> |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 254 | |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 255 | Example: |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 256 | |
| 257 | .. code-block:: llvm |
| 258 | |
| 259 | %bb.0 |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 260 | |
| 261 | The following syntax is also supported, but the former syntax is preferred for |
| 262 | block references: |
| 263 | |
| 264 | .. code-block:: text |
| 265 | |
| 266 | %bb.<id>[.<name>] |
| 267 | |
| 268 | Example: |
| 269 | |
| 270 | .. code-block:: llvm |
| 271 | |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 272 | %bb.1.then |
| 273 | |
| 274 | Successors |
| 275 | ^^^^^^^^^^ |
| 276 | |
| 277 | The machine basic block's successors have to be specified before any of the |
| 278 | instructions: |
| 279 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 280 | .. code-block:: text |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 281 | |
| 282 | bb.0.entry: |
| 283 | successors: %bb.1.then, %bb.2.else |
| 284 | <instructions> |
| 285 | bb.1.then: |
| 286 | <instructions> |
| 287 | bb.2.else: |
| 288 | <instructions> |
| 289 | |
| 290 | The branch weights can be specified in brackets after the successor blocks. |
| 291 | The example below defines a block that has two successors with branch weights |
| 292 | of 32 and 16: |
| 293 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 294 | .. code-block:: text |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 295 | |
| 296 | bb.0.entry: |
| 297 | successors: %bb.1.then(32), %bb.2.else(16) |
| 298 | |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 299 | .. _bb-liveins: |
| 300 | |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 301 | Live In Registers |
| 302 | ^^^^^^^^^^^^^^^^^ |
| 303 | |
| 304 | The machine basic block's live in registers have to be specified before any of |
| 305 | the instructions: |
| 306 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 307 | .. code-block:: text |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 308 | |
| 309 | bb.0.entry: |
| 310 | liveins: %edi, %esi |
| 311 | |
| 312 | The list of live in registers and successors can be empty. The language also |
| 313 | allows multiple live in register and successor lists - they are combined into |
| 314 | one list by the parser. |
| 315 | |
| 316 | Miscellaneous Attributes |
| 317 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 318 | |
| 319 | The attributes ``IsAddressTaken``, ``IsLandingPad`` and ``Alignment`` can be |
| 320 | specified in brackets after the block's definition: |
| 321 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 322 | .. code-block:: text |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 323 | |
| 324 | bb.0.entry (address-taken): |
| 325 | <instructions> |
| 326 | bb.2.else (align 4): |
| 327 | <instructions> |
| 328 | bb.3(landing-pad, align 4): |
| 329 | <instructions> |
| 330 | |
| 331 | .. TODO: Describe the way the reference to an unnamed LLVM IR block can be |
| 332 | preserved. |
| 333 | |
Alex Lorenz | 8eadc3f | 2015-08-21 17:26:38 +0000 | [diff] [blame] | 334 | Machine Instructions |
| 335 | -------------------- |
| 336 | |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 337 | A machine instruction is composed of a name, |
| 338 | :ref:`machine operands <machine-operands>`, |
Alex Lorenz | 8eadc3f | 2015-08-21 17:26:38 +0000 | [diff] [blame] | 339 | :ref:`instruction flags <instruction-flags>`, and machine memory operands. |
| 340 | |
| 341 | The instruction's name is usually specified before the operands. The example |
| 342 | below shows an instance of the X86 ``RETQ`` instruction with a single machine |
| 343 | operand: |
| 344 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 345 | .. code-block:: text |
Alex Lorenz | 8eadc3f | 2015-08-21 17:26:38 +0000 | [diff] [blame] | 346 | |
| 347 | RETQ %eax |
| 348 | |
| 349 | However, if the machine instruction has one or more explicitly defined register |
| 350 | operands, the instruction's name has to be specified after them. The example |
| 351 | below shows an instance of the AArch64 ``LDPXpost`` instruction with three |
| 352 | defined register operands: |
| 353 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 354 | .. code-block:: text |
Alex Lorenz | 8eadc3f | 2015-08-21 17:26:38 +0000 | [diff] [blame] | 355 | |
| 356 | %sp, %fp, %lr = LDPXpost %sp, 2 |
| 357 | |
| 358 | The instruction names are serialized using the exact definitions from the |
| 359 | target's ``*InstrInfo.td`` files, and they are case sensitive. This means that |
| 360 | similar instruction names like ``TSTri`` and ``tSTRi`` represent different |
| 361 | machine instructions. |
| 362 | |
| 363 | .. _instruction-flags: |
| 364 | |
| 365 | Instruction Flags |
| 366 | ^^^^^^^^^^^^^^^^^ |
| 367 | |
Francis Visoiu Mistrih | dbf2c48 | 2018-01-09 11:33:22 +0000 | [diff] [blame] | 368 | The flag ``frame-setup`` or ``frame-destroy`` can be specified before the |
| 369 | instruction's name: |
Alex Lorenz | 8eadc3f | 2015-08-21 17:26:38 +0000 | [diff] [blame] | 370 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 371 | .. code-block:: text |
Alex Lorenz | 8eadc3f | 2015-08-21 17:26:38 +0000 | [diff] [blame] | 372 | |
| 373 | %fp = frame-setup ADDXri %sp, 0, 0 |
| 374 | |
Francis Visoiu Mistrih | dbf2c48 | 2018-01-09 11:33:22 +0000 | [diff] [blame] | 375 | .. code-block:: text |
| 376 | |
| 377 | %x21, %x20 = frame-destroy LDPXi %sp |
| 378 | |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 379 | .. _registers: |
| 380 | |
Francis Visoiu Mistrih | 5836790 | 2018-01-10 17:53:16 +0000 | [diff] [blame] | 381 | Bundled Instructions |
| 382 | ^^^^^^^^^^^^^^^^^^^^ |
| 383 | |
| 384 | The syntax for bundled instructions is the following: |
| 385 | |
| 386 | .. code-block:: text |
| 387 | |
| 388 | BUNDLE implicit-def %r0, implicit-def %r1, implicit %r2 { |
| 389 | %r0 = SOME_OP %r2 |
| 390 | %r1 = ANOTHER_OP internal %r0 |
| 391 | } |
| 392 | |
| 393 | The first instruction is often a bundle header. The instructions between ``{`` |
| 394 | and ``}`` are bundled with the first instruction. |
| 395 | |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 396 | Registers |
| 397 | --------- |
| 398 | |
| 399 | Registers are one of the key primitives in the machine instructions |
| 400 | serialization language. They are primarly used in the |
| 401 | :ref:`register machine operands <register-operands>`, |
| 402 | but they can also be used in a number of other places, like the |
| 403 | :ref:`basic block's live in list <bb-liveins>`. |
| 404 | |
| 405 | The physical registers are identified by their name. They use the following |
| 406 | syntax: |
| 407 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 408 | .. code-block:: text |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 409 | |
| 410 | %<name> |
| 411 | |
| 412 | The example below shows three X86 physical registers: |
| 413 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 414 | .. code-block:: text |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 415 | |
| 416 | %eax |
| 417 | %r15 |
| 418 | %eflags |
| 419 | |
| 420 | The virtual registers are identified by their ID number. They use the following |
| 421 | syntax: |
| 422 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 423 | .. code-block:: text |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 424 | |
| 425 | %<id> |
| 426 | |
| 427 | Example: |
| 428 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 429 | .. code-block:: text |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 430 | |
| 431 | %0 |
| 432 | |
| 433 | The null registers are represented using an underscore ('``_``'). They can also be |
| 434 | represented using a '``%noreg``' named register, although the former syntax |
| 435 | is preferred. |
| 436 | |
| 437 | .. _machine-operands: |
| 438 | |
| 439 | Machine Operands |
| 440 | ---------------- |
| 441 | |
| 442 | There are seventeen different kinds of machine operands, and all of them, except |
| 443 | the ``MCSymbol`` operand, can be serialized. The ``MCSymbol`` operands are |
| 444 | just printed out - they can't be parsed back yet. |
| 445 | |
| 446 | Immediate Operands |
| 447 | ^^^^^^^^^^^^^^^^^^ |
| 448 | |
| 449 | The immediate machine operands are untyped, 64-bit signed integers. The |
| 450 | example below shows an instance of the X86 ``MOV32ri`` instruction that has an |
| 451 | immediate machine operand ``-42``: |
| 452 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 453 | .. code-block:: text |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 454 | |
| 455 | %eax = MOV32ri -42 |
| 456 | |
Francis Visoiu Mistrih | 440f69c | 2017-12-08 22:53:21 +0000 | [diff] [blame] | 457 | An immediate operand is also used to represent a subregister index when the |
| 458 | machine instruction has one of the following opcodes: |
| 459 | |
| 460 | - ``EXTRACT_SUBREG`` |
| 461 | |
| 462 | - ``INSERT_SUBREG`` |
| 463 | |
| 464 | - ``REG_SEQUENCE`` |
| 465 | |
| 466 | - ``SUBREG_TO_REG`` |
| 467 | |
| 468 | In case this is true, the Machine Operand is printed according to the target. |
| 469 | |
| 470 | For example: |
| 471 | |
| 472 | In AArch64RegisterInfo.td: |
| 473 | |
| 474 | .. code-block:: text |
| 475 | |
| 476 | def sub_32 : SubRegIndex<32>; |
| 477 | |
| 478 | If the third operand is an immediate with the value ``15`` (target-dependent |
| 479 | value), based on the instruction's opcode and the operand's index the operand |
| 480 | will be printed as ``%subreg.sub_32``: |
| 481 | |
| 482 | .. code-block:: text |
| 483 | |
| 484 | %1:gpr64 = SUBREG_TO_REG 0, %0, %subreg.sub_32 |
| 485 | |
Francis Visoiu Mistrih | 6c4ca71 | 2017-12-08 11:40:06 +0000 | [diff] [blame] | 486 | For integers > 64bit, we use a special machine operand, ``MO_CImmediate``, |
| 487 | which stores the immediate in a ``ConstantInt`` using an ``APInt`` (LLVM's |
| 488 | arbitrary precision integers). |
| 489 | |
| 490 | .. TODO: Describe the FPIMM immediate operands. |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 491 | |
| 492 | .. _register-operands: |
| 493 | |
| 494 | Register Operands |
| 495 | ^^^^^^^^^^^^^^^^^ |
| 496 | |
| 497 | The :ref:`register <registers>` primitive is used to represent the register |
| 498 | machine operands. The register operands can also have optional |
| 499 | :ref:`register flags <register-flags>`, |
Alex Lorenz | 37e0262 | 2015-09-08 11:39:47 +0000 | [diff] [blame] | 500 | :ref:`a subregister index <subregister-indices>`, |
| 501 | and a reference to the tied register operand. |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 502 | The full syntax of a register operand is shown below: |
| 503 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 504 | .. code-block:: text |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 505 | |
| 506 | [<flags>] <register> [ :<subregister-idx-name> ] [ (tied-def <tied-op>) ] |
| 507 | |
| 508 | This example shows an instance of the X86 ``XOR32rr`` instruction that has |
| 509 | 5 register operands with different register flags: |
| 510 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 511 | .. code-block:: text |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 512 | |
| 513 | dead %eax = XOR32rr undef %eax, undef %eax, implicit-def dead %eflags, implicit-def %al |
| 514 | |
| 515 | .. _register-flags: |
| 516 | |
| 517 | Register Flags |
| 518 | ~~~~~~~~~~~~~~ |
| 519 | |
| 520 | The table below shows all of the possible register flags along with the |
| 521 | corresponding internal ``llvm::RegState`` representation: |
| 522 | |
| 523 | .. list-table:: |
| 524 | :header-rows: 1 |
| 525 | |
| 526 | * - Flag |
| 527 | - Internal Value |
| 528 | |
| 529 | * - ``implicit`` |
| 530 | - ``RegState::Implicit`` |
| 531 | |
| 532 | * - ``implicit-def`` |
| 533 | - ``RegState::ImplicitDefine`` |
| 534 | |
| 535 | * - ``def`` |
| 536 | - ``RegState::Define`` |
| 537 | |
| 538 | * - ``dead`` |
| 539 | - ``RegState::Dead`` |
| 540 | |
| 541 | * - ``killed`` |
| 542 | - ``RegState::Kill`` |
| 543 | |
| 544 | * - ``undef`` |
| 545 | - ``RegState::Undef`` |
| 546 | |
| 547 | * - ``internal`` |
| 548 | - ``RegState::InternalRead`` |
| 549 | |
| 550 | * - ``early-clobber`` |
| 551 | - ``RegState::EarlyClobber`` |
| 552 | |
| 553 | * - ``debug-use`` |
| 554 | - ``RegState::Debug`` |
Alex Lorenz | 3a4a60c | 2015-08-15 01:06:06 +0000 | [diff] [blame] | 555 | |
Geoff Berry | 60c4310 | 2017-12-12 17:53:59 +0000 | [diff] [blame] | 556 | * - ``renamable`` |
| 557 | - ``RegState::Renamable`` |
| 558 | |
Alex Lorenz | 37e0262 | 2015-09-08 11:39:47 +0000 | [diff] [blame] | 559 | .. _subregister-indices: |
| 560 | |
| 561 | Subregister Indices |
| 562 | ~~~~~~~~~~~~~~~~~~~ |
| 563 | |
| 564 | The register machine operands can reference a portion of a register by using |
| 565 | the subregister indices. The example below shows an instance of the ``COPY`` |
| 566 | pseudo instruction that uses the X86 ``sub_8bit`` subregister index to copy 8 |
| 567 | lower bits from the 32-bit virtual register 0 to the 8-bit virtual register 1: |
| 568 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 569 | .. code-block:: text |
Alex Lorenz | 37e0262 | 2015-09-08 11:39:47 +0000 | [diff] [blame] | 570 | |
| 571 | %1 = COPY %0:sub_8bit |
| 572 | |
| 573 | The names of the subregister indices are target specific, and are typically |
| 574 | defined in the target's ``*RegisterInfo.td`` file. |
| 575 | |
Francis Visoiu Mistrih | 26ae8a6 | 2017-12-13 10:30:45 +0000 | [diff] [blame] | 576 | Constant Pool Indices |
| 577 | ^^^^^^^^^^^^^^^^^^^^^ |
| 578 | |
| 579 | A constant pool index (CPI) operand is printed using its index in the |
| 580 | function's ``MachineConstantPool`` and an offset. |
| 581 | |
| 582 | For example, a CPI with the index 1 and offset 8: |
| 583 | |
| 584 | .. code-block:: text |
| 585 | |
| 586 | %1:gr64 = MOV64ri %const.1 + 8 |
| 587 | |
| 588 | For a CPI with the index 0 and offset -12: |
| 589 | |
| 590 | .. code-block:: text |
| 591 | |
| 592 | %1:gr64 = MOV64ri %const.0 - 12 |
| 593 | |
| 594 | A constant pool entry is bound to a LLVM IR ``Constant`` or a target-specific |
| 595 | ``MachineConstantPoolValue``. When serializing all the function's constants the |
| 596 | following format is used: |
| 597 | |
| 598 | .. code-block:: text |
| 599 | |
| 600 | constants: |
| 601 | - id: <index> |
| 602 | value: <value> |
| 603 | alignment: <alignment> |
| 604 | isTargetSpecific: <target-specific> |
| 605 | |
| 606 | where ``<index>`` is a 32-bit unsigned integer, ``<value>`` is a `LLVM IR Constant |
| 607 | <https://www.llvm.org/docs/LangRef.html#constants>`_, alignment is a 32-bit |
| 608 | unsigned integer, and ``<target-specific>`` is either true or false. |
| 609 | |
| 610 | Example: |
| 611 | |
| 612 | .. code-block:: text |
| 613 | |
| 614 | constants: |
| 615 | - id: 0 |
| 616 | value: 'double 3.250000e+00' |
| 617 | alignment: 8 |
| 618 | - id: 1 |
| 619 | value: 'g-(LPC0+8)' |
| 620 | alignment: 4 |
| 621 | isTargetSpecific: true |
| 622 | |
Alex Lorenz | d4990eb | 2015-09-08 11:38:16 +0000 | [diff] [blame] | 623 | Global Value Operands |
| 624 | ^^^^^^^^^^^^^^^^^^^^^ |
| 625 | |
| 626 | The global value machine operands reference the global values from the |
| 627 | :ref:`embedded LLVM IR module <embedded-module>`. |
| 628 | The example below shows an instance of the X86 ``MOV64rm`` instruction that has |
| 629 | a global value operand named ``G``: |
| 630 | |
Renato Golin | 124f259 | 2016-07-20 12:16:38 +0000 | [diff] [blame] | 631 | .. code-block:: text |
Alex Lorenz | d4990eb | 2015-09-08 11:38:16 +0000 | [diff] [blame] | 632 | |
| 633 | %rax = MOV64rm %rip, 1, _, @G, _ |
| 634 | |
| 635 | The named global values are represented using an identifier with the '@' prefix. |
| 636 | If the identifier doesn't match the regular expression |
| 637 | `[-a-zA-Z$._][-a-zA-Z$._0-9]*`, then this identifier must be quoted. |
| 638 | |
| 639 | The unnamed global values are represented using an unsigned numeric value with |
| 640 | the '@' prefix, like in the following examples: ``@0``, ``@989``. |
| 641 | |
Francis Visoiu Mistrih | b3a0d51 | 2017-12-13 10:30:51 +0000 | [diff] [blame] | 642 | Target-dependent Index Operands |
| 643 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 644 | |
| 645 | A target index operand is a target-specific index and an offset. The |
| 646 | target-specific index is printed using target-specific names and a positive or |
| 647 | negative offset. |
| 648 | |
| 649 | For example, the ``amdgpu-constdata-start`` is associated with the index ``0`` |
| 650 | in the AMDGPU backend. So if we have a target index operand with the index 0 |
| 651 | and the offset 8: |
| 652 | |
| 653 | .. code-block:: text |
| 654 | |
| 655 | %sgpr2 = S_ADD_U32 _, target-index(amdgpu-constdata-start) + 8, implicit-def _, implicit-def _ |
| 656 | |
Francis Visoiu Mistrih | b41dbbe | 2017-12-13 10:30:59 +0000 | [diff] [blame] | 657 | Jump-table Index Operands |
| 658 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 659 | |
| 660 | A jump-table index operand with the index 0 is printed as following: |
| 661 | |
| 662 | .. code-block:: text |
| 663 | |
| 664 | tBR_JTr killed %r0, %jump-table.0 |
| 665 | |
| 666 | A machine jump-table entry contains a list of ``MachineBasicBlocks``. When serializing all the function's jump-table entries, the following format is used: |
| 667 | |
| 668 | .. code-block:: text |
| 669 | |
| 670 | jumpTable: |
| 671 | kind: <kind> |
| 672 | entries: |
| 673 | - id: <index> |
| 674 | blocks: [ <bbreference>, <bbreference>, ... ] |
| 675 | |
| 676 | where ``<kind>`` is describing how the jump table is represented and emitted (plain address, relocations, PIC, etc.), and each ``<index>`` is a 32-bit unsigned integer and ``blocks`` contains a list of :ref:`machine basic block references <block-references>`. |
| 677 | |
| 678 | Example: |
| 679 | |
| 680 | .. code-block:: text |
| 681 | |
| 682 | jumpTable: |
| 683 | kind: inline |
| 684 | entries: |
| 685 | - id: 0 |
| 686 | blocks: [ '%bb.3', '%bb.9', '%bb.4.d3' ] |
| 687 | - id: 1 |
| 688 | blocks: [ '%bb.7', '%bb.7', '%bb.4.d3', '%bb.5' ] |
| 689 | |
Francis Visoiu Mistrih | e76c5fc | 2017-12-14 10:02:58 +0000 | [diff] [blame] | 690 | External Symbol Operands |
| 691 | ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 692 | |
| 693 | An external symbol operand is represented using an identifier with the ``$`` |
| 694 | prefix. The identifier is surrounded with ""'s and escaped if it has any |
| 695 | special non-printable characters in it. |
| 696 | |
| 697 | Example: |
| 698 | |
| 699 | .. code-block:: text |
| 700 | |
| 701 | CALL64pcrel32 $__stack_chk_fail, csr_64, implicit %rsp, implicit-def %rsp |
| 702 | |
Francis Visoiu Mistrih | 3c99371 | 2017-12-14 10:03:23 +0000 | [diff] [blame] | 703 | MCSymbol Operands |
| 704 | ^^^^^^^^^^^^^^^^^ |
| 705 | |
| 706 | A MCSymbol operand is holding a pointer to a ``MCSymbol``. For the limitations |
| 707 | of this operand in MIR, see :ref:`limitations <limitations>`. |
| 708 | |
| 709 | The syntax is: |
| 710 | |
| 711 | .. code-block:: text |
| 712 | |
| 713 | EH_LABEL <mcsymbol Ltmp1> |
Francis Visoiu Mistrih | e76c5fc | 2017-12-14 10:02:58 +0000 | [diff] [blame] | 714 | |
Francis Visoiu Mistrih | 874ae6f | 2017-12-19 16:51:52 +0000 | [diff] [blame] | 715 | CFIIndex Operands |
| 716 | ^^^^^^^^^^^^^^^^^ |
| 717 | |
| 718 | A CFI Index operand is holding an index into a per-function side-table, |
| 719 | ``MachineFunction::getFrameInstructions()``, which references all the frame |
| 720 | instructions in a ``MachineFunction``. A ``CFI_INSTRUCTION`` may look like it |
| 721 | contains multiple operands, but the only operand it contains is the CFI Index. |
| 722 | The other operands are tracked by the ``MCCFIInstruction`` object. |
| 723 | |
| 724 | The syntax is: |
| 725 | |
| 726 | .. code-block:: text |
| 727 | |
| 728 | CFI_INSTRUCTION offset %w30, -16 |
| 729 | |
| 730 | which may be emitted later in the MC layer as: |
| 731 | |
| 732 | .. code-block:: text |
| 733 | |
| 734 | .cfi_offset w30, -16 |
| 735 | |
Francis Visoiu Mistrih | bbd610a | 2017-12-19 21:47:05 +0000 | [diff] [blame] | 736 | IntrinsicID Operands |
| 737 | ^^^^^^^^^^^^^^^^^^^^ |
| 738 | |
| 739 | An Intrinsic ID operand contains a generic intrinsic ID or a target-specific ID. |
| 740 | |
| 741 | The syntax for the ``returnaddress`` intrinsic is: |
| 742 | |
| 743 | .. code-block:: text |
| 744 | |
| 745 | %x0 = COPY intrinsic(@llvm.returnaddress) |
| 746 | |
Francis Visoiu Mistrih | cb2683d | 2017-12-19 21:47:10 +0000 | [diff] [blame] | 747 | Predicate Operands |
| 748 | ^^^^^^^^^^^^^^^^^^ |
| 749 | |
| 750 | A Predicate operand contains an IR predicate from ``CmpInst::Predicate``, like |
| 751 | ``ICMP_EQ``, etc. |
| 752 | |
| 753 | For an int eq predicate ``ICMP_EQ``, the syntax is: |
| 754 | |
| 755 | .. code-block:: text |
| 756 | |
| 757 | %2:gpr(s32) = G_ICMP intpred(eq), %0, %1 |
| 758 | |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 759 | .. TODO: Describe the parsers default behaviour when optional YAML attributes |
| 760 | are missing. |
Alex Lorenz | b981d37 | 2015-08-21 21:17:01 +0000 | [diff] [blame] | 761 | .. TODO: Describe the syntax for virtual register YAML definitions. |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 762 | .. TODO: Describe the machine function's YAML flag attributes. |
Francis Visoiu Mistrih | e76c5fc | 2017-12-14 10:02:58 +0000 | [diff] [blame] | 763 | .. TODO: Describe the syntax for the register mask machine operands. |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 764 | .. TODO: Describe the frame information YAML mapping. |
| 765 | .. TODO: Describe the syntax of the stack object machine operands and their |
| 766 | YAML definitions. |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 767 | .. TODO: Describe the syntax of the block address machine operands. |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 768 | .. TODO: Describe the syntax of the metadata machine operands, and the |
| 769 | instructions debug location attribute. |
Alex Lorenz | 3d31177 | 2015-08-06 22:55:19 +0000 | [diff] [blame] | 770 | .. TODO: Describe the syntax of the register live out machine operands. |
| 771 | .. TODO: Describe the syntax of the machine memory operands. |