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