1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <title>LLVM Assembly Language Reference Manual</title>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <meta name="author" content="Chris Lattner">
8 <meta name="description"
9 content="LLVM Assembly Language Reference Manual.">
10 <link rel="stylesheet" href="llvm.css" type="text/css">
15 <div class="doc_title"> LLVM Language Reference Manual </div>
17 <li><a href="#abstract">Abstract</a></li>
18 <li><a href="#introduction">Introduction</a></li>
19 <li><a href="#identifiers">Identifiers</a></li>
20 <li><a href="#typesystem">Type System</a>
22 <li><a href="#t_primitive">Primitive Types</a>
24 <li><a href="#t_classifications">Type Classifications</a></li>
27 <li><a href="#t_derived">Derived Types</a>
29 <li><a href="#t_array">Array Type</a></li>
30 <li><a href="#t_function">Function Type</a></li>
31 <li><a href="#t_pointer">Pointer Type</a></li>
32 <li><a href="#t_struct">Structure Type</a></li>
33 <li><a href="#t_packed">Packed Type</a></li>
38 <li><a href="#highlevel">High Level Structure</a>
40 <li><a href="#modulestructure">Module Structure</a></li>
41 <li><a href="#globalvars">Global Variables</a></li>
42 <li><a href="#functionstructure">Function Structure</a></li>
45 <li><a href="#instref">Instruction Reference</a>
47 <li><a href="#terminators">Terminator Instructions</a>
49 <li><a href="#i_ret">'<tt>ret</tt>' Instruction</a></li>
50 <li><a href="#i_br">'<tt>br</tt>' Instruction</a></li>
51 <li><a href="#i_switch">'<tt>switch</tt>' Instruction</a></li>
52 <li><a href="#i_invoke">'<tt>invoke</tt>' Instruction</a></li>
53 <li><a href="#i_unwind">'<tt>unwind</tt>' Instruction</a></li>
54 <li><a href="#i_unreachable">'<tt>unreachable</tt>' Instruction</a></li>
57 <li><a href="#binaryops">Binary Operations</a>
59 <li><a href="#i_add">'<tt>add</tt>' Instruction</a></li>
60 <li><a href="#i_sub">'<tt>sub</tt>' Instruction</a></li>
61 <li><a href="#i_mul">'<tt>mul</tt>' Instruction</a></li>
62 <li><a href="#i_div">'<tt>div</tt>' Instruction</a></li>
63 <li><a href="#i_rem">'<tt>rem</tt>' Instruction</a></li>
64 <li><a href="#i_setcc">'<tt>set<i>cc</i></tt>' Instructions</a></li>
67 <li><a href="#bitwiseops">Bitwise Binary Operations</a>
69 <li><a href="#i_and">'<tt>and</tt>' Instruction</a></li>
70 <li><a href="#i_or">'<tt>or</tt>' Instruction</a></li>
71 <li><a href="#i_xor">'<tt>xor</tt>' Instruction</a></li>
72 <li><a href="#i_shl">'<tt>shl</tt>' Instruction</a></li>
73 <li><a href="#i_shr">'<tt>shr</tt>' Instruction</a></li>
76 <li><a href="#memoryops">Memory Access Operations</a>
78 <li><a href="#i_malloc">'<tt>malloc</tt>' Instruction</a></li>
79 <li><a href="#i_free">'<tt>free</tt>' Instruction</a></li>
80 <li><a href="#i_alloca">'<tt>alloca</tt>' Instruction</a></li>
81 <li><a href="#i_load">'<tt>load</tt>' Instruction</a></li>
82 <li><a href="#i_store">'<tt>store</tt>' Instruction</a></li>
83 <li><a href="#i_getelementptr">'<tt>getelementptr</tt>' Instruction</a></li>
86 <li><a href="#otherops">Other Operations</a>
88 <li><a href="#i_phi">'<tt>phi</tt>' Instruction</a></li>
89 <li><a href="#i_cast">'<tt>cast .. to</tt>' Instruction</a></li>
90 <li><a href="#i_select">'<tt>select</tt>' Instruction</a></li>
91 <li><a href="#i_call">'<tt>call</tt>' Instruction</a></li>
92 <li><a href="#i_vanext">'<tt>vanext</tt>' Instruction</a></li>
93 <li><a href="#i_vaarg">'<tt>vaarg</tt>' Instruction</a></li>
98 <li><a href="#intrinsics">Intrinsic Functions</a>
100 <li><a href="#int_varargs">Variable Argument Handling Intrinsics</a>
102 <li><a href="#i_va_start">'<tt>llvm.va_start</tt>' Intrinsic</a></li>
103 <li><a href="#i_va_end">'<tt>llvm.va_end</tt>' Intrinsic</a></li>
104 <li><a href="#i_va_copy">'<tt>llvm.va_copy</tt>' Intrinsic</a></li>
107 <li><a href="#int_gc">Accurate Garbage Collection Intrinsics</a>
109 <li><a href="#i_gcroot">'<tt>llvm.gcroot</tt>' Intrinsic</a></li>
110 <li><a href="#i_gcread">'<tt>llvm.gcread</tt>' Intrinsic</a></li>
111 <li><a href="#i_gcwrite">'<tt>llvm.gcwrite</tt>' Intrinsic</a></li>
114 <li><a href="#int_codegen">Code Generator Intrinsics</a>
116 <li><a href="#i_returnaddress">'<tt>llvm.returnaddress</tt>' Intrinsic</a></li>
117 <li><a href="#i_frameaddress">'<tt>llvm.frameaddress</tt>' Intrinsic</a></li>
120 <li><a href="#int_os">Operating System Intrinsics</a>
122 <li><a href="#i_readport">'<tt>llvm.readport</tt>' Intrinsic</a></li>
123 <li><a href="#i_writeport">'<tt>llvm.writeport</tt>' Intrinsic</a></li>
124 <li><a href="#i_readio">'<tt>llvm.readio</tt>' Intrinsic</a></li>
125 <li><a href="#i_writeio">'<tt>llvm.writeio</tt>' Intrinsic</a></li>
127 <li><a href="#int_libc">Standard C Library Intrinsics</a>
129 <li><a href="#i_memcpy">'<tt>llvm.memcpy</tt>' Intrinsic</a></li>
130 <li><a href="#i_memmove">'<tt>llvm.memmove</tt>' Intrinsic</a></li>
131 <li><a href="#i_memset">'<tt>llvm.memset</tt>' Intrinsic</a></li>
132 <li><a href="#i_isunordered">'<tt>llvm.isunordered</tt>' Intrinsic</a></li>
135 <li><a href="#int_debugger">Debugger intrinsics</a></li>
140 <div class="doc_author">
141 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>
142 and <a href="mailto:vadve@cs.uiuc.edu">Vikram Adve</a></p>
145 <!-- *********************************************************************** -->
146 <div class="doc_section"> <a name="abstract">Abstract </a></div>
147 <!-- *********************************************************************** -->
149 <div class="doc_text">
150 <p>This document is a reference manual for the LLVM assembly language.
151 LLVM is an SSA based representation that provides type safety,
152 low-level operations, flexibility, and the capability of representing
153 'all' high-level languages cleanly. It is the common code
154 representation used throughout all phases of the LLVM compilation
158 <!-- *********************************************************************** -->
159 <div class="doc_section"> <a name="introduction">Introduction</a> </div>
160 <!-- *********************************************************************** -->
162 <div class="doc_text">
164 <p>The LLVM code representation is designed to be used in three
165 different forms: as an in-memory compiler IR, as an on-disk bytecode
166 representation (suitable for fast loading by a Just-In-Time compiler),
167 and as a human readable assembly language representation. This allows
168 LLVM to provide a powerful intermediate representation for efficient
169 compiler transformations and analysis, while providing a natural means
170 to debug and visualize the transformations. The three different forms
171 of LLVM are all equivalent. This document describes the human readable
172 representation and notation.</p>
174 <p>The LLVM representation aims to be a light-weight and low-level
175 while being expressive, typed, and extensible at the same time. It
176 aims to be a "universal IR" of sorts, by being at a low enough level
177 that high-level ideas may be cleanly mapped to it (similar to how
178 microprocessors are "universal IR's", allowing many source languages to
179 be mapped to them). By providing type information, LLVM can be used as
180 the target of optimizations: for example, through pointer analysis, it
181 can be proven that a C automatic variable is never accessed outside of
182 the current function... allowing it to be promoted to a simple SSA
183 value instead of a memory location.</p>
187 <!-- _______________________________________________________________________ -->
188 <div class="doc_subsubsection"> <a name="wellformed">Well-Formedness</a> </div>
190 <div class="doc_text">
192 <p>It is important to note that this document describes 'well formed'
193 LLVM assembly language. There is a difference between what the parser
194 accepts and what is considered 'well formed'. For example, the
195 following instruction is syntactically okay, but not well formed:</p>
198 %x = <a href="#i_add">add</a> int 1, %x
201 <p>...because the definition of <tt>%x</tt> does not dominate all of
202 its uses. The LLVM infrastructure provides a verification pass that may
203 be used to verify that an LLVM module is well formed. This pass is
204 automatically run by the parser after parsing input assembly, and by
205 the optimizer before it outputs bytecode. The violations pointed out
206 by the verifier pass indicate bugs in transformation passes or input to
209 <!-- Describe the typesetting conventions here. --> </div>
211 <!-- *********************************************************************** -->
212 <div class="doc_section"> <a name="identifiers">Identifiers</a> </div>
213 <!-- *********************************************************************** -->
215 <div class="doc_text">
217 <p>LLVM uses three different forms of identifiers, for different
221 <li>Numeric constants are represented as you would expect: 12, -3
222 123.421, etc. Floating point constants have an optional hexadecimal
224 <li>Named values are represented as a string of characters with a '%'
225 prefix. For example, %foo, %DivisionByZero,
226 %a.really.long.identifier. The actual regular expression used is '<tt>%[a-zA-Z$._][a-zA-Z$._0-9]*</tt>'.
227 Identifiers which require other characters in their names can be
228 surrounded with quotes. In this way, anything except a <tt>"</tt>
229 character can be used in a name.</li>
230 <li>Unnamed values are represented as an unsigned numeric value with
231 a '%' prefix. For example, %12, %2, %44.</li>
233 <p>LLVM requires that values start with a '%' sign for two reasons:
234 Compilers don't need to worry about name clashes with reserved words,
235 and the set of reserved words may be expanded in the future without
236 penalty. Additionally, unnamed identifiers allow a compiler to quickly
237 come up with a temporary variable without having to avoid symbol table
239 <p>Reserved words in LLVM are very similar to reserved words in other
240 languages. There are keywords for different opcodes ('<tt><a
241 href="#i_add">add</a></tt>', '<tt><a href="#i_cast">cast</a></tt>', '<tt><a
242 href="#i_ret">ret</a></tt>', etc...), for primitive type names ('<tt><a
243 href="#t_void">void</a></tt>', '<tt><a href="#t_uint">uint</a></tt>',
244 etc...), and others. These reserved words cannot conflict with
245 variable names, because none of them start with a '%' character.</p>
246 <p>Here is an example of LLVM code to multiply the integer variable '<tt>%X</tt>'
249 <pre> %result = <a href="#i_mul">mul</a> uint %X, 8<br></pre>
250 <p>After strength reduction:</p>
251 <pre> %result = <a href="#i_shl">shl</a> uint %X, ubyte 3<br></pre>
252 <p>And the hard way:</p>
253 <pre> <a href="#i_add">add</a> uint %X, %X <i>; yields {uint}:%0</i>
255 href="#i_add">add</a> uint %0, %0 <i>; yields {uint}:%1</i>
257 href="#i_add">add</a> uint %1, %1<br></pre>
258 <p>This last way of multiplying <tt>%X</tt> by 8 illustrates several
259 important lexical features of LLVM:</p>
261 <li>Comments are delimited with a '<tt>;</tt>' and go until the end
263 <li>Unnamed temporaries are created when the result of a computation
264 is not assigned to a named value.</li>
265 <li>Unnamed temporaries are numbered sequentially</li>
267 <p>...and it also show a convention that we follow in this document.
268 When demonstrating instructions, we will follow an instruction with a
269 comment that defines the type and name of value produced. Comments are
270 shown in italic text.</p>
271 <p>The one non-intuitive notation for constants is the optional
272 hexidecimal form of floating point constants. For example, the form '<tt>double
273 0x432ff973cafa8000</tt>' is equivalent to (but harder to read than) '<tt>double
274 4.5e+15</tt>' which is also supported by the parser. The only time
275 hexadecimal floating point constants are useful (and the only time that
276 they are generated by the disassembler) is when an FP constant has to
277 be emitted that is not representable as a decimal floating point number
278 exactly. For example, NaN's, infinities, and other special cases are
279 represented in their IEEE hexadecimal format so that assembly and
280 disassembly do not cause any bits to change in the constants.</p>
282 <!-- *********************************************************************** -->
283 <div class="doc_section"> <a name="typesystem">Type System</a> </div>
284 <!-- *********************************************************************** -->
285 <div class="doc_text">
286 <p>The LLVM type system is one of the most important features of the
287 intermediate representation. Being typed enables a number of
288 optimizations to be performed on the IR directly, without having to do
289 extra analyses on the side before the transformation. A strong type
290 system makes it easier to read the generated code and enables novel
291 analyses and transformations that are not feasible to perform on normal
292 three address code representations.</p>
293 <!-- The written form for the type system was heavily influenced by the
294 syntactic problems with types in the C language<sup><a
295 href="#rw_stroustrup">1</a></sup>.<p> --> </div>
296 <!-- ======================================================================= -->
297 <div class="doc_subsection"> <a name="t_primitive">Primitive Types</a> </div>
298 <div class="doc_text">
299 <p>The primitive types are the fundamental building blocks of the LLVM
300 system. The current set of primitive types are as follows:</p>
302 <table class="layout">
307 <tr><th>Type</th><th>Description</th></tr>
308 <tr><td><tt>void</tt></td><td>No value</td></tr>
309 <tr><td><tt>ubyte</tt></td><td>Unsigned 8 bit value</td></tr>
310 <tr><td><tt>ushort</tt></td><td>Unsigned 16 bit value</td></tr>
311 <tr><td><tt>uint</tt></td><td>Unsigned 32 bit value</td></tr>
312 <tr><td><tt>ulong</tt></td><td>Unsigned 64 bit value</td></tr>
313 <tr><td><tt>float</tt></td><td>32 bit floating point value</td></tr>
314 <tr><td><tt>label</tt></td><td>Branch destination</td></tr>
321 <tr><th>Type</th><th>Description</th></tr>
322 <tr><td><tt>bool</tt></td><td>True or False value</td></tr>
323 <tr><td><tt>sbyte</tt></td><td>Signed 8 bit value</td></tr>
324 <tr><td><tt>short</tt></td><td>Signed 16 bit value</td></tr>
325 <tr><td><tt>int</tt></td><td>Signed 32 bit value</td></tr>
326 <tr><td><tt>long</tt></td><td>Signed 64 bit value</td></tr>
327 <tr><td><tt>double</tt></td><td>64 bit floating point value</td></tr>
335 <!-- _______________________________________________________________________ -->
336 <div class="doc_subsubsection"> <a name="t_classifications">Type
337 Classifications</a> </div>
338 <div class="doc_text">
339 <p>These different primitive types fall into a few useful
342 <table border="1" cellspacing="0" cellpadding="4">
344 <tr><th>Classification</th><th>Types</th></tr>
346 <td><a name="t_signed">signed</a></td>
347 <td><tt>sbyte, short, int, long, float, double</tt></td>
350 <td><a name="t_unsigned">unsigned</a></td>
351 <td><tt>ubyte, ushort, uint, ulong</tt></td>
354 <td><a name="t_integer">integer</a></td>
355 <td><tt>ubyte, sbyte, ushort, short, uint, int, ulong, long</tt></td>
358 <td><a name="t_integral">integral</a></td>
359 <td><tt>bool, ubyte, sbyte, ushort, short, uint, int, ulong, long</tt>
363 <td><a name="t_floating">floating point</a></td>
364 <td><tt>float, double</tt></td>
367 <td><a name="t_firstclass">first class</a></td>
368 <td><tt>bool, ubyte, sbyte, ushort, short, uint, int, ulong, long,<br>
369 float, double, <a href="#t_pointer">pointer</a>,
370 <a href="#t_packed">packed</a></tt></td>
375 <p>The <a href="#t_firstclass">first class</a> types are perhaps the
376 most important. Values of these types are the only ones which can be
377 produced by instructions, passed as arguments, or used as operands to
378 instructions. This means that all structures and arrays must be
379 manipulated either by pointer or by component.</p>
381 <!-- ======================================================================= -->
382 <div class="doc_subsection"> <a name="t_derived">Derived Types</a> </div>
383 <div class="doc_text">
384 <p>The real power in LLVM comes from the derived types in the system.
385 This is what allows a programmer to represent arrays, functions,
386 pointers, and other useful types. Note that these derived types may be
387 recursive: For example, it is possible to have a two dimensional array.</p>
389 <!-- _______________________________________________________________________ -->
390 <div class="doc_subsubsection"> <a name="t_array">Array Type</a> </div>
391 <div class="doc_text">
393 <p>The array type is a very simple derived type that arranges elements
394 sequentially in memory. The array type requires a size (number of
395 elements) and an underlying data type.</p>
397 <pre> [<# elements> x <elementtype>]<br></pre>
398 <p>The number of elements is a constant integer value, elementtype may
399 be any type with a size.</p>
401 <table class="layout">
404 <tt>[40 x int ]</tt><br/>
405 <tt>[41 x int ]</tt><br/>
406 <tt>[40 x uint]</tt><br/>
409 Array of 40 integer values.<br/>
410 Array of 41 integer values.<br/>
411 Array of 40 unsigned integer values.<br/>
415 <p>Here are some examples of multidimensional arrays:</p>
416 <table class="layout">
419 <tt>[3 x [4 x int]]</tt><br/>
420 <tt>[12 x [10 x float]]</tt><br/>
421 <tt>[2 x [3 x [4 x uint]]]</tt><br/>
424 3x4 array integer values.<br/>
425 12x10 array of single precision floating point values.<br/>
426 2x3x4 array of unsigned integer values.<br/>
432 <!-- _______________________________________________________________________ -->
433 <div class="doc_subsubsection"> <a name="t_function">Function Type</a> </div>
434 <div class="doc_text">
436 <p>The function type can be thought of as a function signature. It
437 consists of a return type and a list of formal parameter types.
438 Function types are usually used to build virtual function tables
439 (which are structures of pointers to functions), for indirect function
440 calls, and when defining a function.</p>
442 The return type of a function type cannot be an aggregate type.
445 <pre> <returntype> (<parameter list>)<br></pre>
446 <p>Where '<tt><parameter list></tt>' is a comma-separated list of type
447 specifiers. Optionally, the parameter list may include a type <tt>...</tt>,
448 which indicates that the function takes a variable number of arguments.
449 Variable argument functions can access their arguments with the <a
450 href="#int_varargs">variable argument handling intrinsic</a> functions.</p>
452 <table class="layout">
455 <tt>int (int)</tt> <br/>
456 <tt>float (int, int *) *</tt><br/>
457 <tt>int (sbyte *, ...)</tt><br/>
460 function taking an <tt>int</tt>, returning an <tt>int</tt><br/>
461 <a href="#t_pointer">Pointer</a> to a function that takes an
462 <tt>int</tt> and a <a href="#t_pointer">pointer</a> to <tt>int</tt>,
463 returning <tt>float</tt>.<br/>
464 A vararg function that takes at least one <a href="#t_pointer">pointer</a>
465 to <tt>sbyte</tt> (signed char in C), which returns an integer. This is
466 the signature for <tt>printf</tt> in LLVM.<br/>
472 <!-- _______________________________________________________________________ -->
473 <div class="doc_subsubsection"> <a name="t_struct">Structure Type</a> </div>
474 <div class="doc_text">
476 <p>The structure type is used to represent a collection of data members
477 together in memory. The packing of the field types is defined to match
478 the ABI of the underlying processor. The elements of a structure may
479 be any type that has a size.</p>
480 <p>Structures are accessed using '<tt><a href="#i_load">load</a></tt>
481 and '<tt><a href="#i_store">store</a></tt>' by getting a pointer to a
482 field with the '<tt><a href="#i_getelementptr">getelementptr</a></tt>'
485 <pre> { <type list> }<br></pre>
487 <table class="layout">
490 <tt>{ int, int, int }</tt><br/>
491 <tt>{ float, int (int) * }</tt><br/>
494 a triple of three <tt>int</tt> values<br/>
495 A pair, where the first element is a <tt>float</tt> and the second element
496 is a <a href="#t_pointer">pointer</a> to a <a href="#t_function">function</a>
497 that takes an <tt>int</tt>, returning an <tt>int</tt>.<br/>
503 <!-- _______________________________________________________________________ -->
504 <div class="doc_subsubsection"> <a name="t_pointer">Pointer Type</a> </div>
505 <div class="doc_text">
507 <p>As in many languages, the pointer type represents a pointer or
508 reference to another object, which must live in memory.</p>
510 <pre> <type> *<br></pre>
512 <table class="layout">
515 <tt>[4x int]*</tt><br/>
516 <tt>int (int *) *</tt><br/>
519 A <a href="#t_pointer">pointer</a> to <a href="#t_array">array</a> of
520 four <tt>int</tt> values<br/>
521 A <a href="#t_pointer">pointer</a> to a <a
522 href="#t_function">function</a> that takes an <tt>int</tt>, returning an
529 <!-- _______________________________________________________________________ -->
530 <div class="doc_subsubsection"> <a name="t_packed">Packed Type</a> </div>
531 <div class="doc_text">
533 <p>A packed type is a simple derived type that represents a vector
534 of elements. Packed types are used when multiple primitive data
535 are operated in parallel using a single instruction (SIMD).
536 A packed type requires a size (number of
537 elements) and an underlying primitive data type. Packed types are
538 considered <a href="#t_firstclass">first class</a>.</p>
540 <pre> < <# elements> x <elementtype> ><br></pre>
541 <p>The number of elements is a constant integer value, elementtype may
542 be any integral or floating point type.</p>
544 <table class="layout">
547 <tt><4 x int></tt><br/>
548 <tt><8 x float></tt><br/>
549 <tt><2 x uint></tt><br/>
552 Packed vector of 4 integer values.<br/>
553 Packed vector of 8 floating-point values.<br/>
554 Packed vector of 2 unsigned integer values.<br/>
560 <!-- *********************************************************************** -->
561 <div class="doc_section"> <a name="highlevel">High Level Structure</a> </div>
562 <!-- *********************************************************************** -->
563 <!-- ======================================================================= -->
564 <div class="doc_subsection"> <a name="modulestructure">Module Structure</a>
566 <div class="doc_text">
567 <p>LLVM programs are composed of "Module"s, each of which is a
568 translation unit of the input programs. Each module consists of
569 functions, global variables, and symbol table entries. Modules may be
570 combined together with the LLVM linker, which merges function (and
571 global variable) definitions, resolves forward declarations, and merges
572 symbol table entries. Here is an example of the "hello world" module:</p>
573 <pre><i>; Declare the string constant as a global constant...</i>
574 <a href="#identifiers">%.LC0</a> = <a href="#linkage_internal">internal</a> <a
575 href="#globalvars">constant</a> <a href="#t_array">[13 x sbyte]</a> c"hello world\0A\00" <i>; [13 x sbyte]*</i>
577 <i>; External declaration of the puts function</i>
578 <a href="#functionstructure">declare</a> int %puts(sbyte*) <i>; int(sbyte*)* </i>
580 <i>; Definition of main function</i>
581 int %main() { <i>; int()* </i>
582 <i>; Convert [13x sbyte]* to sbyte *...</i>
584 href="#i_getelementptr">getelementptr</a> [13 x sbyte]* %.LC0, long 0, long 0 <i>; sbyte*</i>
586 <i>; Call puts function to write out the string to stdout...</i>
588 href="#i_call">call</a> int %puts(sbyte* %cast210) <i>; int</i>
590 href="#i_ret">ret</a> int 0<br>}<br></pre>
591 <p>This example is made up of a <a href="#globalvars">global variable</a>
592 named "<tt>.LC0</tt>", an external declaration of the "<tt>puts</tt>"
593 function, and a <a href="#functionstructure">function definition</a>
594 for "<tt>main</tt>".</p>
595 <a name="linkage"> In general, a module is made up of a list of global
596 values, where both functions and global variables are global values.
597 Global values are represented by a pointer to a memory location (in
598 this case, a pointer to an array of char, and a pointer to a function),
599 and have one of the following linkage types:</a>
602 <dt><tt><b><a name="linkage_internal">internal</a></b></tt> </dt>
603 <dd>Global values with internal linkage are only directly accessible
604 by objects in the current module. In particular, linking code into a
605 module with an internal global value may cause the internal to be
606 renamed as necessary to avoid collisions. Because the symbol is
607 internal to the module, all references can be updated. This
608 corresponds to the notion of the '<tt>static</tt>' keyword in C, or the
609 idea of "anonymous namespaces" in C++.
612 <dt><tt><b><a name="linkage_linkonce">linkonce</a></b></tt>: </dt>
613 <dd>"<tt>linkonce</tt>" linkage is similar to <tt>internal</tt>
614 linkage, with the twist that linking together two modules defining the
615 same <tt>linkonce</tt> globals will cause one of the globals to be
616 discarded. This is typically used to implement inline functions.
617 Unreferenced <tt>linkonce</tt> globals are allowed to be discarded.
620 <dt><tt><b><a name="linkage_weak">weak</a></b></tt>: </dt>
621 <dd>"<tt>weak</tt>" linkage is exactly the same as <tt>linkonce</tt>
622 linkage, except that unreferenced <tt>weak</tt> globals may not be
623 discarded. This is used to implement constructs in C such as "<tt>int
624 X;</tt>" at global scope.
627 <dt><tt><b><a name="linkage_appending">appending</a></b></tt>: </dt>
628 <dd>"<tt>appending</tt>" linkage may only be applied to global
629 variables of pointer to array type. When two global variables with
630 appending linkage are linked together, the two global arrays are
631 appended together. This is the LLVM, typesafe, equivalent of having
632 the system linker append together "sections" with identical names when
636 <dt><tt><b><a name="linkage_external">externally visible</a></b></tt>:</dt>
637 <dd>If none of the above identifiers are used, the global is
638 externally visible, meaning that it participates in linkage and can be
639 used to resolve external symbol references.
644 <p><a name="linkage_external">For example, since the "<tt>.LC0</tt>"
645 variable is defined to be internal, if another module defined a "<tt>.LC0</tt>"
646 variable and was linked with this one, one of the two would be renamed,
647 preventing a collision. Since "<tt>main</tt>" and "<tt>puts</tt>" are
648 external (i.e., lacking any linkage declarations), they are accessible
649 outside of the current module. It is illegal for a function <i>declaration</i>
650 to have any linkage type other than "externally visible".</a></p>
653 <!-- ======================================================================= -->
654 <div class="doc_subsection">
655 <a name="globalvars">Global Variables</a>
658 <div class="doc_text">
660 <p>Global variables define regions of memory allocated at compilation
661 time instead of run-time. Global variables may optionally be
662 initialized. A variable may be defined as a global "constant", which
663 indicates that the contents of the variable will never be modified
664 (opening options for optimization).</p>
666 <p>As SSA values, global variables define pointer values that are in
667 scope (i.e. they dominate) for all basic blocks in the program. Global
668 variables always define a pointer to their "content" type because they
669 describe a region of memory, and all memory objects in LLVM are
670 accessed through pointers.</p>
675 <!-- ======================================================================= -->
676 <div class="doc_subsection">
677 <a name="functionstructure">Functions</a>
680 <div class="doc_text">
682 <p>LLVM function definitions are composed of a (possibly empty) argument list,
683 an opening curly brace, a list of basic blocks, and a closing curly brace. LLVM
684 function declarations are defined with the "<tt>declare</tt>" keyword, a
685 function name, and a function signature.</p>
687 <p>A function definition contains a list of basic blocks, forming the CFG for
688 the function. Each basic block may optionally start with a label (giving the
689 basic block a symbol table entry), contains a list of instructions, and ends
690 with a <a href="#terminators">terminator</a> instruction (such as a branch or
691 function return).</p>
693 <p>The first basic block in program is special in two ways: it is immediately
694 executed on entrance to the function, and it is not allowed to have predecessor
695 basic blocks (i.e. there can not be any branches to the entry block of a
696 function). Because the block can have no predecessors, it also cannot have any
697 <a href="#i_phi">PHI nodes</a>.</p>
699 <p>LLVM functions are identified by their name and type signature. Hence, two
700 functions with the same name but different parameter lists or return values are
701 considered different functions, and LLVM will resolves references to each
707 <!-- *********************************************************************** -->
708 <div class="doc_section"> <a name="instref">Instruction Reference</a> </div>
709 <!-- *********************************************************************** -->
710 <div class="doc_text">
711 <p>The LLVM instruction set consists of several different
712 classifications of instructions: <a href="#terminators">terminator
713 instructions</a>, <a href="#binaryops">binary instructions</a>, <a
714 href="#memoryops">memory instructions</a>, and <a href="#otherops">other
715 instructions</a>.</p>
717 <!-- ======================================================================= -->
718 <div class="doc_subsection"> <a name="terminators">Terminator
719 Instructions</a> </div>
720 <div class="doc_text">
721 <p>As mentioned <a href="#functionstructure">previously</a>, every
722 basic block in a program ends with a "Terminator" instruction, which
723 indicates which block should be executed after the current block is
724 finished. These terminator instructions typically yield a '<tt>void</tt>'
725 value: they produce control flow, not values (the one exception being
726 the '<a href="#i_invoke"><tt>invoke</tt></a>' instruction).</p>
727 <p>There are five different terminator instructions: the '<a
728 href="#i_ret"><tt>ret</tt></a>' instruction, the '<a href="#i_br"><tt>br</tt></a>'
729 instruction, the '<a href="#i_switch"><tt>switch</tt></a>' instruction,
730 the '<a href="#i_invoke"><tt>invoke</tt></a>' instruction, the '<a
731 href="#i_unwind"><tt>unwind</tt></a>' instruction, and the '<a
732 href="#i_unreachable"><tt>unreachable</tt></a>' instruction.</p>
734 <!-- _______________________________________________________________________ -->
735 <div class="doc_subsubsection"> <a name="i_ret">'<tt>ret</tt>'
736 Instruction</a> </div>
737 <div class="doc_text">
739 <pre> ret <type> <value> <i>; Return a value from a non-void function</i>
740 ret void <i>; Return from void function</i>
743 <p>The '<tt>ret</tt>' instruction is used to return control flow (and a
744 value) from a function, back to the caller.</p>
745 <p>There are two forms of the '<tt>ret</tt>' instruction: one that
746 returns a value and then causes control flow, and one that just causes
747 control flow to occur.</p>
749 <p>The '<tt>ret</tt>' instruction may return any '<a
750 href="#t_firstclass">first class</a>' type. Notice that a function is
751 not <a href="#wellformed">well formed</a> if there exists a '<tt>ret</tt>'
752 instruction inside of the function that returns a value that does not
753 match the return type of the function.</p>
755 <p>When the '<tt>ret</tt>' instruction is executed, control flow
756 returns back to the calling function's context. If the caller is a "<a
757 href="#i_call"><tt>call</tt></a>" instruction, execution continues at
758 the instruction after the call. If the caller was an "<a
759 href="#i_invoke"><tt>invoke</tt></a>" instruction, execution continues
760 at the beginning "normal" of the destination block. If the instruction
761 returns a value, that value shall set the call or invoke instruction's
764 <pre> ret int 5 <i>; Return an integer value of 5</i>
765 ret void <i>; Return from a void function</i>
768 <!-- _______________________________________________________________________ -->
769 <div class="doc_subsubsection"> <a name="i_br">'<tt>br</tt>' Instruction</a> </div>
770 <div class="doc_text">
772 <pre> br bool <cond>, label <iftrue>, label <iffalse><br> br label <dest> <i>; Unconditional branch</i>
775 <p>The '<tt>br</tt>' instruction is used to cause control flow to
776 transfer to a different basic block in the current function. There are
777 two forms of this instruction, corresponding to a conditional branch
778 and an unconditional branch.</p>
780 <p>The conditional branch form of the '<tt>br</tt>' instruction takes a
781 single '<tt>bool</tt>' value and two '<tt>label</tt>' values. The
782 unconditional form of the '<tt>br</tt>' instruction takes a single '<tt>label</tt>'
783 value as a target.</p>
785 <p>Upon execution of a conditional '<tt>br</tt>' instruction, the '<tt>bool</tt>'
786 argument is evaluated. If the value is <tt>true</tt>, control flows
787 to the '<tt>iftrue</tt>' <tt>label</tt> argument. If "cond" is <tt>false</tt>,
788 control flows to the '<tt>iffalse</tt>' <tt>label</tt> argument.</p>
790 <pre>Test:<br> %cond = <a href="#i_setcc">seteq</a> int %a, %b<br> br bool %cond, label %IfEqual, label %IfUnequal<br>IfEqual:<br> <a
791 href="#i_ret">ret</a> int 1<br>IfUnequal:<br> <a href="#i_ret">ret</a> int 0<br></pre>
793 <!-- _______________________________________________________________________ -->
794 <div class="doc_subsubsection">
795 <a name="i_switch">'<tt>switch</tt>' Instruction</a>
798 <div class="doc_text">
802 switch <intty> <value>, label <defaultdest> [ <intty> <val>, label <dest> ... ]
807 <p>The '<tt>switch</tt>' instruction is used to transfer control flow to one of
808 several different places. It is a generalization of the '<tt>br</tt>'
809 instruction, allowing a branch to occur to one of many possible
815 <p>The '<tt>switch</tt>' instruction uses three parameters: an integer
816 comparison value '<tt>value</tt>', a default '<tt>label</tt>' destination, and
817 an array of pairs of comparison value constants and '<tt>label</tt>'s. The
818 table is not allowed to contain duplicate constant entries.</p>
822 <p>The <tt>switch</tt> instruction specifies a table of values and
823 destinations. When the '<tt>switch</tt>' instruction is executed, this
824 table is searched for the given value. If the value is found, control flow is
825 transfered to the corresponding destination; otherwise, control flow is
826 transfered to the default destination.</p>
828 <h5>Implementation:</h5>
830 <p>Depending on properties of the target machine and the particular
831 <tt>switch</tt> instruction, this instruction may be code generated in different
832 ways. For example, it could be generated as a series of chained conditional
833 branches or with a lookup table.</p>
838 <i>; Emulate a conditional br instruction</i>
839 %Val = <a href="#i_cast">cast</a> bool %value to int
840 switch int %Val, label %truedest [int 0, label %falsedest ]
842 <i>; Emulate an unconditional br instruction</i>
843 switch uint 0, label %dest [ ]
845 <i>; Implement a jump table:</i>
846 switch uint %val, label %otherwise [ uint 0, label %onzero
848 uint 2, label %ontwo ]
851 <!-- _______________________________________________________________________ -->
852 <div class="doc_subsubsection"> <a name="i_invoke">'<tt>invoke</tt>'
853 Instruction</a> </div>
854 <div class="doc_text">
856 <pre> <result> = invoke <ptr to function ty> %<function ptr val>(<function args>)<br> to label <normal label> except label <exception label><br></pre>
858 <p>The '<tt>invoke</tt>' instruction causes control to transfer to a
859 specified function, with the possibility of control flow transfer to
860 either the '<tt>normal</tt>' <tt>label</tt> label or the '<tt>exception</tt>'<tt>label</tt>.
861 If the callee function returns with the "<tt><a href="#i_ret">ret</a></tt>"
862 instruction, control flow will return to the "normal" label. If the
863 callee (or any indirect callees) returns with the "<a href="#i_unwind"><tt>unwind</tt></a>"
864 instruction, control is interrupted, and continued at the dynamically
865 nearest "except" label.</p>
867 <p>This instruction requires several arguments:</p>
869 <li>'<tt>ptr to function ty</tt>': shall be the signature of the
870 pointer to function value being invoked. In most cases, this is a
871 direct function invocation, but indirect <tt>invoke</tt>s are just as
872 possible, branching off an arbitrary pointer to function value. </li>
873 <li>'<tt>function ptr val</tt>': An LLVM value containing a pointer
874 to a function to be invoked. </li>
875 <li>'<tt>function args</tt>': argument list whose types match the
876 function signature argument types. If the function signature indicates
877 the function accepts a variable number of arguments, the extra
878 arguments can be specified. </li>
879 <li>'<tt>normal label</tt>': the label reached when the called
880 function executes a '<tt><a href="#i_ret">ret</a></tt>' instruction. </li>
881 <li>'<tt>exception label</tt>': the label reached when a callee
882 returns with the <a href="#i_unwind"><tt>unwind</tt></a> instruction. </li>
885 <p>This instruction is designed to operate as a standard '<tt><a
886 href="#i_call">call</a></tt>' instruction in most regards. The
887 primary difference is that it establishes an association with a label,
888 which is used by the runtime library to unwind the stack.</p>
889 <p>This instruction is used in languages with destructors to ensure
890 that proper cleanup is performed in the case of either a <tt>longjmp</tt>
891 or a thrown exception. Additionally, this is important for
892 implementation of '<tt>catch</tt>' clauses in high-level languages that
895 <pre> %retval = invoke int %Test(int 15)<br> to label %Continue<br> except label %TestCleanup <i>; {int}:retval set</i>
900 <!-- _______________________________________________________________________ -->
902 <div class="doc_subsubsection"> <a name="i_unwind">'<tt>unwind</tt>'
903 Instruction</a> </div>
905 <div class="doc_text">
914 <p>The '<tt>unwind</tt>' instruction unwinds the stack, continuing control flow
915 at the first callee in the dynamic call stack which used an <a
916 href="#i_invoke"><tt>invoke</tt></a> instruction to perform the call. This is
917 primarily used to implement exception handling.</p>
921 <p>The '<tt>unwind</tt>' intrinsic causes execution of the current function to
922 immediately halt. The dynamic call stack is then searched for the first <a
923 href="#i_invoke"><tt>invoke</tt></a> instruction on the call stack. Once found,
924 execution continues at the "exceptional" destination block specified by the
925 <tt>invoke</tt> instruction. If there is no <tt>invoke</tt> instruction in the
926 dynamic call chain, undefined behavior results.</p>
929 <!-- _______________________________________________________________________ -->
931 <div class="doc_subsubsection"> <a name="i_unreachable">'<tt>unreachable</tt>'
932 Instruction</a> </div>
934 <div class="doc_text">
943 <p>The '<tt>unreachable</tt>' instruction has no defined semantics. This
944 instruction is used to inform the optimizer that a particular portion of the
945 code is not reachable. This can be used to indicate that the code after a
946 no-return function cannot be reached, and other facts.</p>
950 <p>The '<tt>unreachable</tt>' instruction has no defined semantics.</p>
955 <!-- ======================================================================= -->
956 <div class="doc_subsection"> <a name="binaryops">Binary Operations</a> </div>
957 <div class="doc_text">
958 <p>Binary operators are used to do most of the computation in a
959 program. They require two operands, execute an operation on them, and
960 produce a single value. Although, that single value might represent
961 multiple data, as is the case with the <a href="#t_packed">packed</a> data type.
962 The result value of a binary operator is not
963 necessarily the same type as its operands.</p>
964 <p>There are several different binary operators:</p>
966 <!-- _______________________________________________________________________ -->
967 <div class="doc_subsubsection"> <a name="i_add">'<tt>add</tt>'
968 Instruction</a> </div>
969 <div class="doc_text">
971 <pre> <result> = add <ty> <var1>, <var2> <i>; yields {ty}:result</i>
974 <p>The '<tt>add</tt>' instruction returns the sum of its two operands.</p>
976 <p>The two arguments to the '<tt>add</tt>' instruction must be either <a
977 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a> values.
978 This instruction can also take <a href="#t_packed">packed</a> versions of the values.
979 Both arguments must have identical types.</p>
981 <p>The value produced is the integer or floating point sum of the two
984 <pre> <result> = add int 4, %var <i>; yields {int}:result = 4 + %var</i>
987 <!-- _______________________________________________________________________ -->
988 <div class="doc_subsubsection"> <a name="i_sub">'<tt>sub</tt>'
989 Instruction</a> </div>
990 <div class="doc_text">
992 <pre> <result> = sub <ty> <var1>, <var2> <i>; yields {ty}:result</i>
995 <p>The '<tt>sub</tt>' instruction returns the difference of its two
997 <p>Note that the '<tt>sub</tt>' instruction is used to represent the '<tt>neg</tt>'
998 instruction present in most other intermediate representations.</p>
1000 <p>The two arguments to the '<tt>sub</tt>' instruction must be either <a
1001 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
1003 This instruction can also take <a href="#t_packed">packed</a> versions of the values.
1004 Both arguments must have identical types.</p>
1006 <p>The value produced is the integer or floating point difference of
1007 the two operands.</p>
1009 <pre> <result> = sub int 4, %var <i>; yields {int}:result = 4 - %var</i>
1010 <result> = sub int 0, %val <i>; yields {int}:result = -%var</i>
1013 <!-- _______________________________________________________________________ -->
1014 <div class="doc_subsubsection"> <a name="i_mul">'<tt>mul</tt>'
1015 Instruction</a> </div>
1016 <div class="doc_text">
1018 <pre> <result> = mul <ty> <var1>, <var2> <i>; yields {ty}:result</i>
1021 <p>The '<tt>mul</tt>' instruction returns the product of its two
1024 <p>The two arguments to the '<tt>mul</tt>' instruction must be either <a
1025 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
1027 This instruction can also take <a href="#t_packed">packed</a> versions of the values.
1028 Both arguments must have identical types.</p>
1030 <p>The value produced is the integer or floating point product of the
1032 <p>There is no signed vs unsigned multiplication. The appropriate
1033 action is taken based on the type of the operand.</p>
1035 <pre> <result> = mul int 4, %var <i>; yields {int}:result = 4 * %var</i>
1038 <!-- _______________________________________________________________________ -->
1039 <div class="doc_subsubsection"> <a name="i_div">'<tt>div</tt>'
1040 Instruction</a> </div>
1041 <div class="doc_text">
1043 <pre> <result> = div <ty> <var1>, <var2> <i>; yields {ty}:result</i>
1046 <p>The '<tt>div</tt>' instruction returns the quotient of its two
1049 <p>The two arguments to the '<tt>div</tt>' instruction must be either <a
1050 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
1052 This instruction can also take <a href="#t_packed">packed</a> versions of the values.
1053 Both arguments must have identical types.</p>
1055 <p>The value produced is the integer or floating point quotient of the
1058 <pre> <result> = div int 4, %var <i>; yields {int}:result = 4 / %var</i>
1061 <!-- _______________________________________________________________________ -->
1062 <div class="doc_subsubsection"> <a name="i_rem">'<tt>rem</tt>'
1063 Instruction</a> </div>
1064 <div class="doc_text">
1066 <pre> <result> = rem <ty> <var1>, <var2> <i>; yields {ty}:result</i>
1069 <p>The '<tt>rem</tt>' instruction returns the remainder from the
1070 division of its two operands.</p>
1072 <p>The two arguments to the '<tt>rem</tt>' instruction must be either <a
1073 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
1075 This instruction can also take <a href="#t_packed">packed</a> versions of the values.
1076 Both arguments must have identical types.</p>
1078 <p>This returns the <i>remainder</i> of a division (where the result
1079 has the same sign as the divisor), not the <i>modulus</i> (where the
1080 result has the same sign as the dividend) of a value. For more
1081 information about the difference, see: <a
1082 href="http://mathforum.org/dr.math/problems/anne.4.28.99.html">The
1085 <pre> <result> = rem int 4, %var <i>; yields {int}:result = 4 % %var</i>
1088 <!-- _______________________________________________________________________ -->
1089 <div class="doc_subsubsection"> <a name="i_setcc">'<tt>set<i>cc</i></tt>'
1090 Instructions</a> </div>
1091 <div class="doc_text">
1093 <pre> <result> = seteq <ty> <var1>, <var2> <i>; yields {bool}:result</i>
1094 <result> = setne <ty> <var1>, <var2> <i>; yields {bool}:result</i>
1095 <result> = setlt <ty> <var1>, <var2> <i>; yields {bool}:result</i>
1096 <result> = setgt <ty> <var1>, <var2> <i>; yields {bool}:result</i>
1097 <result> = setle <ty> <var1>, <var2> <i>; yields {bool}:result</i>
1098 <result> = setge <ty> <var1>, <var2> <i>; yields {bool}:result</i>
1101 <p>The '<tt>set<i>cc</i></tt>' family of instructions returns a boolean
1102 value based on a comparison of their two operands.</p>
1104 <p>The two arguments to the '<tt>set<i>cc</i></tt>' instructions must
1105 be of <a href="#t_firstclass">first class</a> type (it is not possible
1106 to compare '<tt>label</tt>'s, '<tt>array</tt>'s, '<tt>structure</tt>'
1107 or '<tt>void</tt>' values, etc...). Both arguments must have identical
1110 <p>The '<tt>seteq</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1111 value if both operands are equal.<br>
1112 The '<tt>setne</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1113 value if both operands are unequal.<br>
1114 The '<tt>setlt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1115 value if the first operand is less than the second operand.<br>
1116 The '<tt>setgt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1117 value if the first operand is greater than the second operand.<br>
1118 The '<tt>setle</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1119 value if the first operand is less than or equal to the second operand.<br>
1120 The '<tt>setge</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1121 value if the first operand is greater than or equal to the second
1124 <pre> <result> = seteq int 4, 5 <i>; yields {bool}:result = false</i>
1125 <result> = setne float 4, 5 <i>; yields {bool}:result = true</i>
1126 <result> = setlt uint 4, 5 <i>; yields {bool}:result = true</i>
1127 <result> = setgt sbyte 4, 5 <i>; yields {bool}:result = false</i>
1128 <result> = setle sbyte 4, 5 <i>; yields {bool}:result = true</i>
1129 <result> = setge sbyte 4, 5 <i>; yields {bool}:result = false</i>
1132 <!-- ======================================================================= -->
1133 <div class="doc_subsection"> <a name="bitwiseops">Bitwise Binary
1134 Operations</a> </div>
1135 <div class="doc_text">
1136 <p>Bitwise binary operators are used to do various forms of
1137 bit-twiddling in a program. They are generally very efficient
1138 instructions, and can commonly be strength reduced from other
1139 instructions. They require two operands, execute an operation on them,
1140 and produce a single value. The resulting value of the bitwise binary
1141 operators is always the same type as its first operand.</p>
1143 <!-- _______________________________________________________________________ -->
1144 <div class="doc_subsubsection"> <a name="i_and">'<tt>and</tt>'
1145 Instruction</a> </div>
1146 <div class="doc_text">
1148 <pre> <result> = and <ty> <var1>, <var2> <i>; yields {ty}:result</i>
1151 <p>The '<tt>and</tt>' instruction returns the bitwise logical and of
1152 its two operands.</p>
1154 <p>The two arguments to the '<tt>and</tt>' instruction must be <a
1155 href="#t_integral">integral</a> values. Both arguments must have
1156 identical types.</p>
1158 <p>The truth table used for the '<tt>and</tt>' instruction is:</p>
1160 <div style="align: center">
1161 <table border="1" cellspacing="0" cellpadding="4">
1192 <pre> <result> = and int 4, %var <i>; yields {int}:result = 4 & %var</i>
1193 <result> = and int 15, 40 <i>; yields {int}:result = 8</i>
1194 <result> = and int 4, 8 <i>; yields {int}:result = 0</i>
1197 <!-- _______________________________________________________________________ -->
1198 <div class="doc_subsubsection"> <a name="i_or">'<tt>or</tt>' Instruction</a> </div>
1199 <div class="doc_text">
1201 <pre> <result> = or <ty> <var1>, <var2> <i>; yields {ty}:result</i>
1204 <p>The '<tt>or</tt>' instruction returns the bitwise logical inclusive
1205 or of its two operands.</p>
1207 <p>The two arguments to the '<tt>or</tt>' instruction must be <a
1208 href="#t_integral">integral</a> values. Both arguments must have
1209 identical types.</p>
1211 <p>The truth table used for the '<tt>or</tt>' instruction is:</p>
1213 <div style="align: center">
1214 <table border="1" cellspacing="0" cellpadding="4">
1245 <pre> <result> = or int 4, %var <i>; yields {int}:result = 4 | %var</i>
1246 <result> = or int 15, 40 <i>; yields {int}:result = 47</i>
1247 <result> = or int 4, 8 <i>; yields {int}:result = 12</i>
1250 <!-- _______________________________________________________________________ -->
1251 <div class="doc_subsubsection"> <a name="i_xor">'<tt>xor</tt>'
1252 Instruction</a> </div>
1253 <div class="doc_text">
1255 <pre> <result> = xor <ty> <var1>, <var2> <i>; yields {ty}:result</i>
1258 <p>The '<tt>xor</tt>' instruction returns the bitwise logical exclusive
1259 or of its two operands. The <tt>xor</tt> is used to implement the
1260 "one's complement" operation, which is the "~" operator in C.</p>
1262 <p>The two arguments to the '<tt>xor</tt>' instruction must be <a
1263 href="#t_integral">integral</a> values. Both arguments must have
1264 identical types.</p>
1266 <p>The truth table used for the '<tt>xor</tt>' instruction is:</p>
1268 <div style="align: center">
1269 <table border="1" cellspacing="0" cellpadding="4">
1301 <pre> <result> = xor int 4, %var <i>; yields {int}:result = 4 ^ %var</i>
1302 <result> = xor int 15, 40 <i>; yields {int}:result = 39</i>
1303 <result> = xor int 4, 8 <i>; yields {int}:result = 12</i>
1304 <result> = xor int %V, -1 <i>; yields {int}:result = ~%V</i>
1307 <!-- _______________________________________________________________________ -->
1308 <div class="doc_subsubsection"> <a name="i_shl">'<tt>shl</tt>'
1309 Instruction</a> </div>
1310 <div class="doc_text">
1312 <pre> <result> = shl <ty> <var1>, ubyte <var2> <i>; yields {ty}:result</i>
1315 <p>The '<tt>shl</tt>' instruction returns the first operand shifted to
1316 the left a specified number of bits.</p>
1318 <p>The first argument to the '<tt>shl</tt>' instruction must be an <a
1319 href="#t_integer">integer</a> type. The second argument must be an '<tt>ubyte</tt>'
1322 <p>The value produced is <tt>var1</tt> * 2<sup><tt>var2</tt></sup>.</p>
1324 <pre> <result> = shl int 4, ubyte %var <i>; yields {int}:result = 4 << %var</i>
1325 <result> = shl int 4, ubyte 2 <i>; yields {int}:result = 16</i>
1326 <result> = shl int 1, ubyte 10 <i>; yields {int}:result = 1024</i>
1329 <!-- _______________________________________________________________________ -->
1330 <div class="doc_subsubsection"> <a name="i_shr">'<tt>shr</tt>'
1331 Instruction</a> </div>
1332 <div class="doc_text">
1334 <pre> <result> = shr <ty> <var1>, ubyte <var2> <i>; yields {ty}:result</i>
1337 <p>The '<tt>shr</tt>' instruction returns the first operand shifted to
1338 the right a specified number of bits.</p>
1340 <p>The first argument to the '<tt>shr</tt>' instruction must be an <a
1341 href="#t_integer">integer</a> type. The second argument must be an '<tt>ubyte</tt>'
1344 <p>If the first argument is a <a href="#t_signed">signed</a> type, the
1345 most significant bit is duplicated in the newly free'd bit positions.
1346 If the first argument is unsigned, zero bits shall fill the empty
1349 <pre> <result> = shr int 4, ubyte %var <i>; yields {int}:result = 4 >> %var</i>
1350 <result> = shr uint 4, ubyte 1 <i>; yields {uint}:result = 2</i>
1351 <result> = shr int 4, ubyte 2 <i>; yields {int}:result = 1</i>
1352 <result> = shr sbyte 4, ubyte 3 <i>; yields {sbyte}:result = 0</i>
1353 <result> = shr sbyte -2, ubyte 1 <i>; yields {sbyte}:result = -1</i>
1356 <!-- ======================================================================= -->
1357 <div class="doc_subsection"> <a name="memoryops">Memory Access
1358 Operations</a></div>
1359 <div class="doc_text">
1360 <p>A key design point of an SSA-based representation is how it
1361 represents memory. In LLVM, no memory locations are in SSA form, which
1362 makes things very simple. This section describes how to read, write,
1363 allocate and free memory in LLVM.</p>
1365 <!-- _______________________________________________________________________ -->
1366 <div class="doc_subsubsection"> <a name="i_malloc">'<tt>malloc</tt>'
1367 Instruction</a> </div>
1368 <div class="doc_text">
1370 <pre> <result> = malloc <type>, uint <NumElements> <i>; yields {type*}:result</i>
1371 <result> = malloc <type> <i>; yields {type*}:result</i>
1374 <p>The '<tt>malloc</tt>' instruction allocates memory from the system
1375 heap and returns a pointer to it.</p>
1377 <p>The '<tt>malloc</tt>' instruction allocates <tt>sizeof(<type>)*NumElements</tt>
1378 bytes of memory from the operating system and returns a pointer of the
1379 appropriate type to the program. The second form of the instruction is
1380 a shorter version of the first instruction that defaults to allocating
1382 <p>'<tt>type</tt>' must be a sized type.</p>
1384 <p>Memory is allocated using the system "<tt>malloc</tt>" function, and
1385 a pointer is returned.</p>
1387 <pre> %array = malloc [4 x ubyte ] <i>; yields {[%4 x ubyte]*}:array</i>
1390 href="#i_add">add</a> uint 2, 2 <i>; yields {uint}:size = uint 4</i>
1391 %array1 = malloc ubyte, uint 4 <i>; yields {ubyte*}:array1</i>
1392 %array2 = malloc [12 x ubyte], uint %size <i>; yields {[12 x ubyte]*}:array2</i>
1395 <!-- _______________________________________________________________________ -->
1396 <div class="doc_subsubsection"> <a name="i_free">'<tt>free</tt>'
1397 Instruction</a> </div>
1398 <div class="doc_text">
1400 <pre> free <type> <value> <i>; yields {void}</i>
1403 <p>The '<tt>free</tt>' instruction returns memory back to the unused
1404 memory heap, to be reallocated in the future.</p>
1407 <p>'<tt>value</tt>' shall be a pointer value that points to a value
1408 that was allocated with the '<tt><a href="#i_malloc">malloc</a></tt>'
1411 <p>Access to the memory pointed to by the pointer is not longer defined
1412 after this instruction executes.</p>
1414 <pre> %array = <a href="#i_malloc">malloc</a> [4 x ubyte] <i>; yields {[4 x ubyte]*}:array</i>
1415 free [4 x ubyte]* %array
1418 <!-- _______________________________________________________________________ -->
1419 <div class="doc_subsubsection"> <a name="i_alloca">'<tt>alloca</tt>'
1420 Instruction</a> </div>
1421 <div class="doc_text">
1423 <pre> <result> = alloca <type>, uint <NumElements> <i>; yields {type*}:result</i>
1424 <result> = alloca <type> <i>; yields {type*}:result</i>
1427 <p>The '<tt>alloca</tt>' instruction allocates memory on the current
1428 stack frame of the procedure that is live until the current function
1429 returns to its caller.</p>
1431 <p>The the '<tt>alloca</tt>' instruction allocates <tt>sizeof(<type>)*NumElements</tt>
1432 bytes of memory on the runtime stack, returning a pointer of the
1433 appropriate type to the program. The second form of the instruction is
1434 a shorter version of the first that defaults to allocating one element.</p>
1435 <p>'<tt>type</tt>' may be any sized type.</p>
1437 <p>Memory is allocated, a pointer is returned. '<tt>alloca</tt>'d
1438 memory is automatically released when the function returns. The '<tt>alloca</tt>'
1439 instruction is commonly used to represent automatic variables that must
1440 have an address available. When the function returns (either with the <tt><a
1441 href="#i_ret">ret</a></tt> or <tt><a href="#i_invoke">invoke</a></tt>
1442 instructions), the memory is reclaimed.</p>
1444 <pre> %ptr = alloca int <i>; yields {int*}:ptr</i>
1445 %ptr = alloca int, uint 4 <i>; yields {int*}:ptr</i>
1448 <!-- _______________________________________________________________________ -->
1449 <div class="doc_subsubsection"> <a name="i_load">'<tt>load</tt>'
1450 Instruction</a> </div>
1451 <div class="doc_text">
1453 <pre> <result> = load <ty>* <pointer><br> <result> = volatile load <ty>* <pointer><br></pre>
1455 <p>The '<tt>load</tt>' instruction is used to read from memory.</p>
1457 <p>The argument to the '<tt>load</tt>' instruction specifies the memory
1458 address to load from. The pointer must point to a <a
1459 href="#t_firstclass">first class</a> type. If the <tt>load</tt> is
1460 marked as <tt>volatile</tt> then the optimizer is not allowed to modify
1461 the number or order of execution of this <tt>load</tt> with other
1462 volatile <tt>load</tt> and <tt><a href="#i_store">store</a></tt>
1465 <p>The location of memory pointed to is loaded.</p>
1467 <pre> %ptr = <a href="#i_alloca">alloca</a> int <i>; yields {int*}:ptr</i>
1469 href="#i_store">store</a> int 3, int* %ptr <i>; yields {void}</i>
1470 %val = load int* %ptr <i>; yields {int}:val = int 3</i>
1473 <!-- _______________________________________________________________________ -->
1474 <div class="doc_subsubsection"> <a name="i_store">'<tt>store</tt>'
1475 Instruction</a> </div>
1477 <pre> store <ty> <value>, <ty>* <pointer> <i>; yields {void}</i>
1478 volatile store <ty> <value>, <ty>* <pointer> <i>; yields {void}</i>
1481 <p>The '<tt>store</tt>' instruction is used to write to memory.</p>
1483 <p>There are two arguments to the '<tt>store</tt>' instruction: a value
1484 to store and an address to store it into. The type of the '<tt><pointer></tt>'
1485 operand must be a pointer to the type of the '<tt><value></tt>'
1486 operand. If the <tt>store</tt> is marked as <tt>volatile</tt> then the
1487 optimizer is not allowed to modify the number or order of execution of
1488 this <tt>store</tt> with other volatile <tt>load</tt> and <tt><a
1489 href="#i_store">store</a></tt> instructions.</p>
1491 <p>The contents of memory are updated to contain '<tt><value></tt>'
1492 at the location specified by the '<tt><pointer></tt>' operand.</p>
1494 <pre> %ptr = <a href="#i_alloca">alloca</a> int <i>; yields {int*}:ptr</i>
1496 href="#i_store">store</a> int 3, int* %ptr <i>; yields {void}</i>
1497 %val = load int* %ptr <i>; yields {int}:val = int 3</i>
1499 <!-- _______________________________________________________________________ -->
1500 <div class="doc_subsubsection">
1501 <a name="i_getelementptr">'<tt>getelementptr</tt>' Instruction</a>
1504 <div class="doc_text">
1507 <result> = getelementptr <ty>* <ptrval>{, <ty> <idx>}*
1513 The '<tt>getelementptr</tt>' instruction is used to get the address of a
1514 subelement of an aggregate data structure.</p>
1518 <p>This instruction takes a list of integer constants that indicate what
1519 elements of the aggregate object to index to. The actual types of the arguments
1520 provided depend on the type of the first pointer argument. The
1521 '<tt>getelementptr</tt>' instruction is used to index down through the type
1522 levels of a structure. When indexing into a structure, only <tt>uint</tt>
1523 integer constants are allowed. When indexing into an array or pointer
1524 <tt>int</tt> and <tt>long</tt> indexes are allowed of any sign.</p>
1526 <p>For example, let's consider a C code fragment and how it gets
1527 compiled to LLVM:</p>
1541 int *foo(struct ST *s) {
1542 return &s[1].Z.B[5][13];
1546 <p>The LLVM code generated by the GCC frontend is:</p>
1549 %RT = type { sbyte, [10 x [20 x int]], sbyte }
1550 %ST = type { int, double, %RT }
1554 int* %foo(%ST* %s) {
1556 %reg = getelementptr %ST* %s, int 1, uint 2, uint 1, int 5, int 13
1563 <p>The index types specified for the '<tt>getelementptr</tt>' instruction depend
1564 on the pointer type that is being index into. <a href="#t_pointer">Pointer</a>
1565 and <a href="#t_array">array</a> types require <tt>uint</tt>, <tt>int</tt>,
1566 <tt>ulong</tt>, or <tt>long</tt> values, and <a href="#t_struct">structure</a>
1567 types require <tt>uint</tt> <b>constants</b>.</p>
1569 <p>In the example above, the first index is indexing into the '<tt>%ST*</tt>'
1570 type, which is a pointer, yielding a '<tt>%ST</tt>' = '<tt>{ int, double, %RT
1571 }</tt>' type, a structure. The second index indexes into the third element of
1572 the structure, yielding a '<tt>%RT</tt>' = '<tt>{ sbyte, [10 x [20 x int]],
1573 sbyte }</tt>' type, another structure. The third index indexes into the second
1574 element of the structure, yielding a '<tt>[10 x [20 x int]]</tt>' type, an
1575 array. The two dimensions of the array are subscripted into, yielding an
1576 '<tt>int</tt>' type. The '<tt>getelementptr</tt>' instruction return a pointer
1577 to this element, thus computing a value of '<tt>int*</tt>' type.</p>
1579 <p>Note that it is perfectly legal to index partially through a
1580 structure, returning a pointer to an inner element. Because of this,
1581 the LLVM code for the given testcase is equivalent to:</p>
1584 int* "foo"(%ST* %s) {
1585 %t1 = getelementptr %ST* %s, int 1 <i>; yields %ST*:%t1</i>
1586 %t2 = getelementptr %ST* %t1, int 0, uint 2 <i>; yields %RT*:%t2</i>
1587 %t3 = getelementptr %RT* %t2, int 0, uint 1 <i>; yields [10 x [20 x int]]*:%t3</i>
1588 %t4 = getelementptr [10 x [20 x int]]* %t3, int 0, int 5 <i>; yields [20 x int]*:%t4</i>
1589 %t5 = getelementptr [20 x int]* %t4, int 0, int 13 <i>; yields int*:%t5</i>
1595 <i>; yields [12 x ubyte]*:aptr</i>
1596 %aptr = getelementptr {int, [12 x ubyte]}* %sptr, long 0, uint 1
1600 <!-- ======================================================================= -->
1601 <div class="doc_subsection"> <a name="otherops">Other Operations</a> </div>
1602 <div class="doc_text">
1603 <p>The instructions in this category are the "miscellaneous"
1604 instructions, which defy better classification.</p>
1606 <!-- _______________________________________________________________________ -->
1607 <div class="doc_subsubsection"> <a name="i_phi">'<tt>phi</tt>'
1608 Instruction</a> </div>
1609 <div class="doc_text">
1611 <pre> <result> = phi <ty> [ <val0>, <label0>], ...<br></pre>
1613 <p>The '<tt>phi</tt>' instruction is used to implement the φ node in
1614 the SSA graph representing the function.</p>
1616 <p>The type of the incoming values are specified with the first type
1617 field. After this, the '<tt>phi</tt>' instruction takes a list of pairs
1618 as arguments, with one pair for each predecessor basic block of the
1619 current block. Only values of <a href="#t_firstclass">first class</a>
1620 type may be used as the value arguments to the PHI node. Only labels
1621 may be used as the label arguments.</p>
1622 <p>There must be no non-phi instructions between the start of a basic
1623 block and the PHI instructions: i.e. PHI instructions must be first in
1626 <p>At runtime, the '<tt>phi</tt>' instruction logically takes on the
1627 value specified by the parameter, depending on which basic block we
1628 came from in the last <a href="#terminators">terminator</a> instruction.</p>
1630 <pre>Loop: ; Infinite loop that counts from 0 on up...<br> %indvar = phi uint [ 0, %LoopHeader ], [ %nextindvar, %Loop ]<br> %nextindvar = add uint %indvar, 1<br> br label %Loop<br></pre>
1633 <!-- _______________________________________________________________________ -->
1634 <div class="doc_subsubsection">
1635 <a name="i_cast">'<tt>cast .. to</tt>' Instruction</a>
1638 <div class="doc_text">
1643 <result> = cast <ty> <value> to <ty2> <i>; yields ty2</i>
1649 The '<tt>cast</tt>' instruction is used as the primitive means to convert
1650 integers to floating point, change data type sizes, and break type safety (by
1658 The '<tt>cast</tt>' instruction takes a value to cast, which must be a first
1659 class value, and a type to cast it to, which must also be a <a
1660 href="#t_firstclass">first class</a> type.
1666 This instruction follows the C rules for explicit casts when determining how the
1667 data being cast must change to fit in its new container.
1671 When casting to bool, any value that would be considered true in the context of
1672 a C '<tt>if</tt>' condition is converted to the boolean '<tt>true</tt>' values,
1673 all else are '<tt>false</tt>'.
1677 When extending an integral value from a type of one signness to another (for
1678 example '<tt>sbyte</tt>' to '<tt>ulong</tt>'), the value is sign-extended if the
1679 <b>source</b> value is signed, and zero-extended if the source value is
1680 unsigned. <tt>bool</tt> values are always zero extended into either zero or
1687 %X = cast int 257 to ubyte <i>; yields ubyte:1</i>
1688 %Y = cast int 123 to bool <i>; yields bool:true</i>
1692 <!-- _______________________________________________________________________ -->
1693 <div class="doc_subsubsection">
1694 <a name="i_select">'<tt>select</tt>' Instruction</a>
1697 <div class="doc_text">
1702 <result> = select bool <cond>, <ty> <val1>, <ty> <val2> <i>; yields ty</i>
1708 The '<tt>select</tt>' instruction is used to choose one value based on a
1709 condition, without branching.
1716 The '<tt>select</tt>' instruction requires a boolean value indicating the condition, and two values of the same <a href="#t_firstclass">first class</a> type.
1722 If the boolean condition evaluates to true, the instruction returns the first
1723 value argument, otherwise it returns the second value argument.
1729 %X = select bool true, ubyte 17, ubyte 42 <i>; yields ubyte:17</i>
1737 <!-- _______________________________________________________________________ -->
1738 <div class="doc_subsubsection"> <a name="i_call">'<tt>call</tt>'
1739 Instruction</a> </div>
1740 <div class="doc_text">
1742 <pre> <result> = call <ty>* <fnptrval>(<param list>)<br></pre>
1744 <p>The '<tt>call</tt>' instruction represents a simple function call.</p>
1746 <p>This instruction requires several arguments:</p>
1749 <p>'<tt>ty</tt>': shall be the signature of the pointer to function
1750 value being invoked. The argument types must match the types implied
1751 by this signature.</p>
1754 <p>'<tt>fnptrval</tt>': An LLVM value containing a pointer to a
1755 function to be invoked. In most cases, this is a direct function
1756 invocation, but indirect <tt>call</tt>s are just as possible,
1757 calling an arbitrary pointer to function values.</p>
1760 <p>'<tt>function args</tt>': argument list whose types match the
1761 function signature argument types. If the function signature
1762 indicates the function accepts a variable number of arguments, the
1763 extra arguments can be specified.</p>
1767 <p>The '<tt>call</tt>' instruction is used to cause control flow to
1768 transfer to a specified function, with its incoming arguments bound to
1769 the specified values. Upon a '<tt><a href="#i_ret">ret</a></tt>'
1770 instruction in the called function, control flow continues with the
1771 instruction after the function call, and the return value of the
1772 function is bound to the result argument. This is a simpler case of
1773 the <a href="#i_invoke">invoke</a> instruction.</p>
1775 <pre> %retval = call int %test(int %argc)<br> call int(sbyte*, ...) *%printf(sbyte* %msg, int 12, sbyte 42);<br></pre>
1778 <!-- _______________________________________________________________________ -->
1779 <div class="doc_subsubsection">
1780 <a name="i_vanext">'<tt>vanext</tt>' Instruction</a>
1783 <div class="doc_text">
1788 <resultarglist> = vanext <va_list> <arglist>, <argty>
1793 <p>The '<tt>vanext</tt>' instruction is used to access arguments passed
1794 through the "variable argument" area of a function call. It is used to
1795 implement the <tt>va_arg</tt> macro in C.</p>
1799 <p>This instruction takes a <tt>va_list</tt> value and the type of the
1800 argument. It returns another <tt>va_list</tt>. The actual type of
1801 <tt>va_list</tt> may be defined differently for different targets. Most targets
1802 use a <tt>va_list</tt> type of <tt>sbyte*</tt> or some other pointer type.</p>
1806 <p>The '<tt>vanext</tt>' instruction advances the specified <tt>va_list</tt>
1807 past an argument of the specified type. In conjunction with the <a
1808 href="#i_vaarg"><tt>vaarg</tt></a> instruction, it is used to implement
1809 the <tt>va_arg</tt> macro available in C. For more information, see
1810 the variable argument handling <a href="#int_varargs">Intrinsic
1813 <p>It is legal for this instruction to be called in a function which
1814 does not take a variable number of arguments, for example, the <tt>vfprintf</tt>
1817 <p><tt>vanext</tt> is an LLVM instruction instead of an <a
1818 href="#intrinsics">intrinsic function</a> because it takes a type as an
1819 argument. The type refers to the current argument in the <tt>va_list</tt>, it
1820 tells the compiler how far on the stack it needs to advance to find the next
1825 <p>See the <a href="#int_varargs">variable argument processing</a>
1830 <!-- _______________________________________________________________________ -->
1831 <div class="doc_subsubsection">
1832 <a name="i_vaarg">'<tt>vaarg</tt>' Instruction</a>
1835 <div class="doc_text">
1840 <resultval> = vaarg <va_list> <arglist>, <argty>
1845 <p>The '<tt>vaarg</tt>' instruction is used to access arguments passed through
1846 the "variable argument" area of a function call. It is used to implement the
1847 <tt>va_arg</tt> macro in C.</p>
1851 <p>This instruction takes a <tt>va_list</tt> value and the type of the
1852 argument. It returns a value of the specified argument type. Again, the actual
1853 type of <tt>va_list</tt> is target specific.</p>
1857 <p>The '<tt>vaarg</tt>' instruction loads an argument of the specified type from
1858 the specified <tt>va_list</tt>. In conjunction with the <a
1859 href="#i_vanext"><tt>vanext</tt></a> instruction, it is used to implement the
1860 <tt>va_arg</tt> macro available in C. For more information, see the variable
1861 argument handling <a href="#int_varargs">Intrinsic Functions</a>.</p>
1863 <p>It is legal for this instruction to be called in a function which does not
1864 take a variable number of arguments, for example, the <tt>vfprintf</tt>
1867 <p><tt>vaarg</tt> is an LLVM instruction instead of an <a
1868 href="#intrinsics">intrinsic function</a> because it takes an type as an
1873 <p>See the <a href="#int_varargs">variable argument processing</a> section.</p>
1877 <!-- *********************************************************************** -->
1878 <div class="doc_section"> <a name="intrinsics">Intrinsic Functions</a> </div>
1879 <!-- *********************************************************************** -->
1881 <div class="doc_text">
1883 <p>LLVM supports the notion of an "intrinsic function". These functions have
1884 well known names and semantics, and are required to follow certain
1885 restrictions. Overall, these instructions represent an extension mechanism for
1886 the LLVM language that does not require changing all of the transformations in
1887 LLVM to add to the language (or the bytecode reader/writer, the parser,
1890 <p>Intrinsic function names must all start with an "<tt>llvm.</tt>" prefix, this
1891 prefix is reserved in LLVM for intrinsic names, thus functions may not be named
1892 this. Intrinsic functions must always be external functions: you cannot define
1893 the body of intrinsic functions. Intrinsic functions may only be used in call
1894 or invoke instructions: it is illegal to take the address of an intrinsic
1895 function. Additionally, because intrinsic functions are part of the LLVM
1896 language, it is required that they all be documented here if any are added.</p>
1900 Adding an intrinsic to LLVM is straight-forward if it is possible to express the
1901 concept in LLVM directly (ie, code generator support is not _required_). To do
1902 this, extend the default implementation of the IntrinsicLowering class to handle
1903 the intrinsic. Code generators use this class to lower intrinsics they do not
1904 understand to raw LLVM instructions that they do.
1909 <!-- ======================================================================= -->
1910 <div class="doc_subsection">
1911 <a name="int_varargs">Variable Argument Handling Intrinsics</a>
1914 <div class="doc_text">
1916 <p>Variable argument support is defined in LLVM with the <a
1917 href="#i_vanext"><tt>vanext</tt></a> instruction and these three
1918 intrinsic functions. These functions are related to the similarly
1919 named macros defined in the <tt><stdarg.h></tt> header file.</p>
1921 <p>All of these functions operate on arguments that use a
1922 target-specific value type "<tt>va_list</tt>". The LLVM assembly
1923 language reference manual does not define what this type is, so all
1924 transformations should be prepared to handle intrinsics with any type
1927 <p>This example shows how the <a href="#i_vanext"><tt>vanext</tt></a>
1928 instruction and the variable argument handling intrinsic functions are
1932 int %test(int %X, ...) {
1933 ; Initialize variable argument processing
1934 %ap = call sbyte* %<a href="#i_va_start">llvm.va_start</a>()
1936 ; Read a single integer argument
1937 %tmp = vaarg sbyte* %ap, int
1939 ; Advance to the next argument
1940 %ap2 = vanext sbyte* %ap, int
1942 ; Demonstrate usage of llvm.va_copy and llvm.va_end
1943 %aq = call sbyte* %<a href="#i_va_copy">llvm.va_copy</a>(sbyte* %ap2)
1944 call void %<a href="#i_va_end">llvm.va_end</a>(sbyte* %aq)
1946 ; Stop processing of arguments.
1947 call void %<a href="#i_va_end">llvm.va_end</a>(sbyte* %ap2)
1953 <!-- _______________________________________________________________________ -->
1954 <div class="doc_subsubsection">
1955 <a name="i_va_start">'<tt>llvm.va_start</tt>' Intrinsic</a>
1959 <div class="doc_text">
1961 <pre> call <va_list> ()* %llvm.va_start()<br></pre>
1963 <p>The '<tt>llvm.va_start</tt>' intrinsic returns a new <tt><arglist></tt>
1964 for subsequent use by the variable argument intrinsics.</p>
1966 <p>The '<tt>llvm.va_start</tt>' intrinsic works just like the <tt>va_start</tt>
1967 macro available in C. In a target-dependent way, it initializes and
1968 returns a <tt>va_list</tt> element, so that the next <tt>vaarg</tt>
1969 will produce the first variable argument passed to the function. Unlike
1970 the C <tt>va_start</tt> macro, this intrinsic does not need to know the
1971 last argument of the function, the compiler can figure that out.</p>
1972 <p>Note that this intrinsic function is only legal to be called from
1973 within the body of a variable argument function.</p>
1976 <!-- _______________________________________________________________________ -->
1977 <div class="doc_subsubsection">
1978 <a name="i_va_end">'<tt>llvm.va_end</tt>' Intrinsic</a>
1981 <div class="doc_text">
1983 <pre> call void (<va_list>)* %llvm.va_end(<va_list> <arglist>)<br></pre>
1985 <p>The '<tt>llvm.va_end</tt>' intrinsic destroys <tt><arglist></tt>
1986 which has been initialized previously with <tt><a href="#i_va_start">llvm.va_start</a></tt>
1987 or <tt><a href="#i_va_copy">llvm.va_copy</a></tt>.</p>
1989 <p>The argument is a <tt>va_list</tt> to destroy.</p>
1991 <p>The '<tt>llvm.va_end</tt>' intrinsic works just like the <tt>va_end</tt>
1992 macro available in C. In a target-dependent way, it destroys the <tt>va_list</tt>.
1993 Calls to <a href="#i_va_start"><tt>llvm.va_start</tt></a> and <a
1994 href="#i_va_copy"><tt>llvm.va_copy</tt></a> must be matched exactly
1995 with calls to <tt>llvm.va_end</tt>.</p>
1998 <!-- _______________________________________________________________________ -->
1999 <div class="doc_subsubsection">
2000 <a name="i_va_copy">'<tt>llvm.va_copy</tt>' Intrinsic</a>
2003 <div class="doc_text">
2008 call <va_list> (<va_list>)* %llvm.va_copy(<va_list> <destarglist>)
2013 <p>The '<tt>llvm.va_copy</tt>' intrinsic copies the current argument position
2014 from the source argument list to the destination argument list.</p>
2018 <p>The argument is the <tt>va_list</tt> to copy.</p>
2022 <p>The '<tt>llvm.va_copy</tt>' intrinsic works just like the <tt>va_copy</tt>
2023 macro available in C. In a target-dependent way, it copies the source
2024 <tt>va_list</tt> element into the returned list. This intrinsic is necessary
2025 because the <tt><a href="#i_va_start">llvm.va_start</a></tt> intrinsic may be
2026 arbitrarily complex and require memory allocation, for example.</p>
2030 <!-- ======================================================================= -->
2031 <div class="doc_subsection">
2032 <a name="int_gc">Accurate Garbage Collection Intrinsics</a>
2035 <div class="doc_text">
2038 LLVM support for <a href="GarbageCollection.html">Accurate Garbage
2039 Collection</a> requires the implementation and generation of these intrinsics.
2040 These intrinsics allow identification of <a href="#i_gcroot">GC roots on the
2041 stack</a>, as well as garbage collector implementations that require <a
2042 href="#i_gcread">read</a> and <a href="#i_gcwrite">write</a> barriers.
2043 Front-ends for type-safe garbage collected languages should generate these
2044 intrinsics to make use of the LLVM garbage collectors. For more details, see <a
2045 href="GarbageCollection.html">Accurate Garbage Collection with LLVM</a>.
2049 <!-- _______________________________________________________________________ -->
2050 <div class="doc_subsubsection">
2051 <a name="i_gcroot">'<tt>llvm.gcroot</tt>' Intrinsic</a>
2054 <div class="doc_text">
2059 call void (<ty>**, <ty2>*)* %llvm.gcroot(<ty>** %ptrloc, <ty2>* %metadata)
2064 <p>The '<tt>llvm.gcroot</tt>' intrinsic declares the existance of a GC root to
2065 the code generator, and allows some metadata to be associated with it.</p>
2069 <p>The first argument specifies the address of a stack object that contains the
2070 root pointer. The second pointer (which must be either a constant or a global
2071 value address) contains the meta-data to be associated with the root.</p>
2075 <p>At runtime, a call to this intrinsics stores a null pointer into the "ptrloc"
2076 location. At compile-time, the code generator generates information to allow
2077 the runtime to find the pointer at GC safe points.
2083 <!-- _______________________________________________________________________ -->
2084 <div class="doc_subsubsection">
2085 <a name="i_gcread">'<tt>llvm.gcread</tt>' Intrinsic</a>
2088 <div class="doc_text">
2093 call sbyte* (sbyte**)* %llvm.gcread(sbyte** %Ptr)
2098 <p>The '<tt>llvm.gcread</tt>' intrinsic identifies reads of references from heap
2099 locations, allowing garbage collector implementations that require read
2104 <p>The argument is the address to read from, which should be an address
2105 allocated from the garbage collector.</p>
2109 <p>The '<tt>llvm.gcread</tt>' intrinsic has the same semantics as a load
2110 instruction, but may be replaced with substantially more complex code by the
2111 garbage collector runtime, as needed.</p>
2116 <!-- _______________________________________________________________________ -->
2117 <div class="doc_subsubsection">
2118 <a name="i_gcwrite">'<tt>llvm.gcwrite</tt>' Intrinsic</a>
2121 <div class="doc_text">
2126 call void (sbyte*, sbyte**)* %llvm.gcwrite(sbyte* %P1, sbyte** %P2)
2131 <p>The '<tt>llvm.gcwrite</tt>' intrinsic identifies writes of references to heap
2132 locations, allowing garbage collector implementations that require write
2133 barriers (such as generational or reference counting collectors).</p>
2137 <p>The first argument is the reference to store, and the second is the heap
2138 location to store to.</p>
2142 <p>The '<tt>llvm.gcwrite</tt>' intrinsic has the same semantics as a store
2143 instruction, but may be replaced with substantially more complex code by the
2144 garbage collector runtime, as needed.</p>
2150 <!-- ======================================================================= -->
2151 <div class="doc_subsection">
2152 <a name="int_codegen">Code Generator Intrinsics</a>
2155 <div class="doc_text">
2157 These intrinsics are provided by LLVM to expose special features that may only
2158 be implemented with code generator support.
2163 <!-- _______________________________________________________________________ -->
2164 <div class="doc_subsubsection">
2165 <a name="i_returnaddress">'<tt>llvm.returnaddress</tt>' Intrinsic</a>
2168 <div class="doc_text">
2172 call void* ()* %llvm.returnaddress(uint <level>)
2178 The '<tt>llvm.returnaddress</tt>' intrinsic returns a target-specific value
2179 indicating the return address of the current function or one of its callers.
2185 The argument to this intrinsic indicates which function to return the address
2186 for. Zero indicates the calling function, one indicates its caller, etc. The
2187 argument is <b>required</b> to be a constant integer value.
2193 The '<tt>llvm.returnaddress</tt>' intrinsic either returns a pointer indicating
2194 the return address of the specified call frame, or zero if it cannot be
2195 identified. The value returned by this intrinsic is likely to be incorrect or 0
2196 for arguments other than zero, so it should only be used for debugging purposes.
2200 Note that calling this intrinsic does not prevent function inlining or other
2201 aggressive transformations, so the value returned may not that of the obvious
2202 source-language caller.
2207 <!-- _______________________________________________________________________ -->
2208 <div class="doc_subsubsection">
2209 <a name="i_frameaddress">'<tt>llvm.frameaddress</tt>' Intrinsic</a>
2212 <div class="doc_text">
2216 call void* ()* %llvm.frameaddress(uint <level>)
2222 The '<tt>llvm.frameaddress</tt>' intrinsic returns the target-specific frame
2223 pointer value for the specified stack frame.
2229 The argument to this intrinsic indicates which function to return the frame
2230 pointer for. Zero indicates the calling function, one indicates its caller,
2231 etc. The argument is <b>required</b> to be a constant integer value.
2237 The '<tt>llvm.frameaddress</tt>' intrinsic either returns a pointer indicating
2238 the frame address of the specified call frame, or zero if it cannot be
2239 identified. The value returned by this intrinsic is likely to be incorrect or 0
2240 for arguments other than zero, so it should only be used for debugging purposes.
2244 Note that calling this intrinsic does not prevent function inlining or other
2245 aggressive transformations, so the value returned may not that of the obvious
2246 source-language caller.
2250 <!-- ======================================================================= -->
2251 <div class="doc_subsection">
2252 <a name="int_os">Operating System Intrinsics</a>
2255 <div class="doc_text">
2257 These intrinsics are provided by LLVM to support the implementation of
2258 operating system level code.
2263 <!-- _______________________________________________________________________ -->
2264 <div class="doc_subsubsection">
2265 <a name="i_readport">'<tt>llvm.readport</tt>' Intrinsic</a>
2268 <div class="doc_text">
2272 call <integer type> (<integer type>)* %llvm.readport (<integer type> <address>)
2278 The '<tt>llvm.readport</tt>' intrinsic reads data from the specified hardware
2285 The argument to this intrinsic indicates the hardware I/O address from which
2286 to read the data. The address is in the hardware I/O address namespace (as
2287 opposed to being a memory location for memory mapped I/O).
2293 The '<tt>llvm.readport</tt>' intrinsic reads data from the hardware I/O port
2294 specified by <i>address</i> and returns the value. The address and return
2295 value must be integers, but the size is dependent upon the platform upon which
2296 the program is code generated. For example, on x86, the address must be an
2297 unsigned 16 bit value, and the return value must be 8, 16, or 32 bits.
2302 <!-- _______________________________________________________________________ -->
2303 <div class="doc_subsubsection">
2304 <a name="i_writeport">'<tt>llvm.writeport</tt>' Intrinsic</a>
2307 <div class="doc_text">
2311 call void (<integer type>, <integer type>)* %llvm.writeport (<integer type> <value>, <integer type> <address>)
2317 The '<tt>llvm.writeport</tt>' intrinsic writes data to the specified hardware
2324 The first argument is the value to write to the I/O port.
2328 The second argument indicates the hardware I/O address to which data should be
2329 written. The address is in the hardware I/O address namespace (as opposed to
2330 being a memory location for memory mapped I/O).
2336 The '<tt>llvm.writeport</tt>' intrinsic writes <i>value</i> to the I/O port
2337 specified by <i>address</i>. The address and value must be integers, but the
2338 size is dependent upon the platform upon which the program is code generated.
2339 For example, on x86, the address must be an unsigned 16 bit value, and the
2340 value written must be 8, 16, or 32 bits in length.
2345 <!-- _______________________________________________________________________ -->
2346 <div class="doc_subsubsection">
2347 <a name="i_readio">'<tt>llvm.readio</tt>' Intrinsic</a>
2350 <div class="doc_text">
2354 call <result> (<ty>*)* %llvm.readio (<ty> * <pointer>)
2360 The '<tt>llvm.readio</tt>' intrinsic reads data from a memory mapped I/O
2367 The argument to this intrinsic is a pointer indicating the memory address from
2368 which to read the data. The data must be a
2369 <a href="#t_firstclass">first class</a> type.
2375 The '<tt>llvm.readio</tt>' intrinsic reads data from a memory mapped I/O
2376 location specified by <i>pointer</i> and returns the value. The argument must
2377 be a pointer, and the return value must be a
2378 <a href="#t_firstclass">first class</a> type. However, certain architectures
2379 may not support I/O on all first class types. For example, 32 bit processors
2380 may only support I/O on data types that are 32 bits or less.
2384 This intrinsic enforces an in-order memory model for llvm.readio and
2385 llvm.writeio calls on machines that use dynamic scheduling. Dynamically
2386 scheduled processors may execute loads and stores out of order, re-ordering at
2387 run time accesses to memory mapped I/O registers. Using these intrinsics
2388 ensures that accesses to memory mapped I/O registers occur in program order.
2393 <!-- _______________________________________________________________________ -->
2394 <div class="doc_subsubsection">
2395 <a name="i_writeio">'<tt>llvm.writeio</tt>' Intrinsic</a>
2398 <div class="doc_text">
2402 call void (<ty1>, <ty2>*)* %llvm.writeio (<ty1> <value>, <ty2> * <pointer>)
2408 The '<tt>llvm.writeio</tt>' intrinsic writes data to the specified memory
2415 The first argument is the value to write to the memory mapped I/O location.
2416 The second argument is a pointer indicating the memory address to which the
2417 data should be written.
2423 The '<tt>llvm.writeio</tt>' intrinsic writes <i>value</i> to the memory mapped
2424 I/O address specified by <i>pointer</i>. The value must be a
2425 <a href="#t_firstclass">first class</a> type. However, certain architectures
2426 may not support I/O on all first class types. For example, 32 bit processors
2427 may only support I/O on data types that are 32 bits or less.
2431 This intrinsic enforces an in-order memory model for llvm.readio and
2432 llvm.writeio calls on machines that use dynamic scheduling. Dynamically
2433 scheduled processors may execute loads and stores out of order, re-ordering at
2434 run time accesses to memory mapped I/O registers. Using these intrinsics
2435 ensures that accesses to memory mapped I/O registers occur in program order.
2440 <!-- ======================================================================= -->
2441 <div class="doc_subsection">
2442 <a name="int_libc">Standard C Library Intrinsics</a>
2445 <div class="doc_text">
2447 LLVM provides intrinsics for a few important standard C library functions.
2448 These intrinsics allow source-language front-ends to pass information about the
2449 alignment of the pointer arguments to the code generator, providing opportunity
2450 for more efficient code generation.
2455 <!-- _______________________________________________________________________ -->
2456 <div class="doc_subsubsection">
2457 <a name="i_memcpy">'<tt>llvm.memcpy</tt>' Intrinsic</a>
2460 <div class="doc_text">
2464 call void (sbyte*, sbyte*, uint, uint)* %llvm.memcpy(sbyte* <dest>, sbyte* <src>,
2465 uint <len>, uint <align>)
2471 The '<tt>llvm.memcpy</tt>' intrinsic copies a block of memory from the source
2472 location to the destination location.
2476 Note that, unlike the standard libc function, the <tt>llvm.memcpy</tt> intrinsic
2477 does not return a value, and takes an extra alignment argument.
2483 The first argument is a pointer to the destination, the second is a pointer to
2484 the source. The third argument is an (arbitrarily sized) integer argument
2485 specifying the number of bytes to copy, and the fourth argument is the alignment
2486 of the source and destination locations.
2490 If the call to this intrinisic has an alignment value that is not 0 or 1, then
2491 the caller guarantees that the size of the copy is a multiple of the alignment
2492 and that both the source and destination pointers are aligned to that boundary.
2498 The '<tt>llvm.memcpy</tt>' intrinsic copies a block of memory from the source
2499 location to the destination location, which are not allowed to overlap. It
2500 copies "len" bytes of memory over. If the argument is known to be aligned to
2501 some boundary, this can be specified as the fourth argument, otherwise it should
2507 <!-- _______________________________________________________________________ -->
2508 <div class="doc_subsubsection">
2509 <a name="i_memmove">'<tt>llvm.memmove</tt>' Intrinsic</a>
2512 <div class="doc_text">
2516 call void (sbyte*, sbyte*, uint, uint)* %llvm.memmove(sbyte* <dest>, sbyte* <src>,
2517 uint <len>, uint <align>)
2523 The '<tt>llvm.memmove</tt>' intrinsic moves a block of memory from the source
2524 location to the destination location. It is similar to the '<tt>llvm.memcpy</tt>'
2525 intrinsic but allows the two memory locations to overlap.
2529 Note that, unlike the standard libc function, the <tt>llvm.memmove</tt> intrinsic
2530 does not return a value, and takes an extra alignment argument.
2536 The first argument is a pointer to the destination, the second is a pointer to
2537 the source. The third argument is an (arbitrarily sized) integer argument
2538 specifying the number of bytes to copy, and the fourth argument is the alignment
2539 of the source and destination locations.
2543 If the call to this intrinisic has an alignment value that is not 0 or 1, then
2544 the caller guarantees that the size of the copy is a multiple of the alignment
2545 and that both the source and destination pointers are aligned to that boundary.
2551 The '<tt>llvm.memmove</tt>' intrinsic copies a block of memory from the source
2552 location to the destination location, which may overlap. It
2553 copies "len" bytes of memory over. If the argument is known to be aligned to
2554 some boundary, this can be specified as the fourth argument, otherwise it should
2560 <!-- _______________________________________________________________________ -->
2561 <div class="doc_subsubsection">
2562 <a name="i_memset">'<tt>llvm.memset</tt>' Intrinsic</a>
2565 <div class="doc_text">
2569 call void (sbyte*, ubyte, uint, uint)* %llvm.memset(sbyte* <dest>, ubyte <val>,
2570 uint <len>, uint <align>)
2576 The '<tt>llvm.memset</tt>' intrinsic fills a block of memory with a particular
2581 Note that, unlike the standard libc function, the <tt>llvm.memset</tt> intrinsic
2582 does not return a value, and takes an extra alignment argument.
2588 The first argument is a pointer to the destination to fill, the second is the
2589 byte value to fill it with, the third argument is an (arbitrarily sized) integer
2590 argument specifying the number of bytes to fill, and the fourth argument is the
2591 known alignment of destination location.
2595 If the call to this intrinisic has an alignment value that is not 0 or 1, then
2596 the caller guarantees that the size of the copy is a multiple of the alignment
2597 and that the destination pointer is aligned to that boundary.
2603 The '<tt>llvm.memset</tt>' intrinsic fills "len" bytes of memory starting at the
2604 destination location. If the argument is known to be aligned to some boundary,
2605 this can be specified as the fourth argument, otherwise it should be set to 0 or
2611 <!-- _______________________________________________________________________ -->
2612 <div class="doc_subsubsection">
2613 <a name="i_isunordered">'<tt>llvm.isunordered</tt>' Intrinsic</a>
2616 <div class="doc_text">
2620 call bool (<float or double>, <float or double>)* %llvm.isunordered(<float or double> Val1,
2621 <float or double> Val2)
2627 The '<tt>llvm.isunordered</tt>' intrinsic returns true if either or both of the
2628 specified floating point values is a NAN.
2634 The arguments are floating point numbers of the same type.
2640 If either or both of the arguments is a SNAN or QNAN, it returns true, otherwise
2648 <!-- ======================================================================= -->
2649 <div class="doc_subsection">
2650 <a name="int_debugger">Debugger Intrinsics</a>
2653 <div class="doc_text">
2655 The LLVM debugger intrinsics (which all start with <tt>llvm.dbg.</tt> prefix),
2656 are described in the <a
2657 href="SourceLevelDebugging.html#format_common_intrinsics">LLVM Source Level
2658 Debugging</a> document.
2663 <!-- *********************************************************************** -->
2666 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
2667 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
2668 <a href="http://validator.w3.org/check/referer"><img
2669 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
2671 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
2672 <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a><br>
2673 Last modified: $Date$