blob: e8c1f01cee16ccc4b0f886d16113a3cc8daf0955 [file] [log] [blame]
Chris Lattner261efe92003-11-25 01:02:51 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
Misha Brukman9d0919f2003-11-08 01:05:38 +00002<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>
Chris Lattner261efe92003-11-25 01:02:51 +00008<div class="doc_title"> LLVM Language Reference Manual </div>
Chris Lattner00950542001-06-06 20:29:01 +00009<ol>
Misha Brukman9d0919f2003-11-08 01:05:38 +000010 <li><a href="#abstract">Abstract</a></li>
11 <li><a href="#introduction">Introduction</a></li>
12 <li><a href="#identifiers">Identifiers</a></li>
Chris Lattner00950542001-06-06 20:29:01 +000013 <li><a href="#typesystem">Type System</a>
14 <ol>
Chris Lattner261efe92003-11-25 01:02:51 +000015 <li><a href="#t_primitive">Primitive Types</a>
16 <ol>
Misha Brukman9d0919f2003-11-08 01:05:38 +000017 <li><a href="#t_classifications">Type Classifications</a></li>
Chris Lattner261efe92003-11-25 01:02:51 +000018 </ol>
19 </li>
Chris Lattner00950542001-06-06 20:29:01 +000020 <li><a href="#t_derived">Derived Types</a>
21 <ol>
Chris Lattner261efe92003-11-25 01:02:51 +000022 <li><a href="#t_array">Array Type</a></li>
Misha Brukman9d0919f2003-11-08 01:05:38 +000023 <li><a href="#t_function">Function Type</a></li>
24 <li><a href="#t_pointer">Pointer Type</a></li>
Chris Lattner261efe92003-11-25 01:02:51 +000025 <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>
Chris Lattner00950542001-06-06 20:29:01 +000031 <li><a href="#highlevel">High Level Structure</a>
32 <ol>
Misha Brukman9d0919f2003-11-08 01:05:38 +000033 <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>
Chris Lattner261efe92003-11-25 01:02:51 +000036 </ol>
37 </li>
Chris Lattner00950542001-06-06 20:29:01 +000038 <li><a href="#instref">Instruction Reference</a>
39 <ol>
40 <li><a href="#terminators">Terminator Instructions</a>
41 <ol>
Chris Lattner261efe92003-11-25 01:02:51 +000042 <li><a href="#i_ret">'<tt>ret</tt>' Instruction</a></li>
43 <li><a href="#i_br">'<tt>br</tt>' Instruction</a></li>
Misha Brukman9d0919f2003-11-08 01:05:38 +000044 <li><a href="#i_switch">'<tt>switch</tt>' Instruction</a></li>
45 <li><a href="#i_invoke">'<tt>invoke</tt>' Instruction</a></li>
Chris Lattner261efe92003-11-25 01:02:51 +000046 <li><a href="#i_unwind">'<tt>unwind</tt>' Instruction</a></li>
47 </ol>
48 </li>
Chris Lattner00950542001-06-06 20:29:01 +000049 <li><a href="#binaryops">Binary Operations</a>
50 <ol>
Chris Lattner261efe92003-11-25 01:02:51 +000051 <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>
Misha Brukman9d0919f2003-11-08 01:05:38 +000056 <li><a href="#i_setcc">'<tt>set<i>cc</i></tt>' Instructions</a></li>
Chris Lattner261efe92003-11-25 01:02:51 +000057 </ol>
58 </li>
Chris Lattner00950542001-06-06 20:29:01 +000059 <li><a href="#bitwiseops">Bitwise Binary Operations</a>
60 <ol>
Misha Brukman9d0919f2003-11-08 01:05:38 +000061 <li><a href="#i_and">'<tt>and</tt>' Instruction</a></li>
Chris Lattner261efe92003-11-25 01:02:51 +000062 <li><a href="#i_or">'<tt>or</tt>' Instruction</a></li>
Misha Brukman9d0919f2003-11-08 01:05:38 +000063 <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>
Chris Lattner261efe92003-11-25 01:02:51 +000066 </ol>
67 </li>
Chris Lattner00950542001-06-06 20:29:01 +000068 <li><a href="#memoryops">Memory Access Operations</a>
69 <ol>
Chris Lattner261efe92003-11-25 01:02:51 +000070 <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>
Chris Lattner00950542001-06-06 20:29:01 +000078 <li><a href="#otherops">Other Operations</a>
79 <ol>
Chris Lattner261efe92003-11-25 01:02:51 +000080 <li><a href="#i_phi">'<tt>phi</tt>' Instruction</a></li>
Misha Brukman9d0919f2003-11-08 01:05:38 +000081 <li><a href="#i_cast">'<tt>cast .. to</tt>' Instruction</a></li>
Chris Lattner261efe92003-11-25 01:02:51 +000082 <li><a href="#i_call">'<tt>call</tt>' Instruction</a></li>
Misha Brukman9d0919f2003-11-08 01:05:38 +000083 <li><a href="#i_vanext">'<tt>vanext</tt>' Instruction</a></li>
Chris Lattner261efe92003-11-25 01:02:51 +000084 <li><a href="#i_vaarg">'<tt>vaarg</tt>' Instruction</a></li>
Chris Lattner00950542001-06-06 20:29:01 +000085 </ol>
Chris Lattner261efe92003-11-25 01:02:51 +000086 </li>
Chris Lattner00950542001-06-06 20:29:01 +000087 </ol>
Chris Lattner261efe92003-11-25 01:02:51 +000088 </li>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +000089 <li><a href="#intrinsics">Intrinsic Functions</a>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +000090 <ol>
Chris Lattner261efe92003-11-25 01:02:51 +000091 <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>
Chris Lattner10610642004-02-14 04:08:35 +000098 <li><a href="#int_codegen">Code Generator Intrinsics</a>
99 <ol>
100 <li><a href="#i_returnaddress">'<tt>llvm.returnaddress</tt>' Intrinsic</a></li>
101 <li><a href="#i_frameaddress">'<tt>llvm.frameaddress</tt>' Intrinsic</a></li>
102 </ol>
103 </li>
Chris Lattner33aec9e2004-02-12 17:01:32 +0000104 <li><a href="#int_libc">Standard C Library Intrinsics</a>
105 <ol>
106 <li><a href="#i_memcpy">'<tt>llvm.memcpy</tt>' Intrinsic</a></li>
Chris Lattner0eb51b42004-02-12 18:10:10 +0000107 <li><a href="#i_memmove">'<tt>llvm.memmove</tt>' Intrinsic</a></li>
Chris Lattner10610642004-02-14 04:08:35 +0000108 <li><a href="#i_memset">'<tt>llvm.memset</tt>' Intrinsic</a></li>
Chris Lattner33aec9e2004-02-12 17:01:32 +0000109 </ol>
110 </li>
Chris Lattner8ff75902004-01-06 05:31:32 +0000111 <li><a href="#int_debugger">Debugger intrinsics</a>
Chris Lattner261efe92003-11-25 01:02:51 +0000112 </ol>
113 </li>
Chris Lattner00950542001-06-06 20:29:01 +0000114</ol>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000115<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000116<p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>
117and <a href="mailto:vadve@cs.uiuc.edu">Vikram Adve</a></b></p>
118<p> </p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000119</div>
Chris Lattner00950542001-06-06 20:29:01 +0000120<!-- *********************************************************************** -->
Chris Lattner261efe92003-11-25 01:02:51 +0000121<div class="doc_section"> <a name="abstract">Abstract </a></div>
122<!-- *********************************************************************** -->
Misha Brukman9d0919f2003-11-08 01:05:38 +0000123<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000124<p>This document is a reference manual for the LLVM assembly language.
125LLVM is an SSA based representation that provides type safety,
126low-level operations, flexibility, and the capability of representing
127'all' high-level languages cleanly. It is the common code
128representation used throughout all phases of the LLVM compilation
129strategy.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000130</div>
Chris Lattner00950542001-06-06 20:29:01 +0000131<!-- *********************************************************************** -->
Chris Lattner261efe92003-11-25 01:02:51 +0000132<div class="doc_section"> <a name="introduction">Introduction</a> </div>
133<!-- *********************************************************************** -->
Misha Brukman9d0919f2003-11-08 01:05:38 +0000134<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000135<p>The LLVM code representation is designed to be used in three
136different forms: as an in-memory compiler IR, as an on-disk bytecode
137representation (suitable for fast loading by a Just-In-Time compiler),
138and as a human readable assembly language representation. This allows
139LLVM to provide a powerful intermediate representation for efficient
140compiler transformations and analysis, while providing a natural means
141to debug and visualize the transformations. The three different forms
142of LLVM are all equivalent. This document describes the human readable
143representation and notation.</p>
144<p>The LLVM representation aims to be a light-weight and low-level
145while being expressive, typed, and extensible at the same time. It
146aims to be a "universal IR" of sorts, by being at a low enough level
147that high-level ideas may be cleanly mapped to it (similar to how
148microprocessors are "universal IR's", allowing many source languages to
149be mapped to them). By providing type information, LLVM can be used as
150the target of optimizations: for example, through pointer analysis, it
151can be proven that a C automatic variable is never accessed outside of
152the current function... allowing it to be promoted to a simple SSA
153value instead of a memory location.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000154</div>
Chris Lattner00950542001-06-06 20:29:01 +0000155<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000156<div class="doc_subsubsection"> <a name="wellformed">Well-Formedness</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000157<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000158<p>It is important to note that this document describes 'well formed'
159LLVM assembly language. There is a difference between what the parser
160accepts and what is considered 'well formed'. For example, the
161following instruction is syntactically okay, but not well formed:</p>
162<pre> %x = <a href="#i_add">add</a> int 1, %x<br></pre>
163<p>...because the definition of <tt>%x</tt> does not dominate all of
164its uses. The LLVM infrastructure provides a verification pass that may
165be used to verify that an LLVM module is well formed. This pass is
166automatically run by the parser after parsing input assembly, and by
167the optimizer before it outputs bytecode. The violations pointed out
168by the verifier pass indicate bugs in transformation passes or input to
169the parser.</p>
170<!-- Describe the typesetting conventions here. --> </div>
Chris Lattner00950542001-06-06 20:29:01 +0000171<!-- *********************************************************************** -->
Chris Lattner261efe92003-11-25 01:02:51 +0000172<div class="doc_section"> <a name="identifiers">Identifiers</a> </div>
Chris Lattner00950542001-06-06 20:29:01 +0000173<!-- *********************************************************************** -->
Misha Brukman9d0919f2003-11-08 01:05:38 +0000174<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000175<p>LLVM uses three different forms of identifiers, for different
176purposes:</p>
Chris Lattner00950542001-06-06 20:29:01 +0000177<ol>
Chris Lattner261efe92003-11-25 01:02:51 +0000178 <li>Numeric constants are represented as you would expect: 12, -3
179123.421, etc. Floating point constants have an optional hexidecimal
180notation.</li>
181 <li>Named values are represented as a string of characters with a '%'
182prefix. For example, %foo, %DivisionByZero,
183%a.really.long.identifier. The actual regular expression used is '<tt>%[a-zA-Z$._][a-zA-Z$._0-9]*</tt>'.
184Identifiers which require other characters in their names can be
185surrounded with quotes. In this way, anything except a <tt>"</tt>
186character can be used in a name.</li>
187 <li>Unnamed values are represented as an unsigned numeric value with
188a '%' prefix. For example, %12, %2, %44.</li>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000189</ol>
Chris Lattner261efe92003-11-25 01:02:51 +0000190<p>LLVM requires the values start with a '%' sign for two reasons:
191Compilers don't need to worry about name clashes with reserved words,
192and the set of reserved words may be expanded in the future without
193penalty. Additionally, unnamed identifiers allow a compiler to quickly
194come up with a temporary variable without having to avoid symbol table
195conflicts.</p>
196<p>Reserved words in LLVM are very similar to reserved words in other
197languages. There are keywords for different opcodes ('<tt><a
198 href="#i_add">add</a></tt>', '<tt><a href="#i_cast">cast</a></tt>', '<tt><a
199 href="#i_ret">ret</a></tt>', etc...), for primitive type names ('<tt><a
200 href="#t_void">void</a></tt>', '<tt><a href="#t_uint">uint</a></tt>',
201etc...), and others. These reserved words cannot conflict with
202variable names, because none of them start with a '%' character.</p>
203<p>Here is an example of LLVM code to multiply the integer variable '<tt>%X</tt>'
204by 8:</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000205<p>The easy way:</p>
Chris Lattner261efe92003-11-25 01:02:51 +0000206<pre> %result = <a href="#i_mul">mul</a> uint %X, 8<br></pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000207<p>After strength reduction:</p>
Chris Lattner261efe92003-11-25 01:02:51 +0000208<pre> %result = <a href="#i_shl">shl</a> uint %X, ubyte 3<br></pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000209<p>And the hard way:</p>
Chris Lattner261efe92003-11-25 01:02:51 +0000210<pre> <a href="#i_add">add</a> uint %X, %X <i>; yields {uint}:%0</i>
211 <a
212 href="#i_add">add</a> uint %0, %0 <i>; yields {uint}:%1</i>
213 %result = <a
214 href="#i_add">add</a> uint %1, %1<br></pre>
215<p>This last way of multiplying <tt>%X</tt> by 8 illustrates several
216important lexical features of LLVM:</p>
Chris Lattner00950542001-06-06 20:29:01 +0000217<ol>
Chris Lattner261efe92003-11-25 01:02:51 +0000218 <li>Comments are delimited with a '<tt>;</tt>' and go until the end
219of line.</li>
220 <li>Unnamed temporaries are created when the result of a computation
221is not assigned to a named value.</li>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000222 <li>Unnamed temporaries are numbered sequentially</li>
223</ol>
Chris Lattner261efe92003-11-25 01:02:51 +0000224<p>...and it also show a convention that we follow in this document.
225When demonstrating instructions, we will follow an instruction with a
226comment that defines the type and name of value produced. Comments are
227shown in italic text.</p>
228<p>The one non-intuitive notation for constants is the optional
229hexidecimal form of floating point constants. For example, the form '<tt>double
Chris Lattner2b7d3202002-05-06 03:03:22 +00002300x432ff973cafa8000</tt>' is equivalent to (but harder to read than) '<tt>double
Chris Lattner261efe92003-11-25 01:02:51 +00002314.5e+15</tt>' which is also supported by the parser. The only time
232hexadecimal floating point constants are useful (and the only time that
233they are generated by the disassembler) is when an FP constant has to
234be emitted that is not representable as a decimal floating point number
235exactly. For example, NaN's, infinities, and other special cases are
236represented in their IEEE hexadecimal format so that assembly and
237disassembly do not cause any bits to change in the constants.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000238</div>
Chris Lattner00950542001-06-06 20:29:01 +0000239<!-- *********************************************************************** -->
Chris Lattner261efe92003-11-25 01:02:51 +0000240<div class="doc_section"> <a name="typesystem">Type System</a> </div>
241<!-- *********************************************************************** -->
Misha Brukman9d0919f2003-11-08 01:05:38 +0000242<div class="doc_text">
Misha Brukman9d0919f2003-11-08 01:05:38 +0000243<p>The LLVM type system is one of the most important features of the
Chris Lattner261efe92003-11-25 01:02:51 +0000244intermediate representation. Being typed enables a number of
245optimizations to be performed on the IR directly, without having to do
246extra analyses on the side before the transformation. A strong type
247system makes it easier to read the generated code and enables novel
248analyses and transformations that are not feasible to perform on normal
249three address code representations.</p>
Chris Lattner7bae3952002-06-25 18:03:17 +0000250<!-- The written form for the type system was heavily influenced by the
251syntactic problems with types in the C language<sup><a
Chris Lattner261efe92003-11-25 01:02:51 +0000252href="#rw_stroustrup">1</a></sup>.<p> --> </div>
Chris Lattner00950542001-06-06 20:29:01 +0000253<!-- ======================================================================= -->
Chris Lattner261efe92003-11-25 01:02:51 +0000254<div class="doc_subsection"> <a name="t_primitive">Primitive Types</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000255<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000256<p>The primitive types are the fundemental building blocks of the LLVM
257system. The current set of primitive types are as follows:</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000258<p>
259<table border="0" align="center">
Chris Lattner261efe92003-11-25 01:02:51 +0000260 <tbody>
261 <tr>
262 <td>
263 <table border="1" cellspacing="0" cellpadding="4" align="center">
264 <tbody>
265 <tr>
266 <td><tt>void</tt></td>
267 <td>No value</td>
268 </tr>
269 <tr>
270 <td><tt>ubyte</tt></td>
271 <td>Unsigned 8 bit value</td>
272 </tr>
273 <tr>
274 <td><tt>ushort</tt></td>
275 <td>Unsigned 16 bit value</td>
276 </tr>
277 <tr>
278 <td><tt>uint</tt></td>
279 <td>Unsigned 32 bit value</td>
280 </tr>
281 <tr>
282 <td><tt>ulong</tt></td>
283 <td>Unsigned 64 bit value</td>
284 </tr>
285 <tr>
286 <td><tt>float</tt></td>
287 <td>32 bit floating point value</td>
288 </tr>
289 <tr>
290 <td><tt>label</tt></td>
291 <td>Branch destination</td>
292 </tr>
293 </tbody>
294 </table>
295 </td>
296 <td valign="top">
297 <table border="1" cellspacing="0" cellpadding="4" align="center&quot;">
298 <tbody>
299 <tr>
300 <td><tt>bool</tt></td>
301 <td>True or False value</td>
302 </tr>
303 <tr>
304 <td><tt>sbyte</tt></td>
305 <td>Signed 8 bit value</td>
306 </tr>
307 <tr>
308 <td><tt>short</tt></td>
309 <td>Signed 16 bit value</td>
310 </tr>
311 <tr>
312 <td><tt>int</tt></td>
313 <td>Signed 32 bit value</td>
314 </tr>
315 <tr>
316 <td><tt>long</tt></td>
317 <td>Signed 64 bit value</td>
318 </tr>
319 <tr>
320 <td><tt>double</tt></td>
321 <td>64 bit floating point value</td>
322 </tr>
323 </tbody>
324 </table>
325 </td>
326 </tr>
327 </tbody>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000328</table>
329</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000330</div>
Chris Lattner00950542001-06-06 20:29:01 +0000331<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000332<div class="doc_subsubsection"> <a name="t_classifications">Type
333Classifications</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000334<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000335<p>These different primitive types fall into a few useful
336classifications:</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000337<p>
338<table border="1" cellspacing="0" cellpadding="4" align="center">
Chris Lattner261efe92003-11-25 01:02:51 +0000339 <tbody>
340 <tr>
341 <td><a name="t_signed">signed</a></td>
342 <td><tt>sbyte, short, int, long, float, double</tt></td>
343 </tr>
344 <tr>
345 <td><a name="t_unsigned">unsigned</a></td>
346 <td><tt>ubyte, ushort, uint, ulong</tt></td>
347 </tr>
348 <tr>
349 <td><a name="t_integer">integer</a></td>
350 <td><tt>ubyte, sbyte, ushort, short, uint, int, ulong, long</tt></td>
351 </tr>
352 <tr>
353 <td><a name="t_integral">integral</a></td>
354 <td><tt>bool, ubyte, sbyte, ushort, short, uint, int, ulong, long</tt></td>
355 </tr>
356 <tr>
357 <td><a name="t_floating">floating point</a></td>
358 <td><tt>float, double</tt></td>
359 </tr>
360 <tr>
361 <td><a name="t_firstclass">first class</a></td>
362 <td><tt>bool, ubyte, sbyte, ushort, short,<br>
363uint, int, ulong, long, float, double, <a href="#t_pointer">pointer</a></tt></td>
364 </tr>
365 </tbody>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000366</table>
367</p>
Chris Lattner261efe92003-11-25 01:02:51 +0000368<p>The <a href="#t_firstclass">first class</a> types are perhaps the
369most important. Values of these types are the only ones which can be
370produced by instructions, passed as arguments, or used as operands to
371instructions. This means that all structures and arrays must be
372manipulated either by pointer or by component.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000373</div>
Chris Lattner00950542001-06-06 20:29:01 +0000374<!-- ======================================================================= -->
Chris Lattner261efe92003-11-25 01:02:51 +0000375<div class="doc_subsection"> <a name="t_derived">Derived Types</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000376<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000377<p>The real power in LLVM comes from the derived types in the system.
378This is what allows a programmer to represent arrays, functions,
379pointers, and other useful types. Note that these derived types may be
380recursive: For example, it is possible to have a two dimensional array.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000381</div>
Chris Lattner00950542001-06-06 20:29:01 +0000382<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000383<div class="doc_subsubsection"> <a name="t_array">Array Type</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000384<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000385<h5>Overview:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000386<p>The array type is a very simple derived type that arranges elements
Chris Lattner261efe92003-11-25 01:02:51 +0000387sequentially in memory. The array type requires a size (number of
388elements) and an underlying data type.</p>
Chris Lattner7faa8832002-04-14 06:13:44 +0000389<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000390<pre> [&lt;# elements&gt; x &lt;elementtype&gt;]<br></pre>
391<p>The number of elements is a constant integer value, elementtype may
392be any type with a size.</p>
Chris Lattner7faa8832002-04-14 06:13:44 +0000393<h5>Examples:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000394<p> <tt>[40 x int ]</tt>: Array of 40 integer values.<br>
395<tt>[41 x int ]</tt>: Array of 41 integer values.<br>
396<tt>[40 x uint]</tt>: Array of 40 unsigned integer values.</p>
397<p> </p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000398<p>Here are some examples of multidimensional arrays:</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000399<p>
400<table border="0" cellpadding="0" cellspacing="0">
Chris Lattner261efe92003-11-25 01:02:51 +0000401 <tbody>
402 <tr>
403 <td><tt>[3 x [4 x int]]</tt></td>
404 <td>: 3x4 array integer values.</td>
405 </tr>
406 <tr>
407 <td><tt>[12 x [10 x float]]</tt></td>
408 <td>: 12x10 array of single precision floating point values.</td>
409 </tr>
410 <tr>
411 <td><tt>[2 x [3 x [4 x uint]]]</tt></td>
412 <td>: 2x3x4 array of unsigned integer values.</td>
413 </tr>
414 </tbody>
Chris Lattner00950542001-06-06 20:29:01 +0000415</table>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000416</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000417</div>
Chris Lattner00950542001-06-06 20:29:01 +0000418<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000419<div class="doc_subsubsection"> <a name="t_function">Function Type</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000420<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000421<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000422<p>The function type can be thought of as a function signature. It
423consists of a return type and a list of formal parameter types.
John Criswell009900b2003-11-25 21:45:46 +0000424Function types are usually used to build virtual function tables
Chris Lattner261efe92003-11-25 01:02:51 +0000425(which are structures of pointers to functions), for indirect function
426calls, and when defining a function.</p>
John Criswell009900b2003-11-25 21:45:46 +0000427<p>
428The return type of a function type cannot be an aggregate type.
429</p>
Chris Lattner00950542001-06-06 20:29:01 +0000430<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000431<pre> &lt;returntype&gt; (&lt;parameter list&gt;)<br></pre>
432<p>Where '<tt>&lt;parameter list&gt;</tt>' is a comma-separated list of
433type specifiers. Optionally, the parameter list may include a type <tt>...</tt>,
Chris Lattner27f71f22003-09-03 00:41:47 +0000434which indicates that the function takes a variable number of arguments.
435Variable argument functions can access their arguments with the <a
Chris Lattner261efe92003-11-25 01:02:51 +0000436 href="#int_varargs">variable argument handling intrinsic</a> functions.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000437<h5>Examples:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000438<p>
439<table border="0" cellpadding="0" cellspacing="0">
Chris Lattner261efe92003-11-25 01:02:51 +0000440 <tbody>
441 <tr>
442 <td><tt>int (int)</tt></td>
443 <td>: function taking an <tt>int</tt>, returning an <tt>int</tt></td>
444 </tr>
445 <tr>
446 <td><tt>float (int, int *) *</tt></td>
447 <td>: <a href="#t_pointer">Pointer</a> to a function that takes
448an <tt>int</tt> and a <a href="#t_pointer">pointer</a> to <tt>int</tt>,
449returning <tt>float</tt>.</td>
450 </tr>
451 <tr>
452 <td><tt>int (sbyte *, ...)</tt></td>
453 <td>: A vararg function that takes at least one <a
454 href="#t_pointer">pointer</a> to <tt>sbyte</tt> (signed char in C),
455which returns an integer. This is the signature for <tt>printf</tt>
456in LLVM.</td>
457 </tr>
458 </tbody>
Chris Lattner00950542001-06-06 20:29:01 +0000459</table>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000460</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000461</div>
Chris Lattner00950542001-06-06 20:29:01 +0000462<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000463<div class="doc_subsubsection"> <a name="t_struct">Structure Type</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000464<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000465<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000466<p>The structure type is used to represent a collection of data members
467together in memory. The packing of the field types is defined to match
468the ABI of the underlying processor. The elements of a structure may
469be any type that has a size.</p>
470<p>Structures are accessed using '<tt><a href="#i_load">load</a></tt>
471and '<tt><a href="#i_store">store</a></tt>' by getting a pointer to a
472field with the '<tt><a href="#i_getelementptr">getelementptr</a></tt>'
473instruction.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000474<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000475<pre> { &lt;type list&gt; }<br></pre>
Chris Lattner00950542001-06-06 20:29:01 +0000476<h5>Examples:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000477<p>
478<table border="0" cellpadding="0" cellspacing="0">
Chris Lattner261efe92003-11-25 01:02:51 +0000479 <tbody>
480 <tr>
481 <td><tt>{ int, int, int }</tt></td>
482 <td>: a triple of three <tt>int</tt> values</td>
483 </tr>
484 <tr>
485 <td><tt>{ float, int (int) * }</tt></td>
486 <td>: A pair, where the first element is a <tt>float</tt> and the
487second element is a <a href="#t_pointer">pointer</a> to a <a
488 href="t_function">function</a> that takes an <tt>int</tt>, returning
489an <tt>int</tt>.</td>
490 </tr>
491 </tbody>
Chris Lattner00950542001-06-06 20:29:01 +0000492</table>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000493</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000494</div>
Chris Lattner00950542001-06-06 20:29:01 +0000495<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000496<div class="doc_subsubsection"> <a name="t_pointer">Pointer Type</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000497<div class="doc_text">
Chris Lattner7faa8832002-04-14 06:13:44 +0000498<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000499<p>As in many languages, the pointer type represents a pointer or
500reference to another object, which must live in memory.</p>
Chris Lattner7faa8832002-04-14 06:13:44 +0000501<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000502<pre> &lt;type&gt; *<br></pre>
Chris Lattner7faa8832002-04-14 06:13:44 +0000503<h5>Examples:</h5>
Chris Lattner7faa8832002-04-14 06:13:44 +0000504<p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000505<table border="0" cellpadding="0" cellspacing="0">
Chris Lattner261efe92003-11-25 01:02:51 +0000506 <tbody>
507 <tr>
508 <td><tt>[4x int]*</tt></td>
509 <td>: <a href="#t_pointer">pointer</a> to <a href="#t_array">array</a>
510of four <tt>int</tt> values</td>
511 </tr>
512 <tr>
513 <td><tt>int (int *) *</tt></td>
514 <td>: A <a href="#t_pointer">pointer</a> to a <a
515 href="t_function">function</a> that takes an <tt>int</tt>, returning
516an <tt>int</tt>.</td>
517 </tr>
518 </tbody>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000519</table>
520</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000521</div>
Chris Lattner261efe92003-11-25 01:02:51 +0000522<!-- _______________________________________________________________________ --><!--
Misha Brukman9d0919f2003-11-08 01:05:38 +0000523<div class="doc_subsubsection">
524 <a name="t_packed">Packed Type</a>
525</div>
526
527<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000528
529Mention/decide that packed types work with saturation or not. Maybe have a packed+saturated type in addition to just a packed type.<p>
530
531Packed types should be 'nonsaturated' because standard data types are not saturated. Maybe have a saturated packed type?<p>
532
Misha Brukman9d0919f2003-11-08 01:05:38 +0000533</div>
534
Chris Lattner261efe92003-11-25 01:02:51 +0000535--><!-- *********************************************************************** -->
536<div class="doc_section"> <a name="highlevel">High Level Structure</a> </div>
537<!-- *********************************************************************** --><!-- ======================================================================= -->
538<div class="doc_subsection"> <a name="modulestructure">Module Structure</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000539<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000540<p>LLVM programs are composed of "Module"s, each of which is a
541translation unit of the input programs. Each module consists of
542functions, global variables, and symbol table entries. Modules may be
543combined together with the LLVM linker, which merges function (and
544global variable) definitions, resolves forward declarations, and merges
545symbol table entries. Here is an example of the "hello world" module:</p>
546<pre><i>; Declare the string constant as a global constant...</i>
547<a href="#identifiers">%.LC0</a> = <a href="#linkage_internal">internal</a> <a
548 href="#globalvars">constant</a> <a href="#t_array">[13 x sbyte]</a> c"hello world\0A\00" <i>; [13 x sbyte]*</i>
Chris Lattner2b7d3202002-05-06 03:03:22 +0000549
Chris Lattner27f71f22003-09-03 00:41:47 +0000550<i>; External declaration of the puts function</i>
551<a href="#functionstructure">declare</a> int %puts(sbyte*) <i>; int(sbyte*)* </i>
Chris Lattner2b7d3202002-05-06 03:03:22 +0000552
553<i>; Definition of main function</i>
Chris Lattner27f71f22003-09-03 00:41:47 +0000554int %main() { <i>; int()* </i>
Chris Lattner2b7d3202002-05-06 03:03:22 +0000555 <i>; Convert [13x sbyte]* to sbyte *...</i>
Chris Lattner261efe92003-11-25 01:02:51 +0000556 %cast210 = <a
557 href="#i_getelementptr">getelementptr</a> [13 x sbyte]* %.LC0, long 0, long 0 <i>; sbyte*</i>
Chris Lattner2b7d3202002-05-06 03:03:22 +0000558
559 <i>; Call puts function to write out the string to stdout...</i>
Chris Lattner261efe92003-11-25 01:02:51 +0000560 <a
561 href="#i_call">call</a> int %puts(sbyte* %cast210) <i>; int</i>
562 <a
563 href="#i_ret">ret</a> int 0<br>}<br></pre>
564<p>This example is made up of a <a href="#globalvars">global variable</a>
565named "<tt>.LC0</tt>", an external declaration of the "<tt>puts</tt>"
566function, and a <a href="#functionstructure">function definition</a>
567for "<tt>main</tt>".</p>
568<a name="linkage"> In general, a module is made up of a list of global
569values, where both functions and global variables are global values.
570Global values are represented by a pointer to a memory location (in
571this case, a pointer to an array of char, and a pointer to a function),
572and have one of the following linkage types:</a>
573<p> </p>
Chris Lattner27f71f22003-09-03 00:41:47 +0000574<dl>
Chris Lattner261efe92003-11-25 01:02:51 +0000575 <a name="linkage_internal"> <dt><tt><b>internal</b></tt> </dt>
576 <dd>Global values with internal linkage are only directly accessible
577by objects in the current module. In particular, linking code into a
578module with an internal global value may cause the internal to be
579renamed as necessary to avoid collisions. Because the symbol is
580internal to the module, all references can be updated. This
581corresponds to the notion of the '<tt>static</tt>' keyword in C, or the
582idea of "anonymous namespaces" in C++.
583 <p> </p>
584 </dd>
585 </a><a name="linkage_linkonce"> <dt><tt><b>linkonce</b></tt>: </dt>
586 <dd>"<tt>linkonce</tt>" linkage is similar to <tt>internal</tt>
587linkage, with the twist that linking together two modules defining the
588same <tt>linkonce</tt> globals will cause one of the globals to be
589discarded. This is typically used to implement inline functions.
590Unreferenced <tt>linkonce</tt> globals are allowed to be discarded.
591 <p> </p>
592 </dd>
593 </a><a name="linkage_weak"> <dt><tt><b>weak</b></tt>: </dt>
594 <dd>"<tt>weak</tt>" linkage is exactly the same as <tt>linkonce</tt>
595linkage, except that unreferenced <tt>weak</tt> globals may not be
596discarded. This is used to implement constructs in C such as "<tt>int
597X;</tt>" at global scope.
598 <p> </p>
599 </dd>
600 </a><a name="linkage_appending"> <dt><tt><b>appending</b></tt>: </dt>
601 <dd>"<tt>appending</tt>" linkage may only be applied to global
602variables of pointer to array type. When two global variables with
603appending linkage are linked together, the two global arrays are
604appended together. This is the LLVM, typesafe, equivalent of having
605the system linker append together "sections" with identical names when
606.o files are linked.
607 <p> </p>
608 </dd>
609 </a><a name="linkage_external"> <dt><tt><b>externally visible</b></tt>:</dt>
610 <dd>If none of the above identifiers are used, the global is
611externally visible, meaning that it participates in linkage and can be
612used to resolve external symbol references.
613 <p> </p>
614 </dd>
615 </a>
616</dl>
617<p> </p>
618<p><a name="linkage_external">For example, since the "<tt>.LC0</tt>"
619variable is defined to be internal, if another module defined a "<tt>.LC0</tt>"
620variable and was linked with this one, one of the two would be renamed,
621preventing a collision. Since "<tt>main</tt>" and "<tt>puts</tt>" are
622external (i.e., lacking any linkage declarations), they are accessible
623outside of the current module. It is illegal for a function <i>declaration</i>
624to have any linkage type other than "externally visible".</a></p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000625</div>
Chris Lattner00950542001-06-06 20:29:01 +0000626<!-- ======================================================================= -->
Chris Lattner261efe92003-11-25 01:02:51 +0000627<div class="doc_subsection"> <a name="globalvars">Global Variables</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000628<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000629<p>Global variables define regions of memory allocated at compilation
630time instead of run-time. Global variables may optionally be
631initialized. A variable may be defined as a global "constant", which
632indicates that the contents of the variable will never be modified
633(opening options for optimization). Constants must always have an
634initial value.</p>
635<p>As SSA values, global variables define pointer values that are in
636scope (i.e. they dominate) for all basic blocks in the program. Global
637variables always define a pointer to their "content" type because they
638describe a region of memory, and all memory objects in LLVM are
639accessed through pointers.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000640</div>
Chris Lattner2b7d3202002-05-06 03:03:22 +0000641<!-- ======================================================================= -->
Chris Lattner261efe92003-11-25 01:02:51 +0000642<div class="doc_subsection"> <a name="functionstructure">Functions</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000643<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000644<p>LLVM function definitions are composed of a (possibly empty)
645argument list, an opening curly brace, a list of basic blocks, and a
646closing curly brace. LLVM function declarations are defined with the "<tt>declare</tt>"
647keyword, a function name, and a function signature.</p>
648<p>A function definition contains a list of basic blocks, forming the
649CFG for the function. Each basic block may optionally start with a
650label (giving the basic block a symbol table entry), contains a list of
651instructions, and ends with a <a href="#terminators">terminator</a>
652instruction (such as a branch or function return).</p>
653<p>The first basic block in program is special in two ways: it is
654immediately executed on entrance to the function, and it is not allowed
655to have predecessor basic blocks (i.e. there can not be any branches to
656the entry block of a function). Because the block can have no
657predecessors, it also cannot have any <a href="#i_phi">PHI nodes</a>.</p>
John Criswell009900b2003-11-25 21:45:46 +0000658<p>
659LLVM functions are identified by their name and type signature. Hence, two
660functions with the same name but different parameter lists or return values
661are considered different functions, and LLVM will resolves references to each
662appropriately.
663</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000664</div>
Chris Lattner00950542001-06-06 20:29:01 +0000665<!-- *********************************************************************** -->
Chris Lattner261efe92003-11-25 01:02:51 +0000666<div class="doc_section"> <a name="instref">Instruction Reference</a> </div>
667<!-- *********************************************************************** -->
Misha Brukman9d0919f2003-11-08 01:05:38 +0000668<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000669<p>The LLVM instruction set consists of several different
670classifications of instructions: <a href="#terminators">terminator
671instructions</a>, <a href="#binaryops">binary instructions</a>, <a
672 href="#memoryops">memory instructions</a>, and <a href="#otherops">other
673instructions</a>.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000674</div>
Chris Lattner00950542001-06-06 20:29:01 +0000675<!-- ======================================================================= -->
Chris Lattner261efe92003-11-25 01:02:51 +0000676<div class="doc_subsection"> <a name="terminators">Terminator
677Instructions</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000678<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000679<p>As mentioned <a href="#functionstructure">previously</a>, every
680basic block in a program ends with a "Terminator" instruction, which
681indicates which block should be executed after the current block is
682finished. These terminator instructions typically yield a '<tt>void</tt>'
683value: they produce control flow, not values (the one exception being
684the '<a href="#i_invoke"><tt>invoke</tt></a>' instruction).</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000685<p>There are five different terminator instructions: the '<a
Chris Lattner261efe92003-11-25 01:02:51 +0000686 href="#i_ret"><tt>ret</tt></a>' instruction, the '<a href="#i_br"><tt>br</tt></a>'
687instruction, the '<a href="#i_switch"><tt>switch</tt></a>' instruction,
688the '<a href="#i_invoke"><tt>invoke</tt></a>' instruction, and the '<a
689 href="#i_unwind"><tt>unwind</tt></a>' instruction.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000690</div>
Chris Lattner00950542001-06-06 20:29:01 +0000691<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000692<div class="doc_subsubsection"> <a name="i_ret">'<tt>ret</tt>'
693Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000694<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000695<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000696<pre> ret &lt;type&gt; &lt;value&gt; <i>; Return a value from a non-void function</i>
Chris Lattner7faa8832002-04-14 06:13:44 +0000697 ret void <i>; Return from void function</i>
Chris Lattner00950542001-06-06 20:29:01 +0000698</pre>
Chris Lattner00950542001-06-06 20:29:01 +0000699<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000700<p>The '<tt>ret</tt>' instruction is used to return control flow (and a
701value) from a function, back to the caller.</p>
702<p>There are two forms of the '<tt>ret</tt>' instructruction: one that
703returns a value and then causes control flow, and one that just causes
704control flow to occur.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000705<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000706<p>The '<tt>ret</tt>' instruction may return any '<a
707 href="#t_firstclass">first class</a>' type. Notice that a function is
708not <a href="#wellformed">well formed</a> if there exists a '<tt>ret</tt>'
709instruction inside of the function that returns a value that does not
710match the return type of the function.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000711<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000712<p>When the '<tt>ret</tt>' instruction is executed, control flow
713returns back to the calling function's context. If the caller is a "<a
714 href="#i_call"><tt>call</tt></a> instruction, execution continues at
715the instruction after the call. If the caller was an "<a
716 href="#i_invoke"><tt>invoke</tt></a>" instruction, execution continues
717at the beginning "normal" of the destination block. If the instruction
718returns a value, that value shall set the call or invoke instruction's
719return value.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000720<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000721<pre> ret int 5 <i>; Return an integer value of 5</i>
Chris Lattner7faa8832002-04-14 06:13:44 +0000722 ret void <i>; Return from a void function</i>
Chris Lattner00950542001-06-06 20:29:01 +0000723</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000724</div>
Chris Lattner00950542001-06-06 20:29:01 +0000725<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000726<div class="doc_subsubsection"> <a name="i_br">'<tt>br</tt>' Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000727<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000728<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000729<pre> br bool &lt;cond&gt;, label &lt;iftrue&gt;, label &lt;iffalse&gt;<br> br label &lt;dest&gt; <i>; Unconditional branch</i>
Chris Lattner00950542001-06-06 20:29:01 +0000730</pre>
Chris Lattner00950542001-06-06 20:29:01 +0000731<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000732<p>The '<tt>br</tt>' instruction is used to cause control flow to
733transfer to a different basic block in the current function. There are
734two forms of this instruction, corresponding to a conditional branch
735and an unconditional branch.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000736<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000737<p>The conditional branch form of the '<tt>br</tt>' instruction takes a
738single '<tt>bool</tt>' value and two '<tt>label</tt>' values. The
739unconditional form of the '<tt>br</tt>' instruction takes a single '<tt>label</tt>'
740value as a target.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000741<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000742<p>Upon execution of a conditional '<tt>br</tt>' instruction, the '<tt>bool</tt>'
743argument is evaluated. If the value is <tt>true</tt>, control flows
744to the '<tt>iftrue</tt>' <tt>label</tt> argument. If "cond" is <tt>false</tt>,
745control flows to the '<tt>iffalse</tt>' <tt>label</tt> argument.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000746<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000747<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
748 href="#i_ret">ret</a> int 1<br>IfUnequal:<br> <a href="#i_ret">ret</a> int 0<br></pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000749</div>
Chris Lattner00950542001-06-06 20:29:01 +0000750<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000751<div class="doc_subsubsection"> <a name="i_switch">'<tt>switch</tt>'
752Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000753<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000754<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000755<pre> switch uint &lt;value&gt;, label &lt;defaultdest&gt; [ int &lt;val&gt;, label &amp;dest&gt;, ... ]<br></pre>
Chris Lattner00950542001-06-06 20:29:01 +0000756<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000757<p>The '<tt>switch</tt>' instruction is used to transfer control flow
758to one of several different places. It is a generalization of the '<tt>br</tt>'
Misha Brukman9d0919f2003-11-08 01:05:38 +0000759instruction, allowing a branch to occur to one of many possible
760destinations.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000761<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000762<p>The '<tt>switch</tt>' instruction uses three parameters: a '<tt>uint</tt>'
Chris Lattner261efe92003-11-25 01:02:51 +0000763comparison value '<tt>value</tt>', a default '<tt>label</tt>'
764destination, and an array of pairs of comparison value constants and '<tt>label</tt>'s.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000765<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000766<p>The <tt>switch</tt> instruction specifies a table of values and
767destinations. When the '<tt>switch</tt>' instruction is executed, this
768table is searched for the given value. If the value is found, the
769corresponding destination is branched to, otherwise the default value
770it transfered to.</p>
Chris Lattnerc29b1252003-05-08 05:08:48 +0000771<h5>Implementation:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000772<p>Depending on properties of the target machine and the particular <tt>switch</tt>
773instruction, this instruction may be code generated as a series of
774chained conditional branches, or with a lookup table.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000775<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000776<pre> <i>; Emulate a conditional br instruction</i>
777 %Val = <a
778 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>
Chris Lattner27f71f22003-09-03 00:41:47 +0000779 switch uint 0, label %dest [ ]
Chris Lattner00950542001-06-06 20:29:01 +0000780
Chris Lattner2b7d3202002-05-06 03:03:22 +0000781 <i>; Implement a jump table:</i>
Chris Lattner27f71f22003-09-03 00:41:47 +0000782 switch uint %val, label %otherwise [ int 0, label %onzero,
783 int 1, label %onone,
784 int 2, label %ontwo ]
Chris Lattner00950542001-06-06 20:29:01 +0000785</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000786</div>
Chris Lattner00950542001-06-06 20:29:01 +0000787<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000788<div class="doc_subsubsection"> <a name="i_invoke">'<tt>invoke</tt>'
789Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000790<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000791<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000792<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>
Chris Lattner6536cfe2002-05-06 22:08:29 +0000793<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000794<p>The '<tt>invoke</tt>' instruction causes control to transfer to a
795specified function, with the possibility of control flow transfer to
796either the '<tt>normal</tt>' <tt>label</tt> label or the '<tt>exception</tt>'<tt>label</tt>.
797If the callee function returns with the "<tt><a href="#i_ret">ret</a></tt>"
798instruction, control flow will return to the "normal" label. If the
799callee (or any indirect callees) returns with the "<a href="#i_unwind"><tt>unwind</tt></a>"
800instruction, control is interrupted, and continued at the dynamically
801nearest "except" label.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000802<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000803<p>This instruction requires several arguments:</p>
Chris Lattner00950542001-06-06 20:29:01 +0000804<ol>
Chris Lattner261efe92003-11-25 01:02:51 +0000805 <li>'<tt>ptr to function ty</tt>': shall be the signature of the
806pointer to function value being invoked. In most cases, this is a
807direct function invocation, but indirect <tt>invoke</tt>s are just as
808possible, branching off an arbitrary pointer to function value. </li>
809 <li>'<tt>function ptr val</tt>': An LLVM value containing a pointer
810to a function to be invoked. </li>
811 <li>'<tt>function args</tt>': argument list whose types match the
812function signature argument types. If the function signature indicates
813the function accepts a variable number of arguments, the extra
814arguments can be specified. </li>
815 <li>'<tt>normal label</tt>': the label reached when the called
816function executes a '<tt><a href="#i_ret">ret</a></tt>' instruction. </li>
817 <li>'<tt>exception label</tt>': the label reached when a callee
818returns with the <a href="#i_unwind"><tt>unwind</tt></a> instruction. </li>
Chris Lattner00950542001-06-06 20:29:01 +0000819</ol>
Chris Lattner00950542001-06-06 20:29:01 +0000820<h5>Semantics:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000821<p>This instruction is designed to operate as a standard '<tt><a
Chris Lattner261efe92003-11-25 01:02:51 +0000822 href="#i_call">call</a></tt>' instruction in most regards. The
823primary difference is that it establishes an association with a label,
824which is used by the runtime library to unwind the stack.</p>
825<p>This instruction is used in languages with destructors to ensure
826that proper cleanup is performed in the case of either a <tt>longjmp</tt>
827or a thrown exception. Additionally, this is important for
828implementation of '<tt>catch</tt>' clauses in high-level languages that
829support them.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000830<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000831<pre> %retval = invoke int %Test(int 15)<br> to label %Continue<br> except label %TestCleanup <i>; {int}:retval set</i>
Chris Lattner00950542001-06-06 20:29:01 +0000832</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000833</div>
Chris Lattner27f71f22003-09-03 00:41:47 +0000834<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000835<div class="doc_subsubsection"> <a name="i_unwind">'<tt>unwind</tt>'
836Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000837<div class="doc_text">
Chris Lattner27f71f22003-09-03 00:41:47 +0000838<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000839<pre> unwind<br></pre>
Chris Lattner27f71f22003-09-03 00:41:47 +0000840<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000841<p>The '<tt>unwind</tt>' instruction unwinds the stack, continuing
842control flow at the first callee in the dynamic call stack which used
843an <a href="#i_invoke"><tt>invoke</tt></a> instruction to perform the
844call. This is primarily used to implement exception handling.</p>
Chris Lattner27f71f22003-09-03 00:41:47 +0000845<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000846<p>The '<tt>unwind</tt>' intrinsic causes execution of the current
847function to immediately halt. The dynamic call stack is then searched
848for the first <a href="#i_invoke"><tt>invoke</tt></a> instruction on
849the call stack. Once found, execution continues at the "exceptional"
850destination block specified by the <tt>invoke</tt> instruction. If
851there is no <tt>invoke</tt> instruction in the dynamic call chain,
852undefined behavior results.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000853</div>
Chris Lattner00950542001-06-06 20:29:01 +0000854<!-- ======================================================================= -->
Chris Lattner261efe92003-11-25 01:02:51 +0000855<div class="doc_subsection"> <a name="binaryops">Binary Operations</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000856<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +0000857<p>Binary operators are used to do most of the computation in a
858program. They require two operands, execute an operation on them, and
859produce a single value. The result value of a binary operator is not
860necessarily the same type as its operands.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000861<p>There are several different binary operators:</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000862</div>
Chris Lattner00950542001-06-06 20:29:01 +0000863<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000864<div class="doc_subsubsection"> <a name="i_add">'<tt>add</tt>'
865Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000866<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000867<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000868<pre> &lt;result&gt; = add &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {ty}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +0000869</pre>
Chris Lattner00950542001-06-06 20:29:01 +0000870<h5>Overview:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000871<p>The '<tt>add</tt>' instruction returns the sum of its two operands.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000872<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000873<p>The two arguments to the '<tt>add</tt>' instruction must be either <a
Chris Lattner261efe92003-11-25 01:02:51 +0000874 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
875values. Both arguments must have identical types.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000876<h5>Semantics:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000877<p>The value produced is the integer or floating point sum of the two
878operands.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000879<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000880<pre> &lt;result&gt; = add int 4, %var <i>; yields {int}:result = 4 + %var</i>
Chris Lattner00950542001-06-06 20:29:01 +0000881</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000882</div>
Chris Lattner00950542001-06-06 20:29:01 +0000883<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000884<div class="doc_subsubsection"> <a name="i_sub">'<tt>sub</tt>'
885Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000886<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000887<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000888<pre> &lt;result&gt; = sub &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {ty}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +0000889</pre>
Chris Lattner00950542001-06-06 20:29:01 +0000890<h5>Overview:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000891<p>The '<tt>sub</tt>' instruction returns the difference of its two
892operands.</p>
Chris Lattner261efe92003-11-25 01:02:51 +0000893<p>Note that the '<tt>sub</tt>' instruction is used to represent the '<tt>neg</tt>'
894instruction present in most other intermediate representations.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000895<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000896<p>The two arguments to the '<tt>sub</tt>' instruction must be either <a
Chris Lattner261efe92003-11-25 01:02:51 +0000897 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
898values. Both arguments must have identical types.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000899<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000900<p>The value produced is the integer or floating point difference of
901the two operands.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000902<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000903<pre> &lt;result&gt; = sub int 4, %var <i>; yields {int}:result = 4 - %var</i>
Chris Lattner00950542001-06-06 20:29:01 +0000904 &lt;result&gt; = sub int 0, %val <i>; yields {int}:result = -%var</i>
905</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000906</div>
Chris Lattner00950542001-06-06 20:29:01 +0000907<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000908<div class="doc_subsubsection"> <a name="i_mul">'<tt>mul</tt>'
909Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000910<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000911<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000912<pre> &lt;result&gt; = mul &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {ty}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +0000913</pre>
Chris Lattner00950542001-06-06 20:29:01 +0000914<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000915<p>The '<tt>mul</tt>' instruction returns the product of its two
916operands.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000917<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000918<p>The two arguments to the '<tt>mul</tt>' instruction must be either <a
Chris Lattner261efe92003-11-25 01:02:51 +0000919 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
920values. Both arguments must have identical types.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000921<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000922<p>The value produced is the integer or floating point product of the
Misha Brukman9d0919f2003-11-08 01:05:38 +0000923two operands.</p>
Chris Lattner261efe92003-11-25 01:02:51 +0000924<p>There is no signed vs unsigned multiplication. The appropriate
925action is taken based on the type of the operand.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000926<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000927<pre> &lt;result&gt; = mul int 4, %var <i>; yields {int}:result = 4 * %var</i>
Chris Lattner00950542001-06-06 20:29:01 +0000928</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000929</div>
Chris Lattner00950542001-06-06 20:29:01 +0000930<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +0000931<div class="doc_subsubsection"> <a name="i_div">'<tt>div</tt>'
932Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +0000933<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +0000934<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000935<pre> &lt;result&gt; = div &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {ty}:result</i>
936</pre>
937<h5>Overview:</h5>
938<p>The '<tt>div</tt>' instruction returns the quotient of its two
939operands.</p>
940<h5>Arguments:</h5>
941<p>The two arguments to the '<tt>div</tt>' instruction must be either <a
942 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
943values. Both arguments must have identical types.</p>
944<h5>Semantics:</h5>
945<p>The value produced is the integer or floating point quotient of the
946two operands.</p>
947<h5>Example:</h5>
948<pre> &lt;result&gt; = div int 4, %var <i>; yields {int}:result = 4 / %var</i>
949</pre>
950</div>
951<!-- _______________________________________________________________________ -->
952<div class="doc_subsubsection"> <a name="i_rem">'<tt>rem</tt>'
953Instruction</a> </div>
954<div class="doc_text">
955<h5>Syntax:</h5>
956<pre> &lt;result&gt; = rem &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {ty}:result</i>
957</pre>
958<h5>Overview:</h5>
959<p>The '<tt>rem</tt>' instruction returns the remainder from the
960division of its two operands.</p>
961<h5>Arguments:</h5>
962<p>The two arguments to the '<tt>rem</tt>' instruction must be either <a
963 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
964values. Both arguments must have identical types.</p>
965<h5>Semantics:</h5>
966<p>This returns the <i>remainder</i> of a division (where the result
967has the same sign as the divisor), not the <i>modulus</i> (where the
968result has the same sign as the dividend) of a value. For more
969information about the difference, see: <a
970 href="http://mathforum.org/dr.math/problems/anne.4.28.99.html">The
971Math Forum</a>.</p>
972<h5>Example:</h5>
973<pre> &lt;result&gt; = rem int 4, %var <i>; yields {int}:result = 4 % %var</i>
974</pre>
975</div>
976<!-- _______________________________________________________________________ -->
977<div class="doc_subsubsection"> <a name="i_setcc">'<tt>set<i>cc</i></tt>'
978Instructions</a> </div>
979<div class="doc_text">
980<h5>Syntax:</h5>
981<pre> &lt;result&gt; = seteq &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {bool}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +0000982 &lt;result&gt; = setne &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {bool}:result</i>
983 &lt;result&gt; = setlt &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {bool}:result</i>
984 &lt;result&gt; = setgt &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {bool}:result</i>
985 &lt;result&gt; = setle &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {bool}:result</i>
986 &lt;result&gt; = setge &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {bool}:result</i>
987</pre>
Chris Lattner261efe92003-11-25 01:02:51 +0000988<h5>Overview:</h5>
989<p>The '<tt>set<i>cc</i></tt>' family of instructions returns a boolean
990value based on a comparison of their two operands.</p>
991<h5>Arguments:</h5>
992<p>The two arguments to the '<tt>set<i>cc</i></tt>' instructions must
993be of <a href="#t_firstclass">first class</a> type (it is not possible
994to compare '<tt>label</tt>'s, '<tt>array</tt>'s, '<tt>structure</tt>'
995or '<tt>void</tt>' values, etc...). Both arguments must have identical
996types.</p>
Chris Lattner00950542001-06-06 20:29:01 +0000997<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +0000998<p>The '<tt>seteq</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
999value if both operands are equal.<br>
1000The '<tt>setne</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1001value if both operands are unequal.<br>
1002The '<tt>setlt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1003value if the first operand is less than the second operand.<br>
1004The '<tt>setgt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1005value if the first operand is greater than the second operand.<br>
1006The '<tt>setle</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1007value if the first operand is less than or equal to the second operand.<br>
1008The '<tt>setge</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
1009value if the first operand is greater than or equal to the second
1010operand.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001011<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001012<pre> &lt;result&gt; = seteq int 4, 5 <i>; yields {bool}:result = false</i>
Chris Lattner00950542001-06-06 20:29:01 +00001013 &lt;result&gt; = setne float 4, 5 <i>; yields {bool}:result = true</i>
1014 &lt;result&gt; = setlt uint 4, 5 <i>; yields {bool}:result = true</i>
1015 &lt;result&gt; = setgt sbyte 4, 5 <i>; yields {bool}:result = false</i>
1016 &lt;result&gt; = setle sbyte 4, 5 <i>; yields {bool}:result = true</i>
1017 &lt;result&gt; = setge sbyte 4, 5 <i>; yields {bool}:result = false</i>
1018</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001019</div>
Chris Lattner00950542001-06-06 20:29:01 +00001020<!-- ======================================================================= -->
Chris Lattner261efe92003-11-25 01:02:51 +00001021<div class="doc_subsection"> <a name="bitwiseops">Bitwise Binary
1022Operations</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001023<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +00001024<p>Bitwise binary operators are used to do various forms of
1025bit-twiddling in a program. They are generally very efficient
1026instructions, and can commonly be strength reduced from other
1027instructions. They require two operands, execute an operation on them,
1028and produce a single value. The resulting value of the bitwise binary
1029operators is always the same type as its first operand.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001030</div>
Chris Lattner00950542001-06-06 20:29:01 +00001031<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001032<div class="doc_subsubsection"> <a name="i_and">'<tt>and</tt>'
1033Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001034<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +00001035<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001036<pre> &lt;result&gt; = and &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {ty}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +00001037</pre>
Chris Lattner00950542001-06-06 20:29:01 +00001038<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001039<p>The '<tt>and</tt>' instruction returns the bitwise logical and of
1040its two operands.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001041<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001042<p>The two arguments to the '<tt>and</tt>' instruction must be <a
Chris Lattner261efe92003-11-25 01:02:51 +00001043 href="#t_integral">integral</a> values. Both arguments must have
1044identical types.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001045<h5>Semantics:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001046<p>The truth table used for the '<tt>and</tt>' instruction is:</p>
Chris Lattner261efe92003-11-25 01:02:51 +00001047<p> </p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001048<center>
1049<table border="1" cellspacing="0" cellpadding="4">
Chris Lattner261efe92003-11-25 01:02:51 +00001050 <tbody>
1051 <tr>
1052 <td>In0</td>
1053 <td>In1</td>
1054 <td>Out</td>
1055 </tr>
1056 <tr>
1057 <td>0</td>
1058 <td>0</td>
1059 <td>0</td>
1060 </tr>
1061 <tr>
1062 <td>0</td>
1063 <td>1</td>
1064 <td>0</td>
1065 </tr>
1066 <tr>
1067 <td>1</td>
1068 <td>0</td>
1069 <td>0</td>
1070 </tr>
1071 <tr>
1072 <td>1</td>
1073 <td>1</td>
1074 <td>1</td>
1075 </tr>
1076 </tbody>
1077</table>
1078</center>
Chris Lattner00950542001-06-06 20:29:01 +00001079<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001080<pre> &lt;result&gt; = and int 4, %var <i>; yields {int}:result = 4 &amp; %var</i>
Chris Lattner00950542001-06-06 20:29:01 +00001081 &lt;result&gt; = and int 15, 40 <i>; yields {int}:result = 8</i>
1082 &lt;result&gt; = and int 4, 8 <i>; yields {int}:result = 0</i>
1083</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001084</div>
Chris Lattner00950542001-06-06 20:29:01 +00001085<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001086<div class="doc_subsubsection"> <a name="i_or">'<tt>or</tt>' Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001087<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +00001088<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001089<pre> &lt;result&gt; = or &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {ty}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +00001090</pre>
Chris Lattner261efe92003-11-25 01:02:51 +00001091<h5>Overview:</h5>
1092<p>The '<tt>or</tt>' instruction returns the bitwise logical inclusive
1093or of its two operands.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001094<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001095<p>The two arguments to the '<tt>or</tt>' instruction must be <a
Chris Lattner261efe92003-11-25 01:02:51 +00001096 href="#t_integral">integral</a> values. Both arguments must have
1097identical types.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001098<h5>Semantics:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001099<p>The truth table used for the '<tt>or</tt>' instruction is:</p>
Chris Lattner261efe92003-11-25 01:02:51 +00001100<p> </p>
1101<center>
1102<table border="1" cellspacing="0" cellpadding="4">
1103 <tbody>
1104 <tr>
1105 <td>In0</td>
1106 <td>In1</td>
1107 <td>Out</td>
1108 </tr>
1109 <tr>
1110 <td>0</td>
1111 <td>0</td>
1112 <td>0</td>
1113 </tr>
1114 <tr>
1115 <td>0</td>
1116 <td>1</td>
1117 <td>1</td>
1118 </tr>
1119 <tr>
1120 <td>1</td>
1121 <td>0</td>
1122 <td>1</td>
1123 </tr>
1124 <tr>
1125 <td>1</td>
1126 <td>1</td>
1127 <td>1</td>
1128 </tr>
1129 </tbody>
1130</table>
1131</center>
Chris Lattner00950542001-06-06 20:29:01 +00001132<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001133<pre> &lt;result&gt; = or int 4, %var <i>; yields {int}:result = 4 | %var</i>
Chris Lattner00950542001-06-06 20:29:01 +00001134 &lt;result&gt; = or int 15, 40 <i>; yields {int}:result = 47</i>
1135 &lt;result&gt; = or int 4, 8 <i>; yields {int}:result = 12</i>
1136</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001137</div>
Chris Lattner00950542001-06-06 20:29:01 +00001138<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001139<div class="doc_subsubsection"> <a name="i_xor">'<tt>xor</tt>'
1140Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001141<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +00001142<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001143<pre> &lt;result&gt; = xor &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt; <i>; yields {ty}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +00001144</pre>
Chris Lattner00950542001-06-06 20:29:01 +00001145<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001146<p>The '<tt>xor</tt>' instruction returns the bitwise logical exclusive
1147or of its two operands. The <tt>xor</tt> is used to implement the
1148"one's complement" operation, which is the "~" operator in C.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001149<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001150<p>The two arguments to the '<tt>xor</tt>' instruction must be <a
Chris Lattner261efe92003-11-25 01:02:51 +00001151 href="#t_integral">integral</a> values. Both arguments must have
1152identical types.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001153<h5>Semantics:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001154<p>The truth table used for the '<tt>xor</tt>' instruction is:</p>
Chris Lattner261efe92003-11-25 01:02:51 +00001155<p> </p>
1156<center>
1157<table border="1" cellspacing="0" cellpadding="4">
1158 <tbody>
1159 <tr>
1160 <td>In0</td>
1161 <td>In1</td>
1162 <td>Out</td>
1163 </tr>
1164 <tr>
1165 <td>0</td>
1166 <td>0</td>
1167 <td>0</td>
1168 </tr>
1169 <tr>
1170 <td>0</td>
1171 <td>1</td>
1172 <td>1</td>
1173 </tr>
1174 <tr>
1175 <td>1</td>
1176 <td>0</td>
1177 <td>1</td>
1178 </tr>
1179 <tr>
1180 <td>1</td>
1181 <td>1</td>
1182 <td>0</td>
1183 </tr>
1184 </tbody>
1185</table>
1186</center>
1187<p> </p>
Chris Lattner00950542001-06-06 20:29:01 +00001188<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001189<pre> &lt;result&gt; = xor int 4, %var <i>; yields {int}:result = 4 ^ %var</i>
Chris Lattner00950542001-06-06 20:29:01 +00001190 &lt;result&gt; = xor int 15, 40 <i>; yields {int}:result = 39</i>
1191 &lt;result&gt; = xor int 4, 8 <i>; yields {int}:result = 12</i>
Chris Lattner27f71f22003-09-03 00:41:47 +00001192 &lt;result&gt; = xor int %V, -1 <i>; yields {int}:result = ~%V</i>
Chris Lattner00950542001-06-06 20:29:01 +00001193</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001194</div>
Chris Lattner00950542001-06-06 20:29:01 +00001195<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001196<div class="doc_subsubsection"> <a name="i_shl">'<tt>shl</tt>'
1197Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001198<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +00001199<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001200<pre> &lt;result&gt; = shl &lt;ty&gt; &lt;var1&gt;, ubyte &lt;var2&gt; <i>; yields {ty}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +00001201</pre>
Chris Lattner00950542001-06-06 20:29:01 +00001202<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001203<p>The '<tt>shl</tt>' instruction returns the first operand shifted to
1204the left a specified number of bits.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001205<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001206<p>The first argument to the '<tt>shl</tt>' instruction must be an <a
Chris Lattner261efe92003-11-25 01:02:51 +00001207 href="#t_integer">integer</a> type. The second argument must be an '<tt>ubyte</tt>'
1208type.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001209<h5>Semantics:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001210<p>The value produced is <tt>var1</tt> * 2<sup><tt>var2</tt></sup>.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001211<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001212<pre> &lt;result&gt; = shl int 4, ubyte %var <i>; yields {int}:result = 4 &lt;&lt; %var</i>
Chris Lattner00950542001-06-06 20:29:01 +00001213 &lt;result&gt; = shl int 4, ubyte 2 <i>; yields {int}:result = 16</i>
1214 &lt;result&gt; = shl int 1, ubyte 10 <i>; yields {int}:result = 1024</i>
1215</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001216</div>
Chris Lattner00950542001-06-06 20:29:01 +00001217<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001218<div class="doc_subsubsection"> <a name="i_shr">'<tt>shr</tt>'
1219Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001220<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +00001221<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001222<pre> &lt;result&gt; = shr &lt;ty&gt; &lt;var1&gt;, ubyte &lt;var2&gt; <i>; yields {ty}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +00001223</pre>
Chris Lattner00950542001-06-06 20:29:01 +00001224<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001225<p>The '<tt>shr</tt>' instruction returns the first operand shifted to
1226the right a specified number of bits.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001227<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001228<p>The first argument to the '<tt>shr</tt>' instruction must be an <a
Chris Lattner261efe92003-11-25 01:02:51 +00001229 href="#t_integer">integer</a> type. The second argument must be an '<tt>ubyte</tt>'
1230type.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001231<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001232<p>If the first argument is a <a href="#t_signed">signed</a> type, the
1233most significant bit is duplicated in the newly free'd bit positions.
1234If the first argument is unsigned, zero bits shall fill the empty
1235positions.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001236<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001237<pre> &lt;result&gt; = shr int 4, ubyte %var <i>; yields {int}:result = 4 &gt;&gt; %var</i>
Chris Lattner8c6bb902003-06-18 21:30:51 +00001238 &lt;result&gt; = shr uint 4, ubyte 1 <i>; yields {uint}:result = 2</i>
Chris Lattner00950542001-06-06 20:29:01 +00001239 &lt;result&gt; = shr int 4, ubyte 2 <i>; yields {int}:result = 1</i>
Chris Lattner8c6bb902003-06-18 21:30:51 +00001240 &lt;result&gt; = shr sbyte 4, ubyte 3 <i>; yields {sbyte}:result = 0</i>
1241 &lt;result&gt; = shr sbyte -2, ubyte 1 <i>; yields {sbyte}:result = -1</i>
Chris Lattner00950542001-06-06 20:29:01 +00001242</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001243</div>
Chris Lattner00950542001-06-06 20:29:01 +00001244<!-- ======================================================================= -->
Chris Lattner261efe92003-11-25 01:02:51 +00001245<div class="doc_subsection"> <a name="memoryops">Memory Access
1246Operations</a></div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001247<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +00001248<p>A key design point of an SSA-based representation is how it
1249represents memory. In LLVM, no memory locations are in SSA form, which
1250makes things very simple. This section describes how to read, write,
1251allocate and free memory in LLVM.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001252</div>
Chris Lattner00950542001-06-06 20:29:01 +00001253<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001254<div class="doc_subsubsection"> <a name="i_malloc">'<tt>malloc</tt>'
1255Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001256<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +00001257<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001258<pre> &lt;result&gt; = malloc &lt;type&gt;, uint &lt;NumElements&gt; <i>; yields {type*}:result</i>
Chris Lattner7faa8832002-04-14 06:13:44 +00001259 &lt;result&gt; = malloc &lt;type&gt; <i>; yields {type*}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +00001260</pre>
Chris Lattner00950542001-06-06 20:29:01 +00001261<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001262<p>The '<tt>malloc</tt>' instruction allocates memory from the system
1263heap and returns a pointer to it.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001264<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001265<p>The the '<tt>malloc</tt>' instruction allocates <tt>sizeof(&lt;type&gt;)*NumElements</tt>
1266bytes of memory from the operating system, and returns a pointer of the
1267appropriate type to the program. The second form of the instruction is
1268a shorter version of the first instruction that defaults to allocating
1269one element.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001270<p>'<tt>type</tt>' must be a sized type.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001271<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001272<p>Memory is allocated using the system "<tt>malloc</tt>" function, and
1273a pointer is returned.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001274<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001275<pre> %array = malloc [4 x ubyte ] <i>; yields {[%4 x ubyte]*}:array</i>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001276
Chris Lattner261efe92003-11-25 01:02:51 +00001277 %size = <a
1278 href="#i_add">add</a> uint 2, 2 <i>; yields {uint}:size = uint 4</i>
Chris Lattner7faa8832002-04-14 06:13:44 +00001279 %array1 = malloc ubyte, uint 4 <i>; yields {ubyte*}:array1</i>
1280 %array2 = malloc [12 x ubyte], uint %size <i>; yields {[12 x ubyte]*}:array2</i>
Chris Lattner00950542001-06-06 20:29:01 +00001281</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001282</div>
Chris Lattner00950542001-06-06 20:29:01 +00001283<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001284<div class="doc_subsubsection"> <a name="i_free">'<tt>free</tt>'
1285Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001286<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +00001287<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001288<pre> free &lt;type&gt; &lt;value&gt; <i>; yields {void}</i>
Chris Lattner00950542001-06-06 20:29:01 +00001289</pre>
Chris Lattner00950542001-06-06 20:29:01 +00001290<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001291<p>The '<tt>free</tt>' instruction returns memory back to the unused
1292memory heap, to be reallocated in the future.</p>
1293<p> </p>
Chris Lattner00950542001-06-06 20:29:01 +00001294<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001295<p>'<tt>value</tt>' shall be a pointer value that points to a value
1296that was allocated with the '<tt><a href="#i_malloc">malloc</a></tt>'
1297instruction.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001298<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001299<p>Access to the memory pointed to by the pointer is not longer defined
1300after this instruction executes.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001301<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001302<pre> %array = <a href="#i_malloc">malloc</a> [4 x ubyte] <i>; yields {[4 x ubyte]*}:array</i>
Chris Lattner00950542001-06-06 20:29:01 +00001303 free [4 x ubyte]* %array
1304</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001305</div>
Chris Lattner00950542001-06-06 20:29:01 +00001306<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001307<div class="doc_subsubsection"> <a name="i_alloca">'<tt>alloca</tt>'
1308Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001309<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +00001310<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001311<pre> &lt;result&gt; = alloca &lt;type&gt;, uint &lt;NumElements&gt; <i>; yields {type*}:result</i>
Chris Lattner7faa8832002-04-14 06:13:44 +00001312 &lt;result&gt; = alloca &lt;type&gt; <i>; yields {type*}:result</i>
Chris Lattner00950542001-06-06 20:29:01 +00001313</pre>
Chris Lattner00950542001-06-06 20:29:01 +00001314<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001315<p>The '<tt>alloca</tt>' instruction allocates memory on the current
1316stack frame of the procedure that is live until the current function
1317returns to its caller.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001318<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001319<p>The the '<tt>alloca</tt>' instruction allocates <tt>sizeof(&lt;type&gt;)*NumElements</tt>
1320bytes of memory on the runtime stack, returning a pointer of the
1321appropriate type to the program. The second form of the instruction is
1322a shorter version of the first that defaults to allocating one element.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001323<p>'<tt>type</tt>' may be any sized type.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001324<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001325<p>Memory is allocated, a pointer is returned. '<tt>alloca</tt>'d
1326memory is automatically released when the function returns. The '<tt>alloca</tt>'
1327instruction is commonly used to represent automatic variables that must
1328have an address available. When the function returns (either with the <tt><a
1329 href="#i_ret">ret</a></tt> or <tt><a href="#i_invoke">invoke</a></tt>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001330instructions), the memory is reclaimed.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001331<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001332<pre> %ptr = alloca int <i>; yields {int*}:ptr</i>
Chris Lattner7faa8832002-04-14 06:13:44 +00001333 %ptr = alloca int, uint 4 <i>; yields {int*}:ptr</i>
Chris Lattner00950542001-06-06 20:29:01 +00001334</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001335</div>
Chris Lattner00950542001-06-06 20:29:01 +00001336<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001337<div class="doc_subsubsection"> <a name="i_load">'<tt>load</tt>'
1338Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001339<div class="doc_text">
Chris Lattner2b7d3202002-05-06 03:03:22 +00001340<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001341<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>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001342<h5>Overview:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001343<p>The '<tt>load</tt>' instruction is used to read from memory.</p>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001344<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001345<p>The argument to the '<tt>load</tt>' instruction specifies the memory
1346address to load from. The pointer must point to a <a
1347 href="t_firstclass">first class</a> type. If the <tt>load</tt> is
1348marked as <tt>volatile</tt> then the optimizer is not allowed to modify
1349the number or order of execution of this <tt>load</tt> with other
1350volatile <tt>load</tt> and <tt><a href="#i_store">store</a></tt>
1351instructions. </p>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001352<h5>Semantics:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001353<p>The location of memory pointed to is loaded.</p>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001354<h5>Examples:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001355<pre> %ptr = <a href="#i_alloca">alloca</a> int <i>; yields {int*}:ptr</i>
1356 <a
1357 href="#i_store">store</a> int 3, int* %ptr <i>; yields {void}</i>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001358 %val = load int* %ptr <i>; yields {int}:val = int 3</i>
1359</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001360</div>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001361<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001362<div class="doc_subsubsection"> <a name="i_store">'<tt>store</tt>'
1363Instruction</a> </div>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001364<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001365<pre> store &lt;ty&gt; &lt;value&gt;, &lt;ty&gt;* &lt;pointer&gt; <i>; yields {void}</i>
Chris Lattnerf0651072003-09-08 18:27:49 +00001366 volatile store &lt;ty&gt; &lt;value&gt;, &lt;ty&gt;* &lt;pointer&gt; <i>; yields {void}</i>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001367</pre>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001368<h5>Overview:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001369<p>The '<tt>store</tt>' instruction is used to write to memory.</p>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001370<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001371<p>There are two arguments to the '<tt>store</tt>' instruction: a value
1372to store and an address to store it into. The type of the '<tt>&lt;pointer&gt;</tt>'
1373operand must be a pointer to the type of the '<tt>&lt;value&gt;</tt>'
1374operand. If the <tt>store</tt> is marked as <tt>volatile</tt> then the
1375optimizer is not allowed to modify the number or order of execution of
1376this <tt>store</tt> with other volatile <tt>load</tt> and <tt><a
1377 href="#i_store">store</a></tt> instructions.</p>
1378<h5>Semantics:</h5>
1379<p>The contents of memory are updated to contain '<tt>&lt;value&gt;</tt>'
1380at the location specified by the '<tt>&lt;pointer&gt;</tt>' operand.</p>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001381<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001382<pre> %ptr = <a href="#i_alloca">alloca</a> int <i>; yields {int*}:ptr</i>
1383 <a
1384 href="#i_store">store</a> int 3, int* %ptr <i>; yields {void}</i>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001385 %val = load int* %ptr <i>; yields {int}:val = int 3</i>
1386</pre>
Chris Lattner2b7d3202002-05-06 03:03:22 +00001387<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001388<div class="doc_subsubsection"> <a name="i_getelementptr">'<tt>getelementptr</tt>'
1389Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001390<div class="doc_text">
Chris Lattner7faa8832002-04-14 06:13:44 +00001391<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001392<pre> &lt;result&gt; = getelementptr &lt;ty&gt;* &lt;ptrval&gt;{, long &lt;aidx&gt;|, ubyte &lt;sidx&gt;}*<br></pre>
Chris Lattner7faa8832002-04-14 06:13:44 +00001393<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001394<p>The '<tt>getelementptr</tt>' instruction is used to get the address
1395of a subelement of an aggregate data structure.</p>
Chris Lattner7faa8832002-04-14 06:13:44 +00001396<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001397<p>This instruction takes a list of <tt>long</tt> values and <tt>ubyte</tt>
Chris Lattner261efe92003-11-25 01:02:51 +00001398constants that indicate what form of addressing to perform. The actual
1399types of the arguments provided depend on the type of the first pointer
1400argument. The '<tt>getelementptr</tt>' instruction is used to index
1401down through the type levels of a structure.</p>
1402<p>For example, let's consider a C code fragment and how it gets
1403compiled to LLVM:</p>
1404<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>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001405<p>The LLVM code generated by the GCC frontend is:</p>
Chris Lattner261efe92003-11-25 01:02:51 +00001406<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>
Chris Lattner7faa8832002-04-14 06:13:44 +00001407<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001408<p>The index types specified for the '<tt>getelementptr</tt>'
1409instruction depend on the pointer type that is being index into. <a
1410 href="t_pointer">Pointer</a> and <a href="t_array">array</a> types
1411require '<tt>long</tt>' values, and <a href="t_struct">structure</a>
1412types require '<tt>ubyte</tt>' <b>constants</b>.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001413<p>In the example above, the first index is indexing into the '<tt>%ST*</tt>'
Chris Lattner261efe92003-11-25 01:02:51 +00001414type, which is a pointer, yielding a '<tt>%ST</tt>' = '<tt>{ int,
1415double, %RT }</tt>' type, a structure. The second index indexes into
1416the third element of the structure, yielding a '<tt>%RT</tt>' = '<tt>{
1417sbyte, [10 x [20 x int]], sbyte }</tt>' type, another structure. The
1418third index indexes into the second element of the structure, yielding
1419a '<tt>[10 x [20 x int]]</tt>' type, an array. The two dimensions of
1420the array are subscripted into, yielding an '<tt>int</tt>' type. The '<tt>getelementptr</tt>'
1421instruction return a pointer to this element, thus yielding a '<tt>int*</tt>'
1422type.</p>
1423<p>Note that it is perfectly legal to index partially through a
1424structure, returning a pointer to an inner element. Because of this,
1425the LLVM code for the given testcase is equivalent to:</p>
1426<pre>int* "foo"(%ST* %s) {<br> %t1 = getelementptr %ST* %s , long 1 <i>; yields %ST*:%t1</i>
Chris Lattner3dfa10b2002-12-13 06:01:21 +00001427 %t2 = getelementptr %ST* %t1, long 0, ubyte 2 <i>; yields %RT*:%t2</i>
1428 %t3 = getelementptr %RT* %t2, long 0, ubyte 1 <i>; yields [10 x [20 x int]]*:%t3</i>
1429 %t4 = getelementptr [10 x [20 x int]]* %t3, long 0, long 5 <i>; yields [20 x int]*:%t4</i>
1430 %t5 = getelementptr [20 x int]* %t4, long 0, long 13 <i>; yields int*:%t5</i>
Chris Lattner6536cfe2002-05-06 22:08:29 +00001431 ret int* %t5
1432}
1433</pre>
Chris Lattner7faa8832002-04-14 06:13:44 +00001434<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001435<pre> <i>; yields [12 x ubyte]*:aptr</i>
1436 %aptr = getelementptr {int, [12 x ubyte]}* %sptr, long 0, ubyte 1<br></pre>
1437<h5>&nbsp;Note To The Novice:</h5>
1438When using indexing into global arrays with the '<tt>getelementptr</tt>'
1439instruction, you must remember that the&nbsp; </div>
Chris Lattner00950542001-06-06 20:29:01 +00001440<!-- ======================================================================= -->
Chris Lattner261efe92003-11-25 01:02:51 +00001441<div class="doc_subsection"> <a name="otherops">Other Operations</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001442<div class="doc_text">
Chris Lattner261efe92003-11-25 01:02:51 +00001443<p>The instructions in this catagory are the "miscellaneous"
1444instructions, which defy better classification.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001445</div>
Chris Lattner00950542001-06-06 20:29:01 +00001446<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001447<div class="doc_subsubsection"> <a name="i_phi">'<tt>phi</tt>'
1448Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001449<div class="doc_text">
Chris Lattner33ba0d92001-07-09 00:26:23 +00001450<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001451<pre> &lt;result&gt; = phi &lt;ty&gt; [ &lt;val0&gt;, &lt;label0&gt;], ...<br></pre>
Chris Lattner33ba0d92001-07-09 00:26:23 +00001452<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001453<p>The '<tt>phi</tt>' instruction is used to implement the &#966; node in
1454the SSA graph representing the function.</p>
Chris Lattner33ba0d92001-07-09 00:26:23 +00001455<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001456<p>The type of the incoming values are specified with the first type
1457field. After this, the '<tt>phi</tt>' instruction takes a list of pairs
1458as arguments, with one pair for each predecessor basic block of the
1459current block. Only values of <a href="#t_firstclass">first class</a>
1460type may be used as the value arguments to the PHI node. Only labels
1461may be used as the label arguments.</p>
1462<p>There must be no non-phi instructions between the start of a basic
1463block and the PHI instructions: i.e. PHI instructions must be first in
1464a basic block.</p>
Chris Lattner33ba0d92001-07-09 00:26:23 +00001465<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001466<p>At runtime, the '<tt>phi</tt>' instruction logically takes on the
1467value specified by the parameter, depending on which basic block we
1468came from in the last <a href="#terminators">terminator</a> instruction.</p>
Chris Lattner6536cfe2002-05-06 22:08:29 +00001469<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001470<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>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001471</div>
Chris Lattner6536cfe2002-05-06 22:08:29 +00001472<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001473<div class="doc_subsubsection"> <a name="i_cast">'<tt>cast .. to</tt>'
1474Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001475<div class="doc_text">
Chris Lattner6536cfe2002-05-06 22:08:29 +00001476<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001477<pre> &lt;result&gt; = cast &lt;ty&gt; &lt;value&gt; to &lt;ty2&gt; <i>; yields ty2</i>
Chris Lattner6536cfe2002-05-06 22:08:29 +00001478</pre>
Chris Lattner6536cfe2002-05-06 22:08:29 +00001479<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001480<p>The '<tt>cast</tt>' instruction is used as the primitive means to
1481convert integers to floating point, change data type sizes, and break
1482type safety (by casting pointers).</p>
Chris Lattner6536cfe2002-05-06 22:08:29 +00001483<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001484<p>The '<tt>cast</tt>' instruction takes a value to cast, which must be
1485a first class value, and a type to cast it to, which must also be a <a
1486 href="#t_firstclass">first class</a> type.</p>
Chris Lattner6536cfe2002-05-06 22:08:29 +00001487<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001488<p>This instruction follows the C rules for explicit casts when
1489determining how the data being cast must change to fit in its new
1490container.</p>
1491<p>When casting to bool, any value that would be considered true in the
1492context of a C '<tt>if</tt>' condition is converted to the boolean '<tt>true</tt>'
Misha Brukman9d0919f2003-11-08 01:05:38 +00001493values, all else are '<tt>false</tt>'.</p>
Chris Lattner261efe92003-11-25 01:02:51 +00001494<p>When extending an integral value from a type of one signness to
1495another (for example '<tt>sbyte</tt>' to '<tt>ulong</tt>'), the value
1496is sign-extended if the <b>source</b> value is signed, and
1497zero-extended if the source value is unsigned. <tt>bool</tt> values
1498are always zero extended into either zero or one.</p>
Chris Lattner33ba0d92001-07-09 00:26:23 +00001499<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001500<pre> %X = cast int 257 to ubyte <i>; yields ubyte:1</i>
Chris Lattner7bae3952002-06-25 18:03:17 +00001501 %Y = cast int 123 to bool <i>; yields bool:true</i>
Chris Lattner33ba0d92001-07-09 00:26:23 +00001502</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001503</div>
Chris Lattner33ba0d92001-07-09 00:26:23 +00001504<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001505<div class="doc_subsubsection"> <a name="i_call">'<tt>call</tt>'
1506Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001507<div class="doc_text">
Chris Lattner00950542001-06-06 20:29:01 +00001508<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001509<pre> &lt;result&gt; = call &lt;ty&gt;* &lt;fnptrval&gt;(&lt;param list&gt;)<br></pre>
Chris Lattner00950542001-06-06 20:29:01 +00001510<h5>Overview:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001511<p>The '<tt>call</tt>' instruction represents a simple function call.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001512<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001513<p>This instruction requires several arguments:</p>
Chris Lattner6536cfe2002-05-06 22:08:29 +00001514<ol>
Chris Lattner261efe92003-11-25 01:02:51 +00001515 <li>
1516 <p>'<tt>ty</tt>': shall be the signature of the pointer to function
1517value being invoked. The argument types must match the types implied
1518by this signature.</p>
1519 </li>
1520 <li>
1521 <p>'<tt>fnptrval</tt>': An LLVM value containing a pointer to a
1522function to be invoked. In most cases, this is a direct function
1523invocation, but indirect <tt>call</tt>s are just as possible,
1524calling an arbitrary pointer to function values.</p>
1525 </li>
1526 <li>
1527 <p>'<tt>function args</tt>': argument list whose types match the
1528function signature argument types. If the function signature
1529indicates the function accepts a variable number of arguments, the
1530extra arguments can be specified.</p>
1531 </li>
Chris Lattner6536cfe2002-05-06 22:08:29 +00001532</ol>
Chris Lattner00950542001-06-06 20:29:01 +00001533<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001534<p>The '<tt>call</tt>' instruction is used to cause control flow to
1535transfer to a specified function, with its incoming arguments bound to
1536the specified values. Upon a '<tt><a href="#i_ret">ret</a></tt>'
1537instruction in the called function, control flow continues with the
1538instruction after the function call, and the return value of the
1539function is bound to the result argument. This is a simpler case of
1540the <a href="#i_invoke">invoke</a> instruction.</p>
Chris Lattner00950542001-06-06 20:29:01 +00001541<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001542<pre> %retval = call int %test(int %argc)<br> call int(sbyte*, ...) *%printf(sbyte* %msg, int 12, sbyte 42);<br></pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001543</div>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001544<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001545<div class="doc_subsubsection"> <a name="i_vanext">'<tt>vanext</tt>'
1546Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001547<div class="doc_text">
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001548<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001549<pre> &lt;resultarglist&gt; = vanext &lt;va_list&gt; &lt;arglist&gt;, &lt;argty&gt;<br></pre>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001550<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001551<p>The '<tt>vanext</tt>' instruction is used to access arguments passed
1552through the "variable argument" area of a function call. It is used to
1553implement the <tt>va_arg</tt> macro in C.</p>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001554<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001555<p>This instruction takes a <tt>valist</tt> value and the type of the
1556argument. It returns another <tt>valist</tt>.</p>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001557<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001558<p>The '<tt>vanext</tt>' instruction advances the specified <tt>valist</tt>
1559past an argument of the specified type. In conjunction with the <a
1560 href="#i_vaarg"><tt>vaarg</tt></a> instruction, it is used to implement
1561the <tt>va_arg</tt> macro available in C. For more information, see
1562the variable argument handling <a href="#int_varargs">Intrinsic
1563Functions</a>.</p>
1564<p>It is legal for this instruction to be called in a function which
1565does not take a variable number of arguments, for example, the <tt>vfprintf</tt>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001566function.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001567<p><tt>vanext</tt> is an LLVM instruction instead of an <a
Chris Lattner261efe92003-11-25 01:02:51 +00001568 href="#intrinsics">intrinsic function</a> because it takes an type as
1569an argument.</p>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001570<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001571<p>See the <a href="#int_varargs">variable argument processing</a>
1572section.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001573</div>
Chris Lattner8d1a81d2003-10-18 05:51:36 +00001574<!-- _______________________________________________________________________ -->
Chris Lattner261efe92003-11-25 01:02:51 +00001575<div class="doc_subsubsection"> <a name="i_vaarg">'<tt>vaarg</tt>'
1576Instruction</a> </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001577<div class="doc_text">
Chris Lattner8d1a81d2003-10-18 05:51:36 +00001578<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001579<pre> &lt;resultval&gt; = vaarg &lt;va_list&gt; &lt;arglist&gt;, &lt;argty&gt;<br></pre>
Chris Lattner8d1a81d2003-10-18 05:51:36 +00001580<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001581<p>The '<tt>vaarg</tt>' instruction is used to access arguments passed
1582through the "variable argument" area of a function call. It is used to
1583implement the <tt>va_arg</tt> macro in C.</p>
Chris Lattner8d1a81d2003-10-18 05:51:36 +00001584<h5>Arguments:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001585<p>This instruction takes a <tt>valist</tt> value and the type of the
1586argument. It returns a value of the specified argument type.</p>
Chris Lattner8d1a81d2003-10-18 05:51:36 +00001587<h5>Semantics:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001588<p>The '<tt>vaarg</tt>' instruction loads an argument of the specified
1589type from the specified <tt>va_list</tt>. In conjunction with the <a
1590 href="#i_vanext"><tt>vanext</tt></a> instruction, it is used to
1591implement the <tt>va_arg</tt> macro available in C. For more
1592information, see the variable argument handling <a href="#int_varargs">Intrinsic
1593Functions</a>.</p>
1594<p>It is legal for this instruction to be called in a function which
1595does not take a variable number of arguments, for example, the <tt>vfprintf</tt>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001596function.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001597<p><tt>vaarg</tt> is an LLVM instruction instead of an <a
Chris Lattner261efe92003-11-25 01:02:51 +00001598 href="#intrinsics">intrinsic function</a> because it takes an type as
1599an argument.</p>
Chris Lattner8d1a81d2003-10-18 05:51:36 +00001600<h5>Example:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001601<p>See the <a href="#int_varargs">variable argument processing</a>
1602section.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001603</div>
Chris Lattner8ff75902004-01-06 05:31:32 +00001604
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001605<!-- *********************************************************************** -->
Chris Lattner261efe92003-11-25 01:02:51 +00001606<div class="doc_section"> <a name="intrinsics">Intrinsic Functions</a> </div>
1607<!-- *********************************************************************** -->
Chris Lattner8ff75902004-01-06 05:31:32 +00001608
Misha Brukman9d0919f2003-11-08 01:05:38 +00001609<div class="doc_text">
Chris Lattner33aec9e2004-02-12 17:01:32 +00001610
1611<p>LLVM supports the notion of an "intrinsic function". These functions have
1612well known names and semantics, and are required to follow certain
1613restrictions. Overall, these instructions represent an extension mechanism for
1614the LLVM language that does not require changing all of the transformations in
1615LLVM to add to the language (or the bytecode reader/writer, the parser,
1616etc...).</p>
1617
1618<p>Intrinsic function names must all start with an "<tt>llvm.</tt>" prefix, this
1619prefix is reserved in LLVM for intrinsic names, thus functions may not be named
1620this. Intrinsic functions must always be external functions: you cannot define
1621the body of intrinsic functions. Intrinsic functions may only be used in call
1622or invoke instructions: it is illegal to take the address of an intrinsic
1623function. Additionally, because intrinsic functions are part of the LLVM
1624language, it is required that they all be documented here if any are added.</p>
1625
1626
1627<p>
1628Adding an intrinsic to LLVM is straight-forward if it is possible to express the
1629concept in LLVM directly (ie, code generator support is not _required_). To do
1630this, extend the default implementation of the IntrinsicLowering class to handle
1631the intrinsic. Code generators use this class to lower intrinsics they do not
1632understand to raw LLVM instructions that they do.
1633</p>
1634
Misha Brukman9d0919f2003-11-08 01:05:38 +00001635</div>
Chris Lattner8ff75902004-01-06 05:31:32 +00001636
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001637<!-- ======================================================================= -->
Chris Lattner8ff75902004-01-06 05:31:32 +00001638<div class="doc_subsection">
1639 <a name="int_varargs">Variable Argument Handling Intrinsics</a>
1640</div>
1641
Misha Brukman9d0919f2003-11-08 01:05:38 +00001642<div class="doc_text">
Misha Brukman9d0919f2003-11-08 01:05:38 +00001643<p>Variable argument support is defined in LLVM with the <a
Chris Lattner261efe92003-11-25 01:02:51 +00001644 href="#i_vanext"><tt>vanext</tt></a> instruction and these three
1645intrinsic functions. These functions are related to the similarly
1646named macros defined in the <tt>&lt;stdarg.h&gt;</tt> header file.</p>
1647<p>All of these functions operate on arguments that use a
1648target-specific value type "<tt>va_list</tt>". The LLVM assembly
1649language reference manual does not define what this type is, so all
1650transformations should be prepared to handle intrinsics with any type
1651used.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001652<p>This example shows how the <a href="#i_vanext"><tt>vanext</tt></a>
Chris Lattner261efe92003-11-25 01:02:51 +00001653instruction and the variable argument handling intrinsic functions are
1654used.</p>
Chris Lattner33aec9e2004-02-12 17:01:32 +00001655<pre>
1656int %test(int %X, ...) {
1657 ; Initialize variable argument processing
1658 %ap = call sbyte* %<a href="#i_va_start">llvm.va_start</a>()
1659
1660 ; Read a single integer argument
1661 %tmp = vaarg sbyte* %ap, int
1662
1663 ; Advance to the next argument
1664 %ap2 = vanext sbyte* %ap, int
1665
1666 ; Demonstrate usage of llvm.va_copy and llvm.va_end
1667 %aq = call sbyte* %<a href="#i_va_copy">llvm.va_copy</a>(sbyte* %ap2)
1668 call void %<a href="#i_va_end">llvm.va_end</a>(sbyte* %aq)
1669
1670 ; Stop processing of arguments.
1671 call void %<a href="#i_va_end">llvm.va_end</a>(sbyte* %ap2)
1672 ret int %tmp
1673}
1674</pre>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001675</div>
Chris Lattner8ff75902004-01-06 05:31:32 +00001676
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001677<!-- _______________________________________________________________________ -->
Chris Lattner8ff75902004-01-06 05:31:32 +00001678<div class="doc_subsubsection">
1679 <a name="i_va_start">'<tt>llvm.va_start</tt>' Intrinsic</a>
1680</div>
1681
1682
Misha Brukman9d0919f2003-11-08 01:05:38 +00001683<div class="doc_text">
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001684<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001685<pre> call va_list ()* %llvm.va_start()<br></pre>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001686<h5>Overview:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001687<p>The '<tt>llvm.va_start</tt>' intrinsic returns a new <tt>&lt;arglist&gt;</tt>
1688for subsequent use by the variable argument intrinsics.</p>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001689<h5>Semantics:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001690<p>The '<tt>llvm.va_start</tt>' intrinsic works just like the <tt>va_start</tt>
Chris Lattner261efe92003-11-25 01:02:51 +00001691macro available in C. In a target-dependent way, it initializes and
1692returns a <tt>va_list</tt> element, so that the next <tt>vaarg</tt>
1693will produce the first variable argument passed to the function. Unlike
1694the C <tt>va_start</tt> macro, this intrinsic does not need to know the
1695last argument of the function, the compiler can figure that out.</p>
1696<p>Note that this intrinsic function is only legal to be called from
1697within the body of a variable argument function.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001698</div>
Chris Lattner8ff75902004-01-06 05:31:32 +00001699
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001700<!-- _______________________________________________________________________ -->
Chris Lattner8ff75902004-01-06 05:31:32 +00001701<div class="doc_subsubsection">
1702 <a name="i_va_end">'<tt>llvm.va_end</tt>' Intrinsic</a>
1703</div>
1704
Misha Brukman9d0919f2003-11-08 01:05:38 +00001705<div class="doc_text">
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001706<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001707<pre> call void (va_list)* %llvm.va_end(va_list &lt;arglist&gt;)<br></pre>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001708<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001709<p>The '<tt>llvm.va_end</tt>' intrinsic destroys <tt>&lt;arglist&gt;</tt>
1710which has been initialized previously with <tt><a href="#i_va_start">llvm.va_start</a></tt>
1711or <tt><a href="#i_va_copy">llvm.va_copy</a></tt>.</p>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001712<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001713<p>The argument is a <tt>va_list</tt> to destroy.</p>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001714<h5>Semantics:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001715<p>The '<tt>llvm.va_end</tt>' intrinsic works just like the <tt>va_end</tt>
Chris Lattner261efe92003-11-25 01:02:51 +00001716macro available in C. In a target-dependent way, it destroys the <tt>va_list</tt>.
1717Calls to <a href="#i_va_start"><tt>llvm.va_start</tt></a> and <a
1718 href="#i_va_copy"><tt>llvm.va_copy</tt></a> must be matched exactly
1719with calls to <tt>llvm.va_end</tt>.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001720</div>
Chris Lattner8ff75902004-01-06 05:31:32 +00001721
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001722<!-- _______________________________________________________________________ -->
Chris Lattner8ff75902004-01-06 05:31:32 +00001723<div class="doc_subsubsection">
1724 <a name="i_va_copy">'<tt>llvm.va_copy</tt>' Intrinsic</a>
1725</div>
1726
Misha Brukman9d0919f2003-11-08 01:05:38 +00001727<div class="doc_text">
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001728<h5>Syntax:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001729<pre> call va_list (va_list)* %llvm.va_copy(va_list &lt;destarglist&gt;)<br></pre>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001730<h5>Overview:</h5>
Chris Lattner261efe92003-11-25 01:02:51 +00001731<p>The '<tt>llvm.va_copy</tt>' intrinsic copies the current argument
1732position from the source argument list to the destination argument list.</p>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001733<h5>Arguments:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001734<p>The argument is the <tt>va_list</tt> to copy.</p>
Chris Lattnerd9ad5b32003-05-08 04:57:36 +00001735<h5>Semantics:</h5>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001736<p>The '<tt>llvm.va_copy</tt>' intrinsic works just like the <tt>va_copy</tt>
Chris Lattner261efe92003-11-25 01:02:51 +00001737macro available in C. In a target-dependent way, it copies the source <tt>va_list</tt>
1738element into the returned list. This intrinsic is necessary because the <tt><a
1739 href="i_va_start">llvm.va_start</a></tt> intrinsic may be arbitrarily
1740complex and require memory allocation, for example.</p>
Misha Brukman9d0919f2003-11-08 01:05:38 +00001741</div>
Chris Lattner8ff75902004-01-06 05:31:32 +00001742
Chris Lattner33aec9e2004-02-12 17:01:32 +00001743<!-- ======================================================================= -->
1744<div class="doc_subsection">
Chris Lattner10610642004-02-14 04:08:35 +00001745 <a name="int_codegen">Code Generator Intrinsics</a>
1746</div>
1747
1748<div class="doc_text">
1749<p>
1750These intrinsics are provided by LLVM to expose special features that may only
1751be implemented with code generator support.
1752</p>
1753
1754</div>
1755
1756<!-- _______________________________________________________________________ -->
1757<div class="doc_subsubsection">
1758 <a name="i_returnaddress">'<tt>llvm.returnaddress</tt>' Intrinsic</a>
1759</div>
1760
1761<div class="doc_text">
1762
1763<h5>Syntax:</h5>
1764<pre>
1765 call void* ()* %llvm.returnaddress(uint &lt;level&gt;)
1766</pre>
1767
1768<h5>Overview:</h5>
1769
1770<p>
1771The '<tt>llvm.returnaddress</tt>' intrinsic returns a target-specific value
1772indicating the return address of the current function or one of its callers.
1773</p>
1774
1775<h5>Arguments:</h5>
1776
1777<p>
1778The argument to this intrinsic indicates which function to return the address
1779for. Zero indicates the calling function, one indicates its caller, etc. The
1780argument is <b>required</b> to be a constant integer value.
1781</p>
1782
1783<h5>Semantics:</h5>
1784
1785<p>
1786The '<tt>llvm.returnaddress</tt>' intrinsic either returns a pointer indicating
1787the return address of the specified call frame, or zero if it cannot be
1788identified. The value returned by this intrinsic is likely to be incorrect or 0
1789for arguments other than zero, so it should only be used for debugging purposes.
1790</p>
1791
1792<p>
1793Note that calling this intrinsic does not prevent function inlining or other
1794aggressive transformations, so the value returned may not that of the obvious
1795source-language caller.
1796</p>
1797</div>
1798
1799
1800<!-- _______________________________________________________________________ -->
1801<div class="doc_subsubsection">
1802 <a name="i_frameaddress">'<tt>llvm.frameaddress</tt>' Intrinsic</a>
1803</div>
1804
1805<div class="doc_text">
1806
1807<h5>Syntax:</h5>
1808<pre>
1809 call void* ()* %llvm.frameaddress(uint &lt;level&gt;)
1810</pre>
1811
1812<h5>Overview:</h5>
1813
1814<p>
1815The '<tt>llvm.frameaddress</tt>' intrinsic returns the target-specific frame
1816pointer value for the specified stack frame.
1817</p>
1818
1819<h5>Arguments:</h5>
1820
1821<p>
1822The argument to this intrinsic indicates which function to return the frame
1823pointer for. Zero indicates the calling function, one indicates its caller,
1824etc. The argument is <b>required</b> to be a constant integer value.
1825</p>
1826
1827<h5>Semantics:</h5>
1828
1829<p>
1830The '<tt>llvm.frameaddress</tt>' intrinsic either returns a pointer indicating
1831the frame address of the specified call frame, or zero if it cannot be
1832identified. The value returned by this intrinsic is likely to be incorrect or 0
1833for arguments other than zero, so it should only be used for debugging purposes.
1834</p>
1835
1836<p>
1837Note that calling this intrinsic does not prevent function inlining or other
1838aggressive transformations, so the value returned may not that of the obvious
1839source-language caller.
1840</p>
1841</div>
1842
1843
1844<!-- ======================================================================= -->
1845<div class="doc_subsection">
Chris Lattner33aec9e2004-02-12 17:01:32 +00001846 <a name="int_libc">Standard C Library Intrinsics</a>
1847</div>
1848
1849<div class="doc_text">
1850<p>
Chris Lattner10610642004-02-14 04:08:35 +00001851LLVM provides intrinsics for a few important standard C library functions.
1852These intrinsics allow source-language front-ends to pass information about the
1853alignment of the pointer arguments to the code generator, providing opportunity
1854for more efficient code generation.
Chris Lattner33aec9e2004-02-12 17:01:32 +00001855</p>
1856
1857</div>
1858
1859<!-- _______________________________________________________________________ -->
1860<div class="doc_subsubsection">
1861 <a name="i_memcpy">'<tt>llvm.memcpy</tt>' Intrinsic</a>
1862</div>
1863
1864<div class="doc_text">
1865
1866<h5>Syntax:</h5>
1867<pre>
1868 call void (sbyte*, sbyte*, uint, uint)* %llvm.memcpy(sbyte* &lt;dest&gt;, sbyte* &lt;src&gt;,
1869 uint &lt;len&gt;, uint &lt;align&gt;)
1870</pre>
1871
1872<h5>Overview:</h5>
1873
1874<p>
1875The '<tt>llvm.memcpy</tt>' intrinsic copies a block of memory from the source
1876location to the destination location.
1877</p>
1878
1879<p>
1880Note that, unlike the standard libc function, the <tt>llvm.memcpy</tt> intrinsic
1881does not return a value, and takes an extra alignment argument.
1882</p>
1883
1884<h5>Arguments:</h5>
1885
1886<p>
1887The first argument is a pointer to the destination, the second is a pointer to
1888the source. The third argument is an (arbitrarily sized) integer argument
1889specifying the number of bytes to copy, and the fourth argument is the alignment
1890of the source and destination locations.
1891</p>
1892
Chris Lattner3301ced2004-02-12 21:18:15 +00001893<p>
1894If the call to this intrinisic has an alignment value that is not 0 or 1, then
1895the caller guarantees that the size of the copy is a multiple of the alignment
1896and that both the source and destination pointers are aligned to that boundary.
1897</p>
1898
Chris Lattner33aec9e2004-02-12 17:01:32 +00001899<h5>Semantics:</h5>
1900
1901<p>
1902The '<tt>llvm.memcpy</tt>' intrinsic copies a block of memory from the source
1903location to the destination location, which are not allowed to overlap. It
1904copies "len" bytes of memory over. If the argument is known to be aligned to
1905some boundary, this can be specified as the fourth argument, otherwise it should
1906be set to 0 or 1.
1907</p>
1908</div>
1909
1910
Chris Lattner0eb51b42004-02-12 18:10:10 +00001911<!-- _______________________________________________________________________ -->
1912<div class="doc_subsubsection">
1913 <a name="i_memmove">'<tt>llvm.memmove</tt>' Intrinsic</a>
1914</div>
1915
1916<div class="doc_text">
1917
1918<h5>Syntax:</h5>
1919<pre>
1920 call void (sbyte*, sbyte*, uint, uint)* %llvm.memmove(sbyte* &lt;dest&gt;, sbyte* &lt;src&gt;,
1921 uint &lt;len&gt;, uint &lt;align&gt;)
1922</pre>
1923
1924<h5>Overview:</h5>
1925
1926<p>
1927The '<tt>llvm.memmove</tt>' intrinsic moves a block of memory from the source
1928location to the destination location. It is similar to the '<tt>llvm.memcpy</tt>'
1929intrinsic but allows the two memory locations to overlap.
1930</p>
1931
1932<p>
1933Note that, unlike the standard libc function, the <tt>llvm.memmove</tt> intrinsic
1934does not return a value, and takes an extra alignment argument.
1935</p>
1936
1937<h5>Arguments:</h5>
1938
1939<p>
1940The first argument is a pointer to the destination, the second is a pointer to
1941the source. The third argument is an (arbitrarily sized) integer argument
1942specifying the number of bytes to copy, and the fourth argument is the alignment
1943of the source and destination locations.
1944</p>
1945
Chris Lattner3301ced2004-02-12 21:18:15 +00001946<p>
1947If the call to this intrinisic has an alignment value that is not 0 or 1, then
1948the caller guarantees that the size of the copy is a multiple of the alignment
1949and that both the source and destination pointers are aligned to that boundary.
1950</p>
1951
Chris Lattner0eb51b42004-02-12 18:10:10 +00001952<h5>Semantics:</h5>
1953
1954<p>
1955The '<tt>llvm.memmove</tt>' intrinsic copies a block of memory from the source
1956location to the destination location, which may overlap. It
1957copies "len" bytes of memory over. If the argument is known to be aligned to
1958some boundary, this can be specified as the fourth argument, otherwise it should
1959be set to 0 or 1.
1960</p>
1961</div>
1962
Chris Lattner8ff75902004-01-06 05:31:32 +00001963
Chris Lattner10610642004-02-14 04:08:35 +00001964<!-- _______________________________________________________________________ -->
1965<div class="doc_subsubsection">
1966 <a name="i_memset">'<tt>llvm.memset</tt>' Intrinsic</a>
1967</div>
1968
1969<div class="doc_text">
1970
1971<h5>Syntax:</h5>
1972<pre>
1973 call void (sbyte*, ubyte, uint, uint)* %llvm.memset(sbyte* &lt;dest&gt;, ubyte &lt;val&gt;,
1974 uint &lt;len&gt;, uint &lt;align&gt;)
1975</pre>
1976
1977<h5>Overview:</h5>
1978
1979<p>
1980The '<tt>llvm.memset</tt>' intrinsic fills a block of memory with a particular
1981byte value.
1982</p>
1983
1984<p>
1985Note that, unlike the standard libc function, the <tt>llvm.memset</tt> intrinsic
1986does not return a value, and takes an extra alignment argument.
1987</p>
1988
1989<h5>Arguments:</h5>
1990
1991<p>
1992The first argument is a pointer to the destination to fill, the second is the
1993byte value to fill it with, the third argument is an (arbitrarily sized) integer
1994argument specifying the number of bytes to fill, and the fourth argument is the
1995known alignment of destination location.
1996</p>
1997
1998<p>
1999If the call to this intrinisic has an alignment value that is not 0 or 1, then
2000the caller guarantees that the size of the copy is a multiple of the alignment
2001and that the destination pointer is aligned to that boundary.
2002</p>
2003
2004<h5>Semantics:</h5>
2005
2006<p>
2007The '<tt>llvm.memset</tt>' intrinsic fills "len" bytes of memory starting at the
2008destination location. If the argument is known to be aligned to some boundary,
2009this can be specified as the fourth argument, otherwise it should be set to 0 or
20101.
2011</p>
2012</div>
2013
2014
Chris Lattner8ff75902004-01-06 05:31:32 +00002015<!-- ======================================================================= -->
2016<div class="doc_subsection">
2017 <a name="int_debugger">Debugger Intrinsics</a>
2018</div>
2019
2020<div class="doc_text">
2021<p>
2022The LLVM debugger intrinsics (which all start with <tt>llvm.dbg.</tt> prefix),
2023are described in the <a
2024href="SourceLevelDebugging.html#format_common_intrinsics">LLVM Source Level
2025Debugging</a> document.
2026</p>
2027</div>
2028
2029
Chris Lattner00950542001-06-06 20:29:01 +00002030<!-- *********************************************************************** -->
Chris Lattner00950542001-06-06 20:29:01 +00002031<hr>
Misha Brukman9d0919f2003-11-08 01:05:38 +00002032<div class="doc_footer">
Chris Lattner261efe92003-11-25 01:02:51 +00002033<address><a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
2034<a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a> <br>
2035Last modified: $Date$ </div>
Misha Brukman9d0919f2003-11-08 01:05:38 +00002036</body>
2037</html>