From e9006fe14254019ef9ccc7a0742aed82c81acb89 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 29 Oct 2009 04:25:46 +0000 Subject: [PATCH] greatly improve the LLVM IR bitcode encoding documentation, patch by Peter Housel! llvm-svn: 85479 --- llvm/docs/BitCodeFormat.html | 499 +++++++++++++++++++++++++++++++---- 1 file changed, 451 insertions(+), 48 deletions(-) diff --git a/llvm/docs/BitCodeFormat.html b/llvm/docs/BitCodeFormat.html index 7296ad138d02..655d7f69b558 100644 --- a/llvm/docs/BitCodeFormat.html +++ b/llvm/docs/BitCodeFormat.html @@ -27,6 +27,15 @@
  • LLVM IR Encoding
    1. Basics
    2. +
    3. MODULE_BLOCK Contents
    4. +
    5. PARAMATTR_BLOCK Contents
    6. +
    7. TYPE_BLOCK Contents
    8. +
    9. CONSTANTS_BLOCK Contents
    10. +
    11. FUNCTION_BLOCK Contents
    12. +
    13. TYPE_SYMTAB_BLOCK Contents
    14. +
    15. VALUE_SYMTAB_BLOCK Contents
    16. +
    17. METADATA_BLOCK Contents
    18. +
    19. METADATA_ATTACHMENT Contents
  • @@ -220,7 +229,7 @@ A bitstream is a sequential series of Blocks and abbreviation ID encoded as a fixed-bitwidth field. The width is specified by the current block, as described below. The value of the abbreviation ID specifies either a builtin ID (which have special meanings, defined below) or -one of the abbreviation IDs defined by the stream itself. +one of the abbreviation IDs defined for the current block by the stream itself.

    @@ -258,7 +267,7 @@ application specific. Nested blocks capture the hierarchical structure of the da encoded in it, and various properties are associated with blocks as the file is parsed. Block definitions allow the reader to efficiently skip blocks in constant time if the reader wants a summary of blocks, or if it wants to -efficiently skip data they do not understand. The LLVM IR reader uses this +efficiently skip data it does not understand. The LLVM IR reader uses this mechanism to skip function bodies, lazily reading them on demand.

    @@ -268,7 +277,8 @@ block. In particular, each block maintains:

      -
    1. A current abbrev id width. This value starts at 2, and is set every time a +
    2. A current abbrev id width. This value starts at 2 at the beginning of + the stream, and is set every time a block record is entered. The block entry specifies the abbrev id width for the body of the block.
    3. @@ -335,13 +345,14 @@ an even multiple of 32-bits.

      -Data records consist of a record code and a number of (up to) 64-bit integer -values. The interpretation of the code and values is application specific and -there are multiple different ways to encode a record (with an unabbrev record or -with an abbreviation). In the LLVM IR format, for example, there is a record +Data records consist of a record code and a number of (up to) 64-bit +integer values. The interpretation of the code and values is +application specific and may vary between different block types. +Records can be encoded either using an unabbrev record, or with an +abbreviation. In the LLVM IR format, for example, there is a record which encodes the target triple of a module. The code is -MODULE_CODE_TRIPLE, and the values of the record are the ASCII codes -for the characters in the string. +MODULE_CODE_TRIPLE, and the values of the record are the +ASCII codes for the characters in the string.

      @@ -358,7 +369,7 @@ Encoding

      An UNABBREV_RECORD provides a default fallback encoding, which is both completely general and extremely inefficient. It can describe an arbitrary -record by emitting the code and operands as vbrs. +record by emitting the code and operands as VBRs.

      @@ -391,6 +402,11 @@ allows the files to be completely self describing. The actual encoding of abbreviations is defined below.

      +

      The record code, which is the first field of an abbreviated record, +may be encoded in the abbreviation definition (as a literal +operand) or supplied in the abbreviated record (as a Fixed or VBR +operand value).

      + @@ -409,8 +425,9 @@ emitted.

      Abbreviations can be determined dynamically per client, per file. Because the abbreviations are stored in the bitstream itself, different streams of the same -format can contain different sets of abbreviations if the specific stream does -not need it. As a concrete example, LLVM IR files usually emit an abbreviation +format can contain different sets of abbreviations according to the needs +of the specific stream. +As a concrete example, LLVM IR files usually emit an abbreviation for binary operators. If a specific LLVM module contained no or few binary operators, the abbreviation does not need to be emitted.

      @@ -431,7 +448,8 @@ defined abbreviations in the scope of this block. This definition only exists inside this immediate block — it is not visible in subblocks or enclosing blocks. Abbreviations are implicitly assigned IDs sequentially starting from 4 (the first application-defined abbreviation ID). Any abbreviations defined in a -BLOCKINFO record receive IDs first, in order, followed by any +BLOCKINFO record for the particular block type +receive IDs first, in order, followed by any abbreviations defined within the block itself. Abbreviated data records reference this ID to indicate what abbreviation they are invoking.

      @@ -461,31 +479,32 @@ emitted as their code, followed by the extra data.

      The possible operand encodings are:

      -
        -
      1. Fixed: The field should be emitted as +
          +
        • Fixed (code 1): The field should be emitted as a fixed-width value, whose width is specified by the operand's extra data.
        • -
        • VBR: The field should be emitted as +
        • VBR (code 2): The field should be emitted as a variable-width value, whose width is specified by the operand's extra data.
        • -
        • Array: This field is an array of values. The array operand - has no extra data, but expects another operand to follow it which indicates +
        • Array (code 3): This field is an array of values. The array operand + has no extra data, but expects another operand to follow it, indicating the element type of the array. When reading an array in an abbreviated record, the first integer is a vbr6 that indicates the array length, followed by the encoded elements of the array. An array may only occur as the last operand of an abbreviation (except for the one final operand that gives the array's type).
        • -
        • Char6: This field should be emitted as +
        • Char6 (code 4): This field should be emitted as a char6-encoded value. This operand type takes no - extra data.
        • -
        • Blob: This field is emitted as a vbr6, followed by padding to a + extra data. Char6 encoding is normally used as an array element type. +
        • +
        • Blob (code 5): This field is emitted as a vbr6, followed by padding to a 32-bit boundary (for alignment) and an array of 8-bit objects. The array of bytes is further followed by tail padding to ensure that its total length is a multiple of 4 bytes. This makes it very efficient for the reader to decode the data without having to make a copy of it: it can use a pointer to the data in the mapped in file and poke directly at it. A blob may only occur as the last operand of an abbreviation.
        • -
      +

      For example, target triples in LLVM modules are encoded as a record of the @@ -517,7 +536,7 @@ as:

      1. The first value, 4, is the abbreviation ID for this abbreviation.
      2. -
      3. The second value, 2, is the code for TRIPLE in LLVM IR files.
      4. +
      5. The second value, 2, is the record code for TRIPLE records within LLVM IR file MODULE_BLOCK blocks.
      6. The third value, 4, is the length of the array.
      7. The rest of the values are the char6 encoded values for "abcd".
      8. @@ -541,7 +560,7 @@ used for any other string value.

        In addition to the basic block structure and record encodings, the bitstream -also defines specific builtin block types. These block types specify how the +also defines specific built-in block types. These block types specify how the stream is to be decoded or other metadata. In the future, new standard blocks may be added. Block IDs 0-7 are reserved for standard blocks.

        @@ -569,7 +588,7 @@ blocks. The currently specified records are:

        -The SETBID record indicates which block ID is being +The SETBID record (code 1) indicates which block ID is being described. SETBID records can occur multiple times throughout the block to change which block ID is being described. There must be a SETBID record prior to any other records. @@ -584,13 +603,13 @@ in BLOCKINFO blocks receive abbreviation IDs as described in DEFINE_ABBREV.

        -

        The BLOCKNAME can optionally occur in this block. The elements of -the record are the bytes for the string name of the block. llvm-bcanalyzer uses +

        The BLOCKNAME record (code 2) can optionally occur in this block. The elements of +the record are the bytes of the string name of the block. llvm-bcanalyzer can use this to dump out bitcode files symbolically.

        -

        The SETRECORDNAME record can optionally occur in this block. The -first entry is a record ID number and the rest of the elements of the record are -the bytes for the string name of the record. llvm-bcanalyzer uses +

        The SETRECORDNAME record (code 3) can also optionally occur in this block. The +first operand value is a record ID number, and the rest of the elements of the record are +the bytes for the string name of the record. llvm-bcanalyzer can use this to dump out bitcode files symbolically.

        @@ -626,7 +645,7 @@ Each of the fields are 32-bit fields stored in little endian form (as with the rest of the bitcode file fields). The Magic number is always 0x0B17C0DE and the version is currently always 0. The Offset field is the offset in bytes to the start of the bitcode stream in the file, and -the Size field is a size in bytes of the stream. CPUType is a target-specific +the Size field is the size in bytes of the stream. CPUType is a target-specific value that can be used to encode the CPU of the target.

        @@ -681,26 +700,28 @@ When combined with the bitcode magic number and viewed as bytes, this is

        -Variable Width Integers are an efficient way to -encode arbitrary sized unsigned values, but is an extremely inefficient way to -encode signed values (as signed values are otherwise treated as maximally large -unsigned values). +Variable Width Integer encoding is an efficient way to +encode arbitrary sized unsigned values, but is an extremely inefficient for +encoding signed values, as signed values are otherwise treated as maximally large +unsigned values.

        -As such, signed vbr values of a specific width are emitted as follows: +As such, signed VBR values of a specific width are emitted as follows:

          -
        • Positive values are emitted as vbrs of the specified width, but with their +
        • Positive values are emitted as VBRs of the specified width, but with their value shifted left by one.
        • -
        • Negative values are emitted as vbrs of the specified width, but the negated +
        • Negative values are emitted as VBRs of the specified width, but the negated value is shifted left by one, and the low bit is set.

        -With this encoding, small positive and small negative values can both be emitted -efficiently. +With this encoding, small positive and small negative values can both +be emitted efficiently. Signed VBR encoding is used in +CST_CODE_INTEGER and CST_CODE_WIDE_INTEGER records +within CONSTANTS_BLOCK blocks.

        @@ -716,21 +737,23 @@ LLVM IR is defined with the following blocks:

          -
        • 8 — MODULE_BLOCK — This is the top-level block that +
        • 8 — MODULE_BLOCK — This is the top-level block that contains the entire module, and describes a variety of per-module information.
        • -
        • 9 — PARAMATTR_BLOCK — This enumerates the parameter +
        • 9 — PARAMATTR_BLOCK — This enumerates the parameter attributes.
        • -
        • 10 — TYPE_BLOCK — This describes all of the types in +
        • 10 — TYPE_BLOCK — This describes all of the types in the module.
        • -
        • 11 — CONSTANTS_BLOCK — This describes constants for a +
        • 11 — CONSTANTS_BLOCK — This describes constants for a module or function.
        • -
        • 12 — FUNCTION_BLOCK — This describes a function +
        • 12 — FUNCTION_BLOCK — This describes a function body.
        • -
        • 13 — TYPE_SYMTAB_BLOCK — This describes the type symbol +
        • 13 — TYPE_SYMTAB_BLOCK — This describes the type symbol table.
        • -
        • 14 — VALUE_SYMTAB_BLOCK — This describes a value symbol +
        • 14 — VALUE_SYMTAB_BLOCK — This describes a value symbol table.
        • +
        • 15 — METADATA_BLOCK — This describes metadata items.
        • +
        • 16 — METADATA_ATTACHMENT — This contains records associating metadata with function instruction values.
        @@ -741,7 +764,387 @@ LLVM IR is defined with the following blocks:
        -

        +

        The MODULE_BLOCK block (id 8) is the top-level block for LLVM +bitcode files, and each bitcode file must contain exactly one. In +addition to records (described below) containing information +about the module, a MODULE_BLOCK block may contain the +following sub-blocks: +

        + + + +
        + + + + +
        + +

        [VERSION, version#]

        + +

        The VERSION record (code 1) contains a single value +indicating the format version. Only version 0 is supported at this +time.

        +
        + + + + +
        +

        [TRIPLE, ...string...]

        + +

        The TRIPLE record (code 2) contains a variable number of +values representing the bytes of the target triple +specification string.

        +
        + + + + +
        +

        [DATALAYOUT, ...string...]

        + +

        The DATALAYOUT record (code 3) contains a variable number of +values representing the bytes of the target datalayout +specification string.

        +
        + + + + +
        +

        [ASM, ...string...]

        + +

        The ASM record (code 4) contains a variable number of +values representing the bytes of module asm strings, with +individual assembly blocks separated by newline (ASCII 10) characters.

        +
        + + + + +
        +

        [SECTIONNAME, ...string...]

        + +

        The SECTIONNAME record (code 5) contains a variable number +of values representing the bytes of a single section name +string. There should be one SECTIONNAME record for each +section name referenced (e.g., in global variable or function +section attributes) within the module. These records can be +referenced by the 1-based index in the section fields of +GLOBALVAR or FUNCTION records.

        +
        + + + + +
        +

        [DEPLIB, ...string...]

        + +

        The DEPLIB record (code 6) contains a variable number of +values representing the bytes of a single dependent library name +string, one of the libraries mentioned in a deplibs +declaration. There should be one DEPLIB record for each +library name referenced.

        +
        + + + + +
        +

        [GLOBALVAR, pointer type, isconst, initid, linkage, alignment, section, visibility, threadlocal]

        + +

        The GLOBALVAR record (code 7) marks the declaration or +definition of a global variable. The operand fields are:

        + +
          +
        • pointer type: The type index of the pointer type used to point to +this global variable
        • + +
        • isconst: Non-zero if the variable is treated as constant within +the module, or zero if it is not
        • + +
        • initid: If non-zero, the value index of the initializer for this +variable, plus 1.
        • + +
        • linkage: An encoding of the linkage +type for this variable: +
            +
          • external: code 0
          • +
          • weak: code 1
          • +
          • appending: code 2
          • +
          • internal: code 3
          • +
          • linkonce: code 4
          • +
          • dllimport: code 5
          • +
          • dllexport: code 6
          • +
          • extern_weak: code 7
          • +
          • common: code 8
          • +
          • private: code 9
          • +
          • weak_odr: code 10
          • +
          • linkonce_odr: code 11
          • +
          • available_externally: code 12
          • +
          • linker_private: code 13
          • +
          +
        • + +
        • alignment: The logarithm base 2 of the variable's requested +alignment, plus 1
        • + +
        • section: If non-zero, the 1-based section index in the +table of MODULE_CODE_SECTIONNAME +entries.
        • + +
        • visibility: If present, an +encoding of the visibility of this variable: +
            +
          • default: code 0
          • +
          • hidden: code 1
          • +
          • protected: code 2
          • +
          +
        • + +
        • threadlocal: If present and non-zero, indicates that the variable +is thread_local
        • + +
        +
        + + + + +
        + +

        [FUNCTION, type, callingconv, isproto, linkage, paramattr, alignment, section, visibility, gc]

        + +

        The FUNCTION record (code 8) marks the declaration or +definition of a function. The operand fields are:

        + +
          +
        • type: The type index of the function type describing this function
        • + +
        • callingconv: The calling convention number: +
            +
          • ccc: code 0
          • +
          • fastcc: code 8
          • +
          • coldcc: code 9
          • +
          • x86_stdcallcc: code 64
          • +
          • x86_fastcallcc: code 65
          • +
          • arm_apcscc: code 66
          • +
          • arm_aapcscc: code 67
          • +
          • arm_aapcs_vfpcc: code 68
          • +
          +
        • + +
        • isproto: Non-zero if this entry represents a declaration +rather than a definition
        • + +
        • linkage: An encoding of the linkage type +for this function
        • + +
        • paramattr: If nonzero, the 1-based parameter attribute index +into the table of PARAMATTR_CODE_ENTRY +entries.
        • + +
        • alignment: The logarithm base 2 of the function's requested +alignment, plus 1
        • + +
        • section: If non-zero, the 1-based section index in the +table of MODULE_CODE_SECTIONNAME +entries.
        • + +
        • visibility: An encoding of the visibility + of this function
        • + +
        • gc: If present and nonzero, the 1-based garbage collector +index in the table of +MODULE_CODE_GCNAME entries.
        • +
        +
        + + + + +
        + +

        [ALIAS, alias type, aliasee val#, linkage, visibility]

        + +

        The ALIAS record (code 9) marks the definition of an +alias. The operand fields are

        + +
          +
        • alias type: The type index of the alias
        • + +
        • aliasee val#: The value index of the aliased value
        • + +
        • linkage: An encoding of the linkage type +for this alias
        • + +
        • visibility: If present, an encoding of the +visibility of the alias
        • + +
        +
        + + + + +
        +

        [PURGEVALS, numvals]

        + +

        The PURGEVALS record (code 10) resets the module-level +value list to the size given by the single operand value. Module-level +value list items are added by GLOBALVAR, FUNCTION, +and ALIAS records. After a PURGEVALS record is seen, +new value indices will start from the given numvals value.

        +
        + + + + +
        +

        [GCNAME, ...string...]

        + +

        The GCNAME record (code 11) contains a variable number of +values representing the bytes of a single garbage collector name +string. There should be one GCNAME record for each garbage +collector name referenced in function gc attributes within +the module. These records can be referenced by 1-based index in the gc +fields of FUNCTION records.

        +
        + + + + +
        + +

        The PARAMATTR_BLOCK block (id 9) ... +

        + +
        + + + + + +
        + +

        [ENTRY, paramidx0, attr0, paramidx1, attr1...]

        + +

        The ENTRY record (code 1) ... +

        +
        + + + + +
        + +

        The TYPE_BLOCK block (id 10) ... +

        + +
        + + + + + +
        + +

        The CONSTANTS_BLOCK block (id 11) ... +

        + +
        + + + + + +
        + +

        The FUNCTION_BLOCK block (id 12) ... +

        + +

        In addition to the record types described below, a +FUNCTION_BLOCK block may contain the following sub-blocks: +

        + + + +
        + + + + + +
        + +

        The TYPE_SYMTAB_BLOCK block (id 13) ... +

        + +
        + + + + + +
        + +

        The VALUE_SYMTAB_BLOCK block (id 14) ... +

        + +
        + + + + + +
        + +

        The METADATA_BLOCK block (id 15) ... +

        + +
        + + + + + +
        + +

        The METADATA_ATTACHMENT block (id 16) ...