Fixed another minor grammatical error.
[oota-llvm.git] / docs / ProgrammersManual.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html><head><title>LLVM Programmer's Manual</title></head>
3
4 <body bgcolor=white>
5
6 <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
7 <tr><td>&nbsp; <font size=+3 color="#EEEEFF" face="Georgia,Palatino,Times,Roman"><b>LLVM Programmer's Manual</b></font></td>
8 </tr></table>
9  
10 <ol>
11   <li><a href="#introduction">Introduction</a>
12   <li><a href="#general">General Information</a>
13   <ul>
14     <li><a href="#stl">The C++ Standard Template Library</a>
15 <!--
16     <li>The <tt>-time-passes</tt> option
17     <li>How to use the LLVM Makefile system
18     <li>How to write a regression test
19 -->
20   </ul>
21   <li><a href="#apis">Important and useful LLVM APIs</a>
22   <ul>
23     <li><a href="#isa">The <tt>isa&lt;&gt;</tt>, <tt>cast&lt;&gt;</tt> and
24                        <tt>dyn_cast&lt;&gt;</tt> templates</a>
25     <li><a href="#DEBUG">The <tt>DEBUG()</tt> macro &amp;
26                        <tt>-debug</tt> option</a>
27     <ul>
28       <li><a href="#DEBUG_TYPE">Fine grained debug info with 
29           <tt>DEBUG_TYPE</tt> and the <tt>-debug-only</tt> option</a/>
30     </ul>
31     <li><a href="#Statistic">The <tt>Statistic</tt> template &amp;
32                        <tt>-stats</tt> option</a>
33 <!--
34     <li>The <tt>InstVisitor</tt> template
35     <li>The general graph API
36 -->
37   </ul>
38   <li><a href="#common">Helpful Hints for Common Operations</a>
39   <ul>
40     <li><a href="#inspection">Basic Inspection and Traversal Routines</a>
41     <ul>
42       <li><a href="#iterate_function">Iterating over the <tt>BasicBlock</tt>s
43                                        in a <tt>Function</tt></a>
44       <li><a href="#iterate_basicblock">Iterating over the <tt>Instruction</tt>s
45                                        in a <tt>BasicBlock</tt></a>
46       <li><a href="#iterate_institer">Iterating over the <tt>Instruction</tt>s
47                                        in a <tt>Function</tt></a>
48       <li><a href="#iterate_convert">Turning an iterator into a class
49                                         pointer</a>
50       <li><a href="#iterate_complex">Finding call sites: a more complex
51                                         example</a>
52       <li><a href="#calls_and_invokes">Treating calls and invokes the
53                                         same way</a>
54       <li><a href="#iterate_chains">Iterating over def-use &amp; use-def
55                                     chains</a>
56     </ul>
57     <li><a href="#simplechanges">Making simple changes</a>
58     <ul>
59       <li><a href="#schanges_creating">Creating and inserting new
60                   <tt>Instruction</tt>s</a>
61       <li><a href="#schanges_deleting">Deleting
62                   <tt>Instruction</tt>s</a> 
63       <li><a href="#schanges_replacing">Replacing an
64                   <tt>Instruction</tt> with another <tt>Value</tt></a>
65     </ul>
66 <!--
67     <li>Working with the Control Flow Graph
68     <ul>
69       <li>Accessing predecessors and successors of a <tt>BasicBlock</tt>
70       <li>
71       <li>
72     </ul>
73 -->
74   </ul>
75   <li><a href="#coreclasses">The Core LLVM Class Hierarchy Reference</a>
76   <ul>
77     <li><a href="#Value">The <tt>Value</tt> class</a>
78     <ul>
79       <li><a href="#User">The <tt>User</tt> class</a>
80       <ul>
81         <li><a href="#Instruction">The <tt>Instruction</tt> class</a>
82         <ul>
83         <li>
84         </ul>
85         <li><a href="#GlobalValue">The <tt>GlobalValue</tt> class</a>
86         <ul>
87           <li><a href="#BasicBlock">The <tt>BasicBlock</tt> class</a>
88           <li><a href="#Function">The <tt>Function</tt> class</a>
89           <li><a href="#GlobalVariable">The <tt>GlobalVariable</tt> class</a>
90         </ul>
91         <li><a href="#Module">The <tt>Module</tt> class</a>
92         <li><a href="#Constant">The <tt>Constant</tt> class</a>
93         <ul>
94         <li>
95         <li>
96         </ul>
97       </ul>
98       <li><a href="#Type">The <tt>Type</tt> class</a>
99       <li><a href="#Argument">The <tt>Argument</tt> class</a>
100     </ul>
101     <li>The <tt>SymbolTable</tt> class
102     <li>The <tt>ilist</tt> and <tt>iplist</tt> classes
103     <ul>
104       <li>Creating, inserting, moving and deleting from LLVM lists
105     </ul>
106     <li>Important iterator invalidation semantics to be aware of
107   </ul>
108
109   <p><b>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>,
110         <a href="mailto:dhurjati@cs.uiuc.edu">Dinakar Dhurjati</a>, and
111       <a href="mailto:jstanley@cs.uiuc.edu">Joel Stanley</a></b><p>
112 </ol>
113
114
115 <!-- *********************************************************************** -->
116 <table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
117 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
118 <a name="introduction">Introduction
119 </b></font></td></tr></table><ul>
120 <!-- *********************************************************************** -->
121
122 This document is meant to highlight some of the important classes and interfaces
123 available in the LLVM source-base.  This manual is not intended to explain what
124 LLVM is, how it works, and what LLVM code looks like.  It assumes that you know
125 the basics of LLVM and are interested in writing transformations or otherwise
126 analyzing or manipulating the code.<p>
127
128 This document should get you oriented so that you can find your way in the
129 continuously growing source code that makes up the LLVM infrastructure.  Note
130 that this manual is not intended to serve as a replacement for reading the
131 source code, so if you think there should be a method in one of these classes to
132 do something, but it's not listed, check the source.  Links to the <a
133 href="/doxygen/">doxygen</a> sources are provided to make this as easy as
134 possible.<p>
135
136 The first section of this document describes general information that is useful
137 to know when working in the LLVM infrastructure, and the second describes the
138 Core LLVM classes.  In the future this manual will be extended with information
139 describing how to use extension libraries, such as dominator information, CFG
140 traversal routines, and useful utilities like the <tt><a
141 href="/doxygen/InstVisitor_8h-source.html">InstVisitor</a></tt> template.<p>
142
143
144 <!-- *********************************************************************** -->
145 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
146 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
147 <a name="general">General Information
148 </b></font></td></tr></table><ul>
149 <!-- *********************************************************************** -->
150
151 This section contains general information that is useful if you are working in
152 the LLVM source-base, but that isn't specific to any particular API.<p>
153
154
155 <!-- ======================================================================= -->
156 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
157 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
158 <font color="#EEEEFF" face="Georgia,Palatino"><b>
159 <a name="stl">The C++ Standard Template Library</a>
160 </b></font></td></tr></table><ul>
161
162 LLVM makes heavy use of the C++ Standard Template Library (STL), perhaps much
163 more than you are used to, or have seen before.  Because of this, you might want
164 to do a little background reading in the techniques used and capabilities of the
165 library.  There are many good pages that discuss the STL, and several books on
166 the subject that you can get, so it will not be discussed in this document.<p>
167
168 Here are some useful links:<p>
169
170 <ol>
171 <li><a href="http://www.dinkumware.com/refxcpp.html">Dinkumware C++
172 Library reference</a> - an excellent reference for the STL and other parts of
173 the standard C++ library.
174
175 <li><a href="http://www.tempest-sw.com/cpp/">C++ In a Nutshell</a> - This is an
176 O'Reilly book in the making.  It has a decent <a
177 href="http://www.tempest-sw.com/cpp/ch13-libref.html">Standard Library
178 Reference</a> that rivals Dinkumware's, and is actually free until the book is
179 published.
180
181 <li><a href="http://www.parashift.com/c++-faq-lite/">C++ Frequently Asked
182 Questions</a>
183
184 <li><a href="http://www.sgi.com/tech/stl/">SGI's STL Programmer's Guide</a> -
185 Contains a useful <a
186 href="http://www.sgi.com/tech/stl/stl_introduction.html">Introduction to the
187 STL</a>.
188
189 <li><a href="http://www.research.att.com/~bs/C++.html">Bjarne Stroustrup's C++
190 Page</a>
191
192 </ol><p>
193
194 You are also encouraged to take a look at the <a
195 href="CodingStandards.html">LLVM Coding Standards</a> guide which focuses on how
196 to write maintainable code more than where to put your curly braces.<p>
197
198 <!-- ======================================================================= -->
199 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
200 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
201 <font color="#EEEEFF" face="Georgia,Palatino"><b>
202 <a name="stl">Other useful references</a>
203 </b></font></td></tr></table><ul>
204
205 LLVM is currently using CVS as its source versioning system. You may find this
206 reference handy:<p>
207
208 <ol>
209 <li><a href="http://www.psc.edu/~semke/cvs_branches.html">CVS Branch and Tag
210 Primer</a></li>
211 </ol><p>
212
213 <!-- *********************************************************************** -->
214 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
215 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
216 <a name="apis">Important and useful LLVM APIs
217 </b></font></td></tr></table><ul>
218 <!-- *********************************************************************** -->
219
220 Here we highlight some LLVM APIs that are generally useful and good to know
221 about when writing transformations.<p>
222
223 <!-- ======================================================================= -->
224 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
225 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
226 <font color="#EEEEFF" face="Georgia,Palatino"><b>
227 <a name="isa">The isa&lt;&gt;, cast&lt;&gt; and dyn_cast&lt;&gt; templates</a>
228 </b></font></td></tr></table><ul>
229
230 The LLVM source-base makes extensive use of a custom form of RTTI.  These
231 templates have many similarities to the C++ <tt>dynamic_cast&lt;&gt;</tt>
232 operator, but they don't have some drawbacks (primarily stemming from the fact
233 that <tt>dynamic_cast&lt;&gt;</tt> only works on classes that have a v-table).
234 Because they are used so often, you must know what they do and how they work.
235 All of these templates are defined in the <a
236 href="/doxygen/Casting_8h-source.html"><tt>Support/Casting.h</tt></a> file (note
237 that you very rarely have to include this file directly).<p>
238
239 <dl>
240
241 <dt><tt>isa&lt;&gt;</tt>:
242
243 <dd>The <tt>isa&lt;&gt;</tt> operator works exactly like the Java
244 "<tt>instanceof</tt>" operator.  It returns true or false depending on whether a
245 reference or pointer points to an instance of the specified class.  This can be
246 very useful for constraint checking of various sorts (example below).<p>
247
248
249 <dt><tt>cast&lt;&gt;</tt>:
250
251 <dd>The <tt>cast&lt;&gt;</tt> operator is a "checked cast" operation.  It
252 converts a pointer or reference from a base class to a derived cast, causing an
253 assertion failure if it is not really an instance of the right type.  This
254 should be used in cases where you have some information that makes you believe
255 that something is of the right type.  An example of the <tt>isa&lt;&gt;</tt> and
256 <tt>cast&lt;&gt;</tt> template is:<p>
257
258 <pre>
259 static bool isLoopInvariant(const <a href="#Value">Value</a> *V, const Loop *L) {
260   if (isa&lt;<a href="#Constant">Constant</a>&gt;(V) || isa&lt;<a href="#Argument">Argument</a>&gt;(V) || isa&lt;<a href="#GlobalValue">GlobalValue</a>&gt;(V))
261     return true;
262
263   <i>// Otherwise, it must be an instruction...</i>
264   return !L-&gt;contains(cast&lt;<a href="#Instruction">Instruction</a>&gt;(V)-&gt;getParent());
265 </pre><p>
266
267 Note that you should <b>not</b> use an <tt>isa&lt;&gt;</tt> test followed by a
268 <tt>cast&lt;&gt;</tt>, for that use the <tt>dyn_cast&lt;&gt;</tt> operator.<p>
269
270
271 <dt><tt>dyn_cast&lt;&gt;</tt>:
272
273 <dd>The <tt>dyn_cast&lt;&gt;</tt> operator is a "checking cast" operation.  It
274 checks to see if the operand is of the specified type, and if so, returns a
275 pointer to it (this operator does not work with references).  If the operand is
276 not of the correct type, a null pointer is returned.  Thus, this works very much
277 like the <tt>dynamic_cast</tt> operator in C++, and should be used in the same
278 circumstances.  Typically, the <tt>dyn_cast&lt;&gt;</tt> operator is used in an
279 <tt>if</tt> statement or some other flow control statement like this:<p>
280
281 <pre>
282   if (<a href="#AllocationInst">AllocationInst</a> *AI = dyn_cast&lt;<a href="#AllocationInst">AllocationInst</a>&gt;(Val)) {
283     ...
284   }
285 </pre><p>
286
287 This form of the <tt>if</tt> statement effectively combines together a call to
288 <tt>isa&lt;&gt;</tt> and a call to <tt>cast&lt;&gt;</tt> into one statement,
289 which is very convenient.<p>
290
291 Another common example is:<p>
292
293 <pre>
294   <i>// Loop over all of the phi nodes in a basic block</i>
295   BasicBlock::iterator BBI = BB-&gt;begin();
296   for (; <a href="#PhiNode">PHINode</a> *PN = dyn_cast&lt;<a href="#PHINode">PHINode</a>&gt;(BBI); ++BBI)
297     cerr &lt;&lt; *PN;
298 </pre><p>
299
300 Note that the <tt>dyn_cast&lt;&gt;</tt> operator, like C++'s
301 <tt>dynamic_cast</tt> or Java's <tt>instanceof</tt> operator, can be abused.  In
302 particular you should not use big chained <tt>if/then/else</tt> blocks to check
303 for lots of different variants of classes.  If you find yourself wanting to do
304 this, it is much cleaner and more efficient to use the InstVisitor class to
305 dispatch over the instruction type directly.<p>
306
307
308 <dt><tt>cast_or_null&lt;&gt;</tt>:
309
310 <dd>The <tt>cast_or_null&lt;&gt;</tt> operator works just like the
311 <tt>cast&lt;&gt;</tt> operator, except that it allows for a null pointer as an
312 argument (which it then propagates).  This can sometimes be useful, allowing you
313 to combine several null checks into one.<p>
314
315
316 <dt><tt>dyn_cast_or_null&lt;&gt;</tt>:
317
318 <dd>The <tt>dyn_cast_or_null&lt;&gt;</tt> operator works just like the
319 <tt>dyn_cast&lt;&gt;</tt> operator, except that it allows for a null pointer as
320 an argument (which it then propagates).  This can sometimes be useful, allowing
321 you to combine several null checks into one.<p>
322
323 </dl>
324
325 These five templates can be used with any classes, whether they have a v-table
326 or not.  To add support for these templates, you simply need to add
327 <tt>classof</tt> static methods to the class you are interested casting to.
328 Describing this is currently outside the scope of this document, but there are
329 lots of examples in the LLVM source base.<p>
330
331
332 <!-- ======================================================================= -->
333 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
334 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
335 <font color="#EEEEFF" face="Georgia,Palatino"><b>
336 <a name="DEBUG">The <tt>DEBUG()</tt> macro &amp; <tt>-debug</tt> option</a>
337 </b></font></td></tr></table><ul>
338
339 Often when working on your pass you will put a bunch of debugging printouts and
340 other code into your pass.  After you get it working, you want to remove
341 it... but you may need it again in the future (to work out new bugs that you run
342 across).<p>
343
344 Naturally, because of this, you don't want to delete the debug printouts, but
345 you don't want them to always be noisy.  A standard compromise is to comment
346 them out, allowing you to enable them if you need them in the future.<p>
347
348 The "<tt><a href="/doxygen/Debug_8h-source.html">Support/Debug.h</a></tt>" file
349 provides a macro named <tt>DEBUG()</tt> that is a much nicer solution to this
350 problem.  Basically, you can put arbitrary code into the argument of the
351 <tt>DEBUG</tt> macro, and it is only executed if '<tt>opt</tt>' (or any other
352 tool) is run with the '<tt>-debug</tt>' command line argument:
353
354 <pre>
355      ... 
356      DEBUG(std::cerr &lt;&lt; "I am here!\n");
357      ...
358 </pre><p>
359
360 Then you can run your pass like this:<p>
361
362 <pre>
363   $ opt &lt; a.bc &gt; /dev/null -mypass
364     &lt;no output&gt;
365   $ opt &lt; a.bc &gt; /dev/null -mypass -debug
366     I am here!
367   $
368 </pre><p>
369
370 Using the <tt>DEBUG()</tt> macro instead of a home-brewed solution allows you to
371 now have to create "yet another" command line option for the debug output for
372 your pass.  Note that <tt>DEBUG()</tt> macros are disabled for optimized builds,
373 so they do not cause a performance impact at all (for the same reason, they
374 should also not contain side-effects!).<p>
375
376 One additional nice thing about the <tt>DEBUG()</tt> macro is that you can
377 enable or disable it directly in gdb.  Just use "<tt>set DebugFlag=0</tt>" or
378 "<tt>set DebugFlag=1</tt>" from the gdb if the program is running.  If the
379 program hasn't been started yet, you can always just run it with
380 <tt>-debug</tt>.<p>
381
382 <!-- _______________________________________________________________________ -->
383 </ul><h4><a name="DEBUG_TYPE"><hr size=0>Fine grained debug info with 
384           <tt>DEBUG_TYPE()</tt> and the <tt>-debug-only</tt> option</a> </h4><ul>
385
386 Sometimes you may find yourself in a situation where enabling <tt>-debug</tt>
387 just turns on <b>too much</b> information (such as when working on the code
388 generator).  If you want to enable debug information with more fine-grained
389 control, you define the <tt>DEBUG_TYPE</tt> macro and the <tt>-debug</tt> only
390 option as follows:<p>
391
392 <pre>
393      ...
394      DEBUG(std::cerr &lt;&lt; "No debug type\n");
395      #undef  DEBUG_TYPE
396      #define DEBUG_TYPE "foo"
397      DEBUG(std::cerr &lt;&lt; "'foo' debug type\n");
398      #undef  DEBUG_TYPE
399      #define DEBUG_TYPE "bar"
400      DEBUG(std::cerr &lt;&lt; "'bar' debug type\n");
401      #undef  DEBUG_TYPE
402      #define DEBUG_TYPE ""
403      DEBUG(std::cerr &lt;&lt; "No debug type (2)\n");
404      ...
405 </pre><p>
406
407 Then you can run your pass like this:<p>
408
409 <pre>
410   $ opt &lt; a.bc &gt; /dev/null -mypass
411     &lt;no output&gt;
412   $ opt &lt; a.bc &gt; /dev/null -mypass -debug
413     No debug type
414     'foo' debug type
415     'bar' debug type
416     No debug type (2)
417   $ opt &lt; a.bc &gt; /dev/null -mypass -debug-only=foo
418     'foo' debug type
419   $ opt &lt; a.bc &gt; /dev/null -mypass -debug-only=bar
420     'bar' debug type
421   $
422 </pre><p>
423
424 Of course, in practice, you should only set <tt>DEBUG_TYPE</tt> at the top of a
425 file, to specify the debug type for the entire module (if you do this before you
426 <tt>#include "Support/Debug.h"</tt>, you don't have to insert the ugly
427 <tt>#undef</tt>'s).  Also, you should use names more meaningful that "foo" and
428 "bar", because there is no system in place to ensure that names do not conflict:
429 if two different modules use the same string, they will all be turned on when
430 the name is specified.  This allows all, say, instruction scheduling, debug
431 information to be enabled with <tt>-debug-type=InstrSched</tt>, even if the
432 source lives in multiple files.<p>
433
434
435 <!-- ======================================================================= -->
436 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
437 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
438 <font color="#EEEEFF" face="Georgia,Palatino"><b>
439 <a name="Statistic">The <tt>Statistic</tt> template &amp; <tt>-stats</tt>
440 option</a>
441 </b></font></td></tr></table><ul>
442
443 The "<tt><a
444 href="/doxygen/Statistic_8h-source.html">Support/Statistic.h</a></tt>"
445 file provides a template named <tt>Statistic</tt> that is used as a unified way
446 to keeping track of what the LLVM compiler is doing and how effective various
447 optimizations are.  It is useful to see what optimizations are contributing to
448 making a particular program run faster.<p>
449
450 Often you may run your pass on some big program, and you're interested to see
451 how many times it makes a certain transformation.  Although you can do this with
452 hand inspection, or some ad-hoc method, this is a real pain and not very useful
453 for big programs.  Using the <tt>Statistic</tt> template makes it very easy to
454 keep track of this information, and the calculated information is presented in a
455 uniform manner with the rest of the passes being executed.<p>
456
457 There are many examples of <tt>Statistic</tt> users, but this basics of using it
458 are as follows:<p>
459
460 <ol>
461 <li>Define your statistic like this:<p>
462
463 <pre>
464 static Statistic&lt;&gt; NumXForms("mypassname", "The # of times I did stuff");
465 </pre><p>
466
467 The <tt>Statistic</tt> template can emulate just about any data-type, but if you
468 do not specify a template argument, it defaults to acting like an unsigned int
469 counter (this is usually what you want).<p>
470
471 <li>Whenever you make a transformation, bump the counter:<p>
472
473 <pre>
474    ++NumXForms;   // I did stuff
475 </pre><p>
476
477 </ol><p>
478
479 That's all you have to do.  To get '<tt>opt</tt>' to print out the statistics
480 gathered, use the '<tt>-stats</tt>' option:<p>
481
482 <pre>
483    $ opt -stats -mypassname &lt; program.bc &gt; /dev/null
484     ... statistic output ...
485 </pre><p>
486
487 When running <tt>gccas</tt> on a C file from the SPEC benchmark suite, it gives
488 a report that looks like this:<p>
489
490 <pre>
491    7646 bytecodewriter  - Number of normal instructions
492     725 bytecodewriter  - Number of oversized instructions
493  129996 bytecodewriter  - Number of bytecode bytes written
494    2817 raise           - Number of insts DCEd or constprop'd
495    3213 raise           - Number of cast-of-self removed
496    5046 raise           - Number of expression trees converted
497      75 raise           - Number of other getelementptr's formed
498     138 raise           - Number of load/store peepholes
499      42 deadtypeelim    - Number of unused typenames removed from symtab
500     392 funcresolve     - Number of varargs functions resolved
501      27 globaldce       - Number of global variables removed
502       2 adce            - Number of basic blocks removed
503     134 cee             - Number of branches revectored
504      49 cee             - Number of setcc instruction eliminated
505     532 gcse            - Number of loads removed
506    2919 gcse            - Number of instructions removed
507      86 indvars         - Number of canonical indvars added
508      87 indvars         - Number of aux indvars removed
509      25 instcombine     - Number of dead inst eliminate
510     434 instcombine     - Number of insts combined
511     248 licm            - Number of load insts hoisted
512    1298 licm            - Number of insts hoisted to a loop pre-header
513       3 licm            - Number of insts hoisted to multiple loop preds (bad, no loop pre-header)
514      75 mem2reg         - Number of alloca's promoted
515    1444 cfgsimplify     - Number of blocks simplified
516 </pre><p>
517
518 Obviously, with so many optimizations, having a unified framework for this stuff
519 is very nice.  Making your pass fit well into the framework makes it more
520 maintainable and useful.<p>
521
522
523 <!-- *********************************************************************** -->
524 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
525 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
526 <a name="common">Helpful Hints for Common Operations
527 </b></font></td></tr></table><ul> <!--
528 *********************************************************************** -->
529
530 This section describes how to perform some very simple transformations of LLVM
531 code.  This is meant to give examples of common idioms used, showing the
532 practical side of LLVM transformations.<p>
533
534 Because this is a "how-to" section, you should also read about the main classes
535 that you will be working with.  The <a href="#coreclasses">Core LLVM Class
536 Hierarchy Reference</a> contains details and descriptions of the main classes
537 that you should know about.<p>
538
539 <!-- NOTE: this section should be heavy on example code -->
540
541
542 <!-- ======================================================================= -->
543 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
544 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
545 <font color="#EEEEFF" face="Georgia,Palatino"><b>
546 <a name="inspection">Basic Inspection and Traversal Routines</a>
547 </b></font></td></tr></table><ul>
548
549 The LLVM compiler infrastructure have many different data structures that may be
550 traversed.  Following the example of the C++ standard template library, the
551 techniques used to traverse these various data structures are all basically the
552 same.  For a enumerable sequence of values, the <tt>XXXbegin()</tt> function (or
553 method) returns an iterator to the start of the sequence, the <tt>XXXend()</tt>
554 function returns an iterator pointing to one past the last valid element of the
555 sequence, and there is some <tt>XXXiterator</tt> data type that is common
556 between the two operations.<p>
557
558 Because the pattern for iteration is common across many different aspects of the
559 program representation, the standard template library algorithms may be used on
560 them, and it is easier to remember how to iterate.  First we show a few common
561 examples of the data structures that need to be traversed.  Other data
562 structures are traversed in very similar ways.<p>
563
564
565 <!-- _______________________________________________________________________ -->
566 </ul><h4><a name="iterate_function"><hr size=0>Iterating over the <a
567 href="#BasicBlock"><tt>BasicBlock</tt></a>s in a <a
568 href="#Function"><tt>Function</tt></a> </h4><ul>
569
570 It's quite common to have a <tt>Function</tt> instance that you'd like
571 to transform in some way; in particular, you'd like to manipulate its
572 <tt>BasicBlock</tt>s.  To facilitate this, you'll need to iterate over
573 all of the <tt>BasicBlock</tt>s that constitute the <tt>Function</tt>.
574 The following is an example that prints the name of a
575 <tt>BasicBlock</tt> and the number of <tt>Instruction</tt>s it
576 contains:
577
578 <pre>
579   // func is a pointer to a Function instance
580   for (Function::iterator i = func-&gt;begin(), e = func-&gt;end(); i != e; ++i) {
581
582       // print out the name of the basic block if it has one, and then the
583       // number of instructions that it contains
584
585       cerr &lt;&lt "Basic block (name=" &lt;&lt i-&gt;getName() &lt;&lt; ") has " 
586            &lt;&lt i-&gt;size() &lt;&lt " instructions.\n";
587   }
588 </pre>
589
590 Note that i can be used as if it were a pointer for the purposes of
591 invoking member functions of the <tt>Instruction</tt> class.  This is
592 because the indirection operator is overloaded for the iterator
593 classes.  In the above code, the expression <tt>i-&gt;size()</tt> is
594 exactly equivalent to <tt>(*i).size()</tt> just like you'd expect.
595
596 <!-- _______________________________________________________________________ -->
597 </ul><h4><a name="iterate_basicblock"><hr size=0>Iterating over the <a
598 href="#Instruction"><tt>Instruction</tt></a>s in a <a
599 href="#BasicBlock"><tt>BasicBlock</tt></a> </h4><ul>
600
601 Just like when dealing with <tt>BasicBlock</tt>s in
602 <tt>Function</tt>s, it's easy to iterate over the individual
603 instructions that make up <tt>BasicBlock</tt>s.  Here's a code snippet
604 that prints out each instruction in a <tt>BasicBlock</tt>:
605
606 <pre>
607   // blk is a pointer to a BasicBlock instance
608   for (BasicBlock::iterator i = blk-&gt;begin(), e = blk-&gt;end(); i != e; ++i)
609      // the next statement works since operator&lt;&lt;(ostream&amp;,...) 
610      // is overloaded for Instruction&amp;
611      cerr &lt;&lt; *i &lt;&lt; "\n";
612 </pre>
613
614 However, this isn't really the best way to print out the contents of a
615 <tt>BasicBlock</tt>!  Since the ostream operators are overloaded for
616 virtually anything you'll care about, you could have just invoked the
617 print routine on the basic block itself: <tt>cerr &lt;&lt; *blk &lt;&lt;
618 "\n";</tt>.<p>
619
620 Note that currently operator&lt;&lt; is implemented for <tt>Value*</tt>, so it 
621 will print out the contents of the pointer, instead of 
622 the pointer value you might expect.  This is a deprecated interface that will
623 be removed in the future, so it's best not to depend on it.  To print out the
624 pointer value for now, you must cast to <tt>void*</tt>.<p>
625
626
627 <!-- _______________________________________________________________________ -->
628 </ul><h4><a name="iterate_institer"><hr size=0>Iterating over the <a
629 href="#Instruction"><tt>Instruction</tt></a>s in a <a
630 href="#Function"><tt>Function</tt></a></h4><ul>
631
632 If you're finding that you commonly iterate over a <tt>Function</tt>'s
633 <tt>BasicBlock</tt>s and then that <tt>BasicBlock</tt>'s
634 <tt>Instruction</tt>s, <tt>InstIterator</tt> should be used instead.
635 You'll need to include <a href="/doxygen/InstIterator_8h-source.html"><tt>llvm/Support/InstIterator.h</tt></a>, and then
636 instantiate <tt>InstIterator</tt>s explicitly in your code.  Here's a
637 small example that shows how to dump all instructions in a function to
638 stderr (<b>Note:</b> Dereferencing an <tt>InstIterator</tt> yields an
639 <tt>Instruction*</tt>, <i>not</i> an <tt>Instruction&amp</tt>!):
640
641 <pre>
642 #include "<a href="/doxygen/InstIterator_8h-source.html">llvm/Support/InstIterator.h</a>"
643 ...
644 // Suppose F is a ptr to a function
645 for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
646   cerr &lt;&lt **i &lt;&lt "\n";
647 </pre>
648
649 Easy, isn't it?  You can also use <tt>InstIterator</tt>s to fill a
650 worklist with its initial contents.  For example, if you wanted to
651 initialize a worklist to contain all instructions in a
652 <tt>Function</tt> F, all you would need to do is something like:
653
654 <pre>
655 std::set&lt;Instruction*&gt worklist;
656 worklist.insert(inst_begin(F), inst_end(F));
657 </pre>
658
659 The STL set <tt>worklist</tt> would now contain all instructions in
660 the <tt>Function</tt> pointed to by F.
661
662 <!-- _______________________________________________________________________ -->
663 </ul><h4><a name="iterate_convert"><hr size=0>Turning an iterator into a class
664 pointer (and vice-versa) </h4><ul>
665
666 Sometimes, it'll be useful to grab a reference (or pointer) to a class
667 instance when all you've got at hand is an iterator.  Well, extracting
668 a reference or a pointer from an iterator is very straightforward.
669 Assuming that <tt>i</tt> is a <tt>BasicBlock::iterator</tt> and
670 <tt>j</tt> is a <tt>BasicBlock::const_iterator</tt>:
671
672 <pre>
673     Instruction&amp; inst = *i;   // grab reference to instruction reference
674     Instruction* pinst = &amp;*i; // grab pointer to instruction reference
675     const Instruction&amp; inst = *j;
676 </pre>
677 However, the iterators you'll be working with in the LLVM framework
678 are special: they will automatically convert to a ptr-to-instance type
679 whenever they need to.  Instead of dereferencing the iterator and then
680 taking the address of the result, you can simply assign the iterator
681 to the proper pointer type and you get the dereference and address-of
682 operation as a result of the assignment (behind the scenes, this is a
683 result of overloading casting mechanisms).  Thus the last line of the
684 last example,
685
686 <pre>Instruction* pinst = &amp;*i;</pre>
687
688 is semantically equivalent to
689
690 <pre>Instruction* pinst = i;</pre>
691
692 It's also possible to turn a class pointer into the corresponding
693 iterator.  Usually, this conversion is quite inexpensive.  The
694 following code snippet illustrates use of the conversion constructors
695 provided by LLVM iterators.  By using these, you can explicitly grab
696 the iterator of something without actually obtaining it via iteration
697 over some structure:
698
699 <pre>
700 void printNextInstruction(Instruction* inst) {
701     BasicBlock::iterator it(inst);
702     ++it; // after this line, it refers to the instruction after *inst.
703     if (it != inst-&gt;getParent()-&gt;end()) cerr &lt;&lt; *it &lt;&lt; "\n";
704 }
705 </pre>
706 Of course, this example is strictly pedagogical, because it'd be much
707 better to explicitly grab the next instruction directly from inst.
708
709
710 <!--_______________________________________________________________________-->
711 </ul><h4><a name="iterate_complex"><hr size=0>Finding call sites: a slightly
712 more complex example </h4><ul>
713
714 Say that you're writing a FunctionPass and would like to count all the
715 locations in the entire module (that is, across every
716 <tt>Function</tt>) where a certain function (i.e., some
717 <tt>Function</tt>*) is already in scope.  As you'll learn later, you may
718 want to use an <tt>InstVisitor</tt> to accomplish this in a much more
719 straightforward manner, but this example will allow us to explore how
720 you'd do it if you didn't have <tt>InstVisitor</tt> around.  In
721 pseudocode, this is what we want to do:
722
723 <pre>
724 initialize callCounter to zero
725 for each Function f in the Module
726     for each BasicBlock b in f
727       for each Instruction i in b
728         if (i is a CallInst and calls the given function)
729           increment callCounter
730 </pre>
731
732 And the actual code is (remember, since we're writing a
733 <tt>FunctionPass</tt>, our <tt>FunctionPass</tt>-derived class simply
734 has to override the <tt>runOnFunction</tt> method...):
735
736 <pre>
737 Function* targetFunc = ...;
738
739 class OurFunctionPass : public FunctionPass {
740   public:
741     OurFunctionPass(): callCounter(0) { }
742
743     virtual runOnFunction(Function&amp; F) {
744         for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) {
745             for (BasicBlock::iterator i = b-&gt;begin(); ie = b-&gt;end(); i != ie; ++i) {
746                 if (<a href="#CallInst">CallInst</a>* callInst = <a href="#isa">dyn_cast</a>&lt;<a href="#CallInst">CallInst</a>&gt;(&amp;*i)) {
747                     // we know we've encountered a call instruction, so we
748                     // need to determine if it's a call to the
749                     // function pointed to by m_func or not.
750   
751                     if (callInst-&gt;getCalledFunction() == targetFunc)
752                         ++callCounter;
753             }
754         }
755     }
756     
757   private:
758     unsigned  callCounter;
759 };
760 </pre>
761
762
763 <!--_______________________________________________________________________-->
764 </ul><h4><a name="calls_and_invokes"><hr size=0>Treating calls and
765 invokes the same way</h4><ul>
766
767 <p>You may have noticed that the previous example was a bit
768 oversimplified in that it did not deal with call sites generated by
769 'invoke' instructions. In this, and in other situations, you may find
770 that you want to treat <tt>CallInst</tt>s and <tt>InvokeInst</tt>s the
771 same way, even though their most-specific common base class is
772 <tt>Instruction</tt>, which includes lots of less closely-related
773 things. For these cases, LLVM provides a handy wrapper class called <a
774 href="http://llvm.cs.uiuc.edu/doxygen/classCallSite.html"><tt>CallSite
775 </tt></a>.  It is essentially a wrapper around an <tt>Instruction</tt>
776 pointer, with some methods that provide functionality common to
777 <tt>CallInst</tt>s and <tt>InvokeInst</tt>s.</p>
778
779 <p>This class is supposed to have "value semantics". So it should be
780 passed by value, not by reference; it should not be dynamically
781 allocated or deallocated using <tt>operator new</tt> or <tt>operator
782 delete</tt>. It is efficiently copyable, assignable and constructable,
783 with costs equivalents to that of a bare pointer. (You will notice, if
784 you look at its definition, that it has only a single data member.)</p>
785
786
787 <!--_______________________________________________________________________-->
788 </ul><h4><a name="iterate_chains"><hr size=0>Iterating over def-use &amp;
789 use-def chains</h4><ul>
790
791 Frequently, we might have an instance of the <a
792 href="/doxygen/classValue.html">Value Class</a> and we want to
793 determine which <tt>User</tt>s use the <tt>Value</tt>.  The list of
794 all <tt>User</tt>s of a particular <tt>Value</tt> is called a
795 <i>def-use</i> chain.  For example, let's say we have a
796 <tt>Function*</tt> named <tt>F</tt> to a particular function
797 <tt>foo</tt>. Finding all of the instructions that <i>use</i>
798 <tt>foo</tt> is as simple as iterating over the <i>def-use</i> chain of
799 <tt>F</tt>:
800
801 <pre>
802 Function* F = ...;
803
804 for (Value::use_iterator i = F-&gt;use_begin(), e = F-&gt;use_end(); i != e; ++i) {
805     if (Instruction *Inst = dyn_cast&lt;Instruction&gt;(*i)) {
806         cerr &lt;&lt; "F is used in instruction:\n";
807         cerr &lt;&lt; *Inst &lt;&lt; "\n";
808     }
809 }
810 </pre>
811
812 Alternately, it's common to have an instance of the <a
813 href="/doxygen/classUser.html">User Class</a> and need to know what
814 <tt>Value</tt>s are used by it.  The list of all <tt>Value</tt>s used
815 by a <tt>User</tt> is known as a <i>use-def</i> chain.  Instances of
816 class <tt>Instruction</tt> are common <tt>User</tt>s, so we might want
817 to iterate over all of the values that a particular instruction uses
818 (that is, the operands of the particular <tt>Instruction</tt>):
819
820 <pre>
821 Instruction* pi = ...;
822
823 for (User::op_iterator i = pi-&gt;op_begin(), e = pi-&gt;op_end(); i != e; ++i) {
824     Value* v = *i;
825     ...
826 }
827 </pre>
828     
829
830 <!--
831   def-use chains ("finding all users of"): Value::use_begin/use_end
832   use-def chains ("finding all values used"): User::op_begin/op_end [op=operand]
833 -->
834
835 <!-- ======================================================================= -->
836 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
837 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
838 <font color="#EEEEFF" face="Georgia,Palatino"><b>
839 <a name="simplechanges">Making simple changes</a>
840 </b></font></td></tr></table><ul>
841
842 There are some primitive transformation operations present in the LLVM
843 infrastructure that are worth knowing about.  When performing
844 transformations, it's fairly common to manipulate the contents of
845 basic blocks.  This section describes some of the common methods for
846 doing so and gives example code.
847
848 <!--_______________________________________________________________________-->
849 </ul><h4><a name="schanges_creating"><hr size=0>Creating and inserting
850     new <tt>Instruction</tt>s</h4><ul> 
851
852 <i>Instantiating Instructions</i>
853
854 <p>Creation of <tt>Instruction</tt>s is straightforward: simply call the
855 constructor for the kind of instruction to instantiate and provide the
856 necessary parameters.  For example, an <tt>AllocaInst</tt> only
857 <i>requires</i> a (const-ptr-to) <tt>Type</tt>.  Thus:
858
859 <pre>AllocaInst* ai = new AllocaInst(Type::IntTy);</pre> 
860
861 will create an <tt>AllocaInst</tt> instance that represents the
862 allocation of one integer in the current stack frame, at runtime.
863 Each <tt>Instruction</tt> subclass is likely to have varying default
864 parameters which change the semantics of the instruction, so refer to
865 the <a href="/doxygen/classInstruction.html">doxygen documentation for
866 the subclass of Instruction</a> that you're interested in
867 instantiating.</p>
868
869 <p><i>Naming values</i></p>
870
871 <p>
872 It is very useful to name the values of instructions when you're able
873 to, as this facilitates the debugging of your transformations.  If you
874 end up looking at generated LLVM machine code, you definitely want to
875 have logical names associated with the results of instructions!  By
876 supplying a value for the <tt>Name</tt> (default) parameter of the
877 <tt>Instruction</tt> constructor, you associate a logical name with
878 the result of the instruction's execution at runtime.  For example,
879 say that I'm writing a transformation that dynamically allocates space
880 for an integer on the stack, and that integer is going to be used as
881 some kind of index by some other code.  To accomplish this, I place an
882 <tt>AllocaInst</tt> at the first point in the first
883 <tt>BasicBlock</tt> of some <tt>Function</tt>, and I'm intending to
884 use it within the same <tt>Function</tt>.  I might do:
885
886 <pre>AllocaInst* pa = new AllocaInst(Type::IntTy, 0, "indexLoc");</pre>
887
888 where <tt>indexLoc</tt> is now the logical name of the instruction's
889 execution value, which is a pointer to an integer on the runtime
890 stack.
891 </p>
892
893 <p><i>Inserting instructions</i></p>
894
895 <p>
896 There are essentially two ways to insert an <tt>Instruction</tt> into
897 an existing sequence of instructions that form a <tt>BasicBlock</tt>:
898 <ul>
899 <li>Insertion into an explicit instruction list
900
901 <p>Given a <tt>BasicBlock* pb</tt>, an <tt>Instruction* pi</tt> within
902 that <tt>BasicBlock</tt>, and a newly-created instruction
903 we wish to insert before <tt>*pi</tt>, we do the following:
904
905 <pre>
906   BasicBlock *pb = ...;
907   Instruction *pi = ...;
908   Instruction *newInst = new Instruction(...);
909   pb-&gt;getInstList().insert(pi, newInst); // inserts newInst before pi in pb
910 </pre>
911 </p>
912
913 <li>Insertion into an implicit instruction list
914 <p><tt>Instruction</tt> instances that are already in
915 <tt>BasicBlock</tt>s are implicitly associated with an existing
916 instruction list: the instruction list of the enclosing basic block.
917 Thus, we could have accomplished the same thing as the above code
918 without being given a <tt>BasicBlock</tt> by doing:
919 <pre>
920   Instruction *pi = ...;
921   Instruction *newInst = new Instruction(...);
922   pi-&gt;getParent()-&gt;getInstList().insert(pi, newInst);
923 </pre>
924 In fact, this sequence of steps occurs so frequently that the
925 <tt>Instruction</tt> class and <tt>Instruction</tt>-derived classes
926 provide constructors which take (as a default parameter) a pointer to
927 an <tt>Instruction</tt> which the newly-created <tt>Instruction</tt>
928 should precede.  That is, <tt>Instruction</tt> constructors are
929 capable of inserting the newly-created instance into the
930 <tt>BasicBlock</tt> of a provided instruction, immediately before that
931 instruction.  Using an <tt>Instruction</tt> constructor with a
932 <tt>insertBefore</tt> (default) parameter, the above code becomes:
933 <pre>
934 Instruction* pi = ...;
935 Instruction* newInst = new Instruction(..., pi);
936 </pre>
937 which is much cleaner, especially if you're creating a lot of
938 instructions and adding them to <tt>BasicBlock</tt>s.
939  </p>
940 </p>
941 </ul>
942
943 <!--_______________________________________________________________________-->
944 </ul><h4><a name="schanges_deleting"><hr size=0>Deleting
945 <tt>Instruction</tt>s</h4><ul>
946
947 Deleting an instruction from an existing sequence of instructions that form a <a
948 href="#BasicBlock"><tt>BasicBlock</tt></a> is very straightforward. First, you
949 must have a pointer to the instruction that you wish to delete.  Second, you
950 need to obtain the pointer to that instruction's basic block. You use the
951 pointer to the basic block to get its list of instructions and then use the
952 erase function to remove your instruction.<p>
953
954 For example:<p>
955
956 <pre>
957   <a href="#Instruction">Instruction</a> *I = .. ;
958   <a href="#BasicBlock">BasicBlock</a> *BB = I-&gt;getParent();
959   BB-&gt;getInstList().erase(I);
960 </pre><p>
961
962 <!--_______________________________________________________________________-->
963 </ul><h4><a name="schanges_replacing"><hr size=0>Replacing an
964     <tt>Instruction</tt> with another <tt>Value</tt></h4><ul>
965
966 <p><i>Replacing individual instructions</i></p>
967 <p>
968 Including "<a
969 href="/doxygen/BasicBlockUtils_8h-source.html">llvm/Transforms/Utils/BasicBlockUtils.h</a>" permits use of two very useful replace functions:
970 <tt>ReplaceInstWithValue</tt> and <tt>ReplaceInstWithInst</tt>.  
971
972 <ul>
973
974 <li><tt>ReplaceInstWithValue</tt>
975
976 <p>This function replaces all uses (within a basic block) of a given
977 instruction with a value, and then removes the original instruction.
978 The following example illustrates the replacement of the result of a
979 particular <tt>AllocaInst</tt> that allocates memory for a single
980 integer with an null pointer to an integer.</p>
981
982 <pre>
983 AllocaInst* instToReplace = ...;
984 BasicBlock::iterator ii(instToReplace);
985 ReplaceInstWithValue(instToReplace-&gt;getParent()-&gt;getInstList(), ii,
986                      Constant::getNullValue(PointerType::get(Type::IntTy)));
987 </pre>
988
989 <li><tt>ReplaceInstWithInst</tt>
990
991 <p>This function replaces a particular instruction with another
992 instruction.  The following example illustrates the replacement of one
993 <tt>AllocaInst</tt> with another.<p>
994
995 <pre>
996 AllocaInst* instToReplace = ...;
997 BasicBlock::iterator ii(instToReplace);
998 ReplaceInstWithInst(instToReplace-&gt;getParent()-&gt;getInstList(), ii,
999                     new AllocaInst(Type::IntTy, 0, "ptrToReplacedInt"));
1000 </pre>
1001
1002 </ul>
1003 <p><i>Replacing multiple uses of <tt>User</tt>s and
1004                     <tt>Value</tt>s</i></p>
1005   
1006 You can use <tt>Value::replaceAllUsesWith</tt> and
1007 <tt>User::replaceUsesOfWith</tt> to change more than one use at a
1008 time.  See the doxygen documentation for the <a
1009 href="/doxygen/classValue.html">Value Class</a> and <a
1010 href="/doxygen/classUser.html">User Class</a>, respectively, for more
1011 information.
1012
1013 <!-- Value::replaceAllUsesWith User::replaceUsesOfWith Point out:
1014 include/llvm/Transforms/Utils/ especially BasicBlockUtils.h with:
1015 ReplaceInstWithValue, ReplaceInstWithInst
1016 -->
1017
1018 <!-- *********************************************************************** -->
1019 </ul><table width="100%" bgcolor="#330077" border=0 cellpadding=4 cellspacing=0>
1020 <tr><td align=center><font color="#EEEEFF" size=+2 face="Georgia,Palatino"><b>
1021 <a name="coreclasses">The Core LLVM Class Hierarchy Reference
1022 </b></font></td></tr></table><ul>
1023 <!-- *********************************************************************** -->
1024
1025 The Core LLVM classes are the primary means of representing the program being
1026 inspected or transformed.  The core LLVM classes are defined in header files in
1027 the <tt>include/llvm/</tt> directory, and implemented in the <tt>lib/VMCore</tt>
1028 directory.<p>
1029
1030
1031 <!-- ======================================================================= -->
1032 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1033 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1034 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1035 <a name="Value">The <tt>Value</tt> class</a>
1036 </b></font></td></tr></table><ul>
1037
1038 <tt>#include "<a href="/doxygen/Value_8h-source.html">llvm/Value.h</a>"</tt></b><br>
1039 doxygen info: <a href="/doxygen/classValue.html">Value Class</a><p>
1040
1041
1042 The <tt>Value</tt> class is the most important class in LLVM Source base.  It
1043 represents a typed value that may be used (among other things) as an operand to
1044 an instruction.  There are many different types of <tt>Value</tt>s, such as <a
1045 href="#Constant"><tt>Constant</tt></a>s, <a
1046 href="#Argument"><tt>Argument</tt></a>s, and even <a
1047 href="#Instruction"><tt>Instruction</tt></a>s and <a
1048 href="#Function"><tt>Function</tt></a>s are <tt>Value</tt>s.<p>
1049
1050 A particular <tt>Value</tt> may be used many times in the LLVM representation
1051 for a program.  For example, an incoming argument to a function (represented
1052 with an instance of the <a href="#Argument">Argument</a> class) is "used" by
1053 every instruction in the function that references the argument.  To keep track
1054 of this relationship, the <tt>Value</tt> class keeps a list of all of the <a
1055 href="#User"><tt>User</tt></a>s that is using it (the <a
1056 href="#User"><tt>User</tt></a> class is a base class for all nodes in the LLVM
1057 graph that can refer to <tt>Value</tt>s).  This use list is how LLVM represents
1058 def-use information in the program, and is accessible through the <tt>use_</tt>*
1059 methods, shown below.<p>
1060
1061 Because LLVM is a typed representation, every LLVM <tt>Value</tt> is typed, and
1062 this <a href="#Type">Type</a> is available through the <tt>getType()</tt>
1063 method.  <a name="#nameWarning">In addition, all LLVM values can be named.  The
1064 "name" of the <tt>Value</tt> is symbolic string printed in the LLVM code:<p>
1065
1066 <pre>
1067    %<b>foo</b> = add int 1, 2
1068 </pre>
1069
1070 The name of this instruction is "foo".  <b>NOTE</b> that the name of any value
1071 may be missing (an empty string), so names should <b>ONLY</b> be used for
1072 debugging (making the source code easier to read, debugging printouts), they
1073 should not be used to keep track of values or map between them.  For this
1074 purpose, use a <tt>std::map</tt> of pointers to the <tt>Value</tt> itself
1075 instead.<p>
1076
1077 One important aspect of LLVM is that there is no distinction between an SSA
1078 variable and the operation that produces it.  Because of this, any reference to
1079 the value produced by an instruction (or the value available as an incoming
1080 argument, for example) is represented as a direct pointer to the class that
1081 represents this value.  Although this may take some getting used to, it
1082 simplifies the representation and makes it easier to manipulate.<p>
1083
1084
1085 <!-- _______________________________________________________________________ -->
1086 </ul><h4><a name="m_Value"><hr size=0>Important Public Members of
1087 the <tt>Value</tt> class</h4><ul>
1088
1089 <li><tt>Value::use_iterator</tt> - Typedef for iterator over the use-list<br>
1090     <tt>Value::use_const_iterator</tt>
1091                  - Typedef for const_iterator over the use-list<br>
1092     <tt>unsigned use_size()</tt> - Returns the number of users of the value.<br>
1093     <tt>bool use_empty()</tt> - Returns true if there are no users.<br>
1094     <tt>use_iterator use_begin()</tt>
1095                  - Get an iterator to the start of the use-list.<br>
1096     <tt>use_iterator use_end()</tt>
1097                  - Get an iterator to the end of the use-list.<br>
1098     <tt><a href="#User">User</a> *use_back()</tt>
1099                  - Returns the last element in the list.<p>
1100
1101 These methods are the interface to access the def-use information in LLVM.  As with all other iterators in LLVM, the naming conventions follow the conventions defined by the <a href="#stl">STL</a>.<p>
1102
1103 <li><tt><a href="#Type">Type</a> *getType() const</tt><p>
1104 This method returns the Type of the Value.
1105
1106 <li><tt>bool hasName() const</tt><br>
1107     <tt>std::string getName() const</tt><br>
1108     <tt>void setName(const std::string &amp;Name)</tt><p>
1109
1110 This family of methods is used to access and assign a name to a <tt>Value</tt>,
1111 be aware of the <a href="#nameWarning">precaution above</a>.<p>
1112
1113
1114 <li><tt>void replaceAllUsesWith(Value *V)</tt><p>
1115
1116 This method traverses the use list of a <tt>Value</tt> changing all <a
1117 href="#User"><tt>User</tt>s</a> of the current value to refer to "<tt>V</tt>"
1118 instead.  For example, if you detect that an instruction always produces a
1119 constant value (for example through constant folding), you can replace all uses
1120 of the instruction with the constant like this:<p>
1121
1122 <pre>
1123   Inst-&gt;replaceAllUsesWith(ConstVal);
1124 </pre><p>
1125
1126
1127
1128 <!-- ======================================================================= -->
1129 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1130 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1131 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1132 <a name="User">The <tt>User</tt> class</a>
1133 </b></font></td></tr></table><ul>
1134
1135 <tt>#include "<a href="/doxygen/User_8h-source.html">llvm/User.h</a>"</tt></b><br>
1136 doxygen info: <a href="/doxygen/classUser.html">User Class</a><br>
1137 Superclass: <a href="#Value"><tt>Value</tt></a><p>
1138
1139
1140 The <tt>User</tt> class is the common base class of all LLVM nodes that may
1141 refer to <a href="#Value"><tt>Value</tt></a>s.  It exposes a list of "Operands"
1142 that are all of the <a href="#Value"><tt>Value</tt></a>s that the User is
1143 referring to.  The <tt>User</tt> class itself is a subclass of
1144 <tt>Value</tt>.<p>
1145
1146 The operands of a <tt>User</tt> point directly to the LLVM <a
1147 href="#Value"><tt>Value</tt></a> that it refers to.  Because LLVM uses Static
1148 Single Assignment (SSA) form, there can only be one definition referred to,
1149 allowing this direct connection.  This connection provides the use-def
1150 information in LLVM.<p>
1151
1152 <!-- _______________________________________________________________________ -->
1153 </ul><h4><a name="m_User"><hr size=0>Important Public Members of
1154 the <tt>User</tt> class</h4><ul>
1155
1156 The <tt>User</tt> class exposes the operand list in two ways: through an index
1157 access interface and through an iterator based interface.<p>
1158
1159 <li><tt>Value *getOperand(unsigned i)</tt><br>
1160     <tt>unsigned getNumOperands()</tt><p>
1161
1162 These two methods expose the operands of the <tt>User</tt> in a convenient form
1163 for direct access.<p>
1164
1165 <li><tt>User::op_iterator</tt> - Typedef for iterator over the operand list<br>
1166     <tt>User::op_const_iterator</tt>
1167     <tt>use_iterator op_begin()</tt>
1168                  - Get an iterator to the start of the operand list.<br>
1169     <tt>use_iterator op_end()</tt>
1170                  - Get an iterator to the end of the operand list.<p>
1171
1172 Together, these methods make up the iterator based interface to the operands of
1173 a <tt>User</tt>.<p>
1174
1175
1176
1177 <!-- ======================================================================= -->
1178 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1179 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1180 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1181 <a name="Instruction">The <tt>Instruction</tt> class</a>
1182 </b></font></td></tr></table><ul>
1183
1184 <tt>#include "<a
1185 href="/doxygen/Instruction_8h-source.html">llvm/Instruction.h</a>"</tt></b><br>
1186 doxygen info: <a href="/doxygen/classInstruction.html">Instruction Class</a><br>
1187 Superclasses: <a href="#User"><tt>User</tt></a>, <a
1188 href="#Value"><tt>Value</tt></a><p>
1189
1190 The <tt>Instruction</tt> class is the common base class for all LLVM
1191 instructions.  It provides only a few methods, but is a very commonly used
1192 class.  The primary data tracked by the <tt>Instruction</tt> class itself is the
1193 opcode (instruction type) and the parent <a
1194 href="#BasicBlock"><tt>BasicBlock</tt></a> the <tt>Instruction</tt> is embedded
1195 into.  To represent a specific type of instruction, one of many subclasses of
1196 <tt>Instruction</tt> are used.<p>
1197
1198 Because the <tt>Instruction</tt> class subclasses the <a
1199 href="#User"><tt>User</tt></a> class, its operands can be accessed in the same
1200 way as for other <a href="#User"><tt>User</tt></a>s (with the
1201 <tt>getOperand()</tt>/<tt>getNumOperands()</tt> and
1202 <tt>op_begin()</tt>/<tt>op_end()</tt> methods).<p>
1203
1204 An important file for the <tt>Instruction</tt> class is the
1205 <tt>llvm/Instruction.def</tt> file.  This file contains some meta-data about the
1206 various different types of instructions in LLVM.  It describes the enum values
1207 that are used as opcodes (for example <tt>Instruction::Add</tt> and
1208 <tt>Instruction::SetLE</tt>), as well as the concrete sub-classes of
1209 <tt>Instruction</tt> that implement the instruction (for example <tt><a
1210 href="#BinaryOperator">BinaryOperator</a></tt> and <tt><a
1211 href="#SetCondInst">SetCondInst</a></tt>).  Unfortunately, the use of macros in
1212 this file confused doxygen, so these enum values don't show up correctly in the
1213 <a href="/doxygen/classInstruction.html">doxygen output</a>.<p>
1214
1215
1216 <!-- _______________________________________________________________________ -->
1217 </ul><h4><a name="m_Instruction"><hr size=0>Important Public Members of
1218 the <tt>Instruction</tt> class</h4><ul>
1219
1220 <li><tt><a href="#BasicBlock">BasicBlock</a> *getParent()</tt><p>
1221
1222 Returns the <a href="#BasicBlock"><tt>BasicBlock</tt></a> that this
1223 <tt>Instruction</tt> is embedded into.<p>
1224
1225 <li><tt>bool mayWriteToMemory()</tt><p>
1226
1227 Returns true if the instruction writes to memory, i.e. it is a <tt>call</tt>,
1228 <tt>free</tt>, <tt>invoke</tt>, or <tt>store</tt>.<p>
1229
1230 <li><tt>unsigned getOpcode()</tt><p>
1231
1232 Returns the opcode for the <tt>Instruction</tt>.<p>
1233
1234 <li><tt><a href="#Instruction">Instruction</a> *clone() const</tt><p>
1235
1236 Returns another instance of the specified instruction, identical in all ways to
1237 the original except that the instruction has no parent (ie it's not embedded
1238 into a <a href="#BasicBlock"><tt>BasicBlock</tt></a>), and it has no name.<p>
1239
1240
1241
1242 <!--
1243
1244 \subsection{Subclasses of Instruction :} 
1245 \begin{itemize}
1246 <li>BinaryOperator : This subclass of Instruction defines a general interface to the all the instructions involvong  binary operators in LLVM.
1247         \begin{itemize}
1248         <li><tt>bool swapOperands()</tt>: Exchange the two operands to this instruction. If the instruction cannot be reversed (i.e. if it's a Div), it returns true. 
1249         \end{itemize}
1250 <li>TerminatorInst : This subclass of Instructions defines an interface for all instructions that can terminate a BasicBlock.
1251         \begin{itemize}
1252          <li> <tt>unsigned getNumSuccessors()</tt>: Returns the number of successors for this terminator instruction.
1253         <li><tt>BasicBlock *getSuccessor(unsigned i)</tt>: As the name suggests returns the ith successor BasicBlock.
1254         <li><tt>void setSuccessor(unsigned i, BasicBlock *B)</tt>: sets BasicBlock B as the ith succesor to this terminator instruction.
1255         \end{itemize}
1256
1257 <li>PHINode : This represents the PHI instructions in the SSA form. 
1258         \begin{itemize}
1259         <li><tt> unsigned getNumIncomingValues()</tt>: Returns the number of incoming edges to this PHI node.
1260         <li><tt> Value *getIncomingValue(unsigned i)</tt>: Returns the ith incoming Value.
1261         <li><tt>void setIncomingValue(unsigned i, Value *V)</tt>: Sets the ith incoming Value as V 
1262         <li><tt>BasicBlock *getIncomingBlock(unsigned i)</tt>: Returns the Basic Block corresponding to the ith incoming Value.
1263         <li><tt> void addIncoming(Value *D, BasicBlock *BB)</tt>: 
1264         Add an incoming value to the end of the PHI list
1265         <li><tt> int getBasicBlockIndex(const BasicBlock *BB) const</tt>: 
1266         Returns the first index of the specified basic block in the value list for this PHI.  Returns -1 if no instance.
1267         \end{itemize}
1268 <li>CastInst : In LLVM all casts have to be done through explicit cast instructions. CastInst defines the interface to the cast instructions.
1269 <li>CallInst : This defines an interface to the call instruction in LLVM. ARguments to the function are nothing but operands of the instruction.
1270         \begin{itemize}
1271         <li>: <tt>Function *getCalledFunction()</tt>: Returns a handle to the function that is being called by this Function. 
1272         \end{itemize}
1273 <li>LoadInst, StoreInst, GetElemPtrInst : These subclasses represent load, store and getelementptr instructions in LLVM.
1274         \begin{itemize}
1275         <li><tt>Value * getPointerOperand()</tt>: Returns the Pointer Operand which is typically the 0th operand.
1276         \end{itemize}
1277 <li>BranchInst : This is a subclass of TerminatorInst and defines the interface for conditional and unconditional branches in LLVM.
1278         \begin{itemize}
1279         <li><tt>bool isConditional()</tt>: Returns true if the branch is a conditional branch else returns false
1280         <li> <tt>Value *getCondition()</tt>: Returns the condition if it is a conditional branch else returns null.
1281         <li> <tt>void setUnconditionalDest(BasicBlock *Dest)</tt>: Changes the current branch to an unconditional one targetting the specified block.
1282         \end{itemize}
1283
1284 \end{itemize}
1285
1286 -->
1287
1288
1289 <!-- ======================================================================= -->
1290 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1291 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1292 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1293 <a name="BasicBlock">The <tt>BasicBlock</tt> class</a>
1294 </b></font></td></tr></table><ul>
1295
1296 <tt>#include "<a
1297 href="/doxygen/BasicBlock_8h-source.html">llvm/BasicBlock.h</a>"</tt></b><br>
1298 doxygen info: <a href="/doxygen/classBasicBlock.html">BasicBlock Class</a><br>
1299 Superclass: <a href="#Value"><tt>Value</tt></a><p>
1300
1301
1302 This class represents a single entry multiple exit section of the code, commonly
1303 known as a basic block by the compiler community.  The <tt>BasicBlock</tt> class
1304 maintains a list of <a href="#Instruction"><tt>Instruction</tt></a>s, which form
1305 the body of the block.  Matching the language definition, the last element of
1306 this list of instructions is always a terminator instruction (a subclass of the
1307 <a href="#TerminatorInst"><tt>TerminatorInst</tt></a> class).<p>
1308
1309 In addition to tracking the list of instructions that make up the block, the
1310 <tt>BasicBlock</tt> class also keeps track of the <a
1311 href="#Function"><tt>Function</tt></a> that it is embedded into.<p>
1312
1313 Note that <tt>BasicBlock</tt>s themselves are <a
1314 href="#Value"><tt>Value</tt></a>s, because they are referenced by instructions
1315 like branches and can go in the switch tables.  <tt>BasicBlock</tt>s have type
1316 <tt>label</tt>.<p>
1317
1318
1319 <!-- _______________________________________________________________________ -->
1320 </ul><h4><a name="m_BasicBlock"><hr size=0>Important Public Members of
1321 the <tt>BasicBlock</tt> class</h4><ul>
1322
1323 <li><tt>BasicBlock(const std::string &amp;Name = "", <a 
1324 href="#Function">Function</a> *Parent = 0)</tt><p>
1325
1326 The <tt>BasicBlock</tt> constructor is used to create new basic blocks for
1327 insertion into a function.  The constructor simply takes a name for the new
1328 block, and optionally a <a href="#Function"><tt>Function</tt></a> to insert it
1329 into.  If the <tt>Parent</tt> parameter is specified, the new
1330 <tt>BasicBlock</tt> is automatically inserted at the end of the specified <a
1331 href="#Function"><tt>Function</tt></a>, if not specified, the BasicBlock must be
1332 manually inserted into the <a href="#Function"><tt>Function</tt></a>.<p>
1333
1334 <li><tt>BasicBlock::iterator</tt> - Typedef for instruction list iterator<br>
1335     <tt>BasicBlock::const_iterator</tt> - Typedef for const_iterator.<br>
1336     <tt>begin()</tt>, <tt>end()</tt>, <tt>front()</tt>, <tt>back()</tt>,
1337     <tt>size()</tt>, <tt>empty()</tt>, <tt>rbegin()</tt>, <tt>rend()</tt><p>
1338
1339 These methods and typedefs are forwarding functions that have the same semantics
1340 as the standard library methods of the same names.  These methods expose the
1341 underlying instruction list of a basic block in a way that is easy to
1342 manipulate.  To get the full complement of container operations (including
1343 operations to update the list), you must use the <tt>getInstList()</tt>
1344 method.<p>
1345
1346 <li><tt>BasicBlock::InstListType &amp;getInstList()</tt><p>
1347
1348 This method is used to get access to the underlying container that actually
1349 holds the Instructions.  This method must be used when there isn't a forwarding
1350 function in the <tt>BasicBlock</tt> class for the operation that you would like
1351 to perform.  Because there are no forwarding functions for "updating"
1352 operations, you need to use this if you want to update the contents of a
1353 <tt>BasicBlock</tt>.<p>
1354
1355 <li><tt><A href="#Function">Function</a> *getParent()</tt><p>
1356
1357 Returns a pointer to <a href="#Function"><tt>Function</tt></a> the block is
1358 embedded into, or a null pointer if it is homeless.<p>
1359
1360 <li><tt><a href="#TerminatorInst">TerminatorInst</a> *getTerminator()</tt><p>
1361
1362 Returns a pointer to the terminator instruction that appears at the end of the
1363 <tt>BasicBlock</tt>.  If there is no terminator instruction, or if the last
1364 instruction in the block is not a terminator, then a null pointer is
1365 returned.<p>
1366
1367
1368 <!-- ======================================================================= -->
1369 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1370 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1371 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1372 <a name="GlobalValue">The <tt>GlobalValue</tt> class</a>
1373 </b></font></td></tr></table><ul>
1374
1375 <tt>#include "<a
1376 href="/doxygen/GlobalValue_8h-source.html">llvm/GlobalValue.h</a>"</tt></b><br>
1377 doxygen info: <a href="/doxygen/classGlobalValue.html">GlobalValue Class</a><br>
1378 Superclasses: <a href="#User"><tt>User</tt></a>, <a
1379 href="#Value"><tt>Value</tt></a><p>
1380
1381 Global values (<A href="#GlobalVariable"><tt>GlobalVariable</tt></a>s or <a
1382 href="#Function"><tt>Function</tt></a>s) are the only LLVM values that are
1383 visible in the bodies of all <a href="#Function"><tt>Function</tt></a>s.
1384 Because they are visible at global scope, they are also subject to linking with
1385 other globals defined in different translation units.  To control the linking
1386 process, <tt>GlobalValue</tt>s know their linkage rules.  Specifically,
1387 <tt>GlobalValue</tt>s know whether they have internal or external linkage.<p>
1388
1389 If a <tt>GlobalValue</tt> has internal linkage (equivalent to being
1390 <tt>static</tt> in C), it is not visible to code outside the current translation
1391 unit, and does not participate in linking.  If it has external linkage, it is
1392 visible to external code, and does participate in linking.  In addition to
1393 linkage information, <tt>GlobalValue</tt>s keep track of which <a
1394 href="#Module"><tt>Module</tt></a> they are currently part of.<p>
1395
1396 Because <tt>GlobalValue</tt>s are memory objects, they are always referred to by
1397 their address.  As such, the <a href="#Type"><tt>Type</tt></a> of a global is
1398 always a pointer to its contents.  This is explained in the LLVM Language
1399 Reference Manual.<p>
1400
1401
1402 <!-- _______________________________________________________________________ -->
1403 </ul><h4><a name="m_GlobalValue"><hr size=0>Important Public Members of
1404 the <tt>GlobalValue</tt> class</h4><ul>
1405
1406 <li><tt>bool hasInternalLinkage() const</tt><br>
1407     <tt>bool hasExternalLinkage() const</tt><br>
1408     <tt>void setInternalLinkage(bool HasInternalLinkage)</tt><p>
1409
1410 These methods manipulate the linkage characteristics of the
1411 <tt>GlobalValue</tt>.<p>
1412
1413 <li><tt><a href="#Module">Module</a> *getParent()</tt><p>
1414
1415 This returns the <a href="#Module"><tt>Module</tt></a> that the GlobalValue is
1416 currently embedded into.<p>
1417
1418
1419
1420 <!-- ======================================================================= -->
1421 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1422 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1423 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1424 <a name="Function">The <tt>Function</tt> class</a>
1425 </b></font></td></tr></table><ul>
1426
1427 <tt>#include "<a
1428 href="/doxygen/Function_8h-source.html">llvm/Function.h</a>"</tt></b><br>
1429 doxygen info: <a href="/doxygen/classFunction.html">Function Class</a><br>
1430 Superclasses: <a href="#GlobalValue"><tt>GlobalValue</tt></a>, <a
1431 href="#User"><tt>User</tt></a>, <a href="#Value"><tt>Value</tt></a><p>
1432
1433 The <tt>Function</tt> class represents a single procedure in LLVM.  It is
1434 actually one of the more complex classes in the LLVM heirarchy because it must
1435 keep track of a large amount of data.  The <tt>Function</tt> class keeps track
1436 of a list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s, a list of formal <a
1437 href="#Argument"><tt>Argument</tt></a>s, and a <a
1438 href="#SymbolTable"><tt>SymbolTable</tt></a>.<p>
1439
1440 The list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s is the most commonly
1441 used part of <tt>Function</tt> objects.  The list imposes an implicit ordering
1442 of the blocks in the function, which indicate how the code will be layed out by
1443 the backend.  Additionally, the first <a
1444 href="#BasicBlock"><tt>BasicBlock</tt></a> is the implicit entry node for the
1445 <tt>Function</tt>.  It is not legal in LLVM explicitly branch to this initial
1446 block.  There are no implicit exit nodes, and in fact there may be multiple exit
1447 nodes from a single <tt>Function</tt>.  If the <a
1448 href="#BasicBlock"><tt>BasicBlock</tt></a> list is empty, this indicates that
1449 the <tt>Function</tt> is actually a function declaration: the actual body of the
1450 function hasn't been linked in yet.<p>
1451
1452 In addition to a list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s, the
1453 <tt>Function</tt> class also keeps track of the list of formal <a
1454 href="#Argument"><tt>Argument</tt></a>s that the function receives.  This
1455 container manages the lifetime of the <a href="#Argument"><tt>Argument</tt></a>
1456 nodes, just like the <a href="#BasicBlock"><tt>BasicBlock</tt></a> list does for
1457 the <a href="#BasicBlock"><tt>BasicBlock</tt></a>s.<p>
1458
1459 The <a href="#SymbolTable"><tt>SymbolTable</tt></a> is a very rarely used LLVM
1460 feature that is only used when you have to look up a value by name.  Aside from
1461 that, the <a href="#SymbolTable"><tt>SymbolTable</tt></a> is used internally to
1462 make sure that there are not conflicts between the names of <a
1463 href="#Instruction"><tt>Instruction</tt></a>s, <a
1464 href="#BasicBlock"><tt>BasicBlock</tt></a>s, or <a
1465 href="#Argument"><tt>Argument</tt></a>s in the function body.<p>
1466
1467
1468 <!-- _______________________________________________________________________ -->
1469 </ul><h4><a name="m_Function"><hr size=0>Important Public Members of
1470 the <tt>Function</tt> class</h4><ul>
1471
1472 <li><tt>Function(const <a href="#FunctionType">FunctionType</a> *Ty, bool isInternal, const std::string &amp;N = "")</tt><p>
1473
1474 Constructor used when you need to create new <tt>Function</tt>s to add the the
1475 program.  The constructor must specify the type of the function to create and
1476 whether or not it should start out with internal or external linkage.<p>
1477
1478 <li><tt>bool isExternal()</tt><p>
1479
1480 Return whether or not the <tt>Function</tt> has a body defined.  If the function
1481 is "external", it does not have a body, and thus must be resolved by linking
1482 with a function defined in a different translation unit.<p>
1483
1484
1485 <li><tt>Function::iterator</tt> - Typedef for basic block list iterator<br>
1486     <tt>Function::const_iterator</tt> - Typedef for const_iterator.<br>
1487     <tt>begin()</tt>, <tt>end()</tt>, <tt>front()</tt>, <tt>back()</tt>,
1488     <tt>size()</tt>, <tt>empty()</tt>, <tt>rbegin()</tt>, <tt>rend()</tt><p>
1489
1490 These are forwarding methods that make it easy to access the contents of a
1491 <tt>Function</tt> object's <a href="#BasicBlock"><tt>BasicBlock</tt></a>
1492 list.<p>
1493
1494 <li><tt>Function::BasicBlockListType &amp;getBasicBlockList()</tt><p>
1495
1496 Returns the list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s.  This is
1497 necessary to use when you need to update the list or perform a complex action
1498 that doesn't have a forwarding method.<p>
1499
1500
1501 <li><tt>Function::aiterator</tt> - Typedef for the argument list iterator<br>
1502     <tt>Function::const_aiterator</tt> - Typedef for const_iterator.<br>
1503     <tt>abegin()</tt>, <tt>aend()</tt>, <tt>afront()</tt>, <tt>aback()</tt>,
1504     <tt>asize()</tt>, <tt>aempty()</tt>, <tt>arbegin()</tt>, <tt>arend()</tt><p>
1505
1506 These are forwarding methods that make it easy to access the contents of a
1507 <tt>Function</tt> object's <a href="#Argument"><tt>Argument</tt></a> list.<p>
1508
1509 <li><tt>Function::ArgumentListType &amp;getArgumentList()</tt><p>
1510
1511 Returns the list of <a href="#Argument"><tt>Argument</tt></a>s.  This is
1512 necessary to use when you need to update the list or perform a complex action
1513 that doesn't have a forwarding method.<p>
1514
1515
1516
1517 <li><tt><a href="#BasicBlock">BasicBlock</a> &getEntryBlock()</tt><p>
1518
1519 Returns the entry <a href="#BasicBlock"><tt>BasicBlock</tt></a> for the
1520 function.  Because the entry block for the function is always the first block,
1521 this returns the first block of the <tt>Function</tt>.<p>
1522
1523 <li><tt><a href="#Type">Type</a> *getReturnType()</tt><br>
1524     <tt><a href="#FunctionType">FunctionType</a> *getFunctionType()</tt><p>
1525
1526 This traverses the <a href="#Type"><tt>Type</tt></a> of the <tt>Function</tt>
1527 and returns the return type of the function, or the <a
1528 href="#FunctionType"><tt>FunctionType</tt></a> of the actual function.<p>
1529
1530 <li><tt><a href="#SymbolTable">SymbolTable</a> *getSymbolTable()</tt><p>
1531
1532 Return a pointer to the <a href="#SymbolTable"><tt>SymbolTable</tt></a> for this
1533 <tt>Function</tt>.<p>
1534
1535
1536
1537 <!-- ======================================================================= -->
1538 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1539 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1540 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1541 <a name="GlobalVariable">The <tt>GlobalVariable</tt> class</a>
1542 </b></font></td></tr></table><ul>
1543
1544 <tt>#include "<a
1545 href="/doxygen/GlobalVariable_8h-source.html">llvm/GlobalVariable.h</a>"</tt></b><br>
1546 doxygen info: <a href="/doxygen/classGlobalVariable.html">GlobalVariable Class</a><br>
1547 Superclasses: <a href="#GlobalValue"><tt>GlobalValue</tt></a>, <a
1548 href="#User"><tt>User</tt></a>, <a href="#Value"><tt>Value</tt></a><p>
1549
1550 Global variables are represented with the (suprise suprise)
1551 <tt>GlobalVariable</tt> class.  Like functions, <tt>GlobalVariable</tt>s are
1552 also subclasses of <a href="#GlobalValue"><tt>GlobalValue</tt></a>, and as such
1553 are always referenced by their address (global values must live in memory, so
1554 their "name" refers to their address).  Global variables may have an initial
1555 value (which must be a <a href="#Constant"><tt>Constant</tt></a>), and if they
1556 have an initializer, they may be marked as "constant" themselves (indicating
1557 that their contents never change at runtime).<p>
1558
1559
1560 <!-- _______________________________________________________________________ -->
1561 </ul><h4><a name="m_GlobalVariable"><hr size=0>Important Public Members of the
1562 <tt>GlobalVariable</tt> class</h4><ul>
1563
1564 <li><tt>GlobalVariable(const <a href="#Type">Type</a> *Ty, bool isConstant, bool
1565 isInternal, <a href="#Constant">Constant</a> *Initializer = 0, const std::string
1566 &amp;Name = "")</tt><p>
1567
1568 Create a new global variable of the specified type.  If <tt>isConstant</tt> is
1569 true then the global variable will be marked as unchanging for the program, and
1570 if <tt>isInternal</tt> is true the resultant global variable will have internal
1571 linkage.  Optionally an initializer and name may be specified for the global variable as well.<p>
1572
1573
1574 <li><tt>bool isConstant() const</tt><p>
1575
1576 Returns true if this is a global variable is known not to be modified at
1577 runtime.<p>
1578
1579
1580 <li><tt>bool hasInitializer()</tt><p>
1581
1582 Returns true if this <tt>GlobalVariable</tt> has an intializer.<p>
1583
1584
1585 <li><tt><a href="#Constant">Constant</a> *getInitializer()</tt><p>
1586
1587 Returns the intial value for a <tt>GlobalVariable</tt>.  It is not legal to call
1588 this method if there is no initializer.<p>
1589
1590
1591 <!-- ======================================================================= -->
1592 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1593 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1594 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1595 <a name="Module">The <tt>Module</tt> class</a>
1596 </b></font></td></tr></table><ul>
1597
1598 <tt>#include "<a
1599 href="/doxygen/Module_8h-source.html">llvm/Module.h</a>"</tt></b><br>
1600 doxygen info: <a href="/doxygen/classModule.html">Module Class</a><p>
1601
1602 The <tt>Module</tt> class represents the top level structure present in LLVM
1603 programs.  An LLVM module is effectively either a translation unit of the
1604 original program or a combination of several translation units merged by the
1605 linker.  The <tt>Module</tt> class keeps track of a list of <a
1606 href="#Function"><tt>Function</tt></a>s, a list of <a
1607 href="#GlobalVariable"><tt>GlobalVariable</tt></a>s, and a <a
1608 href="#SymbolTable"><tt>SymbolTable</tt></a>.  Additionally, it contains a few
1609 helpful member functions that try to make common operations easy.<p>
1610
1611
1612 <!-- _______________________________________________________________________ -->
1613 </ul><h4><a name="m_Module"><hr size=0>Important Public Members of the
1614 <tt>Module</tt> class</h4><ul>
1615
1616 <li><tt>Module::iterator</tt> - Typedef for function list iterator<br>
1617     <tt>Module::const_iterator</tt> - Typedef for const_iterator.<br>
1618     <tt>begin()</tt>, <tt>end()</tt>, <tt>front()</tt>, <tt>back()</tt>,
1619     <tt>size()</tt>, <tt>empty()</tt>, <tt>rbegin()</tt>, <tt>rend()</tt><p>
1620
1621 These are forwarding methods that make it easy to access the contents of a
1622 <tt>Module</tt> object's <a href="#Function"><tt>Function</tt></a>
1623 list.<p>
1624
1625 <li><tt>Module::FunctionListType &amp;getFunctionList()</tt><p>
1626
1627 Returns the list of <a href="#Function"><tt>Function</tt></a>s.  This is
1628 necessary to use when you need to update the list or perform a complex action
1629 that doesn't have a forwarding method.<p>
1630
1631 <!--  Global Variable -->
1632 <hr size=0>
1633
1634 <li><tt>Module::giterator</tt> - Typedef for global variable list iterator<br>
1635     <tt>Module::const_giterator</tt> - Typedef for const_iterator.<br>
1636     <tt>gbegin()</tt>, <tt>gend()</tt>, <tt>gfront()</tt>, <tt>gback()</tt>,
1637     <tt>gsize()</tt>, <tt>gempty()</tt>, <tt>grbegin()</tt>, <tt>grend()</tt><p>
1638
1639 These are forwarding methods that make it easy to access the contents of a
1640 <tt>Module</tt> object's <a href="#GlobalVariable"><tt>GlobalVariable</tt></a>
1641 list.<p>
1642
1643 <li><tt>Module::GlobalListType &amp;getGlobalList()</tt><p>
1644
1645 Returns the list of <a href="#GlobalVariable"><tt>GlobalVariable</tt></a>s.
1646 This is necessary to use when you need to update the list or perform a complex
1647 action that doesn't have a forwarding method.<p>
1648
1649
1650 <!--  Symbol table stuff -->
1651 <hr size=0>
1652
1653 <li><tt><a href="#SymbolTable">SymbolTable</a> *getSymbolTable()</tt><p>
1654
1655 Return a reference to the <a href="#SymbolTable"><tt>SymbolTable</tt></a> for
1656 this <tt>Module</tt>.<p>
1657
1658
1659 <!--  Convenience methods -->
1660 <hr size=0>
1661
1662 <li><tt><a href="#Function">Function</a> *getFunction(const std::string &amp;Name, const <a href="#FunctionType">FunctionType</a> *Ty)</tt><p>
1663
1664 Look up the specified function in the <tt>Module</tt> <a
1665 href="#SymbolTable"><tt>SymbolTable</tt></a>. If it does not exist, return
1666 <tt>null</tt>.<p>
1667
1668
1669 <li><tt><a href="#Function">Function</a> *getOrInsertFunction(const std::string
1670          &amp;Name, const <a href="#FunctionType">FunctionType</a> *T)</tt><p>
1671
1672 Look up the specified function in the <tt>Module</tt> <a
1673 href="#SymbolTable"><tt>SymbolTable</tt></a>. If it does not exist, add an
1674 external declaration for the function and return it.<p>
1675
1676
1677 <li><tt>std::string getTypeName(const <a href="#Type">Type</a> *Ty)</tt><p>
1678
1679 If there is at least one entry in the <a
1680 href="#SymbolTable"><tt>SymbolTable</tt></a> for the specified <a
1681 href="#Type"><tt>Type</tt></a>, return it.  Otherwise return the empty
1682 string.<p>
1683
1684
1685 <li><tt>bool addTypeName(const std::string &Name, const <a href="#Type">Type</a>
1686 *Ty)</tt><p>
1687
1688 Insert an entry in the <a href="#SymbolTable"><tt>SymbolTable</tt></a> mapping
1689 <tt>Name</tt> to <tt>Ty</tt>. If there is already an entry for this name, true
1690 is returned and the <a href="#SymbolTable"><tt>SymbolTable</tt></a> is not
1691 modified.<p>
1692
1693
1694 <!-- ======================================================================= -->
1695 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1696 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1697 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1698 <a name="Constant">The <tt>Constant</tt> class and subclasses</a>
1699 </b></font></td></tr></table><ul>
1700
1701 Constant represents a base class for different types of constants. It is
1702 subclassed by ConstantBool, ConstantInt, ConstantSInt, ConstantUInt,
1703 ConstantArray etc for representing the various types of Constants.<p>
1704
1705
1706 <!-- _______________________________________________________________________ -->
1707 </ul><h4><a name="m_Value"><hr size=0>Important Public Methods</h4><ul>
1708
1709 <li><tt>bool isConstantExpr()</tt>: Returns true if it is a ConstantExpr
1710
1711
1712 <hr>
1713 Important Subclasses of Constant<p>
1714
1715 <ul>
1716 <li>ConstantSInt : This subclass of Constant represents a signed integer constant.
1717 <ul>
1718         <li><tt>int64_t getValue() const</tt>: Returns the underlying value of this constant.
1719 </ul>
1720 <li>ConstantUInt : This class represents an unsigned integer.
1721 <ul>
1722         <li><tt>uint64_t getValue() const</tt>: Returns the underlying value of this constant.
1723 </ul>
1724 <li>ConstantFP : This class represents a floating point constant.
1725 <ul>
1726         <li><tt>double getValue() const</tt>: Returns the underlying value of this constant.
1727 </ul>
1728 <li>ConstantBool : This represents a boolean constant.
1729 <ul>
1730         <li><tt>bool getValue() const</tt>: Returns the underlying value of this constant.
1731 </ul>
1732 <li>ConstantArray : This represents a constant array.
1733 <ul>
1734         <li><tt>const std::vector&lt;Use&gt; &amp;getValues() const</tt>: Returns a Vecotr of component constants that makeup this array.
1735 </ul>
1736 <li>ConstantStruct : This represents a constant struct.
1737 <ul>
1738         <li><tt>const std::vector&lt;Use&gt; &amp;getValues() const</tt>: Returns a Vecotr of component constants that makeup this array.
1739 </ul>
1740 <li>ConstantPointerRef : This represents a constant pointer value that is initialized to point to a global value, which lies at a constant fixed address.
1741 <ul>
1742 <li><tt>GlobalValue *getValue()</tt>: Returns the global value to which this pointer is pointing to.
1743 </ul>
1744 </ul>
1745
1746
1747 <!-- ======================================================================= -->
1748 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1749 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1750 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1751 <a name="Type">The <tt>Type</tt> class and Derived Types</a>
1752 </b></font></td></tr></table><ul>
1753
1754 Type as noted earlier is also a subclass of a Value class.  Any primitive
1755 type (like int, short etc) in LLVM is an instance of Type Class.  All
1756 other types are instances of subclasses of type like FunctionType,
1757 ArrayType etc. DerivedType is the interface for all such dervied types
1758 including FunctionType, ArrayType, PointerType, StructType. Types can have
1759 names. They can be recursive (StructType). There exists exactly one instance 
1760 of any type structure at a time. This allows using pointer equality of Type *s for comparing types. 
1761
1762 <!-- _______________________________________________________________________ -->
1763 </ul><h4><a name="m_Value"><hr size=0>Important Public Methods</h4><ul>
1764
1765 <li><tt>PrimitiveID getPrimitiveID() const</tt>: Returns the base type of the type.
1766 <li><tt> bool isSigned() const</tt>: Returns whether an integral numeric type is signed. This is true for SByteTy, ShortTy, IntTy, LongTy. Note that this is not true for Float and Double.
1767 <li><tt>bool isUnsigned() const</tt>: Returns whether a numeric type is unsigned. This is not quite the complement of isSigned... nonnumeric types return false as they do with isSigned. This returns true for UByteTy, UShortTy, UIntTy, and ULongTy. 
1768 <li><tt> bool isInteger() const</tt>: Equilivent to isSigned() || isUnsigned(), but with only a single virtual function invocation. 
1769 <li><tt>bool isIntegral() const</tt>: Returns true if this is an integral type, which is either Bool type or one of the Integer types.
1770
1771 <li><tt>bool isFloatingPoint()</tt>: Return true if this is one of the two floating point types.
1772 <li><tt>bool isRecursive() const</tt>: Returns rue if the type graph contains a cycle.
1773 <li><tt>isLosslesslyConvertableTo (const Type *Ty) const</tt>: Return true if this type can be converted to 'Ty' without any reinterpretation of bits. For example, uint to int.
1774 <li><tt>bool isPrimitiveType() const</tt>: Returns true if it is a primitive type.
1775 <li><tt>bool isDerivedType() const</tt>: Returns true if it is a derived type.
1776 <li><tt>const Type * getContainedType (unsigned i) const</tt>: 
1777 This method is used to implement the type iterator. For derived types, this returns the types 'contained' in the derived type, returning 0 when 'i' becomes invalid. This allows the user to iterate over the types in a struct, for example, really easily.
1778 <li><tt>unsigned getNumContainedTypes() const</tt>: Return the number of types in the derived type. 
1779
1780 <p>
1781
1782 <hr>
1783 Derived Types<p>
1784
1785 <ul>
1786 <li>SequentialType : This is subclassed by ArrayType and PointerType 
1787 <ul>
1788         <li><tt>const Type * getElementType() const</tt>: Returns the type of each of the elements in the sequential type.
1789 </ul>
1790 <li>ArrayType : This is a subclass of SequentialType and defines interface for array types.
1791 <ul>
1792         <li><tt>unsigned getNumElements() const</tt>: Returns the number of elements in the array.
1793 </ul>
1794 <li>PointerType : Subclass of SequentialType for  pointer types.
1795 <li>StructType : subclass of DerivedTypes for struct types
1796 <li>FunctionType : subclass of DerivedTypes for function types.
1797
1798 <ul>
1799         
1800         <li><tt>bool isVarArg() const</tt>: Returns true if its a vararg function
1801         <li><tt> const Type * getReturnType() const</tt>: Returns the return type of the function.
1802         <li><tt> const ParamTypes &amp;getParamTypes() const</tt>: Returns a vector of parameter types.
1803         <li><tt>const Type * getParamType (unsigned i)</tt>: Returns the type of the ith parameter.
1804         <li><tt> const unsigned getNumParams() const</tt>: Returns the number of formal parameters.
1805 </ul>
1806 </ul>
1807
1808
1809
1810
1811 <!-- ======================================================================= -->
1812 </ul><table width="100%" bgcolor="#441188" border=0 cellpadding=4 cellspacing=0>
1813 <tr><td>&nbsp;</td><td width="100%">&nbsp; 
1814 <font color="#EEEEFF" face="Georgia,Palatino"><b>
1815 <a name="Argument">The <tt>Argument</tt> class</a>
1816 </b></font></td></tr></table><ul>
1817
1818 This subclass of Value defines the interface for incoming formal arguments to a
1819 function. A Function maitanis a list of its formal arguments. An argument has a
1820 pointer to the parent Function.
1821
1822
1823
1824
1825 <!-- *********************************************************************** -->
1826 </ul>
1827 <!-- *********************************************************************** -->
1828
1829 <hr><font size-1>
1830 <address>By: <a href="mailto:dhurjati@cs.uiuc.edu">Dinakar Dhurjati</a> and
1831 <a href="mailto:sabre@nondot.org">Chris Lattner</a></address>
1832 <a href="http://llvm.cs.uiuc.edu">The LLVM Compiler Infrastructure</a>
1833 <br>
1834 <!-- Created: Tue Aug  6 15:00:33 CDT 2002 -->
1835 <!-- hhmts start -->
1836 Last modified: Fri Nov  7 13:24:22 CST 2003
1837 <!-- hhmts end -->
1838 </font></body></html>