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