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