llvm Assembly Language Reference Manual |
- Abstract
- Introduction
- Identifiers
- Type System
- Primitive Types
- Type Classifications
- Derived Types
- Array Type
- Function Type
- Pointer Type
- Structure Type
- Packed Type
- High Level Structure
- Module Structure
- Function Structure
- Instruction Reference
- Terminator Instructions
- 'ret' Instruction
- 'br' Instruction
- 'switch' Instruction
- 'invoke' Instruction
- Unary Operations
- 'not' Instruction
- Binary Operations
- 'add' Instruction
- 'sub' Instruction
- 'mul' Instruction
- 'div' Instruction
- 'rem' Instruction
- 'setcc' Instructions
- Bitwise Binary Operations
- 'and' Instruction
- 'or' Instruction
- 'xor' Instruction
- 'shl' Instruction
- 'shr' Instruction
- Memory Access Operations
- 'malloc' Instruction
- 'free' Instruction
- 'alloca' Instruction
- 'getelementptr' Instruction
- 'load' Instruction
- 'store' Instruction
- Other Operations
- 'cast .. to' Instruction
- 'call' Instruction
- 'icall' Instruction
- 'phi' Instruction
- Builtin Functions
- TODO List
- Exception Handling Instructions
- Synchronization Instructions
- Possible Extensions
- 'tailcall' Instruction
- Global Variables
- Explicit Parrellelism
- Related Work
This document describes the LLVM assembly language. LLVM is an SSA based
representation that is a useful midlevel IR, providing type safety, low level
operations, flexibility, and the capability to represent 'all' high level
languages cleanly.
The LLVM code representation is designed to be used in three different forms: as
an in-memory compiler IR, as an on-disk bytecode representation, suitable for
fast loading by a dynamic 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 a light weight and low level while being
expressive, type safe, 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
safety, 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 int 1, %x
...because only a phi node may refer to itself.
The LLVM api provides a verification pass (created by the
createVerifierPass function) 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 bytecode. Often,
violations pointed out by the verifier pass indicate bugs in transformation
passes.
Describe the typesetting conventions here.
LLVM uses three different forms of identifiers, for different purposes:
- Numeric constants are represented as you would expect: 12, -3 123.421, etc.
- Named values are represented as a string of characters with a '%' prefix. For example, %foo, %DivisionByZero, %a.really.long.identifier. The actual regular expression used is '%[a-zA-Z$._][a-zA-Z$._0-9]*'.
- Unnamed values are represented as an unsigned numeric value with a '%' prefix. For example, %12, %2, %44.
LLVM requires the values start with a '%' sign 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',
'cast', 'ret',
etc...), for primitive type names ('void',
'uint', etc...), and others. These reserved
words cannot conflict with variable names, because none of them start with a '%'
character.
Here is an example of LLVM code to multiply the integer variable '%X'
by 8:
The easy way:
%result = mul int %X, 8
After strength reduction:
%result = shl int %X, ubyte 3
And the hard way:
add int %X, %X ; yields {int}:%0
add int %0, %0 ; yields {int}:%1
%result = add int %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
...and it also show 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. Comments are shown in italic
text.
The LLVM type system is critical to the overall usefulness of the language and
runtime. Being strongly typed enables a number of optimizations to be performed
on the IR directly, without having to do extra analyses on the side before the
transformation. A strong type system makes it easier to read the generated code
and enables novel analyses and transformations that are not feasible to perform
on normal three address code representations.
The assembly language form for the type system was heavily influenced by the
type problems in the C language1.
The primitive types are the fundemental building blocks of the LLVM system. The
current set of primitive types are as follows:
void | No value |
ubyte | Unsigned 8 bit value |
ushort | Unsigned 16 bit value |
uint | Unsigned 32 bit value |
ulong | Unsigned 64 bit value |
float | 32 bit floating point value |
label | Branch destination |
|
bool | True or False value |
sbyte | Signed 8 bit value |
short | Signed 16 bit value |
int | Signed 32 bit value |
long | Signed 64 bit value |
double | 64 bit floating point value |
|
Type Classifications
These different primitive types fall into a few useful classifications:
signed | sbyte, short, int, long, float, double |
unsigned | ubyte, ushort, uint, ulong |
integral | ubyte, sbyte, ushort, short, uint, int, ulong, long |
floating point | float, double |
first class | bool, ubyte, sbyte, ushort, short, uint, int, ulong, long, float, double |
The real power in LLVM comes from the derived types in the system. This is what
allows a programmer to represent arrays, functions, pointers, and other useful
types. Note that these derived types may be recursive: For example, it is
possible to have a two dimensional array.
Array Type
Overview:
The array type is a very simple derived type that arranges elements sequentially
in memory. The array type requires a size (number of elements) and an
underlying data type.
Syntax:
[<# elements> x <elementtype>]
The number of elements is a constant integer value, elementtype may be any time
with a size.
Examples:
[40 x int ]: Array of 40 integer values.
[41 x int ]: Array of 41 integer values.
[40 x uint]: Array of 40 unsigned integer values.
Here are some examples of multidimensional arrays:
[3 x [4 x int]] | : 3x4 array integer values. |
[12 x [10 x float]] | : 2x10 array of single precision floating point values. |
[2 x [3 x [4 x uint]]] | : 2x3x4 array of unsigned integer values. |
Function Type
Overview:
The function type can be thought of as a function signature. It consists of a
return type and a list of formal parameter types. Function types are usually
used when to build virtual function tables (which are structures of pointers to
functions), for indirect function calls, and when defining a function.
Syntax:
<returntype> (<parameter list>)
Where '<parameter list>' is a comma seperated list of type
specifiers. Optionally, the parameter list may include a type ...,
which indicates that the function takes a variable number of arguments. Note
that there currently is no way to define a function in LLVM that takes a
variable number of arguments, but it is possible to call a function that
is vararg.
Examples:
int (int) | : function taking an int, returning
an int |
float (int, int *) * | : Pointer
to a function that takes an int and a pointer
to int, returning float. |
int (sbyte *, ...) | : A vararg function that takes at
least one pointer to sbyte (signed char in C),
which returns an integer. This is the signature for printf in
LLVM. |
Structure Type
Overview:
The structure type is used to represent a collection of data members together in memory. Although the runtime is allowed to lay out the data members any way that it would like, they are guaranteed to be "close" to each other.
Structures are accessed using 'load and 'store' by getting a pointer to a field with the 'getelementptr' instruction.
Syntax:
{ <type list> }
Examples:
{ int, int, int } | : a triple of three int
values |
{ float, int (int *) * } | : A pair, where the first
element is a float and the second element is a pointer to a function that takes
an int, returning an int. |
Pointer Type
Overview:
As in many languages, the pointer type represents a pointer or reference to
another object, which must live in memory.
Syntax:
<type> *
Examples:
talk about the elements of a module: constant pool and function list.
talk about the optional constant pool
talk about how basic blocks delinate labels
talk about how basic blocks end with terminators
List all of the instructions, list valid types that they accept. Tell what they
do and stuff also.
As was mentioned previously, every basic block
in a program ends with a "Terminator" instruction. All of these terminator
instructions yield a 'void' value: they produce control flow, not
values.
There are four different terminator instructions: the 'ret' instruction, the 'br' instruction, the 'switch' instruction, and the 'invoke' instruction.
'ret' Instruction
ret <type> <value> ; Return a value from a non-void function
ret void ; Return from void function
Overview:
The 'ret' instruction is used to return control flow (and optionally a
value) from a function, back to the caller.
There are two forms of the 'ret' instructruction: one that returns a
value and then causes control flow, and one that just causes control flow to
occur.
Arguments:
The 'ret' instruction may return any 'first
class' type. Notice that a function is not well
formed if there exists a 'ret' instruction inside of the function
that returns a value that does not match the return type of the function.
Semantics:
When the 'ret' instruction is executed, control flow returns back to
the calling function's context. If the instruction returns a value, that value
shall be propogated into the calling function's data space.
Example:
ret int 5 ; Return an integer value of 5
ret void ; Return from a void function
'br' Instruction
br bool <cond>, label <iftrue>, label <iffalse>
br label <dest> ; Unconditional branch
Overview:
The 'br' instruction is used to cause control flow to transfer to a
different basic block in the current function. There are two forms of this
instruction, corresponding to a conditional branch and an unconditional
branch.
Arguments:
The conditional branch form of the 'br' instruction takes a single
'bool' value and two 'label' values. The unconditional form
of the 'br' instruction takes a single 'label' value as a
target.
Semantics:
Upon execution of a conditional 'br' instruction, the 'bool'
argument is evaluated. If the value is true, control flows to the
'iftrue' 'label' argument. If "cond" is false,
control flows to the 'iffalse' 'label' argument.
Example:
Test:
%cond = seteq int %a, %b
br bool %cond, label %IfEqual, label %IfUnequal
IfEqual:
ret bool true
IfUnequal:
ret bool false
'switch' Instruction
; Definitions for lookup indirect branch
%switchtype = type [<anysize> x { uint, label }]
; Lookup indirect branch
switch uint <value>, label <defaultdest>, %switchtype <switchtable>
; Indexed indirect branch
switch uint <idxvalue>, label <defaultdest>, [<anysize> x label] <desttable>
Overview:
The 'switch' instruction is used to transfer control flow to one of
several different places. It is a generalization of the 'br'
instruction, allowing a branch to occur to one of many possible destinations.
The 'switch' statement supports two different styles of indirect
branching: lookup branching and indexed branching. Lookup branching is
generally useful if the values to switch on are spread far appart, where index
branching is useful if the values to switch on are generally dense.
The two different forms of the 'switch' statement are simple hints to
the underlying virtual machine implementation. For example, a virtual machine
may choose to implement a small indirect branch table as a series of predicated
comparisons: if it is faster for the target architecture.
Arguments:
The lookup form of the 'switch' instruction uses three parameters: a
'uint' comparison value 'value', a default 'label'
destination, and an array of pairs of comparison value constants and
'label's. The sized array must be a constant value.
The indexed form of the 'switch' instruction uses three parameters: an
'uint' index value, a default 'label' and a sized array of
'label's. The 'dests' array must be a constant array.
Semantics:
The lookup style switch statement specifies a table of values and destinations.
When the 'switch' instruction is executed, this table is searched for
the given value. If the value is found, the corresponding destination is
branched to.
The index branch form simply looks up a label element directly in a table and
branches to it.
In either case, the compiler knows the static size of the array, because it is
provided as part of the constant values type.
Example:
; Emulate a conditional br instruction
%Val = cast bool %value to uint
switch uint %Val, label %truedest, [1 x label] [label %falsedest ]
; Emulate an unconditional br instruction
switch uint 0, label %dest, [ 0 x label] [ ]
; Implement a jump table using the constant pool:
void "testmeth"(int %arg0)
%switchdests = [3 x label] [ label %onzero, label %onone, label %ontwo ]
begin
...
switch uint %val, label %otherwise, [3 x label] %switchdests...
...
end
; Implement the equivilent jump table directly:
switch uint %val, label %otherwise, [3 x label] [ label %onzero,
label %onone,
label %ontwo ]
'invoke' Instruction
<result> = invoke <ptr to function ty> %<function ptr val>(<function args>)
to label <normal label> except label <exception label>
Overview:
The 'invoke' instruction is used to cause control
flow to transfer to a specified function, with the possibility of control flow
transfer to either the 'normal label' label or the 'exception
label'. The 'call' instruction is closely
related, but guarantees that control flow either never returns from the called
function, or that it returns to the instruction succeeding the 'call' instruction.
Arguments:
This instruction requires several arguments:
- 'ptr to function ty': shall be the signature of the pointer to
function value being invoked. In most cases, this is a direct method
invocation, but indirect invoke's are just as possible, branching off
an arbitrary pointer to function value.
- 'function ptr val': An LLVM value containing a pointer to a
function to be invoked.
- 'function args': argument list whose types match the function
signature argument types.
- 'normal label': the label reached when the called function executes
a 'ret' instruction.
- 'exception label': the label reached when an exception is thrown.
Semantics:
This instruction is designed to operate as a standard 'call' instruction in most regards. The primary difference is that it assiciates a label with the function invocation that may be accessed via the runtime library provided by the execution environment. This instruction is used in languages with destructors to ensure that proper cleanup is performed in the case of either a longjmp or a thrown exception. Additionally, this is important for implementation of 'catch' clauses in high-level languages that support them.
For a more comprehensive explanation of this instruction look in the llvm/docs/2001-05-18-ExceptionHandling.txt document.
Example:
%retval = invoke int %Test(int 15)
to label %Continue except label %TestCleanup ; {int}:retval set
'not' Instruction
<result> = not <ty> <var> ; yields {ty}:result
Overview:
The 'not' instruction returns the logical inverse of its operand.
Arguments:
The single argument to 'not' must be of of integral type.
Semantics:
The 'not' instruction returns the logical inverse of an integral type.
<result> = xor bool true, <var> ; yields {bool}:result
Example:
%x = not int 1 ; {int}:x is now equal to 0
%x = not bool true ; {bool}:x is now equal to false
'add' Instruction
<result> = add <ty> <var1>, <var2> ; yields {ty}:result
Overview:
The 'add' instruction returns the sum of its two operands.
Arguments:
The two arguments to the 'add' instruction must be either integral or floating point values. Both arguments must have identical types.
Semantics:
...
Example:
<result> = add int 4, %var ; yields {int}:result = 4 + %var
'sub' Instruction
<result> = sub <ty> <var1>, <var2> ; yields {ty}:result
Overview:
The 'sub' instruction returns the difference of its two operands.
Note that the 'sub' instruction is used to represent the 'neg'
instruction present in most other intermediate representations.
Arguments:
The two arguments to the 'sub' instruction must be either integral or floating point
values. Both arguments must have identical types.
Semantics:
...
Example:
<result> = sub int 4, %var ; yields {int}:result = 4 - %var
<result> = sub int 0, %val ; yields {int}:result = -%var
'mul' Instruction
<result> = mul <ty> <var1>, <var2> ; yields {ty}:result
Overview:
The 'mul' instruction returns the product of its two operands.
Arguments:
The two arguments to the 'mul' instruction must be either integral or floating point values. Both arguments must have identical types.
Semantics:
...
There is no signed vs unsigned multiplication. The appropriate action is taken
based on the type of the operand.
Example:
<result> = mul int 4, %var ; yields {int}:result = 4 * %var
'div' Instruction
<result> = div <ty> <var1>, <var2> ; yields {ty}:result
Overview:
The 'div' instruction returns the quotient of its two operands.
Arguments:
The two arguments to the 'div' instruction must be either integral or floating point
values. Both arguments must have identical types.
Semantics:
...
Example:
<result> = div int 4, %var ; yields {int}:result = 4 / %var
'rem' Instruction
<result> = rem <ty> <var1>, <var2> ; yields {ty}:result
Overview:
The 'rem' instruction returns the remainder from the division of its two operands.
Arguments:
The two arguments to the 'rem' instruction must be either integral or floating point values. Both arguments must have identical types.
Semantics:
TODO: remainder or modulus?
...
Example:
<result> = rem int 4, %var ; yields {int}:result = 4 % %var
'setcc' Instructions
<result> = seteq <ty> <var1>, <var2> ; yields {bool}:result
<result> = setne <ty> <var1>, <var2> ; yields {bool}:result
<result> = setlt <ty> <var1>, <var2> ; yields {bool}:result
<result> = setgt <ty> <var1>, <var2> ; yields {bool}:result
<result> = setle <ty> <var1>, <var2> ; yields {bool}:result
<result> = setge <ty> <var1>, <var2> ; yields {bool}:result
Overview:
The 'setcc' family of instructions returns a boolean value based on a comparison of their two operands.
Arguments:
The two arguments to the 'setcc'
instructions must be of first class or pointer type (it is not possible to compare
'label's, 'array's, 'structure' or 'void'
values). Both arguments must have identical types.
The 'setlt', 'setgt', 'setle', and 'setge' instructions do not operate on 'bool' typed arguments.
Semantics:
The 'seteq' instruction yields a true 'bool' value if both operands are equal.
The 'setne' instruction yields a true 'bool' value if both operands are unequal.
The 'setlt' instruction yields a true 'bool' value if the first operand is less than the second operand.
The 'setgt' instruction yields a true 'bool' value if the first operand is greater than the second operand.
The 'setle' instruction yields a true 'bool' value if the first operand is less than or equal to the second operand.
The 'setge' instruction yields a true 'bool' value if the first operand is greater than or equal to the second operand.
Example:
<result> = seteq int 4, 5 ; yields {bool}:result = false
<result> = setne float 4, 5 ; yields {bool}:result = true
<result> = setlt uint 4, 5 ; yields {bool}:result = true
<result> = setgt sbyte 4, 5 ; yields {bool}:result = false
<result> = setle sbyte 4, 5 ; yields {bool}:result = true
<result> = setge sbyte 4, 5 ; yields {bool}:result = false
Bitwise binary operators are used to do various forms of bit-twiddling in a program. They are generally very efficient instructions, and can commonly be strength reduced from other instructions. They require two operands, execute an operation on them, and produce a single value. The resulting value of the bitwise binary operators is always the same type as its first operand.
'and' Instruction
<result> = and <ty> <var1>, <var2> ; yields {ty}:result
Overview:
The 'and' instruction returns the bitwise logical and of its two operands.
Arguments:
The two arguments to the 'and' instruction must be either integral or bool values. Both arguments must have identical types.
Semantics:
...
Example:
<result> = and int 4, %var ; yields {int}:result = 4 & %var
<result> = and int 15, 40 ; yields {int}:result = 8
<result> = and int 4, 8 ; yields {int}:result = 0
'or' Instruction
<result> = or <ty> <var1>, <var2> ; yields {ty}:result
Overview:
The 'or' instruction returns the bitwise logical
inclusive or of its two operands.
Arguments:
The two arguments to the 'or' instruction must be either integral or bool values.
Both arguments must have identical types.
Semantics:
...
Example:
<result> = or int 4, %var ; yields {int}:result = 4 | %var
<result> = or int 15, 40 ; yields {int}:result = 47
<result> = or int 4, 8 ; yields {int}:result = 12
'xor' Instruction
<result> = xor <ty> <var1>, <var2> ; yields {ty}:result
Overview:
The 'xor' instruction returns the bitwise logical exclusive or of its
two operands.
Arguments:
The two arguments to the 'xor' instruction must be either integral or bool values.
Both arguments must have identical types.
Semantics:
...
Example:
<result> = xor int 4, %var ; yields {int}:result = 4 ^ %var
<result> = xor int 15, 40 ; yields {int}:result = 39
<result> = xor int 4, 8 ; yields {int}:result = 12
'shl' Instruction
<result> = shl <ty> <var1>, ubyte <var2> ; yields {ty}:result
Overview:
The 'shl' instruction returns the first operand shifted to the left a
specified number of bits.
Arguments:
The first argument to the 'shl' instruction must be an integral type. The second argument must be an
'ubyte' type.
Semantics:
... 0 bits are shifted into the emptied bit positions...
Example:
<result> = shl int 4, ubyte %var ; yields {int}:result = 4 << %var
<result> = shl int 4, ubyte 2 ; yields {int}:result = 16
<result> = shl int 1, ubyte 10 ; yields {int}:result = 1024
'shr' Instruction
<result> = shr <ty> <var1>, ubyte <var2> ; yields {ty}:result
Overview:
The 'shr' instruction returns the first operand shifted to the right a specified number of bits.
Arguments:
The first argument to the 'shr' instruction must be an integral type. The second argument must be an 'ubyte' type.
Semantics:
... if the first argument is a signed type, the most significant bit is duplicated in the newly free'd bit positions. If the first argument is unsigned, zeros shall fill the empty positions...
Example:
<result> = shr int 4, ubyte %var ; yields {int}:result = 4 >> %var
<result> = shr int 4, ubyte 1 ; yields {int}:result = 2
<result> = shr int 4, ubyte 2 ; yields {int}:result = 1
<result> = shr int 4, ubyte 3 ; yields {int}:result = 0
Accessing memory in SSA form is, well, sticky at best. This section describes how to read and write memory in LLVM.
'malloc' Instruction
<result> = malloc <type>, uint <NumElements> ; yields {type*}:result
<result> = malloc <type> ; yields {type*}:result
Overview:
The 'malloc' instruction allocates memory from the system heap and returns a pointer to it.
Arguments:
The the 'malloc' instruction allocates
sizeof(<type>)*NumElements bytes of memory from the operating
system, and returns a pointer of the appropriate type to the program. The
second form of the instruction is a shorter version of the first instruction
that defaults to allocating one element.
'type' must be a sized type
Semantics:
Memory is allocated, a pointer is returned.
Example:
%array = malloc [4 x ubyte ] ; yields {[%4 x ubyte]*}:array
%size = add uint 2, 2 ; yields {uint}:size = uint 4
%array1 = malloc ubyte, uint 4 ; yields {ubyte*}:array1
%array2 = malloc [12 x ubyte], uint %size ; yields {[12 x ubyte]*}:array2
'free' Instruction
free <type> <value> ; yields {void}
Overview:
The 'free' instruction returns memory back to the unused memory heap, to be reallocated in the future.
Arguments:
'value' shall be a pointer value that points to a value that was allocated with the 'malloc' instruction.
Semantics:
Memory is available for use after this point. The contents of the 'value' pointer are undefined after this instruction.
Example:
%array = malloc [4 x ubyte] ; yields {[4 x ubyte]*}:array
free [4 x ubyte]* %array
'alloca' Instruction
<result> = alloca <type>, uint <NumElements> ; yields {type*}:result
<result> = alloca <type> ; yields {type*}:result
Overview:
The 'alloca' instruction allocates memory on the current stack frame of
the procedure that is live until the current function returns to its caller.
Arguments:
The the 'alloca' instruction allocates
sizeof(<type>)*NumElements bytes of memory on the runtime stack,
returning a pointer of the appropriate type to the program. The second form of
the instruction is a shorter version of the first that defaults to allocating
one element.
'type' may be any sized type.
Semantics:
Memory is allocated, a pointer is returned. 'alloca'd memory is
automatically released when the function returns. The 'alloca'
instruction is commonly used to represent automatic variables that must have an
address available, as well as spilled variables.
Example:
%ptr = alloca int ; yields {int*}:ptr
%ptr = alloca int, uint 4 ; yields {int*}:ptr
'getelementptr' Instruction
<result> = getelementptr <ty>* <ptrval>{, uint <aidx>|, ubyte <sidx>}*
Overview:
The 'getelementptr' instruction is used to get the address of a
subelement of an aggregate data structure. In addition to being present as an
explicit instruction, the 'getelementptr' functionality is present in
both the 'load' and 'store' instructions to allow more compact specification
of common expressions.
Arguments:
This instruction takes a list of uint values and ubyte
constants that indicate what form of addressing to perform. The actual types of
the arguments provided depend on the type of the first pointer argument. The
'getelementptr' instruction is used to index down through the type
levels of a structure.
TODO.
Semantics:
Example:
%aptr = getelementptr {int, [12 x ubyte]}* %sptr, 1 ; yields {[12 x ubyte]*}:aptr
%ub = load [12x ubyte]* %aptr, 4 ;yields {ubyte}:ub
'load' Instruction
<result> = load <ty>* <pointer>
<result> = load <ty>* <pointer> <index list>
Overview:
The 'load' instruction is used to read from memory.
Arguments:
There are three forms of the 'load' instruction: one for reading from a general pointer, one for reading from a pointer to an array, and one for reading from a pointer to a structure.
In the first form, '<ty>' must be a pointer to a simple type (a primitive type or another pointer).
In the second form, '<ty>' must be a pointer to an array, and a list of one or more indices is provided as indexes into the (possibly multidimensional) array. No bounds checking is performed on array reads.
In the third form, the pointer must point to a (possibly nested) structure. There shall be one ubyte argument for each level of dereferencing involved.
Semantics:
...
Examples:
%ptr = alloca int ; yields {int*}:ptr
store int 3, int* %ptr ; yields {void}
%val = load int* %ptr ; yields {int}:val = int 3
%array = malloc [4 x ubyte] ; yields {[4 x ubyte]*}:array
store ubyte 124, [4 x ubyte]* %array, uint 4
%val = load [4 x ubyte]* %array, uint 4 ; yields {ubyte}:val = ubyte 124
%val = load {{int, float}}* %stptr, 0, 1 ; yields {float}:val
'store' Instruction
store <ty> <value>, <ty>* <pointer> ; yields {void}
store <ty> <value>, <ty>* <arrayptr>{, uint <idx>}+ ; yields {void}
store <ty> <value>, <ty>* <structptr>{, ubyte <idx>}+ ; yields {void}e
Overview:
The 'store' instruction is used to write to memory.
Arguments:
There are three forms of the 'store' instruction: one for writing through a general pointer, one for writing through a pointer to a (possibly multidimensional) array, and one for writing to an element of a (potentially nested) structure.
The semantics of this instruction closely match that of the load instruction, except that memory is written to, not read from.
Semantics:
...
Example:
%ptr = alloca int ; yields {int*}:ptr
store int 3, int* %ptr ; yields {void}
%val = load int* %ptr ; yields {int}:val = int 3
%array = malloc [4 x ubyte] ; yields {[4 x ubyte]*}:array
store ubyte 124, [4 x ubyte]* %array, uint 4
%val = load [4 x ubyte]* %array, uint 4 ; yields {ubyte}:val = ubyte 124
%val = load {{int, float}}* %stptr, 0, 1 ; yields {float}:val
The instructions in this catagory are the "miscellaneous" functions, that defy better classification.
'cast .. to' Instruction
'call' Instruction
Overview:
Arguments:
Semantics:
Example:
%retval = call int %test(int %argc)
'icall' Instruction
%retval = icall int %funcptr(int %arg1) ; yields {int}:%retval
'phi' Instruction
Overview:
Arguments:
Semantics:
Example:
Notice: Preliminary idea!
Builtin functions are very similar to normal functions, except they are defined by the implementation. Invocations of these functions are very similar to function invocations, except that the syntax is a little less verbose.
Builtin functions are useful to implement semi-high level ideas like a 'min' or 'max' operation that can have important properties when doing program analysis. For example:
- Some optimizations can make use of identities defined over the functions,
for example a parrallelizing compiler could make use of 'min'
identities to parrellelize a loop.
- Builtin functions would have polymorphic types, where normal function calls
may only have a single type.
- Builtin functions would be known to not have side effects, simplifying
analysis over straight function calls.
- The syntax of the builtin are cleaner than the syntax of the
'call' instruction (very minor point).
Because these invocations are explicit in the representation, the runtime can choose to implement these builtin functions any way that they want, including:
- Inlining the code directly into the invocation
- Implementing the functions in some sort of Runtime class, convert invocation
to a standard function call.
- Implementing the functions in some sort of Runtime class, and perform
standard inlining optimizations on it.
Note that these builtins do not use quoted identifiers: the name of the builtin effectively becomes an identifier in the language.
Example:
; Example of a normal function call
%maximum = call int %maximum(int %arg1, int %arg2) ; yields {int}:%maximum
; Examples of potential builtin functions
%max = max(int %arg1, int %arg2) ; yields {int}:%max
%min = min(int %arg1, int %arg2) ; yields {int}:%min
%sin = sin(double %arg) ; yields {double}:%sin
%cos = cos(double %arg) ; yields {double}:%cos
; Show that builtin's are polymorphic, like instructions
%max = max(float %arg1, float %arg2) ; yields {float}:%max
%cos = cos(float %arg) ; yields {float}:%cos
The 'maximum' vs 'max' example illustrates the difference in calling semantics between a 'call' instruction and a builtin function invocation. Notice that the 'maximum' example assumes that the function is defined local to the caller.
This list of random topics includes things that will need to be addressed before the llvm may be used to implement a java like langauge. Right now, it is pretty much useless for any language, given to unavailable of structure types
Synchronization Instructions
We will need some type of synchronization instructions to be able to implement stuff in Java well. The way I currently envision doing this is to introduce a 'lock' type, and then add two (builtin or instructions) operations to lock and unlock the lock.
These extensions are distinct from the TODO list, as they are mostly "interesting" ideas that could be implemented in the future by someone so motivated. They are not directly required to get Java like languages working.
'tailcall' Instruction
This could be useful. Who knows. '.net' does it, but is the optimization really worth the extra hassle? Using strong typing would make this trivial to implement and a runtime could always callback to using downconverting this to a normal 'call' instruction.
Global Variables
In order to represent programs written in languages like C, we need to be able to support variables at the module (global) scope. Perhaps they should be written outside of the module definition even. Maybe global functions should be handled like this as well.
Explicit Parrellelism
With the rise of massively parrellel architectures (like the IA64 architecture, multithreaded CPU cores, and SIMD data sets) it is becoming increasingly more important to extract all of the ILP from a code stream possible. It would be interesting to research encoding functions that can explicitly represent this. One straightforward way to do this would be to introduce a "stop" instruction that is equilivent to the IA64 stop bit.
Vectorized Architectures
Chris Lattner
Last modified: Sun Apr 14 01:12:55 CDT 2002