Be a bit more specific about what the alignment value means and the restrictions...
[oota-llvm.git] / docs / LangRef.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4   <title>LLVM Assembly Language Reference Manual</title>
5   <link rel="stylesheet" href="llvm.css" type="text/css">
6 </head>
7 <body>
8 <div class="doc_title"> LLVM Language Reference Manual </div>
9 <ol>
10   <li><a href="#abstract">Abstract</a></li>
11   <li><a href="#introduction">Introduction</a></li>
12   <li><a href="#identifiers">Identifiers</a></li>
13   <li><a href="#typesystem">Type System</a>
14     <ol>
15       <li><a href="#t_primitive">Primitive Types</a>    
16         <ol>
17           <li><a href="#t_classifications">Type Classifications</a></li>
18         </ol>
19       </li>
20       <li><a href="#t_derived">Derived Types</a>
21         <ol>
22           <li><a href="#t_array">Array Type</a></li>
23           <li><a href="#t_function">Function Type</a></li>
24           <li><a href="#t_pointer">Pointer Type</a></li>
25           <li><a href="#t_struct">Structure Type</a></li>
26 <!-- <li><a href="#t_packed" >Packed Type</a> -->
27         </ol>
28       </li>
29     </ol>
30   </li>
31   <li><a href="#highlevel">High Level Structure</a>
32     <ol>
33       <li><a href="#modulestructure">Module Structure</a></li>
34       <li><a href="#globalvars">Global Variables</a></li>
35       <li><a href="#functionstructure">Function Structure</a></li>
36     </ol>
37   </li>
38   <li><a href="#instref">Instruction Reference</a>
39     <ol>
40       <li><a href="#terminators">Terminator Instructions</a>
41         <ol>
42           <li><a href="#i_ret">'<tt>ret</tt>' Instruction</a></li>
43           <li><a href="#i_br">'<tt>br</tt>' Instruction</a></li>
44           <li><a href="#i_switch">'<tt>switch</tt>' Instruction</a></li>
45           <li><a href="#i_invoke">'<tt>invoke</tt>' Instruction</a></li>
46           <li><a href="#i_unwind">'<tt>unwind</tt>'  Instruction</a></li>
47         </ol>
48       </li>
49       <li><a href="#binaryops">Binary Operations</a>
50         <ol>
51           <li><a href="#i_add">'<tt>add</tt>' Instruction</a></li>
52           <li><a href="#i_sub">'<tt>sub</tt>' Instruction</a></li>
53           <li><a href="#i_mul">'<tt>mul</tt>' Instruction</a></li>
54           <li><a href="#i_div">'<tt>div</tt>' Instruction</a></li>
55           <li><a href="#i_rem">'<tt>rem</tt>' Instruction</a></li>
56           <li><a href="#i_setcc">'<tt>set<i>cc</i></tt>' Instructions</a></li>
57         </ol>
58       </li>
59       <li><a href="#bitwiseops">Bitwise Binary Operations</a>
60         <ol>
61           <li><a href="#i_and">'<tt>and</tt>' Instruction</a></li>
62           <li><a href="#i_or">'<tt>or</tt>'  Instruction</a></li>
63           <li><a href="#i_xor">'<tt>xor</tt>' Instruction</a></li>
64           <li><a href="#i_shl">'<tt>shl</tt>' Instruction</a></li>
65           <li><a href="#i_shr">'<tt>shr</tt>' Instruction</a></li>
66         </ol>
67       </li>
68       <li><a href="#memoryops">Memory Access Operations</a>
69         <ol>
70           <li><a href="#i_malloc">'<tt>malloc</tt>'   Instruction</a></li>
71           <li><a href="#i_free">'<tt>free</tt>'     Instruction</a></li>
72           <li><a href="#i_alloca">'<tt>alloca</tt>'   Instruction</a></li>
73          <li><a href="#i_load">'<tt>load</tt>'     Instruction</a></li>
74          <li><a href="#i_store">'<tt>store</tt>'    Instruction</a></li>
75          <li><a href="#i_getelementptr">'<tt>getelementptr</tt>' Instruction</a></li>
76         </ol>
77       </li>
78       <li><a href="#otherops">Other Operations</a>
79         <ol>
80           <li><a href="#i_phi">'<tt>phi</tt>'   Instruction</a></li>
81           <li><a href="#i_cast">'<tt>cast .. to</tt>' Instruction</a></li>
82           <li><a href="#i_call">'<tt>call</tt>'  Instruction</a></li>
83           <li><a href="#i_vanext">'<tt>vanext</tt>' Instruction</a></li>
84           <li><a href="#i_vaarg">'<tt>vaarg</tt>'  Instruction</a></li>
85         </ol>
86       </li>
87     </ol>
88   </li>
89   <li><a href="#intrinsics">Intrinsic Functions</a>
90     <ol>
91       <li><a href="#int_varargs">Variable Argument Handling Intrinsics</a>
92         <ol>
93           <li><a href="#i_va_start">'<tt>llvm.va_start</tt>' Intrinsic</a></li>
94           <li><a href="#i_va_end">'<tt>llvm.va_end</tt>'   Intrinsic</a></li>
95           <li><a href="#i_va_copy">'<tt>llvm.va_copy</tt>'  Intrinsic</a></li>
96         </ol>
97       </li>
98       <li><a href="#int_libc">Standard C Library Intrinsics</a>
99         <ol>
100           <li><a href="#i_memcpy">'<tt>llvm.memcpy</tt>' Intrinsic</a></li>
101           <li><a href="#i_memmove">'<tt>llvm.memmove</tt>' Intrinsic</a></li>
102         </ol>
103       </li>
104       <li><a href="#int_debugger">Debugger intrinsics</a>
105     </ol>
106   </li>
107 </ol>
108 <div class="doc_text">
109 <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>
110 and <a href="mailto:vadve@cs.uiuc.edu">Vikram Adve</a></b></p>
111 <p> </p>
112 </div>
113 <!-- *********************************************************************** -->
114 <div class="doc_section"> <a name="abstract">Abstract </a></div>
115 <!-- *********************************************************************** -->
116 <div class="doc_text">
117 <p>This document is a reference manual for the LLVM assembly language. 
118 LLVM is an SSA based representation that provides type safety,
119 low-level operations, flexibility, and the capability of representing
120 'all' high-level languages cleanly.  It is the common code
121 representation used throughout all phases of the LLVM compilation
122 strategy.</p>
123 </div>
124 <!-- *********************************************************************** -->
125 <div class="doc_section"> <a name="introduction">Introduction</a> </div>
126 <!-- *********************************************************************** -->
127 <div class="doc_text">
128 <p>The LLVM code representation is designed to be used in three
129 different forms: as an in-memory compiler IR, as an on-disk bytecode
130 representation (suitable for fast loading by a Just-In-Time compiler),
131 and as a human readable assembly language representation.  This allows
132 LLVM to provide a powerful intermediate representation for efficient
133 compiler transformations and analysis, while providing a natural means
134 to debug and visualize the transformations.  The three different forms
135 of LLVM are all equivalent.  This document describes the human readable
136 representation and notation.</p>
137 <p>The LLVM representation aims to be a light-weight and low-level
138 while being expressive, typed, and extensible at the same time.  It
139 aims to be a "universal IR" of sorts, by being at a low enough level
140 that high-level ideas may be cleanly mapped to it (similar to how
141 microprocessors are "universal IR's", allowing many source languages to
142 be mapped to them).  By providing type information, LLVM can be used as
143 the target of optimizations: for example, through pointer analysis, it
144 can be proven that a C automatic variable is never accessed outside of
145 the current function... allowing it to be promoted to a simple SSA
146 value instead of a memory location.</p>
147 </div>
148 <!-- _______________________________________________________________________ -->
149 <div class="doc_subsubsection"> <a name="wellformed">Well-Formedness</a> </div>
150 <div class="doc_text">
151 <p>It is important to note that this document describes 'well formed'
152 LLVM assembly language.  There is a difference between what the parser
153 accepts and what is considered 'well formed'.  For example, the
154 following instruction is syntactically okay, but not well formed:</p>
155 <pre>  %x = <a href="#i_add">add</a> int 1, %x<br></pre>
156 <p>...because the definition of <tt>%x</tt> does not dominate all of
157 its uses. The LLVM infrastructure provides a verification pass that may
158 be used to verify that an LLVM module is well formed.  This pass is
159 automatically run by the parser after parsing input assembly, and by
160 the optimizer before it outputs bytecode.  The violations pointed out
161 by the verifier pass indicate bugs in transformation passes or input to
162 the parser.</p>
163 <!-- Describe the typesetting conventions here. --> </div>
164 <!-- *********************************************************************** -->
165 <div class="doc_section"> <a name="identifiers">Identifiers</a> </div>
166 <!-- *********************************************************************** -->
167 <div class="doc_text">
168 <p>LLVM uses three different forms of identifiers, for different
169 purposes:</p>
170 <ol>
171   <li>Numeric constants are represented as you would expect: 12, -3
172 123.421,   etc.  Floating point constants have an optional hexidecimal
173 notation.</li>
174   <li>Named values are represented as a string of characters with a '%'
175 prefix.   For example, %foo, %DivisionByZero,
176 %a.really.long.identifier.  The actual   regular expression used is '<tt>%[a-zA-Z$._][a-zA-Z$._0-9]*</tt>'.
177 Identifiers which require other characters in their names can be
178 surrounded   with quotes.  In this way, anything except a <tt>"</tt>
179 character can be used   in a name.</li>
180   <li>Unnamed values are represented as an unsigned numeric value with
181 a '%'   prefix.  For example, %12, %2, %44.</li>
182 </ol>
183 <p>LLVM requires the values start with a '%' sign for two reasons:
184 Compilers don't need to worry about name clashes with reserved words,
185 and the set of reserved words may be expanded in the future without
186 penalty.  Additionally, unnamed identifiers allow a compiler to quickly
187 come up with a temporary variable without having to avoid symbol table
188 conflicts.</p>
189 <p>Reserved words in LLVM are very similar to reserved words in other
190 languages. There are keywords for different opcodes ('<tt><a
191  href="#i_add">add</a></tt>', '<tt><a href="#i_cast">cast</a></tt>', '<tt><a
192  href="#i_ret">ret</a></tt>', etc...), for primitive type names ('<tt><a
193  href="#t_void">void</a></tt>', '<tt><a href="#t_uint">uint</a></tt>',
194 etc...), and others.  These reserved words cannot conflict with
195 variable names, because none of them start with a '%' character.</p>
196 <p>Here is an example of LLVM code to multiply the integer variable '<tt>%X</tt>'
197 by 8:</p>
198 <p>The easy way:</p>
199 <pre>  %result = <a href="#i_mul">mul</a> uint %X, 8<br></pre>
200 <p>After strength reduction:</p>
201 <pre>  %result = <a href="#i_shl">shl</a> uint %X, ubyte 3<br></pre>
202 <p>And the hard way:</p>
203 <pre>  <a href="#i_add">add</a> uint %X, %X           <i>; yields {uint}:%0</i>
204   <a
205  href="#i_add">add</a> uint %0, %0           <i>; yields {uint}:%1</i>
206   %result = <a
207  href="#i_add">add</a> uint %1, %1<br></pre>
208 <p>This last way of multiplying <tt>%X</tt> by 8 illustrates several
209 important lexical features of LLVM:</p>
210 <ol>
211   <li>Comments are delimited with a '<tt>;</tt>' and go until the end
212 of   line.</li>
213   <li>Unnamed temporaries are created when the result of a computation
214 is not   assigned to a named value.</li>
215   <li>Unnamed temporaries are numbered sequentially</li>
216 </ol>
217 <p>...and it also show a convention that we follow in this document. 
218 When demonstrating instructions, we will follow an instruction with a
219 comment that defines the type and name of value produced.  Comments are
220 shown in italic text.</p>
221 <p>The one non-intuitive notation for constants is the optional
222 hexidecimal form of floating point constants.  For example, the form '<tt>double
223 0x432ff973cafa8000</tt>' is equivalent to (but harder to read than) '<tt>double
224 4.5e+15</tt>' which is also supported by the parser.  The only time
225 hexadecimal floating point constants are useful (and the only time that
226 they are generated by the disassembler) is when an FP constant has to
227 be emitted that is not representable as a decimal floating point number
228 exactly.  For example, NaN's, infinities, and other special cases are
229 represented in their IEEE hexadecimal format so that assembly and
230 disassembly do not cause any bits to change in the constants.</p>
231 </div>
232 <!-- *********************************************************************** -->
233 <div class="doc_section"> <a name="typesystem">Type System</a> </div>
234 <!-- *********************************************************************** -->
235 <div class="doc_text">
236 <p>The LLVM type system is one of the most important features of the
237 intermediate representation.  Being typed enables a number of
238 optimizations to be performed on the IR directly, without having to do
239 extra analyses on the side before the transformation.  A strong type
240 system makes it easier to read the generated code and enables novel
241 analyses and transformations that are not feasible to perform on normal
242 three address code representations.</p>
243 <!-- The written form for the type system was heavily influenced by the
244 syntactic problems with types in the C language<sup><a
245 href="#rw_stroustrup">1</a></sup>.<p> --> </div>
246 <!-- ======================================================================= -->
247 <div class="doc_subsection"> <a name="t_primitive">Primitive Types</a> </div>
248 <div class="doc_text">
249 <p>The primitive types are the fundemental building blocks of the LLVM
250 system. The current set of primitive types are as follows:</p>
251 <p>
252 <table border="0" align="center">
253   <tbody>
254     <tr>
255       <td>
256       <table border="1" cellspacing="0" cellpadding="4" align="center">
257         <tbody>
258           <tr>
259             <td><tt>void</tt></td>
260             <td>No value</td>
261           </tr>
262           <tr>
263             <td><tt>ubyte</tt></td>
264             <td>Unsigned 8 bit value</td>
265           </tr>
266           <tr>
267             <td><tt>ushort</tt></td>
268             <td>Unsigned 16 bit value</td>
269           </tr>
270           <tr>
271             <td><tt>uint</tt></td>
272             <td>Unsigned 32 bit value</td>
273           </tr>
274           <tr>
275             <td><tt>ulong</tt></td>
276             <td>Unsigned 64 bit value</td>
277           </tr>
278           <tr>
279             <td><tt>float</tt></td>
280             <td>32 bit floating point value</td>
281           </tr>
282           <tr>
283             <td><tt>label</tt></td>
284             <td>Branch destination</td>
285           </tr>
286         </tbody>
287       </table>
288       </td>
289       <td valign="top">
290       <table border="1" cellspacing="0" cellpadding="4" align="center&quot;">
291         <tbody>
292           <tr>
293             <td><tt>bool</tt></td>
294             <td>True or False value</td>
295           </tr>
296           <tr>
297             <td><tt>sbyte</tt></td>
298             <td>Signed 8 bit value</td>
299           </tr>
300           <tr>
301             <td><tt>short</tt></td>
302             <td>Signed 16 bit value</td>
303           </tr>
304           <tr>
305             <td><tt>int</tt></td>
306             <td>Signed 32 bit value</td>
307           </tr>
308           <tr>
309             <td><tt>long</tt></td>
310             <td>Signed 64 bit value</td>
311           </tr>
312           <tr>
313             <td><tt>double</tt></td>
314             <td>64 bit floating point value</td>
315           </tr>
316         </tbody>
317       </table>
318       </td>
319     </tr>
320   </tbody>
321 </table>
322 </p>
323 </div>
324 <!-- _______________________________________________________________________ -->
325 <div class="doc_subsubsection"> <a name="t_classifications">Type
326 Classifications</a> </div>
327 <div class="doc_text">
328 <p>These different primitive types fall into a few useful
329 classifications:</p>
330 <p>
331 <table border="1" cellspacing="0" cellpadding="4" align="center">
332   <tbody>
333     <tr>
334       <td><a name="t_signed">signed</a></td>
335       <td><tt>sbyte, short, int, long, float, double</tt></td>
336     </tr>
337     <tr>
338       <td><a name="t_unsigned">unsigned</a></td>
339       <td><tt>ubyte, ushort, uint, ulong</tt></td>
340     </tr>
341     <tr>
342       <td><a name="t_integer">integer</a></td>
343       <td><tt>ubyte, sbyte, ushort, short, uint, int, ulong, long</tt></td>
344     </tr>
345     <tr>
346       <td><a name="t_integral">integral</a></td>
347       <td><tt>bool, ubyte, sbyte, ushort, short, uint, int, ulong, long</tt></td>
348     </tr>
349     <tr>
350       <td><a name="t_floating">floating point</a></td>
351       <td><tt>float, double</tt></td>
352     </tr>
353     <tr>
354       <td><a name="t_firstclass">first class</a></td>
355       <td><tt>bool, ubyte, sbyte, ushort, short,<br>
356 uint, int, ulong, long, float, double, <a href="#t_pointer">pointer</a></tt></td>
357     </tr>
358   </tbody>
359 </table>
360 </p>
361 <p>The <a href="#t_firstclass">first class</a> types are perhaps the
362 most important.  Values of these types are the only ones which can be
363 produced by instructions, passed as arguments, or used as operands to
364 instructions.  This means that all structures and arrays must be
365 manipulated either by pointer or by component.</p>
366 </div>
367 <!-- ======================================================================= -->
368 <div class="doc_subsection"> <a name="t_derived">Derived Types</a> </div>
369 <div class="doc_text">
370 <p>The real power in LLVM comes from the derived types in the system. 
371 This is what allows a programmer to represent arrays, functions,
372 pointers, and other useful types.  Note that these derived types may be
373 recursive: For example, it is possible to have a two dimensional array.</p>
374 </div>
375 <!-- _______________________________________________________________________ -->
376 <div class="doc_subsubsection"> <a name="t_array">Array Type</a> </div>
377 <div class="doc_text">
378 <h5>Overview:</h5>
379 <p>The array type is a very simple derived type that arranges elements
380 sequentially in memory.  The array type requires a size (number of
381 elements) and an underlying data type.</p>
382 <h5>Syntax:</h5>
383 <pre>  [&lt;# elements&gt; x &lt;elementtype&gt;]<br></pre>
384 <p>The number of elements is a constant integer value, elementtype may
385 be any type with a size.</p>
386 <h5>Examples:</h5>
387 <p> <tt>[40 x int ]</tt>: Array of 40 integer values.<br>
388 <tt>[41 x int ]</tt>: Array of 41 integer values.<br>
389 <tt>[40 x uint]</tt>: Array of 40 unsigned integer values.</p>
390 <p> </p>
391 <p>Here are some examples of multidimensional arrays:</p>
392 <p>
393 <table border="0" cellpadding="0" cellspacing="0">
394   <tbody>
395     <tr>
396       <td><tt>[3 x [4 x int]]</tt></td>
397       <td>: 3x4 array integer values.</td>
398     </tr>
399     <tr>
400       <td><tt>[12 x [10 x float]]</tt></td>
401       <td>: 12x10 array of single precision floating point values.</td>
402     </tr>
403     <tr>
404       <td><tt>[2 x [3 x [4 x uint]]]</tt></td>
405       <td>: 2x3x4 array of unsigned integer values.</td>
406     </tr>
407   </tbody>
408 </table>
409 </p>
410 </div>
411 <!-- _______________________________________________________________________ -->
412 <div class="doc_subsubsection"> <a name="t_function">Function Type</a> </div>
413 <div class="doc_text">
414 <h5>Overview:</h5>
415 <p>The function type can be thought of as a function signature.  It
416 consists of a return type and a list of formal parameter types. 
417 Function types are usually used to build virtual function tables
418 (which are structures of pointers to functions), for indirect function
419 calls, and when defining a function.</p>
420 <p>
421 The return type of a function type cannot be an aggregate type.
422 </p>
423 <h5>Syntax:</h5>
424 <pre>  &lt;returntype&gt; (&lt;parameter list&gt;)<br></pre>
425 <p>Where '<tt>&lt;parameter list&gt;</tt>' is a comma-separated list of
426 type specifiers.  Optionally, the parameter list may include a type <tt>...</tt>,
427 which indicates that the function takes a variable number of arguments.
428 Variable argument functions can access their arguments with the <a
429  href="#int_varargs">variable argument handling intrinsic</a> functions.</p>
430 <h5>Examples:</h5>
431 <p>
432 <table border="0" cellpadding="0" cellspacing="0">
433   <tbody>
434     <tr>
435       <td><tt>int (int)</tt></td>
436       <td>: function taking an <tt>int</tt>, returning an <tt>int</tt></td>
437     </tr>
438     <tr>
439       <td><tt>float (int, int *) *</tt></td>
440       <td>: <a href="#t_pointer">Pointer</a> to a function that takes
441 an <tt>int</tt> and a <a href="#t_pointer">pointer</a> to <tt>int</tt>,
442 returning <tt>float</tt>.</td>
443     </tr>
444     <tr>
445       <td><tt>int (sbyte *, ...)</tt></td>
446       <td>: A vararg function that takes at least one <a
447  href="#t_pointer">pointer</a> to <tt>sbyte</tt> (signed char in C),
448 which       returns an integer.  This is the signature for <tt>printf</tt>
449 in LLVM.</td>
450     </tr>
451   </tbody>
452 </table>
453 </p>
454 </div>
455 <!-- _______________________________________________________________________ -->
456 <div class="doc_subsubsection"> <a name="t_struct">Structure Type</a> </div>
457 <div class="doc_text">
458 <h5>Overview:</h5>
459 <p>The structure type is used to represent a collection of data members
460 together in memory.  The packing of the field types is defined to match
461 the ABI of the underlying processor.  The elements of a structure may
462 be any type that has a size.</p>
463 <p>Structures are accessed using '<tt><a href="#i_load">load</a></tt>
464 and '<tt><a href="#i_store">store</a></tt>' by getting a pointer to a
465 field with the '<tt><a href="#i_getelementptr">getelementptr</a></tt>'
466 instruction.</p>
467 <h5>Syntax:</h5>
468 <pre>  { &lt;type list&gt; }<br></pre>
469 <h5>Examples:</h5>
470 <p>
471 <table border="0" cellpadding="0" cellspacing="0">
472   <tbody>
473     <tr>
474       <td><tt>{ int, int, int }</tt></td>
475       <td>: a triple of three <tt>int</tt> values</td>
476     </tr>
477     <tr>
478       <td><tt>{ float, int (int) * }</tt></td>
479       <td>: A pair, where the first element is a <tt>float</tt> and the
480 second       element is a <a href="#t_pointer">pointer</a> to a <a
481  href="t_function">function</a> that takes an <tt>int</tt>, returning
482 an <tt>int</tt>.</td>
483     </tr>
484   </tbody>
485 </table>
486 </p>
487 </div>
488 <!-- _______________________________________________________________________ -->
489 <div class="doc_subsubsection"> <a name="t_pointer">Pointer Type</a> </div>
490 <div class="doc_text">
491 <h5>Overview:</h5>
492 <p>As in many languages, the pointer type represents a pointer or
493 reference to another object, which must live in memory.</p>
494 <h5>Syntax:</h5>
495 <pre>  &lt;type&gt; *<br></pre>
496 <h5>Examples:</h5>
497 <p>
498 <table border="0" cellpadding="0" cellspacing="0">
499   <tbody>
500     <tr>
501       <td><tt>[4x int]*</tt></td>
502       <td>: <a href="#t_pointer">pointer</a> to <a href="#t_array">array</a>
503 of four <tt>int</tt> values</td>
504     </tr>
505     <tr>
506       <td><tt>int (int *) *</tt></td>
507       <td>: A <a href="#t_pointer">pointer</a> to a <a
508  href="t_function">function</a> that takes an <tt>int</tt>, returning
509 an <tt>int</tt>.</td>
510     </tr>
511   </tbody>
512 </table>
513 </p>
514 </div>
515 <!-- _______________________________________________________________________ --><!--
516 <div class="doc_subsubsection">
517   <a name="t_packed">Packed Type</a>
518 </div>
519
520 <div class="doc_text">
521
522 Mention/decide that packed types work with saturation or not. Maybe have a packed+saturated type in addition to just a packed type.<p>
523
524 Packed types should be 'nonsaturated' because standard data types are not saturated.  Maybe have a saturated packed type?<p>
525
526 </div>
527
528 --><!-- *********************************************************************** -->
529 <div class="doc_section"> <a name="highlevel">High Level Structure</a> </div>
530 <!-- *********************************************************************** --><!-- ======================================================================= -->
531 <div class="doc_subsection"> <a name="modulestructure">Module Structure</a> </div>
532 <div class="doc_text">
533 <p>LLVM programs are composed of "Module"s, each of which is a
534 translation unit of the input programs.  Each module consists of
535 functions, global variables, and symbol table entries.  Modules may be
536 combined together with the LLVM linker, which merges function (and
537 global variable) definitions, resolves forward declarations, and merges
538 symbol table entries. Here is an example of the "hello world" module:</p>
539 <pre><i>; Declare the string constant as a global constant...</i>
540 <a href="#identifiers">%.LC0</a> = <a href="#linkage_internal">internal</a> <a
541  href="#globalvars">constant</a> <a href="#t_array">[13 x sbyte]</a> c"hello world\0A\00"          <i>; [13 x sbyte]*</i>
542
543 <i>; External declaration of the puts function</i>
544 <a href="#functionstructure">declare</a> int %puts(sbyte*)                                            <i>; int(sbyte*)* </i>
545
546 <i>; Definition of main function</i>
547 int %main() {                                                        <i>; int()* </i>
548         <i>; Convert [13x sbyte]* to sbyte *...</i>
549         %cast210 = <a
550  href="#i_getelementptr">getelementptr</a> [13 x sbyte]* %.LC0, long 0, long 0 <i>; sbyte*</i>
551
552         <i>; Call puts function to write out the string to stdout...</i>
553         <a
554  href="#i_call">call</a> int %puts(sbyte* %cast210)                              <i>; int</i>
555         <a
556  href="#i_ret">ret</a> int 0<br>}<br></pre>
557 <p>This example is made up of a <a href="#globalvars">global variable</a>
558 named "<tt>.LC0</tt>", an external declaration of the "<tt>puts</tt>"
559 function, and a <a href="#functionstructure">function definition</a>
560 for "<tt>main</tt>".</p>
561 <a name="linkage"> In general, a module is made up of a list of global
562 values, where both functions and global variables are global values. 
563 Global values are represented by a pointer to a memory location (in
564 this case, a pointer to an array of char, and a pointer to a function),
565 and have one of the following linkage types:</a>
566 <p> </p>
567 <dl>
568   <a name="linkage_internal"> <dt><tt><b>internal</b></tt> </dt>
569   <dd>Global values with internal linkage are only directly accessible
570 by objects in the current module.  In particular, linking code into a
571 module with an internal global value may cause the internal to be
572 renamed as necessary to avoid collisions.  Because the symbol is
573 internal to the module, all references can be updated.  This
574 corresponds to the notion of the '<tt>static</tt>' keyword in C, or the
575 idea of "anonymous namespaces" in C++.
576     <p> </p>
577   </dd>
578   </a><a name="linkage_linkonce"> <dt><tt><b>linkonce</b></tt>: </dt>
579   <dd>"<tt>linkonce</tt>" linkage is similar to <tt>internal</tt>
580 linkage, with the twist that linking together two modules defining the
581 same <tt>linkonce</tt> globals will cause one of the globals to be
582 discarded.  This is typically used to implement inline functions. 
583 Unreferenced <tt>linkonce</tt> globals are allowed to be discarded.
584     <p> </p>
585   </dd>
586   </a><a name="linkage_weak"> <dt><tt><b>weak</b></tt>: </dt>
587   <dd>"<tt>weak</tt>" linkage is exactly the same as <tt>linkonce</tt>
588 linkage, except that unreferenced <tt>weak</tt> globals may not be
589 discarded.  This is used to implement constructs in C such as "<tt>int
590 X;</tt>" at global scope.
591     <p> </p>
592   </dd>
593   </a><a name="linkage_appending"> <dt><tt><b>appending</b></tt>: </dt>
594   <dd>"<tt>appending</tt>" linkage may only be applied to global
595 variables of pointer to array type.  When two global variables with
596 appending linkage are linked together, the two global arrays are
597 appended together.  This is the LLVM, typesafe, equivalent of having
598 the system linker append together "sections" with identical names when
599 .o files are linked.
600     <p> </p>
601   </dd>
602   </a><a name="linkage_external"> <dt><tt><b>externally visible</b></tt>:</dt>
603   <dd>If none of the above identifiers are used, the global is
604 externally visible, meaning that it participates in linkage and can be
605 used to resolve external symbol references.
606     <p> </p>
607   </dd>
608   </a>
609 </dl>
610 <p> </p>
611 <p><a name="linkage_external">For example, since the "<tt>.LC0</tt>"
612 variable is defined to be internal, if another module defined a "<tt>.LC0</tt>"
613 variable and was linked with this one, one of the two would be renamed,
614 preventing a collision.  Since "<tt>main</tt>" and "<tt>puts</tt>" are
615 external (i.e., lacking any linkage declarations), they are accessible
616 outside of the current module.  It is illegal for a function <i>declaration</i>
617 to have any linkage type other than "externally visible".</a></p>
618 </div>
619 <!-- ======================================================================= -->
620 <div class="doc_subsection"> <a name="globalvars">Global Variables</a> </div>
621 <div class="doc_text">
622 <p>Global variables define regions of memory allocated at compilation
623 time instead of run-time.  Global variables may optionally be
624 initialized.  A variable may be defined as a global "constant", which
625 indicates that the contents of the variable will never be modified
626 (opening options for optimization).  Constants must always have an
627 initial value.</p>
628 <p>As SSA values, global variables define pointer values that are in
629 scope (i.e. they dominate) for all basic blocks in the program.  Global
630 variables always define a pointer to their "content" type because they
631 describe a region of memory, and all memory objects in LLVM are
632 accessed through pointers.</p>
633 </div>
634 <!-- ======================================================================= -->
635 <div class="doc_subsection"> <a name="functionstructure">Functions</a> </div>
636 <div class="doc_text">
637 <p>LLVM function definitions are composed of a (possibly empty)
638 argument list, an opening curly brace, a list of basic blocks, and a
639 closing curly brace.  LLVM function declarations are defined with the "<tt>declare</tt>"
640 keyword, a function name, and a function signature.</p>
641 <p>A function definition contains a list of basic blocks, forming the
642 CFG for the function.  Each basic block may optionally start with a
643 label (giving the basic block a symbol table entry), contains a list of
644 instructions, and ends with a <a href="#terminators">terminator</a>
645 instruction (such as a branch or function return).</p>
646 <p>The first basic block in program is special in two ways: it is
647 immediately executed on entrance to the function, and it is not allowed
648 to have predecessor basic blocks (i.e. there can not be any branches to
649 the entry block of a function).  Because the block can have no
650 predecessors, it also cannot have any <a href="#i_phi">PHI nodes</a>.</p>
651 <p>
652 LLVM functions are identified by their name and type signature.  Hence, two
653 functions with the same name but different parameter lists or return values
654 are considered different functions, and LLVM will resolves references to each
655 appropriately.
656 </p>
657 </div>
658 <!-- *********************************************************************** -->
659 <div class="doc_section"> <a name="instref">Instruction Reference</a> </div>
660 <!-- *********************************************************************** -->
661 <div class="doc_text">
662 <p>The LLVM instruction set consists of several different
663 classifications of instructions: <a href="#terminators">terminator
664 instructions</a>, <a href="#binaryops">binary instructions</a>, <a
665  href="#memoryops">memory instructions</a>, and <a href="#otherops">other
666 instructions</a>.</p>
667 </div>
668 <!-- ======================================================================= -->
669 <div class="doc_subsection"> <a name="terminators">Terminator
670 Instructions</a> </div>
671 <div class="doc_text">
672 <p>As mentioned <a href="#functionstructure">previously</a>, every
673 basic block in a program ends with a "Terminator" instruction, which
674 indicates which block should be executed after the current block is
675 finished. These terminator instructions typically yield a '<tt>void</tt>'
676 value: they produce control flow, not values (the one exception being
677 the '<a href="#i_invoke"><tt>invoke</tt></a>' instruction).</p>
678 <p>There are five different terminator instructions: the '<a
679  href="#i_ret"><tt>ret</tt></a>' instruction, the '<a href="#i_br"><tt>br</tt></a>'
680 instruction, the '<a href="#i_switch"><tt>switch</tt></a>' instruction,
681 the '<a href="#i_invoke"><tt>invoke</tt></a>' instruction, and the '<a
682  href="#i_unwind"><tt>unwind</tt></a>' instruction.</p>
683 </div>
684 <!-- _______________________________________________________________________ -->
685 <div class="doc_subsubsection"> <a name="i_ret">'<tt>ret</tt>'
686 Instruction</a> </div>
687 <div class="doc_text">
688 <h5>Syntax:</h5>
689 <pre>  ret &lt;type&gt; &lt;value&gt;       <i>; Return a value from a non-void function</i>
690   ret void                 <i>; Return from void function</i>
691 </pre>
692 <h5>Overview:</h5>
693 <p>The '<tt>ret</tt>' instruction is used to return control flow (and a
694 value) from a function, back to the caller.</p>
695 <p>There are two forms of the '<tt>ret</tt>' instructruction: one that
696 returns a value and then causes control flow, and one that just causes
697 control flow to occur.</p>
698 <h5>Arguments:</h5>
699 <p>The '<tt>ret</tt>' instruction may return any '<a
700  href="#t_firstclass">first class</a>' type.  Notice that a function is
701 not <a href="#wellformed">well formed</a> if there exists a '<tt>ret</tt>'
702 instruction inside of the function that returns a value that does not
703 match the return type of the function.</p>
704 <h5>Semantics:</h5>
705 <p>When the '<tt>ret</tt>' instruction is executed, control flow
706 returns back to the calling function's context.  If the caller is a "<a
707  href="#i_call"><tt>call</tt></a> instruction, execution continues at
708 the instruction after the call.  If the caller was an "<a
709  href="#i_invoke"><tt>invoke</tt></a>" instruction, execution continues
710 at the beginning "normal" of the destination block.  If the instruction
711 returns a value, that value shall set the call or invoke instruction's
712 return value.</p>
713 <h5>Example:</h5>
714 <pre>  ret int 5                       <i>; Return an integer value of 5</i>
715   ret void                        <i>; Return from a void function</i>
716 </pre>
717 </div>
718 <!-- _______________________________________________________________________ -->
719 <div class="doc_subsubsection"> <a name="i_br">'<tt>br</tt>' Instruction</a> </div>
720 <div class="doc_text">
721 <h5>Syntax:</h5>
722 <pre>  br bool &lt;cond&gt;, label &lt;iftrue&gt;, label &lt;iffalse&gt;<br>  br label &lt;dest&gt;          <i>; Unconditional branch</i>
723 </pre>
724 <h5>Overview:</h5>
725 <p>The '<tt>br</tt>' instruction is used to cause control flow to
726 transfer to a different basic block in the current function.  There are
727 two forms of this instruction, corresponding to a conditional branch
728 and an unconditional branch.</p>
729 <h5>Arguments:</h5>
730 <p>The conditional branch form of the '<tt>br</tt>' instruction takes a
731 single '<tt>bool</tt>' value and two '<tt>label</tt>' values.  The
732 unconditional form of the '<tt>br</tt>' instruction takes a single '<tt>label</tt>'
733 value as a target.</p>
734 <h5>Semantics:</h5>
735 <p>Upon execution of a conditional '<tt>br</tt>' instruction, the '<tt>bool</tt>'
736 argument is evaluated.  If the value is <tt>true</tt>, control flows
737 to the '<tt>iftrue</tt>' <tt>label</tt> argument.  If "cond" is <tt>false</tt>,
738 control flows to the '<tt>iffalse</tt>' <tt>label</tt> argument.</p>
739 <h5>Example:</h5>
740 <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
741  href="#i_ret">ret</a> int 1<br>IfUnequal:<br>  <a href="#i_ret">ret</a> int 0<br></pre>
742 </div>
743 <!-- _______________________________________________________________________ -->
744 <div class="doc_subsubsection"> <a name="i_switch">'<tt>switch</tt>'
745 Instruction</a> </div>
746 <div class="doc_text">
747 <h5>Syntax:</h5>
748 <pre>  switch uint &lt;value&gt;, label &lt;defaultdest&gt; [ int &lt;val&gt;, label &amp;dest&gt;, ... ]<br></pre>
749 <h5>Overview:</h5>
750 <p>The '<tt>switch</tt>' instruction is used to transfer control flow
751 to one of several different places.  It is a generalization of the '<tt>br</tt>'
752 instruction, allowing a branch to occur to one of many possible
753 destinations.</p>
754 <h5>Arguments:</h5>
755 <p>The '<tt>switch</tt>' instruction uses three parameters: a '<tt>uint</tt>'
756 comparison value '<tt>value</tt>', a default '<tt>label</tt>'
757 destination, and an array of pairs of comparison value constants and '<tt>label</tt>'s.</p>
758 <h5>Semantics:</h5>
759 <p>The <tt>switch</tt> instruction specifies a table of values and
760 destinations. When the '<tt>switch</tt>' instruction is executed, this
761 table is searched for the given value.  If the value is found, the
762 corresponding destination is branched to, otherwise the default value
763 it transfered to.</p>
764 <h5>Implementation:</h5>
765 <p>Depending on properties of the target machine and the particular <tt>switch</tt>
766 instruction, this instruction may be code generated as a series of
767 chained conditional branches, or with a lookup table.</p>
768 <h5>Example:</h5>
769 <pre>  <i>; Emulate a conditional br instruction</i>
770   %Val = <a
771  href="#i_cast">cast</a> bool %value to uint<br>  switch uint %Val, label %truedest [int 0, label %falsedest ]<br><br>  <i>; Emulate an unconditional br instruction</i>
772   switch uint 0, label %dest [ ]
773
774   <i>; Implement a jump table:</i>
775   switch uint %val, label %otherwise [ int 0, label %onzero, 
776                                        int 1, label %onone, 
777                                        int 2, label %ontwo ]
778 </pre>
779 </div>
780 <!-- _______________________________________________________________________ -->
781 <div class="doc_subsubsection"> <a name="i_invoke">'<tt>invoke</tt>'
782 Instruction</a> </div>
783 <div class="doc_text">
784 <h5>Syntax:</h5>
785 <pre>  &lt;result&gt; = invoke &lt;ptr to function ty&gt; %&lt;function ptr val&gt;(&lt;function args&gt;)<br>                 to label &lt;normal label&gt; except label &lt;exception label&gt;<br></pre>
786 <h5>Overview:</h5>
787 <p>The '<tt>invoke</tt>' instruction causes control to transfer to a
788 specified function, with the possibility of control flow transfer to
789 either the '<tt>normal</tt>' <tt>label</tt> label or the '<tt>exception</tt>'<tt>label</tt>.
790 If the callee function returns with the "<tt><a href="#i_ret">ret</a></tt>"
791 instruction, control flow will return to the "normal" label.  If the
792 callee (or any indirect callees) returns with the "<a href="#i_unwind"><tt>unwind</tt></a>"
793 instruction, control is interrupted, and continued at the dynamically
794 nearest "except" label.</p>
795 <h5>Arguments:</h5>
796 <p>This instruction requires several arguments:</p>
797 <ol>
798   <li>'<tt>ptr to function ty</tt>': shall be the signature of the
799 pointer to function value being invoked.  In most cases, this is a
800 direct function invocation, but indirect <tt>invoke</tt>s are just as
801 possible, branching off an arbitrary pointer to function value. </li>
802   <li>'<tt>function ptr val</tt>': An LLVM value containing a pointer
803 to a function to be invoked. </li>
804   <li>'<tt>function args</tt>': argument list whose types match the
805 function signature argument types.  If the function signature indicates
806 the function accepts a variable number of arguments, the extra
807 arguments can be specified. </li>
808   <li>'<tt>normal label</tt>': the label reached when the called
809 function executes a '<tt><a href="#i_ret">ret</a></tt>' instruction. </li>
810   <li>'<tt>exception label</tt>': the label reached when a callee
811 returns with the <a href="#i_unwind"><tt>unwind</tt></a> instruction. </li>
812 </ol>
813 <h5>Semantics:</h5>
814 <p>This instruction is designed to operate as a standard '<tt><a
815  href="#i_call">call</a></tt>' instruction in most regards.  The
816 primary difference is that it establishes an association with a label,
817 which is used by the runtime library to unwind the stack.</p>
818 <p>This instruction is used in languages with destructors to ensure
819 that proper cleanup is performed in the case of either a <tt>longjmp</tt>
820 or a thrown exception.  Additionally, this is important for
821 implementation of '<tt>catch</tt>' clauses in high-level languages that
822 support them.</p>
823 <h5>Example:</h5>
824 <pre>  %retval = invoke int %Test(int 15)<br>              to label %Continue<br>              except label %TestCleanup     <i>; {int}:retval set</i>
825 </pre>
826 </div>
827 <!-- _______________________________________________________________________ -->
828 <div class="doc_subsubsection"> <a name="i_unwind">'<tt>unwind</tt>'
829 Instruction</a> </div>
830 <div class="doc_text">
831 <h5>Syntax:</h5>
832 <pre>  unwind<br></pre>
833 <h5>Overview:</h5>
834 <p>The '<tt>unwind</tt>' instruction unwinds the stack, continuing
835 control flow at the first callee in the dynamic call stack which used
836 an <a href="#i_invoke"><tt>invoke</tt></a> instruction to perform the
837 call.  This is primarily used to implement exception handling.</p>
838 <h5>Semantics:</h5>
839 <p>The '<tt>unwind</tt>' intrinsic causes execution of the current
840 function to immediately halt.  The dynamic call stack is then searched
841 for the first <a href="#i_invoke"><tt>invoke</tt></a> instruction on
842 the call stack.  Once found, execution continues at the "exceptional"
843 destination block specified by the <tt>invoke</tt> instruction.  If
844 there is no <tt>invoke</tt> instruction in the dynamic call chain,
845 undefined behavior results.</p>
846 </div>
847 <!-- ======================================================================= -->
848 <div class="doc_subsection"> <a name="binaryops">Binary Operations</a> </div>
849 <div class="doc_text">
850 <p>Binary operators are used to do most of the computation in a
851 program.  They require two operands, execute an operation on them, and
852 produce a single value. The result value of a binary operator is not
853 necessarily the same type as its operands.</p>
854 <p>There are several different binary operators:</p>
855 </div>
856 <!-- _______________________________________________________________________ -->
857 <div class="doc_subsubsection"> <a name="i_add">'<tt>add</tt>'
858 Instruction</a> </div>
859 <div class="doc_text">
860 <h5>Syntax:</h5>
861 <pre>  &lt;result&gt; = add &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
862 </pre>
863 <h5>Overview:</h5>
864 <p>The '<tt>add</tt>' instruction returns the sum of its two operands.</p>
865 <h5>Arguments:</h5>
866 <p>The two arguments to the '<tt>add</tt>' instruction must be either <a
867  href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
868 values. Both arguments must have identical types.</p>
869 <h5>Semantics:</h5>
870 <p>The value produced is the integer or floating point sum of the two
871 operands.</p>
872 <h5>Example:</h5>
873 <pre>  &lt;result&gt; = add int 4, %var          <i>; yields {int}:result = 4 + %var</i>
874 </pre>
875 </div>
876 <!-- _______________________________________________________________________ -->
877 <div class="doc_subsubsection"> <a name="i_sub">'<tt>sub</tt>'
878 Instruction</a> </div>
879 <div class="doc_text">
880 <h5>Syntax:</h5>
881 <pre>  &lt;result&gt; = sub &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
882 </pre>
883 <h5>Overview:</h5>
884 <p>The '<tt>sub</tt>' instruction returns the difference of its two
885 operands.</p>
886 <p>Note that the '<tt>sub</tt>' instruction is used to represent the '<tt>neg</tt>'
887 instruction present in most other intermediate representations.</p>
888 <h5>Arguments:</h5>
889 <p>The two arguments to the '<tt>sub</tt>' instruction must be either <a
890  href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
891 values. Both arguments must have identical types.</p>
892 <h5>Semantics:</h5>
893 <p>The value produced is the integer or floating point difference of
894 the two operands.</p>
895 <h5>Example:</h5>
896 <pre>  &lt;result&gt; = sub int 4, %var          <i>; yields {int}:result = 4 - %var</i>
897   &lt;result&gt; = sub int 0, %val          <i>; yields {int}:result = -%var</i>
898 </pre>
899 </div>
900 <!-- _______________________________________________________________________ -->
901 <div class="doc_subsubsection"> <a name="i_mul">'<tt>mul</tt>'
902 Instruction</a> </div>
903 <div class="doc_text">
904 <h5>Syntax:</h5>
905 <pre>  &lt;result&gt; = mul &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
906 </pre>
907 <h5>Overview:</h5>
908 <p>The  '<tt>mul</tt>' instruction returns the product of its two
909 operands.</p>
910 <h5>Arguments:</h5>
911 <p>The two arguments to the '<tt>mul</tt>' instruction must be either <a
912  href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
913 values. Both arguments must have identical types.</p>
914 <h5>Semantics:</h5>
915 <p>The value produced is the integer or floating point product of the
916 two operands.</p>
917 <p>There is no signed vs unsigned multiplication.  The appropriate
918 action is taken based on the type of the operand.</p>
919 <h5>Example:</h5>
920 <pre>  &lt;result&gt; = mul int 4, %var          <i>; yields {int}:result = 4 * %var</i>
921 </pre>
922 </div>
923 <!-- _______________________________________________________________________ -->
924 <div class="doc_subsubsection"> <a name="i_div">'<tt>div</tt>'
925 Instruction</a> </div>
926 <div class="doc_text">
927 <h5>Syntax:</h5>
928 <pre>  &lt;result&gt; = div &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
929 </pre>
930 <h5>Overview:</h5>
931 <p>The '<tt>div</tt>' instruction returns the quotient of its two
932 operands.</p>
933 <h5>Arguments:</h5>
934 <p>The two arguments to the '<tt>div</tt>' instruction must be either <a
935  href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
936 values. Both arguments must have identical types.</p>
937 <h5>Semantics:</h5>
938 <p>The value produced is the integer or floating point quotient of the
939 two operands.</p>
940 <h5>Example:</h5>
941 <pre>  &lt;result&gt; = div int 4, %var          <i>; yields {int}:result = 4 / %var</i>
942 </pre>
943 </div>
944 <!-- _______________________________________________________________________ -->
945 <div class="doc_subsubsection"> <a name="i_rem">'<tt>rem</tt>'
946 Instruction</a> </div>
947 <div class="doc_text">
948 <h5>Syntax:</h5>
949 <pre>  &lt;result&gt; = rem &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
950 </pre>
951 <h5>Overview:</h5>
952 <p>The '<tt>rem</tt>' instruction returns the remainder from the
953 division of its two operands.</p>
954 <h5>Arguments:</h5>
955 <p>The two arguments to the '<tt>rem</tt>' instruction must be either <a
956  href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
957 values. Both arguments must have identical types.</p>
958 <h5>Semantics:</h5>
959 <p>This returns the <i>remainder</i> of a division (where the result
960 has the same sign as the divisor), not the <i>modulus</i> (where the
961 result has the same sign as the dividend) of a value.  For more
962 information about the difference, see: <a
963  href="http://mathforum.org/dr.math/problems/anne.4.28.99.html">The
964 Math Forum</a>.</p>
965 <h5>Example:</h5>
966 <pre>  &lt;result&gt; = rem int 4, %var          <i>; yields {int}:result = 4 % %var</i>
967 </pre>
968 </div>
969 <!-- _______________________________________________________________________ -->
970 <div class="doc_subsubsection"> <a name="i_setcc">'<tt>set<i>cc</i></tt>'
971 Instructions</a> </div>
972 <div class="doc_text">
973 <h5>Syntax:</h5>
974 <pre>  &lt;result&gt; = seteq &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
975   &lt;result&gt; = setne &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
976   &lt;result&gt; = setlt &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
977   &lt;result&gt; = setgt &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
978   &lt;result&gt; = setle &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
979   &lt;result&gt; = setge &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
980 </pre>
981 <h5>Overview:</h5>
982 <p>The '<tt>set<i>cc</i></tt>' family of instructions returns a boolean
983 value based on a comparison of their two operands.</p>
984 <h5>Arguments:</h5>
985 <p>The two arguments to the '<tt>set<i>cc</i></tt>' instructions must
986 be of <a href="#t_firstclass">first class</a> type (it is not possible
987 to compare '<tt>label</tt>'s, '<tt>array</tt>'s, '<tt>structure</tt>'
988 or '<tt>void</tt>' values, etc...).  Both arguments must have identical
989 types.</p>
990 <h5>Semantics:</h5>
991 <p>The '<tt>seteq</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
992 value if both operands are equal.<br>
993 The '<tt>setne</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
994 value if both operands are unequal.<br>
995 The '<tt>setlt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
996 value if the first operand is less than the second operand.<br>
997 The '<tt>setgt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
998 value if the first operand is greater than the second operand.<br>
999 The '<tt>setle</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1000 value if the first operand is less than or equal to the second operand.<br>
1001 The '<tt>setge</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1002 value if the first operand is greater than or equal to the second
1003 operand.</p>
1004 <h5>Example:</h5>
1005 <pre>  &lt;result&gt; = seteq int   4, 5        <i>; yields {bool}:result = false</i>
1006   &lt;result&gt; = setne float 4, 5        <i>; yields {bool}:result = true</i>
1007   &lt;result&gt; = setlt uint  4, 5        <i>; yields {bool}:result = true</i>
1008   &lt;result&gt; = setgt sbyte 4, 5        <i>; yields {bool}:result = false</i>
1009   &lt;result&gt; = setle sbyte 4, 5        <i>; yields {bool}:result = true</i>
1010   &lt;result&gt; = setge sbyte 4, 5        <i>; yields {bool}:result = false</i>
1011 </pre>
1012 </div>
1013 <!-- ======================================================================= -->
1014 <div class="doc_subsection"> <a name="bitwiseops">Bitwise Binary
1015 Operations</a> </div>
1016 <div class="doc_text">
1017 <p>Bitwise binary operators are used to do various forms of
1018 bit-twiddling in a program.  They are generally very efficient
1019 instructions, and can commonly be strength reduced from other
1020 instructions.  They require two operands, execute an operation on them,
1021 and produce a single value.  The resulting value of the bitwise binary
1022 operators is always the same type as its first operand.</p>
1023 </div>
1024 <!-- _______________________________________________________________________ -->
1025 <div class="doc_subsubsection"> <a name="i_and">'<tt>and</tt>'
1026 Instruction</a> </div>
1027 <div class="doc_text">
1028 <h5>Syntax:</h5>
1029 <pre>  &lt;result&gt; = and &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
1030 </pre>
1031 <h5>Overview:</h5>
1032 <p>The '<tt>and</tt>' instruction returns the bitwise logical and of
1033 its two operands.</p>
1034 <h5>Arguments:</h5>
1035 <p>The two arguments to the '<tt>and</tt>' instruction must be <a
1036  href="#t_integral">integral</a> values.  Both arguments must have
1037 identical types.</p>
1038 <h5>Semantics:</h5>
1039 <p>The truth table used for the '<tt>and</tt>' instruction is:</p>
1040 <p> </p>
1041 <center>
1042 <table border="1" cellspacing="0" cellpadding="4">
1043   <tbody>
1044     <tr>
1045       <td>In0</td>
1046       <td>In1</td>
1047       <td>Out</td>
1048     </tr>
1049     <tr>
1050       <td>0</td>
1051       <td>0</td>
1052       <td>0</td>
1053     </tr>
1054     <tr>
1055       <td>0</td>
1056       <td>1</td>
1057       <td>0</td>
1058     </tr>
1059     <tr>
1060       <td>1</td>
1061       <td>0</td>
1062       <td>0</td>
1063     </tr>
1064     <tr>
1065       <td>1</td>
1066       <td>1</td>
1067       <td>1</td>
1068     </tr>
1069   </tbody>
1070 </table>
1071 </center>
1072 <h5>Example:</h5>
1073 <pre>  &lt;result&gt; = and int 4, %var         <i>; yields {int}:result = 4 &amp; %var</i>
1074   &lt;result&gt; = and int 15, 40          <i>; yields {int}:result = 8</i>
1075   &lt;result&gt; = and int 4, 8            <i>; yields {int}:result = 0</i>
1076 </pre>
1077 </div>
1078 <!-- _______________________________________________________________________ -->
1079 <div class="doc_subsubsection"> <a name="i_or">'<tt>or</tt>' Instruction</a> </div>
1080 <div class="doc_text">
1081 <h5>Syntax:</h5>
1082 <pre>  &lt;result&gt; = or &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
1083 </pre>
1084 <h5>Overview:</h5>
1085 <p>The '<tt>or</tt>' instruction returns the bitwise logical inclusive
1086 or of its two operands.</p>
1087 <h5>Arguments:</h5>
1088 <p>The two arguments to the '<tt>or</tt>' instruction must be <a
1089  href="#t_integral">integral</a> values.  Both arguments must have
1090 identical types.</p>
1091 <h5>Semantics:</h5>
1092 <p>The truth table used for the '<tt>or</tt>' instruction is:</p>
1093 <p> </p>
1094 <center>
1095 <table border="1" cellspacing="0" cellpadding="4">
1096   <tbody>
1097     <tr>
1098       <td>In0</td>
1099       <td>In1</td>
1100       <td>Out</td>
1101     </tr>
1102     <tr>
1103       <td>0</td>
1104       <td>0</td>
1105       <td>0</td>
1106     </tr>
1107     <tr>
1108       <td>0</td>
1109       <td>1</td>
1110       <td>1</td>
1111     </tr>
1112     <tr>
1113       <td>1</td>
1114       <td>0</td>
1115       <td>1</td>
1116     </tr>
1117     <tr>
1118       <td>1</td>
1119       <td>1</td>
1120       <td>1</td>
1121     </tr>
1122   </tbody>
1123 </table>
1124 </center>
1125 <h5>Example:</h5>
1126 <pre>  &lt;result&gt; = or int 4, %var         <i>; yields {int}:result = 4 | %var</i>
1127   &lt;result&gt; = or int 15, 40          <i>; yields {int}:result = 47</i>
1128   &lt;result&gt; = or int 4, 8            <i>; yields {int}:result = 12</i>
1129 </pre>
1130 </div>
1131 <!-- _______________________________________________________________________ -->
1132 <div class="doc_subsubsection"> <a name="i_xor">'<tt>xor</tt>'
1133 Instruction</a> </div>
1134 <div class="doc_text">
1135 <h5>Syntax:</h5>
1136 <pre>  &lt;result&gt; = xor &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
1137 </pre>
1138 <h5>Overview:</h5>
1139 <p>The '<tt>xor</tt>' instruction returns the bitwise logical exclusive
1140 or of its two operands.  The <tt>xor</tt> is used to implement the
1141 "one's complement" operation, which is the "~" operator in C.</p>
1142 <h5>Arguments:</h5>
1143 <p>The two arguments to the '<tt>xor</tt>' instruction must be <a
1144  href="#t_integral">integral</a> values.  Both arguments must have
1145 identical types.</p>
1146 <h5>Semantics:</h5>
1147 <p>The truth table used for the '<tt>xor</tt>' instruction is:</p>
1148 <p> </p>
1149 <center>
1150 <table border="1" cellspacing="0" cellpadding="4">
1151   <tbody>
1152     <tr>
1153       <td>In0</td>
1154       <td>In1</td>
1155       <td>Out</td>
1156     </tr>
1157     <tr>
1158       <td>0</td>
1159       <td>0</td>
1160       <td>0</td>
1161     </tr>
1162     <tr>
1163       <td>0</td>
1164       <td>1</td>
1165       <td>1</td>
1166     </tr>
1167     <tr>
1168       <td>1</td>
1169       <td>0</td>
1170       <td>1</td>
1171     </tr>
1172     <tr>
1173       <td>1</td>
1174       <td>1</td>
1175       <td>0</td>
1176     </tr>
1177   </tbody>
1178 </table>
1179 </center>
1180 <p> </p>
1181 <h5>Example:</h5>
1182 <pre>  &lt;result&gt; = xor int 4, %var         <i>; yields {int}:result = 4 ^ %var</i>
1183   &lt;result&gt; = xor int 15, 40          <i>; yields {int}:result = 39</i>
1184   &lt;result&gt; = xor int 4, 8            <i>; yields {int}:result = 12</i>
1185   &lt;result&gt; = xor int %V, -1          <i>; yields {int}:result = ~%V</i>
1186 </pre>
1187 </div>
1188 <!-- _______________________________________________________________________ -->
1189 <div class="doc_subsubsection"> <a name="i_shl">'<tt>shl</tt>'
1190 Instruction</a> </div>
1191 <div class="doc_text">
1192 <h5>Syntax:</h5>
1193 <pre>  &lt;result&gt; = shl &lt;ty&gt; &lt;var1&gt;, ubyte &lt;var2&gt;   <i>; yields {ty}:result</i>
1194 </pre>
1195 <h5>Overview:</h5>
1196 <p>The '<tt>shl</tt>' instruction returns the first operand shifted to
1197 the left a specified number of bits.</p>
1198 <h5>Arguments:</h5>
1199 <p>The first argument to the '<tt>shl</tt>' instruction must be an <a
1200  href="#t_integer">integer</a> type.  The second argument must be an '<tt>ubyte</tt>'
1201 type.</p>
1202 <h5>Semantics:</h5>
1203 <p>The value produced is <tt>var1</tt> * 2<sup><tt>var2</tt></sup>.</p>
1204 <h5>Example:</h5>
1205 <pre>  &lt;result&gt; = shl int 4, ubyte %var   <i>; yields {int}:result = 4 &lt;&lt; %var</i>
1206   &lt;result&gt; = shl int 4, ubyte 2      <i>; yields {int}:result = 16</i>
1207   &lt;result&gt; = shl int 1, ubyte 10     <i>; yields {int}:result = 1024</i>
1208 </pre>
1209 </div>
1210 <!-- _______________________________________________________________________ -->
1211 <div class="doc_subsubsection"> <a name="i_shr">'<tt>shr</tt>'
1212 Instruction</a> </div>
1213 <div class="doc_text">
1214 <h5>Syntax:</h5>
1215 <pre>  &lt;result&gt; = shr &lt;ty&gt; &lt;var1&gt;, ubyte &lt;var2&gt;   <i>; yields {ty}:result</i>
1216 </pre>
1217 <h5>Overview:</h5>
1218 <p>The '<tt>shr</tt>' instruction returns the first operand shifted to
1219 the right a specified number of bits.</p>
1220 <h5>Arguments:</h5>
1221 <p>The first argument to the '<tt>shr</tt>' instruction must be an <a
1222  href="#t_integer">integer</a> type.  The second argument must be an '<tt>ubyte</tt>'
1223 type.</p>
1224 <h5>Semantics:</h5>
1225 <p>If the first argument is a <a href="#t_signed">signed</a> type, the
1226 most significant bit is duplicated in the newly free'd bit positions. 
1227 If the first argument is unsigned, zero bits shall fill the empty
1228 positions.</p>
1229 <h5>Example:</h5>
1230 <pre>  &lt;result&gt; = shr int 4, ubyte %var   <i>; yields {int}:result = 4 &gt;&gt; %var</i>
1231   &lt;result&gt; = shr uint 4, ubyte 1     <i>; yields {uint}:result = 2</i>
1232   &lt;result&gt; = shr int 4, ubyte 2      <i>; yields {int}:result = 1</i>
1233   &lt;result&gt; = shr sbyte 4, ubyte 3    <i>; yields {sbyte}:result = 0</i>
1234   &lt;result&gt; = shr sbyte -2, ubyte 1   <i>; yields {sbyte}:result = -1</i>
1235 </pre>
1236 </div>
1237 <!-- ======================================================================= -->
1238 <div class="doc_subsection"> <a name="memoryops">Memory Access
1239 Operations</a></div>
1240 <div class="doc_text">
1241 <p>A key design point of an SSA-based representation is how it
1242 represents memory.  In LLVM, no memory locations are in SSA form, which
1243 makes things very simple.  This section describes how to read, write,
1244 allocate and free memory in LLVM.</p>
1245 </div>
1246 <!-- _______________________________________________________________________ -->
1247 <div class="doc_subsubsection"> <a name="i_malloc">'<tt>malloc</tt>'
1248 Instruction</a> </div>
1249 <div class="doc_text">
1250 <h5>Syntax:</h5>
1251 <pre>  &lt;result&gt; = malloc &lt;type&gt;, uint &lt;NumElements&gt;     <i>; yields {type*}:result</i>
1252   &lt;result&gt; = malloc &lt;type&gt;                         <i>; yields {type*}:result</i>
1253 </pre>
1254 <h5>Overview:</h5>
1255 <p>The '<tt>malloc</tt>' instruction allocates memory from the system
1256 heap and returns a pointer to it.</p>
1257 <h5>Arguments:</h5>
1258 <p>The the '<tt>malloc</tt>' instruction allocates <tt>sizeof(&lt;type&gt;)*NumElements</tt>
1259 bytes of memory from the operating system, and returns a pointer of the
1260 appropriate type to the program.  The second form of the instruction is
1261 a shorter version of the first instruction that defaults to allocating
1262 one element.</p>
1263 <p>'<tt>type</tt>' must be a sized type.</p>
1264 <h5>Semantics:</h5>
1265 <p>Memory is allocated using the system "<tt>malloc</tt>" function, and
1266 a pointer is returned.</p>
1267 <h5>Example:</h5>
1268 <pre>  %array  = malloc [4 x ubyte ]                    <i>; yields {[%4 x ubyte]*}:array</i>
1269
1270   %size   = <a
1271  href="#i_add">add</a> uint 2, 2                          <i>; yields {uint}:size = uint 4</i>
1272   %array1 = malloc ubyte, uint 4                   <i>; yields {ubyte*}:array1</i>
1273   %array2 = malloc [12 x ubyte], uint %size        <i>; yields {[12 x ubyte]*}:array2</i>
1274 </pre>
1275 </div>
1276 <!-- _______________________________________________________________________ -->
1277 <div class="doc_subsubsection"> <a name="i_free">'<tt>free</tt>'
1278 Instruction</a> </div>
1279 <div class="doc_text">
1280 <h5>Syntax:</h5>
1281 <pre>  free &lt;type&gt; &lt;value&gt;                              <i>; yields {void}</i>
1282 </pre>
1283 <h5>Overview:</h5>
1284 <p>The '<tt>free</tt>' instruction returns memory back to the unused
1285 memory heap, to be reallocated in the future.</p>
1286 <p> </p>
1287 <h5>Arguments:</h5>
1288 <p>'<tt>value</tt>' shall be a pointer value that points to a value
1289 that was allocated with the '<tt><a href="#i_malloc">malloc</a></tt>'
1290 instruction.</p>
1291 <h5>Semantics:</h5>
1292 <p>Access to the memory pointed to by the pointer is not longer defined
1293 after this instruction executes.</p>
1294 <h5>Example:</h5>
1295 <pre>  %array  = <a href="#i_malloc">malloc</a> [4 x ubyte]                    <i>; yields {[4 x ubyte]*}:array</i>
1296             free   [4 x ubyte]* %array
1297 </pre>
1298 </div>
1299 <!-- _______________________________________________________________________ -->
1300 <div class="doc_subsubsection"> <a name="i_alloca">'<tt>alloca</tt>'
1301 Instruction</a> </div>
1302 <div class="doc_text">
1303 <h5>Syntax:</h5>
1304 <pre>  &lt;result&gt; = alloca &lt;type&gt;, uint &lt;NumElements&gt;  <i>; yields {type*}:result</i>
1305   &lt;result&gt; = alloca &lt;type&gt;                      <i>; yields {type*}:result</i>
1306 </pre>
1307 <h5>Overview:</h5>
1308 <p>The '<tt>alloca</tt>' instruction allocates memory on the current
1309 stack frame of the procedure that is live until the current function
1310 returns to its caller.</p>
1311 <h5>Arguments:</h5>
1312 <p>The the '<tt>alloca</tt>' instruction allocates <tt>sizeof(&lt;type&gt;)*NumElements</tt>
1313 bytes of memory on the runtime stack, returning a pointer of the
1314 appropriate type to the program.  The second form of the instruction is
1315 a shorter version of the first that defaults to allocating one element.</p>
1316 <p>'<tt>type</tt>' may be any sized type.</p>
1317 <h5>Semantics:</h5>
1318 <p>Memory is allocated, a pointer is returned.  '<tt>alloca</tt>'d
1319 memory is automatically released when the function returns.  The '<tt>alloca</tt>'
1320 instruction is commonly used to represent automatic variables that must
1321 have an address available.  When the function returns (either with the <tt><a
1322  href="#i_ret">ret</a></tt> or <tt><a href="#i_invoke">invoke</a></tt>
1323 instructions), the memory is reclaimed.</p>
1324 <h5>Example:</h5>
1325 <pre>  %ptr = alloca int                              <i>; yields {int*}:ptr</i>
1326   %ptr = alloca int, uint 4                      <i>; yields {int*}:ptr</i>
1327 </pre>
1328 </div>
1329 <!-- _______________________________________________________________________ -->
1330 <div class="doc_subsubsection"> <a name="i_load">'<tt>load</tt>'
1331 Instruction</a> </div>
1332 <div class="doc_text">
1333 <h5>Syntax:</h5>
1334 <pre>  &lt;result&gt; = load &lt;ty&gt;* &lt;pointer&gt;<br>  &lt;result&gt; = volatile load &lt;ty&gt;* &lt;pointer&gt;<br></pre>
1335 <h5>Overview:</h5>
1336 <p>The '<tt>load</tt>' instruction is used to read from memory.</p>
1337 <h5>Arguments:</h5>
1338 <p>The argument to the '<tt>load</tt>' instruction specifies the memory
1339 address to load from.  The pointer must point to a <a
1340  href="t_firstclass">first class</a> type.  If the <tt>load</tt> is
1341 marked as <tt>volatile</tt> then the optimizer is not allowed to modify
1342 the number or order of execution of this <tt>load</tt> with other
1343 volatile <tt>load</tt> and <tt><a href="#i_store">store</a></tt>
1344 instructions. </p>
1345 <h5>Semantics:</h5>
1346 <p>The location of memory pointed to is loaded.</p>
1347 <h5>Examples:</h5>
1348 <pre>  %ptr = <a href="#i_alloca">alloca</a> int                               <i>; yields {int*}:ptr</i>
1349   <a
1350  href="#i_store">store</a> int 3, int* %ptr                          <i>; yields {void}</i>
1351   %val = load int* %ptr                           <i>; yields {int}:val = int 3</i>
1352 </pre>
1353 </div>
1354 <!-- _______________________________________________________________________ -->
1355 <div class="doc_subsubsection"> <a name="i_store">'<tt>store</tt>'
1356 Instruction</a> </div>
1357 <h5>Syntax:</h5>
1358 <pre>  store &lt;ty&gt; &lt;value&gt;, &lt;ty&gt;* &lt;pointer&gt;                   <i>; yields {void}</i>
1359   volatile store &lt;ty&gt; &lt;value&gt;, &lt;ty&gt;* &lt;pointer&gt;                   <i>; yields {void}</i>
1360 </pre>
1361 <h5>Overview:</h5>
1362 <p>The '<tt>store</tt>' instruction is used to write to memory.</p>
1363 <h5>Arguments:</h5>
1364 <p>There are two arguments to the '<tt>store</tt>' instruction: a value
1365 to store and an address to store it into.  The type of the '<tt>&lt;pointer&gt;</tt>'
1366 operand must be a pointer to the type of the '<tt>&lt;value&gt;</tt>'
1367 operand. If the <tt>store</tt> is marked as <tt>volatile</tt> then the
1368 optimizer is not allowed to modify the number or order of execution of
1369 this <tt>store</tt> with other volatile <tt>load</tt> and <tt><a
1370  href="#i_store">store</a></tt> instructions.</p>
1371 <h5>Semantics:</h5>
1372 <p>The contents of memory are updated to contain '<tt>&lt;value&gt;</tt>'
1373 at the location specified by the '<tt>&lt;pointer&gt;</tt>' operand.</p>
1374 <h5>Example:</h5>
1375 <pre>  %ptr = <a href="#i_alloca">alloca</a> int                               <i>; yields {int*}:ptr</i>
1376   <a
1377  href="#i_store">store</a> int 3, int* %ptr                          <i>; yields {void}</i>
1378   %val = load int* %ptr                           <i>; yields {int}:val = int 3</i>
1379 </pre>
1380 <!-- _______________________________________________________________________ -->
1381 <div class="doc_subsubsection"> <a name="i_getelementptr">'<tt>getelementptr</tt>'
1382 Instruction</a> </div>
1383 <div class="doc_text">
1384 <h5>Syntax:</h5>
1385 <pre>  &lt;result&gt; = getelementptr &lt;ty&gt;* &lt;ptrval&gt;{, long &lt;aidx&gt;|, ubyte &lt;sidx&gt;}*<br></pre>
1386 <h5>Overview:</h5>
1387 <p>The '<tt>getelementptr</tt>' instruction is used to get the address
1388 of a subelement of an aggregate data structure.</p>
1389 <h5>Arguments:</h5>
1390 <p>This instruction takes a list of <tt>long</tt> values and <tt>ubyte</tt>
1391 constants that indicate what form of addressing to perform.  The actual
1392 types of the arguments provided depend on the type of the first pointer
1393 argument.  The '<tt>getelementptr</tt>' instruction is used to index
1394 down through the type levels of a structure.</p>
1395 <p>For example, let's consider a C code fragment and how it gets
1396 compiled to LLVM:</p>
1397 <pre>struct RT {<br>  char A;<br>  int B[10][20];<br>  char C;<br>};<br>struct ST {<br>  int X;<br>  double Y;<br>  struct RT Z;<br>};<br><br>int *foo(struct ST *s) {<br>  return &amp;s[1].Z.B[5][13];<br>}<br></pre>
1398 <p>The LLVM code generated by the GCC frontend is:</p>
1399 <pre>%RT = type { sbyte, [10 x [20 x int]], sbyte }<br>%ST = type { int, double, %RT }<br><br>int* "foo"(%ST* %s) {<br>  %reg = getelementptr %ST* %s, long 1, ubyte 2, ubyte 1, long 5, long 13<br>  ret int* %reg<br>}<br></pre>
1400 <h5>Semantics:</h5>
1401 <p>The index types specified for the '<tt>getelementptr</tt>'
1402 instruction depend on the pointer type that is being index into. <a
1403  href="t_pointer">Pointer</a> and <a href="t_array">array</a> types
1404 require '<tt>long</tt>' values, and <a href="t_struct">structure</a>
1405 types require '<tt>ubyte</tt>' <b>constants</b>.</p>
1406 <p>In the example above, the first index is indexing into the '<tt>%ST*</tt>'
1407 type, which is a pointer, yielding a '<tt>%ST</tt>' = '<tt>{ int,
1408 double, %RT }</tt>' type, a structure.  The second index indexes into
1409 the third element of the structure, yielding a '<tt>%RT</tt>' = '<tt>{
1410 sbyte, [10 x [20 x int]], sbyte }</tt>' type, another structure.  The
1411 third index indexes into the second element of the structure, yielding
1412 a '<tt>[10 x [20 x int]]</tt>' type, an array.  The two dimensions of
1413 the array are subscripted into, yielding an '<tt>int</tt>' type.  The '<tt>getelementptr</tt>'
1414 instruction return a pointer to this element, thus yielding a '<tt>int*</tt>'
1415 type.</p>
1416 <p>Note that it is perfectly legal to index partially through a
1417 structure, returning a pointer to an inner element.  Because of this,
1418 the LLVM code for the given testcase is equivalent to:</p>
1419 <pre>int* "foo"(%ST* %s) {<br>  %t1 = getelementptr %ST* %s , long 1                        <i>; yields %ST*:%t1</i>
1420   %t2 = getelementptr %ST* %t1, long 0, ubyte 2               <i>; yields %RT*:%t2</i>
1421   %t3 = getelementptr %RT* %t2, long 0, ubyte 1               <i>; yields [10 x [20 x int]]*:%t3</i>
1422   %t4 = getelementptr [10 x [20 x int]]* %t3, long 0, long 5  <i>; yields [20 x int]*:%t4</i>
1423   %t5 = getelementptr [20 x int]* %t4, long 0, long 13        <i>; yields int*:%t5</i>
1424   ret int* %t5
1425 }
1426 </pre>
1427 <h5>Example:</h5>
1428 <pre>  <i>; yields [12 x ubyte]*:aptr</i>
1429   %aptr = getelementptr {int, [12 x ubyte]}* %sptr, long 0, ubyte 1<br></pre>
1430 <h5>&nbsp;Note To The Novice:</h5>
1431 When using indexing into global arrays with the  '<tt>getelementptr</tt>'
1432 instruction, you must remember that the&nbsp; </div>
1433 <!-- ======================================================================= -->
1434 <div class="doc_subsection"> <a name="otherops">Other Operations</a> </div>
1435 <div class="doc_text">
1436 <p>The instructions in this catagory are the "miscellaneous"
1437 instructions, which defy better classification.</p>
1438 </div>
1439 <!-- _______________________________________________________________________ -->
1440 <div class="doc_subsubsection"> <a name="i_phi">'<tt>phi</tt>'
1441 Instruction</a> </div>
1442 <div class="doc_text">
1443 <h5>Syntax:</h5>
1444 <pre>  &lt;result&gt; = phi &lt;ty&gt; [ &lt;val0&gt;, &lt;label0&gt;], ...<br></pre>
1445 <h5>Overview:</h5>
1446 <p>The '<tt>phi</tt>' instruction is used to implement the &#966; node in
1447 the SSA graph representing the function.</p>
1448 <h5>Arguments:</h5>
1449 <p>The type of the incoming values are specified with the first type
1450 field. After this, the '<tt>phi</tt>' instruction takes a list of pairs
1451 as arguments, with one pair for each predecessor basic block of the
1452 current block.  Only values of <a href="#t_firstclass">first class</a>
1453 type may be used as the value arguments to the PHI node.  Only labels
1454 may be used as the label arguments.</p>
1455 <p>There must be no non-phi instructions between the start of a basic
1456 block and the PHI instructions: i.e. PHI instructions must be first in
1457 a basic block.</p>
1458 <h5>Semantics:</h5>
1459 <p>At runtime, the '<tt>phi</tt>' instruction logically takes on the
1460 value specified by the parameter, depending on which basic block we
1461 came from in the last <a href="#terminators">terminator</a> instruction.</p>
1462 <h5>Example:</h5>
1463 <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>
1464 </div>
1465 <!-- _______________________________________________________________________ -->
1466 <div class="doc_subsubsection"> <a name="i_cast">'<tt>cast .. to</tt>'
1467 Instruction</a> </div>
1468 <div class="doc_text">
1469 <h5>Syntax:</h5>
1470 <pre>  &lt;result&gt; = cast &lt;ty&gt; &lt;value&gt; to &lt;ty2&gt;             <i>; yields ty2</i>
1471 </pre>
1472 <h5>Overview:</h5>
1473 <p>The '<tt>cast</tt>' instruction is used as the primitive means to
1474 convert integers to floating point, change data type sizes, and break
1475 type safety (by casting pointers).</p>
1476 <h5>Arguments:</h5>
1477 <p>The '<tt>cast</tt>' instruction takes a value to cast, which must be
1478 a first class value, and a type to cast it to, which must also be a <a
1479  href="#t_firstclass">first class</a> type.</p>
1480 <h5>Semantics:</h5>
1481 <p>This instruction follows the C rules for explicit casts when
1482 determining how the data being cast must change to fit in its new
1483 container.</p>
1484 <p>When casting to bool, any value that would be considered true in the
1485 context of a C '<tt>if</tt>' condition is converted to the boolean '<tt>true</tt>'
1486 values, all else are '<tt>false</tt>'.</p>
1487 <p>When extending an integral value from a type of one signness to
1488 another (for example '<tt>sbyte</tt>' to '<tt>ulong</tt>'), the value
1489 is sign-extended if the <b>source</b> value is signed, and
1490 zero-extended if the source value is unsigned. <tt>bool</tt> values
1491 are always zero extended into either zero or one.</p>
1492 <h5>Example:</h5>
1493 <pre>  %X = cast int 257 to ubyte              <i>; yields ubyte:1</i>
1494   %Y = cast int 123 to bool               <i>; yields bool:true</i>
1495 </pre>
1496 </div>
1497 <!-- _______________________________________________________________________ -->
1498 <div class="doc_subsubsection"> <a name="i_call">'<tt>call</tt>'
1499 Instruction</a> </div>
1500 <div class="doc_text">
1501 <h5>Syntax:</h5>
1502 <pre>  &lt;result&gt; = call &lt;ty&gt;* &lt;fnptrval&gt;(&lt;param list&gt;)<br></pre>
1503 <h5>Overview:</h5>
1504 <p>The '<tt>call</tt>' instruction represents a simple function call.</p>
1505 <h5>Arguments:</h5>
1506 <p>This instruction requires several arguments:</p>
1507 <ol>
1508   <li>
1509     <p>'<tt>ty</tt>': shall be the signature of the pointer to function
1510 value   being invoked.  The argument types must match the types implied
1511 by this   signature.</p>
1512   </li>
1513   <li>
1514     <p>'<tt>fnptrval</tt>': An LLVM value containing a pointer to a
1515 function   to be invoked. In most cases, this is a direct function
1516 invocation, but   indirect <tt>call</tt>s are just as possible,
1517 calling an arbitrary pointer to   function values.</p>
1518   </li>
1519   <li>
1520     <p>'<tt>function args</tt>': argument list whose types match the
1521 function   signature argument types.  If the function signature
1522 indicates the function   accepts a variable number of arguments, the
1523 extra arguments can be   specified.</p>
1524   </li>
1525 </ol>
1526 <h5>Semantics:</h5>
1527 <p>The '<tt>call</tt>' instruction is used to cause control flow to
1528 transfer to a specified function, with its incoming arguments bound to
1529 the specified values. Upon a '<tt><a href="#i_ret">ret</a></tt>'
1530 instruction in the called function, control flow continues with the
1531 instruction after the function call, and the return value of the
1532 function is bound to the result argument.  This is a simpler case of
1533 the <a href="#i_invoke">invoke</a> instruction.</p>
1534 <h5>Example:</h5>
1535 <pre>  %retval = call int %test(int %argc)<br>  call int(sbyte*, ...) *%printf(sbyte* %msg, int 12, sbyte 42);<br></pre>
1536 </div>
1537 <!-- _______________________________________________________________________ -->
1538 <div class="doc_subsubsection"> <a name="i_vanext">'<tt>vanext</tt>'
1539 Instruction</a> </div>
1540 <div class="doc_text">
1541 <h5>Syntax:</h5>
1542 <pre>  &lt;resultarglist&gt; = vanext &lt;va_list&gt; &lt;arglist&gt;, &lt;argty&gt;<br></pre>
1543 <h5>Overview:</h5>
1544 <p>The '<tt>vanext</tt>' instruction is used to access arguments passed
1545 through the "variable argument" area of a function call.  It is used to
1546 implement the <tt>va_arg</tt> macro in C.</p>
1547 <h5>Arguments:</h5>
1548 <p>This instruction takes a <tt>valist</tt> value and the type of the
1549 argument. It returns another <tt>valist</tt>.</p>
1550 <h5>Semantics:</h5>
1551 <p>The '<tt>vanext</tt>' instruction advances the specified <tt>valist</tt>
1552 past an argument of the specified type.  In conjunction with the <a
1553  href="#i_vaarg"><tt>vaarg</tt></a> instruction, it is used to implement
1554 the <tt>va_arg</tt> macro available in C.  For more information, see
1555 the variable argument handling <a href="#int_varargs">Intrinsic
1556 Functions</a>.</p>
1557 <p>It is legal for this instruction to be called in a function which
1558 does not take a variable number of arguments, for example, the <tt>vfprintf</tt>
1559 function.</p>
1560 <p><tt>vanext</tt> is an LLVM instruction instead of an <a
1561  href="#intrinsics">intrinsic function</a> because it takes an type as
1562 an argument.</p>
1563 <h5>Example:</h5>
1564 <p>See the <a href="#int_varargs">variable argument processing</a>
1565 section.</p>
1566 </div>
1567 <!-- _______________________________________________________________________ -->
1568 <div class="doc_subsubsection"> <a name="i_vaarg">'<tt>vaarg</tt>'
1569 Instruction</a> </div>
1570 <div class="doc_text">
1571 <h5>Syntax:</h5>
1572 <pre>  &lt;resultval&gt; = vaarg &lt;va_list&gt; &lt;arglist&gt;, &lt;argty&gt;<br></pre>
1573 <h5>Overview:</h5>
1574 <p>The '<tt>vaarg</tt>' instruction is used to access arguments passed
1575 through the "variable argument" area of a function call.  It is used to
1576 implement the <tt>va_arg</tt> macro in C.</p>
1577 <h5>Arguments:</h5>
1578 <p>This instruction takes a <tt>valist</tt> value and the type of the
1579 argument. It returns a value of the specified argument type.</p>
1580 <h5>Semantics:</h5>
1581 <p>The '<tt>vaarg</tt>' instruction loads an argument of the specified
1582 type from the specified <tt>va_list</tt>.  In conjunction with the <a
1583  href="#i_vanext"><tt>vanext</tt></a> instruction, it is used to
1584 implement the <tt>va_arg</tt> macro available in C.  For more
1585 information, see the variable argument handling <a href="#int_varargs">Intrinsic
1586 Functions</a>.</p>
1587 <p>It is legal for this instruction to be called in a function which
1588 does not take a variable number of arguments, for example, the <tt>vfprintf</tt>
1589 function.</p>
1590 <p><tt>vaarg</tt> is an LLVM instruction instead of an <a
1591  href="#intrinsics">intrinsic function</a> because it takes an type as
1592 an argument.</p>
1593 <h5>Example:</h5>
1594 <p>See the <a href="#int_varargs">variable argument processing</a>
1595 section.</p>
1596 </div>
1597
1598 <!-- *********************************************************************** -->
1599 <div class="doc_section"> <a name="intrinsics">Intrinsic Functions</a> </div>
1600 <!-- *********************************************************************** -->
1601
1602 <div class="doc_text">
1603
1604 <p>LLVM supports the notion of an "intrinsic function".  These functions have
1605 well known names and semantics, and are required to follow certain
1606 restrictions. Overall, these instructions represent an extension mechanism for
1607 the LLVM language that does not require changing all of the transformations in
1608 LLVM to add to the language (or the bytecode reader/writer, the parser,
1609 etc...).</p>
1610
1611 <p>Intrinsic function names must all start with an "<tt>llvm.</tt>" prefix, this
1612 prefix is reserved in LLVM for intrinsic names, thus functions may not be named
1613 this.  Intrinsic functions must always be external functions: you cannot define
1614 the body of intrinsic functions.  Intrinsic functions may only be used in call
1615 or invoke instructions: it is illegal to take the address of an intrinsic
1616 function.  Additionally, because intrinsic functions are part of the LLVM
1617 language, it is required that they all be documented here if any are added.</p>
1618
1619
1620 <p>
1621 Adding an intrinsic to LLVM is straight-forward if it is possible to express the
1622 concept in LLVM directly (ie, code generator support is not _required_).  To do
1623 this, extend the default implementation of the IntrinsicLowering class to handle
1624 the intrinsic.  Code generators use this class to lower intrinsics they do not
1625 understand to raw LLVM instructions that they do.
1626 </p>
1627
1628 </div>
1629
1630 <!-- ======================================================================= -->
1631 <div class="doc_subsection">
1632   <a name="int_varargs">Variable Argument Handling Intrinsics</a>
1633 </div>
1634
1635 <div class="doc_text">
1636 <p>Variable argument support is defined in LLVM with the <a
1637  href="#i_vanext"><tt>vanext</tt></a> instruction and these three
1638 intrinsic functions.  These functions are related to the similarly
1639 named macros defined in the <tt>&lt;stdarg.h&gt;</tt> header file.</p>
1640 <p>All of these functions operate on arguments that use a
1641 target-specific value type "<tt>va_list</tt>".  The LLVM assembly
1642 language reference manual does not define what this type is, so all
1643 transformations should be prepared to handle intrinsics with any type
1644 used.</p>
1645 <p>This example shows how the <a href="#i_vanext"><tt>vanext</tt></a>
1646 instruction and the variable argument handling intrinsic functions are
1647 used.</p>
1648 <pre>
1649 int %test(int %X, ...) {
1650   ; Initialize variable argument processing
1651   %ap = call sbyte* %<a href="#i_va_start">llvm.va_start</a>()
1652
1653   ; Read a single integer argument
1654   %tmp = vaarg sbyte* %ap, int
1655
1656   ; Advance to the next argument
1657   %ap2 = vanext sbyte* %ap, int
1658
1659   ; Demonstrate usage of llvm.va_copy and llvm.va_end
1660   %aq = call sbyte* %<a href="#i_va_copy">llvm.va_copy</a>(sbyte* %ap2)
1661   call void %<a href="#i_va_end">llvm.va_end</a>(sbyte* %aq)
1662
1663   ; Stop processing of arguments.
1664   call void %<a href="#i_va_end">llvm.va_end</a>(sbyte* %ap2)
1665   ret int %tmp
1666 }
1667 </pre>
1668 </div>
1669
1670 <!-- _______________________________________________________________________ -->
1671 <div class="doc_subsubsection">
1672   <a name="i_va_start">'<tt>llvm.va_start</tt>' Intrinsic</a>
1673 </div>
1674
1675
1676 <div class="doc_text">
1677 <h5>Syntax:</h5>
1678 <pre>  call va_list ()* %llvm.va_start()<br></pre>
1679 <h5>Overview:</h5>
1680 <p>The '<tt>llvm.va_start</tt>' intrinsic returns a new <tt>&lt;arglist&gt;</tt>
1681 for subsequent use by the variable argument intrinsics.</p>
1682 <h5>Semantics:</h5>
1683 <p>The '<tt>llvm.va_start</tt>' intrinsic works just like the <tt>va_start</tt>
1684 macro available in C.  In a target-dependent way, it initializes and
1685 returns a <tt>va_list</tt> element, so that the next <tt>vaarg</tt>
1686 will produce the first variable argument passed to the function.  Unlike
1687 the C <tt>va_start</tt> macro, this intrinsic does not need to know the
1688 last argument of the function, the compiler can figure that out.</p>
1689 <p>Note that this intrinsic function is only legal to be called from
1690 within the body of a variable argument function.</p>
1691 </div>
1692
1693 <!-- _______________________________________________________________________ -->
1694 <div class="doc_subsubsection">
1695  <a name="i_va_end">'<tt>llvm.va_end</tt>' Intrinsic</a>
1696 </div>
1697
1698 <div class="doc_text">
1699 <h5>Syntax:</h5>
1700 <pre>  call void (va_list)* %llvm.va_end(va_list &lt;arglist&gt;)<br></pre>
1701 <h5>Overview:</h5>
1702 <p>The '<tt>llvm.va_end</tt>' intrinsic destroys <tt>&lt;arglist&gt;</tt>
1703 which has been initialized previously with <tt><a href="#i_va_start">llvm.va_start</a></tt>
1704 or <tt><a href="#i_va_copy">llvm.va_copy</a></tt>.</p>
1705 <h5>Arguments:</h5>
1706 <p>The argument is a <tt>va_list</tt> to destroy.</p>
1707 <h5>Semantics:</h5>
1708 <p>The '<tt>llvm.va_end</tt>' intrinsic works just like the <tt>va_end</tt>
1709 macro available in C.  In a target-dependent way, it destroys the <tt>va_list</tt>.
1710 Calls to <a href="#i_va_start"><tt>llvm.va_start</tt></a> and <a
1711  href="#i_va_copy"><tt>llvm.va_copy</tt></a> must be matched exactly
1712 with calls to <tt>llvm.va_end</tt>.</p>
1713 </div>
1714
1715 <!-- _______________________________________________________________________ -->
1716 <div class="doc_subsubsection">
1717   <a name="i_va_copy">'<tt>llvm.va_copy</tt>' Intrinsic</a>
1718 </div>
1719
1720 <div class="doc_text">
1721 <h5>Syntax:</h5>
1722 <pre>  call va_list (va_list)* %llvm.va_copy(va_list &lt;destarglist&gt;)<br></pre>
1723 <h5>Overview:</h5>
1724 <p>The '<tt>llvm.va_copy</tt>' intrinsic copies the current argument
1725 position from the source argument list to the destination argument list.</p>
1726 <h5>Arguments:</h5>
1727 <p>The argument is the <tt>va_list</tt> to copy.</p>
1728 <h5>Semantics:</h5>
1729 <p>The '<tt>llvm.va_copy</tt>' intrinsic works just like the <tt>va_copy</tt>
1730 macro available in C.  In a target-dependent way, it copies the source <tt>va_list</tt>
1731 element into the returned list.  This intrinsic is necessary because the <tt><a
1732  href="i_va_start">llvm.va_start</a></tt> intrinsic may be arbitrarily
1733 complex and require memory allocation, for example.</p>
1734 </div>
1735
1736 <!-- ======================================================================= -->
1737 <div class="doc_subsection">
1738   <a name="int_libc">Standard C Library Intrinsics</a>
1739 </div>
1740
1741 <div class="doc_text">
1742 <p>
1743
1744 </p>
1745
1746 </div>
1747
1748 <!-- _______________________________________________________________________ -->
1749 <div class="doc_subsubsection">
1750   <a name="i_memcpy">'<tt>llvm.memcpy</tt>' Intrinsic</a>
1751 </div>
1752
1753 <div class="doc_text">
1754
1755 <h5>Syntax:</h5>
1756 <pre>
1757   call void (sbyte*, sbyte*, uint, uint)* %llvm.memcpy(sbyte* &lt;dest&gt;, sbyte* &lt;src&gt;,
1758                                                        uint &lt;len&gt;, uint &lt;align&gt;)
1759 </pre>
1760
1761 <h5>Overview:</h5>
1762
1763 <p>
1764 The '<tt>llvm.memcpy</tt>' intrinsic copies a block of memory from the source
1765 location to the destination location.
1766 </p>
1767
1768 <p>
1769 Note that, unlike the standard libc function, the <tt>llvm.memcpy</tt> intrinsic
1770 does not return a value, and takes an extra alignment argument.
1771 </p>
1772
1773 <h5>Arguments:</h5>
1774
1775 <p>
1776 The first argument is a pointer to the destination, the second is a pointer to
1777 the source.  The third argument is an (arbitrarily sized) integer argument
1778 specifying the number of bytes to copy, and the fourth argument is the alignment
1779 of the source and destination locations.
1780 </p>
1781
1782 <p>
1783 If the call to this intrinisic has an alignment value that is not 0 or 1, then
1784 the caller guarantees that the size of the copy is a multiple of the alignment
1785 and that both the source and destination pointers are aligned to that boundary.
1786 </p>
1787
1788 <h5>Semantics:</h5>
1789
1790 <p>
1791 The '<tt>llvm.memcpy</tt>' intrinsic copies a block of memory from the source
1792 location to the destination location, which are not allowed to overlap.  It
1793 copies "len" bytes of memory over.  If the argument is known to be aligned to
1794 some boundary, this can be specified as the fourth argument, otherwise it should
1795 be set to 0 or 1.
1796 </p>
1797 </div>
1798
1799
1800 <!-- _______________________________________________________________________ -->
1801 <div class="doc_subsubsection">
1802   <a name="i_memmove">'<tt>llvm.memmove</tt>' Intrinsic</a>
1803 </div>
1804
1805 <div class="doc_text">
1806
1807 <h5>Syntax:</h5>
1808 <pre>
1809   call void (sbyte*, sbyte*, uint, uint)* %llvm.memmove(sbyte* &lt;dest&gt;, sbyte* &lt;src&gt;,
1810                                                        uint &lt;len&gt;, uint &lt;align&gt;)
1811 </pre>
1812
1813 <h5>Overview:</h5>
1814
1815 <p>
1816 The '<tt>llvm.memmove</tt>' intrinsic moves a block of memory from the source
1817 location to the destination location. It is similar to the '<tt>llvm.memcpy</tt>' 
1818 intrinsic but allows the two memory locations to overlap.
1819 </p>
1820
1821 <p>
1822 Note that, unlike the standard libc function, the <tt>llvm.memmove</tt> intrinsic
1823 does not return a value, and takes an extra alignment argument.
1824 </p>
1825
1826 <h5>Arguments:</h5>
1827
1828 <p>
1829 The first argument is a pointer to the destination, the second is a pointer to
1830 the source.  The third argument is an (arbitrarily sized) integer argument
1831 specifying the number of bytes to copy, and the fourth argument is the alignment
1832 of the source and destination locations.
1833 </p>
1834
1835 <p>
1836 If the call to this intrinisic has an alignment value that is not 0 or 1, then
1837 the caller guarantees that the size of the copy is a multiple of the alignment
1838 and that both the source and destination pointers are aligned to that boundary.
1839 </p>
1840
1841 <h5>Semantics:</h5>
1842
1843 <p>
1844 The '<tt>llvm.memmove</tt>' intrinsic copies a block of memory from the source
1845 location to the destination location, which may overlap.  It
1846 copies "len" bytes of memory over.  If the argument is known to be aligned to
1847 some boundary, this can be specified as the fourth argument, otherwise it should
1848 be set to 0 or 1.
1849 </p>
1850 </div>
1851
1852
1853 <!-- ======================================================================= -->
1854 <div class="doc_subsection">
1855   <a name="int_debugger">Debugger Intrinsics</a>
1856 </div>
1857
1858 <div class="doc_text">
1859 <p>
1860 The LLVM debugger intrinsics (which all start with <tt>llvm.dbg.</tt> prefix),
1861 are described in the <a
1862 href="SourceLevelDebugging.html#format_common_intrinsics">LLVM Source Level
1863 Debugging</a> document.
1864 </p>
1865 </div>
1866
1867
1868 <!-- *********************************************************************** -->
1869 <hr>
1870 <div class="doc_footer">
1871 <address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
1872 <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a> <br>
1873 Last modified: $Date$ </div>
1874 </body>
1875 </html>