1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8">
6 <title>LLVM Bitcode File Format
</title>
7 <link rel=
"stylesheet" href=
"llvm.css" type=
"text/css">
10 <div class=
"doc_title"> LLVM Bitcode File Format
</div>
12 <li><a href=
"#abstract">Abstract
</a></li>
13 <li><a href=
"#overview">Overview
</a></li>
14 <li><a href=
"#bitstream">Bitstream Format
</a>
16 <li><a href=
"#magic">Magic Numbers
</a></li>
17 <li><a href=
"#primitives">Primitives
</a></li>
18 <li><a href=
"#abbrevid">Abbreviation IDs
</a></li>
19 <li><a href=
"#blocks">Blocks
</a></li>
20 <li><a href=
"#datarecord">Data Records
</a></li>
21 <li><a href=
"#abbreviations">Abbreviations
</a></li>
22 <li><a href=
"#stdblocks">Standard Blocks
</a></li>
25 <li><a href=
"#llvmir">LLVM IR Encoding
</a>
27 <li><a href=
"#basics">Basics
</a></li>
31 <div class=
"doc_author">
32 <p>Written by
<a href=
"mailto:sabre@nondot.org">Chris Lattner
</a>.
36 <!-- *********************************************************************** -->
37 <div class=
"doc_section"> <a name=
"abstract">Abstract
</a></div>
38 <!-- *********************************************************************** -->
40 <div class=
"doc_text">
42 <p>This document describes the LLVM bitstream file format and the encoding of
43 the LLVM IR into it.
</p>
47 <!-- *********************************************************************** -->
48 <div class=
"doc_section"> <a name=
"overview">Overview
</a></div>
49 <!-- *********************************************************************** -->
51 <div class=
"doc_text">
54 What is commonly known as the LLVM bitcode file format (also, sometimes
55 anachronistically known as bytecode) is actually two things: a
<a
56 href=
"#bitstream">bitstream container format
</a>
57 and an
<a href=
"#llvmir">encoding of LLVM IR
</a> into the container format.
</p>
60 The bitstream format is an abstract encoding of structured data, very
61 similar to XML in some ways. Like XML, bitstream files contain tags, and nested
62 structures, and you can parse the file without having to understand the tags.
63 Unlike XML, the bitstream format is a binary encoding, and unlike XML it
64 provides a mechanism for the file to self-describe
"abbreviations", which are
65 effectively size optimizations for the content.
</p>
67 <p>This document first describes the LLVM bitstream format, then describes the
68 record structure used by LLVM IR files.
73 <!-- *********************************************************************** -->
74 <div class=
"doc_section"> <a name=
"bitstream">Bitstream Format
</a></div>
75 <!-- *********************************************************************** -->
77 <div class=
"doc_text">
80 The bitstream format is literally a stream of bits, with a very simple
81 structure. This structure consists of the following concepts:
85 <li>A
"<a href="#magic
">magic number</a>" that identifies the contents of
87 <li>Encoding
<a href=
"#primitives">primitives
</a> like variable bit-rate
89 <li><a href=
"#blocks">Blocks
</a>, which define nested content.
</li>
90 <li><a href=
"#datarecord">Data Records
</a>, which describe entities within the
92 <li>Abbreviations, which specify compression optimizations for the file.
</li>
96 href=
"CommandGuide/html/llvm-bcanalyzer.html">llvm-bcanalyzer
</a> tool can be
97 used to dump and inspect arbitrary bitstreams, which is very useful for
98 understanding the encoding.
</p>
102 <!-- ======================================================================= -->
103 <div class=
"doc_subsection"><a name=
"magic">Magic Numbers
</a>
106 <div class=
"doc_text">
108 <p>The first four bytes of the stream identify the encoding of the file. This
109 is used by a reader to know what is contained in the file.
</p>
113 <!-- ======================================================================= -->
114 <div class=
"doc_subsection"><a name=
"primitives">Primitives
</a>
117 <div class=
"doc_text">
120 A bitstream literally consists of a stream of bits. This stream is made up of a
121 number of primitive values that encode a stream of unsigned integer values.
123 integers are are encoded in two ways: either as
<a href=
"#fixedwidth">Fixed
124 Width Integers
</a> or as
<a href=
"#variablewidth">Variable Width
130 <!-- _______________________________________________________________________ -->
131 <div class=
"doc_subsubsection"> <a name=
"fixedwidth">Fixed Width Integers
</a>
134 <div class=
"doc_text">
136 <p>Fixed-width integer values have their low bits emitted directly to the file.
137 For example, a
3-bit integer value encodes
1 as
001. Fixed width integers
138 are used when there are a well-known number of options for a field. For
139 example, boolean values are usually encoded with a
1-bit wide integer.
144 <!-- _______________________________________________________________________ -->
145 <div class=
"doc_subsubsection"> <a name=
"variablewidth">Variable Width
148 <div class=
"doc_text">
150 <p>Variable-width integer (VBR) values encode values of arbitrary size,
151 optimizing for the case where the values are small. Given a
4-bit VBR field,
152 any
3-bit value (
0 through
7) is encoded directly, with the high bit set to
153 zero. Values larger than N-
1 bits emit their bits in a series of N-
1 bit
154 chunks, where all but the last set the high bit.
</p>
156 <p>For example, the value
27 (
0x1B) is encoded as
1011 0011 when emitted as a
157 vbr4 value. The first set of four bits indicates the value
3 (
011) with a
158 continuation piece (indicated by a high bit of
1). The next word indicates a
159 value of
24 (
011 <<
3) with no continuation. The sum (
3+
24) yields the value
165 <!-- _______________________________________________________________________ -->
166 <div class=
"doc_subsubsection"> <a name=
"char6">6-bit characters
</a></div>
168 <div class=
"doc_text">
170 <p>6-bit characters encode common characters into a fixed
6-bit field. They
171 represent the following characters with the following
6-bit values:
</p>
174 <li>'a' .. 'z' -
0 ..
25</li>
175 <li>'A' .. 'Z' -
26 ..
52</li>
176 <li>'
0' .. '
9' -
53 ..
61</li>
181 <p>This encoding is only suitable for encoding characters and strings that
182 consist only of the above characters. It is completely incapable of encoding
183 characters not in the set.
</p>
187 <!-- _______________________________________________________________________ -->
188 <div class=
"doc_subsubsection"> <a name=
"wordalign">Word Alignment
</a></div>
190 <div class=
"doc_text">
192 <p>Occasionally, it is useful to emit zero bits until the bitstream is a
193 multiple of
32 bits. This ensures that the bit position in the stream can be
194 represented as a multiple of
32-bit words.
</p>
199 <!-- ======================================================================= -->
200 <div class=
"doc_subsection"><a name=
"abbrevid">Abbreviation IDs
</a>
203 <div class=
"doc_text">
206 A bitstream is a sequential series of
<a href=
"#blocks">Blocks
</a> and
207 <a href=
"#datarecord">Data Records
</a>. Both of these start with an
208 abbreviation ID encoded as a fixed-bitwidth field. The width is specified by
209 the current block, as described below. The value of the abbreviation ID
210 specifies either a builtin ID (which have special meanings, defined below) or
211 one of the abbreviation IDs defined by the stream itself.
215 The set of builtin abbrev IDs is:
219 <li>0 -
<a href=
"#END_BLOCK">END_BLOCK
</a> - This abbrev ID marks the end of the
221 <li>1 -
<a href=
"#ENTER_SUBBLOCK">ENTER_SUBBLOCK
</a> - This abbrev ID marks the
222 beginning of a new block.
</li>
223 <li>2 -
<a href=
"#DEFINE_ABBREV">DEFINE_ABBREV
</a> - This defines a new
225 <li>3 -
<a href=
"#UNABBREV_RECORD">UNABBREV_RECORD
</a> - This ID specifies the
226 definition of an unabbreviated record.
</li>
229 <p>Abbreviation IDs
4 and above are defined by the stream itself, and specify
230 an
<a href=
"#abbrev_records">abbreviated record encoding
</a>.
</p>
234 <!-- ======================================================================= -->
235 <div class=
"doc_subsection"><a name=
"blocks">Blocks
</a>
238 <div class=
"doc_text">
241 Blocks in a bitstream denote nested regions of the stream, and are identified by
242 a content-specific id number (for example, LLVM IR uses an ID of
12 to represent
243 function bodies). Nested blocks capture the hierachical structure of the data
244 encoded in it, and various properties are associated with blocks as the file is
245 parsed. Block definitions allow the reader to efficiently skip blocks
246 in constant time if the reader wants a summary of blocks, or if it wants to
247 efficiently skip data they do not understand. The LLVM IR reader uses this
248 mechanism to skip function bodies, lazily reading them on demand.
252 When reading and encoding the stream, several properties are maintained for the
253 block. In particular, each block maintains:
257 <li>A current abbrev id width. This value starts at
2, and is set every time a
258 block record is entered. The block entry specifies the abbrev id width for
259 the body of the block.
</li>
261 <li>A set of abbreviations. Abbreviations may be defined within a block, or
262 they may be associated with all blocks of a particular ID.
266 <p>As sub blocks are entered, these properties are saved and the new sub-block
267 has its own set of abbreviations, and its own abbrev id width. When a sub-block
268 is popped, the saved values are restored.
</p>
272 <!-- _______________________________________________________________________ -->
273 <div class=
"doc_subsubsection"> <a name=
"ENTER_SUBBLOCK">ENTER_SUBBLOCK
276 <div class=
"doc_text">
278 <p><tt>[ENTER_SUBBLOCK, blockid
<sub>vbr8
</sub>, newabbrevlen
<sub>vbr4
</sub>,
279 <align32bits
>, blocklen
<sub>32</sub>]
</tt></p>
282 The ENTER_SUBBLOCK abbreviation ID specifies the start of a new block record.
283 The
<tt>blockid
</tt> value is encoded as a
8-bit VBR identifier, and indicates
284 the type of block being entered (which is application specific). The
285 <tt>newabbrevlen
</tt> value is a
4-bit VBR which specifies the
286 abbrev id width for the sub-block. The
<tt>blocklen
</tt> is a
32-bit aligned
287 value that specifies the size of the subblock, in
32-bit words. This value
288 allows the reader to skip over the entire block in one jump.
293 <!-- _______________________________________________________________________ -->
294 <div class=
"doc_subsubsection"> <a name=
"END_BLOCK">END_BLOCK
297 <div class=
"doc_text">
299 <p><tt>[END_BLOCK,
<align32bits
>]
</tt></p>
302 The END_BLOCK abbreviation ID specifies the end of the current block record.
303 Its end is aligned to
32-bits to ensure that the size of the block is an even
304 multiple of
32-bits.
</p>
310 <!-- ======================================================================= -->
311 <div class=
"doc_subsection"><a name=
"datarecord">Data Records
</a>
314 <div class=
"doc_text">
316 Data records consist of a record code and a number of (up to)
64-bit integer
317 values. The interpretation of the code and values is application specific and
318 there are multiple different ways to encode a record (with an unabbrev record
319 or with an abbreviation). In the LLVM IR format, for example, there is a record
320 which encodes the target triple of a module. The code is MODULE_CODE_TRIPLE,
321 and the values of the record are the ascii codes for the characters in the
326 <!-- _______________________________________________________________________ -->
327 <div class=
"doc_subsubsection"> <a name=
"UNABBREV_RECORD">UNABBREV_RECORD
330 <div class=
"doc_text">
332 <p><tt>[UNABBREV_RECORD, code
<sub>vbr6
</sub>, numops
<sub>vbr6
</sub>,
333 op0
<sub>vbr6
</sub>, op1
<sub>vbr6
</sub>, ...]
</tt></p>
335 <p>An UNABBREV_RECORD provides a default fallback encoding, which is both
336 completely general and also extremely inefficient. It can describe an arbitrary
337 record, by emitting the code and operands as vbrs.
</p>
339 <p>For example, emitting an LLVM IR target triple as an unabbreviated record
340 requires emitting the UNABBREV_RECORD abbrevid, a vbr6 for the
341 MODULE_CODE_TRIPLE code, a vbr6 for the length of the string (which is equal to
342 the number of operands), and a vbr6 for each character. Since there are no
343 letters with value less than
32, each letter would need to be emitted as at
344 least a two-part VBR, which means that each letter would require at least
12
345 bits. This is not an efficient encoding, but it is fully general.
</p>
349 <!-- _______________________________________________________________________ -->
350 <div class=
"doc_subsubsection"> <a name=
"abbrev_records">Abbreviated Record
353 <div class=
"doc_text">
355 <p><tt>[
<abbrevid
>, fields...]
</tt></p>
357 <p>An abbreviated record is a abbreviation id followed by a set of fields that
358 are encoded according to the
<a href=
"#abbreviations">abbreviation
359 definition
</a>. This allows records to be encoded significantly more densely
360 than records encoded with the
<a href=
"#UNABBREV_RECORD">UNABBREV_RECORD
</a>
361 type, and allows the abbreviation types to be specified in the stream itself,
362 which allows the files to be completely self describing. The actual encoding
363 of abbreviations is defined below.
368 <!-- ======================================================================= -->
369 <div class=
"doc_subsection"><a name=
"abbreviations">Abbreviations
</a>
372 <div class=
"doc_text">
374 Abbreviations are an important form of compression for bitstreams. The idea is
375 to specify a dense encoding for a class of records once, then use that encoding
376 to emit many records. It takes space to emit the encoding into the file, but
377 the space is recouped (hopefully plus some) when the records that use it are
382 Abbreviations can be determined dynamically per client, per file. Since the
383 abbreviations are stored in the bitstream itself, different streams of the same
384 format can contain different sets of abbreviations if the specific stream does
385 not need it. As a concrete example, LLVM IR files usually emit an abbreviation
386 for binary operators. If a specific LLVM module contained no or few binary
387 operators, the abbreviation does not need to be emitted.
391 <!-- _______________________________________________________________________ -->
392 <div class=
"doc_subsubsection"><a name=
"DEFINE_ABBREV">DEFINE_ABBREV
395 <div class=
"doc_text">
397 <p><tt>[DEFINE_ABBREV, numabbrevops
<sub>vbr5
</sub>, abbrevop0, abbrevop1,
400 <p>An abbreviation definition consists of the DEFINE_ABBREV abbrevid followed
401 by a VBR that specifies the number of abbrev operands, then the abbrev
402 operands themselves. Abbreviation operands come in three forms. They all start
403 with a single bit that indicates whether the abbrev operand is a literal operand
404 (when the bit is
1) or an encoding operand (when the bit is
0).
</p>
407 <li>Literal operands -
<tt>[
1<sub>1</sub>, litvalue
<sub>vbr8
</sub>]
</tt> -
408 Literal operands specify that the value in the result
409 is always a single specific value. This specific value is emitted as a vbr8
410 after the bit indicating that it is a literal operand.
</li>
411 <li>Encoding info without data -
<tt>[
0<sub>1</sub>, encoding
<sub>3</sub>]
</tt>
412 - Operand encodings that do not have extra data are just emitted as their code.
414 <li>Encoding info with data -
<tt>[
0<sub>1</sub>, encoding
<sub>3</sub>,
415 value
<sub>vbr5
</sub>]
</tt> - Operand encodings that do have extra data are
416 emitted as their code, followed by the extra data.
420 <p>The possible operand encodings are:
</p>
423 <li>1 - Fixed - The field should be emitted as a
<a
424 href=
"#fixedwidth">fixed-width value
</a>, whose width
425 is specified by the encoding operand.
</li>
426 <li>2 - VBR - The field should be emitted as a
<a
427 href=
"#variablewidth">variable-width value
</a>, whose width
428 is specified by the encoding operand.
</li>
429 <li>3 - Array - This field is an array of values. The element type of the array
430 is specified by the next encoding operand.
</li>
431 <li>4 - Char6 - This field should be emitted as a
<a href=
"#char6">char6-encoded
435 <p>For example, target triples in LLVM modules are encoded as a record of the
436 form
<tt>[TRIPLE, 'a', 'b', 'c', 'd']
</tt>. Consider if the bitstream emitted
437 the following abbrev entry:
</p>
440 <li><tt>[
0, Fixed,
4]
</tt></li>
441 <li><tt>[
0, Array]
</tt></li>
442 <li><tt>[
0, Char6]
</tt></li>
445 <p>When emitting a record with this abbreviation, the above entry would be
448 <p><tt>[
4<sub>abbrevwidth
</sub>,
2<sub>4</sub>,
4<sub>vbr6
</sub>,
449 0<sub>6</sub>,
1<sub>6</sub>,
2<sub>6</sub>,
3<sub>6</sub>]
</tt></p>
451 <p>These values are:
</p>
454 <li>The first value,
4, is the abbreviation ID for this abbreviation.
</li>
455 <li>The second value,
2, is the code for TRIPLE in LLVM IR files.
</li>
456 <li>The third value,
4, is the length of the array.
</li>
457 <li>The rest of the values are the char6 encoded values for
"abcd".
</li>
460 <p>With this abbreviation, the triple is emitted with only
37 bits (assuming a
461 abbrev id width of
3). Without the abbreviation, significantly more space would
462 be required to emit the target triple. Also, since the TRIPLE value is not
463 emitted as a literal in the abbreviation, the abbreviation can also be used for
464 any other string value.
469 <!-- ======================================================================= -->
470 <div class=
"doc_subsection"><a name=
"stdblocks">Standard Blocks
</a>
473 <div class=
"doc_text">
476 In addition to the basic block structure and record encodings, the bitstream
477 also defines specific builtin block types. These block types specify how the
478 stream is to be decoded or other metadata. In the future, new standard blocks
484 <!-- _______________________________________________________________________ -->
485 <div class=
"doc_subsubsection"><a name=
"BLOCKINFO">#
0 - BLOCKINFO
488 <div class=
"doc_text">
490 <p>The BLOCKINFO block allows the description of metadata for other blocks. The
491 currently specified records are:
</p>
494 <li><tt>[SETBID (#
1), blockid]
</tt></li>
495 <li><tt>[DEFINE_ABBREV, ...]
</tt></li>
499 The SETBID record indicates which block ID is being described. The standard
500 DEFINE_ABBREV record specifies an abbreviation. The abbreviation is associated
501 with the record ID, and any records with matching ID automatically get the
507 <!-- *********************************************************************** -->
508 <div class=
"doc_section"> <a name=
"llvmir">LLVM IR Encoding
</a></div>
509 <!-- *********************************************************************** -->
511 <div class=
"doc_text">
513 <p>LLVM IR is encoded into a bitstream by defining blocks and records. It uses
514 blocks for things like constant pools, functions, symbol tables, etc. It uses
515 records for things like instructions, global variable descriptors, type
516 descriptions, etc. This document does not describe the set of abbreviations
517 that the writer uses, as these are fully self-described in the file, and the
518 reader is not allowed to build in any knowledge of this.
</p>
522 <!-- ======================================================================= -->
523 <div class=
"doc_subsection"><a name=
"basics">Basics
</a>
526 <!-- _______________________________________________________________________ -->
527 <div class=
"doc_subsubsection"><a name=
"ir_magic">LLVM IR Magic Number
</a></div>
529 <div class=
"doc_text">
532 The magic number for LLVM IR files is:
535 <p><tt>['B'
<sub>8</sub>, 'C'
<sub>8</sub>,
0x0<sub>4</sub>,
0xC<sub>4</sub>,
536 0xE<sub>4</sub>,
0xD<sub>4</sub>]
</tt></p>
538 <p>When viewed as bytes, this is
"BC 0xC0DE".
</p>
542 <!-- _______________________________________________________________________ -->
543 <div class=
"doc_subsubsection"><a name=
"ir_signed_vbr">Signed VBRs
</a></div>
545 <div class=
"doc_text">
548 <a href=
"#variablewidth">Variable Width Integers
</a> are an efficient way to
549 encode arbitrary sized unsigned values, but is an extremely inefficient way to
550 encode signed values (as signed values are otherwise treated as maximally large
551 unsigned values).
</p>
553 <p>As such, signed vbr values of a specific width are emitted as follows:
</p>
556 <li>Positive values are emitted as vbrs of the specified width, but with their
557 value shifted left by one.
</li>
558 <li>Negative values are emitted as vbrs of the specified width, but the negated
559 value is shifted left by one, and the low bit is set.
</li>
562 <p>With this encoding, small positive and small negative values can both be
563 emitted efficiently.
</p>
568 <!-- _______________________________________________________________________ -->
569 <div class=
"doc_subsubsection"><a name=
"ir_blocks">LLVM IR Blocks
</a></div>
571 <div class=
"doc_text">
574 LLVM IR is defined with the following blocks:
578 <li>8 - MODULE_BLOCK - This is the top-level block that contains the
579 entire module, and describes a variety of per-module information.
</li>
580 <li>9 - PARAMATTR_BLOCK - This enumerates the parameter attributes.
</li>
581 <li>10 - TYPE_BLOCK - This describes all of the types in the module.
</li>
582 <li>11 - CONSTANTS_BLOCK - This describes constants for a module or
584 <li>12 - FUNCTION_BLOCK - This describes a function body.
</li>
585 <li>13 - TYPE_SYMTAB_BLOCK - This describes the type symbol table.
</li>
586 <li>14 - VALUE_SYMTAB_BLOCK - This describes a value symbol table.
</li>
591 <!-- ======================================================================= -->
592 <div class=
"doc_subsection"><a name=
"MODULE_BLOCK">MODULE_BLOCK Contents
</a>
595 <div class=
"doc_text">
603 <!-- *********************************************************************** -->
605 <address> <a href=
"http://jigsaw.w3.org/css-validator/check/referer"><img
606 src=
"http://jigsaw.w3.org/css-validator/images/vcss" alt=
"Valid CSS!"></a>
607 <a href=
"http://validator.w3.org/check/referer"><img
608 src=
"http://www.w3.org/Icons/valid-html401" alt=
"Valid HTML 4.01!"></a>
609 <a href=
"mailto:sabre@nondot.org">Chris Lattner
</a><br>
610 <a href=
"http://llvm.org">The LLVM Compiler Infrastructure
</a><br>
611 Last modified: $Date$