docs: Fix long standing linking antipattern.
[oota-llvm.git] / docs / BitCodeFormat.rst
1 .. role:: raw-html(raw)
2    :format: html
3
4 ========================
5 LLVM Bitcode File Format
6 ========================
7
8 .. contents::
9    :local:
10
11 Abstract
12 ========
13
14 This document describes the LLVM bitstream file format and the encoding of the
15 LLVM IR into it.
16
17 Overview
18 ========
19
20 What is commonly known as the LLVM bitcode file format (also, sometimes
21 anachronistically known as bytecode) is actually two things: a `bitstream
22 container format`_ and an `encoding of LLVM IR`_ into the container format.
23
24 The bitstream format is an abstract encoding of structured data, very similar to
25 XML in some ways.  Like XML, bitstream files contain tags, and nested
26 structures, and you can parse the file without having to understand the tags.
27 Unlike XML, the bitstream format is a binary encoding, and unlike XML it
28 provides a mechanism for the file to self-describe "abbreviations", which are
29 effectively size optimizations for the content.
30
31 LLVM IR files may be optionally embedded into a `wrapper`_ structure that makes
32 it easy to embed extra data along with LLVM IR files.
33
34 This document first describes the LLVM bitstream format, describes the wrapper
35 format, then describes the record structure used by LLVM IR files.
36
37 .. _bitstream container format:
38
39 Bitstream Format
40 ================
41
42 The bitstream format is literally a stream of bits, with a very simple
43 structure.  This structure consists of the following concepts:
44
45 * A "`magic number`_" that identifies the contents of the stream.
46
47 * Encoding `primitives`_ like variable bit-rate integers.
48
49 * `Blocks`_, which define nested content.
50
51 * `Data Records`_, which describe entities within the file.
52
53 * Abbreviations, which specify compression optimizations for the file.
54
55 Note that the :doc:`llvm-bcanalyzer <CommandGuide/llvm-bcanalyzer>` tool can be
56 used to dump and inspect arbitrary bitstreams, which is very useful for
57 understanding the encoding.
58
59 .. _magic number:
60
61 Magic Numbers
62 -------------
63
64 The first two bytes of a bitcode file are 'BC' (``0x42``, ``0x43``).  The second
65 two bytes are an application-specific magic number.  Generic bitcode tools can
66 look at only the first two bytes to verify the file is bitcode, while
67 application-specific programs will want to look at all four.
68
69 .. _primitives:
70
71 Primitives
72 ----------
73
74 A bitstream literally consists of a stream of bits, which are read in order
75 starting with the least significant bit of each byte.  The stream is made up of
76 a number of primitive values that encode a stream of unsigned integer values.
77 These integers are encoded in two ways: either as `Fixed Width Integers`_ or as
78 `Variable Width Integers`_.
79
80 .. _Fixed Width Integers:
81 .. _fixed-width value:
82
83 Fixed Width Integers
84 ^^^^^^^^^^^^^^^^^^^^
85
86 Fixed-width integer values have their low bits emitted directly to the file.
87 For example, a 3-bit integer value encodes 1 as 001.  Fixed width integers are
88 used when there are a well-known number of options for a field.  For example,
89 boolean values are usually encoded with a 1-bit wide integer.
90
91 .. _Variable Width Integers:
92 .. _Variable Width Integer:
93 .. _variable-width value:
94
95 Variable Width Integers
96 ^^^^^^^^^^^^^^^^^^^^^^^
97
98 Variable-width integer (VBR) values encode values of arbitrary size, optimizing
99 for the case where the values are small.  Given a 4-bit VBR field, any 3-bit
100 value (0 through 7) is encoded directly, with the high bit set to zero.  Values
101 larger than N-1 bits emit their bits in a series of N-1 bit chunks, where all
102 but the last set the high bit.
103
104 For example, the value 27 (0x1B) is encoded as 1011 0011 when emitted as a vbr4
105 value.  The first set of four bits indicates the value 3 (011) with a
106 continuation piece (indicated by a high bit of 1).  The next word indicates a
107 value of 24 (011 << 3) with no continuation.  The sum (3+24) yields the value
108 27.
109
110 .. _char6-encoded value:
111
112 6-bit characters
113 ^^^^^^^^^^^^^^^^
114
115 6-bit characters encode common characters into a fixed 6-bit field.  They
116 represent the following characters with the following 6-bit values:
117
118 ::
119
120   'a' .. 'z' ---  0 .. 25
121   'A' .. 'Z' --- 26 .. 51
122   '0' .. '9' --- 52 .. 61
123          '.' --- 62
124          '_' --- 63
125
126 This encoding is only suitable for encoding characters and strings that consist
127 only of the above characters.  It is completely incapable of encoding characters
128 not in the set.
129
130 Word Alignment
131 ^^^^^^^^^^^^^^
132
133 Occasionally, it is useful to emit zero bits until the bitstream is a multiple
134 of 32 bits.  This ensures that the bit position in the stream can be represented
135 as a multiple of 32-bit words.
136
137 Abbreviation IDs
138 ----------------
139
140 A bitstream is a sequential series of `Blocks`_ and `Data Records`_.  Both of
141 these start with an abbreviation ID encoded as a fixed-bitwidth field.  The
142 width is specified by the current block, as described below.  The value of the
143 abbreviation ID specifies either a builtin ID (which have special meanings,
144 defined below) or one of the abbreviation IDs defined for the current block by
145 the stream itself.
146
147 The set of builtin abbrev IDs is:
148
149 * 0 - `END_BLOCK`_ --- This abbrev ID marks the end of the current block.
150
151 * 1 - `ENTER_SUBBLOCK`_ --- This abbrev ID marks the beginning of a new
152   block.
153
154 * 2 - `DEFINE_ABBREV`_ --- This defines a new abbreviation.
155
156 * 3 - `UNABBREV_RECORD`_ --- This ID specifies the definition of an
157   unabbreviated record.
158
159 Abbreviation IDs 4 and above are defined by the stream itself, and specify an
160 `abbreviated record encoding`_.
161
162 .. _Blocks:
163
164 Blocks
165 ------
166
167 Blocks in a bitstream denote nested regions of the stream, and are identified by
168 a content-specific id number (for example, LLVM IR uses an ID of 12 to represent
169 function bodies).  Block IDs 0-7 are reserved for `standard blocks`_ whose
170 meaning is defined by Bitcode; block IDs 8 and greater are application
171 specific. Nested blocks capture the hierarchical structure of the data encoded
172 in it, and various properties are associated with blocks as the file is parsed.
173 Block definitions allow the reader to efficiently skip blocks in constant time
174 if the reader wants a summary of blocks, or if it wants to efficiently skip data
175 it does not understand.  The LLVM IR reader uses this mechanism to skip function
176 bodies, lazily reading them on demand.
177
178 When reading and encoding the stream, several properties are maintained for the
179 block.  In particular, each block maintains:
180
181 #. A current abbrev id width.  This value starts at 2 at the beginning of the
182    stream, and is set every time a block record is entered.  The block entry
183    specifies the abbrev id width for the body of the block.
184
185 #. A set of abbreviations.  Abbreviations may be defined within a block, in
186    which case they are only defined in that block (neither subblocks nor
187    enclosing blocks see the abbreviation).  Abbreviations can also be defined
188    inside a `BLOCKINFO`_ block, in which case they are defined in all blocks
189    that match the ID that the ``BLOCKINFO`` block is describing.
190
191 As sub blocks are entered, these properties are saved and the new sub-block has
192 its own set of abbreviations, and its own abbrev id width.  When a sub-block is
193 popped, the saved values are restored.
194
195 .. _ENTER_SUBBLOCK:
196
197 ENTER_SUBBLOCK Encoding
198 ^^^^^^^^^^^^^^^^^^^^^^^
199
200 :raw-html:`<tt>`
201 [ENTER_SUBBLOCK, blockid\ :sub:`vbr8`, newabbrevlen\ :sub:`vbr4`, <align32bits>, blocklen_32]
202 :raw-html:`</tt>`
203
204 The ``ENTER_SUBBLOCK`` abbreviation ID specifies the start of a new block
205 record.  The ``blockid`` value is encoded as an 8-bit VBR identifier, and
206 indicates the type of block being entered, which can be a `standard block`_ or
207 an application-specific block.  The ``newabbrevlen`` value is a 4-bit VBR, which
208 specifies the abbrev id width for the sub-block.  The ``blocklen`` value is a
209 32-bit aligned value that specifies the size of the subblock in 32-bit
210 words. This value allows the reader to skip over the entire block in one jump.
211
212 .. _END_BLOCK:
213
214 END_BLOCK Encoding
215 ^^^^^^^^^^^^^^^^^^
216
217 ``[END_BLOCK, <align32bits>]``
218
219 The ``END_BLOCK`` abbreviation ID specifies the end of the current block record.
220 Its end is aligned to 32-bits to ensure that the size of the block is an even
221 multiple of 32-bits.
222
223 .. _Data Records:
224
225 Data Records
226 ------------
227
228 Data records consist of a record code and a number of (up to) 64-bit integer
229 values.  The interpretation of the code and values is application specific and
230 may vary between different block types.  Records can be encoded either using an
231 unabbrev record, or with an abbreviation.  In the LLVM IR format, for example,
232 there is a record which encodes the target triple of a module.  The code is
233 ``MODULE_CODE_TRIPLE``, and the values of the record are the ASCII codes for the
234 characters in the string.
235
236 .. _UNABBREV_RECORD:
237
238 UNABBREV_RECORD Encoding
239 ^^^^^^^^^^^^^^^^^^^^^^^^
240
241 :raw-html:`<tt>`
242 [UNABBREV_RECORD, code\ :sub:`vbr6`, numops\ :sub:`vbr6`, op0\ :sub:`vbr6`, op1\ :sub:`vbr6`, ...]
243 :raw-html:`</tt>`
244
245 An ``UNABBREV_RECORD`` provides a default fallback encoding, which is both
246 completely general and extremely inefficient.  It can describe an arbitrary
247 record by emitting the code and operands as VBRs.
248
249 For example, emitting an LLVM IR target triple as an unabbreviated record
250 requires emitting the ``UNABBREV_RECORD`` abbrevid, a vbr6 for the
251 ``MODULE_CODE_TRIPLE`` code, a vbr6 for the length of the string, which is equal
252 to the number of operands, and a vbr6 for each character.  Because there are no
253 letters with values less than 32, each letter would need to be emitted as at
254 least a two-part VBR, which means that each letter would require at least 12
255 bits.  This is not an efficient encoding, but it is fully general.
256
257 .. _abbreviated record encoding:
258
259 Abbreviated Record Encoding
260 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
261
262 ``[<abbrevid>, fields...]``
263
264 An abbreviated record is a abbreviation id followed by a set of fields that are
265 encoded according to the `abbreviation definition`_.  This allows records to be
266 encoded significantly more densely than records encoded with the
267 `UNABBREV_RECORD`_ type, and allows the abbreviation types to be specified in
268 the stream itself, which allows the files to be completely self describing.  The
269 actual encoding of abbreviations is defined below.
270
271 The record code, which is the first field of an abbreviated record, may be
272 encoded in the abbreviation definition (as a literal operand) or supplied in the
273 abbreviated record (as a Fixed or VBR operand value).
274
275 .. _abbreviation definition:
276
277 Abbreviations
278 -------------
279
280 Abbreviations are an important form of compression for bitstreams.  The idea is
281 to specify a dense encoding for a class of records once, then use that encoding
282 to emit many records.  It takes space to emit the encoding into the file, but
283 the space is recouped (hopefully plus some) when the records that use it are
284 emitted.
285
286 Abbreviations can be determined dynamically per client, per file. Because the
287 abbreviations are stored in the bitstream itself, different streams of the same
288 format can contain different sets of abbreviations according to the needs of the
289 specific stream.  As a concrete example, LLVM IR files usually emit an
290 abbreviation for binary operators.  If a specific LLVM module contained no or
291 few binary operators, the abbreviation does not need to be emitted.
292
293 .. _DEFINE_ABBREV:
294
295 DEFINE_ABBREV Encoding
296 ^^^^^^^^^^^^^^^^^^^^^^
297
298 :raw-html:`<tt>`
299 [DEFINE_ABBREV, numabbrevops\ :sub:`vbr5`, abbrevop0, abbrevop1, ...]
300 :raw-html:`</tt>`
301
302 A ``DEFINE_ABBREV`` record adds an abbreviation to the list of currently defined
303 abbreviations in the scope of this block.  This definition only exists inside
304 this immediate block --- it is not visible in subblocks or enclosing blocks.
305 Abbreviations are implicitly assigned IDs sequentially starting from 4 (the
306 first application-defined abbreviation ID).  Any abbreviations defined in a
307 ``BLOCKINFO`` record for the particular block type receive IDs first, in order,
308 followed by any abbreviations defined within the block itself.  Abbreviated data
309 records reference this ID to indicate what abbreviation they are invoking.
310
311 An abbreviation definition consists of the ``DEFINE_ABBREV`` abbrevid followed
312 by a VBR that specifies the number of abbrev operands, then the abbrev operands
313 themselves.  Abbreviation operands come in three forms.  They all start with a
314 single bit that indicates whether the abbrev operand is a literal operand (when
315 the bit is 1) or an encoding operand (when the bit is 0).
316
317 #. Literal operands --- :raw-html:`<tt>` [1\ :sub:`1`, litvalue\
318    :sub:`vbr8`] :raw-html:`</tt>` --- Literal operands specify that the value in
319    the result is always a single specific value.  This specific value is emitted
320    as a vbr8 after the bit indicating that it is a literal operand.
321
322 #. Encoding info without data --- :raw-html:`<tt>` [0\ :sub:`1`, encoding\
323    :sub:`3`] :raw-html:`</tt>` --- Operand encodings that do not have extra data
324    are just emitted as their code.
325
326 #. Encoding info with data --- :raw-html:`<tt>` [0\ :sub:`1`, encoding\
327    :sub:`3`, value\ :sub:`vbr5`] :raw-html:`</tt>` --- Operand encodings that do
328    have extra data are emitted as their code, followed by the extra data.
329
330 The possible operand encodings are:
331
332 * Fixed (code 1): The field should be emitted as a `fixed-width value`_, whose
333   width is specified by the operand's extra data.
334
335 * VBR (code 2): The field should be emitted as a `variable-width value`_, whose
336   width is specified by the operand's extra data.
337
338 * Array (code 3): This field is an array of values.  The array operand has no
339   extra data, but expects another operand to follow it, indicating the element
340   type of the array.  When reading an array in an abbreviated record, the first
341   integer is a vbr6 that indicates the array length, followed by the encoded
342   elements of the array.  An array may only occur as the last operand of an
343   abbreviation (except for the one final operand that gives the array's
344   type).
345
346 * Char6 (code 4): This field should be emitted as a `char6-encoded value`_.
347   This operand type takes no extra data. Char6 encoding is normally used as an
348   array element type.
349
350 * Blob (code 5): This field is emitted as a vbr6, followed by padding to a
351   32-bit boundary (for alignment) and an array of 8-bit objects.  The array of
352   bytes is further followed by tail padding to ensure that its total length is a
353   multiple of 4 bytes.  This makes it very efficient for the reader to decode
354   the data without having to make a copy of it: it can use a pointer to the data
355   in the mapped in file and poke directly at it.  A blob may only occur as the
356   last operand of an abbreviation.
357
358 For example, target triples in LLVM modules are encoded as a record of the form
359 ``[TRIPLE, 'a', 'b', 'c', 'd']``.  Consider if the bitstream emitted the
360 following abbrev entry:
361
362 ::
363
364   [0, Fixed, 4]
365   [0, Array]
366   [0, Char6]
367
368 When emitting a record with this abbreviation, the above entry would be emitted
369 as:
370
371 :raw-html:`<tt><blockquote>`
372 [4\ :sub:`abbrevwidth`, 2\ :sub:`4`, 4\ :sub:`vbr6`, 0\ :sub:`6`, 1\ :sub:`6`, 2\ :sub:`6`, 3\ :sub:`6`]
373 :raw-html:`</blockquote></tt>`
374
375 These values are:
376
377 #. The first value, 4, is the abbreviation ID for this abbreviation.
378
379 #. The second value, 2, is the record code for ``TRIPLE`` records within LLVM IR
380    file ``MODULE_BLOCK`` blocks.
381
382 #. The third value, 4, is the length of the array.
383
384 #. The rest of the values are the char6 encoded values for ``"abcd"``.
385
386 With this abbreviation, the triple is emitted with only 37 bits (assuming a
387 abbrev id width of 3).  Without the abbreviation, significantly more space would
388 be required to emit the target triple.  Also, because the ``TRIPLE`` value is
389 not emitted as a literal in the abbreviation, the abbreviation can also be used
390 for any other string value.
391
392 .. _standard blocks:
393 .. _standard block:
394
395 Standard Blocks
396 ---------------
397
398 In addition to the basic block structure and record encodings, the bitstream
399 also defines specific built-in block types.  These block types specify how the
400 stream is to be decoded or other metadata.  In the future, new standard blocks
401 may be added.  Block IDs 0-7 are reserved for standard blocks.
402
403 .. _BLOCKINFO:
404
405 #0 - BLOCKINFO Block
406 ^^^^^^^^^^^^^^^^^^^^
407
408 The ``BLOCKINFO`` block allows the description of metadata for other blocks.
409 The currently specified records are:
410
411 ::
412
413   [SETBID (#1), blockid]
414   [DEFINE_ABBREV, ...]
415   [BLOCKNAME, ...name...]
416   [SETRECORDNAME, RecordID, ...name...]
417
418 The ``SETBID`` record (code 1) indicates which block ID is being described.
419 ``SETBID`` records can occur multiple times throughout the block to change which
420 block ID is being described.  There must be a ``SETBID`` record prior to any
421 other records.
422
423 Standard ``DEFINE_ABBREV`` records can occur inside ``BLOCKINFO`` blocks, but
424 unlike their occurrence in normal blocks, the abbreviation is defined for blocks
425 matching the block ID we are describing, *not* the ``BLOCKINFO`` block
426 itself.  The abbreviations defined in ``BLOCKINFO`` blocks receive abbreviation
427 IDs as described in `DEFINE_ABBREV`_.
428
429 The ``BLOCKNAME`` record (code 2) can optionally occur in this block.  The
430 elements of the record are the bytes of the string name of the block.
431 llvm-bcanalyzer can use this to dump out bitcode files symbolically.
432
433 The ``SETRECORDNAME`` record (code 3) can also optionally occur in this block.
434 The first operand value is a record ID number, and the rest of the elements of
435 the record are the bytes for the string name of the record.  llvm-bcanalyzer can
436 use this to dump out bitcode files symbolically.
437
438 Note that although the data in ``BLOCKINFO`` blocks is described as "metadata,"
439 the abbreviations they contain are essential for parsing records from the
440 corresponding blocks.  It is not safe to skip them.
441
442 .. _wrapper:
443
444 Bitcode Wrapper Format
445 ======================
446
447 Bitcode files for LLVM IR may optionally be wrapped in a simple wrapper
448 structure.  This structure contains a simple header that indicates the offset
449 and size of the embedded BC file.  This allows additional information to be
450 stored alongside the BC file.  The structure of this file header is:
451
452 :raw-html:`<tt><blockquote>`
453 [Magic\ :sub:`32`, Version\ :sub:`32`, Offset\ :sub:`32`, Size\ :sub:`32`, CPUType\ :sub:`32`]
454 :raw-html:`</blockquote></tt>`
455
456 Each of the fields are 32-bit fields stored in little endian form (as with the
457 rest of the bitcode file fields).  The Magic number is always ``0x0B17C0DE`` and
458 the version is currently always ``0``.  The Offset field is the offset in bytes
459 to the start of the bitcode stream in the file, and the Size field is the size
460 in bytes of the stream. CPUType is a target-specific value that can be used to
461 encode the CPU of the target.
462
463 .. _encoding of LLVM IR:
464
465 LLVM IR Encoding
466 ================
467
468 LLVM IR is encoded into a bitstream by defining blocks and records.  It uses
469 blocks for things like constant pools, functions, symbol tables, etc.  It uses
470 records for things like instructions, global variable descriptors, type
471 descriptions, etc.  This document does not describe the set of abbreviations
472 that the writer uses, as these are fully self-described in the file, and the
473 reader is not allowed to build in any knowledge of this.
474
475 Basics
476 ------
477
478 LLVM IR Magic Number
479 ^^^^^^^^^^^^^^^^^^^^
480
481 The magic number for LLVM IR files is:
482
483 :raw-html:`<tt><blockquote>`
484 [0x0\ :sub:`4`, 0xC\ :sub:`4`, 0xE\ :sub:`4`, 0xD\ :sub:`4`]
485 :raw-html:`</blockquote></tt>`
486
487 When combined with the bitcode magic number and viewed as bytes, this is
488 ``"BC 0xC0DE"``.
489
490 .. _Signed VBRs:
491
492 Signed VBRs
493 ^^^^^^^^^^^
494
495 `Variable Width Integer`_ encoding is an efficient way to encode arbitrary sized
496 unsigned values, but is an extremely inefficient for encoding signed values, as
497 signed values are otherwise treated as maximally large unsigned values.
498
499 As such, signed VBR values of a specific width are emitted as follows:
500
501 * Positive values are emitted as VBRs of the specified width, but with their
502   value shifted left by one.
503
504 * Negative values are emitted as VBRs of the specified width, but the negated
505   value is shifted left by one, and the low bit is set.
506
507 With this encoding, small positive and small negative values can both be emitted
508 efficiently. Signed VBR encoding is used in ``CST_CODE_INTEGER`` and
509 ``CST_CODE_WIDE_INTEGER`` records within ``CONSTANTS_BLOCK`` blocks.
510 It is also used for phi instruction operands in `MODULE_CODE_VERSION`_ 1.
511
512 LLVM IR Blocks
513 ^^^^^^^^^^^^^^
514
515 LLVM IR is defined with the following blocks:
516
517 * 8 --- `MODULE_BLOCK`_ --- This is the top-level block that contains the entire
518   module, and describes a variety of per-module information.
519
520 * 9 --- `PARAMATTR_BLOCK`_ --- This enumerates the parameter attributes.
521
522 * 10 --- `TYPE_BLOCK`_ --- This describes all of the types in the module.
523
524 * 11 --- `CONSTANTS_BLOCK`_ --- This describes constants for a module or
525   function.
526
527 * 12 --- `FUNCTION_BLOCK`_ --- This describes a function body.
528
529 * 13 --- `TYPE_SYMTAB_BLOCK`_ --- This describes the type symbol table.
530
531 * 14 --- `VALUE_SYMTAB_BLOCK`_ --- This describes a value symbol table.
532
533 * 15 --- `METADATA_BLOCK`_ --- This describes metadata items.
534
535 * 16 --- `METADATA_ATTACHMENT`_ --- This contains records associating metadata
536   with function instruction values.
537
538 .. _MODULE_BLOCK:
539
540 MODULE_BLOCK Contents
541 ---------------------
542
543 The ``MODULE_BLOCK`` block (id 8) is the top-level block for LLVM bitcode files,
544 and each bitcode file must contain exactly one. In addition to records
545 (described below) containing information about the module, a ``MODULE_BLOCK``
546 block may contain the following sub-blocks:
547
548 * `BLOCKINFO`_
549 * `PARAMATTR_BLOCK`_
550 * `TYPE_BLOCK`_
551 * `TYPE_SYMTAB_BLOCK`_
552 * `VALUE_SYMTAB_BLOCK`_
553 * `CONSTANTS_BLOCK`_
554 * `FUNCTION_BLOCK`_
555 * `METADATA_BLOCK`_
556
557 .. _MODULE_CODE_VERSION:
558
559 MODULE_CODE_VERSION Record
560 ^^^^^^^^^^^^^^^^^^^^^^^^^^
561
562 ``[VERSION, version#]``
563
564 The ``VERSION`` record (code 1) contains a single value indicating the format
565 version. Versions 0 and 1 are supported at this time. The difference between
566 version 0 and 1 is in the encoding of instruction operands in
567 each `FUNCTION_BLOCK`_.
568
569 In version 0, each value defined by an instruction is assigned an ID
570 unique to the function. Function-level value IDs are assigned starting from
571 ``NumModuleValues`` since they share the same namespace as module-level
572 values. The value enumerator resets after each function. When a value is
573 an operand of an instruction, the value ID is used to represent the operand.
574 For large functions or large modules, these operand values can be large.
575
576 The encoding in version 1 attempts to avoid large operand values
577 in common cases. Instead of using the value ID directly, operands are
578 encoded as relative to the current instruction. Thus, if an operand
579 is the value defined by the previous instruction, the operand
580 will be encoded as 1.
581
582 For example, instead of
583
584 .. code-block:: llvm
585
586   #n = load #n-1
587   #n+1 = icmp eq #n, #const0
588   br #n+1, label #(bb1), label #(bb2)
589
590 version 1 will encode the instructions as
591
592 .. code-block:: llvm
593
594   #n = load #1
595   #n+1 = icmp eq #1, (#n+1)-#const0
596   br #1, label #(bb1), label #(bb2)
597
598 Note in the example that operands which are constants also use
599 the relative encoding, while operands like basic block labels
600 do not use the relative encoding.
601
602 Forward references will result in a negative value.
603 This can be inefficient, as operands are normally encoded
604 as unsigned VBRs. However, forward references are rare, except in the
605 case of phi instructions. For phi instructions, operands are encoded as
606 `Signed VBRs`_ to deal with forward references.
607
608
609 MODULE_CODE_TRIPLE Record
610 ^^^^^^^^^^^^^^^^^^^^^^^^^
611
612 ``[TRIPLE, ...string...]``
613
614 The ``TRIPLE`` record (code 2) contains a variable number of values representing
615 the bytes of the ``target triple`` specification string.
616
617 MODULE_CODE_DATALAYOUT Record
618 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
619
620 ``[DATALAYOUT, ...string...]``
621
622 The ``DATALAYOUT`` record (code 3) contains a variable number of values
623 representing the bytes of the ``target datalayout`` specification string.
624
625 MODULE_CODE_ASM Record
626 ^^^^^^^^^^^^^^^^^^^^^^
627
628 ``[ASM, ...string...]``
629
630 The ``ASM`` record (code 4) contains a variable number of values representing
631 the bytes of ``module asm`` strings, with individual assembly blocks separated
632 by newline (ASCII 10) characters.
633
634 .. _MODULE_CODE_SECTIONNAME:
635
636 MODULE_CODE_SECTIONNAME Record
637 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
638
639 ``[SECTIONNAME, ...string...]``
640
641 The ``SECTIONNAME`` record (code 5) contains a variable number of values
642 representing the bytes of a single section name string. There should be one
643 ``SECTIONNAME`` record for each section name referenced (e.g., in global
644 variable or function ``section`` attributes) within the module. These records
645 can be referenced by the 1-based index in the *section* fields of ``GLOBALVAR``
646 or ``FUNCTION`` records.
647
648 MODULE_CODE_DEPLIB Record
649 ^^^^^^^^^^^^^^^^^^^^^^^^^
650
651 ``[DEPLIB, ...string...]``
652
653 The ``DEPLIB`` record (code 6) contains a variable number of values representing
654 the bytes of a single dependent library name string, one of the libraries
655 mentioned in a ``deplibs`` declaration.  There should be one ``DEPLIB`` record
656 for each library name referenced.
657
658 MODULE_CODE_GLOBALVAR Record
659 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
660
661 ``[GLOBALVAR, pointer type, isconst, initid, linkage, alignment, section, visibility, threadlocal, unnamed_addr]``
662
663 The ``GLOBALVAR`` record (code 7) marks the declaration or definition of a
664 global variable. The operand fields are:
665
666 * *pointer type*: The type index of the pointer type used to point to this
667   global variable
668
669 * *isconst*: Non-zero if the variable is treated as constant within the module,
670   or zero if it is not
671
672 * *initid*: If non-zero, the value index of the initializer for this variable,
673   plus 1.
674
675 .. _linkage type:
676
677 * *linkage*: An encoding of the linkage type for this variable:
678   * ``external``: code 0
679   * ``weak``: code 1
680   * ``appending``: code 2
681   * ``internal``: code 3
682   * ``linkonce``: code 4
683   * ``dllimport``: code 5
684   * ``dllexport``: code 6
685   * ``extern_weak``: code 7
686   * ``common``: code 8
687   * ``private``: code 9
688   * ``weak_odr``: code 10
689   * ``linkonce_odr``: code 11
690   * ``available_externally``: code 12
691   * ``linker_private``: code 13
692
693 * alignment*: The logarithm base 2 of the variable's requested alignment, plus 1
694
695 * *section*: If non-zero, the 1-based section index in the table of
696   `MODULE_CODE_SECTIONNAME`_ entries.
697
698 .. _visibility:
699
700 * *visibility*: If present, an encoding of the visibility of this variable:
701   * ``default``: code 0
702   * ``hidden``: code 1
703   * ``protected``: code 2
704
705 * *threadlocal*: If present, an encoding of the thread local storage mode of the
706   variable:
707   * ``not thread local``: code 0
708   * ``thread local; default TLS model``: code 1
709   * ``localdynamic``: code 2
710   * ``initialexec``: code 3
711   * ``localexec``: code 4
712
713 * *unnamed_addr*: If present and non-zero, indicates that the variable has
714   ``unnamed_addr``
715
716 .. _FUNCTION:
717
718 MODULE_CODE_FUNCTION Record
719 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
720
721 ``[FUNCTION, type, callingconv, isproto, linkage, paramattr, alignment, section, visibility, gc]``
722
723 The ``FUNCTION`` record (code 8) marks the declaration or definition of a
724 function. The operand fields are:
725
726 * *type*: The type index of the function type describing this function
727
728 * *callingconv*: The calling convention number:
729   * ``ccc``: code 0
730   * ``fastcc``: code 8
731   * ``coldcc``: code 9
732   * ``x86_stdcallcc``: code 64
733   * ``x86_fastcallcc``: code 65
734   * ``arm_apcscc``: code 66
735   * ``arm_aapcscc``: code 67
736   * ``arm_aapcs_vfpcc``: code 68
737
738 * isproto*: Non-zero if this entry represents a declaration rather than a
739   definition
740
741 * *linkage*: An encoding of the `linkage type`_ for this function
742
743 * *paramattr*: If nonzero, the 1-based parameter attribute index into the table
744   of `PARAMATTR_CODE_ENTRY`_ entries.
745
746 * *alignment*: The logarithm base 2 of the function's requested alignment, plus
747   1
748
749 * *section*: If non-zero, the 1-based section index in the table of
750   `MODULE_CODE_SECTIONNAME`_ entries.
751
752 * *visibility*: An encoding of the `visibility`_ of this function
753
754 * *gc*: If present and nonzero, the 1-based garbage collector index in the table
755   of `MODULE_CODE_GCNAME`_ entries.
756
757 * *unnamed_addr*: If present and non-zero, indicates that the function has
758   ``unnamed_addr``
759
760 MODULE_CODE_ALIAS Record
761 ^^^^^^^^^^^^^^^^^^^^^^^^
762
763 ``[ALIAS, alias type, aliasee val#, linkage, visibility]``
764
765 The ``ALIAS`` record (code 9) marks the definition of an alias. The operand
766 fields are
767
768 * *alias type*: The type index of the alias
769
770 * *aliasee val#*: The value index of the aliased value
771
772 * *linkage*: An encoding of the `linkage type`_ for this alias
773
774 * *visibility*: If present, an encoding of the `visibility`_ of the alias
775
776 MODULE_CODE_PURGEVALS Record
777 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
778
779 ``[PURGEVALS, numvals]``
780
781 The ``PURGEVALS`` record (code 10) resets the module-level value list to the
782 size given by the single operand value. Module-level value list items are added
783 by ``GLOBALVAR``, ``FUNCTION``, and ``ALIAS`` records.  After a ``PURGEVALS``
784 record is seen, new value indices will start from the given *numvals* value.
785
786 .. _MODULE_CODE_GCNAME:
787
788 MODULE_CODE_GCNAME Record
789 ^^^^^^^^^^^^^^^^^^^^^^^^^
790
791 ``[GCNAME, ...string...]``
792
793 The ``GCNAME`` record (code 11) contains a variable number of values
794 representing the bytes of a single garbage collector name string. There should
795 be one ``GCNAME`` record for each garbage collector name referenced in function
796 ``gc`` attributes within the module. These records can be referenced by 1-based
797 index in the *gc* fields of ``FUNCTION`` records.
798
799 .. _PARAMATTR_BLOCK:
800
801 PARAMATTR_BLOCK Contents
802 ------------------------
803
804 The ``PARAMATTR_BLOCK`` block (id 9) contains a table of entries describing the
805 attributes of function parameters. These entries are referenced by 1-based index
806 in the *paramattr* field of module block `FUNCTION`_ records, or within the
807 *attr* field of function block ``INST_INVOKE`` and ``INST_CALL`` records.
808
809 Entries within ``PARAMATTR_BLOCK`` are constructed to ensure that each is unique
810 (i.e., no two indicies represent equivalent attribute lists).
811
812 .. _PARAMATTR_CODE_ENTRY:
813
814 PARAMATTR_CODE_ENTRY Record
815 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
816
817 ``[ENTRY, paramidx0, attr0, paramidx1, attr1...]``
818
819 The ``ENTRY`` record (code 1) contains an even number of values describing a
820 unique set of function parameter attributes. Each *paramidx* value indicates
821 which set of attributes is represented, with 0 representing the return value
822 attributes, 0xFFFFFFFF representing function attributes, and other values
823 representing 1-based function parameters. Each *attr* value is a bitmap with the
824 following interpretation:
825
826 * bit 0: ``zeroext``
827 * bit 1: ``signext``
828 * bit 2: ``noreturn``
829 * bit 3: ``inreg``
830 * bit 4: ``sret``
831 * bit 5: ``nounwind``
832 * bit 6: ``noalias``
833 * bit 7: ``byval``
834 * bit 8: ``nest``
835 * bit 9: ``readnone``
836 * bit 10: ``readonly``
837 * bit 11: ``noinline``
838 * bit 12: ``alwaysinline``
839 * bit 13: ``optsize``
840 * bit 14: ``ssp``
841 * bit 15: ``sspreq``
842 * bits 16-31: ``align n``
843 * bit 32: ``nocapture``
844 * bit 33: ``noredzone``
845 * bit 34: ``noimplicitfloat``
846 * bit 35: ``naked``
847 * bit 36: ``inlinehint``
848 * bits 37-39: ``alignstack n``, represented as the logarithm
849   base 2 of the requested alignment, plus 1
850
851 .. _TYPE_BLOCK:
852
853 TYPE_BLOCK Contents
854 -------------------
855
856 The ``TYPE_BLOCK`` block (id 10) contains records which constitute a table of
857 type operator entries used to represent types referenced within an LLVM
858 module. Each record (with the exception of `NUMENTRY`_) generates a single type
859 table entry, which may be referenced by 0-based index from instructions,
860 constants, metadata, type symbol table entries, or other type operator records.
861
862 Entries within ``TYPE_BLOCK`` are constructed to ensure that each entry is
863 unique (i.e., no two indicies represent structurally equivalent types).
864
865 .. _TYPE_CODE_NUMENTRY:
866 .. _NUMENTRY:
867
868 TYPE_CODE_NUMENTRY Record
869 ^^^^^^^^^^^^^^^^^^^^^^^^^
870
871 ``[NUMENTRY, numentries]``
872
873 The ``NUMENTRY`` record (code 1) contains a single value which indicates the
874 total number of type code entries in the type table of the module. If present,
875 ``NUMENTRY`` should be the first record in the block.
876
877 TYPE_CODE_VOID Record
878 ^^^^^^^^^^^^^^^^^^^^^
879
880 ``[VOID]``
881
882 The ``VOID`` record (code 2) adds a ``void`` type to the type table.
883
884 TYPE_CODE_HALF Record
885 ^^^^^^^^^^^^^^^^^^^^^
886
887 ``[HALF]``
888
889 The ``HALF`` record (code 10) adds a ``half`` (16-bit floating point) type to
890 the type table.
891
892 TYPE_CODE_FLOAT Record
893 ^^^^^^^^^^^^^^^^^^^^^^
894
895 ``[FLOAT]``
896
897 The ``FLOAT`` record (code 3) adds a ``float`` (32-bit floating point) type to
898 the type table.
899
900 TYPE_CODE_DOUBLE Record
901 ^^^^^^^^^^^^^^^^^^^^^^^
902
903 ``[DOUBLE]``
904
905 The ``DOUBLE`` record (code 4) adds a ``double`` (64-bit floating point) type to
906 the type table.
907
908 TYPE_CODE_LABEL Record
909 ^^^^^^^^^^^^^^^^^^^^^^
910
911 ``[LABEL]``
912
913 The ``LABEL`` record (code 5) adds a ``label`` type to the type table.
914
915 TYPE_CODE_OPAQUE Record
916 ^^^^^^^^^^^^^^^^^^^^^^^
917
918 ``[OPAQUE]``
919
920 The ``OPAQUE`` record (code 6) adds an ``opaque`` type to the type table. Note
921 that distinct ``opaque`` types are not unified.
922
923 TYPE_CODE_INTEGER Record
924 ^^^^^^^^^^^^^^^^^^^^^^^^
925
926 ``[INTEGER, width]``
927
928 The ``INTEGER`` record (code 7) adds an integer type to the type table. The
929 single *width* field indicates the width of the integer type.
930
931 TYPE_CODE_POINTER Record
932 ^^^^^^^^^^^^^^^^^^^^^^^^
933
934 ``[POINTER, pointee type, address space]``
935
936 The ``POINTER`` record (code 8) adds a pointer type to the type table. The
937 operand fields are
938
939 * *pointee type*: The type index of the pointed-to type
940
941 * *address space*: If supplied, the target-specific numbered address space where
942   the pointed-to object resides. Otherwise, the default address space is zero.
943
944 TYPE_CODE_FUNCTION Record
945 ^^^^^^^^^^^^^^^^^^^^^^^^^
946
947 ``[FUNCTION, vararg, ignored, retty, ...paramty... ]``
948
949 The ``FUNCTION`` record (code 9) adds a function type to the type table. The
950 operand fields are
951
952 * *vararg*: Non-zero if the type represents a varargs function
953
954 * *ignored*: This value field is present for backward compatibility only, and is
955   ignored
956
957 * *retty*: The type index of the function's return type
958
959 * *paramty*: Zero or more type indices representing the parameter types of the
960   function
961
962 TYPE_CODE_STRUCT Record
963 ^^^^^^^^^^^^^^^^^^^^^^^
964
965 ``[STRUCT, ispacked, ...eltty...]``
966
967 The ``STRUCT`` record (code 10) adds a struct type to the type table. The
968 operand fields are
969
970 * *ispacked*: Non-zero if the type represents a packed structure
971
972 * *eltty*: Zero or more type indices representing the element types of the
973   structure
974
975 TYPE_CODE_ARRAY Record
976 ^^^^^^^^^^^^^^^^^^^^^^
977
978 ``[ARRAY, numelts, eltty]``
979
980 The ``ARRAY`` record (code 11) adds an array type to the type table.  The
981 operand fields are
982
983 * *numelts*: The number of elements in arrays of this type
984
985 * *eltty*: The type index of the array element type
986
987 TYPE_CODE_VECTOR Record
988 ^^^^^^^^^^^^^^^^^^^^^^^
989
990 ``[VECTOR, numelts, eltty]``
991
992 The ``VECTOR`` record (code 12) adds a vector type to the type table.  The
993 operand fields are
994
995 * *numelts*: The number of elements in vectors of this type
996
997 * *eltty*: The type index of the vector element type
998
999 TYPE_CODE_X86_FP80 Record
1000 ^^^^^^^^^^^^^^^^^^^^^^^^^
1001
1002 ``[X86_FP80]``
1003
1004 The ``X86_FP80`` record (code 13) adds an ``x86_fp80`` (80-bit floating point)
1005 type to the type table.
1006
1007 TYPE_CODE_FP128 Record
1008 ^^^^^^^^^^^^^^^^^^^^^^
1009
1010 ``[FP128]``
1011
1012 The ``FP128`` record (code 14) adds an ``fp128`` (128-bit floating point) type
1013 to the type table.
1014
1015 TYPE_CODE_PPC_FP128 Record
1016 ^^^^^^^^^^^^^^^^^^^^^^^^^^
1017
1018 ``[PPC_FP128]``
1019
1020 The ``PPC_FP128`` record (code 15) adds a ``ppc_fp128`` (128-bit floating point)
1021 type to the type table.
1022
1023 TYPE_CODE_METADATA Record
1024 ^^^^^^^^^^^^^^^^^^^^^^^^^
1025
1026 ``[METADATA]``
1027
1028 The ``METADATA`` record (code 16) adds a ``metadata`` type to the type table.
1029
1030 .. _CONSTANTS_BLOCK:
1031
1032 CONSTANTS_BLOCK Contents
1033 ------------------------
1034
1035 The ``CONSTANTS_BLOCK`` block (id 11) ...
1036
1037 .. _FUNCTION_BLOCK:
1038
1039 FUNCTION_BLOCK Contents
1040 -----------------------
1041
1042 The ``FUNCTION_BLOCK`` block (id 12) ...
1043
1044 In addition to the record types described below, a ``FUNCTION_BLOCK`` block may
1045 contain the following sub-blocks:
1046
1047 * `CONSTANTS_BLOCK`_
1048 * `VALUE_SYMTAB_BLOCK`_
1049 * `METADATA_ATTACHMENT`_
1050
1051 .. _TYPE_SYMTAB_BLOCK:
1052
1053 TYPE_SYMTAB_BLOCK Contents
1054 --------------------------
1055
1056 The ``TYPE_SYMTAB_BLOCK`` block (id 13) contains entries which map between
1057 module-level named types and their corresponding type indices.
1058
1059 .. _TST_CODE_ENTRY:
1060
1061 TST_CODE_ENTRY Record
1062 ^^^^^^^^^^^^^^^^^^^^^
1063
1064 ``[ENTRY, typeid, ...string...]``
1065
1066 The ``ENTRY`` record (code 1) contains a variable number of values, with the
1067 first giving the type index of the designated type, and the remaining values
1068 giving the character codes of the type name. Each entry corresponds to a single
1069 named type.
1070
1071 .. _VALUE_SYMTAB_BLOCK:
1072
1073 VALUE_SYMTAB_BLOCK Contents
1074 ---------------------------
1075
1076 The ``VALUE_SYMTAB_BLOCK`` block (id 14) ... 
1077
1078 .. _METADATA_BLOCK:
1079
1080 METADATA_BLOCK Contents
1081 -----------------------
1082
1083 The ``METADATA_BLOCK`` block (id 15) ...
1084
1085 .. _METADATA_ATTACHMENT:
1086
1087 METADATA_ATTACHMENT Contents
1088 ----------------------------
1089
1090 The ``METADATA_ATTACHMENT`` block (id 16) ...