Implement function prefix data as an IR feature.
[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, prefix]``
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 * *prefix*: If non-zero, the value index of the prefix data for this function,
761   plus 1.
762
763 MODULE_CODE_ALIAS Record
764 ^^^^^^^^^^^^^^^^^^^^^^^^
765
766 ``[ALIAS, alias type, aliasee val#, linkage, visibility]``
767
768 The ``ALIAS`` record (code 9) marks the definition of an alias. The operand
769 fields are
770
771 * *alias type*: The type index of the alias
772
773 * *aliasee val#*: The value index of the aliased value
774
775 * *linkage*: An encoding of the `linkage type`_ for this alias
776
777 * *visibility*: If present, an encoding of the `visibility`_ of the alias
778
779 MODULE_CODE_PURGEVALS Record
780 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
781
782 ``[PURGEVALS, numvals]``
783
784 The ``PURGEVALS`` record (code 10) resets the module-level value list to the
785 size given by the single operand value. Module-level value list items are added
786 by ``GLOBALVAR``, ``FUNCTION``, and ``ALIAS`` records.  After a ``PURGEVALS``
787 record is seen, new value indices will start from the given *numvals* value.
788
789 .. _MODULE_CODE_GCNAME:
790
791 MODULE_CODE_GCNAME Record
792 ^^^^^^^^^^^^^^^^^^^^^^^^^
793
794 ``[GCNAME, ...string...]``
795
796 The ``GCNAME`` record (code 11) contains a variable number of values
797 representing the bytes of a single garbage collector name string. There should
798 be one ``GCNAME`` record for each garbage collector name referenced in function
799 ``gc`` attributes within the module. These records can be referenced by 1-based
800 index in the *gc* fields of ``FUNCTION`` records.
801
802 .. _PARAMATTR_BLOCK:
803
804 PARAMATTR_BLOCK Contents
805 ------------------------
806
807 The ``PARAMATTR_BLOCK`` block (id 9) contains a table of entries describing the
808 attributes of function parameters. These entries are referenced by 1-based index
809 in the *paramattr* field of module block `FUNCTION`_ records, or within the
810 *attr* field of function block ``INST_INVOKE`` and ``INST_CALL`` records.
811
812 Entries within ``PARAMATTR_BLOCK`` are constructed to ensure that each is unique
813 (i.e., no two indicies represent equivalent attribute lists).
814
815 .. _PARAMATTR_CODE_ENTRY:
816
817 PARAMATTR_CODE_ENTRY Record
818 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
819
820 ``[ENTRY, paramidx0, attr0, paramidx1, attr1...]``
821
822 The ``ENTRY`` record (code 1) contains an even number of values describing a
823 unique set of function parameter attributes. Each *paramidx* value indicates
824 which set of attributes is represented, with 0 representing the return value
825 attributes, 0xFFFFFFFF representing function attributes, and other values
826 representing 1-based function parameters. Each *attr* value is a bitmap with the
827 following interpretation:
828
829 * bit 0: ``zeroext``
830 * bit 1: ``signext``
831 * bit 2: ``noreturn``
832 * bit 3: ``inreg``
833 * bit 4: ``sret``
834 * bit 5: ``nounwind``
835 * bit 6: ``noalias``
836 * bit 7: ``byval``
837 * bit 8: ``nest``
838 * bit 9: ``readnone``
839 * bit 10: ``readonly``
840 * bit 11: ``noinline``
841 * bit 12: ``alwaysinline``
842 * bit 13: ``optsize``
843 * bit 14: ``ssp``
844 * bit 15: ``sspreq``
845 * bits 16-31: ``align n``
846 * bit 32: ``nocapture``
847 * bit 33: ``noredzone``
848 * bit 34: ``noimplicitfloat``
849 * bit 35: ``naked``
850 * bit 36: ``inlinehint``
851 * bits 37-39: ``alignstack n``, represented as the logarithm
852   base 2 of the requested alignment, plus 1
853
854 .. _TYPE_BLOCK:
855
856 TYPE_BLOCK Contents
857 -------------------
858
859 The ``TYPE_BLOCK`` block (id 10) contains records which constitute a table of
860 type operator entries used to represent types referenced within an LLVM
861 module. Each record (with the exception of `NUMENTRY`_) generates a single type
862 table entry, which may be referenced by 0-based index from instructions,
863 constants, metadata, type symbol table entries, or other type operator records.
864
865 Entries within ``TYPE_BLOCK`` are constructed to ensure that each entry is
866 unique (i.e., no two indicies represent structurally equivalent types).
867
868 .. _TYPE_CODE_NUMENTRY:
869 .. _NUMENTRY:
870
871 TYPE_CODE_NUMENTRY Record
872 ^^^^^^^^^^^^^^^^^^^^^^^^^
873
874 ``[NUMENTRY, numentries]``
875
876 The ``NUMENTRY`` record (code 1) contains a single value which indicates the
877 total number of type code entries in the type table of the module. If present,
878 ``NUMENTRY`` should be the first record in the block.
879
880 TYPE_CODE_VOID Record
881 ^^^^^^^^^^^^^^^^^^^^^
882
883 ``[VOID]``
884
885 The ``VOID`` record (code 2) adds a ``void`` type to the type table.
886
887 TYPE_CODE_HALF Record
888 ^^^^^^^^^^^^^^^^^^^^^
889
890 ``[HALF]``
891
892 The ``HALF`` record (code 10) adds a ``half`` (16-bit floating point) type to
893 the type table.
894
895 TYPE_CODE_FLOAT Record
896 ^^^^^^^^^^^^^^^^^^^^^^
897
898 ``[FLOAT]``
899
900 The ``FLOAT`` record (code 3) adds a ``float`` (32-bit floating point) type to
901 the type table.
902
903 TYPE_CODE_DOUBLE Record
904 ^^^^^^^^^^^^^^^^^^^^^^^
905
906 ``[DOUBLE]``
907
908 The ``DOUBLE`` record (code 4) adds a ``double`` (64-bit floating point) type to
909 the type table.
910
911 TYPE_CODE_LABEL Record
912 ^^^^^^^^^^^^^^^^^^^^^^
913
914 ``[LABEL]``
915
916 The ``LABEL`` record (code 5) adds a ``label`` type to the type table.
917
918 TYPE_CODE_OPAQUE Record
919 ^^^^^^^^^^^^^^^^^^^^^^^
920
921 ``[OPAQUE]``
922
923 The ``OPAQUE`` record (code 6) adds an ``opaque`` type to the type table. Note
924 that distinct ``opaque`` types are not unified.
925
926 TYPE_CODE_INTEGER Record
927 ^^^^^^^^^^^^^^^^^^^^^^^^
928
929 ``[INTEGER, width]``
930
931 The ``INTEGER`` record (code 7) adds an integer type to the type table. The
932 single *width* field indicates the width of the integer type.
933
934 TYPE_CODE_POINTER Record
935 ^^^^^^^^^^^^^^^^^^^^^^^^
936
937 ``[POINTER, pointee type, address space]``
938
939 The ``POINTER`` record (code 8) adds a pointer type to the type table. The
940 operand fields are
941
942 * *pointee type*: The type index of the pointed-to type
943
944 * *address space*: If supplied, the target-specific numbered address space where
945   the pointed-to object resides. Otherwise, the default address space is zero.
946
947 TYPE_CODE_FUNCTION Record
948 ^^^^^^^^^^^^^^^^^^^^^^^^^
949
950 ``[FUNCTION, vararg, ignored, retty, ...paramty... ]``
951
952 The ``FUNCTION`` record (code 9) adds a function type to the type table. The
953 operand fields are
954
955 * *vararg*: Non-zero if the type represents a varargs function
956
957 * *ignored*: This value field is present for backward compatibility only, and is
958   ignored
959
960 * *retty*: The type index of the function's return type
961
962 * *paramty*: Zero or more type indices representing the parameter types of the
963   function
964
965 TYPE_CODE_STRUCT Record
966 ^^^^^^^^^^^^^^^^^^^^^^^
967
968 ``[STRUCT, ispacked, ...eltty...]``
969
970 The ``STRUCT`` record (code 10) adds a struct type to the type table. The
971 operand fields are
972
973 * *ispacked*: Non-zero if the type represents a packed structure
974
975 * *eltty*: Zero or more type indices representing the element types of the
976   structure
977
978 TYPE_CODE_ARRAY Record
979 ^^^^^^^^^^^^^^^^^^^^^^
980
981 ``[ARRAY, numelts, eltty]``
982
983 The ``ARRAY`` record (code 11) adds an array type to the type table.  The
984 operand fields are
985
986 * *numelts*: The number of elements in arrays of this type
987
988 * *eltty*: The type index of the array element type
989
990 TYPE_CODE_VECTOR Record
991 ^^^^^^^^^^^^^^^^^^^^^^^
992
993 ``[VECTOR, numelts, eltty]``
994
995 The ``VECTOR`` record (code 12) adds a vector type to the type table.  The
996 operand fields are
997
998 * *numelts*: The number of elements in vectors of this type
999
1000 * *eltty*: The type index of the vector element type
1001
1002 TYPE_CODE_X86_FP80 Record
1003 ^^^^^^^^^^^^^^^^^^^^^^^^^
1004
1005 ``[X86_FP80]``
1006
1007 The ``X86_FP80`` record (code 13) adds an ``x86_fp80`` (80-bit floating point)
1008 type to the type table.
1009
1010 TYPE_CODE_FP128 Record
1011 ^^^^^^^^^^^^^^^^^^^^^^
1012
1013 ``[FP128]``
1014
1015 The ``FP128`` record (code 14) adds an ``fp128`` (128-bit floating point) type
1016 to the type table.
1017
1018 TYPE_CODE_PPC_FP128 Record
1019 ^^^^^^^^^^^^^^^^^^^^^^^^^^
1020
1021 ``[PPC_FP128]``
1022
1023 The ``PPC_FP128`` record (code 15) adds a ``ppc_fp128`` (128-bit floating point)
1024 type to the type table.
1025
1026 TYPE_CODE_METADATA Record
1027 ^^^^^^^^^^^^^^^^^^^^^^^^^
1028
1029 ``[METADATA]``
1030
1031 The ``METADATA`` record (code 16) adds a ``metadata`` type to the type table.
1032
1033 .. _CONSTANTS_BLOCK:
1034
1035 CONSTANTS_BLOCK Contents
1036 ------------------------
1037
1038 The ``CONSTANTS_BLOCK`` block (id 11) ...
1039
1040 .. _FUNCTION_BLOCK:
1041
1042 FUNCTION_BLOCK Contents
1043 -----------------------
1044
1045 The ``FUNCTION_BLOCK`` block (id 12) ...
1046
1047 In addition to the record types described below, a ``FUNCTION_BLOCK`` block may
1048 contain the following sub-blocks:
1049
1050 * `CONSTANTS_BLOCK`_
1051 * `VALUE_SYMTAB_BLOCK`_
1052 * `METADATA_ATTACHMENT`_
1053
1054 .. _TYPE_SYMTAB_BLOCK:
1055
1056 TYPE_SYMTAB_BLOCK Contents
1057 --------------------------
1058
1059 The ``TYPE_SYMTAB_BLOCK`` block (id 13) contains entries which map between
1060 module-level named types and their corresponding type indices.
1061
1062 .. _TST_CODE_ENTRY:
1063
1064 TST_CODE_ENTRY Record
1065 ^^^^^^^^^^^^^^^^^^^^^
1066
1067 ``[ENTRY, typeid, ...string...]``
1068
1069 The ``ENTRY`` record (code 1) contains a variable number of values, with the
1070 first giving the type index of the designated type, and the remaining values
1071 giving the character codes of the type name. Each entry corresponds to a single
1072 named type.
1073
1074 .. _VALUE_SYMTAB_BLOCK:
1075
1076 VALUE_SYMTAB_BLOCK Contents
1077 ---------------------------
1078
1079 The ``VALUE_SYMTAB_BLOCK`` block (id 14) ... 
1080
1081 .. _METADATA_BLOCK:
1082
1083 METADATA_BLOCK Contents
1084 -----------------------
1085
1086 The ``METADATA_BLOCK`` block (id 15) ...
1087
1088 .. _METADATA_ATTACHMENT:
1089
1090 METADATA_ATTACHMENT Contents
1091 ----------------------------
1092
1093 The ``METADATA_ATTACHMENT`` block (id 16) ...