1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><title>llvm Assembly Language Reference Manual</title></head>
5 <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
6 <tr><td> <font size=+5 color="#EEEEFF" face="Georgia,Palatino,Times,Roman"><b>llvm Assembly Language Reference Manual</b></font></td>
10 <li><a href="#abstract">Abstract</a>
11 <li><a href="#introduction">Introduction</a>
12 <li><a href="#identifiers">Identifiers</a>
13 <li><a href="#typesystem">Type System</a>
15 <li><a href="#t_primitive">Primitive Types</a>
17 <li><a href="#t_classifications">Type Classifications</a>
19 <li><a href="#t_derived">Derived Types</a>
21 <li><a href="#t_array" >Array Type</a>
22 <li><a href="#t_function">Function Type</a>
23 <li><a href="#t_pointer">Pointer Type</a>
24 <li><a href="#t_struct" >Structure Type</a>
25 <li><a href="#t_packed" >Packed Type</a>
28 <li><a href="#highlevel">High Level Structure</a>
30 <li><a href="#modulestructure">Module Structure</a>
31 <li><a href="#globalvars">Global Variables</a>
32 <li><a href="#functionstructure">Function Structure</a>
34 <li><a href="#instref">Instruction Reference</a>
36 <li><a href="#terminators">Terminator Instructions</a>
38 <li><a href="#i_ret" >'<tt>ret</tt>' Instruction</a>
39 <li><a href="#i_br" >'<tt>br</tt>' Instruction</a>
40 <li><a href="#i_switch">'<tt>switch</tt>' Instruction</a>
41 <li><a href="#i_invoke">'<tt>invoke</tt>' Instruction</a>
43 <li><a href="#binaryops">Binary Operations</a>
45 <li><a href="#i_add" >'<tt>add</tt>' Instruction</a>
46 <li><a href="#i_sub" >'<tt>sub</tt>' Instruction</a>
47 <li><a href="#i_mul" >'<tt>mul</tt>' Instruction</a>
48 <li><a href="#i_div" >'<tt>div</tt>' Instruction</a>
49 <li><a href="#i_rem" >'<tt>rem</tt>' Instruction</a>
50 <li><a href="#i_setcc">'<tt>set<i>cc</i></tt>' Instructions</a>
52 <li><a href="#bitwiseops">Bitwise Binary Operations</a>
54 <li><a href="#i_and">'<tt>and</tt>' Instruction</a>
55 <li><a href="#i_or" >'<tt>or</tt>' Instruction</a>
56 <li><a href="#i_xor">'<tt>xor</tt>' Instruction</a>
57 <li><a href="#i_shl">'<tt>shl</tt>' Instruction</a>
58 <li><a href="#i_shr">'<tt>shr</tt>' Instruction</a>
60 <li><a href="#memoryops">Memory Access Operations</a>
62 <li><a href="#i_malloc" >'<tt>malloc</tt>' Instruction</a>
63 <li><a href="#i_free" >'<tt>free</tt>' Instruction</a>
64 <li><a href="#i_alloca" >'<tt>alloca</tt>' Instruction</a>
65 <li><a href="#i_load" >'<tt>load</tt>' Instruction</a>
66 <li><a href="#i_store" >'<tt>store</tt>' Instruction</a>
67 <li><a href="#i_getelementptr">'<tt>getelementptr</tt>' Instruction</a>
69 <li><a href="#otherops">Other Operations</a>
71 <li><a href="#i_phi" >'<tt>phi</tt>' Instruction</a>
72 <li><a href="#i_cast">'<tt>cast .. to</tt>' Instruction</a>
73 <li><a href="#i_call" >'<tt>call</tt>' Instruction</a>
77 <li><a href="#related">Related Work</a>
82 <!-- *********************************************************************** -->
83 <p><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
84 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
85 <a name="abstract">Abstract
86 </b></font></td></tr></table><ul>
87 <!-- *********************************************************************** -->
90 This document is a reference manual for the LLVM assembly language. LLVM is
91 an SSA based representation that provides type safety, low level operations,
92 flexibility, and the capability of representing 'all' high level languages
93 cleanly. It is the common code representation used throughout all phases of
94 the LLVM compilation strategy.
100 <!-- *********************************************************************** -->
101 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
102 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
103 <a name="introduction">Introduction
104 </b></font></td></tr></table><ul>
105 <!-- *********************************************************************** -->
107 The LLVM code representation is designed to be used in three different forms: as
108 an in-memory compiler IR, as an on-disk bytecode representation, suitable for
109 fast loading by a dynamic compiler, and as a human readable assembly language
110 representation. This allows LLVM to provide a powerful intermediate
111 representation for efficient compiler transformations and analysis, while
112 providing a natural means to debug and visualize the transformations. The three
113 different forms of LLVM are all equivalent. This document describes the human
114 readable representation and notation.<p>
116 The LLVM representation aims to be a light weight and low level while being
117 expressive, typed, and extensible at the same time. It aims to be a "universal
118 IR" of sorts, by being at a low enough level that high level ideas may be
119 cleanly mapped to it (similar to how microprocessors are "universal IR's",
120 allowing many source languages to be mapped to them). By providing type
121 information, LLVM can be used as the target of optimizations: for example,
122 through pointer analysis, it can be proven that a C automatic variable is never
123 accessed outside of the current function... allowing it to be promoted to a
124 simple SSA value instead of a memory location.<p>
126 <!-- _______________________________________________________________________ -->
127 </ul><a name="wellformed"><h4><hr size=0>Well Formedness</h4><ul>
129 It is important to note that this document describes 'well formed' llvm assembly
130 language. There is a difference between what the parser accepts and what is
131 considered 'well formed'. For example, the following instruction is
132 syntactically okay, but not well formed:<p>
135 %x = <a href="#i_add">add</a> int 1, %x
138 ...because the definition of %x does not dominate all of its uses. The LLVM
139 infrastructure provides a verification pass that may be used to verify that an
140 LLVM module is well formed. This pass is automatically run by the parser after
141 parsing input assembly, and by the optimizer before it outputs bytecode. The
142 violations pointed out by the verifier pass indicate bugs in transformation
143 passes or input to the parser.<p>
145 <!-- Describe the typesetting conventions here. -->
148 <!-- *********************************************************************** -->
149 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
150 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
151 <a name="identifiers">Identifiers
152 </b></font></td></tr></table><ul>
153 <!-- *********************************************************************** -->
155 LLVM uses three different forms of identifiers, for different purposes:<p>
158 <li>Numeric constants are represented as you would expect: 12, -3 123.421, etc. Floating point constants have an optional hexidecimal notation.
159 <li>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 '<tt>%[a-zA-Z$._][a-zA-Z$._0-9]*</tt>'.
160 <li>Unnamed values are represented as an unsigned numeric value with a '%' prefix. For example, %12, %2, %44.
163 LLVM requires the values start with a '%' sign for two reasons: Compilers don't
164 need to worry about name clashes with reserved words, and the set of reserved
165 words may be expanded in the future without penalty. Additionally, unnamed
166 identifiers allow a compiler to quickly come up with a temporary variable
167 without having to avoid symbol table conflicts.<p>
169 Reserved words in LLVM are very similar to reserved words in other languages.
170 There are keywords for different opcodes ('<tt><a href="#i_add">add</a></tt>',
171 '<tt><a href="#i_cast">cast</a></tt>', '<tt><a href="#i_ret">ret</a></tt>',
172 etc...), for primitive type names ('<tt><a href="#t_void">void</a></tt>',
173 '<tt><a href="#t_uint">uint</a></tt>', etc...), and others. These reserved
174 words cannot conflict with variable names, because none of them start with a '%'
177 Here is an example of LLVM code to multiply the integer variable '<tt>%X</tt>'
182 %result = <a href="#i_mul">mul</a> uint %X, 8
185 After strength reduction:
187 %result = <a href="#i_shl">shl</a> uint %X, ubyte 3
192 <a href="#i_add">add</a> uint %X, %X <i>; yields {uint}:%0</i>
193 <a href="#i_add">add</a> uint %0, %0 <i>; yields {uint}:%1</i>
194 %result = <a href="#i_add">add</a> uint %1, %1
197 This last way of multiplying <tt>%X</tt> by 8 illustrates several important lexical features of LLVM:<p>
200 <li>Comments are delimited with a '<tt>;</tt>' and go until the end of line.
201 <li>Unnamed temporaries are created when the result of a computation is not
202 assigned to a named value.
203 <li>Unnamed temporaries are numbered sequentially
206 ...and it also show a convention that we follow in this document. When
207 demonstrating instructions, we will follow an instruction with a comment that
208 defines the type and name of value produced. Comments are shown in italic
211 The one unintuitive notation for constants is the optional hexidecimal form of
212 floating point constants. For example, the form '<tt>double
213 0x432ff973cafa8000</tt>' is equivalent to (but harder to read than) '<tt>double
214 4.5e+15</tt>' which is also supported by the parser. The only time hexadecimal
215 floating point constants are useful (and the only time that they are generated
216 by the disassembler) is when an FP constant has to be emitted that is not
217 representable as a decimal floating point number exactly. For example, NaN's,
218 infinities, and other special cases are represented in their IEEE hexadecimal
219 format so that assembly and disassembly do not cause any bits to change in the
223 <!-- *********************************************************************** -->
224 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
225 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
226 <a name="typesystem">Type System
227 </b></font></td></tr></table><ul>
228 <!-- *********************************************************************** -->
230 The LLVM type system is one of the most important features of the intermediate
231 representation. Being typed enables a number of optimizations to be performed
232 on the IR directly, without having to do extra analyses on the side before the
233 transformation. A strong type system makes it easier to read the generated code
234 and enables novel analyses and transformations that are not feasible to perform
235 on normal three address code representations.<p>
237 <!-- The written form for the type system was heavily influenced by the
238 syntactic problems with types in the C language<sup><a
239 href="#rw_stroustrup">1</a></sup>.<p> -->
243 <!-- ======================================================================= -->
244 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
245 <tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
246 <a name="t_primitive">Primitive Types
247 </b></font></td></tr></table><ul>
249 The primitive types are the fundemental building blocks of the LLVM system. The
250 current set of primitive types are as follows:<p>
252 <table border=0 align=center><tr><td>
254 <table border=1 cellspacing=0 cellpadding=4 align=center>
255 <tr><td><tt>void</tt></td> <td>No value</td></tr>
256 <tr><td><tt>ubyte</tt></td> <td>Unsigned 8 bit value</td></tr>
257 <tr><td><tt>ushort</tt></td><td>Unsigned 16 bit value</td></tr>
258 <tr><td><tt>uint</tt></td> <td>Unsigned 32 bit value</td></tr>
259 <tr><td><tt>ulong</tt></td> <td>Unsigned 64 bit value</td></tr>
260 <tr><td><tt>float</tt></td> <td>32 bit floating point value</td></tr>
261 <tr><td><tt>label</tt></td> <td>Branch destination</td></tr>
266 <table border=1 cellspacing=0 cellpadding=4 align=center>
267 <tr><td><tt>bool</tt></td> <td>True or False value</td></tr>
268 <tr><td><tt>sbyte</tt></td> <td>Signed 8 bit value</td></tr>
269 <tr><td><tt>short</tt></td> <td>Signed 16 bit value</td></tr>
270 <tr><td><tt>int</tt></td> <td>Signed 32 bit value</td></tr>
271 <tr><td><tt>long</tt></td> <td>Signed 64 bit value</td></tr>
272 <tr><td><tt>double</tt></td><td>64 bit floating point value</td></tr>
275 </td></tr></table><p>
279 <!-- _______________________________________________________________________ -->
280 </ul><a name="t_classifications"><h4><hr size=0>Type Classifications</h4><ul>
282 These different primitive types fall into a few useful classifications:<p>
284 <table border=1 cellspacing=0 cellpadding=4 align=center>
285 <tr><td><a name="t_signed">signed</td> <td><tt>sbyte, short, int, long, float, double</tt></td></tr>
286 <tr><td><a name="t_unsigned">unsigned</td><td><tt>ubyte, ushort, uint, ulong</tt></td></tr>
287 <tr><td><a name="t_integral">integral</td><td><tt>ubyte, sbyte, ushort, short, uint, int, ulong, long</tt></td></tr>
288 <tr><td><a name="t_floating">floating point</td><td><tt>float, double</tt></td></tr>
289 <tr><td><a name="t_firstclass">first class</td><td><tt>bool, ubyte, sbyte, ushort, short,<br> uint, int, ulong, long, float, double, <a href="#t_pointer">pointer</a></tt></td></tr>
296 <!-- ======================================================================= -->
297 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
298 <a name="t_derived">Derived Types
299 </b></font></td></tr></table><ul>
301 The real power in LLVM comes from the derived types in the system. This is what
302 allows a programmer to represent arrays, functions, pointers, and other useful
303 types. Note that these derived types may be recursive: For example, it is
304 possible to have a two dimensional array.<p>
308 <!-- _______________________________________________________________________ -->
309 </ul><a name="t_array"><h4><hr size=0>Array Type</h4><ul>
313 The array type is a very simple derived type that arranges elements sequentially
314 in memory. The array type requires a size (number of elements) and an
315 underlying data type.<p>
319 [<# elements> x <elementtype>]
322 The number of elements is a constant integer value, elementtype may be any type
327 <tt>[40 x int ]</tt>: Array of 40 integer values.<br>
328 <tt>[41 x int ]</tt>: Array of 41 integer values.<br>
329 <tt>[40 x uint]</tt>: Array of 40 unsigned integer values.<p>
332 Here are some examples of multidimensional arrays:<p>
334 <table border=0 cellpadding=0 cellspacing=0>
335 <tr><td><tt>[3 x [4 x int]]</tt></td><td>: 3x4 array integer values.</td></tr>
336 <tr><td><tt>[12 x [10 x float]]</tt></td><td>: 2x10 array of single precision floating point values.</td></tr>
337 <tr><td><tt>[2 x [3 x [4 x uint]]]</tt></td><td>: 2x3x4 array of unsigned integer values.</td></tr>
342 <!-- _______________________________________________________________________ -->
343 </ul><a name="t_function"><h4><hr size=0>Function Type</h4><ul>
347 The function type can be thought of as a function signature. It consists of a
348 return type and a list of formal parameter types. Function types are usually
349 used when to build virtual function tables (which are structures of pointers to
350 functions), for indirect function calls, and when defining a function.<p>
354 <returntype> (<parameter list>)
357 Where '<tt><parameter list></tt>' is a comma seperated list of type
358 specifiers. Optionally, the parameter list may include a type <tt>...</tt>,
359 which indicates that the function takes a variable number of arguments. Note
360 that there currently is no way to define a function in LLVM that takes a
361 variable number of arguments, but it is possible to <b>call</b> a function that
366 <table border=0 cellpadding=0 cellspacing=0>
368 <tr><td><tt>int (int)</tt></td><td>: function taking an <tt>int</tt>, returning
369 an <tt>int</tt></td></tr>
371 <tr><td><tt>float (int, int *) *</tt></td><td>: <a href="#t_pointer">Pointer</a>
372 to a function that takes an <tt>int</tt> and a <a href="#t_pointer">pointer</a>
373 to <tt>int</tt>, returning <tt>float</tt>.</td></tr>
375 <tr><td><tt>int (sbyte *, ...)</tt></td><td>: A vararg function that takes at
376 least one <a href="#t_pointer">pointer</a> to <tt>sbyte</tt> (signed char in C),
377 which returns an integer. This is the signature for <tt>printf</tt> in
385 <!-- _______________________________________________________________________ -->
386 </ul><a name="t_struct"><h4><hr size=0>Structure Type</h4><ul>
390 The structure type is used to represent a collection of data members together in
391 memory. The packing of the field types is defined to match the ABI of the
392 underlying processor. The elements of a structure may be any type that has a
395 Structures are accessed using '<tt><a href="#i_load">load</a></tt> and '<tt><a
396 href="#i_store">store</a></tt>' by getting a pointer to a field with the '<tt><a
397 href="#i_getelementptr">getelementptr</a></tt>' instruction.<p>
401 { <type list> }
406 <table border=0 cellpadding=0 cellspacing=0>
408 <tr><td><tt>{ int, int, int }</tt></td><td>: a triple of three <tt>int</tt>
411 <tr><td><tt>{ float, int (int) * }</tt></td><td>: A pair, where the first
412 element is a <tt>float</tt> and the second element is a <a
413 href="#t_pointer">pointer</a> to a <a href="t_function">function</a> that takes
414 an <tt>int</tt>, returning an <tt>int</tt>.</td></tr>
419 <!-- _______________________________________________________________________ -->
420 </ul><a name="t_pointer"><h4><hr size=0>Pointer Type</h4><ul>
424 As in many languages, the pointer type represents a pointer or reference to
425 another object, which must live in memory.<p>
434 <table border=0 cellpadding=0 cellspacing=0>
436 <tr><td><tt>[4x int]*</tt></td><td>: <a href="#t_pointer">pointer</a> to <a
437 href="#t_array">array</a> of four <tt>int</tt> values</td></tr>
439 <tr><td><tt>int (int *) *</tt></td><td>: A <a href="#t_pointer">pointer</a> to a
440 <a href="t_function">function</a> that takes an <tt>int</tt>, returning an
441 <tt>int</tt>.</td></tr>
447 <!-- _______________________________________________________________________ -->
449 </ul><a name="t_packed"><h4><hr size=0>Packed Type</h4><ul>
451 Mention/decide that packed types work with saturation or not. Maybe have a packed+saturated type in addition to just a packed type.<p>
453 Packed types should be 'nonsaturated' because standard data types are not saturated. Maybe have a saturated packed type?<p>
458 <!-- *********************************************************************** -->
459 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
460 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
461 <a name="highlevel">High Level Structure
462 </b></font></td></tr></table><ul>
463 <!-- *********************************************************************** -->
466 <!-- ======================================================================= -->
467 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
468 <tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
469 <a name="modulestructure">Module Structure
470 </b></font></td></tr></table><ul>
472 LLVM programs are composed of "Module"s, each of which is a translation unit of
473 the input programs. Each module consists of functions, global variables, and
474 symbol table entries. Modules may be combined together with the LLVM linker,
475 which merges function (and global variable) definitions, resolves forward
476 declarations, and merges symbol table entries. Here is an example of the "hello world" module:<p>
479 <i>; Declare the string constant as a global constant...</i>
480 <a href="#identifiers">%.LC0</a> = <a href="#linkage_decl">internal</a> <a href="#globalvars">constant</a> <a href="#t_array">[13 x sbyte]</a> c"hello world\0A\00" <i>; [13 x sbyte]*</i>
482 <i>; Forward declaration of puts</i>
483 <a href="#functionstructure">declare</a> int "puts"(sbyte*) <i>; int(sbyte*)* </i>
485 <i>; Definition of main function</i>
486 int "main"() { <i>; int()* </i>
487 <i>; Convert [13x sbyte]* to sbyte *...</i>
488 %cast210 = <a href="#i_getelementptr">getelementptr</a> [13 x sbyte]* %.LC0, uint 0, uint 0 <i>; sbyte*</i>
490 <i>; Call puts function to write out the string to stdout...</i>
491 <a href="#i_call">call</a> int %puts(sbyte* %cast210) <i>; int</i>
492 <a href="#i_ret">ret</a> int 0
496 This example is made up of a <a href="#globalvars">global variable</a> named
497 "<tt>.LC0</tt>", an external declaration of the "<tt>puts</tt>" function, and a
498 <a href="#functionstructure">function definition</a> for "<tt>main</tt>".<p>
500 <a name="linkage_decl">
501 In general, a module is made up of a list of global values, where both functions
502 and global variables are global values. Global values are represented by a
503 pointer to a memory location (in this case, a pointer to an array of char, and a
504 pointer to a function), and can be either "internal" or externally accessible
505 (which corresponds to the static keyword in C, when used at global scope).<p>
507 For example, since the "<tt>.LC0</tt>" variable is defined to be internal, if
508 another module defined a "<tt>.LC0</tt>" variable and was linked with this one,
509 one of the two would be renamed, preventing a collision. Since "<tt>main</tt>"
510 and "<tt>puts</tt>" are external (i.e., lacking "<tt>internal</tt>"
511 declarations), they are accessible outside of the current module. It is illegal
512 for a function declaration to be "<tt>internal</tt>".<p>
515 <!-- ======================================================================= -->
516 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
517 <tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
518 <a name="globalvars">Global Variables
519 </b></font></td></tr></table><ul>
521 Global variables define regions of memory allocated at compilation time instead
522 of run-time. Global variables may optionally be initialized. A variable may
523 be defined as a global "constant", which indicates that the contents of the
524 variable will never be modified (opening options for optimization). Constants
525 must always have an initial value.<p>
527 As SSA values, global variables define pointer values that are in scope
528 (i.e. they dominate) for all basic blocks in the program. Global variables
529 always define a pointer to their "content" type because they describe a region
530 of memory, and all memory objects in LLVM are accessed through pointers.<p>
534 <!-- ======================================================================= -->
535 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
536 <tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
537 <a name="functionstructure">Function Structure
538 </b></font></td></tr></table><ul>
540 LLVM functions definitions are composed of a (possibly empty) argument list, an
541 opening curly brace, a list of basic blocks, and a closing curly brace. LLVM
542 function declarations are defined with the "<tt>declare</tt>" keyword, a
543 function name and a function signature.<p>
545 A function definition contains a list of basic blocks, forming the CFG for the
546 function. Each basic block may optionally start with a label (giving the basic
547 block a symbol table entry), contains a list of instructions, and ends with a <a
548 href="#terminators">terminator</a> instruction (such as a branch or function
551 The first basic block in program is special in two ways: it is immediately
552 executed on entrance to the function, and it is not allowed to have predecessor
553 basic blocks (i.e. there can not be any branches to the entry block of a
557 <!-- *********************************************************************** -->
558 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
559 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
560 <a name="instref">Instruction Reference
561 </b></font></td></tr></table><ul>
562 <!-- *********************************************************************** -->
564 The LLVM instruction set consists of several different classifications of
565 instructions: <a href="#terminators">terminator instructions</a>, <a
566 href="#binaryops">binary instructions</a>, <a href="#memoryops">memory
567 instructions</a>, and <a href="#otherops">other instructions</a>.<p>
570 <!-- ======================================================================= -->
571 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
572 <tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
573 <a name="terminators">Terminator Instructions
574 </b></font></td></tr></table><ul>
576 As mentioned <a href="#functionstructure">previously</a>, every basic block in a
577 program ends with a "Terminator" instruction, which indicates which block should
578 be executed after the current block is finished. These terminator instructions
579 typically yield a '<tt>void</tt>' value: they produce control flow, not values
580 (the one exception being the '<a href="#i_invoke"><tt>invoke</tt></a>'
583 There are four different terminator instructions: the '<a
584 href="#i_ret"><tt>ret</tt></a>' instruction, the '<a
585 href="#i_br"><tt>br</tt></a>' instruction, the '<a
586 href="#i_switch"><tt>switch</tt></a>' instruction, and the '<a
587 href="#i_invoke"><tt>invoke</tt></a>' instruction.<p>
590 <!-- _______________________________________________________________________ -->
591 </ul><a name="i_ret"><h4><hr size=0>'<tt>ret</tt>' Instruction</h4><ul>
595 ret <type> <value> <i>; Return a value from a non-void function</i>
596 ret void <i>; Return from void function</i>
601 The '<tt>ret</tt>' instruction is used to return control flow (and a value) from
602 a function, back to the caller.<p>
604 There are two forms of the '<tt>ret</tt>' instructruction: one that returns a
605 value and then causes control flow, and one that just causes control flow to
610 The '<tt>ret</tt>' instruction may return any '<a href="#t_firstclass">first
611 class</a>' type. Notice that a function is not <a href="#wellformed">well
612 formed</a> if there exists a '<tt>ret</tt>' instruction inside of the function
613 that returns a value that does not match the return type of the function.<p>
617 When the '<tt>ret</tt>' instruction is executed, control flow returns back to
618 the calling function's context. If the instruction returns a value, that value
619 shall be propogated into the calling function's data space.<p>
623 ret int 5 <i>; Return an integer value of 5</i>
624 ret void <i>; Return from a void function</i>
628 <!-- _______________________________________________________________________ -->
629 </ul><a name="i_br"><h4><hr size=0>'<tt>br</tt>' Instruction</h4><ul>
633 br bool <cond>, label <iftrue>, label <iffalse>
634 br label <dest> <i>; Unconditional branch</i>
639 The '<tt>br</tt>' instruction is used to cause control flow to transfer to a
640 different basic block in the current function. There are two forms of this
641 instruction, corresponding to a conditional branch and an unconditional
646 The conditional branch form of the '<tt>br</tt>' instruction takes a single
647 '<tt>bool</tt>' value and two '<tt>label</tt>' values. The unconditional form
648 of the '<tt>br</tt>' instruction takes a single '<tt>label</tt>' value as a
653 Upon execution of a conditional '<tt>br</tt>' instruction, the '<tt>bool</tt>'
654 argument is evaluated. If the value is <tt>true</tt>, control flows to the
655 '<tt>iftrue</tt>' '<tt>label</tt>' argument. If "cond" is <tt>false</tt>,
656 control flows to the '<tt>iffalse</tt>' '<tt>label</tt>' argument.<p>
661 %cond = <a href="#i_setcc">seteq</a> int %a, %b
662 br bool %cond, label %IfEqual, label %IfUnequal
664 <a href="#i_ret">ret</a> int 1
666 <a href="#i_ret">ret</a> int 0
670 <!-- _______________________________________________________________________ -->
671 </ul><a name="i_switch"><h4><hr size=0>'<tt>switch</tt>' Instruction</h4><ul>
675 <i>; Definitions for lookup indirect branch</i>
676 %switchtype = type [<anysize> x { uint, label }]
678 <i>; Lookup indirect branch</i>
679 switch uint <value>, label <defaultdest>, %switchtype <switchtable>
681 <i>; Indexed indirect branch</i>
682 switch uint <idxvalue>, label <defaultdest>, [<anysize> x label] <desttable>
687 The '<tt>switch</tt>' instruction is used to transfer control flow to one of
688 several different places. It is a generalization of the '<tt>br</tt>'
689 instruction, allowing a branch to occur to one of many possible destinations.<p>
691 The '<tt>switch</tt>' statement supports two different styles of indirect
692 branching: lookup branching and indexed branching. Lookup branching is
693 generally useful if the values to switch on are spread far appart, where index
694 branching is useful if the values to switch on are generally dense.<p>
696 The two different forms of the '<tt>switch</tt>' statement are simple hints to
697 the underlying implementation. For example, the compiler may choose to
698 implement a small indirect branch table as a series of predicated comparisons:
699 if it is faster for the target architecture.<p>
703 The lookup form of the '<tt>switch</tt>' instruction uses three parameters: a
704 '<tt>uint</tt>' comparison value '<tt>value</tt>', a default '<tt>label</tt>'
705 destination, and an array of pairs of comparison value constants and
706 '<tt>label</tt>'s. The sized array must be a constant value.<p>
708 The indexed form of the '<tt>switch</tt>' instruction uses three parameters: an
709 '<tt>uint</tt>' index value, a default '<tt>label</tt>' and a sized array of
710 '<tt>label</tt>'s. The '<tt>dests</tt>' array must be a constant array.
714 The lookup style switch statement specifies a table of values and destinations.
715 When the '<tt>switch</tt>' instruction is executed, this table is searched for
716 the given value. If the value is found, the corresponding destination is
719 The index branch form simply looks up a label element directly in a table and
722 In either case, the compiler knows the static size of the array, because it is
723 provided as part of the constant values type.<p>
727 <i>; Emulate a conditional br instruction</i>
728 %Val = <a href="#i_cast">cast</a> bool %value to uint
729 switch uint %Val, label %truedest, [1 x label] [label %falsedest ]
731 <i>; Emulate an unconditional br instruction</i>
732 switch uint 0, label %dest, [ 0 x label] [ ]
734 <i>; Implement a jump table:</i>
735 switch uint %val, label %otherwise, [3 x label] [ label %onzero,
743 <!-- _______________________________________________________________________ -->
744 </ul><a name="i_invoke"><h4><hr size=0>'<tt>invoke</tt>' Instruction</h4><ul>
748 <result> = invoke <ptr to function ty> %<function ptr val>(<function args>)
749 to label <normal label> except label <exception label>
754 The '<tt>invoke</tt>' instruction is used to cause control flow to transfer to a
755 specified function, with the possibility of control flow transfer to either the
756 '<tt>normal label</tt>' label or the '<tt>exception label</tt>'. The '<tt><a
757 href="#i_call">call</a></tt>' instruction is closely related, but guarantees
758 that control flow either never returns from the called function, or that it
759 returns to the instruction following the '<tt><a href="#i_call">call</a></tt>'
764 This instruction requires several arguments:<p>
767 <li>'<tt>ptr to function ty</tt>': shall be the signature of the pointer to
768 function value being invoked. In most cases, this is a direct function
769 invocation, but indirect <tt>invoke</tt>'s are just as possible, branching off
770 an arbitrary pointer to function value.<p>
772 <li>'<tt>function ptr val</tt>': An LLVM value containing a pointer to a
773 function to be invoked.
775 <li>'<tt>function args</tt>': argument list whose types match the function
776 signature argument types. If the function signature indicates the function
777 accepts a variable number of arguments, the extra arguments can be specified.
779 <li>'<tt>normal label</tt>': the label reached when the called function executes
780 a '<tt><a href="#i_ret">ret</a></tt>' instruction.
782 <li>'<tt>exception label</tt>': the label reached when an exception is thrown.
787 This instruction is designed to operate as a standard '<tt><a
788 href="#i_call">call</a></tt>' instruction in most regards. The primary
789 difference is that it associates a label with the function invocation that may
790 be accessed via the runtime library provided by the execution environment. This
791 instruction is used in languages with destructors to ensure that proper cleanup
792 is performed in the case of either a <tt>longjmp</tt> or a thrown exception.
793 Additionally, this is important for implementation of '<tt>catch</tt>' clauses
794 in high-level languages that support them.<p>
796 <!-- For a more comprehensive explanation of how this instruction is used, look in the llvm/docs/2001-05-18-ExceptionHandling.txt document.<p> -->
800 %retval = invoke int %Test(int 15)
801 to label %Continue except label %TestCleanup <i>; {int}:retval set</i>
806 <!-- ======================================================================= -->
807 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0><tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
808 <a name="binaryops">Binary Operations
809 </b></font></td></tr></table><ul>
811 Binary operators are used to do most of the computation in a program. They
812 require two operands, execute an operation on them, and produce a single value.
813 The result value of a binary operator is not neccesarily the same type as its
816 There are several different binary operators:<p>
819 <!-- _______________________________________________________________________ -->
820 </ul><a name="i_add"><h4><hr size=0>'<tt>add</tt>' Instruction</h4><ul>
824 <result> = add <ty> <var1>, <var2> <i>; yields {ty}:result</i>
828 The '<tt>add</tt>' instruction returns the sum of its two operands.<p>
831 The two arguments to the '<tt>add</tt>' instruction must be either <a href="#t_integral">integral</a> or <a href="#t_floating">floating point</a> values. Both arguments must have identical types.<p>
835 The value produced is the integral or floating point sum of the two operands.<p>
839 <result> = add int 4, %var <i>; yields {int}:result = 4 + %var</i>
843 <!-- _______________________________________________________________________ -->
844 </ul><a name="i_sub"><h4><hr size=0>'<tt>sub</tt>' Instruction</h4><ul>
848 <result> = sub <ty> <var1>, <var2> <i>; yields {ty}:result</i>
853 The '<tt>sub</tt>' instruction returns the difference of its two operands.<p>
855 Note that the '<tt>sub</tt>' instruction is used to represent the '<tt>neg</tt>'
856 instruction present in most other intermediate representations.<p>
860 The two arguments to the '<tt>sub</tt>' instruction must be either <a
861 href="#t_integral">integral</a> or <a href="#t_floating">floating point</a>
862 values. Both arguments must have identical types.<p>
866 The value produced is the integral or floating point difference of the two
871 <result> = sub int 4, %var <i>; yields {int}:result = 4 - %var</i>
872 <result> = sub int 0, %val <i>; yields {int}:result = -%var</i>
875 <!-- _______________________________________________________________________ -->
876 </ul><a name="i_mul"><h4><hr size=0>'<tt>mul</tt>' Instruction</h4><ul>
880 <result> = mul <ty> <var1>, <var2> <i>; yields {ty}:result</i>
884 The '<tt>mul</tt>' instruction returns the product of its two operands.<p>
887 The two arguments to the '<tt>mul</tt>' instruction must be either <a href="#t_integral">integral</a> or <a href="#t_floating">floating point</a> values. Both arguments must have identical types.<p>
891 The value produced is the integral or floating point product of the two
894 There is no signed vs unsigned multiplication. The appropriate action is taken
895 based on the type of the operand. <p>
900 <result> = mul int 4, %var <i>; yields {int}:result = 4 * %var</i>
904 <!-- _______________________________________________________________________ -->
905 </ul><a name="i_div"><h4><hr size=0>'<tt>div</tt>' Instruction</h4><ul>
909 <result> = div <ty> <var1>, <var2> <i>; yields {ty}:result</i>
914 The '<tt>div</tt>' instruction returns the quotient of its two operands.<p>
918 The two arguments to the '<tt>div</tt>' instruction must be either <a
919 href="#t_integral">integral</a> or <a href="#t_floating">floating point</a>
920 values. Both arguments must have identical types.<p>
924 The value produced is the integral or floating point quotient of the two
929 <result> = div int 4, %var <i>; yields {int}:result = 4 / %var</i>
933 <!-- _______________________________________________________________________ -->
934 </ul><a name="i_rem"><h4><hr size=0>'<tt>rem</tt>' Instruction</h4><ul>
938 <result> = rem <ty> <var1>, <var2> <i>; yields {ty}:result</i>
942 The '<tt>rem</tt>' instruction returns the remainder from the division of its two operands.<p>
945 The two arguments to the '<tt>rem</tt>' instruction must be either <a href="#t_integral">integral</a> or <a href="#t_floating">floating point</a> values. Both arguments must have identical types.<p>
949 This returns the <i>remainder</i> of a division (where the result has the same
950 sign as the divisor), not the <i>modulus</i> (where the result has the same sign
951 as the dividend) of a value. For more information about the difference, see: <a
952 href="http://mathforum.org/dr.math/problems/anne.4.28.99.html">The Math
957 <result> = rem int 4, %var <i>; yields {int}:result = 4 % %var</i>
961 <!-- _______________________________________________________________________ -->
962 </ul><a name="i_setcc"><h4><hr size=0>'<tt>set<i>cc</i></tt>' Instructions</h4><ul>
966 <result> = seteq <ty> <var1>, <var2> <i>; yields {bool}:result</i>
967 <result> = setne <ty> <var1>, <var2> <i>; yields {bool}:result</i>
968 <result> = setlt <ty> <var1>, <var2> <i>; yields {bool}:result</i>
969 <result> = setgt <ty> <var1>, <var2> <i>; yields {bool}:result</i>
970 <result> = setle <ty> <var1>, <var2> <i>; yields {bool}:result</i>
971 <result> = setge <ty> <var1>, <var2> <i>; yields {bool}:result</i>
974 <h5>Overview:</h5> The '<tt>set<i>cc</i></tt>' family of instructions returns a
975 boolean value based on a comparison of their two operands.<p>
977 <h5>Arguments:</h5> The two arguments to the '<tt>set<i>cc</i></tt>'
978 instructions must be of <a href="#t_firstclass">first class</a> or <a
979 href="#t_pointer">pointer</a> type (it is not possible to compare
980 '<tt>label</tt>'s, '<tt>array</tt>'s, '<tt>structure</tt>' or '<tt>void</tt>'
981 values, etc...). Both arguments must have identical types.<p>
983 The '<tt>setlt</tt>', '<tt>setgt</tt>', '<tt>setle</tt>', and '<tt>setge</tt>'
984 instructions do not operate on '<tt>bool</tt>' typed arguments.<p>
988 The '<tt>seteq</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if
989 both operands are equal.<br>
991 The '<tt>setne</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if
992 both operands are unequal.<br>
994 The '<tt>setlt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if
995 the first operand is less than the second operand.<br>
997 The '<tt>setgt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if
998 the first operand is greater than the second operand.<br>
1000 The '<tt>setle</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if
1001 the first operand is less than or equal to the second operand.<br>
1003 The '<tt>setge</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>' value if
1004 the first operand is greater than or equal to the second operand.<p>
1008 <result> = seteq int 4, 5 <i>; yields {bool}:result = false</i>
1009 <result> = setne float 4, 5 <i>; yields {bool}:result = true</i>
1010 <result> = setlt uint 4, 5 <i>; yields {bool}:result = true</i>
1011 <result> = setgt sbyte 4, 5 <i>; yields {bool}:result = false</i>
1012 <result> = setle sbyte 4, 5 <i>; yields {bool}:result = true</i>
1013 <result> = setge sbyte 4, 5 <i>; yields {bool}:result = false</i>
1018 <!-- ======================================================================= -->
1019 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1020 <tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
1021 <a name="bitwiseops">Bitwise Binary Operations
1022 </b></font></td></tr></table><ul>
1024 Bitwise binary operators are used to do various forms of bit-twiddling in a
1025 program. They are generally very efficient instructions, and can commonly be
1026 strength reduced from other instructions. They require two operands, execute an
1027 operation on them, and produce a single value. The resulting value of the
1028 bitwise binary operators is always the same type as its first operand.<p>
1030 <!-- _______________________________________________________________________ -->
1031 </ul><a name="i_and"><h4><hr size=0>'<tt>and</tt>' Instruction</h4><ul>
1035 <result> = and <ty> <var1>, <var2> <i>; yields {ty}:result</i>
1039 The '<tt>and</tt>' instruction returns the bitwise logical and of its two operands.<p>
1043 The two arguments to the '<tt>and</tt>' instruction must be either <a
1044 href="#t_integral">integral</a> or <tt>bool</tt> values. Both arguments must
1045 have identical types.<p>
1050 The truth table used for the '<tt>and</tt>' instruction is:<p>
1052 <center><table border=1 cellspacing=0 cellpadding=4>
1053 <tr><td>In0</td> <td>In1</td> <td>Out</td></tr>
1054 <tr><td>0</td> <td>0</td> <td>0</td></tr>
1055 <tr><td>0</td> <td>1</td> <td>0</td></tr>
1056 <tr><td>1</td> <td>0</td> <td>0</td></tr>
1057 <tr><td>1</td> <td>1</td> <td>1</td></tr>
1058 </table></center><p>
1063 <result> = and int 4, %var <i>; yields {int}:result = 4 & %var</i>
1064 <result> = and int 15, 40 <i>; yields {int}:result = 8</i>
1065 <result> = and int 4, 8 <i>; yields {int}:result = 0</i>
1070 <!-- _______________________________________________________________________ -->
1071 </ul><a name="i_or"><h4><hr size=0>'<tt>or</tt>' Instruction</h4><ul>
1075 <result> = or <ty> <var1>, <var2> <i>; yields {ty}:result</i>
1078 <h5>Overview:</h5> The '<tt>or</tt>' instruction returns the bitwise logical
1079 inclusive or of its two operands.<p>
1083 The two arguments to the '<tt>or</tt>' instruction must be either <a
1084 href="#t_integral">integral</a> or <tt>bool</tt> values. Both arguments must
1085 have identical types.<p>
1090 The truth table used for the '<tt>or</tt>' instruction is:<p>
1092 <center><table border=1 cellspacing=0 cellpadding=4>
1093 <tr><td>In0</td> <td>In1</td> <td>Out</td></tr>
1094 <tr><td>0</td> <td>0</td> <td>0</td></tr>
1095 <tr><td>0</td> <td>1</td> <td>1</td></tr>
1096 <tr><td>1</td> <td>0</td> <td>1</td></tr>
1097 <tr><td>1</td> <td>1</td> <td>1</td></tr>
1098 </table></center><p>
1103 <result> = or int 4, %var <i>; yields {int}:result = 4 | %var</i>
1104 <result> = or int 15, 40 <i>; yields {int}:result = 47</i>
1105 <result> = or int 4, 8 <i>; yields {int}:result = 12</i>
1109 <!-- _______________________________________________________________________ -->
1110 </ul><a name="i_xor"><h4><hr size=0>'<tt>xor</tt>' Instruction</h4><ul>
1114 <result> = xor <ty> <var1>, <var2> <i>; yields {ty}:result</i>
1119 The '<tt>xor</tt>' instruction returns the bitwise logical exclusive or of its
1124 The two arguments to the '<tt>xor</tt>' instruction must be either <a
1125 href="#t_integral">integral</a> or <tt>bool</tt> values. Both arguments must
1126 have identical types.<p>
1131 The truth table used for the '<tt>xor</tt>' instruction is:<p>
1133 <center><table border=1 cellspacing=0 cellpadding=4>
1134 <tr><td>In0</td> <td>In1</td> <td>Out</td></tr>
1135 <tr><td>0</td> <td>0</td> <td>0</td></tr>
1136 <tr><td>0</td> <td>1</td> <td>1</td></tr>
1137 <tr><td>1</td> <td>0</td> <td>1</td></tr>
1138 <tr><td>1</td> <td>1</td> <td>0</td></tr>
1139 </table></center><p>
1144 <result> = xor int 4, %var <i>; yields {int}:result = 4 ^ %var</i>
1145 <result> = xor int 15, 40 <i>; yields {int}:result = 39</i>
1146 <result> = xor int 4, 8 <i>; yields {int}:result = 12</i>
1150 <!-- _______________________________________________________________________ -->
1151 </ul><a name="i_shl"><h4><hr size=0>'<tt>shl</tt>' Instruction</h4><ul>
1155 <result> = shl <ty> <var1>, ubyte <var2> <i>; yields {ty}:result</i>
1160 The '<tt>shl</tt>' instruction returns the first operand shifted to the left a
1161 specified number of bits.
1165 The first argument to the '<tt>shl</tt>' instruction must be an <a
1166 href="#t_integral">integral</a> type. The second argument must be an
1167 '<tt>ubyte</tt>' type.<p>
1171 The value produced is <tt>var1</tt> * 2<sup><tt>var2</tt></sup>.<p>
1176 <result> = shl int 4, ubyte %var <i>; yields {int}:result = 4 << %var</i>
1177 <result> = shl int 4, ubyte 2 <i>; yields {int}:result = 16</i>
1178 <result> = shl int 1, ubyte 10 <i>; yields {int}:result = 1024</i>
1182 <!-- _______________________________________________________________________ -->
1183 </ul><a name="i_shr"><h4><hr size=0>'<tt>shr</tt>' Instruction</h4><ul>
1188 <result> = shr <ty> <var1>, ubyte <var2> <i>; yields {ty}:result</i>
1192 The '<tt>shr</tt>' instruction returns the first operand shifted to the right a specified number of bits.
1195 The first argument to the '<tt>shr</tt>' instruction must be an <a href="#t_integral">integral</a> type. The second argument must be an '<tt>ubyte</tt>' type.<p>
1199 If the first argument is a <a href="#t_signed">signed</a> type, the most
1200 significant bit is duplicated in the newly free'd bit positions. If the first
1201 argument is unsigned, zero bits shall fill the empty positions.<p>
1205 <result> = shr int 4, ubyte %var <i>; yields {int}:result = 4 >> %var</i>
1206 <result> = shr int 4, ubyte 1 <i>; yields {int}:result = 2</i>
1207 <result> = shr int 4, ubyte 2 <i>; yields {int}:result = 1</i>
1208 <result> = shr int 4, ubyte 3 <i>; yields {int}:result = 0</i>
1215 <!-- ======================================================================= -->
1216 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1217 <tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
1218 <a name="memoryops">Memory Access Operations
1219 </b></font></td></tr></table><ul>
1221 Accessing memory in SSA form is, well, sticky at best. This section describes how to read, write, allocate and free memory in LLVM.<p>
1224 <!-- _______________________________________________________________________ -->
1225 </ul><a name="i_malloc"><h4><hr size=0>'<tt>malloc</tt>' Instruction</h4><ul>
1229 <result> = malloc <type>, uint <NumElements> <i>; yields {type*}:result</i>
1230 <result> = malloc <type> <i>; yields {type*}:result</i>
1234 The '<tt>malloc</tt>' instruction allocates memory from the system heap and returns a pointer to it.<p>
1238 The the '<tt>malloc</tt>' instruction allocates
1239 <tt>sizeof(<type>)*NumElements</tt> bytes of memory from the operating
1240 system, and returns a pointer of the appropriate type to the program. The
1241 second form of the instruction is a shorter version of the first instruction
1242 that defaults to allocating one element.<p>
1244 '<tt>type</tt>' must be a sized type<p>
1247 Memory is allocated, a pointer is returned.<p>
1251 %array = malloc [4 x ubyte ] <i>; yields {[%4 x ubyte]*}:array</i>
1253 %size = <a href="#i_add">add</a> uint 2, 2 <i>; yields {uint}:size = uint 4</i>
1254 %array1 = malloc ubyte, uint 4 <i>; yields {ubyte*}:array1</i>
1255 %array2 = malloc [12 x ubyte], uint %size <i>; yields {[12 x ubyte]*}:array2</i>
1259 <!-- _______________________________________________________________________ -->
1260 </ul><a name="i_free"><h4><hr size=0>'<tt>free</tt>' Instruction</h4><ul>
1264 free <type> <value> <i>; yields {void}</i>
1269 The '<tt>free</tt>' instruction returns memory back to the unused memory heap, to be reallocated in the future.<p>
1274 '<tt>value</tt>' shall be a pointer value that points to a value that was
1275 allocated with the '<tt><a href="#i_malloc">malloc</a></tt>' instruction.<p>
1280 Access to the memory pointed to by the pointer is not longer defined after this instruction executes.<p>
1284 %array = <a href="#i_malloc">malloc</a> [4 x ubyte] <i>; yields {[4 x ubyte]*}:array</i>
1285 free [4 x ubyte]* %array
1289 <!-- _______________________________________________________________________ -->
1290 </ul><a name="i_alloca"><h4><hr size=0>'<tt>alloca</tt>' Instruction</h4><ul>
1294 <result> = alloca <type>, uint <NumElements> <i>; yields {type*}:result</i>
1295 <result> = alloca <type> <i>; yields {type*}:result</i>
1300 The '<tt>alloca</tt>' instruction allocates memory on the current stack frame of
1301 the procedure that is live until the current function returns to its caller.<p>
1305 The the '<tt>alloca</tt>' instruction allocates
1306 <tt>sizeof(<type>)*NumElements</tt> bytes of memory on the runtime stack,
1307 returning a pointer of the appropriate type to the program. The second form of
1308 the instruction is a shorter version of the first that defaults to allocating
1311 '<tt>type</tt>' may be any sized type.<p>
1315 Memory is allocated, a pointer is returned. '<tt>alloca</tt>'d memory is
1316 automatically released when the function returns. The '<tt>alloca</tt>'
1317 instruction is commonly used to represent automatic variables that must have an
1318 address available, as well as spilled variables.<p>
1322 %ptr = alloca int <i>; yields {int*}:ptr</i>
1323 %ptr = alloca int, uint 4 <i>; yields {int*}:ptr</i>
1327 <!-- _______________________________________________________________________ -->
1328 </ul><a name="i_load"><h4><hr size=0>'<tt>load</tt>' Instruction</h4><ul>
1332 <result> = load <ty>* <pointer>
1336 The '<tt>load</tt>' instruction is used to read from memory.<p>
1340 The argument to the '<tt>load</tt>' instruction specifies the memory address to load from. The pointer must point to a <a href="t_firstclass">first class</a> type.<p>
1344 The location of memory pointed to is loaded.
1348 %ptr = <a href="#i_alloca">alloca</a> int <i>; yields {int*}:ptr</i>
1349 <a href="#i_store">store</a> int 3, int* %ptr <i>; yields {void}</i>
1350 %val = load int* %ptr <i>; yields {int}:val = int 3</i>
1356 <!-- _______________________________________________________________________ -->
1357 </ul><a name="i_store"><h4><hr size=0>'<tt>store</tt>' Instruction</h4><ul>
1361 store <ty> <value>, <ty>* <pointer> <i>; yields {void}</i>
1365 The '<tt>store</tt>' instruction is used to write to memory.<p>
1369 There are two arguments to the '<tt>store</tt>' instruction: a value to store
1370 and an address to store it into. The type of the '<tt><pointer></tt>'
1371 operand must be a pointer to the type of the '<tt><value></tt>'
1374 <h5>Semantics:</h5> The contents of memory are updated to contain
1375 '<tt><value></tt>' at the location specified by the
1376 '<tt><pointer></tt>' operand.<p>
1380 %ptr = <a href="#i_alloca">alloca</a> int <i>; yields {int*}:ptr</i>
1381 <a href="#i_store">store</a> int 3, int* %ptr <i>; yields {void}</i>
1382 %val = load int* %ptr <i>; yields {int}:val = int 3</i>
1388 <!-- _______________________________________________________________________ -->
1389 </ul><a name="i_getelementptr"><h4><hr size=0>'<tt>getelementptr</tt>' Instruction</h4><ul>
1393 <result> = getelementptr <ty>* <ptrval>{, uint <aidx>|, ubyte <sidx>}*
1398 The '<tt>getelementptr</tt>' instruction is used to get the address of a
1399 subelement of an aggregate data structure.<p>
1403 This instruction takes a list of <tt>uint</tt> values and <tt>ubyte</tt>
1404 constants that indicate what form of addressing to perform. The actual types of
1405 the arguments provided depend on the type of the first pointer argument. The
1406 '<tt>getelementptr</tt>' instruction is used to index down through the type
1407 levels of a structure.<p>
1409 For example, lets consider a C code fragment and how it gets compiled to
1424 int *foo(struct ST *s) {
1425 return &s[1].Z.B[5][13];
1429 The LLVM code generated by the GCC frontend is:
1432 %RT = type { sbyte, [10 x [20 x int]], sbyte }
1433 %ST = type { int, double, %RT }
1435 int* "foo"(%ST* %s) {
1436 %reg = getelementptr %ST* %s, uint 1, ubyte 2, ubyte 1, uint 5, uint 13
1443 The index types specified for the '<tt>getelementptr</tt>' instruction depend on
1444 the pointer type that is being index into. <a href="t_pointer">Pointer</a> and
1445 <a href="t_array">array</a> types require '<tt>uint</tt>' values, and <a
1446 href="t_struct">structure</a> types require '<tt>ubyte</tt>'
1447 <b>constants</b>.<p>
1449 In the example above, the first index is indexing into the '<tt>%ST*</tt>' type,
1450 which is a pointer, yielding a '<tt>%ST</tt>' = '<tt>{ int, double, %RT }</tt>'
1451 type, a structure. The second index indexes into the third element of the
1452 structure, yielding a '<tt>%RT</tt>' = '<tt>{ sbyte, [10 x [20 x int]], sbyte
1453 }</tt>' type, another structure. The third index indexes into the second
1454 element of the structure, yielding a '<tt>[10 x [20 x int]]</tt>' type, an
1455 array. The two dimensions of the array are subscripted into, yielding an
1456 '<tt>int</tt>' type. The '<tt>getelementptr</tt>' instruction return a pointer
1457 to this element, thus yielding a '<tt>int*</tt>' type.<p>
1459 Note that it is perfectly legal to index partially through a structure,
1460 returning a pointer to an inner element. Because of this, the LLVM code for the
1461 given testcase is equivalent to:<p>
1464 int* "foo"(%ST* %s) {
1465 %t1 = getelementptr %ST* %s , uint 1 <i>; yields %ST*:%t1</i>
1466 %t2 = getelementptr %ST* %t1, uint 0, ubyte 2 <i>; yields %RT*:%t2</i>
1467 %t3 = getelementptr %RT* %t2, uint 0, ubyte 1 <i>; yields [10 x [20 x int]]*:%t3</i>
1468 %t4 = getelementptr [10 x [20 x int]]* %t3, uint 0, uint 5 <i>; yields [20 x int]*:%t4</i>
1469 %t5 = getelementptr [20 x int]* %t4, uint 0, uint 13 <i>; yields int*:%t5</i>
1478 <i>; yields {[12 x ubyte]*}:aptr</i>
1479 %aptr = getelementptr {int, [12 x ubyte]}* %sptr, uint 0, ubyte 1
1484 <!-- ======================================================================= -->
1485 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1486 <tr><td> </td><td width="100%"> <font color="#EEEEFF" face="Georgia,Palatino"><b>
1487 <a name="otherops">Other Operations
1488 </b></font></td></tr></table><ul>
1490 The instructions in this catagory are the "miscellaneous" functions, that defy better classification.<p>
1493 <!-- _______________________________________________________________________ -->
1494 </ul><a name="i_phi"><h4><hr size=0>'<tt>phi</tt>' Instruction</h4><ul>
1498 <result> = phi <ty> [ <val0>, <label0>], ...
1503 The '<tt>phi</tt>' instruction is used to implement the φ node in the SSA
1504 graph representing the function.<p>
1508 The type of the incoming values are specified with the first type field. After
1509 this, the '<tt>phi</tt>' instruction takes a list of pairs as arguments, with
1510 one pair for each predecessor basic block of the current block.<p>
1512 There must be no non-phi instructions between the start of a basic block and the
1513 PHI instructions: i.e. PHI instructions must be first in a basic block.<p>
1517 At runtime, the '<tt>phi</tt>' instruction logically takes on the value
1518 specified by the parameter, depending on which basic block we came from in the
1519 last <a href="#terminators">terminator</a> instruction.<p>
1524 Loop: ; Infinite loop that counts from 0 on up...
1525 %indvar = phi uint [ 0, %LoopHeader ], [ %nextindvar, %Loop ]
1526 %nextindvar = add uint %indvar, 1
1531 <!-- _______________________________________________________________________ -->
1532 </ul><a name="i_cast"><h4><hr size=0>'<tt>cast .. to</tt>' Instruction</h4><ul>
1536 <result> = cast <ty> <value> to <ty2> <i>; yields ty2</i>
1541 The '<tt>cast</tt>' instruction is used as the primitive means to convert
1542 integers to floating point, change data type sizes, and break type safety (by
1543 casting pointers).<p>
1547 The '<tt>cast</tt>' instruction takes a value to cast, which must be a first
1548 class value, and a type to cast it to, which must also be a first class type.<p>
1552 This instruction follows the C rules for explicit casts when determining how the
1553 data being cast must change to fit in its new container.<p>
1555 When casting to bool, any value that would be considered true in the context of
1556 a C '<tt>if</tt>' condition is converted to the boolean '<tt>true</tt>' values,
1557 all else are '<tt>false</tt>'.<p>
1559 When extending an integral value from a type of one signness to another (for
1560 example '<tt>sbyte</tt>' to '<tt>ulong</tt>'), the value is sign-extended if the
1561 <b>source</b> value is signed, and zero-extended if the source value is
1562 unsigned. <tt>bool</tt> values are always zero extended into either zero or
1567 %X = cast int 257 to ubyte <i>; yields ubyte:1</i>
1568 %Y = cast int 123 to bool <i>; yields bool:true</i>
1573 <!-- _______________________________________________________________________ -->
1574 </ul><a name="i_call"><h4><hr size=0>'<tt>call</tt>' Instruction</h4><ul>
1578 <result> = call <ty>* <fnptrval>(<param list>)
1583 The '<tt>call</tt>' instruction represents a simple function call.<p>
1587 This instruction requires several arguments:<p>
1590 <li>'<tt>ty</tt>': shall be the signature of the pointer to function value being
1591 invoked. The argument types must match the types implied by this signature.<p>
1593 <li>'<tt>fnptrval</tt>': An LLVM value containing a pointer to a function to be
1594 invoked. In most cases, this is a direct function invocation, but indirect
1595 <tt>call</tt>'s are just as possible, calling an arbitrary pointer to function
1598 <li>'<tt>function args</tt>': argument list whose types match the function
1599 signature argument types. If the function signature indicates the function
1600 accepts a variable number of arguments, the extra arguments can be specified.
1605 The '<tt>call</tt>' instruction is used to cause control flow to transfer to a
1606 specified function, with its incoming arguments bound to the specified values.
1607 Upon a '<tt><a href="#i_ret">ret</a></tt>' instruction in the called function,
1608 control flow continues with the instruction after the function call, and the
1609 return value of the function is bound to the result argument. This is a simpler
1610 case of the <a href="#i_invoke">invoke</a> instruction.<p>
1614 %retval = call int %test(int %argc)
1615 call int(sbyte*, ...) *%printf(sbyte* %msg, int 12, sbyte 42);
1621 <!x- *********************************************************************** -x>
1622 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
1623 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
1624 <a name="related">Related Work
1625 </b></font></td></tr></table><ul>
1626 <!x- *********************************************************************** -x>
1629 Codesigned virtual machines.<p>
1632 <a name="rw_safetsa">
1634 <DD>Description here<p>
1637 <dt><a href="http://www.javasoft.com">Java</a>
1638 <DD>Desciption here<p>
1641 <dt><a href="http://www.microsoft.com/net">Microsoft .net</a>
1642 <DD>Desciption here<p>
1644 <a name="rw_gccrtl">
1645 <dt><a href="http://www.math.umn.edu/systems_guide/gcc-2.95.1/gcc_15.html">GNU RTL Intermediate Representation</a>
1646 <DD>Desciption here<p>
1649 <dt><a href="http://developer.intel.com/design/ia-64/index.htm">IA64 Architecture & Instruction Set</a>
1650 <DD>Desciption here<p>
1653 <dt><a href="http://www-cs-faculty.stanford.edu/~knuth/mmix-news.html">MMIX Instruction Set</a>
1654 <DD>Desciption here<p>
1656 <a name="rw_stroustrup">
1657 <dt><a href="http://www.research.att.com/~bs/devXinterview.html">"Interview With Bjarne Stroustrup"</a>
1658 <DD>This interview influenced the design and thought process behind LLVM in several ways, most notably the way that derived types are written in text format. See the question that starts with "you defined the C declarator syntax as an experiment that failed".<p>
1661 <!x- _______________________________________________________________________ -x>
1662 </ul><a name="rw_vectorization"><h3><hr size=0>Vectorized Architectures</h3><ul>
1665 <a name="rw_intel_simd">
1666 <dt>Intel MMX, MMX2, SSE, SSE2
1667 <DD>Description here<p>
1669 <a name="rw_amd_simd">
1670 <dt><a href="http://www.nondot.org/~sabre/os/H1ChipFeatures/3DNow!TechnologyManual.pdf">AMD 3Dnow!, 3Dnow! 2</a>
1671 <DD>Desciption here<p>
1673 <a name="rw_sun_simd">
1674 <dt><a href="http://www.nondot.org/~sabre/os/H1ChipFeatures/VISInstructionSetUsersManual.pdf">Sun VIS ISA</a>
1675 <DD>Desciption here<p>
1677 <a name="rw_powerpc_simd">
1679 <DD>Desciption here<p>
1688 <!-- *********************************************************************** -->
1690 <!-- *********************************************************************** -->
1695 <address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
1696 <!-- Created: Tue Jan 23 15:19:28 CST 2001 -->
1697 <!-- hhmts start -->
1698 Last modified: Thu Aug 15 11:18:37 CDT 2002