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