BitCodeFormat.rst 38 KB

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