blob: 30192173502f20f95eb0a20bc910313bf4ccb8ac [file] [log] [blame]
Misha Brukman13fd15c2004-01-15 00:14:41 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
Chris Lattner261efe92003-11-25 01:02:51 +00003<html>
4<head>
5 <title>LLVM Programmer's Manual</title>
Misha Brukman13fd15c2004-01-15 00:14:41 +00006 <link rel="stylesheet" href="llvm.css" type="text/css">
Chris Lattner261efe92003-11-25 01:02:51 +00007</head>
Misha Brukman13fd15c2004-01-15 00:14:41 +00008<body>
9
10<div class="doc_title">
11 LLVM Programmer's Manual
12</div>
13
Chris Lattner9355b472002-09-06 02:50:58 +000014<ol>
Misha Brukman13fd15c2004-01-15 00:14:41 +000015 <li><a href="#introduction">Introduction</a></li>
Chris Lattner9355b472002-09-06 02:50:58 +000016 <li><a href="#general">General Information</a>
Chris Lattner261efe92003-11-25 01:02:51 +000017 <ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +000018 <li><a href="#stl">The C++ Standard Template Library</a></li>
19<!--
20 <li>The <tt>-time-passes</tt> option</li>
21 <li>How to use the LLVM Makefile system</li>
22 <li>How to write a regression test</li>
Chris Lattner61db4652004-12-08 19:05:44 +000023
Reid Spencerfe8f4ff2004-11-01 09:02:53 +000024-->
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>
Misha Brukman2c122ce2005-11-01 21:12:49 +000031 <li><a href="#DEBUG">The <tt>DEBUG()</tt> macro and <tt>-debug</tt>
Chris Lattner261efe92003-11-25 01:02:51 +000032option</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>
Chris Lattner0be6fdf2006-12-19 21:46:21 +000038 <li><a href="#Statistic">The <tt>Statistic</tt> class &amp; <tt>-stats</tt>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +000039option</a></li>
40<!--
41 <li>The <tt>InstVisitor</tt> template
42 <li>The general graph API
43-->
Chris Lattnerf623a082005-10-17 01:36:23 +000044 <li><a href="#ViewGraph">Viewing graphs while debugging code</a></li>
Chris Lattner261efe92003-11-25 01:02:51 +000045 </ul>
46 </li>
Chris Lattner098129a2007-02-03 03:04:03 +000047 <li><a href="#datastructure">Picking the Right Data Structure for a Task</a>
48 <ul>
Chris Lattner74c4ca12007-02-03 07:59:07 +000049 <li><a href="#ds_sequential">Sequential Containers (std::vector, std::list, etc)</a>
50 <ul>
51 <li><a href="#dss_fixedarrays">Fixed Size Arrays</a></li>
52 <li><a href="#dss_heaparrays">Heap Allocated Arrays</a></li>
53 <li><a href="#dss_smallvector">"llvm/ADT/SmallVector.h"</a></li>
54 <li><a href="#dss_vector">&lt;vector&gt;</a></li>
55 <li><a href="#dss_deque">&lt;deque&gt;</a></li>
56 <li><a href="#dss_list">&lt;list&gt;</a></li>
57 <li><a href="#dss_ilist">llvm/ADT/ilist</a></li>
Chris Lattner098129a2007-02-03 03:04:03 +000058 </ul></li>
Chris Lattner74c4ca12007-02-03 07:59:07 +000059 <li><a href="#ds_set">Set-Like Containers (std::set, SmallSet, SetVector, etc)</a>
60 <ul>
61 <li><a href="#dss_sortedvectorset">A sorted 'vector'</a></li>
62 <li><a href="#dss_smallset">"llvm/ADT/SmallSet.h"</a></li>
63 <li><a href="#dss_smallptrset">"llvm/ADT/SmallPtrSet.h"</a></li>
64 <li><a href="#dss_FoldingSet">"llvm/ADT/FoldingSet.h"</a></li>
65 <li><a href="#dss_set">&lt;set&gt;</a></li>
66 <li><a href="#dss_setvector">"llvm/ADT/SetVector.h"</a></li>
67 <li><a href="#dss_otherset">Other Options</a></li>
68 </ul></li>
Chris Lattner098129a2007-02-03 03:04:03 +000069 <li><a href="#ds_map">Map-Like Containers (std::map, DenseMap, etc)</a></li>
Chris Lattner74c4ca12007-02-03 07:59:07 +000070 </ul>
Chris Lattner098129a2007-02-03 03:04:03 +000071 </li>
Chris Lattnerae7f7592002-09-06 18:31:18 +000072 <li><a href="#common">Helpful Hints for Common Operations</a>
Chris Lattnerae7f7592002-09-06 18:31:18 +000073 <ul>
Chris Lattner261efe92003-11-25 01:02:51 +000074 <li><a href="#inspection">Basic Inspection and Traversal Routines</a>
75 <ul>
76 <li><a href="#iterate_function">Iterating over the <tt>BasicBlock</tt>s
77in a <tt>Function</tt></a> </li>
78 <li><a href="#iterate_basicblock">Iterating over the <tt>Instruction</tt>s
79in a <tt>BasicBlock</tt></a> </li>
80 <li><a href="#iterate_institer">Iterating over the <tt>Instruction</tt>s
81in a <tt>Function</tt></a> </li>
82 <li><a href="#iterate_convert">Turning an iterator into a
83class pointer</a> </li>
84 <li><a href="#iterate_complex">Finding call sites: a more
85complex example</a> </li>
86 <li><a href="#calls_and_invokes">Treating calls and invokes
87the same way</a> </li>
88 <li><a href="#iterate_chains">Iterating over def-use &amp;
89use-def chains</a> </li>
90 </ul>
91 </li>
92 <li><a href="#simplechanges">Making simple changes</a>
93 <ul>
94 <li><a href="#schanges_creating">Creating and inserting new
95 <tt>Instruction</tt>s</a> </li>
96 <li><a href="#schanges_deleting">Deleting <tt>Instruction</tt>s</a> </li>
97 <li><a href="#schanges_replacing">Replacing an <tt>Instruction</tt>
98with another <tt>Value</tt></a> </li>
99 </ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000100 </li>
Chris Lattnerae7f7592002-09-06 18:31:18 +0000101<!--
102 <li>Working with the Control Flow Graph
103 <ul>
104 <li>Accessing predecessors and successors of a <tt>BasicBlock</tt>
105 <li>
106 <li>
107 </ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000108-->
Chris Lattner261efe92003-11-25 01:02:51 +0000109 </ul>
110 </li>
Chris Lattnerd9d6e102005-04-23 16:10:52 +0000111
112 <li><a href="#advanced">Advanced Topics</a>
113 <ul>
Chris Lattnerf1b200b2005-04-23 17:27:36 +0000114 <li><a href="#TypeResolve">LLVM Type Resolution</a>
115 <ul>
116 <li><a href="#BuildRecType">Basic Recursive Type Construction</a></li>
117 <li><a href="#refineAbstractTypeTo">The <tt>refineAbstractTypeTo</tt> method</a></li>
118 <li><a href="#PATypeHolder">The PATypeHolder Class</a></li>
119 <li><a href="#AbstractTypeUser">The AbstractTypeUser Class</a></li>
120 </ul></li>
121
Chris Lattnerd9d6e102005-04-23 16:10:52 +0000122 <li><a href="#SymbolTable">The <tt>SymbolTable</tt> class </a></li>
123 </ul></li>
124
Joel Stanley9b96c442002-09-06 21:55:13 +0000125 <li><a href="#coreclasses">The Core LLVM Class Hierarchy Reference</a>
Chris Lattner9355b472002-09-06 02:50:58 +0000126 <ul>
Reid Spencer303c4b42007-01-12 17:26:25 +0000127 <li><a href="#Type">The <tt>Type</tt> class</a> </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000128 <li><a href="#Value">The <tt>Value</tt> class</a>
Chris Lattner9355b472002-09-06 02:50:58 +0000129 <ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000130 <li><a href="#User">The <tt>User</tt> class</a>
Chris Lattner261efe92003-11-25 01:02:51 +0000131 <ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000132 <li><a href="#Instruction">The <tt>Instruction</tt> class</a>
133 <ul>
134 <li><a href="#GetElementPtrInst">The <tt>GetElementPtrInst</tt> class</a></li>
135 </ul>
136 </li>
137 <li><a href="#Module">The <tt>Module</tt> class</a></li>
138 <li><a href="#Constant">The <tt>Constant</tt> class</a>
139 <ul>
140 <li><a href="#GlobalValue">The <tt>GlobalValue</tt> class</a>
141 <ul>
142 <li><a href="#BasicBlock">The <tt>BasicBlock</tt>class</a></li>
143 <li><a href="#Function">The <tt>Function</tt> class</a></li>
144 <li><a href="#GlobalVariable">The <tt>GlobalVariable</tt> class</a></li>
145 </ul>
146 </li>
147 </ul>
148 </li>
Reid Spencer8b2da7a2004-07-18 13:10:31 +0000149 </ul>
150 </li>
Reid Spencer096603a2004-05-26 08:41:35 +0000151 <li><a href="#Argument">The <tt>Argument</tt> class</a></li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000152 </ul>
153 </li>
154 </ul>
Chris Lattner261efe92003-11-25 01:02:51 +0000155 </li>
Chris Lattner9355b472002-09-06 02:50:58 +0000156</ol>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000157
Chris Lattner69bf8a92004-05-23 21:06:58 +0000158<div class="doc_author">
159 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>,
Chris Lattner94c43592004-05-26 16:52:55 +0000160 <a href="mailto:dhurjati@cs.uiuc.edu">Dinakar Dhurjati</a>,
161 <a href="mailto:jstanley@cs.uiuc.edu">Joel Stanley</a>, and
162 <a href="mailto:rspencer@x10sys.com">Reid Spencer</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000163</div>
164
Chris Lattner9355b472002-09-06 02:50:58 +0000165<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000166<div class="doc_section">
167 <a name="introduction">Introduction </a>
168</div>
Chris Lattner9355b472002-09-06 02:50:58 +0000169<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000170
171<div class="doc_text">
172
173<p>This document is meant to highlight some of the important classes and
Chris Lattner261efe92003-11-25 01:02:51 +0000174interfaces available in the LLVM source-base. This manual is not
175intended to explain what LLVM is, how it works, and what LLVM code looks
176like. It assumes that you know the basics of LLVM and are interested
177in writing transformations or otherwise analyzing or manipulating the
Misha Brukman13fd15c2004-01-15 00:14:41 +0000178code.</p>
179
180<p>This document should get you oriented so that you can find your
Chris Lattner261efe92003-11-25 01:02:51 +0000181way in the continuously growing source code that makes up the LLVM
182infrastructure. Note that this manual is not intended to serve as a
183replacement for reading the source code, so if you think there should be
184a method in one of these classes to do something, but it's not listed,
185check the source. Links to the <a href="/doxygen/">doxygen</a> sources
186are provided to make this as easy as possible.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000187
188<p>The first section of this document describes general information that is
189useful to know when working in the LLVM infrastructure, and the second describes
190the Core LLVM classes. In the future this manual will be extended with
191information describing how to use extension libraries, such as dominator
192information, CFG traversal routines, and useful utilities like the <tt><a
193href="/doxygen/InstVisitor_8h-source.html">InstVisitor</a></tt> template.</p>
194
195</div>
196
Chris Lattner9355b472002-09-06 02:50:58 +0000197<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000198<div class="doc_section">
199 <a name="general">General Information</a>
200</div>
201<!-- *********************************************************************** -->
202
203<div class="doc_text">
204
205<p>This section contains general information that is useful if you are working
206in the LLVM source-base, but that isn't specific to any particular API.</p>
207
208</div>
209
210<!-- ======================================================================= -->
211<div class="doc_subsection">
212 <a name="stl">The C++ Standard Template Library</a>
213</div>
214
215<div class="doc_text">
216
217<p>LLVM makes heavy use of the C++ Standard Template Library (STL),
Chris Lattner261efe92003-11-25 01:02:51 +0000218perhaps much more than you are used to, or have seen before. Because of
219this, you might want to do a little background reading in the
220techniques used and capabilities of the library. There are many good
221pages that discuss the STL, and several books on the subject that you
Misha Brukman13fd15c2004-01-15 00:14:41 +0000222can get, so it will not be discussed in this document.</p>
223
224<p>Here are some useful links:</p>
225
226<ol>
227
228<li><a href="http://www.dinkumware.com/refxcpp.html">Dinkumware C++ Library
229reference</a> - an excellent reference for the STL and other parts of the
230standard C++ library.</li>
231
232<li><a href="http://www.tempest-sw.com/cpp/">C++ In a Nutshell</a> - This is an
Tanya Lattner09cf73c2004-06-22 04:24:55 +0000233O'Reilly book in the making. It has a decent
234Standard Library
235Reference that rivals Dinkumware's, and is unfortunately no longer free since the book has been
Misha Brukman13fd15c2004-01-15 00:14:41 +0000236published.</li>
237
238<li><a href="http://www.parashift.com/c++-faq-lite/">C++ Frequently Asked
239Questions</a></li>
240
241<li><a href="http://www.sgi.com/tech/stl/">SGI's STL Programmer's Guide</a> -
242Contains a useful <a
243href="http://www.sgi.com/tech/stl/stl_introduction.html">Introduction to the
244STL</a>.</li>
245
246<li><a href="http://www.research.att.com/%7Ebs/C++.html">Bjarne Stroustrup's C++
247Page</a></li>
248
Tanya Lattner79445ba2004-12-08 18:34:56 +0000249<li><a href="http://64.78.49.204/">
Reid Spencer096603a2004-05-26 08:41:35 +0000250Bruce Eckel's Thinking in C++, 2nd ed. Volume 2 Revision 4.0 (even better, get
251the book).</a></li>
252
Misha Brukman13fd15c2004-01-15 00:14:41 +0000253</ol>
254
255<p>You are also encouraged to take a look at the <a
256href="CodingStandards.html">LLVM Coding Standards</a> guide which focuses on how
257to write maintainable code more than where to put your curly braces.</p>
258
259</div>
260
261<!-- ======================================================================= -->
262<div class="doc_subsection">
263 <a name="stl">Other useful references</a>
264</div>
265
266<div class="doc_text">
267
Misha Brukman13fd15c2004-01-15 00:14:41 +0000268<ol>
269<li><a href="http://www.psc.edu/%7Esemke/cvs_branches.html">CVS
Chris Lattner261efe92003-11-25 01:02:51 +0000270Branch and Tag Primer</a></li>
Misha Brukmana0f71e42004-06-18 18:39:00 +0000271<li><a href="http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html">Using
272static and shared libraries across platforms</a></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000273</ol>
274
275</div>
276
Chris Lattner9355b472002-09-06 02:50:58 +0000277<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000278<div class="doc_section">
279 <a name="apis">Important and useful LLVM APIs</a>
280</div>
281<!-- *********************************************************************** -->
282
283<div class="doc_text">
284
285<p>Here we highlight some LLVM APIs that are generally useful and good to
286know about when writing transformations.</p>
287
288</div>
289
290<!-- ======================================================================= -->
291<div class="doc_subsection">
Misha Brukman2c122ce2005-11-01 21:12:49 +0000292 <a name="isa">The <tt>isa&lt;&gt;</tt>, <tt>cast&lt;&gt;</tt> and
293 <tt>dyn_cast&lt;&gt;</tt> templates</a>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000294</div>
295
296<div class="doc_text">
297
298<p>The LLVM source-base makes extensive use of a custom form of RTTI.
Chris Lattner261efe92003-11-25 01:02:51 +0000299These templates have many similarities to the C++ <tt>dynamic_cast&lt;&gt;</tt>
300operator, but they don't have some drawbacks (primarily stemming from
301the fact that <tt>dynamic_cast&lt;&gt;</tt> only works on classes that
302have a v-table). Because they are used so often, you must know what they
303do and how they work. All of these templates are defined in the <a
Chris Lattner695b78b2005-04-26 22:56:16 +0000304 href="/doxygen/Casting_8h-source.html"><tt>llvm/Support/Casting.h</tt></a>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000305file (note that you very rarely have to include this file directly).</p>
306
307<dl>
308 <dt><tt>isa&lt;&gt;</tt>: </dt>
309
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000310 <dd><p>The <tt>isa&lt;&gt;</tt> operator works exactly like the Java
Misha Brukman13fd15c2004-01-15 00:14:41 +0000311 "<tt>instanceof</tt>" operator. It returns true or false depending on whether
312 a reference or pointer points to an instance of the specified class. This can
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000313 be very useful for constraint checking of various sorts (example below).</p>
314 </dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000315
316 <dt><tt>cast&lt;&gt;</tt>: </dt>
317
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000318 <dd><p>The <tt>cast&lt;&gt;</tt> operator is a "checked cast" operation. It
Misha Brukman13fd15c2004-01-15 00:14:41 +0000319 converts a pointer or reference from a base class to a derived cast, causing
320 an assertion failure if it is not really an instance of the right type. This
321 should be used in cases where you have some information that makes you believe
322 that something is of the right type. An example of the <tt>isa&lt;&gt;</tt>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000323 and <tt>cast&lt;&gt;</tt> template is:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000324
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000325<div class="doc_code">
326<pre>
327static bool isLoopInvariant(const <a href="#Value">Value</a> *V, const Loop *L) {
328 if (isa&lt;<a href="#Constant">Constant</a>&gt;(V) || isa&lt;<a href="#Argument">Argument</a>&gt;(V) || isa&lt;<a href="#GlobalValue">GlobalValue</a>&gt;(V))
329 return true;
Chris Lattner69bf8a92004-05-23 21:06:58 +0000330
Bill Wendling82e2eea2006-10-11 18:00:22 +0000331 // <i>Otherwise, it must be an instruction...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000332 return !L-&gt;contains(cast&lt;<a href="#Instruction">Instruction</a>&gt;(V)-&gt;getParent());
333}
334</pre>
335</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000336
337 <p>Note that you should <b>not</b> use an <tt>isa&lt;&gt;</tt> test followed
338 by a <tt>cast&lt;&gt;</tt>, for that use the <tt>dyn_cast&lt;&gt;</tt>
339 operator.</p>
340
341 </dd>
342
343 <dt><tt>dyn_cast&lt;&gt;</tt>:</dt>
344
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000345 <dd><p>The <tt>dyn_cast&lt;&gt;</tt> operator is a "checking cast" operation.
346 It checks to see if the operand is of the specified type, and if so, returns a
Misha Brukman13fd15c2004-01-15 00:14:41 +0000347 pointer to it (this operator does not work with references). If the operand is
348 not of the correct type, a null pointer is returned. Thus, this works very
Misha Brukman2c122ce2005-11-01 21:12:49 +0000349 much like the <tt>dynamic_cast&lt;&gt;</tt> operator in C++, and should be
350 used in the same circumstances. Typically, the <tt>dyn_cast&lt;&gt;</tt>
351 operator is used in an <tt>if</tt> statement or some other flow control
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000352 statement like this:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000353
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000354<div class="doc_code">
355<pre>
356if (<a href="#AllocationInst">AllocationInst</a> *AI = dyn_cast&lt;<a href="#AllocationInst">AllocationInst</a>&gt;(Val)) {
Bill Wendling82e2eea2006-10-11 18:00:22 +0000357 // <i>...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000358}
359</pre>
360</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000361
Misha Brukman2c122ce2005-11-01 21:12:49 +0000362 <p>This form of the <tt>if</tt> statement effectively combines together a call
363 to <tt>isa&lt;&gt;</tt> and a call to <tt>cast&lt;&gt;</tt> into one
364 statement, which is very convenient.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000365
Misha Brukman2c122ce2005-11-01 21:12:49 +0000366 <p>Note that the <tt>dyn_cast&lt;&gt;</tt> operator, like C++'s
367 <tt>dynamic_cast&lt;&gt;</tt> or Java's <tt>instanceof</tt> operator, can be
368 abused. In particular, you should not use big chained <tt>if/then/else</tt>
369 blocks to check for lots of different variants of classes. If you find
370 yourself wanting to do this, it is much cleaner and more efficient to use the
371 <tt>InstVisitor</tt> class to dispatch over the instruction type directly.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000372
Misha Brukman2c122ce2005-11-01 21:12:49 +0000373 </dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000374
Misha Brukman2c122ce2005-11-01 21:12:49 +0000375 <dt><tt>cast_or_null&lt;&gt;</tt>: </dt>
376
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000377 <dd><p>The <tt>cast_or_null&lt;&gt;</tt> operator works just like the
Misha Brukman2c122ce2005-11-01 21:12:49 +0000378 <tt>cast&lt;&gt;</tt> operator, except that it allows for a null pointer as an
379 argument (which it then propagates). This can sometimes be useful, allowing
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000380 you to combine several null checks into one.</p></dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000381
Misha Brukman2c122ce2005-11-01 21:12:49 +0000382 <dt><tt>dyn_cast_or_null&lt;&gt;</tt>: </dt>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000383
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000384 <dd><p>The <tt>dyn_cast_or_null&lt;&gt;</tt> operator works just like the
Misha Brukman2c122ce2005-11-01 21:12:49 +0000385 <tt>dyn_cast&lt;&gt;</tt> operator, except that it allows for a null pointer
386 as an argument (which it then propagates). This can sometimes be useful,
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000387 allowing you to combine several null checks into one.</p></dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000388
Misha Brukman2c122ce2005-11-01 21:12:49 +0000389</dl>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000390
391<p>These five templates can be used with any classes, whether they have a
392v-table or not. To add support for these templates, you simply need to add
393<tt>classof</tt> static methods to the class you are interested casting
394to. Describing this is currently outside the scope of this document, but there
395are lots of examples in the LLVM source base.</p>
396
397</div>
398
399<!-- ======================================================================= -->
400<div class="doc_subsection">
Misha Brukman2c122ce2005-11-01 21:12:49 +0000401 <a name="DEBUG">The <tt>DEBUG()</tt> macro and <tt>-debug</tt> option</a>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000402</div>
403
404<div class="doc_text">
405
406<p>Often when working on your pass you will put a bunch of debugging printouts
407and other code into your pass. After you get it working, you want to remove
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000408it, but you may need it again in the future (to work out new bugs that you run
Misha Brukman13fd15c2004-01-15 00:14:41 +0000409across).</p>
410
411<p> Naturally, because of this, you don't want to delete the debug printouts,
412but you don't want them to always be noisy. A standard compromise is to comment
413them out, allowing you to enable them if you need them in the future.</p>
414
Chris Lattner695b78b2005-04-26 22:56:16 +0000415<p>The "<tt><a href="/doxygen/Debug_8h-source.html">llvm/Support/Debug.h</a></tt>"
Misha Brukman13fd15c2004-01-15 00:14:41 +0000416file provides a macro named <tt>DEBUG()</tt> that is a much nicer solution to
417this problem. Basically, you can put arbitrary code into the argument of the
418<tt>DEBUG</tt> macro, and it is only executed if '<tt>opt</tt>' (or any other
419tool) is run with the '<tt>-debug</tt>' command line argument:</p>
420
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000421<div class="doc_code">
422<pre>
Bill Wendling832171c2006-12-07 20:04:42 +0000423DOUT &lt;&lt; "I am here!\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000424</pre>
425</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000426
427<p>Then you can run your pass like this:</p>
428
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000429<div class="doc_code">
430<pre>
431$ opt &lt; a.bc &gt; /dev/null -mypass
Bill Wendling82e2eea2006-10-11 18:00:22 +0000432<i>&lt;no output&gt;</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000433$ opt &lt; a.bc &gt; /dev/null -mypass -debug
434I am here!
435</pre>
436</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000437
438<p>Using the <tt>DEBUG()</tt> macro instead of a home-brewed solution allows you
439to not have to create "yet another" command line option for the debug output for
440your pass. Note that <tt>DEBUG()</tt> macros are disabled for optimized builds,
441so they do not cause a performance impact at all (for the same reason, they
442should also not contain side-effects!).</p>
443
444<p>One additional nice thing about the <tt>DEBUG()</tt> macro is that you can
445enable or disable it directly in gdb. Just use "<tt>set DebugFlag=0</tt>" or
446"<tt>set DebugFlag=1</tt>" from the gdb if the program is running. If the
447program hasn't been started yet, you can always just run it with
448<tt>-debug</tt>.</p>
449
450</div>
451
452<!-- _______________________________________________________________________ -->
453<div class="doc_subsubsection">
Chris Lattnerc9151082005-04-26 22:57:07 +0000454 <a name="DEBUG_TYPE">Fine grained debug info with <tt>DEBUG_TYPE</tt> and
Misha Brukman13fd15c2004-01-15 00:14:41 +0000455 the <tt>-debug-only</tt> option</a>
456</div>
457
458<div class="doc_text">
459
460<p>Sometimes you may find yourself in a situation where enabling <tt>-debug</tt>
461just turns on <b>too much</b> information (such as when working on the code
462generator). If you want to enable debug information with more fine-grained
463control, you define the <tt>DEBUG_TYPE</tt> macro and the <tt>-debug</tt> only
464option as follows:</p>
465
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000466<div class="doc_code">
467<pre>
Bill Wendling832171c2006-12-07 20:04:42 +0000468DOUT &lt;&lt; "No debug type\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000469#undef DEBUG_TYPE
470#define DEBUG_TYPE "foo"
Bill Wendling832171c2006-12-07 20:04:42 +0000471DOUT &lt;&lt; "'foo' debug type\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000472#undef DEBUG_TYPE
473#define DEBUG_TYPE "bar"
Bill Wendling832171c2006-12-07 20:04:42 +0000474DOUT &lt;&lt; "'bar' debug type\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000475#undef DEBUG_TYPE
476#define DEBUG_TYPE ""
Bill Wendling832171c2006-12-07 20:04:42 +0000477DOUT &lt;&lt; "No debug type (2)\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000478</pre>
479</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000480
481<p>Then you can run your pass like this:</p>
482
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000483<div class="doc_code">
484<pre>
485$ opt &lt; a.bc &gt; /dev/null -mypass
Bill Wendling82e2eea2006-10-11 18:00:22 +0000486<i>&lt;no output&gt;</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000487$ opt &lt; a.bc &gt; /dev/null -mypass -debug
488No debug type
489'foo' debug type
490'bar' debug type
491No debug type (2)
492$ opt &lt; a.bc &gt; /dev/null -mypass -debug-only=foo
493'foo' debug type
494$ opt &lt; a.bc &gt; /dev/null -mypass -debug-only=bar
495'bar' debug type
496</pre>
497</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000498
499<p>Of course, in practice, you should only set <tt>DEBUG_TYPE</tt> at the top of
500a file, to specify the debug type for the entire module (if you do this before
Chris Lattner695b78b2005-04-26 22:56:16 +0000501you <tt>#include "llvm/Support/Debug.h"</tt>, you don't have to insert the ugly
Misha Brukman13fd15c2004-01-15 00:14:41 +0000502<tt>#undef</tt>'s). Also, you should use names more meaningful than "foo" and
503"bar", because there is no system in place to ensure that names do not
504conflict. If two different modules use the same string, they will all be turned
505on when the name is specified. This allows, for example, all debug information
506for instruction scheduling to be enabled with <tt>-debug-type=InstrSched</tt>,
Chris Lattner261efe92003-11-25 01:02:51 +0000507even if the source lives in multiple files.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000508
509</div>
510
511<!-- ======================================================================= -->
512<div class="doc_subsection">
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000513 <a name="Statistic">The <tt>Statistic</tt> class &amp; <tt>-stats</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000514 option</a>
515</div>
516
517<div class="doc_text">
518
519<p>The "<tt><a
Chris Lattner695b78b2005-04-26 22:56:16 +0000520href="/doxygen/Statistic_8h-source.html">llvm/ADT/Statistic.h</a></tt>" file
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000521provides a class named <tt>Statistic</tt> that is used as a unified way to
Misha Brukman13fd15c2004-01-15 00:14:41 +0000522keep track of what the LLVM compiler is doing and how effective various
523optimizations are. It is useful to see what optimizations are contributing to
524making a particular program run faster.</p>
525
526<p>Often you may run your pass on some big program, and you're interested to see
527how many times it makes a certain transformation. Although you can do this with
528hand inspection, or some ad-hoc method, this is a real pain and not very useful
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000529for big programs. Using the <tt>Statistic</tt> class makes it very easy to
Misha Brukman13fd15c2004-01-15 00:14:41 +0000530keep track of this information, and the calculated information is presented in a
531uniform manner with the rest of the passes being executed.</p>
532
533<p>There are many examples of <tt>Statistic</tt> uses, but the basics of using
534it are as follows:</p>
535
536<ol>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000537 <li><p>Define your statistic like this:</p>
538
539<div class="doc_code">
540<pre>
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000541#define <a href="#DEBUG_TYPE">DEBUG_TYPE</a> "mypassname" <i>// This goes before any #includes.</i>
542STATISTIC(NumXForms, "The # of times I did stuff");
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000543</pre>
544</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000545
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000546 <p>The <tt>STATISTIC</tt> macro defines a static variable, whose name is
547 specified by the first argument. The pass name is taken from the DEBUG_TYPE
548 macro, and the description is taken from the second argument. The variable
Reid Spencer06565dc2007-01-12 17:11:23 +0000549 defined ("NumXForms" in this case) acts like an unsigned integer.</p></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000550
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000551 <li><p>Whenever you make a transformation, bump the counter:</p>
552
553<div class="doc_code">
554<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +0000555++NumXForms; // <i>I did stuff!</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000556</pre>
557</div>
558
Chris Lattner261efe92003-11-25 01:02:51 +0000559 </li>
560 </ol>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000561
562 <p>That's all you have to do. To get '<tt>opt</tt>' to print out the
563 statistics gathered, use the '<tt>-stats</tt>' option:</p>
564
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000565<div class="doc_code">
566<pre>
567$ opt -stats -mypassname &lt; program.bc &gt; /dev/null
Bill Wendling82e2eea2006-10-11 18:00:22 +0000568<i>... statistics output ...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000569</pre>
570</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000571
Chris Lattner261efe92003-11-25 01:02:51 +0000572 <p> When running <tt>gccas</tt> on a C file from the SPEC benchmark
573suite, it gives a report that looks like this:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000574
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000575<div class="doc_code">
576<pre>
577 7646 bytecodewriter - Number of normal instructions
578 725 bytecodewriter - Number of oversized instructions
579 129996 bytecodewriter - Number of bytecode bytes written
580 2817 raise - Number of insts DCEd or constprop'd
581 3213 raise - Number of cast-of-self removed
582 5046 raise - Number of expression trees converted
583 75 raise - Number of other getelementptr's formed
584 138 raise - Number of load/store peepholes
585 42 deadtypeelim - Number of unused typenames removed from symtab
586 392 funcresolve - Number of varargs functions resolved
587 27 globaldce - Number of global variables removed
588 2 adce - Number of basic blocks removed
589 134 cee - Number of branches revectored
590 49 cee - Number of setcc instruction eliminated
591 532 gcse - Number of loads removed
592 2919 gcse - Number of instructions removed
593 86 indvars - Number of canonical indvars added
594 87 indvars - Number of aux indvars removed
595 25 instcombine - Number of dead inst eliminate
596 434 instcombine - Number of insts combined
597 248 licm - Number of load insts hoisted
598 1298 licm - Number of insts hoisted to a loop pre-header
599 3 licm - Number of insts hoisted to multiple loop preds (bad, no loop pre-header)
600 75 mem2reg - Number of alloca's promoted
601 1444 cfgsimplify - Number of blocks simplified
602</pre>
603</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000604
605<p>Obviously, with so many optimizations, having a unified framework for this
606stuff is very nice. Making your pass fit well into the framework makes it more
607maintainable and useful.</p>
608
609</div>
610
Chris Lattnerf623a082005-10-17 01:36:23 +0000611<!-- ======================================================================= -->
612<div class="doc_subsection">
613 <a name="ViewGraph">Viewing graphs while debugging code</a>
614</div>
615
616<div class="doc_text">
617
618<p>Several of the important data structures in LLVM are graphs: for example
619CFGs made out of LLVM <a href="#BasicBlock">BasicBlock</a>s, CFGs made out of
620LLVM <a href="CodeGenerator.html#machinebasicblock">MachineBasicBlock</a>s, and
621<a href="CodeGenerator.html#selectiondag_intro">Instruction Selection
622DAGs</a>. In many cases, while debugging various parts of the compiler, it is
623nice to instantly visualize these graphs.</p>
624
625<p>LLVM provides several callbacks that are available in a debug build to do
626exactly that. If you call the <tt>Function::viewCFG()</tt> method, for example,
627the current LLVM tool will pop up a window containing the CFG for the function
628where each basic block is a node in the graph, and each node contains the
629instructions in the block. Similarly, there also exists
630<tt>Function::viewCFGOnly()</tt> (does not include the instructions), the
631<tt>MachineFunction::viewCFG()</tt> and <tt>MachineFunction::viewCFGOnly()</tt>,
632and the <tt>SelectionDAG::viewGraph()</tt> methods. Within GDB, for example,
Jim Laskey543a0ee2006-10-02 12:28:07 +0000633you can usually use something like <tt>call DAG.viewGraph()</tt> to pop
Chris Lattnerf623a082005-10-17 01:36:23 +0000634up a window. Alternatively, you can sprinkle calls to these functions in your
635code in places you want to debug.</p>
636
637<p>Getting this to work requires a small amount of configuration. On Unix
638systems with X11, install the <a href="http://www.graphviz.org">graphviz</a>
639toolkit, and make sure 'dot' and 'gv' are in your path. If you are running on
640Mac OS/X, download and install the Mac OS/X <a
641href="http://www.pixelglow.com/graphviz/">Graphviz program</a>, and add
642<tt>/Applications/Graphviz.app/Contents/MacOS/</tt> (or whereever you install
643it) to your path. Once in your system and path are set up, rerun the LLVM
644configure script and rebuild LLVM to enable this functionality.</p>
645
Jim Laskey543a0ee2006-10-02 12:28:07 +0000646<p><tt>SelectionDAG</tt> has been extended to make it easier to locate
647<i>interesting</i> nodes in large complex graphs. From gdb, if you
648<tt>call DAG.setGraphColor(<i>node</i>, "<i>color</i>")</tt>, then the
649next <tt>call DAG.viewGraph()</tt> would hilight the node in the
650specified color (choices of colors can be found at <a
Chris Lattner302da1e2007-02-03 03:05:57 +0000651href="http://www.graphviz.org/doc/info/colors.html">colors</a>.) More
Jim Laskey543a0ee2006-10-02 12:28:07 +0000652complex node attributes can be provided with <tt>call
653DAG.setGraphAttrs(<i>node</i>, "<i>attributes</i>")</tt> (choices can be
654found at <a href="http://www.graphviz.org/doc/info/attrs.html">Graph
655Attributes</a>.) If you want to restart and clear all the current graph
656attributes, then you can <tt>call DAG.clearGraphAttrs()</tt>. </p>
657
Chris Lattnerf623a082005-10-17 01:36:23 +0000658</div>
659
Chris Lattner098129a2007-02-03 03:04:03 +0000660<!-- *********************************************************************** -->
661<div class="doc_section">
662 <a name="datastructure">Picking the Right Data Structure for a Task</a>
663</div>
664<!-- *********************************************************************** -->
665
666<div class="doc_text">
667
668<p>LLVM has a plethora of datastructures in the <tt>llvm/ADT/</tt> directory,
669 and we commonly use STL datastructures. This section describes the tradeoffs
670 you should consider when you pick one.</p>
671
672<p>
673The first step is a choose your own adventure: do you want a sequential
674container, a set-like container, or a map-like container? The most important
675thing when choosing a container is the algorithmic properties of how you plan to
676access the container. Based on that, you should use:</p>
677
678<ul>
679<li>a <a href="#ds_map">map-like</a> container if you need efficient lookup
680 of an value based on another value. Map-like containers also support
681 efficient queries for containment (whether a key is in the map). Map-like
682 containers generally do not support efficient reverse mapping (values to
683 keys). If you need that, use two maps. Some map-like containers also
684 support efficient iteration through the keys in sorted order. Map-like
685 containers are the most expensive sort, only use them if you need one of
686 these capabilities.</li>
687
688<li>a <a href="#ds_set">set-like</a> container if you need to put a bunch of
689 stuff into a container that automatically eliminates duplicates. Some
690 set-like containers support efficient iteration through the elements in
691 sorted order. Set-like containers are more expensive than sequential
692 containers.
693</li>
694
695<li>a <a href="#ds_sequential">sequential</a> container provides
696 the most efficient way to add elements and keeps track of the order they are
697 added to the collection. They permit duplicates and support efficient
698 iteration, but do not support efficient lookup based on a key.
699</li>
700
701</ul>
702
703<p>
704Once the proper catagory of container is determined, you can fine tune the
705memory use, constant factors, and cache behaviors of access by intelligently
706picking a member of the catagory. Note that constant factors and cache behavior
707can be a big deal. If you have a vector that usually only contains a few
708elements (but could contain many), for example, it's much better to use
709<a href="#dss_smallvector">SmallVector</a> than <a href="#dss_vector">vector</a>
710. Doing so avoids (relatively) expensive malloc/free calls, which dwarf the
711cost of adding the elements to the container. </p>
712
713</div>
714
715<!-- ======================================================================= -->
716<div class="doc_subsection">
717 <a name="ds_sequential">Sequential Containers (std::vector, std::list, etc)</a>
718</div>
719
720<div class="doc_text">
721There are a variety of sequential containers available for you, based on your
722needs. Pick the first in this section that will do what you want.
723</div>
724
725<!-- _______________________________________________________________________ -->
726<div class="doc_subsubsection">
727 <a name="dss_fixedarrays">Fixed Size Arrays</a>
728</div>
729
730<div class="doc_text">
731<p>Fixed size arrays are very simple and very fast. They are good if you know
732exactly how many elements you have, or you have a (low) upper bound on how many
733you have.</p>
734</div>
735
736<!-- _______________________________________________________________________ -->
737<div class="doc_subsubsection">
738 <a name="dss_heaparrays">Heap Allocated Arrays</a>
739</div>
740
741<div class="doc_text">
742<p>Heap allocated arrays (new[] + delete[]) are also simple. They are good if
743the number of elements is variable, if you know how many elements you will need
744before the array is allocated, and if the array is usually large (if not,
745consider a <a href="#dss_smallvector">SmallVector</a>). The cost of a heap
746allocated array is the cost of the new/delete (aka malloc/free). Also note that
747if you are allocating an array of a type with a constructor, the constructor and
748destructors will be run for every element in the array (resizable vectors only
749construct those elements actually used).</p>
750</div>
751
752<!-- _______________________________________________________________________ -->
753<div class="doc_subsubsection">
754 <a name="dss_smallvector">"llvm/ADT/SmallVector.h"</a>
755</div>
756
757<div class="doc_text">
758<p><tt>SmallVector&lt;Type, N&gt;</tt> is a simple class that looks and smells
759just like <tt>vector&lt;Type&gt;</tt>:
760it supports efficient iteration, lays out elements in memory order (so you can
761do pointer arithmetic between elements), supports efficient push_back/pop_back
762operations, supports efficient random access to its elements, etc.</p>
763
764<p>The advantage of SmallVector is that it allocates space for
765some number of elements (N) <b>in the object itself</b>. Because of this, if
766the SmallVector is dynamically smaller than N, no malloc is performed. This can
767be a big win in cases where the malloc/free call is far more expensive than the
768code that fiddles around with the elements.</p>
769
770<p>This is good for vectors that are "usually small" (e.g. the number of
771predecessors/successors of a block is usually less than 8). On the other hand,
772this makes the size of the SmallVector itself large, so you don't want to
773allocate lots of them (doing so will waste a lot of space). As such,
774SmallVectors are most useful when on the stack.</p>
775
776<p>SmallVector also provides a nice portable and efficient replacement for
777<tt>alloca</tt>.</p>
778
779</div>
780
781<!-- _______________________________________________________________________ -->
782<div class="doc_subsubsection">
783 <a name="dss_vector">&lt;vector&gt;</a>
784</div>
785
786<div class="doc_text">
787<p>
788std::vector is well loved and respected. It is useful when SmallVector isn't:
789when the size of the vector is often large (thus the small optimization will
790rarely be a benefit) or if you will be allocating many instances of the vector
791itself (which would waste space for elements that aren't in the container).
792vector is also useful when interfacing with code that expects vectors :).
793</p>
794</div>
795
796<!-- _______________________________________________________________________ -->
797<div class="doc_subsubsection">
Chris Lattner74c4ca12007-02-03 07:59:07 +0000798 <a name="dss_deque">&lt;deque&gt;</a>
799</div>
800
801<div class="doc_text">
802<p>std::deque is, in some senses, a generalized version of std::vector. Like
803std::vector, it provides constant time random access and other similar
804properties, but it also provides efficient access to the front of the list. It
805does not guarantee continuity of elements within memory.</p>
806
807<p>In exchange for this extra flexibility, std::deque has significantly higher
808constant factor costs than std::vector. If possible, use std::vector or
809something cheaper.</p>
810</div>
811
812<!-- _______________________________________________________________________ -->
813<div class="doc_subsubsection">
Chris Lattner098129a2007-02-03 03:04:03 +0000814 <a name="dss_list">&lt;list&gt;</a>
815</div>
816
817<div class="doc_text">
818<p>std::list is an extremely inefficient class that is rarely useful.
819It performs a heap allocation for every element inserted into it, thus having an
820extremely high constant factor, particularly for small data types. std::list
821also only supports bidirectional iteration, not random access iteration.</p>
822
823<p>In exchange for this high cost, std::list supports efficient access to both
824ends of the list (like std::deque, but unlike std::vector or SmallVector). In
825addition, the iterator invalidation characteristics of std::list are stronger
826than that of a vector class: inserting or removing an element into the list does
827not invalidate iterator or pointers to other elements in the list.</p>
828</div>
829
830<!-- _______________________________________________________________________ -->
831<div class="doc_subsubsection">
832 <a name="dss_ilist">llvm/ADT/ilist</a>
833</div>
834
835<div class="doc_text">
836<p><tt>ilist&lt;T&gt;</tt> implements an 'intrusive' doubly-linked list. It is
837intrusive, because it requires the element to store and provide access to the
838prev/next pointers for the list.</p>
839
840<p>ilist has the same drawbacks as std::list, and additionally requires an
841ilist_traits implementation for the element type, but it provides some novel
842characteristics. In particular, it can efficiently store polymorphic objects,
843the traits class is informed when an element is inserted or removed from the
844list, and ilists are guaranteed to support a constant-time splice operation.
845</p>
846
847<p>These properties are exactly what we want for things like Instructions and
848basic blocks, which is why these are implemented with ilists.</p>
849</div>
850
851<!-- _______________________________________________________________________ -->
852<div class="doc_subsubsection">
853 <a name="dss_other">Other options</a>
854</div>
855
856<div class="doc_text">
Chris Lattner74c4ca12007-02-03 07:59:07 +0000857<p>Other STL containers are available, such as std::string.</p>
Chris Lattner098129a2007-02-03 03:04:03 +0000858
859<p>There are also various STL adapter classes such as std::queue,
860std::priority_queue, std::stack, etc. These provide simplified access to an
861underlying container but don't affect the cost of the container itself.</p>
862
863</div>
864
865
866<!-- ======================================================================= -->
867<div class="doc_subsection">
868 <a name="ds_set">Set-Like Containers (std::set, SmallSet, SetVector, etc)</a>
869</div>
870
871<div class="doc_text">
872
Chris Lattner74c4ca12007-02-03 07:59:07 +0000873<p>Set-like containers are useful when you need to canonicalize multiple values
874into a single representation. There are several different choices for how to do
875this, providing various trade-offs.</p>
876
877</div>
878
879
880<!-- _______________________________________________________________________ -->
881<div class="doc_subsubsection">
882 <a name="dss_sortedvectorset">A sorted 'vector'</a>
883</div>
884
885<div class="doc_text">
886
Chris Lattner3b23a8c2007-02-03 08:10:45 +0000887<p>If you intend to insert a lot of elements, then do a lot of queries, a
888great approach is to use a vector (or other sequential container) with
Chris Lattner74c4ca12007-02-03 07:59:07 +0000889std::sort+std::unique to remove duplicates. This approach works really well if
Chris Lattner3b23a8c2007-02-03 08:10:45 +0000890your usage pattern has these two distinct phases (insert then query), and can be
891coupled with a good choice of <a href="#ds_sequential">sequential container</a>.
892</p>
893
894<p>
895This combination provides the several nice properties: the result data is
896contiguous in memory (good for cache locality), has few allocations, is easy to
897address (iterators in the final vector are just indices or pointers), and can be
898efficiently queried with a standard binary or radix search.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +0000899
900</div>
901
902<!-- _______________________________________________________________________ -->
903<div class="doc_subsubsection">
904 <a name="dss_smallset">"llvm/ADT/SmallSet.h"</a>
905</div>
906
907<div class="doc_text">
908
909<p>If you have a set-like datastructure that is usually small and whose elements
Chris Lattner4ddfac12007-02-03 07:59:51 +0000910are reasonably small, a <tt>SmallSet&lt;Type, N&gt;</tt> is a good choice. This set
Chris Lattner74c4ca12007-02-03 07:59:07 +0000911has space for N elements in place (thus, if the set is dynamically smaller than
Chris Lattner14868db2007-02-03 08:20:15 +0000912N, no malloc traffic is required) and accesses them with a simple linear search.
913When the set grows beyond 'N' elements, it allocates a more expensive representation that
Chris Lattner74c4ca12007-02-03 07:59:07 +0000914guarantees efficient access (for most types, it falls back to std::set, but for
Chris Lattner14868db2007-02-03 08:20:15 +0000915pointers it uses something far better, <a
Chris Lattner74c4ca12007-02-03 07:59:07 +0000916href="#dss_smallptrset">SmallPtrSet</a>).</p>
917
918<p>The magic of this class is that it handles small sets extremely efficiently,
919but gracefully handles extremely large sets without loss of efficiency. The
920drawback is that the interface is quite small: it supports insertion, queries
921and erasing, but does not support iteration.</p>
922
923</div>
924
925<!-- _______________________________________________________________________ -->
926<div class="doc_subsubsection">
927 <a name="dss_smallptrset">"llvm/ADT/SmallPtrSet.h"</a>
928</div>
929
930<div class="doc_text">
931
932<p>SmallPtrSet has all the advantages of SmallSet (and a SmallSet of pointers is
933transparently implemented with a SmallPtrSet), but also suports iterators. If
Chris Lattner14868db2007-02-03 08:20:15 +0000934more than 'N' insertions are performed, a single quadratically
Chris Lattner74c4ca12007-02-03 07:59:07 +0000935probed hash table is allocated and grows as needed, providing extremely
936efficient access (constant time insertion/deleting/queries with low constant
937factors) and is very stingy with malloc traffic.</p>
938
939<p>Note that, unlike std::set, the iterators of SmallPtrSet are invalidated
940whenever an insertion occurs. Also, the values visited by the iterators are not
941visited in sorted order.</p>
942
943</div>
944
945<!-- _______________________________________________________________________ -->
946<div class="doc_subsubsection">
947 <a name="dss_FoldingSet">"llvm/ADT/FoldingSet.h"</a>
948</div>
949
950<div class="doc_text">
951
Chris Lattner098129a2007-02-03 03:04:03 +0000952<p>
Chris Lattner74c4ca12007-02-03 07:59:07 +0000953FoldingSet is an aggregate class that is really good at uniquing
954expensive-to-create or polymorphic objects. It is a combination of a chained
955hash table with intrusive links (uniqued objects are required to inherit from
Chris Lattner14868db2007-02-03 08:20:15 +0000956FoldingSetNode) that uses <a href="#dss_smallvector">SmallVector</a> as part of
957its ID process.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +0000958
Chris Lattner14868db2007-02-03 08:20:15 +0000959<p>Consider a case where you want to implement a "getOrCreateFoo" method for
Chris Lattner74c4ca12007-02-03 07:59:07 +0000960a complex object (for example, a node in the code generator). The client has a
961description of *what* it wants to generate (it knows the opcode and all the
962operands), but we don't want to 'new' a node, then try inserting it into a set
Chris Lattner14868db2007-02-03 08:20:15 +0000963only to find out it already exists, at which point we would have to delete it
964and return the node that already exists.
Chris Lattner098129a2007-02-03 03:04:03 +0000965</p>
966
Chris Lattner74c4ca12007-02-03 07:59:07 +0000967<p>To support this style of client, FoldingSet perform a query with a
968FoldingSetNodeID (which wraps SmallVector) that can be used to describe the
969element that we want to query for. The query either returns the element
970matching the ID or it returns an opaque ID that indicates where insertion should
Chris Lattner14868db2007-02-03 08:20:15 +0000971take place. Construction of the ID usually does not require heap traffic.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +0000972
973<p>Because FoldingSet uses intrusive links, it can support polymorphic objects
974in the set (for example, you can have SDNode instances mixed with LoadSDNodes).
975Because the elements are individually allocated, pointers to the elements are
976stable: inserting or removing elements does not invalidate any pointers to other
977elements.
978</p>
979
980</div>
981
982<!-- _______________________________________________________________________ -->
983<div class="doc_subsubsection">
984 <a name="dss_set">&lt;set&gt;</a>
985</div>
986
987<div class="doc_text">
988
Chris Lattner14868db2007-02-03 08:20:15 +0000989<p><tt>std::set</t> is a reasonable all-around set class, which is good at many
990things but great at nothing. std::set allocates memory for each element
Chris Lattner74c4ca12007-02-03 07:59:07 +0000991inserted (thus it is very malloc intensive) and typically stores three pointers
Chris Lattner14868db2007-02-03 08:20:15 +0000992per element in the set (thus adding a large amount of per-element space
993overhead). It offers guaranteed log(n) performance, which is not particularly
994fast, particularly if the elements of the set are expensive to compare (e.g.
995strings).</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +0000996
Chris Lattner14868db2007-02-03 08:20:15 +0000997<p>The advantages of std::set are that its iterators are stable (deleting or
Chris Lattner74c4ca12007-02-03 07:59:07 +0000998inserting an element from the set does not affect iterators or pointers to other
999elements) and that iteration over the set is guaranteed to be in sorted order.
1000If the elements in the set are large, then the relative overhead of the pointers
1001and malloc traffic is not a big deal, but if the elements of the set are small,
1002std::set is almost never a good choice.</p>
1003
1004</div>
1005
1006<!-- _______________________________________________________________________ -->
1007<div class="doc_subsubsection">
1008 <a name="dss_setvector">"llvm/ADT/SetVector.h"</a>
1009</div>
1010
1011<div class="doc_text">
1012<p>LLVM's SetVector&lt;Type&gt; is actually a combination of a set along with
1013a <a href="#ds_sequential">Sequential Container</a>. The important property
1014that this provides is efficient insertion with uniquing (duplicate elements are
1015ignored) with iteration support. It implements this by inserting elements into
1016both a set-like container and the sequential container, using the set-like
1017container for uniquing and the sequential container for iteration.
1018</p>
1019
1020<p>The difference between SetVector and other sets is that the order of
1021iteration is guaranteed to match the order of insertion into the SetVector.
1022This property is really important for things like sets of pointers. Because
1023pointer values are non-deterministic (e.g. vary across runs of the program on
1024different machines), iterating over the pointers in a std::set or other set will
1025not be in a well-defined order.</p>
1026
1027<p>
1028The drawback of SetVector is that it requires twice as much space as a normal
1029set and has the sum of constant factors from the set-like container and the
1030sequential container that it uses. Use it *only* if you need to iterate over
1031the elements in a deterministic order. SetVector is also expensive to delete
1032elements out of (linear time).
1033</p>
1034
1035</div>
1036
1037<!-- _______________________________________________________________________ -->
1038<div class="doc_subsubsection">
1039 <a name="dss_otherset">Other Options</a>
1040</div>
1041
1042<div class="doc_text">
1043
1044<p>
1045The STL provides several other options, such as std::multiset and the various
1046"hash_set" like containers (whether from C++TR1 or from the SGI library).</p>
1047
1048<p>std::multiset is useful if you're not interested in elimination of
Chris Lattner14868db2007-02-03 08:20:15 +00001049duplicates, but has all the drawbacks of std::set. A sorted vector (where you
1050don't delete duplicate entries) or some other approach is almost always
1051better.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +00001052
1053<p>The various hash_set implementations (exposed portably by
Chris Lattner14868db2007-02-03 08:20:15 +00001054"llvm/ADT/hash_set") is a simple chained hashtable. This algorithm is as malloc
1055intensive as std::set (performing an allocation for each element inserted,
Chris Lattner74c4ca12007-02-03 07:59:07 +00001056thus having really high constant factors) but (usually) provides O(1)
1057insertion/deletion of elements. This can be useful if your elements are large
Chris Lattner14868db2007-02-03 08:20:15 +00001058(thus making the constant-factor cost relatively low) or if comparisons are
1059expensive. Element iteration does not visit elements in a useful order.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +00001060
Chris Lattner098129a2007-02-03 03:04:03 +00001061</div>
1062
1063<!-- ======================================================================= -->
1064<div class="doc_subsection">
1065 <a name="ds_map">Map-Like Containers (std::map, DenseMap, etc)</a>
1066</div>
1067
1068<div class="doc_text">
1069sorted vector
1070std::map
1071DenseMap
Chris Lattner74c4ca12007-02-03 07:59:07 +00001072UniqueVector
Chris Lattner098129a2007-02-03 03:04:03 +00001073IndexedMap
1074hash_map
1075CStringMap
1076</div>
1077
Chris Lattnerf623a082005-10-17 01:36:23 +00001078
Misha Brukman13fd15c2004-01-15 00:14:41 +00001079<!-- *********************************************************************** -->
1080<div class="doc_section">
1081 <a name="common">Helpful Hints for Common Operations</a>
1082</div>
1083<!-- *********************************************************************** -->
1084
1085<div class="doc_text">
1086
1087<p>This section describes how to perform some very simple transformations of
1088LLVM code. This is meant to give examples of common idioms used, showing the
1089practical side of LLVM transformations. <p> Because this is a "how-to" section,
1090you should also read about the main classes that you will be working with. The
1091<a href="#coreclasses">Core LLVM Class Hierarchy Reference</a> contains details
1092and descriptions of the main classes that you should know about.</p>
1093
1094</div>
1095
1096<!-- NOTE: this section should be heavy on example code -->
1097<!-- ======================================================================= -->
1098<div class="doc_subsection">
1099 <a name="inspection">Basic Inspection and Traversal Routines</a>
1100</div>
1101
1102<div class="doc_text">
1103
1104<p>The LLVM compiler infrastructure have many different data structures that may
1105be traversed. Following the example of the C++ standard template library, the
1106techniques used to traverse these various data structures are all basically the
1107same. For a enumerable sequence of values, the <tt>XXXbegin()</tt> function (or
1108method) returns an iterator to the start of the sequence, the <tt>XXXend()</tt>
1109function returns an iterator pointing to one past the last valid element of the
1110sequence, and there is some <tt>XXXiterator</tt> data type that is common
1111between the two operations.</p>
1112
1113<p>Because the pattern for iteration is common across many different aspects of
1114the program representation, the standard template library algorithms may be used
1115on them, and it is easier to remember how to iterate. First we show a few common
1116examples of the data structures that need to be traversed. Other data
1117structures are traversed in very similar ways.</p>
1118
1119</div>
1120
1121<!-- _______________________________________________________________________ -->
Chris Lattner69bf8a92004-05-23 21:06:58 +00001122<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00001123 <a name="iterate_function">Iterating over the </a><a
1124 href="#BasicBlock"><tt>BasicBlock</tt></a>s in a <a
1125 href="#Function"><tt>Function</tt></a>
1126</div>
1127
1128<div class="doc_text">
1129
1130<p>It's quite common to have a <tt>Function</tt> instance that you'd like to
1131transform in some way; in particular, you'd like to manipulate its
1132<tt>BasicBlock</tt>s. To facilitate this, you'll need to iterate over all of
1133the <tt>BasicBlock</tt>s that constitute the <tt>Function</tt>. The following is
1134an example that prints the name of a <tt>BasicBlock</tt> and the number of
1135<tt>Instruction</tt>s it contains:</p>
1136
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001137<div class="doc_code">
1138<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001139// <i>func is a pointer to a Function instance</i>
1140for (Function::iterator i = func-&gt;begin(), e = func-&gt;end(); i != e; ++i)
1141 // <i>Print out the name of the basic block if it has one, and then the</i>
1142 // <i>number of instructions that it contains</i>
Bill Wendling832171c2006-12-07 20:04:42 +00001143 llvm::cerr &lt;&lt; "Basic block (name=" &lt;&lt; i-&gt;getName() &lt;&lt; ") has "
1144 &lt;&lt; i-&gt;size() &lt;&lt; " instructions.\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001145</pre>
1146</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001147
1148<p>Note that i can be used as if it were a pointer for the purposes of
Joel Stanley9b96c442002-09-06 21:55:13 +00001149invoking member functions of the <tt>Instruction</tt> class. This is
1150because the indirection operator is overloaded for the iterator
Chris Lattner7496ec52003-08-05 22:54:23 +00001151classes. In the above code, the expression <tt>i-&gt;size()</tt> is
Misha Brukman13fd15c2004-01-15 00:14:41 +00001152exactly equivalent to <tt>(*i).size()</tt> just like you'd expect.</p>
1153
1154</div>
1155
1156<!-- _______________________________________________________________________ -->
Chris Lattner69bf8a92004-05-23 21:06:58 +00001157<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00001158 <a name="iterate_basicblock">Iterating over the </a><a
1159 href="#Instruction"><tt>Instruction</tt></a>s in a <a
1160 href="#BasicBlock"><tt>BasicBlock</tt></a>
1161</div>
1162
1163<div class="doc_text">
1164
1165<p>Just like when dealing with <tt>BasicBlock</tt>s in <tt>Function</tt>s, it's
1166easy to iterate over the individual instructions that make up
1167<tt>BasicBlock</tt>s. Here's a code snippet that prints out each instruction in
1168a <tt>BasicBlock</tt>:</p>
1169
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001170<div class="doc_code">
Chris Lattner55c04612005-03-06 06:00:13 +00001171<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001172// <i>blk is a pointer to a BasicBlock instance</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001173for (BasicBlock::iterator i = blk-&gt;begin(), e = blk-&gt;end(); i != e; ++i)
Bill Wendling82e2eea2006-10-11 18:00:22 +00001174 // <i>The next statement works since operator&lt;&lt;(ostream&amp;,...)</i>
1175 // <i>is overloaded for Instruction&amp;</i>
Bill Wendling832171c2006-12-07 20:04:42 +00001176 llvm::cerr &lt;&lt; *i &lt;&lt; "\n";
Chris Lattner55c04612005-03-06 06:00:13 +00001177</pre>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001178</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001179
1180<p>However, this isn't really the best way to print out the contents of a
1181<tt>BasicBlock</tt>! Since the ostream operators are overloaded for virtually
1182anything you'll care about, you could have just invoked the print routine on the
Bill Wendling832171c2006-12-07 20:04:42 +00001183basic block itself: <tt>llvm::cerr &lt;&lt; *blk &lt;&lt; "\n";</tt>.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001184
1185</div>
1186
1187<!-- _______________________________________________________________________ -->
Chris Lattner69bf8a92004-05-23 21:06:58 +00001188<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00001189 <a name="iterate_institer">Iterating over the </a><a
1190 href="#Instruction"><tt>Instruction</tt></a>s in a <a
1191 href="#Function"><tt>Function</tt></a>
1192</div>
1193
1194<div class="doc_text">
1195
1196<p>If you're finding that you commonly iterate over a <tt>Function</tt>'s
1197<tt>BasicBlock</tt>s and then that <tt>BasicBlock</tt>'s <tt>Instruction</tt>s,
1198<tt>InstIterator</tt> should be used instead. You'll need to include <a
1199href="/doxygen/InstIterator_8h-source.html"><tt>llvm/Support/InstIterator.h</tt></a>,
1200and then instantiate <tt>InstIterator</tt>s explicitly in your code. Here's a
Chris Lattner69bf8a92004-05-23 21:06:58 +00001201small example that shows how to dump all instructions in a function to the standard error stream:<p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001202
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001203<div class="doc_code">
1204<pre>
1205#include "<a href="/doxygen/InstIterator_8h-source.html">llvm/Support/InstIterator.h</a>"
1206
Bill Wendling82e2eea2006-10-11 18:00:22 +00001207// <i>F is a ptr to a Function instance</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001208for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
Bill Wendling832171c2006-12-07 20:04:42 +00001209 llvm::cerr &lt;&lt; *i &lt;&lt; "\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001210</pre>
1211</div>
1212
1213<p>Easy, isn't it? You can also use <tt>InstIterator</tt>s to fill a
Joel Stanleye7be6502002-09-09 15:50:33 +00001214worklist with its initial contents. For example, if you wanted to
Chris Lattner261efe92003-11-25 01:02:51 +00001215initialize a worklist to contain all instructions in a <tt>Function</tt>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001216F, all you would need to do is something like:</p>
1217
1218<div class="doc_code">
1219<pre>
1220std::set&lt;Instruction*&gt; worklist;
1221worklist.insert(inst_begin(F), inst_end(F));
1222</pre>
1223</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001224
1225<p>The STL set <tt>worklist</tt> would now contain all instructions in the
1226<tt>Function</tt> pointed to by F.</p>
1227
1228</div>
1229
1230<!-- _______________________________________________________________________ -->
1231<div class="doc_subsubsection">
1232 <a name="iterate_convert">Turning an iterator into a class pointer (and
1233 vice-versa)</a>
1234</div>
1235
1236<div class="doc_text">
1237
1238<p>Sometimes, it'll be useful to grab a reference (or pointer) to a class
Joel Stanley9b96c442002-09-06 21:55:13 +00001239instance when all you've got at hand is an iterator. Well, extracting
Chris Lattner69bf8a92004-05-23 21:06:58 +00001240a reference or a pointer from an iterator is very straight-forward.
Chris Lattner261efe92003-11-25 01:02:51 +00001241Assuming that <tt>i</tt> is a <tt>BasicBlock::iterator</tt> and <tt>j</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001242is a <tt>BasicBlock::const_iterator</tt>:</p>
1243
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001244<div class="doc_code">
1245<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001246Instruction&amp; inst = *i; // <i>Grab reference to instruction reference</i>
1247Instruction* pinst = &amp;*i; // <i>Grab pointer to instruction reference</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001248const Instruction&amp; inst = *j;
1249</pre>
1250</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001251
1252<p>However, the iterators you'll be working with in the LLVM framework are
1253special: they will automatically convert to a ptr-to-instance type whenever they
1254need to. Instead of dereferencing the iterator and then taking the address of
1255the result, you can simply assign the iterator to the proper pointer type and
1256you get the dereference and address-of operation as a result of the assignment
1257(behind the scenes, this is a result of overloading casting mechanisms). Thus
1258the last line of the last example,</p>
1259
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001260<div class="doc_code">
1261<pre>
1262Instruction* pinst = &amp;*i;
1263</pre>
1264</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001265
1266<p>is semantically equivalent to</p>
1267
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001268<div class="doc_code">
1269<pre>
1270Instruction* pinst = i;
1271</pre>
1272</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001273
Chris Lattner69bf8a92004-05-23 21:06:58 +00001274<p>It's also possible to turn a class pointer into the corresponding iterator,
1275and this is a constant time operation (very efficient). The following code
1276snippet illustrates use of the conversion constructors provided by LLVM
1277iterators. By using these, you can explicitly grab the iterator of something
1278without actually obtaining it via iteration over some structure:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001279
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001280<div class="doc_code">
1281<pre>
1282void printNextInstruction(Instruction* inst) {
1283 BasicBlock::iterator it(inst);
Bill Wendling82e2eea2006-10-11 18:00:22 +00001284 ++it; // <i>After this line, it refers to the instruction after *inst</i>
Bill Wendling832171c2006-12-07 20:04:42 +00001285 if (it != inst-&gt;getParent()-&gt;end()) llvm::cerr &lt;&lt; *it &lt;&lt; "\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001286}
1287</pre>
1288</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001289
Misha Brukman13fd15c2004-01-15 00:14:41 +00001290</div>
1291
1292<!--_______________________________________________________________________-->
1293<div class="doc_subsubsection">
1294 <a name="iterate_complex">Finding call sites: a slightly more complex
1295 example</a>
1296</div>
1297
1298<div class="doc_text">
1299
1300<p>Say that you're writing a FunctionPass and would like to count all the
1301locations in the entire module (that is, across every <tt>Function</tt>) where a
1302certain function (i.e., some <tt>Function</tt>*) is already in scope. As you'll
1303learn later, you may want to use an <tt>InstVisitor</tt> to accomplish this in a
Chris Lattner69bf8a92004-05-23 21:06:58 +00001304much more straight-forward manner, but this example will allow us to explore how
Misha Brukman13fd15c2004-01-15 00:14:41 +00001305you'd do it if you didn't have <tt>InstVisitor</tt> around. In pseudocode, this
1306is what we want to do:</p>
1307
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001308<div class="doc_code">
1309<pre>
1310initialize callCounter to zero
1311for each Function f in the Module
1312 for each BasicBlock b in f
1313 for each Instruction i in b
1314 if (i is a CallInst and calls the given function)
1315 increment callCounter
1316</pre>
1317</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001318
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001319<p>And the actual code is (remember, because we're writing a
Misha Brukman13fd15c2004-01-15 00:14:41 +00001320<tt>FunctionPass</tt>, our <tt>FunctionPass</tt>-derived class simply has to
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001321override the <tt>runOnFunction</tt> method):</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001322
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001323<div class="doc_code">
1324<pre>
1325Function* targetFunc = ...;
1326
1327class OurFunctionPass : public FunctionPass {
1328 public:
1329 OurFunctionPass(): callCounter(0) { }
1330
1331 virtual runOnFunction(Function&amp; F) {
1332 for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) {
1333 for (BasicBlock::iterator i = b-&gt;begin(); ie = b-&gt;end(); i != ie; ++i) {
1334 if (<a href="#CallInst">CallInst</a>* callInst = <a href="#isa">dyn_cast</a>&lt;<a
1335 href="#CallInst">CallInst</a>&gt;(&amp;*i)) {
Bill Wendling82e2eea2006-10-11 18:00:22 +00001336 // <i>We know we've encountered a call instruction, so we</i>
1337 // <i>need to determine if it's a call to the</i>
1338 // <i>function pointed to by m_func or not</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001339
1340 if (callInst-&gt;getCalledFunction() == targetFunc)
1341 ++callCounter;
1342 }
1343 }
1344 }
Bill Wendling82e2eea2006-10-11 18:00:22 +00001345 }
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001346
1347 private:
1348 unsigned callCounter;
1349};
1350</pre>
1351</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001352
1353</div>
1354
Brian Gaekef1972c62003-11-07 19:25:45 +00001355<!--_______________________________________________________________________-->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001356<div class="doc_subsubsection">
1357 <a name="calls_and_invokes">Treating calls and invokes the same way</a>
1358</div>
1359
1360<div class="doc_text">
1361
1362<p>You may have noticed that the previous example was a bit oversimplified in
1363that it did not deal with call sites generated by 'invoke' instructions. In
1364this, and in other situations, you may find that you want to treat
1365<tt>CallInst</tt>s and <tt>InvokeInst</tt>s the same way, even though their
1366most-specific common base class is <tt>Instruction</tt>, which includes lots of
1367less closely-related things. For these cases, LLVM provides a handy wrapper
1368class called <a
Reid Spencer05fe4b02006-03-14 05:39:39 +00001369href="http://llvm.org/doxygen/classllvm_1_1CallSite.html"><tt>CallSite</tt></a>.
Chris Lattner69bf8a92004-05-23 21:06:58 +00001370It is essentially a wrapper around an <tt>Instruction</tt> pointer, with some
1371methods that provide functionality common to <tt>CallInst</tt>s and
Misha Brukman13fd15c2004-01-15 00:14:41 +00001372<tt>InvokeInst</tt>s.</p>
1373
Chris Lattner69bf8a92004-05-23 21:06:58 +00001374<p>This class has "value semantics": it should be passed by value, not by
1375reference and it should not be dynamically allocated or deallocated using
1376<tt>operator new</tt> or <tt>operator delete</tt>. It is efficiently copyable,
1377assignable and constructable, with costs equivalents to that of a bare pointer.
1378If you look at its definition, it has only a single pointer member.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001379
1380</div>
1381
Chris Lattner1a3105b2002-09-09 05:49:39 +00001382<!--_______________________________________________________________________-->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001383<div class="doc_subsubsection">
1384 <a name="iterate_chains">Iterating over def-use &amp; use-def chains</a>
1385</div>
1386
1387<div class="doc_text">
1388
1389<p>Frequently, we might have an instance of the <a
Chris Lattner00815172007-01-04 22:01:45 +00001390href="/doxygen/classllvm_1_1Value.html">Value Class</a> and we want to
Misha Brukman384047f2004-06-03 23:29:12 +00001391determine which <tt>User</tt>s use the <tt>Value</tt>. The list of all
1392<tt>User</tt>s of a particular <tt>Value</tt> is called a <i>def-use</i> chain.
1393For example, let's say we have a <tt>Function*</tt> named <tt>F</tt> to a
1394particular function <tt>foo</tt>. Finding all of the instructions that
1395<i>use</i> <tt>foo</tt> is as simple as iterating over the <i>def-use</i> chain
1396of <tt>F</tt>:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001397
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001398<div class="doc_code">
1399<pre>
1400Function* F = ...;
1401
Bill Wendling82e2eea2006-10-11 18:00:22 +00001402for (Value::use_iterator i = F-&gt;use_begin(), e = F-&gt;use_end(); i != e; ++i)
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001403 if (Instruction *Inst = dyn_cast&lt;Instruction&gt;(*i)) {
Bill Wendling832171c2006-12-07 20:04:42 +00001404 llvm::cerr &lt;&lt; "F is used in instruction:\n";
1405 llvm::cerr &lt;&lt; *Inst &lt;&lt; "\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001406 }
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001407</pre>
1408</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001409
1410<p>Alternately, it's common to have an instance of the <a
Misha Brukman384047f2004-06-03 23:29:12 +00001411href="/doxygen/classllvm_1_1User.html">User Class</a> and need to know what
Misha Brukman13fd15c2004-01-15 00:14:41 +00001412<tt>Value</tt>s are used by it. The list of all <tt>Value</tt>s used by a
1413<tt>User</tt> is known as a <i>use-def</i> chain. Instances of class
1414<tt>Instruction</tt> are common <tt>User</tt>s, so we might want to iterate over
1415all of the values that a particular instruction uses (that is, the operands of
1416the particular <tt>Instruction</tt>):</p>
1417
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001418<div class="doc_code">
1419<pre>
1420Instruction* pi = ...;
1421
1422for (User::op_iterator i = pi-&gt;op_begin(), e = pi-&gt;op_end(); i != e; ++i) {
1423 Value* v = *i;
Bill Wendling82e2eea2006-10-11 18:00:22 +00001424 // <i>...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001425}
1426</pre>
1427</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001428
Chris Lattner1a3105b2002-09-09 05:49:39 +00001429<!--
1430 def-use chains ("finding all users of"): Value::use_begin/use_end
1431 use-def chains ("finding all values used"): User::op_begin/op_end [op=operand]
Misha Brukman13fd15c2004-01-15 00:14:41 +00001432-->
1433
1434</div>
1435
1436<!-- ======================================================================= -->
1437<div class="doc_subsection">
1438 <a name="simplechanges">Making simple changes</a>
1439</div>
1440
1441<div class="doc_text">
1442
1443<p>There are some primitive transformation operations present in the LLVM
Joel Stanley753eb712002-09-11 22:32:24 +00001444infrastructure that are worth knowing about. When performing
Chris Lattner261efe92003-11-25 01:02:51 +00001445transformations, it's fairly common to manipulate the contents of basic
1446blocks. This section describes some of the common methods for doing so
Misha Brukman13fd15c2004-01-15 00:14:41 +00001447and gives example code.</p>
1448
1449</div>
1450
Chris Lattner261efe92003-11-25 01:02:51 +00001451<!--_______________________________________________________________________-->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001452<div class="doc_subsubsection">
1453 <a name="schanges_creating">Creating and inserting new
1454 <tt>Instruction</tt>s</a>
1455</div>
1456
1457<div class="doc_text">
1458
1459<p><i>Instantiating Instructions</i></p>
1460
Chris Lattner69bf8a92004-05-23 21:06:58 +00001461<p>Creation of <tt>Instruction</tt>s is straight-forward: simply call the
Misha Brukman13fd15c2004-01-15 00:14:41 +00001462constructor for the kind of instruction to instantiate and provide the necessary
1463parameters. For example, an <tt>AllocaInst</tt> only <i>requires</i> a
1464(const-ptr-to) <tt>Type</tt>. Thus:</p>
1465
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001466<div class="doc_code">
1467<pre>
1468AllocaInst* ai = new AllocaInst(Type::IntTy);
1469</pre>
1470</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001471
1472<p>will create an <tt>AllocaInst</tt> instance that represents the allocation of
1473one integer in the current stack frame, at runtime. Each <tt>Instruction</tt>
1474subclass is likely to have varying default parameters which change the semantics
1475of the instruction, so refer to the <a
Misha Brukman31ca1de2004-06-03 23:35:54 +00001476href="/doxygen/classllvm_1_1Instruction.html">doxygen documentation for the subclass of
Misha Brukman13fd15c2004-01-15 00:14:41 +00001477Instruction</a> that you're interested in instantiating.</p>
1478
1479<p><i>Naming values</i></p>
1480
1481<p>It is very useful to name the values of instructions when you're able to, as
1482this facilitates the debugging of your transformations. If you end up looking
1483at generated LLVM machine code, you definitely want to have logical names
1484associated with the results of instructions! By supplying a value for the
1485<tt>Name</tt> (default) parameter of the <tt>Instruction</tt> constructor, you
1486associate a logical name with the result of the instruction's execution at
1487runtime. For example, say that I'm writing a transformation that dynamically
1488allocates space for an integer on the stack, and that integer is going to be
1489used as some kind of index by some other code. To accomplish this, I place an
1490<tt>AllocaInst</tt> at the first point in the first <tt>BasicBlock</tt> of some
1491<tt>Function</tt>, and I'm intending to use it within the same
1492<tt>Function</tt>. I might do:</p>
1493
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001494<div class="doc_code">
1495<pre>
1496AllocaInst* pa = new AllocaInst(Type::IntTy, 0, "indexLoc");
1497</pre>
1498</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001499
1500<p>where <tt>indexLoc</tt> is now the logical name of the instruction's
1501execution value, which is a pointer to an integer on the runtime stack.</p>
1502
1503<p><i>Inserting instructions</i></p>
1504
1505<p>There are essentially two ways to insert an <tt>Instruction</tt>
1506into an existing sequence of instructions that form a <tt>BasicBlock</tt>:</p>
1507
Joel Stanley9dd1ad62002-09-18 03:17:23 +00001508<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001509 <li>Insertion into an explicit instruction list
1510
1511 <p>Given a <tt>BasicBlock* pb</tt>, an <tt>Instruction* pi</tt> within that
1512 <tt>BasicBlock</tt>, and a newly-created instruction we wish to insert
1513 before <tt>*pi</tt>, we do the following: </p>
1514
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001515<div class="doc_code">
1516<pre>
1517BasicBlock *pb = ...;
1518Instruction *pi = ...;
1519Instruction *newInst = new Instruction(...);
1520
Bill Wendling82e2eea2006-10-11 18:00:22 +00001521pb-&gt;getInstList().insert(pi, newInst); // <i>Inserts newInst before pi in pb</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001522</pre>
1523</div>
Alkis Evlogimenos9a5dc4f2004-05-27 00:57:51 +00001524
1525 <p>Appending to the end of a <tt>BasicBlock</tt> is so common that
1526 the <tt>Instruction</tt> class and <tt>Instruction</tt>-derived
1527 classes provide constructors which take a pointer to a
1528 <tt>BasicBlock</tt> to be appended to. For example code that
1529 looked like: </p>
1530
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001531<div class="doc_code">
1532<pre>
1533BasicBlock *pb = ...;
1534Instruction *newInst = new Instruction(...);
1535
Bill Wendling82e2eea2006-10-11 18:00:22 +00001536pb-&gt;getInstList().push_back(newInst); // <i>Appends newInst to pb</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001537</pre>
1538</div>
Alkis Evlogimenos9a5dc4f2004-05-27 00:57:51 +00001539
1540 <p>becomes: </p>
1541
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001542<div class="doc_code">
1543<pre>
1544BasicBlock *pb = ...;
1545Instruction *newInst = new Instruction(..., pb);
1546</pre>
1547</div>
Alkis Evlogimenos9a5dc4f2004-05-27 00:57:51 +00001548
1549 <p>which is much cleaner, especially if you are creating
1550 long instruction streams.</p></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001551
1552 <li>Insertion into an implicit instruction list
1553
1554 <p><tt>Instruction</tt> instances that are already in <tt>BasicBlock</tt>s
1555 are implicitly associated with an existing instruction list: the instruction
1556 list of the enclosing basic block. Thus, we could have accomplished the same
1557 thing as the above code without being given a <tt>BasicBlock</tt> by doing:
1558 </p>
1559
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001560<div class="doc_code">
1561<pre>
1562Instruction *pi = ...;
1563Instruction *newInst = new Instruction(...);
1564
1565pi-&gt;getParent()-&gt;getInstList().insert(pi, newInst);
1566</pre>
1567</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001568
1569 <p>In fact, this sequence of steps occurs so frequently that the
1570 <tt>Instruction</tt> class and <tt>Instruction</tt>-derived classes provide
1571 constructors which take (as a default parameter) a pointer to an
1572 <tt>Instruction</tt> which the newly-created <tt>Instruction</tt> should
1573 precede. That is, <tt>Instruction</tt> constructors are capable of
1574 inserting the newly-created instance into the <tt>BasicBlock</tt> of a
1575 provided instruction, immediately before that instruction. Using an
1576 <tt>Instruction</tt> constructor with a <tt>insertBefore</tt> (default)
1577 parameter, the above code becomes:</p>
1578
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001579<div class="doc_code">
1580<pre>
1581Instruction* pi = ...;
1582Instruction* newInst = new Instruction(..., pi);
1583</pre>
1584</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001585
1586 <p>which is much cleaner, especially if you're creating a lot of
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001587 instructions and adding them to <tt>BasicBlock</tt>s.</p></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001588</ul>
1589
1590</div>
1591
1592<!--_______________________________________________________________________-->
1593<div class="doc_subsubsection">
1594 <a name="schanges_deleting">Deleting <tt>Instruction</tt>s</a>
1595</div>
1596
1597<div class="doc_text">
1598
1599<p>Deleting an instruction from an existing sequence of instructions that form a
Chris Lattner69bf8a92004-05-23 21:06:58 +00001600<a href="#BasicBlock"><tt>BasicBlock</tt></a> is very straight-forward. First,
Misha Brukman13fd15c2004-01-15 00:14:41 +00001601you must have a pointer to the instruction that you wish to delete. Second, you
1602need to obtain the pointer to that instruction's basic block. You use the
1603pointer to the basic block to get its list of instructions and then use the
1604erase function to remove your instruction. For example:</p>
1605
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001606<div class="doc_code">
1607<pre>
1608<a href="#Instruction">Instruction</a> *I = .. ;
1609<a href="#BasicBlock">BasicBlock</a> *BB = I-&gt;getParent();
1610
1611BB-&gt;getInstList().erase(I);
1612</pre>
1613</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001614
1615</div>
1616
1617<!--_______________________________________________________________________-->
1618<div class="doc_subsubsection">
1619 <a name="schanges_replacing">Replacing an <tt>Instruction</tt> with another
1620 <tt>Value</tt></a>
1621</div>
1622
1623<div class="doc_text">
1624
1625<p><i>Replacing individual instructions</i></p>
1626
1627<p>Including "<a href="/doxygen/BasicBlockUtils_8h-source.html">llvm/Transforms/Utils/BasicBlockUtils.h</a>"
Chris Lattner261efe92003-11-25 01:02:51 +00001628permits use of two very useful replace functions: <tt>ReplaceInstWithValue</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001629and <tt>ReplaceInstWithInst</tt>.</p>
1630
Chris Lattner261efe92003-11-25 01:02:51 +00001631<h4><a name="schanges_deleting">Deleting <tt>Instruction</tt>s</a></h4>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001632
Chris Lattner261efe92003-11-25 01:02:51 +00001633<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001634 <li><tt>ReplaceInstWithValue</tt>
1635
1636 <p>This function replaces all uses (within a basic block) of a given
1637 instruction with a value, and then removes the original instruction. The
1638 following example illustrates the replacement of the result of a particular
Chris Lattner58360822005-01-17 00:12:04 +00001639 <tt>AllocaInst</tt> that allocates memory for a single integer with a null
Misha Brukman13fd15c2004-01-15 00:14:41 +00001640 pointer to an integer.</p>
1641
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001642<div class="doc_code">
1643<pre>
1644AllocaInst* instToReplace = ...;
1645BasicBlock::iterator ii(instToReplace);
1646
1647ReplaceInstWithValue(instToReplace-&gt;getParent()-&gt;getInstList(), ii,
1648 Constant::getNullValue(PointerType::get(Type::IntTy)));
1649</pre></div></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001650
1651 <li><tt>ReplaceInstWithInst</tt>
1652
1653 <p>This function replaces a particular instruction with another
1654 instruction. The following example illustrates the replacement of one
1655 <tt>AllocaInst</tt> with another.</p>
1656
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001657<div class="doc_code">
1658<pre>
1659AllocaInst* instToReplace = ...;
1660BasicBlock::iterator ii(instToReplace);
1661
1662ReplaceInstWithInst(instToReplace-&gt;getParent()-&gt;getInstList(), ii,
1663 new AllocaInst(Type::IntTy, 0, "ptrToReplacedInt"));
1664</pre></div></li>
Chris Lattner261efe92003-11-25 01:02:51 +00001665</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001666
1667<p><i>Replacing multiple uses of <tt>User</tt>s and <tt>Value</tt>s</i></p>
1668
1669<p>You can use <tt>Value::replaceAllUsesWith</tt> and
1670<tt>User::replaceUsesOfWith</tt> to change more than one use at a time. See the
Chris Lattner00815172007-01-04 22:01:45 +00001671doxygen documentation for the <a href="/doxygen/classllvm_1_1Value.html">Value Class</a>
Misha Brukman384047f2004-06-03 23:29:12 +00001672and <a href="/doxygen/classllvm_1_1User.html">User Class</a>, respectively, for more
Misha Brukman13fd15c2004-01-15 00:14:41 +00001673information.</p>
1674
1675<!-- Value::replaceAllUsesWith User::replaceUsesOfWith Point out:
1676include/llvm/Transforms/Utils/ especially BasicBlockUtils.h with:
1677ReplaceInstWithValue, ReplaceInstWithInst -->
1678
1679</div>
1680
Chris Lattner9355b472002-09-06 02:50:58 +00001681<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001682<div class="doc_section">
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001683 <a name="advanced">Advanced Topics</a>
1684</div>
1685<!-- *********************************************************************** -->
1686
1687<div class="doc_text">
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001688<p>
1689This section describes some of the advanced or obscure API's that most clients
1690do not need to be aware of. These API's tend manage the inner workings of the
1691LLVM system, and only need to be accessed in unusual circumstances.
1692</p>
1693</div>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001694
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001695<!-- ======================================================================= -->
1696<div class="doc_subsection">
1697 <a name="TypeResolve">LLVM Type Resolution</a>
1698</div>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001699
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001700<div class="doc_text">
1701
1702<p>
1703The LLVM type system has a very simple goal: allow clients to compare types for
1704structural equality with a simple pointer comparison (aka a shallow compare).
1705This goal makes clients much simpler and faster, and is used throughout the LLVM
1706system.
1707</p>
1708
1709<p>
1710Unfortunately achieving this goal is not a simple matter. In particular,
1711recursive types and late resolution of opaque types makes the situation very
1712difficult to handle. Fortunately, for the most part, our implementation makes
1713most clients able to be completely unaware of the nasty internal details. The
1714primary case where clients are exposed to the inner workings of it are when
1715building a recursive type. In addition to this case, the LLVM bytecode reader,
1716assembly parser, and linker also have to be aware of the inner workings of this
1717system.
1718</p>
1719
Chris Lattner0f876db2005-04-25 15:47:57 +00001720<p>
1721For our purposes below, we need three concepts. First, an "Opaque Type" is
1722exactly as defined in the <a href="LangRef.html#t_opaque">language
1723reference</a>. Second an "Abstract Type" is any type which includes an
Reid Spencer06565dc2007-01-12 17:11:23 +00001724opaque type as part of its type graph (for example "<tt>{ opaque, i32 }</tt>").
1725Third, a concrete type is a type that is not an abstract type (e.g. "<tt>{ i32,
Chris Lattner0f876db2005-04-25 15:47:57 +00001726float }</tt>").
1727</p>
1728
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001729</div>
1730
1731<!-- ______________________________________________________________________ -->
1732<div class="doc_subsubsection">
1733 <a name="BuildRecType">Basic Recursive Type Construction</a>
1734</div>
1735
1736<div class="doc_text">
1737
1738<p>
1739Because the most common question is "how do I build a recursive type with LLVM",
1740we answer it now and explain it as we go. Here we include enough to cause this
1741to be emitted to an output .ll file:
1742</p>
1743
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001744<div class="doc_code">
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001745<pre>
Reid Spencer06565dc2007-01-12 17:11:23 +00001746%mylist = type { %mylist*, i32 }
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001747</pre>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001748</div>
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001749
1750<p>
1751To build this, use the following LLVM APIs:
1752</p>
1753
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001754<div class="doc_code">
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001755<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001756// <i>Create the initial outer struct</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001757<a href="#PATypeHolder">PATypeHolder</a> StructTy = OpaqueType::get();
1758std::vector&lt;const Type*&gt; Elts;
1759Elts.push_back(PointerType::get(StructTy));
1760Elts.push_back(Type::IntTy);
1761StructType *NewSTy = StructType::get(Elts);
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001762
Reid Spencer06565dc2007-01-12 17:11:23 +00001763// <i>At this point, NewSTy = "{ opaque*, i32 }". Tell VMCore that</i>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001764// <i>the struct and the opaque type are actually the same.</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001765cast&lt;OpaqueType&gt;(StructTy.get())-&gt;<a href="#refineAbstractTypeTo">refineAbstractTypeTo</a>(NewSTy);
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001766
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001767// <i>NewSTy is potentially invalidated, but StructTy (a <a href="#PATypeHolder">PATypeHolder</a>) is</i>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001768// <i>kept up-to-date</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001769NewSTy = cast&lt;StructType&gt;(StructTy.get());
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001770
Bill Wendling82e2eea2006-10-11 18:00:22 +00001771// <i>Add a name for the type to the module symbol table (optional)</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001772MyModule-&gt;addTypeName("mylist", NewSTy);
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001773</pre>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001774</div>
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001775
1776<p>
1777This code shows the basic approach used to build recursive types: build a
1778non-recursive type using 'opaque', then use type unification to close the cycle.
1779The type unification step is performed by the <tt><a
Chris Lattneraff26d12007-02-03 03:06:52 +00001780href="#refineAbstractTypeTo">refineAbstractTypeTo</a></tt> method, which is
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001781described next. After that, we describe the <a
1782href="#PATypeHolder">PATypeHolder class</a>.
1783</p>
1784
1785</div>
1786
1787<!-- ______________________________________________________________________ -->
1788<div class="doc_subsubsection">
1789 <a name="refineAbstractTypeTo">The <tt>refineAbstractTypeTo</tt> method</a>
1790</div>
1791
1792<div class="doc_text">
1793<p>
1794The <tt>refineAbstractTypeTo</tt> method starts the type unification process.
1795While this method is actually a member of the DerivedType class, it is most
1796often used on OpaqueType instances. Type unification is actually a recursive
1797process. After unification, types can become structurally isomorphic to
1798existing types, and all duplicates are deleted (to preserve pointer equality).
1799</p>
1800
1801<p>
1802In the example above, the OpaqueType object is definitely deleted.
Reid Spencer06565dc2007-01-12 17:11:23 +00001803Additionally, if there is an "{ \2*, i32}" type already created in the system,
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001804the pointer and struct type created are <b>also</b> deleted. Obviously whenever
1805a type is deleted, any "Type*" pointers in the program are invalidated. As
1806such, it is safest to avoid having <i>any</i> "Type*" pointers to abstract types
1807live across a call to <tt>refineAbstractTypeTo</tt> (note that non-abstract
1808types can never move or be deleted). To deal with this, the <a
1809href="#PATypeHolder">PATypeHolder</a> class is used to maintain a stable
1810reference to a possibly refined type, and the <a
1811href="#AbstractTypeUser">AbstractTypeUser</a> class is used to update more
1812complex datastructures.
1813</p>
1814
1815</div>
1816
1817<!-- ______________________________________________________________________ -->
1818<div class="doc_subsubsection">
1819 <a name="PATypeHolder">The PATypeHolder Class</a>
1820</div>
1821
1822<div class="doc_text">
1823<p>
1824PATypeHolder is a form of a "smart pointer" for Type objects. When VMCore
1825happily goes about nuking types that become isomorphic to existing types, it
1826automatically updates all PATypeHolder objects to point to the new type. In the
1827example above, this allows the code to maintain a pointer to the resultant
1828resolved recursive type, even though the Type*'s are potentially invalidated.
1829</p>
1830
1831<p>
1832PATypeHolder is an extremely light-weight object that uses a lazy union-find
1833implementation to update pointers. For example the pointer from a Value to its
1834Type is maintained by PATypeHolder objects.
1835</p>
1836
1837</div>
1838
1839<!-- ______________________________________________________________________ -->
1840<div class="doc_subsubsection">
1841 <a name="AbstractTypeUser">The AbstractTypeUser Class</a>
1842</div>
1843
1844<div class="doc_text">
1845
1846<p>
1847Some data structures need more to perform more complex updates when types get
1848resolved. The <a href="#SymbolTable">SymbolTable</a> class, for example, needs
1849move and potentially merge type planes in its representation when a pointer
1850changes.</p>
1851
1852<p>
1853To support this, a class can derive from the AbstractTypeUser class. This class
1854allows it to get callbacks when certain types are resolved. To register to get
1855callbacks for a particular type, the DerivedType::{add/remove}AbstractTypeUser
Chris Lattner0f876db2005-04-25 15:47:57 +00001856methods can be called on a type. Note that these methods only work for <i>
Reid Spencer06565dc2007-01-12 17:11:23 +00001857 abstract</i> types. Concrete types (those that do not include any opaque
1858objects) can never be refined.
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001859</p>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001860</div>
1861
1862
1863<!-- ======================================================================= -->
1864<div class="doc_subsection">
1865 <a name="SymbolTable">The <tt>SymbolTable</tt> class</a>
1866</div>
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001867
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001868<div class="doc_text">
1869<p>This class provides a symbol table that the <a
1870href="#Function"><tt>Function</tt></a> and <a href="#Module">
1871<tt>Module</tt></a> classes use for naming definitions. The symbol table can
Reid Spencera6362242007-01-07 00:41:39 +00001872provide a name for any <a href="#Value"><tt>Value</tt></a>.
1873<tt>SymbolTable</tt> is an abstract data type. It hides the data it contains
1874and provides access to it through a controlled interface.</p>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001875
Reid Spencera6362242007-01-07 00:41:39 +00001876<p>Note that the <tt>SymbolTable</tt> class should not be directly accessed
1877by most clients. It should only be used when iteration over the symbol table
1878names themselves are required, which is very special purpose. Note that not
1879all LLVM
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001880<a href="#Value">Value</a>s have names, and those without names (i.e. they have
1881an empty name) do not exist in the symbol table.
1882</p>
1883
1884<p>To use the <tt>SymbolTable</tt> well, you need to understand the
1885structure of the information it holds. The class contains two
1886<tt>std::map</tt> objects. The first, <tt>pmap</tt>, is a map of
1887<tt>Type*</tt> to maps of name (<tt>std::string</tt>) to <tt>Value*</tt>.
Reid Spencera6362242007-01-07 00:41:39 +00001888Thus, Values are stored in two-dimensions and accessed by <tt>Type</tt> and
1889name.</p>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001890
1891<p>The interface of this class provides three basic types of operations:
1892<ol>
1893 <li><em>Accessors</em>. Accessors provide read-only access to information
1894 such as finding a value for a name with the
1895 <a href="#SymbolTable_lookup">lookup</a> method.</li>
1896 <li><em>Mutators</em>. Mutators allow the user to add information to the
1897 <tt>SymbolTable</tt> with methods like
1898 <a href="#SymbolTable_insert"><tt>insert</tt></a>.</li>
1899 <li><em>Iterators</em>. Iterators allow the user to traverse the content
1900 of the symbol table in well defined ways, such as the method
Reid Spencera6362242007-01-07 00:41:39 +00001901 <a href="#SymbolTable_plane_begin"><tt>plane_begin</tt></a>.</li>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001902</ol>
1903
1904<h3>Accessors</h3>
1905<dl>
1906 <dt><tt>Value* lookup(const Type* Ty, const std::string&amp; name) const</tt>:
1907 </dt>
1908 <dd>The <tt>lookup</tt> method searches the type plane given by the
1909 <tt>Ty</tt> parameter for a <tt>Value</tt> with the provided <tt>name</tt>.
1910 If a suitable <tt>Value</tt> is not found, null is returned.</dd>
1911
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001912 <dt><tt>bool isEmpty() const</tt>:</dt>
1913 <dd>This function returns true if both the value and types maps are
1914 empty</dd>
1915</dl>
1916
1917<h3>Mutators</h3>
1918<dl>
1919 <dt><tt>void insert(Value *Val)</tt>:</dt>
1920 <dd>This method adds the provided value to the symbol table. The Value must
1921 have both a name and a type which are extracted and used to place the value
1922 in the correct type plane under the value's name.</dd>
1923
1924 <dt><tt>void insert(const std::string&amp; Name, Value *Val)</tt>:</dt>
1925 <dd> Inserts a constant or type into the symbol table with the specified
1926 name. There can be a many to one mapping between names and constants
1927 or types.</dd>
1928
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001929 <dt><tt>void remove(Value* Val)</tt>:</dt>
1930 <dd> This method removes a named value from the symbol table. The
1931 type and name of the Value are extracted from \p N and used to
1932 lookup the Value in the correct type plane. If the Value is
1933 not in the symbol table, this method silently ignores the
1934 request.</dd>
1935
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001936 <dt><tt>Value* remove(const std::string&amp; Name, Value *Val)</tt>:</dt>
1937 <dd> Remove a constant or type with the specified name from the
1938 symbol table.</dd>
1939
Reid Spencera6362242007-01-07 00:41:39 +00001940 <dt><tt>Value *remove(const value_iterator&amp; It)</tt>:</dt>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001941 <dd> Removes a specific value from the symbol table.
1942 Returns the removed value.</dd>
1943
1944 <dt><tt>bool strip()</tt>:</dt>
1945 <dd> This method will strip the symbol table of its names leaving
1946 the type and values. </dd>
1947
1948 <dt><tt>void clear()</tt>:</dt>
1949 <dd>Empty the symbol table completely.</dd>
1950</dl>
1951
1952<h3>Iteration</h3>
1953<p>The following functions describe three types of iterators you can obtain
1954the beginning or end of the sequence for both const and non-const. It is
1955important to keep track of the different kinds of iterators. There are
1956three idioms worth pointing out:</p>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001957
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001958<table>
1959 <tr><th>Units</th><th>Iterator</th><th>Idiom</th></tr>
1960 <tr>
1961 <td align="left">Planes Of name/Value maps</td><td>PI</td>
1962 <td align="left"><pre><tt>
1963for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
1964 PE = ST.plane_end(); PI != PE; ++PI ) {
Bill Wendling82e2eea2006-10-11 18:00:22 +00001965 PI-&gt;first // <i>This is the Type* of the plane</i>
1966 PI-&gt;second // <i>This is the SymbolTable::ValueMap of name/Value pairs</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001967}
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001968 </tt></pre></td>
1969 </tr>
1970 <tr>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001971 <td align="left">name/Value pairs in a plane</td><td>VI</td>
1972 <td align="left"><pre><tt>
1973for (SymbolTable::value_const_iterator VI = ST.value_begin(SomeType),
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001974 VE = ST.value_end(SomeType); VI != VE; ++VI ) {
Bill Wendling82e2eea2006-10-11 18:00:22 +00001975 VI-&gt;first // <i>This is the name of the Value</i>
1976 VI-&gt;second // <i>This is the Value* value associated with the name</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001977}
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001978 </tt></pre></td>
1979 </tr>
1980</table>
1981
1982<p>Using the recommended iterator names and idioms will help you avoid
1983making mistakes. Of particular note, make sure that whenever you use
1984value_begin(SomeType) that you always compare the resulting iterator
1985with value_end(SomeType) not value_end(SomeOtherType) or else you
1986will loop infinitely.</p>
1987
1988<dl>
1989
1990 <dt><tt>plane_iterator plane_begin()</tt>:</dt>
1991 <dd>Get an iterator that starts at the beginning of the type planes.
1992 The iterator will iterate over the Type/ValueMap pairs in the
1993 type planes. </dd>
1994
1995 <dt><tt>plane_const_iterator plane_begin() const</tt>:</dt>
1996 <dd>Get a const_iterator that starts at the beginning of the type
1997 planes. The iterator will iterate over the Type/ValueMap pairs
1998 in the type planes. </dd>
1999
2000 <dt><tt>plane_iterator plane_end()</tt>:</dt>
2001 <dd>Get an iterator at the end of the type planes. This serves as
2002 the marker for end of iteration over the type planes.</dd>
2003
2004 <dt><tt>plane_const_iterator plane_end() const</tt>:</dt>
2005 <dd>Get a const_iterator at the end of the type planes. This serves as
2006 the marker for end of iteration over the type planes.</dd>
2007
2008 <dt><tt>value_iterator value_begin(const Type *Typ)</tt>:</dt>
2009 <dd>Get an iterator that starts at the beginning of a type plane.
2010 The iterator will iterate over the name/value pairs in the type plane.
2011 Note: The type plane must already exist before using this.</dd>
2012
2013 <dt><tt>value_const_iterator value_begin(const Type *Typ) const</tt>:</dt>
2014 <dd>Get a const_iterator that starts at the beginning of a type plane.
2015 The iterator will iterate over the name/value pairs in the type plane.
2016 Note: The type plane must already exist before using this.</dd>
2017
2018 <dt><tt>value_iterator value_end(const Type *Typ)</tt>:</dt>
2019 <dd>Get an iterator to the end of a type plane. This serves as the marker
2020 for end of iteration of the type plane.
2021 Note: The type plane must already exist before using this.</dd>
2022
2023 <dt><tt>value_const_iterator value_end(const Type *Typ) const</tt>:</dt>
2024 <dd>Get a const_iterator to the end of a type plane. This serves as the
2025 marker for end of iteration of the type plane.
2026 Note: the type plane must already exist before using this.</dd>
2027
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002028 <dt><tt>plane_const_iterator find(const Type* Typ ) const</tt>:</dt>
2029 <dd>This method returns a plane_const_iterator for iteration over
2030 the type planes starting at a specific plane, given by \p Ty.</dd>
2031
2032 <dt><tt>plane_iterator find( const Type* Typ </tt>:</dt>
2033 <dd>This method returns a plane_iterator for iteration over the
2034 type planes starting at a specific plane, given by \p Ty.</dd>
2035
2036</dl>
2037</div>
2038
2039
2040
2041<!-- *********************************************************************** -->
2042<div class="doc_section">
Misha Brukman13fd15c2004-01-15 00:14:41 +00002043 <a name="coreclasses">The Core LLVM Class Hierarchy Reference </a>
2044</div>
2045<!-- *********************************************************************** -->
2046
2047<div class="doc_text">
Reid Spencer303c4b42007-01-12 17:26:25 +00002048<p><tt>#include "<a href="/doxygen/Type_8h-source.html">llvm/Type.h</a>"</tt>
2049<br>doxygen info: <a href="/doxygen/classllvm_1_1Type.html">Type Class</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002050
2051<p>The Core LLVM classes are the primary means of representing the program
Chris Lattner261efe92003-11-25 01:02:51 +00002052being inspected or transformed. The core LLVM classes are defined in
2053header files in the <tt>include/llvm/</tt> directory, and implemented in
Misha Brukman13fd15c2004-01-15 00:14:41 +00002054the <tt>lib/VMCore</tt> directory.</p>
2055
2056</div>
2057
2058<!-- ======================================================================= -->
2059<div class="doc_subsection">
Reid Spencer303c4b42007-01-12 17:26:25 +00002060 <a name="Type">The <tt>Type</tt> class and Derived Types</a>
2061</div>
2062
2063<div class="doc_text">
2064
2065 <p><tt>Type</tt> is a superclass of all type classes. Every <tt>Value</tt> has
2066 a <tt>Type</tt>. <tt>Type</tt> cannot be instantiated directly but only
2067 through its subclasses. Certain primitive types (<tt>VoidType</tt>,
2068 <tt>LabelType</tt>, <tt>FloatType</tt> and <tt>DoubleType</tt>) have hidden
2069 subclasses. They are hidden because they offer no useful functionality beyond
2070 what the <tt>Type</tt> class offers except to distinguish themselves from
2071 other subclasses of <tt>Type</tt>.</p>
2072 <p>All other types are subclasses of <tt>DerivedType</tt>. Types can be
2073 named, but this is not a requirement. There exists exactly
2074 one instance of a given shape at any one time. This allows type equality to
2075 be performed with address equality of the Type Instance. That is, given two
2076 <tt>Type*</tt> values, the types are identical if the pointers are identical.
2077 </p>
2078</div>
2079
2080<!-- _______________________________________________________________________ -->
2081<div class="doc_subsubsection">
2082 <a name="m_Value">Important Public Methods</a>
2083</div>
2084
2085<div class="doc_text">
2086
2087<ul>
Chris Lattner8f79df32007-01-15 01:55:32 +00002088 <li><tt>bool isInteger() const</tt>: Returns true for any integer type.</li>
Reid Spencer303c4b42007-01-12 17:26:25 +00002089
2090 <li><tt>bool isFloatingPoint()</tt>: Return true if this is one of the two
2091 floating point types.</li>
2092
2093 <li><tt>bool isAbstract()</tt>: Return true if the type is abstract (contains
2094 an OpaqueType anywhere in its definition).</li>
2095
2096 <li><tt>bool isSized()</tt>: Return true if the type has known size. Things
2097 that don't have a size are abstract types, labels and void.</li>
2098
2099</ul>
2100</div>
2101
2102<!-- _______________________________________________________________________ -->
2103<div class="doc_subsubsection">
2104 <a name="m_Value">Important Derived Types</a>
2105</div>
2106<div class="doc_text">
2107<dl>
2108 <dt><tt>IntegerType</tt></dt>
2109 <dd>Subclass of DerivedType that represents integer types of any bit width.
2110 Any bit width between <tt>IntegerType::MIN_INT_BITS</tt> (1) and
2111 <tt>IntegerType::MAX_INT_BITS</tt> (~8 million) can be represented.
2112 <ul>
2113 <li><tt>static const IntegerType* get(unsigned NumBits)</tt>: get an integer
2114 type of a specific bit width.</li>
2115 <li><tt>unsigned getBitWidth() const</tt>: Get the bit width of an integer
2116 type.</li>
2117 </ul>
2118 </dd>
2119 <dt><tt>SequentialType</tt></dt>
2120 <dd>This is subclassed by ArrayType and PointerType
2121 <ul>
2122 <li><tt>const Type * getElementType() const</tt>: Returns the type of each
2123 of the elements in the sequential type. </li>
2124 </ul>
2125 </dd>
2126 <dt><tt>ArrayType</tt></dt>
2127 <dd>This is a subclass of SequentialType and defines the interface for array
2128 types.
2129 <ul>
2130 <li><tt>unsigned getNumElements() const</tt>: Returns the number of
2131 elements in the array. </li>
2132 </ul>
2133 </dd>
2134 <dt><tt>PointerType</tt></dt>
Chris Lattner302da1e2007-02-03 03:05:57 +00002135 <dd>Subclass of SequentialType for pointer types.</dd>
Reid Spencer303c4b42007-01-12 17:26:25 +00002136 <dt><tt>PackedType</tt></dt>
2137 <dd>Subclass of SequentialType for packed (vector) types. A
2138 packed type is similar to an ArrayType but is distinguished because it is
2139 a first class type wherease ArrayType is not. Packed types are used for
2140 vector operations and are usually small vectors of of an integer or floating
2141 point type.</dd>
2142 <dt><tt>StructType</tt></dt>
2143 <dd>Subclass of DerivedTypes for struct types.</dd>
2144 <dt><tt>FunctionType</tt></dt>
2145 <dd>Subclass of DerivedTypes for function types.
2146 <ul>
2147 <li><tt>bool isVarArg() const</tt>: Returns true if its a vararg
2148 function</li>
2149 <li><tt> const Type * getReturnType() const</tt>: Returns the
2150 return type of the function.</li>
2151 <li><tt>const Type * getParamType (unsigned i)</tt>: Returns
2152 the type of the ith parameter.</li>
2153 <li><tt> const unsigned getNumParams() const</tt>: Returns the
2154 number of formal parameters.</li>
2155 </ul>
2156 </dd>
2157 <dt><tt>OpaqueType</tt></dt>
2158 <dd>Sublcass of DerivedType for abstract types. This class
2159 defines no content and is used as a placeholder for some other type. Note
2160 that OpaqueType is used (temporarily) during type resolution for forward
2161 references of types. Once the referenced type is resolved, the OpaqueType
2162 is replaced with the actual type. OpaqueType can also be used for data
2163 abstraction. At link time opaque types can be resolved to actual types
2164 of the same name.</dd>
2165</dl>
2166</div>
2167
2168<!-- ======================================================================= -->
2169<div class="doc_subsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00002170 <a name="Value">The <tt>Value</tt> class</a>
2171</div>
2172
2173<div>
2174
2175<p><tt>#include "<a href="/doxygen/Value_8h-source.html">llvm/Value.h</a>"</tt>
2176<br>
Chris Lattner00815172007-01-04 22:01:45 +00002177doxygen info: <a href="/doxygen/classllvm_1_1Value.html">Value Class</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002178
2179<p>The <tt>Value</tt> class is the most important class in the LLVM Source
2180base. It represents a typed value that may be used (among other things) as an
2181operand to an instruction. There are many different types of <tt>Value</tt>s,
2182such as <a href="#Constant"><tt>Constant</tt></a>s,<a
2183href="#Argument"><tt>Argument</tt></a>s. Even <a
2184href="#Instruction"><tt>Instruction</tt></a>s and <a
2185href="#Function"><tt>Function</tt></a>s are <tt>Value</tt>s.</p>
2186
2187<p>A particular <tt>Value</tt> may be used many times in the LLVM representation
2188for a program. For example, an incoming argument to a function (represented
2189with an instance of the <a href="#Argument">Argument</a> class) is "used" by
2190every instruction in the function that references the argument. To keep track
2191of this relationship, the <tt>Value</tt> class keeps a list of all of the <a
2192href="#User"><tt>User</tt></a>s that is using it (the <a
2193href="#User"><tt>User</tt></a> class is a base class for all nodes in the LLVM
2194graph that can refer to <tt>Value</tt>s). This use list is how LLVM represents
2195def-use information in the program, and is accessible through the <tt>use_</tt>*
2196methods, shown below.</p>
2197
2198<p>Because LLVM is a typed representation, every LLVM <tt>Value</tt> is typed,
2199and this <a href="#Type">Type</a> is available through the <tt>getType()</tt>
2200method. In addition, all LLVM values can be named. The "name" of the
2201<tt>Value</tt> is a symbolic string printed in the LLVM code:</p>
2202
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002203<div class="doc_code">
2204<pre>
Reid Spencer06565dc2007-01-12 17:11:23 +00002205%<b>foo</b> = add i32 1, 2
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002206</pre>
2207</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002208
2209<p><a name="#nameWarning">The name of this instruction is "foo".</a> <b>NOTE</b>
2210that the name of any value may be missing (an empty string), so names should
2211<b>ONLY</b> be used for debugging (making the source code easier to read,
2212debugging printouts), they should not be used to keep track of values or map
2213between them. For this purpose, use a <tt>std::map</tt> of pointers to the
2214<tt>Value</tt> itself instead.</p>
2215
2216<p>One important aspect of LLVM is that there is no distinction between an SSA
2217variable and the operation that produces it. Because of this, any reference to
2218the value produced by an instruction (or the value available as an incoming
Chris Lattnerd5fc4fc2004-03-18 14:58:55 +00002219argument, for example) is represented as a direct pointer to the instance of
2220the class that
Misha Brukman13fd15c2004-01-15 00:14:41 +00002221represents this value. Although this may take some getting used to, it
2222simplifies the representation and makes it easier to manipulate.</p>
2223
2224</div>
2225
2226<!-- _______________________________________________________________________ -->
2227<div class="doc_subsubsection">
2228 <a name="m_Value">Important Public Members of the <tt>Value</tt> class</a>
2229</div>
2230
2231<div class="doc_text">
2232
Chris Lattner261efe92003-11-25 01:02:51 +00002233<ul>
2234 <li><tt>Value::use_iterator</tt> - Typedef for iterator over the
2235use-list<br>
2236 <tt>Value::use_const_iterator</tt> - Typedef for const_iterator over
2237the use-list<br>
2238 <tt>unsigned use_size()</tt> - Returns the number of users of the
2239value.<br>
Chris Lattner9355b472002-09-06 02:50:58 +00002240 <tt>bool use_empty()</tt> - Returns true if there are no users.<br>
Chris Lattner261efe92003-11-25 01:02:51 +00002241 <tt>use_iterator use_begin()</tt> - Get an iterator to the start of
2242the use-list.<br>
2243 <tt>use_iterator use_end()</tt> - Get an iterator to the end of the
2244use-list.<br>
2245 <tt><a href="#User">User</a> *use_back()</tt> - Returns the last
2246element in the list.
2247 <p> These methods are the interface to access the def-use
2248information in LLVM. As with all other iterators in LLVM, the naming
2249conventions follow the conventions defined by the <a href="#stl">STL</a>.</p>
Chris Lattner261efe92003-11-25 01:02:51 +00002250 </li>
2251 <li><tt><a href="#Type">Type</a> *getType() const</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002252 <p>This method returns the Type of the Value.</p>
Chris Lattner261efe92003-11-25 01:02:51 +00002253 </li>
2254 <li><tt>bool hasName() const</tt><br>
Chris Lattner9355b472002-09-06 02:50:58 +00002255 <tt>std::string getName() const</tt><br>
Chris Lattner261efe92003-11-25 01:02:51 +00002256 <tt>void setName(const std::string &amp;Name)</tt>
2257 <p> This family of methods is used to access and assign a name to a <tt>Value</tt>,
2258be aware of the <a href="#nameWarning">precaution above</a>.</p>
Chris Lattner261efe92003-11-25 01:02:51 +00002259 </li>
2260 <li><tt>void replaceAllUsesWith(Value *V)</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002261
2262 <p>This method traverses the use list of a <tt>Value</tt> changing all <a
2263 href="#User"><tt>User</tt>s</a> of the current value to refer to
2264 "<tt>V</tt>" instead. For example, if you detect that an instruction always
2265 produces a constant value (for example through constant folding), you can
2266 replace all uses of the instruction with the constant like this:</p>
2267
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002268<div class="doc_code">
2269<pre>
2270Inst-&gt;replaceAllUsesWith(ConstVal);
2271</pre>
2272</div>
2273
Chris Lattner261efe92003-11-25 01:02:51 +00002274</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002275
2276</div>
2277
2278<!-- ======================================================================= -->
2279<div class="doc_subsection">
2280 <a name="User">The <tt>User</tt> class</a>
2281</div>
2282
2283<div class="doc_text">
2284
2285<p>
2286<tt>#include "<a href="/doxygen/User_8h-source.html">llvm/User.h</a>"</tt><br>
Misha Brukman384047f2004-06-03 23:29:12 +00002287doxygen info: <a href="/doxygen/classllvm_1_1User.html">User Class</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002288Superclass: <a href="#Value"><tt>Value</tt></a></p>
2289
2290<p>The <tt>User</tt> class is the common base class of all LLVM nodes that may
2291refer to <a href="#Value"><tt>Value</tt></a>s. It exposes a list of "Operands"
2292that are all of the <a href="#Value"><tt>Value</tt></a>s that the User is
2293referring to. The <tt>User</tt> class itself is a subclass of
2294<tt>Value</tt>.</p>
2295
2296<p>The operands of a <tt>User</tt> point directly to the LLVM <a
2297href="#Value"><tt>Value</tt></a> that it refers to. Because LLVM uses Static
2298Single Assignment (SSA) form, there can only be one definition referred to,
2299allowing this direct connection. This connection provides the use-def
2300information in LLVM.</p>
2301
2302</div>
2303
2304<!-- _______________________________________________________________________ -->
2305<div class="doc_subsubsection">
2306 <a name="m_User">Important Public Members of the <tt>User</tt> class</a>
2307</div>
2308
2309<div class="doc_text">
2310
2311<p>The <tt>User</tt> class exposes the operand list in two ways: through
2312an index access interface and through an iterator based interface.</p>
2313
Chris Lattner261efe92003-11-25 01:02:51 +00002314<ul>
Chris Lattner261efe92003-11-25 01:02:51 +00002315 <li><tt>Value *getOperand(unsigned i)</tt><br>
2316 <tt>unsigned getNumOperands()</tt>
2317 <p> These two methods expose the operands of the <tt>User</tt> in a
Misha Brukman13fd15c2004-01-15 00:14:41 +00002318convenient form for direct access.</p></li>
2319
Chris Lattner261efe92003-11-25 01:02:51 +00002320 <li><tt>User::op_iterator</tt> - Typedef for iterator over the operand
2321list<br>
Chris Lattner58360822005-01-17 00:12:04 +00002322 <tt>op_iterator op_begin()</tt> - Get an iterator to the start of
2323the operand list.<br>
2324 <tt>op_iterator op_end()</tt> - Get an iterator to the end of the
Chris Lattner261efe92003-11-25 01:02:51 +00002325operand list.
2326 <p> Together, these methods make up the iterator based interface to
Misha Brukman13fd15c2004-01-15 00:14:41 +00002327the operands of a <tt>User</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002328</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002329
2330</div>
2331
2332<!-- ======================================================================= -->
2333<div class="doc_subsection">
2334 <a name="Instruction">The <tt>Instruction</tt> class</a>
2335</div>
2336
2337<div class="doc_text">
2338
2339<p><tt>#include "</tt><tt><a
2340href="/doxygen/Instruction_8h-source.html">llvm/Instruction.h</a>"</tt><br>
Misha Brukman31ca1de2004-06-03 23:35:54 +00002341doxygen info: <a href="/doxygen/classllvm_1_1Instruction.html">Instruction Class</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002342Superclasses: <a href="#User"><tt>User</tt></a>, <a
2343href="#Value"><tt>Value</tt></a></p>
2344
2345<p>The <tt>Instruction</tt> class is the common base class for all LLVM
2346instructions. It provides only a few methods, but is a very commonly used
2347class. The primary data tracked by the <tt>Instruction</tt> class itself is the
2348opcode (instruction type) and the parent <a
2349href="#BasicBlock"><tt>BasicBlock</tt></a> the <tt>Instruction</tt> is embedded
2350into. To represent a specific type of instruction, one of many subclasses of
2351<tt>Instruction</tt> are used.</p>
2352
2353<p> Because the <tt>Instruction</tt> class subclasses the <a
2354href="#User"><tt>User</tt></a> class, its operands can be accessed in the same
2355way as for other <a href="#User"><tt>User</tt></a>s (with the
2356<tt>getOperand()</tt>/<tt>getNumOperands()</tt> and
2357<tt>op_begin()</tt>/<tt>op_end()</tt> methods).</p> <p> An important file for
2358the <tt>Instruction</tt> class is the <tt>llvm/Instruction.def</tt> file. This
2359file contains some meta-data about the various different types of instructions
2360in LLVM. It describes the enum values that are used as opcodes (for example
Reid Spencerc92d25d2006-12-19 19:47:19 +00002361<tt>Instruction::Add</tt> and <tt>Instruction::ICmp</tt>), as well as the
Misha Brukman13fd15c2004-01-15 00:14:41 +00002362concrete sub-classes of <tt>Instruction</tt> that implement the instruction (for
2363example <tt><a href="#BinaryOperator">BinaryOperator</a></tt> and <tt><a
Reid Spencerc92d25d2006-12-19 19:47:19 +00002364href="#CmpInst">CmpInst</a></tt>). Unfortunately, the use of macros in
Misha Brukman13fd15c2004-01-15 00:14:41 +00002365this file confuses doxygen, so these enum values don't show up correctly in the
Misha Brukman31ca1de2004-06-03 23:35:54 +00002366<a href="/doxygen/classllvm_1_1Instruction.html">doxygen output</a>.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002367
2368</div>
2369
2370<!-- _______________________________________________________________________ -->
2371<div class="doc_subsubsection">
Reid Spencerc92d25d2006-12-19 19:47:19 +00002372 <a name="s_Instruction">Important Subclasses of the <tt>Instruction</tt>
2373 class</a>
2374</div>
2375<div class="doc_text">
2376 <ul>
2377 <li><tt><a name="BinaryOperator">BinaryOperator</a></tt>
2378 <p>This subclasses represents all two operand instructions whose operands
2379 must be the same type, except for the comparison instructions.</p></li>
2380 <li><tt><a name="CastInst">CastInst</a></tt>
2381 <p>This subclass is the parent of the 12 casting instructions. It provides
2382 common operations on cast instructions.</p>
2383 <li><tt><a name="CmpInst">CmpInst</a></tt>
2384 <p>This subclass respresents the two comparison instructions,
2385 <a href="LangRef.html#i_icmp">ICmpInst</a> (integer opreands), and
2386 <a href="LangRef.html#i_fcmp">FCmpInst</a> (floating point operands).</p>
2387 <li><tt><a name="TerminatorInst">TerminatorInst</a></tt>
2388 <p>This subclass is the parent of all terminator instructions (those which
2389 can terminate a block).</p>
2390 </ul>
2391 </div>
2392
2393<!-- _______________________________________________________________________ -->
2394<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00002395 <a name="m_Instruction">Important Public Members of the <tt>Instruction</tt>
2396 class</a>
2397</div>
2398
2399<div class="doc_text">
2400
Chris Lattner261efe92003-11-25 01:02:51 +00002401<ul>
2402 <li><tt><a href="#BasicBlock">BasicBlock</a> *getParent()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002403 <p>Returns the <a href="#BasicBlock"><tt>BasicBlock</tt></a> that
2404this <tt>Instruction</tt> is embedded into.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002405 <li><tt>bool mayWriteToMemory()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002406 <p>Returns true if the instruction writes to memory, i.e. it is a
2407 <tt>call</tt>,<tt>free</tt>,<tt>invoke</tt>, or <tt>store</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002408 <li><tt>unsigned getOpcode()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002409 <p>Returns the opcode for the <tt>Instruction</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002410 <li><tt><a href="#Instruction">Instruction</a> *clone() const</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002411 <p>Returns another instance of the specified instruction, identical
Chris Lattner261efe92003-11-25 01:02:51 +00002412in all ways to the original except that the instruction has no parent
2413(ie it's not embedded into a <a href="#BasicBlock"><tt>BasicBlock</tt></a>),
Misha Brukman13fd15c2004-01-15 00:14:41 +00002414and it has no name</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002415</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002416
2417</div>
2418
2419<!-- ======================================================================= -->
2420<div class="doc_subsection">
2421 <a name="BasicBlock">The <tt>BasicBlock</tt> class</a>
2422</div>
2423
2424<div class="doc_text">
2425
Misha Brukman384047f2004-06-03 23:29:12 +00002426<p><tt>#include "<a
2427href="/doxygen/BasicBlock_8h-source.html">llvm/BasicBlock.h</a>"</tt><br>
2428doxygen info: <a href="/doxygen/structllvm_1_1BasicBlock.html">BasicBlock
2429Class</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002430Superclass: <a href="#Value"><tt>Value</tt></a></p>
2431
2432<p>This class represents a single entry multiple exit section of the code,
2433commonly known as a basic block by the compiler community. The
2434<tt>BasicBlock</tt> class maintains a list of <a
2435href="#Instruction"><tt>Instruction</tt></a>s, which form the body of the block.
2436Matching the language definition, the last element of this list of instructions
2437is always a terminator instruction (a subclass of the <a
2438href="#TerminatorInst"><tt>TerminatorInst</tt></a> class).</p>
2439
2440<p>In addition to tracking the list of instructions that make up the block, the
2441<tt>BasicBlock</tt> class also keeps track of the <a
2442href="#Function"><tt>Function</tt></a> that it is embedded into.</p>
2443
2444<p>Note that <tt>BasicBlock</tt>s themselves are <a
2445href="#Value"><tt>Value</tt></a>s, because they are referenced by instructions
2446like branches and can go in the switch tables. <tt>BasicBlock</tt>s have type
2447<tt>label</tt>.</p>
2448
2449</div>
2450
2451<!-- _______________________________________________________________________ -->
2452<div class="doc_subsubsection">
2453 <a name="m_BasicBlock">Important Public Members of the <tt>BasicBlock</tt>
2454 class</a>
2455</div>
2456
2457<div class="doc_text">
2458
Chris Lattner261efe92003-11-25 01:02:51 +00002459<ul>
Misha Brukmanb0e7e452004-10-29 04:33:19 +00002460
2461<li><tt>BasicBlock(const std::string &amp;Name = "", </tt><tt><a
Chris Lattner261efe92003-11-25 01:02:51 +00002462 href="#Function">Function</a> *Parent = 0)</tt>
Misha Brukmanb0e7e452004-10-29 04:33:19 +00002463
2464<p>The <tt>BasicBlock</tt> constructor is used to create new basic blocks for
2465insertion into a function. The constructor optionally takes a name for the new
2466block, and a <a href="#Function"><tt>Function</tt></a> to insert it into. If
2467the <tt>Parent</tt> parameter is specified, the new <tt>BasicBlock</tt> is
2468automatically inserted at the end of the specified <a
2469href="#Function"><tt>Function</tt></a>, if not specified, the BasicBlock must be
2470manually inserted into the <a href="#Function"><tt>Function</tt></a>.</p></li>
2471
2472<li><tt>BasicBlock::iterator</tt> - Typedef for instruction list iterator<br>
2473<tt>BasicBlock::const_iterator</tt> - Typedef for const_iterator.<br>
2474<tt>begin()</tt>, <tt>end()</tt>, <tt>front()</tt>, <tt>back()</tt>,
Chris Lattner77d69242005-03-15 05:19:20 +00002475<tt>size()</tt>, <tt>empty()</tt>
Misha Brukmanb0e7e452004-10-29 04:33:19 +00002476STL-style functions for accessing the instruction list.
2477
2478<p>These methods and typedefs are forwarding functions that have the same
2479semantics as the standard library methods of the same names. These methods
2480expose the underlying instruction list of a basic block in a way that is easy to
2481manipulate. To get the full complement of container operations (including
2482operations to update the list), you must use the <tt>getInstList()</tt>
2483method.</p></li>
2484
2485<li><tt>BasicBlock::InstListType &amp;getInstList()</tt>
2486
2487<p>This method is used to get access to the underlying container that actually
2488holds the Instructions. This method must be used when there isn't a forwarding
2489function in the <tt>BasicBlock</tt> class for the operation that you would like
2490to perform. Because there are no forwarding functions for "updating"
2491operations, you need to use this if you want to update the contents of a
2492<tt>BasicBlock</tt>.</p></li>
2493
2494<li><tt><a href="#Function">Function</a> *getParent()</tt>
2495
2496<p> Returns a pointer to <a href="#Function"><tt>Function</tt></a> the block is
2497embedded into, or a null pointer if it is homeless.</p></li>
2498
2499<li><tt><a href="#TerminatorInst">TerminatorInst</a> *getTerminator()</tt>
2500
2501<p> Returns a pointer to the terminator instruction that appears at the end of
2502the <tt>BasicBlock</tt>. If there is no terminator instruction, or if the last
2503instruction in the block is not a terminator, then a null pointer is
2504returned.</p></li>
2505
Chris Lattner261efe92003-11-25 01:02:51 +00002506</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002507
2508</div>
2509
2510<!-- ======================================================================= -->
2511<div class="doc_subsection">
2512 <a name="GlobalValue">The <tt>GlobalValue</tt> class</a>
2513</div>
2514
2515<div class="doc_text">
2516
2517<p><tt>#include "<a
2518href="/doxygen/GlobalValue_8h-source.html">llvm/GlobalValue.h</a>"</tt><br>
Misha Brukman384047f2004-06-03 23:29:12 +00002519doxygen info: <a href="/doxygen/classllvm_1_1GlobalValue.html">GlobalValue
2520Class</a><br>
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002521Superclasses: <a href="#Constant"><tt>Constant</tt></a>,
2522<a href="#User"><tt>User</tt></a>, <a href="#Value"><tt>Value</tt></a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002523
2524<p>Global values (<a href="#GlobalVariable"><tt>GlobalVariable</tt></a>s or <a
2525href="#Function"><tt>Function</tt></a>s) are the only LLVM values that are
2526visible in the bodies of all <a href="#Function"><tt>Function</tt></a>s.
2527Because they are visible at global scope, they are also subject to linking with
2528other globals defined in different translation units. To control the linking
2529process, <tt>GlobalValue</tt>s know their linkage rules. Specifically,
2530<tt>GlobalValue</tt>s know whether they have internal or external linkage, as
Reid Spencer8b2da7a2004-07-18 13:10:31 +00002531defined by the <tt>LinkageTypes</tt> enumeration.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002532
2533<p>If a <tt>GlobalValue</tt> has internal linkage (equivalent to being
2534<tt>static</tt> in C), it is not visible to code outside the current translation
2535unit, and does not participate in linking. If it has external linkage, it is
2536visible to external code, and does participate in linking. In addition to
2537linkage information, <tt>GlobalValue</tt>s keep track of which <a
2538href="#Module"><tt>Module</tt></a> they are currently part of.</p>
2539
2540<p>Because <tt>GlobalValue</tt>s are memory objects, they are always referred to
2541by their <b>address</b>. As such, the <a href="#Type"><tt>Type</tt></a> of a
2542global is always a pointer to its contents. It is important to remember this
2543when using the <tt>GetElementPtrInst</tt> instruction because this pointer must
2544be dereferenced first. For example, if you have a <tt>GlobalVariable</tt> (a
2545subclass of <tt>GlobalValue)</tt> that is an array of 24 ints, type <tt>[24 x
Reid Spencer06565dc2007-01-12 17:11:23 +00002546i32]</tt>, then the <tt>GlobalVariable</tt> is a pointer to that array. Although
Misha Brukman13fd15c2004-01-15 00:14:41 +00002547the address of the first element of this array and the value of the
2548<tt>GlobalVariable</tt> are the same, they have different types. The
Reid Spencer06565dc2007-01-12 17:11:23 +00002549<tt>GlobalVariable</tt>'s type is <tt>[24 x i32]</tt>. The first element's type
2550is <tt>i32.</tt> Because of this, accessing a global value requires you to
Misha Brukman13fd15c2004-01-15 00:14:41 +00002551dereference the pointer with <tt>GetElementPtrInst</tt> first, then its elements
2552can be accessed. This is explained in the <a href="LangRef.html#globalvars">LLVM
2553Language Reference Manual</a>.</p>
2554
2555</div>
2556
2557<!-- _______________________________________________________________________ -->
2558<div class="doc_subsubsection">
2559 <a name="m_GlobalValue">Important Public Members of the <tt>GlobalValue</tt>
2560 class</a>
2561</div>
2562
2563<div class="doc_text">
2564
Chris Lattner261efe92003-11-25 01:02:51 +00002565<ul>
2566 <li><tt>bool hasInternalLinkage() const</tt><br>
Chris Lattner9355b472002-09-06 02:50:58 +00002567 <tt>bool hasExternalLinkage() const</tt><br>
Chris Lattner261efe92003-11-25 01:02:51 +00002568 <tt>void setInternalLinkage(bool HasInternalLinkage)</tt>
2569 <p> These methods manipulate the linkage characteristics of the <tt>GlobalValue</tt>.</p>
2570 <p> </p>
2571 </li>
2572 <li><tt><a href="#Module">Module</a> *getParent()</tt>
2573 <p> This returns the <a href="#Module"><tt>Module</tt></a> that the
Misha Brukman13fd15c2004-01-15 00:14:41 +00002574GlobalValue is currently embedded into.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002575</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002576
2577</div>
2578
2579<!-- ======================================================================= -->
2580<div class="doc_subsection">
2581 <a name="Function">The <tt>Function</tt> class</a>
2582</div>
2583
2584<div class="doc_text">
2585
2586<p><tt>#include "<a
2587href="/doxygen/Function_8h-source.html">llvm/Function.h</a>"</tt><br> doxygen
Misha Brukman31ca1de2004-06-03 23:35:54 +00002588info: <a href="/doxygen/classllvm_1_1Function.html">Function Class</a><br>
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002589Superclasses: <a href="#GlobalValue"><tt>GlobalValue</tt></a>,
2590<a href="#Constant"><tt>Constant</tt></a>,
2591<a href="#User"><tt>User</tt></a>,
2592<a href="#Value"><tt>Value</tt></a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002593
2594<p>The <tt>Function</tt> class represents a single procedure in LLVM. It is
2595actually one of the more complex classes in the LLVM heirarchy because it must
2596keep track of a large amount of data. The <tt>Function</tt> class keeps track
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002597of a list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s, a list of formal
2598<a href="#Argument"><tt>Argument</tt></a>s, and a
2599<a href="#SymbolTable"><tt>SymbolTable</tt></a>.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002600
2601<p>The list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s is the most
2602commonly used part of <tt>Function</tt> objects. The list imposes an implicit
2603ordering of the blocks in the function, which indicate how the code will be
2604layed out by the backend. Additionally, the first <a
2605href="#BasicBlock"><tt>BasicBlock</tt></a> is the implicit entry node for the
2606<tt>Function</tt>. It is not legal in LLVM to explicitly branch to this initial
2607block. There are no implicit exit nodes, and in fact there may be multiple exit
2608nodes from a single <tt>Function</tt>. If the <a
2609href="#BasicBlock"><tt>BasicBlock</tt></a> list is empty, this indicates that
2610the <tt>Function</tt> is actually a function declaration: the actual body of the
2611function hasn't been linked in yet.</p>
2612
2613<p>In addition to a list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s, the
2614<tt>Function</tt> class also keeps track of the list of formal <a
2615href="#Argument"><tt>Argument</tt></a>s that the function receives. This
2616container manages the lifetime of the <a href="#Argument"><tt>Argument</tt></a>
2617nodes, just like the <a href="#BasicBlock"><tt>BasicBlock</tt></a> list does for
2618the <a href="#BasicBlock"><tt>BasicBlock</tt></a>s.</p>
2619
2620<p>The <a href="#SymbolTable"><tt>SymbolTable</tt></a> is a very rarely used
2621LLVM feature that is only used when you have to look up a value by name. Aside
2622from that, the <a href="#SymbolTable"><tt>SymbolTable</tt></a> is used
2623internally to make sure that there are not conflicts between the names of <a
2624href="#Instruction"><tt>Instruction</tt></a>s, <a
2625href="#BasicBlock"><tt>BasicBlock</tt></a>s, or <a
2626href="#Argument"><tt>Argument</tt></a>s in the function body.</p>
2627
Reid Spencer8b2da7a2004-07-18 13:10:31 +00002628<p>Note that <tt>Function</tt> is a <a href="#GlobalValue">GlobalValue</a>
2629and therefore also a <a href="#Constant">Constant</a>. The value of the function
2630is its address (after linking) which is guaranteed to be constant.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002631</div>
2632
2633<!-- _______________________________________________________________________ -->
2634<div class="doc_subsubsection">
2635 <a name="m_Function">Important Public Members of the <tt>Function</tt>
2636 class</a>
2637</div>
2638
2639<div class="doc_text">
2640
Chris Lattner261efe92003-11-25 01:02:51 +00002641<ul>
2642 <li><tt>Function(const </tt><tt><a href="#FunctionType">FunctionType</a>
Chris Lattnerac479e52004-08-04 05:10:48 +00002643 *Ty, LinkageTypes Linkage, const std::string &amp;N = "", Module* Parent = 0)</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002644
2645 <p>Constructor used when you need to create new <tt>Function</tt>s to add
2646 the the program. The constructor must specify the type of the function to
Chris Lattnerac479e52004-08-04 05:10:48 +00002647 create and what type of linkage the function should have. The <a
2648 href="#FunctionType"><tt>FunctionType</tt></a> argument
Misha Brukman13fd15c2004-01-15 00:14:41 +00002649 specifies the formal arguments and return value for the function. The same
2650 <a href="#FunctionTypel"><tt>FunctionType</tt></a> value can be used to
2651 create multiple functions. The <tt>Parent</tt> argument specifies the Module
2652 in which the function is defined. If this argument is provided, the function
2653 will automatically be inserted into that module's list of
2654 functions.</p></li>
2655
Chris Lattner261efe92003-11-25 01:02:51 +00002656 <li><tt>bool isExternal()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002657
2658 <p>Return whether or not the <tt>Function</tt> has a body defined. If the
2659 function is "external", it does not have a body, and thus must be resolved
2660 by linking with a function defined in a different translation unit.</p></li>
2661
Chris Lattner261efe92003-11-25 01:02:51 +00002662 <li><tt>Function::iterator</tt> - Typedef for basic block list iterator<br>
Chris Lattner9355b472002-09-06 02:50:58 +00002663 <tt>Function::const_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002664
Chris Lattner77d69242005-03-15 05:19:20 +00002665 <tt>begin()</tt>, <tt>end()</tt>
2666 <tt>size()</tt>, <tt>empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002667
2668 <p>These are forwarding methods that make it easy to access the contents of
2669 a <tt>Function</tt> object's <a href="#BasicBlock"><tt>BasicBlock</tt></a>
2670 list.</p></li>
2671
Chris Lattner261efe92003-11-25 01:02:51 +00002672 <li><tt>Function::BasicBlockListType &amp;getBasicBlockList()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002673
2674 <p>Returns the list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s. This
2675 is necessary to use when you need to update the list or perform a complex
2676 action that doesn't have a forwarding method.</p></li>
2677
Chris Lattner89cc2652005-03-15 04:48:32 +00002678 <li><tt>Function::arg_iterator</tt> - Typedef for the argument list
Chris Lattner261efe92003-11-25 01:02:51 +00002679iterator<br>
Chris Lattner89cc2652005-03-15 04:48:32 +00002680 <tt>Function::const_arg_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002681
Chris Lattner77d69242005-03-15 05:19:20 +00002682 <tt>arg_begin()</tt>, <tt>arg_end()</tt>
Chris Lattner89cc2652005-03-15 04:48:32 +00002683 <tt>arg_size()</tt>, <tt>arg_empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002684
2685 <p>These are forwarding methods that make it easy to access the contents of
2686 a <tt>Function</tt> object's <a href="#Argument"><tt>Argument</tt></a>
2687 list.</p></li>
2688
Chris Lattner261efe92003-11-25 01:02:51 +00002689 <li><tt>Function::ArgumentListType &amp;getArgumentList()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002690
2691 <p>Returns the list of <a href="#Argument"><tt>Argument</tt></a>s. This is
2692 necessary to use when you need to update the list or perform a complex
2693 action that doesn't have a forwarding method.</p></li>
2694
Chris Lattner261efe92003-11-25 01:02:51 +00002695 <li><tt><a href="#BasicBlock">BasicBlock</a> &amp;getEntryBlock()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002696
2697 <p>Returns the entry <a href="#BasicBlock"><tt>BasicBlock</tt></a> for the
2698 function. Because the entry block for the function is always the first
2699 block, this returns the first block of the <tt>Function</tt>.</p></li>
2700
Chris Lattner261efe92003-11-25 01:02:51 +00002701 <li><tt><a href="#Type">Type</a> *getReturnType()</tt><br>
2702 <tt><a href="#FunctionType">FunctionType</a> *getFunctionType()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002703
2704 <p>This traverses the <a href="#Type"><tt>Type</tt></a> of the
2705 <tt>Function</tt> and returns the return type of the function, or the <a
2706 href="#FunctionType"><tt>FunctionType</tt></a> of the actual
2707 function.</p></li>
2708
Chris Lattner261efe92003-11-25 01:02:51 +00002709 <li><tt><a href="#SymbolTable">SymbolTable</a> *getSymbolTable()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002710
Chris Lattner261efe92003-11-25 01:02:51 +00002711 <p> Return a pointer to the <a href="#SymbolTable"><tt>SymbolTable</tt></a>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002712 for this <tt>Function</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002713</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002714
2715</div>
2716
2717<!-- ======================================================================= -->
2718<div class="doc_subsection">
2719 <a name="GlobalVariable">The <tt>GlobalVariable</tt> class</a>
2720</div>
2721
2722<div class="doc_text">
2723
2724<p><tt>#include "<a
2725href="/doxygen/GlobalVariable_8h-source.html">llvm/GlobalVariable.h</a>"</tt>
2726<br>
Tanya Lattnera3da7772004-06-22 08:02:25 +00002727doxygen info: <a href="/doxygen/classllvm_1_1GlobalVariable.html">GlobalVariable
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002728 Class</a><br>
2729Superclasses: <a href="#GlobalValue"><tt>GlobalValue</tt></a>,
2730<a href="#Constant"><tt>Constant</tt></a>,
2731<a href="#User"><tt>User</tt></a>,
2732<a href="#Value"><tt>Value</tt></a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002733
2734<p>Global variables are represented with the (suprise suprise)
2735<tt>GlobalVariable</tt> class. Like functions, <tt>GlobalVariable</tt>s are also
2736subclasses of <a href="#GlobalValue"><tt>GlobalValue</tt></a>, and as such are
2737always referenced by their address (global values must live in memory, so their
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002738"name" refers to their constant address). See
2739<a href="#GlobalValue"><tt>GlobalValue</tt></a> for more on this. Global
2740variables may have an initial value (which must be a
2741<a href="#Constant"><tt>Constant</tt></a>), and if they have an initializer,
2742they may be marked as "constant" themselves (indicating that their contents
2743never change at runtime).</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002744</div>
2745
2746<!-- _______________________________________________________________________ -->
2747<div class="doc_subsubsection">
2748 <a name="m_GlobalVariable">Important Public Members of the
2749 <tt>GlobalVariable</tt> class</a>
2750</div>
2751
2752<div class="doc_text">
2753
Chris Lattner261efe92003-11-25 01:02:51 +00002754<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002755 <li><tt>GlobalVariable(const </tt><tt><a href="#Type">Type</a> *Ty, bool
2756 isConstant, LinkageTypes&amp; Linkage, <a href="#Constant">Constant</a>
2757 *Initializer = 0, const std::string &amp;Name = "", Module* Parent = 0)</tt>
2758
2759 <p>Create a new global variable of the specified type. If
2760 <tt>isConstant</tt> is true then the global variable will be marked as
2761 unchanging for the program. The Linkage parameter specifies the type of
2762 linkage (internal, external, weak, linkonce, appending) for the variable. If
2763 the linkage is InternalLinkage, WeakLinkage, or LinkOnceLinkage,&nbsp; then
2764 the resultant global variable will have internal linkage. AppendingLinkage
2765 concatenates together all instances (in different translation units) of the
2766 variable into a single variable but is only applicable to arrays. &nbsp;See
2767 the <a href="LangRef.html#modulestructure">LLVM Language Reference</a> for
2768 further details on linkage types. Optionally an initializer, a name, and the
2769 module to put the variable into may be specified for the global variable as
2770 well.</p></li>
2771
Chris Lattner261efe92003-11-25 01:02:51 +00002772 <li><tt>bool isConstant() const</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002773
2774 <p>Returns true if this is a global variable that is known not to
2775 be modified at runtime.</p></li>
2776
Chris Lattner261efe92003-11-25 01:02:51 +00002777 <li><tt>bool hasInitializer()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002778
2779 <p>Returns true if this <tt>GlobalVariable</tt> has an intializer.</p></li>
2780
Chris Lattner261efe92003-11-25 01:02:51 +00002781 <li><tt><a href="#Constant">Constant</a> *getInitializer()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002782
2783 <p>Returns the intial value for a <tt>GlobalVariable</tt>. It is not legal
2784 to call this method if there is no initializer.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002785</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002786
2787</div>
2788
2789<!-- ======================================================================= -->
2790<div class="doc_subsection">
2791 <a name="Module">The <tt>Module</tt> class</a>
2792</div>
2793
2794<div class="doc_text">
2795
2796<p><tt>#include "<a
2797href="/doxygen/Module_8h-source.html">llvm/Module.h</a>"</tt><br> doxygen info:
Tanya Lattnera3da7772004-06-22 08:02:25 +00002798<a href="/doxygen/classllvm_1_1Module.html">Module Class</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002799
2800<p>The <tt>Module</tt> class represents the top level structure present in LLVM
2801programs. An LLVM module is effectively either a translation unit of the
2802original program or a combination of several translation units merged by the
2803linker. The <tt>Module</tt> class keeps track of a list of <a
2804href="#Function"><tt>Function</tt></a>s, a list of <a
2805href="#GlobalVariable"><tt>GlobalVariable</tt></a>s, and a <a
2806href="#SymbolTable"><tt>SymbolTable</tt></a>. Additionally, it contains a few
2807helpful member functions that try to make common operations easy.</p>
2808
2809</div>
2810
2811<!-- _______________________________________________________________________ -->
2812<div class="doc_subsubsection">
2813 <a name="m_Module">Important Public Members of the <tt>Module</tt> class</a>
2814</div>
2815
2816<div class="doc_text">
2817
Chris Lattner261efe92003-11-25 01:02:51 +00002818<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002819 <li><tt>Module::Module(std::string name = "")</tt></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002820</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002821
2822<p>Constructing a <a href="#Module">Module</a> is easy. You can optionally
2823provide a name for it (probably based on the name of the translation unit).</p>
2824
Chris Lattner261efe92003-11-25 01:02:51 +00002825<ul>
2826 <li><tt>Module::iterator</tt> - Typedef for function list iterator<br>
Chris Lattner0377de42002-09-06 14:50:55 +00002827 <tt>Module::const_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002828
Chris Lattner77d69242005-03-15 05:19:20 +00002829 <tt>begin()</tt>, <tt>end()</tt>
2830 <tt>size()</tt>, <tt>empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002831
2832 <p>These are forwarding methods that make it easy to access the contents of
2833 a <tt>Module</tt> object's <a href="#Function"><tt>Function</tt></a>
2834 list.</p></li>
2835
Chris Lattner261efe92003-11-25 01:02:51 +00002836 <li><tt>Module::FunctionListType &amp;getFunctionList()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002837
2838 <p> Returns the list of <a href="#Function"><tt>Function</tt></a>s. This is
2839 necessary to use when you need to update the list or perform a complex
2840 action that doesn't have a forwarding method.</p>
2841
2842 <p><!-- Global Variable --></p></li>
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00002843</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002844
2845<hr>
2846
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00002847<ul>
Chris Lattner89cc2652005-03-15 04:48:32 +00002848 <li><tt>Module::global_iterator</tt> - Typedef for global variable list iterator<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002849
Chris Lattner89cc2652005-03-15 04:48:32 +00002850 <tt>Module::const_global_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002851
Chris Lattner77d69242005-03-15 05:19:20 +00002852 <tt>global_begin()</tt>, <tt>global_end()</tt>
Chris Lattner89cc2652005-03-15 04:48:32 +00002853 <tt>global_size()</tt>, <tt>global_empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002854
2855 <p> These are forwarding methods that make it easy to access the contents of
2856 a <tt>Module</tt> object's <a
2857 href="#GlobalVariable"><tt>GlobalVariable</tt></a> list.</p></li>
2858
2859 <li><tt>Module::GlobalListType &amp;getGlobalList()</tt>
2860
2861 <p>Returns the list of <a
2862 href="#GlobalVariable"><tt>GlobalVariable</tt></a>s. This is necessary to
2863 use when you need to update the list or perform a complex action that
2864 doesn't have a forwarding method.</p>
2865
2866 <p><!-- Symbol table stuff --> </p></li>
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00002867</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002868
2869<hr>
2870
2871<ul>
2872 <li><tt><a href="#SymbolTable">SymbolTable</a> *getSymbolTable()</tt>
2873
2874 <p>Return a reference to the <a href="#SymbolTable"><tt>SymbolTable</tt></a>
2875 for this <tt>Module</tt>.</p>
2876
2877 <p><!-- Convenience methods --></p></li>
2878</ul>
2879
2880<hr>
2881
2882<ul>
2883 <li><tt><a href="#Function">Function</a> *getFunction(const std::string
2884 &amp;Name, const <a href="#FunctionType">FunctionType</a> *Ty)</tt>
2885
2886 <p>Look up the specified function in the <tt>Module</tt> <a
2887 href="#SymbolTable"><tt>SymbolTable</tt></a>. If it does not exist, return
2888 <tt>null</tt>.</p></li>
2889
2890 <li><tt><a href="#Function">Function</a> *getOrInsertFunction(const
2891 std::string &amp;Name, const <a href="#FunctionType">FunctionType</a> *T)</tt>
2892
2893 <p>Look up the specified function in the <tt>Module</tt> <a
2894 href="#SymbolTable"><tt>SymbolTable</tt></a>. If it does not exist, add an
2895 external declaration for the function and return it.</p></li>
2896
2897 <li><tt>std::string getTypeName(const <a href="#Type">Type</a> *Ty)</tt>
2898
2899 <p>If there is at least one entry in the <a
2900 href="#SymbolTable"><tt>SymbolTable</tt></a> for the specified <a
2901 href="#Type"><tt>Type</tt></a>, return it. Otherwise return the empty
2902 string.</p></li>
2903
2904 <li><tt>bool addTypeName(const std::string &amp;Name, const <a
2905 href="#Type">Type</a> *Ty)</tt>
2906
2907 <p>Insert an entry in the <a href="#SymbolTable"><tt>SymbolTable</tt></a>
2908 mapping <tt>Name</tt> to <tt>Ty</tt>. If there is already an entry for this
2909 name, true is returned and the <a
2910 href="#SymbolTable"><tt>SymbolTable</tt></a> is not modified.</p></li>
2911</ul>
2912
2913</div>
2914
2915<!-- ======================================================================= -->
2916<div class="doc_subsection">
2917 <a name="Constant">The <tt>Constant</tt> class and subclasses</a>
2918</div>
2919
2920<div class="doc_text">
2921
2922<p>Constant represents a base class for different types of constants. It
Reid Spencer53bfebc2007-01-12 03:36:33 +00002923is subclassed by ConstantInt, ConstantArray, etc. for representing
Reid Spencerb83eb642006-10-20 07:07:24 +00002924the various types of Constants.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002925
2926</div>
2927
2928<!-- _______________________________________________________________________ -->
2929<div class="doc_subsubsection">
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002930 <a name="m_Constant">Important Public Methods</a>
2931</div>
2932<div class="doc_text">
Misha Brukman13fd15c2004-01-15 00:14:41 +00002933</div>
2934
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002935<!-- _______________________________________________________________________ -->
2936<div class="doc_subsubsection">Important Subclasses of Constant </div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002937<div class="doc_text">
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00002938<ul>
Reid Spencer53bfebc2007-01-12 03:36:33 +00002939 <li>ConstantInt : This subclass of Constant represents an integer constant of
2940 any width, including boolean (1 bit integer).
Chris Lattner261efe92003-11-25 01:02:51 +00002941 <ul>
Reid Spencerb83eb642006-10-20 07:07:24 +00002942 <li><tt>int64_t getSExtValue() const</tt>: Returns the underlying value of
2943 this constant as a sign extended signed integer value.</li>
2944 <li><tt>uint64_t getZExtValue() const</tt>: Returns the underlying value
2945 of this constant as a zero extended unsigned integer value.</li>
Reid Spencer53bfebc2007-01-12 03:36:33 +00002946 <li><tt>static ConstantInt* get(const Type *Ty, uint64_t Val)</tt>:
2947 Returns the ConstantInt object that represents the value provided by
2948 <tt>Val</tt> for integer type <tt>Ty</tt>.</li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002949 </ul>
2950 </li>
2951 <li>ConstantFP : This class represents a floating point constant.
2952 <ul>
2953 <li><tt>double getValue() const</tt>: Returns the underlying value of
2954 this constant. </li>
2955 </ul>
2956 </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002957 <li>ConstantArray : This represents a constant array.
2958 <ul>
2959 <li><tt>const std::vector&lt;Use&gt; &amp;getValues() const</tt>: Returns
Chris Lattner58360822005-01-17 00:12:04 +00002960 a vector of component constants that makeup this array. </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002961 </ul>
2962 </li>
2963 <li>ConstantStruct : This represents a constant struct.
2964 <ul>
2965 <li><tt>const std::vector&lt;Use&gt; &amp;getValues() const</tt>: Returns
Chris Lattner58360822005-01-17 00:12:04 +00002966 a vector of component constants that makeup this array. </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002967 </ul>
2968 </li>
2969 <li>GlobalValue : This represents either a global variable or a function. In
2970 either case, the value is a constant fixed address (after linking).
2971 </li>
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00002972</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002973</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002974<!-- ======================================================================= -->
2975<div class="doc_subsection">
2976 <a name="Argument">The <tt>Argument</tt> class</a>
2977</div>
2978
2979<div class="doc_text">
2980
2981<p>This subclass of Value defines the interface for incoming formal
Chris Lattner58360822005-01-17 00:12:04 +00002982arguments to a function. A Function maintains a list of its formal
Misha Brukman13fd15c2004-01-15 00:14:41 +00002983arguments. An argument has a pointer to the parent Function.</p>
2984
2985</div>
2986
Chris Lattner9355b472002-09-06 02:50:58 +00002987<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +00002988<hr>
2989<address>
2990 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
2991 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
2992 <a href="http://validator.w3.org/check/referer"><img
2993 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
2994
2995 <a href="mailto:dhurjati@cs.uiuc.edu">Dinakar Dhurjati</a> and
2996 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencer05fe4b02006-03-14 05:39:39 +00002997 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002998 Last modified: $Date$
2999</address>
3000
Chris Lattner261efe92003-11-25 01:02:51 +00003001</body>
3002</html>