LLVM Language Reference ManualΒΆ
- Abstract
- Introduction
- Identifiers
- High Level Structure
- Module Structure
- Linkage Types
- Calling Conventions
- Visibility Styles
- DLL Storage Classes
- Thread Local Storage Models
- Structure Types
- Global Variables
- Functions
- Aliases
- Comdats
- Named Metadata
- Parameter Attributes
- Garbage Collector Strategy Names
- Prefix Data
- Prologue Data
- Personality Function
- Attribute Groups
- Function Attributes
- Module-Level Inline Assembly
- Data Layout
- Target Triple
- Pointer Aliasing Rules
- Volatile Memory Accesses
- Memory Model for Concurrent Operations
- Atomic Memory Ordering Constraints
- Fast-Math Flags
- Use-list Order Directives
- Type System
- Constants
- Other Values
- Metadata
- Metadata Nodes and Metadata Strings
- Specialized Metadata Nodes
- DICompileUnit
- DIFile
- DIBasicType
- DISubroutineType
- DIDerivedType
- DICompositeType
- DISubrange
- DIEnumerator
- DITemplateTypeParameter
- DITemplateValueParameter
- DINamespace
- DIGlobalVariable
- DISubprogram
- DILexicalBlock
- DILexicalBlockFile
- DILocation
- DILocalVariable
- DIExpression
- DIObjCProperty
- DIImportedEntity
- ‘
tbaa‘ Metadata - ‘
tbaa.struct‘ Metadata - ‘
noalias‘ and ‘alias.scope‘ Metadata - ‘
fpmath‘ Metadata - ‘
range‘ Metadata - ‘
llvm.loop‘ - ‘
llvm.loop.vectorize‘ and ‘llvm.loop.interleave‘ - ‘
llvm.loop.interleave.count‘ Metadata - ‘
llvm.loop.vectorize.enable‘ Metadata - ‘
llvm.loop.vectorize.width‘ Metadata - ‘
llvm.loop.unroll‘ - ‘
llvm.loop.unroll.count‘ Metadata - ‘
llvm.loop.unroll.disable‘ Metadata - ‘
llvm.loop.unroll.runtime.disable‘ Metadata - ‘
llvm.loop.unroll.full‘ Metadata - ‘
llvm.mem‘ - ‘
llvm.mem.parallel_loop_access‘ Metadata - ‘
llvm.bitsets‘
- Specialized Metadata Nodes
- Metadata Nodes and Metadata Strings
- Module Flags Metadata
- Intrinsic Global Variables
- Instruction Reference
- Terminator Instructions
- Binary Operations
- Bitwise Binary Operations
- Vector Operations
- Aggregate Operations
- Memory Access and Addressing Operations
- Conversion Operations
- ‘
trunc .. to‘ Instruction - ‘
zext .. to‘ Instruction - ‘
sext .. to‘ Instruction - ‘
fptrunc .. to‘ Instruction - ‘
fpext .. to‘ Instruction - ‘
fptoui .. to‘ Instruction - ‘
fptosi .. to‘ Instruction - ‘
uitofp .. to‘ Instruction - ‘
sitofp .. to‘ Instruction - ‘
ptrtoint .. to‘ Instruction - ‘
inttoptr .. to‘ Instruction - ‘
bitcast .. to‘ Instruction - ‘
addrspacecast .. to‘ Instruction
- ‘
- Other Operations
- Intrinsic Functions
- Variable Argument Handling Intrinsics
- Accurate Garbage Collection Intrinsics
- Code Generator Intrinsics
- ‘
llvm.returnaddress‘ Intrinsic - ‘
llvm.frameaddress‘ Intrinsic - ‘
llvm.localescape‘ and ‘llvm.localrecover‘ Intrinsics - ‘
llvm.read_register‘ and ‘llvm.write_register‘ Intrinsics - ‘
llvm.stacksave‘ Intrinsic - ‘
llvm.stackrestore‘ Intrinsic - ‘
llvm.prefetch‘ Intrinsic - ‘
llvm.pcmarker‘ Intrinsic - ‘
llvm.readcyclecounter‘ Intrinsic - ‘
llvm.clear_cache‘ Intrinsic - ‘
llvm.instrprof_increment‘ Intrinsic
- ‘
- Standard C Library Intrinsics
- ‘
llvm.memcpy‘ Intrinsic - ‘
llvm.memmove‘ Intrinsic - ‘
llvm.memset.*‘ Intrinsics - ‘
llvm.sqrt.*‘ Intrinsic - ‘
llvm.powi.*‘ Intrinsic - ‘
llvm.sin.*‘ Intrinsic - ‘
llvm.cos.*‘ Intrinsic - ‘
llvm.pow.*‘ Intrinsic - ‘
llvm.exp.*‘ Intrinsic - ‘
llvm.exp2.*‘ Intrinsic - ‘
llvm.log.*‘ Intrinsic - ‘
llvm.log10.*‘ Intrinsic - ‘
llvm.log2.*‘ Intrinsic - ‘
llvm.fma.*‘ Intrinsic - ‘
llvm.fabs.*‘ Intrinsic - ‘
llvm.minnum.*‘ Intrinsic - ‘
llvm.maxnum.*‘ Intrinsic - ‘
llvm.copysign.*‘ Intrinsic - ‘
llvm.floor.*‘ Intrinsic - ‘
llvm.ceil.*‘ Intrinsic - ‘
llvm.trunc.*‘ Intrinsic - ‘
llvm.rint.*‘ Intrinsic - ‘
llvm.nearbyint.*‘ Intrinsic - ‘
llvm.round.*‘ Intrinsic
- ‘
- Bit Manipulation Intrinsics
- Arithmetic with Overflow Intrinsics
- Specialised Arithmetic Intrinsics
- Half Precision Floating Point Intrinsics
- Debugger Intrinsics
- Exception Handling Intrinsics
- Trampoline Intrinsics
- Masked Vector Load and Store Intrinsics
- Masked Vector Gather and Scatter Intrinsics
- Memory Use Markers
- General Intrinsics
- ‘
llvm.var.annotation‘ Intrinsic - ‘
llvm.ptr.annotation.*‘ Intrinsic - ‘
llvm.annotation.*‘ Intrinsic - ‘
llvm.trap‘ Intrinsic - ‘
llvm.debugtrap‘ Intrinsic - ‘
llvm.stackprotector‘ Intrinsic - ‘
llvm.stackprotectorcheck‘ Intrinsic - ‘
llvm.objectsize‘ Intrinsic - ‘
llvm.expect‘ Intrinsic - ‘
llvm.assume‘ Intrinsic - ‘
llvm.bitset.test‘ Intrinsic - ‘
llvm.donothing‘ Intrinsic
- ‘
- Stack Map Intrinsics
AbstractΒΆ
This document is a reference manual for the LLVM assembly language. LLVM is a Static Single Assignment (SSA) based representation that provides type safety, low-level operations, flexibility, and the capability of representing ‘all’ high-level languages cleanly. It is the common code representation used throughout all phases of the LLVM compilation strategy.
IntroductionΒΆ
The LLVM code representation is designed to be used in three different forms: as an in-memory compiler IR, as an on-disk bitcode representation (suitable for fast loading by a Just-In-Time compiler), and as a human readable assembly language representation. This allows LLVM to provide a powerful intermediate representation for efficient compiler transformations and analysis, while providing a natural means to debug and visualize the transformations. The three different forms of LLVM are all equivalent. This document describes the human readable representation and notation.
The LLVM representation aims to be light-weight and low-level while being expressive, typed, and extensible at the same time. It aims to be a “universal IR” of sorts, by being at a low enough level that high-level ideas may be cleanly mapped to it (similar to how microprocessors are “universal IR’s”, allowing many source languages to be mapped to them). By providing type information, LLVM can be used as the target of optimizations: for example, through pointer analysis, it can be proven that a C automatic variable is never accessed outside of the current function, allowing it to be promoted to a simple SSA value instead of a memory location.
Well-FormednessΒΆ
It is important to note that this document describes ‘well formed’ LLVM assembly language. There is a difference between what the parser accepts and what is considered ‘well formed’. For example, the following instruction is syntactically okay, but not well formed:
%x = add i32 1, %x
because the definition of %x does not dominate all of its uses. The
LLVM infrastructure provides a verification pass that may be used to
verify that an LLVM module is well formed. This pass is automatically
run by the parser after parsing input assembly and by the optimizer
before it outputs bitcode. The violations pointed out by the verifier
pass indicate bugs in transformation passes or input to the parser.
IdentifiersΒΆ
LLVM identifiers come in two basic types: global and local. Global
identifiers (functions, global variables) begin with the '@'
character. Local identifiers (register names, types) begin with the
'%' character. Additionally, there are three different formats for
identifiers, for different purposes:
- Named values are represented as a string of characters with their
prefix. For example,
%foo,@DivisionByZero,%a.really.long.identifier. The actual regular expression used is ‘[%@][-a-zA-Z$._][-a-zA-Z$._0-9]*‘. Identifiers that require other characters in their names can be surrounded with quotes. Special characters may be escaped using"\xx"wherexxis the ASCII code for the character in hexadecimal. In this way, any character can be used in a name value, even quotes themselves. The"\01"prefix can be used on global variables to suppress mangling. - Unnamed values are represented as an unsigned numeric value with
their prefix. For example,
%12,@2,%44. - Constants, which are described in the section Constants below.
LLVM requires that values start with a prefix for two reasons: Compilers don’t need to worry about name clashes with reserved words, and the set of reserved words may be expanded in the future without penalty. Additionally, unnamed identifiers allow a compiler to quickly come up with a temporary variable without having to avoid symbol table conflicts.
Reserved words in LLVM are very similar to reserved words in other
languages. There are keywords for different opcodes (‘add‘,
‘bitcast‘, ‘ret‘, etc...), for primitive type names (‘void‘,
‘i32‘, etc...), and others. These reserved words cannot conflict
with variable names, because none of them start with a prefix character
('%' or '@').
Here is an example of LLVM code to multiply the integer variable
‘%X‘ by 8:
The easy way:
%result = mul i32 %X, 8
After strength reduction:
%result = shl i32 %X, 3
And the hard way:
%0 = add i32 %X, %X ; yields i32:%0
%1 = add i32 %0, %0 ; yields i32:%1
%result = add i32 %1, %1
This last way of multiplying %X by 8 illustrates several important
lexical features of LLVM:
- Comments are delimited with a ‘
;‘ and go until the end of line. - Unnamed temporaries are created when the result of a computation is not assigned to a named value.
- Unnamed temporaries are numbered sequentially (using a per-function incrementing counter, starting with 0). Note that basic blocks and unnamed function parameters are included in this numbering. For example, if the entry basic block is not given a label name and all function parameters are named, then it will get number 0.
It also shows a convention that we follow in this document. When demonstrating instructions, we will follow an instruction with a comment that defines the type and name of value produced.
High Level StructureΒΆ
Module StructureΒΆ
LLVM programs are composed of Module‘s, each of which is a
translation unit of the input programs. Each module consists of
functions, global variables, and symbol table entries. Modules may be
combined together with the LLVM linker, which merges function (and
global variable) definitions, resolves forward declarations, and merges
symbol table entries. Here is an example of the “hello world” module:
; Declare the string constant as a global constant.
@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00"
; External declaration of the puts function
declare i32 @puts(i8* nocapture) nounwind
; Definition of main function
define i32 @main() { ; i32()*
; Convert [13 x i8]* to i8 *...
%cast210 = getelementptr [13 x i8], [13 x i8]* @.str, i64 0, i64 0
; Call puts function to write out the string to stdout.
call i32 @puts(i8* %cast210)
ret i32 0
}
; Named metadata
!0 = !{i32 42, null, !"string"}
!foo = !{!0}
This example is made up of a global variable named
“.str”, an external declaration of the “puts” function, a
function definition for “main” and
named metadata “foo”.
In general, a module is made up of a list of global values (where both functions and global variables are global values). Global values are represented by a pointer to a memory location (in this case, a pointer to an array of char, and a pointer to a function), and have one of the following linkage types.
Linkage TypesΒΆ
All Global Variables and Functions have one of the following types of linkage:
private- Global values with “
private” linkage are only directly accessible by objects in the current module. In particular, linking code into a module with an private global value may cause the private to be renamed as necessary to avoid collisions. Because the symbol is private to the module, all references can be updated. This doesn’t show up in any symbol table in the object file. internal- Similar to private, but the value shows as a local symbol
(
STB_LOCALin the case of ELF) in the object file. This corresponds to the notion of the ‘static‘ keyword in C. available_externally- Globals with “
available_externally” linkage are never emitted into the object file corresponding to the LLVM module. They exist to allow inlining and other optimizations to take place given knowledge of the definition of the global, which is known to be somewhere outside the module. Globals withavailable_externallylinkage are allowed to be discarded at will, and are otherwise the same aslinkonce_odr. This linkage type is only allowed on definitions, not declarations. linkonce- Globals with “
linkonce” linkage are merged with other globals of the same name when linkage occurs. This can be used to implement some forms of inline functions, templates, or other code which must be generated in each translation unit that uses it, but where the body may be overridden with a more definitive definition later. Unreferencedlinkonceglobals are allowed to be discarded. Note thatlinkoncelinkage does not actually allow the optimizer to inline the body of this function into callers because it doesn’t know if this definition of the function is
