blob: e2ee74eff947627dd78d73984271577d63038aa0 [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>
49 <li><a href="#ds_sequential">Sequential Containers (std::vector, std::list, etc)</a><ul>
50 <li><a href="#dss_fixedarrays">Fixed Size Arrays</a></li>
51 <li><a href="#dss_heaparrays">Heap Allocated Arrays</a></li>
52 <li><a href="#dss_smallvector">"llvm/ADT/SmallVector.h"</a></li>
53 <li><a href="#dss_vector">&lt;vector&gt;</a></li>
54 <li><a href="#dss_ilist">llvm/ADT/ilist</a></li>
55 <li><a href="#dss_list">&lt;list&gt;</a></li>
56 </ul></li>
57 <li><a href="#ds_set">Set-Like Containers (std::set, SmallSet, SetVector, etc)</a></li>
58 <li><a href="#ds_map">Map-Like Containers (std::map, DenseMap, etc)</a></li>
59 </ul>
60 </li>
Chris Lattnerae7f7592002-09-06 18:31:18 +000061 <li><a href="#common">Helpful Hints for Common Operations</a>
Chris Lattnerae7f7592002-09-06 18:31:18 +000062 <ul>
Chris Lattner261efe92003-11-25 01:02:51 +000063 <li><a href="#inspection">Basic Inspection and Traversal Routines</a>
64 <ul>
65 <li><a href="#iterate_function">Iterating over the <tt>BasicBlock</tt>s
66in a <tt>Function</tt></a> </li>
67 <li><a href="#iterate_basicblock">Iterating over the <tt>Instruction</tt>s
68in a <tt>BasicBlock</tt></a> </li>
69 <li><a href="#iterate_institer">Iterating over the <tt>Instruction</tt>s
70in a <tt>Function</tt></a> </li>
71 <li><a href="#iterate_convert">Turning an iterator into a
72class pointer</a> </li>
73 <li><a href="#iterate_complex">Finding call sites: a more
74complex example</a> </li>
75 <li><a href="#calls_and_invokes">Treating calls and invokes
76the same way</a> </li>
77 <li><a href="#iterate_chains">Iterating over def-use &amp;
78use-def chains</a> </li>
79 </ul>
80 </li>
81 <li><a href="#simplechanges">Making simple changes</a>
82 <ul>
83 <li><a href="#schanges_creating">Creating and inserting new
84 <tt>Instruction</tt>s</a> </li>
85 <li><a href="#schanges_deleting">Deleting <tt>Instruction</tt>s</a> </li>
86 <li><a href="#schanges_replacing">Replacing an <tt>Instruction</tt>
87with another <tt>Value</tt></a> </li>
88 </ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +000089 </li>
Chris Lattnerae7f7592002-09-06 18:31:18 +000090<!--
91 <li>Working with the Control Flow Graph
92 <ul>
93 <li>Accessing predecessors and successors of a <tt>BasicBlock</tt>
94 <li>
95 <li>
96 </ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +000097-->
Chris Lattner261efe92003-11-25 01:02:51 +000098 </ul>
99 </li>
Chris Lattnerd9d6e102005-04-23 16:10:52 +0000100
101 <li><a href="#advanced">Advanced Topics</a>
102 <ul>
Chris Lattnerf1b200b2005-04-23 17:27:36 +0000103 <li><a href="#TypeResolve">LLVM Type Resolution</a>
104 <ul>
105 <li><a href="#BuildRecType">Basic Recursive Type Construction</a></li>
106 <li><a href="#refineAbstractTypeTo">The <tt>refineAbstractTypeTo</tt> method</a></li>
107 <li><a href="#PATypeHolder">The PATypeHolder Class</a></li>
108 <li><a href="#AbstractTypeUser">The AbstractTypeUser Class</a></li>
109 </ul></li>
110
Chris Lattnerd9d6e102005-04-23 16:10:52 +0000111 <li><a href="#SymbolTable">The <tt>SymbolTable</tt> class </a></li>
112 </ul></li>
113
Joel Stanley9b96c442002-09-06 21:55:13 +0000114 <li><a href="#coreclasses">The Core LLVM Class Hierarchy Reference</a>
Chris Lattner9355b472002-09-06 02:50:58 +0000115 <ul>
Reid Spencer303c4b42007-01-12 17:26:25 +0000116 <li><a href="#Type">The <tt>Type</tt> class</a> </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000117 <li><a href="#Value">The <tt>Value</tt> class</a>
Chris Lattner9355b472002-09-06 02:50:58 +0000118 <ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000119 <li><a href="#User">The <tt>User</tt> class</a>
Chris Lattner261efe92003-11-25 01:02:51 +0000120 <ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000121 <li><a href="#Instruction">The <tt>Instruction</tt> class</a>
122 <ul>
123 <li><a href="#GetElementPtrInst">The <tt>GetElementPtrInst</tt> class</a></li>
124 </ul>
125 </li>
126 <li><a href="#Module">The <tt>Module</tt> class</a></li>
127 <li><a href="#Constant">The <tt>Constant</tt> class</a>
128 <ul>
129 <li><a href="#GlobalValue">The <tt>GlobalValue</tt> class</a>
130 <ul>
131 <li><a href="#BasicBlock">The <tt>BasicBlock</tt>class</a></li>
132 <li><a href="#Function">The <tt>Function</tt> class</a></li>
133 <li><a href="#GlobalVariable">The <tt>GlobalVariable</tt> class</a></li>
134 </ul>
135 </li>
136 </ul>
137 </li>
Reid Spencer8b2da7a2004-07-18 13:10:31 +0000138 </ul>
139 </li>
Reid Spencer096603a2004-05-26 08:41:35 +0000140 <li><a href="#Argument">The <tt>Argument</tt> class</a></li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000141 </ul>
142 </li>
143 </ul>
Chris Lattner261efe92003-11-25 01:02:51 +0000144 </li>
Chris Lattner9355b472002-09-06 02:50:58 +0000145</ol>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000146
Chris Lattner69bf8a92004-05-23 21:06:58 +0000147<div class="doc_author">
148 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>,
Chris Lattner94c43592004-05-26 16:52:55 +0000149 <a href="mailto:dhurjati@cs.uiuc.edu">Dinakar Dhurjati</a>,
150 <a href="mailto:jstanley@cs.uiuc.edu">Joel Stanley</a>, and
151 <a href="mailto:rspencer@x10sys.com">Reid Spencer</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000152</div>
153
Chris Lattner9355b472002-09-06 02:50:58 +0000154<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000155<div class="doc_section">
156 <a name="introduction">Introduction </a>
157</div>
Chris Lattner9355b472002-09-06 02:50:58 +0000158<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000159
160<div class="doc_text">
161
162<p>This document is meant to highlight some of the important classes and
Chris Lattner261efe92003-11-25 01:02:51 +0000163interfaces available in the LLVM source-base. This manual is not
164intended to explain what LLVM is, how it works, and what LLVM code looks
165like. It assumes that you know the basics of LLVM and are interested
166in writing transformations or otherwise analyzing or manipulating the
Misha Brukman13fd15c2004-01-15 00:14:41 +0000167code.</p>
168
169<p>This document should get you oriented so that you can find your
Chris Lattner261efe92003-11-25 01:02:51 +0000170way in the continuously growing source code that makes up the LLVM
171infrastructure. Note that this manual is not intended to serve as a
172replacement for reading the source code, so if you think there should be
173a method in one of these classes to do something, but it's not listed,
174check the source. Links to the <a href="/doxygen/">doxygen</a> sources
175are provided to make this as easy as possible.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000176
177<p>The first section of this document describes general information that is
178useful to know when working in the LLVM infrastructure, and the second describes
179the Core LLVM classes. In the future this manual will be extended with
180information describing how to use extension libraries, such as dominator
181information, CFG traversal routines, and useful utilities like the <tt><a
182href="/doxygen/InstVisitor_8h-source.html">InstVisitor</a></tt> template.</p>
183
184</div>
185
Chris Lattner9355b472002-09-06 02:50:58 +0000186<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000187<div class="doc_section">
188 <a name="general">General Information</a>
189</div>
190<!-- *********************************************************************** -->
191
192<div class="doc_text">
193
194<p>This section contains general information that is useful if you are working
195in the LLVM source-base, but that isn't specific to any particular API.</p>
196
197</div>
198
199<!-- ======================================================================= -->
200<div class="doc_subsection">
201 <a name="stl">The C++ Standard Template Library</a>
202</div>
203
204<div class="doc_text">
205
206<p>LLVM makes heavy use of the C++ Standard Template Library (STL),
Chris Lattner261efe92003-11-25 01:02:51 +0000207perhaps much more than you are used to, or have seen before. Because of
208this, you might want to do a little background reading in the
209techniques used and capabilities of the library. There are many good
210pages that discuss the STL, and several books on the subject that you
Misha Brukman13fd15c2004-01-15 00:14:41 +0000211can get, so it will not be discussed in this document.</p>
212
213<p>Here are some useful links:</p>
214
215<ol>
216
217<li><a href="http://www.dinkumware.com/refxcpp.html">Dinkumware C++ Library
218reference</a> - an excellent reference for the STL and other parts of the
219standard C++ library.</li>
220
221<li><a href="http://www.tempest-sw.com/cpp/">C++ In a Nutshell</a> - This is an
Tanya Lattner09cf73c2004-06-22 04:24:55 +0000222O'Reilly book in the making. It has a decent
223Standard Library
224Reference that rivals Dinkumware's, and is unfortunately no longer free since the book has been
Misha Brukman13fd15c2004-01-15 00:14:41 +0000225published.</li>
226
227<li><a href="http://www.parashift.com/c++-faq-lite/">C++ Frequently Asked
228Questions</a></li>
229
230<li><a href="http://www.sgi.com/tech/stl/">SGI's STL Programmer's Guide</a> -
231Contains a useful <a
232href="http://www.sgi.com/tech/stl/stl_introduction.html">Introduction to the
233STL</a>.</li>
234
235<li><a href="http://www.research.att.com/%7Ebs/C++.html">Bjarne Stroustrup's C++
236Page</a></li>
237
Tanya Lattner79445ba2004-12-08 18:34:56 +0000238<li><a href="http://64.78.49.204/">
Reid Spencer096603a2004-05-26 08:41:35 +0000239Bruce Eckel's Thinking in C++, 2nd ed. Volume 2 Revision 4.0 (even better, get
240the book).</a></li>
241
Misha Brukman13fd15c2004-01-15 00:14:41 +0000242</ol>
243
244<p>You are also encouraged to take a look at the <a
245href="CodingStandards.html">LLVM Coding Standards</a> guide which focuses on how
246to write maintainable code more than where to put your curly braces.</p>
247
248</div>
249
250<!-- ======================================================================= -->
251<div class="doc_subsection">
252 <a name="stl">Other useful references</a>
253</div>
254
255<div class="doc_text">
256
Misha Brukman13fd15c2004-01-15 00:14:41 +0000257<ol>
258<li><a href="http://www.psc.edu/%7Esemke/cvs_branches.html">CVS
Chris Lattner261efe92003-11-25 01:02:51 +0000259Branch and Tag Primer</a></li>
Misha Brukmana0f71e42004-06-18 18:39:00 +0000260<li><a href="http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html">Using
261static and shared libraries across platforms</a></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000262</ol>
263
264</div>
265
Chris Lattner9355b472002-09-06 02:50:58 +0000266<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000267<div class="doc_section">
268 <a name="apis">Important and useful LLVM APIs</a>
269</div>
270<!-- *********************************************************************** -->
271
272<div class="doc_text">
273
274<p>Here we highlight some LLVM APIs that are generally useful and good to
275know about when writing transformations.</p>
276
277</div>
278
279<!-- ======================================================================= -->
280<div class="doc_subsection">
Misha Brukman2c122ce2005-11-01 21:12:49 +0000281 <a name="isa">The <tt>isa&lt;&gt;</tt>, <tt>cast&lt;&gt;</tt> and
282 <tt>dyn_cast&lt;&gt;</tt> templates</a>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000283</div>
284
285<div class="doc_text">
286
287<p>The LLVM source-base makes extensive use of a custom form of RTTI.
Chris Lattner261efe92003-11-25 01:02:51 +0000288These templates have many similarities to the C++ <tt>dynamic_cast&lt;&gt;</tt>
289operator, but they don't have some drawbacks (primarily stemming from
290the fact that <tt>dynamic_cast&lt;&gt;</tt> only works on classes that
291have a v-table). Because they are used so often, you must know what they
292do and how they work. All of these templates are defined in the <a
Chris Lattner695b78b2005-04-26 22:56:16 +0000293 href="/doxygen/Casting_8h-source.html"><tt>llvm/Support/Casting.h</tt></a>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000294file (note that you very rarely have to include this file directly).</p>
295
296<dl>
297 <dt><tt>isa&lt;&gt;</tt>: </dt>
298
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000299 <dd><p>The <tt>isa&lt;&gt;</tt> operator works exactly like the Java
Misha Brukman13fd15c2004-01-15 00:14:41 +0000300 "<tt>instanceof</tt>" operator. It returns true or false depending on whether
301 a reference or pointer points to an instance of the specified class. This can
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000302 be very useful for constraint checking of various sorts (example below).</p>
303 </dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000304
305 <dt><tt>cast&lt;&gt;</tt>: </dt>
306
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000307 <dd><p>The <tt>cast&lt;&gt;</tt> operator is a "checked cast" operation. It
Misha Brukman13fd15c2004-01-15 00:14:41 +0000308 converts a pointer or reference from a base class to a derived cast, causing
309 an assertion failure if it is not really an instance of the right type. This
310 should be used in cases where you have some information that makes you believe
311 that something is of the right type. An example of the <tt>isa&lt;&gt;</tt>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000312 and <tt>cast&lt;&gt;</tt> template is:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000313
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000314<div class="doc_code">
315<pre>
316static bool isLoopInvariant(const <a href="#Value">Value</a> *V, const Loop *L) {
317 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))
318 return true;
Chris Lattner69bf8a92004-05-23 21:06:58 +0000319
Bill Wendling82e2eea2006-10-11 18:00:22 +0000320 // <i>Otherwise, it must be an instruction...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000321 return !L-&gt;contains(cast&lt;<a href="#Instruction">Instruction</a>&gt;(V)-&gt;getParent());
322}
323</pre>
324</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000325
326 <p>Note that you should <b>not</b> use an <tt>isa&lt;&gt;</tt> test followed
327 by a <tt>cast&lt;&gt;</tt>, for that use the <tt>dyn_cast&lt;&gt;</tt>
328 operator.</p>
329
330 </dd>
331
332 <dt><tt>dyn_cast&lt;&gt;</tt>:</dt>
333
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000334 <dd><p>The <tt>dyn_cast&lt;&gt;</tt> operator is a "checking cast" operation.
335 It checks to see if the operand is of the specified type, and if so, returns a
Misha Brukman13fd15c2004-01-15 00:14:41 +0000336 pointer to it (this operator does not work with references). If the operand is
337 not of the correct type, a null pointer is returned. Thus, this works very
Misha Brukman2c122ce2005-11-01 21:12:49 +0000338 much like the <tt>dynamic_cast&lt;&gt;</tt> operator in C++, and should be
339 used in the same circumstances. Typically, the <tt>dyn_cast&lt;&gt;</tt>
340 operator is used in an <tt>if</tt> statement or some other flow control
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000341 statement like this:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000342
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000343<div class="doc_code">
344<pre>
345if (<a href="#AllocationInst">AllocationInst</a> *AI = dyn_cast&lt;<a href="#AllocationInst">AllocationInst</a>&gt;(Val)) {
Bill Wendling82e2eea2006-10-11 18:00:22 +0000346 // <i>...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000347}
348</pre>
349</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000350
Misha Brukman2c122ce2005-11-01 21:12:49 +0000351 <p>This form of the <tt>if</tt> statement effectively combines together a call
352 to <tt>isa&lt;&gt;</tt> and a call to <tt>cast&lt;&gt;</tt> into one
353 statement, which is very convenient.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000354
Misha Brukman2c122ce2005-11-01 21:12:49 +0000355 <p>Note that the <tt>dyn_cast&lt;&gt;</tt> operator, like C++'s
356 <tt>dynamic_cast&lt;&gt;</tt> or Java's <tt>instanceof</tt> operator, can be
357 abused. In particular, you should not use big chained <tt>if/then/else</tt>
358 blocks to check for lots of different variants of classes. If you find
359 yourself wanting to do this, it is much cleaner and more efficient to use the
360 <tt>InstVisitor</tt> class to dispatch over the instruction type directly.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000361
Misha Brukman2c122ce2005-11-01 21:12:49 +0000362 </dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000363
Misha Brukman2c122ce2005-11-01 21:12:49 +0000364 <dt><tt>cast_or_null&lt;&gt;</tt>: </dt>
365
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000366 <dd><p>The <tt>cast_or_null&lt;&gt;</tt> operator works just like the
Misha Brukman2c122ce2005-11-01 21:12:49 +0000367 <tt>cast&lt;&gt;</tt> operator, except that it allows for a null pointer as an
368 argument (which it then propagates). This can sometimes be useful, allowing
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000369 you to combine several null checks into one.</p></dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000370
Misha Brukman2c122ce2005-11-01 21:12:49 +0000371 <dt><tt>dyn_cast_or_null&lt;&gt;</tt>: </dt>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000372
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000373 <dd><p>The <tt>dyn_cast_or_null&lt;&gt;</tt> operator works just like the
Misha Brukman2c122ce2005-11-01 21:12:49 +0000374 <tt>dyn_cast&lt;&gt;</tt> operator, except that it allows for a null pointer
375 as an argument (which it then propagates). This can sometimes be useful,
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000376 allowing you to combine several null checks into one.</p></dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000377
Misha Brukman2c122ce2005-11-01 21:12:49 +0000378</dl>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000379
380<p>These five templates can be used with any classes, whether they have a
381v-table or not. To add support for these templates, you simply need to add
382<tt>classof</tt> static methods to the class you are interested casting
383to. Describing this is currently outside the scope of this document, but there
384are lots of examples in the LLVM source base.</p>
385
386</div>
387
388<!-- ======================================================================= -->
389<div class="doc_subsection">
Misha Brukman2c122ce2005-11-01 21:12:49 +0000390 <a name="DEBUG">The <tt>DEBUG()</tt> macro and <tt>-debug</tt> option</a>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000391</div>
392
393<div class="doc_text">
394
395<p>Often when working on your pass you will put a bunch of debugging printouts
396and other code into your pass. After you get it working, you want to remove
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000397it, but you may need it again in the future (to work out new bugs that you run
Misha Brukman13fd15c2004-01-15 00:14:41 +0000398across).</p>
399
400<p> Naturally, because of this, you don't want to delete the debug printouts,
401but you don't want them to always be noisy. A standard compromise is to comment
402them out, allowing you to enable them if you need them in the future.</p>
403
Chris Lattner695b78b2005-04-26 22:56:16 +0000404<p>The "<tt><a href="/doxygen/Debug_8h-source.html">llvm/Support/Debug.h</a></tt>"
Misha Brukman13fd15c2004-01-15 00:14:41 +0000405file provides a macro named <tt>DEBUG()</tt> that is a much nicer solution to
406this problem. Basically, you can put arbitrary code into the argument of the
407<tt>DEBUG</tt> macro, and it is only executed if '<tt>opt</tt>' (or any other
408tool) is run with the '<tt>-debug</tt>' command line argument:</p>
409
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000410<div class="doc_code">
411<pre>
Bill Wendling832171c2006-12-07 20:04:42 +0000412DOUT &lt;&lt; "I am here!\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000413</pre>
414</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000415
416<p>Then you can run your pass like this:</p>
417
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000418<div class="doc_code">
419<pre>
420$ opt &lt; a.bc &gt; /dev/null -mypass
Bill Wendling82e2eea2006-10-11 18:00:22 +0000421<i>&lt;no output&gt;</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000422$ opt &lt; a.bc &gt; /dev/null -mypass -debug
423I am here!
424</pre>
425</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000426
427<p>Using the <tt>DEBUG()</tt> macro instead of a home-brewed solution allows you
428to not have to create "yet another" command line option for the debug output for
429your pass. Note that <tt>DEBUG()</tt> macros are disabled for optimized builds,
430so they do not cause a performance impact at all (for the same reason, they
431should also not contain side-effects!).</p>
432
433<p>One additional nice thing about the <tt>DEBUG()</tt> macro is that you can
434enable or disable it directly in gdb. Just use "<tt>set DebugFlag=0</tt>" or
435"<tt>set DebugFlag=1</tt>" from the gdb if the program is running. If the
436program hasn't been started yet, you can always just run it with
437<tt>-debug</tt>.</p>
438
439</div>
440
441<!-- _______________________________________________________________________ -->
442<div class="doc_subsubsection">
Chris Lattnerc9151082005-04-26 22:57:07 +0000443 <a name="DEBUG_TYPE">Fine grained debug info with <tt>DEBUG_TYPE</tt> and
Misha Brukman13fd15c2004-01-15 00:14:41 +0000444 the <tt>-debug-only</tt> option</a>
445</div>
446
447<div class="doc_text">
448
449<p>Sometimes you may find yourself in a situation where enabling <tt>-debug</tt>
450just turns on <b>too much</b> information (such as when working on the code
451generator). If you want to enable debug information with more fine-grained
452control, you define the <tt>DEBUG_TYPE</tt> macro and the <tt>-debug</tt> only
453option as follows:</p>
454
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000455<div class="doc_code">
456<pre>
Bill Wendling832171c2006-12-07 20:04:42 +0000457DOUT &lt;&lt; "No debug type\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000458#undef DEBUG_TYPE
459#define DEBUG_TYPE "foo"
Bill Wendling832171c2006-12-07 20:04:42 +0000460DOUT &lt;&lt; "'foo' debug type\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000461#undef DEBUG_TYPE
462#define DEBUG_TYPE "bar"
Bill Wendling832171c2006-12-07 20:04:42 +0000463DOUT &lt;&lt; "'bar' debug type\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000464#undef DEBUG_TYPE
465#define DEBUG_TYPE ""
Bill Wendling832171c2006-12-07 20:04:42 +0000466DOUT &lt;&lt; "No debug type (2)\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000467</pre>
468</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000469
470<p>Then you can run your pass like this:</p>
471
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000472<div class="doc_code">
473<pre>
474$ opt &lt; a.bc &gt; /dev/null -mypass
Bill Wendling82e2eea2006-10-11 18:00:22 +0000475<i>&lt;no output&gt;</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000476$ opt &lt; a.bc &gt; /dev/null -mypass -debug
477No debug type
478'foo' debug type
479'bar' debug type
480No debug type (2)
481$ opt &lt; a.bc &gt; /dev/null -mypass -debug-only=foo
482'foo' debug type
483$ opt &lt; a.bc &gt; /dev/null -mypass -debug-only=bar
484'bar' debug type
485</pre>
486</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000487
488<p>Of course, in practice, you should only set <tt>DEBUG_TYPE</tt> at the top of
489a file, to specify the debug type for the entire module (if you do this before
Chris Lattner695b78b2005-04-26 22:56:16 +0000490you <tt>#include "llvm/Support/Debug.h"</tt>, you don't have to insert the ugly
Misha Brukman13fd15c2004-01-15 00:14:41 +0000491<tt>#undef</tt>'s). Also, you should use names more meaningful than "foo" and
492"bar", because there is no system in place to ensure that names do not
493conflict. If two different modules use the same string, they will all be turned
494on when the name is specified. This allows, for example, all debug information
495for instruction scheduling to be enabled with <tt>-debug-type=InstrSched</tt>,
Chris Lattner261efe92003-11-25 01:02:51 +0000496even if the source lives in multiple files.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000497
498</div>
499
500<!-- ======================================================================= -->
501<div class="doc_subsection">
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000502 <a name="Statistic">The <tt>Statistic</tt> class &amp; <tt>-stats</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000503 option</a>
504</div>
505
506<div class="doc_text">
507
508<p>The "<tt><a
Chris Lattner695b78b2005-04-26 22:56:16 +0000509href="/doxygen/Statistic_8h-source.html">llvm/ADT/Statistic.h</a></tt>" file
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000510provides a class named <tt>Statistic</tt> that is used as a unified way to
Misha Brukman13fd15c2004-01-15 00:14:41 +0000511keep track of what the LLVM compiler is doing and how effective various
512optimizations are. It is useful to see what optimizations are contributing to
513making a particular program run faster.</p>
514
515<p>Often you may run your pass on some big program, and you're interested to see
516how many times it makes a certain transformation. Although you can do this with
517hand inspection, or some ad-hoc method, this is a real pain and not very useful
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000518for big programs. Using the <tt>Statistic</tt> class makes it very easy to
Misha Brukman13fd15c2004-01-15 00:14:41 +0000519keep track of this information, and the calculated information is presented in a
520uniform manner with the rest of the passes being executed.</p>
521
522<p>There are many examples of <tt>Statistic</tt> uses, but the basics of using
523it are as follows:</p>
524
525<ol>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000526 <li><p>Define your statistic like this:</p>
527
528<div class="doc_code">
529<pre>
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000530#define <a href="#DEBUG_TYPE">DEBUG_TYPE</a> "mypassname" <i>// This goes before any #includes.</i>
531STATISTIC(NumXForms, "The # of times I did stuff");
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000532</pre>
533</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000534
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000535 <p>The <tt>STATISTIC</tt> macro defines a static variable, whose name is
536 specified by the first argument. The pass name is taken from the DEBUG_TYPE
537 macro, and the description is taken from the second argument. The variable
Reid Spencer06565dc2007-01-12 17:11:23 +0000538 defined ("NumXForms" in this case) acts like an unsigned integer.</p></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000539
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000540 <li><p>Whenever you make a transformation, bump the counter:</p>
541
542<div class="doc_code">
543<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +0000544++NumXForms; // <i>I did stuff!</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000545</pre>
546</div>
547
Chris Lattner261efe92003-11-25 01:02:51 +0000548 </li>
549 </ol>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000550
551 <p>That's all you have to do. To get '<tt>opt</tt>' to print out the
552 statistics gathered, use the '<tt>-stats</tt>' option:</p>
553
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000554<div class="doc_code">
555<pre>
556$ opt -stats -mypassname &lt; program.bc &gt; /dev/null
Bill Wendling82e2eea2006-10-11 18:00:22 +0000557<i>... statistics output ...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000558</pre>
559</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000560
Chris Lattner261efe92003-11-25 01:02:51 +0000561 <p> When running <tt>gccas</tt> on a C file from the SPEC benchmark
562suite, it gives a report that looks like this:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000563
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000564<div class="doc_code">
565<pre>
566 7646 bytecodewriter - Number of normal instructions
567 725 bytecodewriter - Number of oversized instructions
568 129996 bytecodewriter - Number of bytecode bytes written
569 2817 raise - Number of insts DCEd or constprop'd
570 3213 raise - Number of cast-of-self removed
571 5046 raise - Number of expression trees converted
572 75 raise - Number of other getelementptr's formed
573 138 raise - Number of load/store peepholes
574 42 deadtypeelim - Number of unused typenames removed from symtab
575 392 funcresolve - Number of varargs functions resolved
576 27 globaldce - Number of global variables removed
577 2 adce - Number of basic blocks removed
578 134 cee - Number of branches revectored
579 49 cee - Number of setcc instruction eliminated
580 532 gcse - Number of loads removed
581 2919 gcse - Number of instructions removed
582 86 indvars - Number of canonical indvars added
583 87 indvars - Number of aux indvars removed
584 25 instcombine - Number of dead inst eliminate
585 434 instcombine - Number of insts combined
586 248 licm - Number of load insts hoisted
587 1298 licm - Number of insts hoisted to a loop pre-header
588 3 licm - Number of insts hoisted to multiple loop preds (bad, no loop pre-header)
589 75 mem2reg - Number of alloca's promoted
590 1444 cfgsimplify - Number of blocks simplified
591</pre>
592</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000593
594<p>Obviously, with so many optimizations, having a unified framework for this
595stuff is very nice. Making your pass fit well into the framework makes it more
596maintainable and useful.</p>
597
598</div>
599
Chris Lattnerf623a082005-10-17 01:36:23 +0000600<!-- ======================================================================= -->
601<div class="doc_subsection">
602 <a name="ViewGraph">Viewing graphs while debugging code</a>
603</div>
604
605<div class="doc_text">
606
607<p>Several of the important data structures in LLVM are graphs: for example
608CFGs made out of LLVM <a href="#BasicBlock">BasicBlock</a>s, CFGs made out of
609LLVM <a href="CodeGenerator.html#machinebasicblock">MachineBasicBlock</a>s, and
610<a href="CodeGenerator.html#selectiondag_intro">Instruction Selection
611DAGs</a>. In many cases, while debugging various parts of the compiler, it is
612nice to instantly visualize these graphs.</p>
613
614<p>LLVM provides several callbacks that are available in a debug build to do
615exactly that. If you call the <tt>Function::viewCFG()</tt> method, for example,
616the current LLVM tool will pop up a window containing the CFG for the function
617where each basic block is a node in the graph, and each node contains the
618instructions in the block. Similarly, there also exists
619<tt>Function::viewCFGOnly()</tt> (does not include the instructions), the
620<tt>MachineFunction::viewCFG()</tt> and <tt>MachineFunction::viewCFGOnly()</tt>,
621and the <tt>SelectionDAG::viewGraph()</tt> methods. Within GDB, for example,
Jim Laskey543a0ee2006-10-02 12:28:07 +0000622you can usually use something like <tt>call DAG.viewGraph()</tt> to pop
Chris Lattnerf623a082005-10-17 01:36:23 +0000623up a window. Alternatively, you can sprinkle calls to these functions in your
624code in places you want to debug.</p>
625
626<p>Getting this to work requires a small amount of configuration. On Unix
627systems with X11, install the <a href="http://www.graphviz.org">graphviz</a>
628toolkit, and make sure 'dot' and 'gv' are in your path. If you are running on
629Mac OS/X, download and install the Mac OS/X <a
630href="http://www.pixelglow.com/graphviz/">Graphviz program</a>, and add
631<tt>/Applications/Graphviz.app/Contents/MacOS/</tt> (or whereever you install
632it) to your path. Once in your system and path are set up, rerun the LLVM
633configure script and rebuild LLVM to enable this functionality.</p>
634
Jim Laskey543a0ee2006-10-02 12:28:07 +0000635<p><tt>SelectionDAG</tt> has been extended to make it easier to locate
636<i>interesting</i> nodes in large complex graphs. From gdb, if you
637<tt>call DAG.setGraphColor(<i>node</i>, "<i>color</i>")</tt>, then the
638next <tt>call DAG.viewGraph()</tt> would hilight the node in the
639specified color (choices of colors can be found at <a
640href="http://www.graphviz.org/doc/info/colors.html">Colors<a>.) More
641complex node attributes can be provided with <tt>call
642DAG.setGraphAttrs(<i>node</i>, "<i>attributes</i>")</tt> (choices can be
643found at <a href="http://www.graphviz.org/doc/info/attrs.html">Graph
644Attributes</a>.) If you want to restart and clear all the current graph
645attributes, then you can <tt>call DAG.clearGraphAttrs()</tt>. </p>
646
Chris Lattnerf623a082005-10-17 01:36:23 +0000647</div>
648
Chris Lattner098129a2007-02-03 03:04:03 +0000649<!-- *********************************************************************** -->
650<div class="doc_section">
651 <a name="datastructure">Picking the Right Data Structure for a Task</a>
652</div>
653<!-- *********************************************************************** -->
654
655<div class="doc_text">
656
657<p>LLVM has a plethora of datastructures in the <tt>llvm/ADT/</tt> directory,
658 and we commonly use STL datastructures. This section describes the tradeoffs
659 you should consider when you pick one.</p>
660
661<p>
662The first step is a choose your own adventure: do you want a sequential
663container, a set-like container, or a map-like container? The most important
664thing when choosing a container is the algorithmic properties of how you plan to
665access the container. Based on that, you should use:</p>
666
667<ul>
668<li>a <a href="#ds_map">map-like</a> container if you need efficient lookup
669 of an value based on another value. Map-like containers also support
670 efficient queries for containment (whether a key is in the map). Map-like
671 containers generally do not support efficient reverse mapping (values to
672 keys). If you need that, use two maps. Some map-like containers also
673 support efficient iteration through the keys in sorted order. Map-like
674 containers are the most expensive sort, only use them if you need one of
675 these capabilities.</li>
676
677<li>a <a href="#ds_set">set-like</a> container if you need to put a bunch of
678 stuff into a container that automatically eliminates duplicates. Some
679 set-like containers support efficient iteration through the elements in
680 sorted order. Set-like containers are more expensive than sequential
681 containers.
682</li>
683
684<li>a <a href="#ds_sequential">sequential</a> container provides
685 the most efficient way to add elements and keeps track of the order they are
686 added to the collection. They permit duplicates and support efficient
687 iteration, but do not support efficient lookup based on a key.
688</li>
689
690</ul>
691
692<p>
693Once the proper catagory of container is determined, you can fine tune the
694memory use, constant factors, and cache behaviors of access by intelligently
695picking a member of the catagory. Note that constant factors and cache behavior
696can be a big deal. If you have a vector that usually only contains a few
697elements (but could contain many), for example, it's much better to use
698<a href="#dss_smallvector">SmallVector</a> than <a href="#dss_vector">vector</a>
699. Doing so avoids (relatively) expensive malloc/free calls, which dwarf the
700cost of adding the elements to the container. </p>
701
702</div>
703
704<!-- ======================================================================= -->
705<div class="doc_subsection">
706 <a name="ds_sequential">Sequential Containers (std::vector, std::list, etc)</a>
707</div>
708
709<div class="doc_text">
710There are a variety of sequential containers available for you, based on your
711needs. Pick the first in this section that will do what you want.
712</div>
713
714<!-- _______________________________________________________________________ -->
715<div class="doc_subsubsection">
716 <a name="dss_fixedarrays">Fixed Size Arrays</a>
717</div>
718
719<div class="doc_text">
720<p>Fixed size arrays are very simple and very fast. They are good if you know
721exactly how many elements you have, or you have a (low) upper bound on how many
722you have.</p>
723</div>
724
725<!-- _______________________________________________________________________ -->
726<div class="doc_subsubsection">
727 <a name="dss_heaparrays">Heap Allocated Arrays</a>
728</div>
729
730<div class="doc_text">
731<p>Heap allocated arrays (new[] + delete[]) are also simple. They are good if
732the number of elements is variable, if you know how many elements you will need
733before the array is allocated, and if the array is usually large (if not,
734consider a <a href="#dss_smallvector">SmallVector</a>). The cost of a heap
735allocated array is the cost of the new/delete (aka malloc/free). Also note that
736if you are allocating an array of a type with a constructor, the constructor and
737destructors will be run for every element in the array (resizable vectors only
738construct those elements actually used).</p>
739</div>
740
741<!-- _______________________________________________________________________ -->
742<div class="doc_subsubsection">
743 <a name="dss_smallvector">"llvm/ADT/SmallVector.h"</a>
744</div>
745
746<div class="doc_text">
747<p><tt>SmallVector&lt;Type, N&gt;</tt> is a simple class that looks and smells
748just like <tt>vector&lt;Type&gt;</tt>:
749it supports efficient iteration, lays out elements in memory order (so you can
750do pointer arithmetic between elements), supports efficient push_back/pop_back
751operations, supports efficient random access to its elements, etc.</p>
752
753<p>The advantage of SmallVector is that it allocates space for
754some number of elements (N) <b>in the object itself</b>. Because of this, if
755the SmallVector is dynamically smaller than N, no malloc is performed. This can
756be a big win in cases where the malloc/free call is far more expensive than the
757code that fiddles around with the elements.</p>
758
759<p>This is good for vectors that are "usually small" (e.g. the number of
760predecessors/successors of a block is usually less than 8). On the other hand,
761this makes the size of the SmallVector itself large, so you don't want to
762allocate lots of them (doing so will waste a lot of space). As such,
763SmallVectors are most useful when on the stack.</p>
764
765<p>SmallVector also provides a nice portable and efficient replacement for
766<tt>alloca</tt>.</p>
767
768</div>
769
770<!-- _______________________________________________________________________ -->
771<div class="doc_subsubsection">
772 <a name="dss_vector">&lt;vector&gt;</a>
773</div>
774
775<div class="doc_text">
776<p>
777std::vector is well loved and respected. It is useful when SmallVector isn't:
778when the size of the vector is often large (thus the small optimization will
779rarely be a benefit) or if you will be allocating many instances of the vector
780itself (which would waste space for elements that aren't in the container).
781vector is also useful when interfacing with code that expects vectors :).
782</p>
783</div>
784
785<!-- _______________________________________________________________________ -->
786<div class="doc_subsubsection">
787 <a name="dss_list">&lt;list&gt;</a>
788</div>
789
790<div class="doc_text">
791<p>std::list is an extremely inefficient class that is rarely useful.
792It performs a heap allocation for every element inserted into it, thus having an
793extremely high constant factor, particularly for small data types. std::list
794also only supports bidirectional iteration, not random access iteration.</p>
795
796<p>In exchange for this high cost, std::list supports efficient access to both
797ends of the list (like std::deque, but unlike std::vector or SmallVector). In
798addition, the iterator invalidation characteristics of std::list are stronger
799than that of a vector class: inserting or removing an element into the list does
800not invalidate iterator or pointers to other elements in the list.</p>
801</div>
802
803<!-- _______________________________________________________________________ -->
804<div class="doc_subsubsection">
805 <a name="dss_ilist">llvm/ADT/ilist</a>
806</div>
807
808<div class="doc_text">
809<p><tt>ilist&lt;T&gt;</tt> implements an 'intrusive' doubly-linked list. It is
810intrusive, because it requires the element to store and provide access to the
811prev/next pointers for the list.</p>
812
813<p>ilist has the same drawbacks as std::list, and additionally requires an
814ilist_traits implementation for the element type, but it provides some novel
815characteristics. In particular, it can efficiently store polymorphic objects,
816the traits class is informed when an element is inserted or removed from the
817list, and ilists are guaranteed to support a constant-time splice operation.
818</p>
819
820<p>These properties are exactly what we want for things like Instructions and
821basic blocks, which is why these are implemented with ilists.</p>
822</div>
823
824<!-- _______________________________________________________________________ -->
825<div class="doc_subsubsection">
826 <a name="dss_other">Other options</a>
827</div>
828
829<div class="doc_text">
830<p>Other STL containers are available, such as std::deque (which has similar
831characteristics to std::vector, but has higher constant factors and provides
832efficient push_front/pop_front methods) and std::string.</p>
833
834<p>There are also various STL adapter classes such as std::queue,
835std::priority_queue, std::stack, etc. These provide simplified access to an
836underlying container but don't affect the cost of the container itself.</p>
837
838</div>
839
840
841<!-- ======================================================================= -->
842<div class="doc_subsection">
843 <a name="ds_set">Set-Like Containers (std::set, SmallSet, SetVector, etc)</a>
844</div>
845
846<div class="doc_text">
847
848<p>
849SmallPtrSet
850SmallSet
851sorted vector
852FoldingSet
853hash_set
854UniqueVector
855SetVector
856</p>
857
858</div>
859
860<!-- ======================================================================= -->
861<div class="doc_subsection">
862 <a name="ds_map">Map-Like Containers (std::map, DenseMap, etc)</a>
863</div>
864
865<div class="doc_text">
866sorted vector
867std::map
868DenseMap
869IndexedMap
870hash_map
871CStringMap
872</div>
873
Chris Lattnerf623a082005-10-17 01:36:23 +0000874
Misha Brukman13fd15c2004-01-15 00:14:41 +0000875<!-- *********************************************************************** -->
876<div class="doc_section">
877 <a name="common">Helpful Hints for Common Operations</a>
878</div>
879<!-- *********************************************************************** -->
880
881<div class="doc_text">
882
883<p>This section describes how to perform some very simple transformations of
884LLVM code. This is meant to give examples of common idioms used, showing the
885practical side of LLVM transformations. <p> Because this is a "how-to" section,
886you should also read about the main classes that you will be working with. The
887<a href="#coreclasses">Core LLVM Class Hierarchy Reference</a> contains details
888and descriptions of the main classes that you should know about.</p>
889
890</div>
891
892<!-- NOTE: this section should be heavy on example code -->
893<!-- ======================================================================= -->
894<div class="doc_subsection">
895 <a name="inspection">Basic Inspection and Traversal Routines</a>
896</div>
897
898<div class="doc_text">
899
900<p>The LLVM compiler infrastructure have many different data structures that may
901be traversed. Following the example of the C++ standard template library, the
902techniques used to traverse these various data structures are all basically the
903same. For a enumerable sequence of values, the <tt>XXXbegin()</tt> function (or
904method) returns an iterator to the start of the sequence, the <tt>XXXend()</tt>
905function returns an iterator pointing to one past the last valid element of the
906sequence, and there is some <tt>XXXiterator</tt> data type that is common
907between the two operations.</p>
908
909<p>Because the pattern for iteration is common across many different aspects of
910the program representation, the standard template library algorithms may be used
911on them, and it is easier to remember how to iterate. First we show a few common
912examples of the data structures that need to be traversed. Other data
913structures are traversed in very similar ways.</p>
914
915</div>
916
917<!-- _______________________________________________________________________ -->
Chris Lattner69bf8a92004-05-23 21:06:58 +0000918<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +0000919 <a name="iterate_function">Iterating over the </a><a
920 href="#BasicBlock"><tt>BasicBlock</tt></a>s in a <a
921 href="#Function"><tt>Function</tt></a>
922</div>
923
924<div class="doc_text">
925
926<p>It's quite common to have a <tt>Function</tt> instance that you'd like to
927transform in some way; in particular, you'd like to manipulate its
928<tt>BasicBlock</tt>s. To facilitate this, you'll need to iterate over all of
929the <tt>BasicBlock</tt>s that constitute the <tt>Function</tt>. The following is
930an example that prints the name of a <tt>BasicBlock</tt> and the number of
931<tt>Instruction</tt>s it contains:</p>
932
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000933<div class="doc_code">
934<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +0000935// <i>func is a pointer to a Function instance</i>
936for (Function::iterator i = func-&gt;begin(), e = func-&gt;end(); i != e; ++i)
937 // <i>Print out the name of the basic block if it has one, and then the</i>
938 // <i>number of instructions that it contains</i>
Bill Wendling832171c2006-12-07 20:04:42 +0000939 llvm::cerr &lt;&lt; "Basic block (name=" &lt;&lt; i-&gt;getName() &lt;&lt; ") has "
940 &lt;&lt; i-&gt;size() &lt;&lt; " instructions.\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000941</pre>
942</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000943
944<p>Note that i can be used as if it were a pointer for the purposes of
Joel Stanley9b96c442002-09-06 21:55:13 +0000945invoking member functions of the <tt>Instruction</tt> class. This is
946because the indirection operator is overloaded for the iterator
Chris Lattner7496ec52003-08-05 22:54:23 +0000947classes. In the above code, the expression <tt>i-&gt;size()</tt> is
Misha Brukman13fd15c2004-01-15 00:14:41 +0000948exactly equivalent to <tt>(*i).size()</tt> just like you'd expect.</p>
949
950</div>
951
952<!-- _______________________________________________________________________ -->
Chris Lattner69bf8a92004-05-23 21:06:58 +0000953<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +0000954 <a name="iterate_basicblock">Iterating over the </a><a
955 href="#Instruction"><tt>Instruction</tt></a>s in a <a
956 href="#BasicBlock"><tt>BasicBlock</tt></a>
957</div>
958
959<div class="doc_text">
960
961<p>Just like when dealing with <tt>BasicBlock</tt>s in <tt>Function</tt>s, it's
962easy to iterate over the individual instructions that make up
963<tt>BasicBlock</tt>s. Here's a code snippet that prints out each instruction in
964a <tt>BasicBlock</tt>:</p>
965
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000966<div class="doc_code">
Chris Lattner55c04612005-03-06 06:00:13 +0000967<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +0000968// <i>blk is a pointer to a BasicBlock instance</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000969for (BasicBlock::iterator i = blk-&gt;begin(), e = blk-&gt;end(); i != e; ++i)
Bill Wendling82e2eea2006-10-11 18:00:22 +0000970 // <i>The next statement works since operator&lt;&lt;(ostream&amp;,...)</i>
971 // <i>is overloaded for Instruction&amp;</i>
Bill Wendling832171c2006-12-07 20:04:42 +0000972 llvm::cerr &lt;&lt; *i &lt;&lt; "\n";
Chris Lattner55c04612005-03-06 06:00:13 +0000973</pre>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000974</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000975
976<p>However, this isn't really the best way to print out the contents of a
977<tt>BasicBlock</tt>! Since the ostream operators are overloaded for virtually
978anything you'll care about, you could have just invoked the print routine on the
Bill Wendling832171c2006-12-07 20:04:42 +0000979basic block itself: <tt>llvm::cerr &lt;&lt; *blk &lt;&lt; "\n";</tt>.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000980
981</div>
982
983<!-- _______________________________________________________________________ -->
Chris Lattner69bf8a92004-05-23 21:06:58 +0000984<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +0000985 <a name="iterate_institer">Iterating over the </a><a
986 href="#Instruction"><tt>Instruction</tt></a>s in a <a
987 href="#Function"><tt>Function</tt></a>
988</div>
989
990<div class="doc_text">
991
992<p>If you're finding that you commonly iterate over a <tt>Function</tt>'s
993<tt>BasicBlock</tt>s and then that <tt>BasicBlock</tt>'s <tt>Instruction</tt>s,
994<tt>InstIterator</tt> should be used instead. You'll need to include <a
995href="/doxygen/InstIterator_8h-source.html"><tt>llvm/Support/InstIterator.h</tt></a>,
996and then instantiate <tt>InstIterator</tt>s explicitly in your code. Here's a
Chris Lattner69bf8a92004-05-23 21:06:58 +0000997small example that shows how to dump all instructions in a function to the standard error stream:<p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000998
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000999<div class="doc_code">
1000<pre>
1001#include "<a href="/doxygen/InstIterator_8h-source.html">llvm/Support/InstIterator.h</a>"
1002
Bill Wendling82e2eea2006-10-11 18:00:22 +00001003// <i>F is a ptr to a Function instance</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001004for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
Bill Wendling832171c2006-12-07 20:04:42 +00001005 llvm::cerr &lt;&lt; *i &lt;&lt; "\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001006</pre>
1007</div>
1008
1009<p>Easy, isn't it? You can also use <tt>InstIterator</tt>s to fill a
Joel Stanleye7be6502002-09-09 15:50:33 +00001010worklist with its initial contents. For example, if you wanted to
Chris Lattner261efe92003-11-25 01:02:51 +00001011initialize a worklist to contain all instructions in a <tt>Function</tt>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001012F, all you would need to do is something like:</p>
1013
1014<div class="doc_code">
1015<pre>
1016std::set&lt;Instruction*&gt; worklist;
1017worklist.insert(inst_begin(F), inst_end(F));
1018</pre>
1019</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001020
1021<p>The STL set <tt>worklist</tt> would now contain all instructions in the
1022<tt>Function</tt> pointed to by F.</p>
1023
1024</div>
1025
1026<!-- _______________________________________________________________________ -->
1027<div class="doc_subsubsection">
1028 <a name="iterate_convert">Turning an iterator into a class pointer (and
1029 vice-versa)</a>
1030</div>
1031
1032<div class="doc_text">
1033
1034<p>Sometimes, it'll be useful to grab a reference (or pointer) to a class
Joel Stanley9b96c442002-09-06 21:55:13 +00001035instance when all you've got at hand is an iterator. Well, extracting
Chris Lattner69bf8a92004-05-23 21:06:58 +00001036a reference or a pointer from an iterator is very straight-forward.
Chris Lattner261efe92003-11-25 01:02:51 +00001037Assuming that <tt>i</tt> is a <tt>BasicBlock::iterator</tt> and <tt>j</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001038is a <tt>BasicBlock::const_iterator</tt>:</p>
1039
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001040<div class="doc_code">
1041<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001042Instruction&amp; inst = *i; // <i>Grab reference to instruction reference</i>
1043Instruction* pinst = &amp;*i; // <i>Grab pointer to instruction reference</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001044const Instruction&amp; inst = *j;
1045</pre>
1046</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001047
1048<p>However, the iterators you'll be working with in the LLVM framework are
1049special: they will automatically convert to a ptr-to-instance type whenever they
1050need to. Instead of dereferencing the iterator and then taking the address of
1051the result, you can simply assign the iterator to the proper pointer type and
1052you get the dereference and address-of operation as a result of the assignment
1053(behind the scenes, this is a result of overloading casting mechanisms). Thus
1054the last line of the last example,</p>
1055
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001056<div class="doc_code">
1057<pre>
1058Instruction* pinst = &amp;*i;
1059</pre>
1060</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001061
1062<p>is semantically equivalent to</p>
1063
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001064<div class="doc_code">
1065<pre>
1066Instruction* pinst = i;
1067</pre>
1068</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001069
Chris Lattner69bf8a92004-05-23 21:06:58 +00001070<p>It's also possible to turn a class pointer into the corresponding iterator,
1071and this is a constant time operation (very efficient). The following code
1072snippet illustrates use of the conversion constructors provided by LLVM
1073iterators. By using these, you can explicitly grab the iterator of something
1074without actually obtaining it via iteration over some structure:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001075
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001076<div class="doc_code">
1077<pre>
1078void printNextInstruction(Instruction* inst) {
1079 BasicBlock::iterator it(inst);
Bill Wendling82e2eea2006-10-11 18:00:22 +00001080 ++it; // <i>After this line, it refers to the instruction after *inst</i>
Bill Wendling832171c2006-12-07 20:04:42 +00001081 if (it != inst-&gt;getParent()-&gt;end()) llvm::cerr &lt;&lt; *it &lt;&lt; "\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001082}
1083</pre>
1084</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001085
Misha Brukman13fd15c2004-01-15 00:14:41 +00001086</div>
1087
1088<!--_______________________________________________________________________-->
1089<div class="doc_subsubsection">
1090 <a name="iterate_complex">Finding call sites: a slightly more complex
1091 example</a>
1092</div>
1093
1094<div class="doc_text">
1095
1096<p>Say that you're writing a FunctionPass and would like to count all the
1097locations in the entire module (that is, across every <tt>Function</tt>) where a
1098certain function (i.e., some <tt>Function</tt>*) is already in scope. As you'll
1099learn later, you may want to use an <tt>InstVisitor</tt> to accomplish this in a
Chris Lattner69bf8a92004-05-23 21:06:58 +00001100much more straight-forward manner, but this example will allow us to explore how
Misha Brukman13fd15c2004-01-15 00:14:41 +00001101you'd do it if you didn't have <tt>InstVisitor</tt> around. In pseudocode, this
1102is what we want to do:</p>
1103
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001104<div class="doc_code">
1105<pre>
1106initialize callCounter to zero
1107for each Function f in the Module
1108 for each BasicBlock b in f
1109 for each Instruction i in b
1110 if (i is a CallInst and calls the given function)
1111 increment callCounter
1112</pre>
1113</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001114
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001115<p>And the actual code is (remember, because we're writing a
Misha Brukman13fd15c2004-01-15 00:14:41 +00001116<tt>FunctionPass</tt>, our <tt>FunctionPass</tt>-derived class simply has to
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001117override the <tt>runOnFunction</tt> method):</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001118
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001119<div class="doc_code">
1120<pre>
1121Function* targetFunc = ...;
1122
1123class OurFunctionPass : public FunctionPass {
1124 public:
1125 OurFunctionPass(): callCounter(0) { }
1126
1127 virtual runOnFunction(Function&amp; F) {
1128 for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) {
1129 for (BasicBlock::iterator i = b-&gt;begin(); ie = b-&gt;end(); i != ie; ++i) {
1130 if (<a href="#CallInst">CallInst</a>* callInst = <a href="#isa">dyn_cast</a>&lt;<a
1131 href="#CallInst">CallInst</a>&gt;(&amp;*i)) {
Bill Wendling82e2eea2006-10-11 18:00:22 +00001132 // <i>We know we've encountered a call instruction, so we</i>
1133 // <i>need to determine if it's a call to the</i>
1134 // <i>function pointed to by m_func or not</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001135
1136 if (callInst-&gt;getCalledFunction() == targetFunc)
1137 ++callCounter;
1138 }
1139 }
1140 }
Bill Wendling82e2eea2006-10-11 18:00:22 +00001141 }
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001142
1143 private:
1144 unsigned callCounter;
1145};
1146</pre>
1147</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001148
1149</div>
1150
Brian Gaekef1972c62003-11-07 19:25:45 +00001151<!--_______________________________________________________________________-->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001152<div class="doc_subsubsection">
1153 <a name="calls_and_invokes">Treating calls and invokes the same way</a>
1154</div>
1155
1156<div class="doc_text">
1157
1158<p>You may have noticed that the previous example was a bit oversimplified in
1159that it did not deal with call sites generated by 'invoke' instructions. In
1160this, and in other situations, you may find that you want to treat
1161<tt>CallInst</tt>s and <tt>InvokeInst</tt>s the same way, even though their
1162most-specific common base class is <tt>Instruction</tt>, which includes lots of
1163less closely-related things. For these cases, LLVM provides a handy wrapper
1164class called <a
Reid Spencer05fe4b02006-03-14 05:39:39 +00001165href="http://llvm.org/doxygen/classllvm_1_1CallSite.html"><tt>CallSite</tt></a>.
Chris Lattner69bf8a92004-05-23 21:06:58 +00001166It is essentially a wrapper around an <tt>Instruction</tt> pointer, with some
1167methods that provide functionality common to <tt>CallInst</tt>s and
Misha Brukman13fd15c2004-01-15 00:14:41 +00001168<tt>InvokeInst</tt>s.</p>
1169
Chris Lattner69bf8a92004-05-23 21:06:58 +00001170<p>This class has "value semantics": it should be passed by value, not by
1171reference and it should not be dynamically allocated or deallocated using
1172<tt>operator new</tt> or <tt>operator delete</tt>. It is efficiently copyable,
1173assignable and constructable, with costs equivalents to that of a bare pointer.
1174If you look at its definition, it has only a single pointer member.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001175
1176</div>
1177
Chris Lattner1a3105b2002-09-09 05:49:39 +00001178<!--_______________________________________________________________________-->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001179<div class="doc_subsubsection">
1180 <a name="iterate_chains">Iterating over def-use &amp; use-def chains</a>
1181</div>
1182
1183<div class="doc_text">
1184
1185<p>Frequently, we might have an instance of the <a
Chris Lattner00815172007-01-04 22:01:45 +00001186href="/doxygen/classllvm_1_1Value.html">Value Class</a> and we want to
Misha Brukman384047f2004-06-03 23:29:12 +00001187determine which <tt>User</tt>s use the <tt>Value</tt>. The list of all
1188<tt>User</tt>s of a particular <tt>Value</tt> is called a <i>def-use</i> chain.
1189For example, let's say we have a <tt>Function*</tt> named <tt>F</tt> to a
1190particular function <tt>foo</tt>. Finding all of the instructions that
1191<i>use</i> <tt>foo</tt> is as simple as iterating over the <i>def-use</i> chain
1192of <tt>F</tt>:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001193
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001194<div class="doc_code">
1195<pre>
1196Function* F = ...;
1197
Bill Wendling82e2eea2006-10-11 18:00:22 +00001198for (Value::use_iterator i = F-&gt;use_begin(), e = F-&gt;use_end(); i != e; ++i)
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001199 if (Instruction *Inst = dyn_cast&lt;Instruction&gt;(*i)) {
Bill Wendling832171c2006-12-07 20:04:42 +00001200 llvm::cerr &lt;&lt; "F is used in instruction:\n";
1201 llvm::cerr &lt;&lt; *Inst &lt;&lt; "\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001202 }
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001203</pre>
1204</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001205
1206<p>Alternately, it's common to have an instance of the <a
Misha Brukman384047f2004-06-03 23:29:12 +00001207href="/doxygen/classllvm_1_1User.html">User Class</a> and need to know what
Misha Brukman13fd15c2004-01-15 00:14:41 +00001208<tt>Value</tt>s are used by it. The list of all <tt>Value</tt>s used by a
1209<tt>User</tt> is known as a <i>use-def</i> chain. Instances of class
1210<tt>Instruction</tt> are common <tt>User</tt>s, so we might want to iterate over
1211all of the values that a particular instruction uses (that is, the operands of
1212the particular <tt>Instruction</tt>):</p>
1213
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001214<div class="doc_code">
1215<pre>
1216Instruction* pi = ...;
1217
1218for (User::op_iterator i = pi-&gt;op_begin(), e = pi-&gt;op_end(); i != e; ++i) {
1219 Value* v = *i;
Bill Wendling82e2eea2006-10-11 18:00:22 +00001220 // <i>...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001221}
1222</pre>
1223</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001224
Chris Lattner1a3105b2002-09-09 05:49:39 +00001225<!--
1226 def-use chains ("finding all users of"): Value::use_begin/use_end
1227 use-def chains ("finding all values used"): User::op_begin/op_end [op=operand]
Misha Brukman13fd15c2004-01-15 00:14:41 +00001228-->
1229
1230</div>
1231
1232<!-- ======================================================================= -->
1233<div class="doc_subsection">
1234 <a name="simplechanges">Making simple changes</a>
1235</div>
1236
1237<div class="doc_text">
1238
1239<p>There are some primitive transformation operations present in the LLVM
Joel Stanley753eb712002-09-11 22:32:24 +00001240infrastructure that are worth knowing about. When performing
Chris Lattner261efe92003-11-25 01:02:51 +00001241transformations, it's fairly common to manipulate the contents of basic
1242blocks. This section describes some of the common methods for doing so
Misha Brukman13fd15c2004-01-15 00:14:41 +00001243and gives example code.</p>
1244
1245</div>
1246
Chris Lattner261efe92003-11-25 01:02:51 +00001247<!--_______________________________________________________________________-->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001248<div class="doc_subsubsection">
1249 <a name="schanges_creating">Creating and inserting new
1250 <tt>Instruction</tt>s</a>
1251</div>
1252
1253<div class="doc_text">
1254
1255<p><i>Instantiating Instructions</i></p>
1256
Chris Lattner69bf8a92004-05-23 21:06:58 +00001257<p>Creation of <tt>Instruction</tt>s is straight-forward: simply call the
Misha Brukman13fd15c2004-01-15 00:14:41 +00001258constructor for the kind of instruction to instantiate and provide the necessary
1259parameters. For example, an <tt>AllocaInst</tt> only <i>requires</i> a
1260(const-ptr-to) <tt>Type</tt>. Thus:</p>
1261
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001262<div class="doc_code">
1263<pre>
1264AllocaInst* ai = new AllocaInst(Type::IntTy);
1265</pre>
1266</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001267
1268<p>will create an <tt>AllocaInst</tt> instance that represents the allocation of
1269one integer in the current stack frame, at runtime. Each <tt>Instruction</tt>
1270subclass is likely to have varying default parameters which change the semantics
1271of the instruction, so refer to the <a
Misha Brukman31ca1de2004-06-03 23:35:54 +00001272href="/doxygen/classllvm_1_1Instruction.html">doxygen documentation for the subclass of
Misha Brukman13fd15c2004-01-15 00:14:41 +00001273Instruction</a> that you're interested in instantiating.</p>
1274
1275<p><i>Naming values</i></p>
1276
1277<p>It is very useful to name the values of instructions when you're able to, as
1278this facilitates the debugging of your transformations. If you end up looking
1279at generated LLVM machine code, you definitely want to have logical names
1280associated with the results of instructions! By supplying a value for the
1281<tt>Name</tt> (default) parameter of the <tt>Instruction</tt> constructor, you
1282associate a logical name with the result of the instruction's execution at
1283runtime. For example, say that I'm writing a transformation that dynamically
1284allocates space for an integer on the stack, and that integer is going to be
1285used as some kind of index by some other code. To accomplish this, I place an
1286<tt>AllocaInst</tt> at the first point in the first <tt>BasicBlock</tt> of some
1287<tt>Function</tt>, and I'm intending to use it within the same
1288<tt>Function</tt>. I might do:</p>
1289
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001290<div class="doc_code">
1291<pre>
1292AllocaInst* pa = new AllocaInst(Type::IntTy, 0, "indexLoc");
1293</pre>
1294</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001295
1296<p>where <tt>indexLoc</tt> is now the logical name of the instruction's
1297execution value, which is a pointer to an integer on the runtime stack.</p>
1298
1299<p><i>Inserting instructions</i></p>
1300
1301<p>There are essentially two ways to insert an <tt>Instruction</tt>
1302into an existing sequence of instructions that form a <tt>BasicBlock</tt>:</p>
1303
Joel Stanley9dd1ad62002-09-18 03:17:23 +00001304<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001305 <li>Insertion into an explicit instruction list
1306
1307 <p>Given a <tt>BasicBlock* pb</tt>, an <tt>Instruction* pi</tt> within that
1308 <tt>BasicBlock</tt>, and a newly-created instruction we wish to insert
1309 before <tt>*pi</tt>, we do the following: </p>
1310
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001311<div class="doc_code">
1312<pre>
1313BasicBlock *pb = ...;
1314Instruction *pi = ...;
1315Instruction *newInst = new Instruction(...);
1316
Bill Wendling82e2eea2006-10-11 18:00:22 +00001317pb-&gt;getInstList().insert(pi, newInst); // <i>Inserts newInst before pi in pb</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001318</pre>
1319</div>
Alkis Evlogimenos9a5dc4f2004-05-27 00:57:51 +00001320
1321 <p>Appending to the end of a <tt>BasicBlock</tt> is so common that
1322 the <tt>Instruction</tt> class and <tt>Instruction</tt>-derived
1323 classes provide constructors which take a pointer to a
1324 <tt>BasicBlock</tt> to be appended to. For example code that
1325 looked like: </p>
1326
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001327<div class="doc_code">
1328<pre>
1329BasicBlock *pb = ...;
1330Instruction *newInst = new Instruction(...);
1331
Bill Wendling82e2eea2006-10-11 18:00:22 +00001332pb-&gt;getInstList().push_back(newInst); // <i>Appends newInst to pb</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001333</pre>
1334</div>
Alkis Evlogimenos9a5dc4f2004-05-27 00:57:51 +00001335
1336 <p>becomes: </p>
1337
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001338<div class="doc_code">
1339<pre>
1340BasicBlock *pb = ...;
1341Instruction *newInst = new Instruction(..., pb);
1342</pre>
1343</div>
Alkis Evlogimenos9a5dc4f2004-05-27 00:57:51 +00001344
1345 <p>which is much cleaner, especially if you are creating
1346 long instruction streams.</p></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001347
1348 <li>Insertion into an implicit instruction list
1349
1350 <p><tt>Instruction</tt> instances that are already in <tt>BasicBlock</tt>s
1351 are implicitly associated with an existing instruction list: the instruction
1352 list of the enclosing basic block. Thus, we could have accomplished the same
1353 thing as the above code without being given a <tt>BasicBlock</tt> by doing:
1354 </p>
1355
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001356<div class="doc_code">
1357<pre>
1358Instruction *pi = ...;
1359Instruction *newInst = new Instruction(...);
1360
1361pi-&gt;getParent()-&gt;getInstList().insert(pi, newInst);
1362</pre>
1363</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001364
1365 <p>In fact, this sequence of steps occurs so frequently that the
1366 <tt>Instruction</tt> class and <tt>Instruction</tt>-derived classes provide
1367 constructors which take (as a default parameter) a pointer to an
1368 <tt>Instruction</tt> which the newly-created <tt>Instruction</tt> should
1369 precede. That is, <tt>Instruction</tt> constructors are capable of
1370 inserting the newly-created instance into the <tt>BasicBlock</tt> of a
1371 provided instruction, immediately before that instruction. Using an
1372 <tt>Instruction</tt> constructor with a <tt>insertBefore</tt> (default)
1373 parameter, the above code becomes:</p>
1374
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001375<div class="doc_code">
1376<pre>
1377Instruction* pi = ...;
1378Instruction* newInst = new Instruction(..., pi);
1379</pre>
1380</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001381
1382 <p>which is much cleaner, especially if you're creating a lot of
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001383 instructions and adding them to <tt>BasicBlock</tt>s.</p></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001384</ul>
1385
1386</div>
1387
1388<!--_______________________________________________________________________-->
1389<div class="doc_subsubsection">
1390 <a name="schanges_deleting">Deleting <tt>Instruction</tt>s</a>
1391</div>
1392
1393<div class="doc_text">
1394
1395<p>Deleting an instruction from an existing sequence of instructions that form a
Chris Lattner69bf8a92004-05-23 21:06:58 +00001396<a href="#BasicBlock"><tt>BasicBlock</tt></a> is very straight-forward. First,
Misha Brukman13fd15c2004-01-15 00:14:41 +00001397you must have a pointer to the instruction that you wish to delete. Second, you
1398need to obtain the pointer to that instruction's basic block. You use the
1399pointer to the basic block to get its list of instructions and then use the
1400erase function to remove your instruction. For example:</p>
1401
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001402<div class="doc_code">
1403<pre>
1404<a href="#Instruction">Instruction</a> *I = .. ;
1405<a href="#BasicBlock">BasicBlock</a> *BB = I-&gt;getParent();
1406
1407BB-&gt;getInstList().erase(I);
1408</pre>
1409</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001410
1411</div>
1412
1413<!--_______________________________________________________________________-->
1414<div class="doc_subsubsection">
1415 <a name="schanges_replacing">Replacing an <tt>Instruction</tt> with another
1416 <tt>Value</tt></a>
1417</div>
1418
1419<div class="doc_text">
1420
1421<p><i>Replacing individual instructions</i></p>
1422
1423<p>Including "<a href="/doxygen/BasicBlockUtils_8h-source.html">llvm/Transforms/Utils/BasicBlockUtils.h</a>"
Chris Lattner261efe92003-11-25 01:02:51 +00001424permits use of two very useful replace functions: <tt>ReplaceInstWithValue</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001425and <tt>ReplaceInstWithInst</tt>.</p>
1426
Chris Lattner261efe92003-11-25 01:02:51 +00001427<h4><a name="schanges_deleting">Deleting <tt>Instruction</tt>s</a></h4>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001428
Chris Lattner261efe92003-11-25 01:02:51 +00001429<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001430 <li><tt>ReplaceInstWithValue</tt>
1431
1432 <p>This function replaces all uses (within a basic block) of a given
1433 instruction with a value, and then removes the original instruction. The
1434 following example illustrates the replacement of the result of a particular
Chris Lattner58360822005-01-17 00:12:04 +00001435 <tt>AllocaInst</tt> that allocates memory for a single integer with a null
Misha Brukman13fd15c2004-01-15 00:14:41 +00001436 pointer to an integer.</p>
1437
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001438<div class="doc_code">
1439<pre>
1440AllocaInst* instToReplace = ...;
1441BasicBlock::iterator ii(instToReplace);
1442
1443ReplaceInstWithValue(instToReplace-&gt;getParent()-&gt;getInstList(), ii,
1444 Constant::getNullValue(PointerType::get(Type::IntTy)));
1445</pre></div></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001446
1447 <li><tt>ReplaceInstWithInst</tt>
1448
1449 <p>This function replaces a particular instruction with another
1450 instruction. The following example illustrates the replacement of one
1451 <tt>AllocaInst</tt> with another.</p>
1452
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001453<div class="doc_code">
1454<pre>
1455AllocaInst* instToReplace = ...;
1456BasicBlock::iterator ii(instToReplace);
1457
1458ReplaceInstWithInst(instToReplace-&gt;getParent()-&gt;getInstList(), ii,
1459 new AllocaInst(Type::IntTy, 0, "ptrToReplacedInt"));
1460</pre></div></li>
Chris Lattner261efe92003-11-25 01:02:51 +00001461</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001462
1463<p><i>Replacing multiple uses of <tt>User</tt>s and <tt>Value</tt>s</i></p>
1464
1465<p>You can use <tt>Value::replaceAllUsesWith</tt> and
1466<tt>User::replaceUsesOfWith</tt> to change more than one use at a time. See the
Chris Lattner00815172007-01-04 22:01:45 +00001467doxygen documentation for the <a href="/doxygen/classllvm_1_1Value.html">Value Class</a>
Misha Brukman384047f2004-06-03 23:29:12 +00001468and <a href="/doxygen/classllvm_1_1User.html">User Class</a>, respectively, for more
Misha Brukman13fd15c2004-01-15 00:14:41 +00001469information.</p>
1470
1471<!-- Value::replaceAllUsesWith User::replaceUsesOfWith Point out:
1472include/llvm/Transforms/Utils/ especially BasicBlockUtils.h with:
1473ReplaceInstWithValue, ReplaceInstWithInst -->
1474
1475</div>
1476
Chris Lattner9355b472002-09-06 02:50:58 +00001477<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001478<div class="doc_section">
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001479 <a name="advanced">Advanced Topics</a>
1480</div>
1481<!-- *********************************************************************** -->
1482
1483<div class="doc_text">
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001484<p>
1485This section describes some of the advanced or obscure API's that most clients
1486do not need to be aware of. These API's tend manage the inner workings of the
1487LLVM system, and only need to be accessed in unusual circumstances.
1488</p>
1489</div>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001490
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001491<!-- ======================================================================= -->
1492<div class="doc_subsection">
1493 <a name="TypeResolve">LLVM Type Resolution</a>
1494</div>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001495
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001496<div class="doc_text">
1497
1498<p>
1499The LLVM type system has a very simple goal: allow clients to compare types for
1500structural equality with a simple pointer comparison (aka a shallow compare).
1501This goal makes clients much simpler and faster, and is used throughout the LLVM
1502system.
1503</p>
1504
1505<p>
1506Unfortunately achieving this goal is not a simple matter. In particular,
1507recursive types and late resolution of opaque types makes the situation very
1508difficult to handle. Fortunately, for the most part, our implementation makes
1509most clients able to be completely unaware of the nasty internal details. The
1510primary case where clients are exposed to the inner workings of it are when
1511building a recursive type. In addition to this case, the LLVM bytecode reader,
1512assembly parser, and linker also have to be aware of the inner workings of this
1513system.
1514</p>
1515
Chris Lattner0f876db2005-04-25 15:47:57 +00001516<p>
1517For our purposes below, we need three concepts. First, an "Opaque Type" is
1518exactly as defined in the <a href="LangRef.html#t_opaque">language
1519reference</a>. Second an "Abstract Type" is any type which includes an
Reid Spencer06565dc2007-01-12 17:11:23 +00001520opaque type as part of its type graph (for example "<tt>{ opaque, i32 }</tt>").
1521Third, a concrete type is a type that is not an abstract type (e.g. "<tt>{ i32,
Chris Lattner0f876db2005-04-25 15:47:57 +00001522float }</tt>").
1523</p>
1524
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001525</div>
1526
1527<!-- ______________________________________________________________________ -->
1528<div class="doc_subsubsection">
1529 <a name="BuildRecType">Basic Recursive Type Construction</a>
1530</div>
1531
1532<div class="doc_text">
1533
1534<p>
1535Because the most common question is "how do I build a recursive type with LLVM",
1536we answer it now and explain it as we go. Here we include enough to cause this
1537to be emitted to an output .ll file:
1538</p>
1539
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001540<div class="doc_code">
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001541<pre>
Reid Spencer06565dc2007-01-12 17:11:23 +00001542%mylist = type { %mylist*, i32 }
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001543</pre>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001544</div>
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001545
1546<p>
1547To build this, use the following LLVM APIs:
1548</p>
1549
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001550<div class="doc_code">
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001551<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001552// <i>Create the initial outer struct</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001553<a href="#PATypeHolder">PATypeHolder</a> StructTy = OpaqueType::get();
1554std::vector&lt;const Type*&gt; Elts;
1555Elts.push_back(PointerType::get(StructTy));
1556Elts.push_back(Type::IntTy);
1557StructType *NewSTy = StructType::get(Elts);
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001558
Reid Spencer06565dc2007-01-12 17:11:23 +00001559// <i>At this point, NewSTy = "{ opaque*, i32 }". Tell VMCore that</i>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001560// <i>the struct and the opaque type are actually the same.</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001561cast&lt;OpaqueType&gt;(StructTy.get())-&gt;<a href="#refineAbstractTypeTo">refineAbstractTypeTo</a>(NewSTy);
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001562
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001563// <i>NewSTy is potentially invalidated, but StructTy (a <a href="#PATypeHolder">PATypeHolder</a>) is</i>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001564// <i>kept up-to-date</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001565NewSTy = cast&lt;StructType&gt;(StructTy.get());
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001566
Bill Wendling82e2eea2006-10-11 18:00:22 +00001567// <i>Add a name for the type to the module symbol table (optional)</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001568MyModule-&gt;addTypeName("mylist", NewSTy);
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001569</pre>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001570</div>
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001571
1572<p>
1573This code shows the basic approach used to build recursive types: build a
1574non-recursive type using 'opaque', then use type unification to close the cycle.
1575The type unification step is performed by the <tt><a
1576ref="#refineAbstractTypeTo">refineAbstractTypeTo</a></tt> method, which is
1577described next. After that, we describe the <a
1578href="#PATypeHolder">PATypeHolder class</a>.
1579</p>
1580
1581</div>
1582
1583<!-- ______________________________________________________________________ -->
1584<div class="doc_subsubsection">
1585 <a name="refineAbstractTypeTo">The <tt>refineAbstractTypeTo</tt> method</a>
1586</div>
1587
1588<div class="doc_text">
1589<p>
1590The <tt>refineAbstractTypeTo</tt> method starts the type unification process.
1591While this method is actually a member of the DerivedType class, it is most
1592often used on OpaqueType instances. Type unification is actually a recursive
1593process. After unification, types can become structurally isomorphic to
1594existing types, and all duplicates are deleted (to preserve pointer equality).
1595</p>
1596
1597<p>
1598In the example above, the OpaqueType object is definitely deleted.
Reid Spencer06565dc2007-01-12 17:11:23 +00001599Additionally, if there is an "{ \2*, i32}" type already created in the system,
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001600the pointer and struct type created are <b>also</b> deleted. Obviously whenever
1601a type is deleted, any "Type*" pointers in the program are invalidated. As
1602such, it is safest to avoid having <i>any</i> "Type*" pointers to abstract types
1603live across a call to <tt>refineAbstractTypeTo</tt> (note that non-abstract
1604types can never move or be deleted). To deal with this, the <a
1605href="#PATypeHolder">PATypeHolder</a> class is used to maintain a stable
1606reference to a possibly refined type, and the <a
1607href="#AbstractTypeUser">AbstractTypeUser</a> class is used to update more
1608complex datastructures.
1609</p>
1610
1611</div>
1612
1613<!-- ______________________________________________________________________ -->
1614<div class="doc_subsubsection">
1615 <a name="PATypeHolder">The PATypeHolder Class</a>
1616</div>
1617
1618<div class="doc_text">
1619<p>
1620PATypeHolder is a form of a "smart pointer" for Type objects. When VMCore
1621happily goes about nuking types that become isomorphic to existing types, it
1622automatically updates all PATypeHolder objects to point to the new type. In the
1623example above, this allows the code to maintain a pointer to the resultant
1624resolved recursive type, even though the Type*'s are potentially invalidated.
1625</p>
1626
1627<p>
1628PATypeHolder is an extremely light-weight object that uses a lazy union-find
1629implementation to update pointers. For example the pointer from a Value to its
1630Type is maintained by PATypeHolder objects.
1631</p>
1632
1633</div>
1634
1635<!-- ______________________________________________________________________ -->
1636<div class="doc_subsubsection">
1637 <a name="AbstractTypeUser">The AbstractTypeUser Class</a>
1638</div>
1639
1640<div class="doc_text">
1641
1642<p>
1643Some data structures need more to perform more complex updates when types get
1644resolved. The <a href="#SymbolTable">SymbolTable</a> class, for example, needs
1645move and potentially merge type planes in its representation when a pointer
1646changes.</p>
1647
1648<p>
1649To support this, a class can derive from the AbstractTypeUser class. This class
1650allows it to get callbacks when certain types are resolved. To register to get
1651callbacks for a particular type, the DerivedType::{add/remove}AbstractTypeUser
Chris Lattner0f876db2005-04-25 15:47:57 +00001652methods can be called on a type. Note that these methods only work for <i>
Reid Spencer06565dc2007-01-12 17:11:23 +00001653 abstract</i> types. Concrete types (those that do not include any opaque
1654objects) can never be refined.
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001655</p>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001656</div>
1657
1658
1659<!-- ======================================================================= -->
1660<div class="doc_subsection">
1661 <a name="SymbolTable">The <tt>SymbolTable</tt> class</a>
1662</div>
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001663
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001664<div class="doc_text">
1665<p>This class provides a symbol table that the <a
1666href="#Function"><tt>Function</tt></a> and <a href="#Module">
1667<tt>Module</tt></a> classes use for naming definitions. The symbol table can
Reid Spencera6362242007-01-07 00:41:39 +00001668provide a name for any <a href="#Value"><tt>Value</tt></a>.
1669<tt>SymbolTable</tt> is an abstract data type. It hides the data it contains
1670and provides access to it through a controlled interface.</p>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001671
Reid Spencera6362242007-01-07 00:41:39 +00001672<p>Note that the <tt>SymbolTable</tt> class should not be directly accessed
1673by most clients. It should only be used when iteration over the symbol table
1674names themselves are required, which is very special purpose. Note that not
1675all LLVM
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001676<a href="#Value">Value</a>s have names, and those without names (i.e. they have
1677an empty name) do not exist in the symbol table.
1678</p>
1679
1680<p>To use the <tt>SymbolTable</tt> well, you need to understand the
1681structure of the information it holds. The class contains two
1682<tt>std::map</tt> objects. The first, <tt>pmap</tt>, is a map of
1683<tt>Type*</tt> to maps of name (<tt>std::string</tt>) to <tt>Value*</tt>.
Reid Spencera6362242007-01-07 00:41:39 +00001684Thus, Values are stored in two-dimensions and accessed by <tt>Type</tt> and
1685name.</p>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001686
1687<p>The interface of this class provides three basic types of operations:
1688<ol>
1689 <li><em>Accessors</em>. Accessors provide read-only access to information
1690 such as finding a value for a name with the
1691 <a href="#SymbolTable_lookup">lookup</a> method.</li>
1692 <li><em>Mutators</em>. Mutators allow the user to add information to the
1693 <tt>SymbolTable</tt> with methods like
1694 <a href="#SymbolTable_insert"><tt>insert</tt></a>.</li>
1695 <li><em>Iterators</em>. Iterators allow the user to traverse the content
1696 of the symbol table in well defined ways, such as the method
Reid Spencera6362242007-01-07 00:41:39 +00001697 <a href="#SymbolTable_plane_begin"><tt>plane_begin</tt></a>.</li>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001698</ol>
1699
1700<h3>Accessors</h3>
1701<dl>
1702 <dt><tt>Value* lookup(const Type* Ty, const std::string&amp; name) const</tt>:
1703 </dt>
1704 <dd>The <tt>lookup</tt> method searches the type plane given by the
1705 <tt>Ty</tt> parameter for a <tt>Value</tt> with the provided <tt>name</tt>.
1706 If a suitable <tt>Value</tt> is not found, null is returned.</dd>
1707
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001708 <dt><tt>bool isEmpty() const</tt>:</dt>
1709 <dd>This function returns true if both the value and types maps are
1710 empty</dd>
1711</dl>
1712
1713<h3>Mutators</h3>
1714<dl>
1715 <dt><tt>void insert(Value *Val)</tt>:</dt>
1716 <dd>This method adds the provided value to the symbol table. The Value must
1717 have both a name and a type which are extracted and used to place the value
1718 in the correct type plane under the value's name.</dd>
1719
1720 <dt><tt>void insert(const std::string&amp; Name, Value *Val)</tt>:</dt>
1721 <dd> Inserts a constant or type into the symbol table with the specified
1722 name. There can be a many to one mapping between names and constants
1723 or types.</dd>
1724
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001725 <dt><tt>void remove(Value* Val)</tt>:</dt>
1726 <dd> This method removes a named value from the symbol table. The
1727 type and name of the Value are extracted from \p N and used to
1728 lookup the Value in the correct type plane. If the Value is
1729 not in the symbol table, this method silently ignores the
1730 request.</dd>
1731
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001732 <dt><tt>Value* remove(const std::string&amp; Name, Value *Val)</tt>:</dt>
1733 <dd> Remove a constant or type with the specified name from the
1734 symbol table.</dd>
1735
Reid Spencera6362242007-01-07 00:41:39 +00001736 <dt><tt>Value *remove(const value_iterator&amp; It)</tt>:</dt>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001737 <dd> Removes a specific value from the symbol table.
1738 Returns the removed value.</dd>
1739
1740 <dt><tt>bool strip()</tt>:</dt>
1741 <dd> This method will strip the symbol table of its names leaving
1742 the type and values. </dd>
1743
1744 <dt><tt>void clear()</tt>:</dt>
1745 <dd>Empty the symbol table completely.</dd>
1746</dl>
1747
1748<h3>Iteration</h3>
1749<p>The following functions describe three types of iterators you can obtain
1750the beginning or end of the sequence for both const and non-const. It is
1751important to keep track of the different kinds of iterators. There are
1752three idioms worth pointing out:</p>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001753
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001754<table>
1755 <tr><th>Units</th><th>Iterator</th><th>Idiom</th></tr>
1756 <tr>
1757 <td align="left">Planes Of name/Value maps</td><td>PI</td>
1758 <td align="left"><pre><tt>
1759for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
1760 PE = ST.plane_end(); PI != PE; ++PI ) {
Bill Wendling82e2eea2006-10-11 18:00:22 +00001761 PI-&gt;first // <i>This is the Type* of the plane</i>
1762 PI-&gt;second // <i>This is the SymbolTable::ValueMap of name/Value pairs</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001763}
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001764 </tt></pre></td>
1765 </tr>
1766 <tr>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001767 <td align="left">name/Value pairs in a plane</td><td>VI</td>
1768 <td align="left"><pre><tt>
1769for (SymbolTable::value_const_iterator VI = ST.value_begin(SomeType),
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001770 VE = ST.value_end(SomeType); VI != VE; ++VI ) {
Bill Wendling82e2eea2006-10-11 18:00:22 +00001771 VI-&gt;first // <i>This is the name of the Value</i>
1772 VI-&gt;second // <i>This is the Value* value associated with the name</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001773}
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001774 </tt></pre></td>
1775 </tr>
1776</table>
1777
1778<p>Using the recommended iterator names and idioms will help you avoid
1779making mistakes. Of particular note, make sure that whenever you use
1780value_begin(SomeType) that you always compare the resulting iterator
1781with value_end(SomeType) not value_end(SomeOtherType) or else you
1782will loop infinitely.</p>
1783
1784<dl>
1785
1786 <dt><tt>plane_iterator plane_begin()</tt>:</dt>
1787 <dd>Get an iterator that starts at the beginning of the type planes.
1788 The iterator will iterate over the Type/ValueMap pairs in the
1789 type planes. </dd>
1790
1791 <dt><tt>plane_const_iterator plane_begin() const</tt>:</dt>
1792 <dd>Get a const_iterator that starts at the beginning of the type
1793 planes. The iterator will iterate over the Type/ValueMap pairs
1794 in the type planes. </dd>
1795
1796 <dt><tt>plane_iterator plane_end()</tt>:</dt>
1797 <dd>Get an iterator at the end of the type planes. This serves as
1798 the marker for end of iteration over the type planes.</dd>
1799
1800 <dt><tt>plane_const_iterator plane_end() const</tt>:</dt>
1801 <dd>Get a const_iterator at the end of the type planes. This serves as
1802 the marker for end of iteration over the type planes.</dd>
1803
1804 <dt><tt>value_iterator value_begin(const Type *Typ)</tt>:</dt>
1805 <dd>Get an iterator that starts at the beginning of a type plane.
1806 The iterator will iterate over the name/value pairs in the type plane.
1807 Note: The type plane must already exist before using this.</dd>
1808
1809 <dt><tt>value_const_iterator value_begin(const Type *Typ) const</tt>:</dt>
1810 <dd>Get a const_iterator that starts at the beginning of a type plane.
1811 The iterator will iterate over the name/value pairs in the type plane.
1812 Note: The type plane must already exist before using this.</dd>
1813
1814 <dt><tt>value_iterator value_end(const Type *Typ)</tt>:</dt>
1815 <dd>Get an iterator to the end of a type plane. This serves as the marker
1816 for end of iteration of the type plane.
1817 Note: The type plane must already exist before using this.</dd>
1818
1819 <dt><tt>value_const_iterator value_end(const Type *Typ) const</tt>:</dt>
1820 <dd>Get a const_iterator to the end of a type plane. This serves as the
1821 marker for end of iteration of the type plane.
1822 Note: the type plane must already exist before using this.</dd>
1823
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001824 <dt><tt>plane_const_iterator find(const Type* Typ ) const</tt>:</dt>
1825 <dd>This method returns a plane_const_iterator for iteration over
1826 the type planes starting at a specific plane, given by \p Ty.</dd>
1827
1828 <dt><tt>plane_iterator find( const Type* Typ </tt>:</dt>
1829 <dd>This method returns a plane_iterator for iteration over the
1830 type planes starting at a specific plane, given by \p Ty.</dd>
1831
1832</dl>
1833</div>
1834
1835
1836
1837<!-- *********************************************************************** -->
1838<div class="doc_section">
Misha Brukman13fd15c2004-01-15 00:14:41 +00001839 <a name="coreclasses">The Core LLVM Class Hierarchy Reference </a>
1840</div>
1841<!-- *********************************************************************** -->
1842
1843<div class="doc_text">
Reid Spencer303c4b42007-01-12 17:26:25 +00001844<p><tt>#include "<a href="/doxygen/Type_8h-source.html">llvm/Type.h</a>"</tt>
1845<br>doxygen info: <a href="/doxygen/classllvm_1_1Type.html">Type Class</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001846
1847<p>The Core LLVM classes are the primary means of representing the program
Chris Lattner261efe92003-11-25 01:02:51 +00001848being inspected or transformed. The core LLVM classes are defined in
1849header files in the <tt>include/llvm/</tt> directory, and implemented in
Misha Brukman13fd15c2004-01-15 00:14:41 +00001850the <tt>lib/VMCore</tt> directory.</p>
1851
1852</div>
1853
1854<!-- ======================================================================= -->
1855<div class="doc_subsection">
Reid Spencer303c4b42007-01-12 17:26:25 +00001856 <a name="Type">The <tt>Type</tt> class and Derived Types</a>
1857</div>
1858
1859<div class="doc_text">
1860
1861 <p><tt>Type</tt> is a superclass of all type classes. Every <tt>Value</tt> has
1862 a <tt>Type</tt>. <tt>Type</tt> cannot be instantiated directly but only
1863 through its subclasses. Certain primitive types (<tt>VoidType</tt>,
1864 <tt>LabelType</tt>, <tt>FloatType</tt> and <tt>DoubleType</tt>) have hidden
1865 subclasses. They are hidden because they offer no useful functionality beyond
1866 what the <tt>Type</tt> class offers except to distinguish themselves from
1867 other subclasses of <tt>Type</tt>.</p>
1868 <p>All other types are subclasses of <tt>DerivedType</tt>. Types can be
1869 named, but this is not a requirement. There exists exactly
1870 one instance of a given shape at any one time. This allows type equality to
1871 be performed with address equality of the Type Instance. That is, given two
1872 <tt>Type*</tt> values, the types are identical if the pointers are identical.
1873 </p>
1874</div>
1875
1876<!-- _______________________________________________________________________ -->
1877<div class="doc_subsubsection">
1878 <a name="m_Value">Important Public Methods</a>
1879</div>
1880
1881<div class="doc_text">
1882
1883<ul>
Chris Lattner8f79df32007-01-15 01:55:32 +00001884 <li><tt>bool isInteger() const</tt>: Returns true for any integer type.</li>
Reid Spencer303c4b42007-01-12 17:26:25 +00001885
1886 <li><tt>bool isFloatingPoint()</tt>: Return true if this is one of the two
1887 floating point types.</li>
1888
1889 <li><tt>bool isAbstract()</tt>: Return true if the type is abstract (contains
1890 an OpaqueType anywhere in its definition).</li>
1891
1892 <li><tt>bool isSized()</tt>: Return true if the type has known size. Things
1893 that don't have a size are abstract types, labels and void.</li>
1894
1895</ul>
1896</div>
1897
1898<!-- _______________________________________________________________________ -->
1899<div class="doc_subsubsection">
1900 <a name="m_Value">Important Derived Types</a>
1901</div>
1902<div class="doc_text">
1903<dl>
1904 <dt><tt>IntegerType</tt></dt>
1905 <dd>Subclass of DerivedType that represents integer types of any bit width.
1906 Any bit width between <tt>IntegerType::MIN_INT_BITS</tt> (1) and
1907 <tt>IntegerType::MAX_INT_BITS</tt> (~8 million) can be represented.
1908 <ul>
1909 <li><tt>static const IntegerType* get(unsigned NumBits)</tt>: get an integer
1910 type of a specific bit width.</li>
1911 <li><tt>unsigned getBitWidth() const</tt>: Get the bit width of an integer
1912 type.</li>
1913 </ul>
1914 </dd>
1915 <dt><tt>SequentialType</tt></dt>
1916 <dd>This is subclassed by ArrayType and PointerType
1917 <ul>
1918 <li><tt>const Type * getElementType() const</tt>: Returns the type of each
1919 of the elements in the sequential type. </li>
1920 </ul>
1921 </dd>
1922 <dt><tt>ArrayType</tt></dt>
1923 <dd>This is a subclass of SequentialType and defines the interface for array
1924 types.
1925 <ul>
1926 <li><tt>unsigned getNumElements() const</tt>: Returns the number of
1927 elements in the array. </li>
1928 </ul>
1929 </dd>
1930 <dt><tt>PointerType</tt></dt>
1931 <dd>Subclass of SequentialType for pointer types.</li>
1932 <dt><tt>PackedType</tt></dt>
1933 <dd>Subclass of SequentialType for packed (vector) types. A
1934 packed type is similar to an ArrayType but is distinguished because it is
1935 a first class type wherease ArrayType is not. Packed types are used for
1936 vector operations and are usually small vectors of of an integer or floating
1937 point type.</dd>
1938 <dt><tt>StructType</tt></dt>
1939 <dd>Subclass of DerivedTypes for struct types.</dd>
1940 <dt><tt>FunctionType</tt></dt>
1941 <dd>Subclass of DerivedTypes for function types.
1942 <ul>
1943 <li><tt>bool isVarArg() const</tt>: Returns true if its a vararg
1944 function</li>
1945 <li><tt> const Type * getReturnType() const</tt>: Returns the
1946 return type of the function.</li>
1947 <li><tt>const Type * getParamType (unsigned i)</tt>: Returns
1948 the type of the ith parameter.</li>
1949 <li><tt> const unsigned getNumParams() const</tt>: Returns the
1950 number of formal parameters.</li>
1951 </ul>
1952 </dd>
1953 <dt><tt>OpaqueType</tt></dt>
1954 <dd>Sublcass of DerivedType for abstract types. This class
1955 defines no content and is used as a placeholder for some other type. Note
1956 that OpaqueType is used (temporarily) during type resolution for forward
1957 references of types. Once the referenced type is resolved, the OpaqueType
1958 is replaced with the actual type. OpaqueType can also be used for data
1959 abstraction. At link time opaque types can be resolved to actual types
1960 of the same name.</dd>
1961</dl>
1962</div>
1963
1964<!-- ======================================================================= -->
1965<div class="doc_subsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00001966 <a name="Value">The <tt>Value</tt> class</a>
1967</div>
1968
1969<div>
1970
1971<p><tt>#include "<a href="/doxygen/Value_8h-source.html">llvm/Value.h</a>"</tt>
1972<br>
Chris Lattner00815172007-01-04 22:01:45 +00001973doxygen info: <a href="/doxygen/classllvm_1_1Value.html">Value Class</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001974
1975<p>The <tt>Value</tt> class is the most important class in the LLVM Source
1976base. It represents a typed value that may be used (among other things) as an
1977operand to an instruction. There are many different types of <tt>Value</tt>s,
1978such as <a href="#Constant"><tt>Constant</tt></a>s,<a
1979href="#Argument"><tt>Argument</tt></a>s. Even <a
1980href="#Instruction"><tt>Instruction</tt></a>s and <a
1981href="#Function"><tt>Function</tt></a>s are <tt>Value</tt>s.</p>
1982
1983<p>A particular <tt>Value</tt> may be used many times in the LLVM representation
1984for a program. For example, an incoming argument to a function (represented
1985with an instance of the <a href="#Argument">Argument</a> class) is "used" by
1986every instruction in the function that references the argument. To keep track
1987of this relationship, the <tt>Value</tt> class keeps a list of all of the <a
1988href="#User"><tt>User</tt></a>s that is using it (the <a
1989href="#User"><tt>User</tt></a> class is a base class for all nodes in the LLVM
1990graph that can refer to <tt>Value</tt>s). This use list is how LLVM represents
1991def-use information in the program, and is accessible through the <tt>use_</tt>*
1992methods, shown below.</p>
1993
1994<p>Because LLVM is a typed representation, every LLVM <tt>Value</tt> is typed,
1995and this <a href="#Type">Type</a> is available through the <tt>getType()</tt>
1996method. In addition, all LLVM values can be named. The "name" of the
1997<tt>Value</tt> is a symbolic string printed in the LLVM code:</p>
1998
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001999<div class="doc_code">
2000<pre>
Reid Spencer06565dc2007-01-12 17:11:23 +00002001%<b>foo</b> = add i32 1, 2
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002002</pre>
2003</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002004
2005<p><a name="#nameWarning">The name of this instruction is "foo".</a> <b>NOTE</b>
2006that the name of any value may be missing (an empty string), so names should
2007<b>ONLY</b> be used for debugging (making the source code easier to read,
2008debugging printouts), they should not be used to keep track of values or map
2009between them. For this purpose, use a <tt>std::map</tt> of pointers to the
2010<tt>Value</tt> itself instead.</p>
2011
2012<p>One important aspect of LLVM is that there is no distinction between an SSA
2013variable and the operation that produces it. Because of this, any reference to
2014the value produced by an instruction (or the value available as an incoming
Chris Lattnerd5fc4fc2004-03-18 14:58:55 +00002015argument, for example) is represented as a direct pointer to the instance of
2016the class that
Misha Brukman13fd15c2004-01-15 00:14:41 +00002017represents this value. Although this may take some getting used to, it
2018simplifies the representation and makes it easier to manipulate.</p>
2019
2020</div>
2021
2022<!-- _______________________________________________________________________ -->
2023<div class="doc_subsubsection">
2024 <a name="m_Value">Important Public Members of the <tt>Value</tt> class</a>
2025</div>
2026
2027<div class="doc_text">
2028
Chris Lattner261efe92003-11-25 01:02:51 +00002029<ul>
2030 <li><tt>Value::use_iterator</tt> - Typedef for iterator over the
2031use-list<br>
2032 <tt>Value::use_const_iterator</tt> - Typedef for const_iterator over
2033the use-list<br>
2034 <tt>unsigned use_size()</tt> - Returns the number of users of the
2035value.<br>
Chris Lattner9355b472002-09-06 02:50:58 +00002036 <tt>bool use_empty()</tt> - Returns true if there are no users.<br>
Chris Lattner261efe92003-11-25 01:02:51 +00002037 <tt>use_iterator use_begin()</tt> - Get an iterator to the start of
2038the use-list.<br>
2039 <tt>use_iterator use_end()</tt> - Get an iterator to the end of the
2040use-list.<br>
2041 <tt><a href="#User">User</a> *use_back()</tt> - Returns the last
2042element in the list.
2043 <p> These methods are the interface to access the def-use
2044information in LLVM. As with all other iterators in LLVM, the naming
2045conventions follow the conventions defined by the <a href="#stl">STL</a>.</p>
Chris Lattner261efe92003-11-25 01:02:51 +00002046 </li>
2047 <li><tt><a href="#Type">Type</a> *getType() const</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002048 <p>This method returns the Type of the Value.</p>
Chris Lattner261efe92003-11-25 01:02:51 +00002049 </li>
2050 <li><tt>bool hasName() const</tt><br>
Chris Lattner9355b472002-09-06 02:50:58 +00002051 <tt>std::string getName() const</tt><br>
Chris Lattner261efe92003-11-25 01:02:51 +00002052 <tt>void setName(const std::string &amp;Name)</tt>
2053 <p> This family of methods is used to access and assign a name to a <tt>Value</tt>,
2054be aware of the <a href="#nameWarning">precaution above</a>.</p>
Chris Lattner261efe92003-11-25 01:02:51 +00002055 </li>
2056 <li><tt>void replaceAllUsesWith(Value *V)</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002057
2058 <p>This method traverses the use list of a <tt>Value</tt> changing all <a
2059 href="#User"><tt>User</tt>s</a> of the current value to refer to
2060 "<tt>V</tt>" instead. For example, if you detect that an instruction always
2061 produces a constant value (for example through constant folding), you can
2062 replace all uses of the instruction with the constant like this:</p>
2063
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002064<div class="doc_code">
2065<pre>
2066Inst-&gt;replaceAllUsesWith(ConstVal);
2067</pre>
2068</div>
2069
Chris Lattner261efe92003-11-25 01:02:51 +00002070</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002071
2072</div>
2073
2074<!-- ======================================================================= -->
2075<div class="doc_subsection">
2076 <a name="User">The <tt>User</tt> class</a>
2077</div>
2078
2079<div class="doc_text">
2080
2081<p>
2082<tt>#include "<a href="/doxygen/User_8h-source.html">llvm/User.h</a>"</tt><br>
Misha Brukman384047f2004-06-03 23:29:12 +00002083doxygen info: <a href="/doxygen/classllvm_1_1User.html">User Class</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002084Superclass: <a href="#Value"><tt>Value</tt></a></p>
2085
2086<p>The <tt>User</tt> class is the common base class of all LLVM nodes that may
2087refer to <a href="#Value"><tt>Value</tt></a>s. It exposes a list of "Operands"
2088that are all of the <a href="#Value"><tt>Value</tt></a>s that the User is
2089referring to. The <tt>User</tt> class itself is a subclass of
2090<tt>Value</tt>.</p>
2091
2092<p>The operands of a <tt>User</tt> point directly to the LLVM <a
2093href="#Value"><tt>Value</tt></a> that it refers to. Because LLVM uses Static
2094Single Assignment (SSA) form, there can only be one definition referred to,
2095allowing this direct connection. This connection provides the use-def
2096information in LLVM.</p>
2097
2098</div>
2099
2100<!-- _______________________________________________________________________ -->
2101<div class="doc_subsubsection">
2102 <a name="m_User">Important Public Members of the <tt>User</tt> class</a>
2103</div>
2104
2105<div class="doc_text">
2106
2107<p>The <tt>User</tt> class exposes the operand list in two ways: through
2108an index access interface and through an iterator based interface.</p>
2109
Chris Lattner261efe92003-11-25 01:02:51 +00002110<ul>
Chris Lattner261efe92003-11-25 01:02:51 +00002111 <li><tt>Value *getOperand(unsigned i)</tt><br>
2112 <tt>unsigned getNumOperands()</tt>
2113 <p> These two methods expose the operands of the <tt>User</tt> in a
Misha Brukman13fd15c2004-01-15 00:14:41 +00002114convenient form for direct access.</p></li>
2115
Chris Lattner261efe92003-11-25 01:02:51 +00002116 <li><tt>User::op_iterator</tt> - Typedef for iterator over the operand
2117list<br>
Chris Lattner58360822005-01-17 00:12:04 +00002118 <tt>op_iterator op_begin()</tt> - Get an iterator to the start of
2119the operand list.<br>
2120 <tt>op_iterator op_end()</tt> - Get an iterator to the end of the
Chris Lattner261efe92003-11-25 01:02:51 +00002121operand list.
2122 <p> Together, these methods make up the iterator based interface to
Misha Brukman13fd15c2004-01-15 00:14:41 +00002123the operands of a <tt>User</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002124</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002125
2126</div>
2127
2128<!-- ======================================================================= -->
2129<div class="doc_subsection">
2130 <a name="Instruction">The <tt>Instruction</tt> class</a>
2131</div>
2132
2133<div class="doc_text">
2134
2135<p><tt>#include "</tt><tt><a
2136href="/doxygen/Instruction_8h-source.html">llvm/Instruction.h</a>"</tt><br>
Misha Brukman31ca1de2004-06-03 23:35:54 +00002137doxygen info: <a href="/doxygen/classllvm_1_1Instruction.html">Instruction Class</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002138Superclasses: <a href="#User"><tt>User</tt></a>, <a
2139href="#Value"><tt>Value</tt></a></p>
2140
2141<p>The <tt>Instruction</tt> class is the common base class for all LLVM
2142instructions. It provides only a few methods, but is a very commonly used
2143class. The primary data tracked by the <tt>Instruction</tt> class itself is the
2144opcode (instruction type) and the parent <a
2145href="#BasicBlock"><tt>BasicBlock</tt></a> the <tt>Instruction</tt> is embedded
2146into. To represent a specific type of instruction, one of many subclasses of
2147<tt>Instruction</tt> are used.</p>
2148
2149<p> Because the <tt>Instruction</tt> class subclasses the <a
2150href="#User"><tt>User</tt></a> class, its operands can be accessed in the same
2151way as for other <a href="#User"><tt>User</tt></a>s (with the
2152<tt>getOperand()</tt>/<tt>getNumOperands()</tt> and
2153<tt>op_begin()</tt>/<tt>op_end()</tt> methods).</p> <p> An important file for
2154the <tt>Instruction</tt> class is the <tt>llvm/Instruction.def</tt> file. This
2155file contains some meta-data about the various different types of instructions
2156in LLVM. It describes the enum values that are used as opcodes (for example
Reid Spencerc92d25d2006-12-19 19:47:19 +00002157<tt>Instruction::Add</tt> and <tt>Instruction::ICmp</tt>), as well as the
Misha Brukman13fd15c2004-01-15 00:14:41 +00002158concrete sub-classes of <tt>Instruction</tt> that implement the instruction (for
2159example <tt><a href="#BinaryOperator">BinaryOperator</a></tt> and <tt><a
Reid Spencerc92d25d2006-12-19 19:47:19 +00002160href="#CmpInst">CmpInst</a></tt>). Unfortunately, the use of macros in
Misha Brukman13fd15c2004-01-15 00:14:41 +00002161this file confuses doxygen, so these enum values don't show up correctly in the
Misha Brukman31ca1de2004-06-03 23:35:54 +00002162<a href="/doxygen/classllvm_1_1Instruction.html">doxygen output</a>.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002163
2164</div>
2165
2166<!-- _______________________________________________________________________ -->
2167<div class="doc_subsubsection">
Reid Spencerc92d25d2006-12-19 19:47:19 +00002168 <a name="s_Instruction">Important Subclasses of the <tt>Instruction</tt>
2169 class</a>
2170</div>
2171<div class="doc_text">
2172 <ul>
2173 <li><tt><a name="BinaryOperator">BinaryOperator</a></tt>
2174 <p>This subclasses represents all two operand instructions whose operands
2175 must be the same type, except for the comparison instructions.</p></li>
2176 <li><tt><a name="CastInst">CastInst</a></tt>
2177 <p>This subclass is the parent of the 12 casting instructions. It provides
2178 common operations on cast instructions.</p>
2179 <li><tt><a name="CmpInst">CmpInst</a></tt>
2180 <p>This subclass respresents the two comparison instructions,
2181 <a href="LangRef.html#i_icmp">ICmpInst</a> (integer opreands), and
2182 <a href="LangRef.html#i_fcmp">FCmpInst</a> (floating point operands).</p>
2183 <li><tt><a name="TerminatorInst">TerminatorInst</a></tt>
2184 <p>This subclass is the parent of all terminator instructions (those which
2185 can terminate a block).</p>
2186 </ul>
2187 </div>
2188
2189<!-- _______________________________________________________________________ -->
2190<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00002191 <a name="m_Instruction">Important Public Members of the <tt>Instruction</tt>
2192 class</a>
2193</div>
2194
2195<div class="doc_text">
2196
Chris Lattner261efe92003-11-25 01:02:51 +00002197<ul>
2198 <li><tt><a href="#BasicBlock">BasicBlock</a> *getParent()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002199 <p>Returns the <a href="#BasicBlock"><tt>BasicBlock</tt></a> that
2200this <tt>Instruction</tt> is embedded into.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002201 <li><tt>bool mayWriteToMemory()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002202 <p>Returns true if the instruction writes to memory, i.e. it is a
2203 <tt>call</tt>,<tt>free</tt>,<tt>invoke</tt>, or <tt>store</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002204 <li><tt>unsigned getOpcode()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002205 <p>Returns the opcode for the <tt>Instruction</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002206 <li><tt><a href="#Instruction">Instruction</a> *clone() const</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002207 <p>Returns another instance of the specified instruction, identical
Chris Lattner261efe92003-11-25 01:02:51 +00002208in all ways to the original except that the instruction has no parent
2209(ie it's not embedded into a <a href="#BasicBlock"><tt>BasicBlock</tt></a>),
Misha Brukman13fd15c2004-01-15 00:14:41 +00002210and it has no name</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002211</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002212
2213</div>
2214
2215<!-- ======================================================================= -->
2216<div class="doc_subsection">
2217 <a name="BasicBlock">The <tt>BasicBlock</tt> class</a>
2218</div>
2219
2220<div class="doc_text">
2221
Misha Brukman384047f2004-06-03 23:29:12 +00002222<p><tt>#include "<a
2223href="/doxygen/BasicBlock_8h-source.html">llvm/BasicBlock.h</a>"</tt><br>
2224doxygen info: <a href="/doxygen/structllvm_1_1BasicBlock.html">BasicBlock
2225Class</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002226Superclass: <a href="#Value"><tt>Value</tt></a></p>
2227
2228<p>This class represents a single entry multiple exit section of the code,
2229commonly known as a basic block by the compiler community. The
2230<tt>BasicBlock</tt> class maintains a list of <a
2231href="#Instruction"><tt>Instruction</tt></a>s, which form the body of the block.
2232Matching the language definition, the last element of this list of instructions
2233is always a terminator instruction (a subclass of the <a
2234href="#TerminatorInst"><tt>TerminatorInst</tt></a> class).</p>
2235
2236<p>In addition to tracking the list of instructions that make up the block, the
2237<tt>BasicBlock</tt> class also keeps track of the <a
2238href="#Function"><tt>Function</tt></a> that it is embedded into.</p>
2239
2240<p>Note that <tt>BasicBlock</tt>s themselves are <a
2241href="#Value"><tt>Value</tt></a>s, because they are referenced by instructions
2242like branches and can go in the switch tables. <tt>BasicBlock</tt>s have type
2243<tt>label</tt>.</p>
2244
2245</div>
2246
2247<!-- _______________________________________________________________________ -->
2248<div class="doc_subsubsection">
2249 <a name="m_BasicBlock">Important Public Members of the <tt>BasicBlock</tt>
2250 class</a>
2251</div>
2252
2253<div class="doc_text">
2254
Chris Lattner261efe92003-11-25 01:02:51 +00002255<ul>
Misha Brukmanb0e7e452004-10-29 04:33:19 +00002256
2257<li><tt>BasicBlock(const std::string &amp;Name = "", </tt><tt><a
Chris Lattner261efe92003-11-25 01:02:51 +00002258 href="#Function">Function</a> *Parent = 0)</tt>
Misha Brukmanb0e7e452004-10-29 04:33:19 +00002259
2260<p>The <tt>BasicBlock</tt> constructor is used to create new basic blocks for
2261insertion into a function. The constructor optionally takes a name for the new
2262block, and a <a href="#Function"><tt>Function</tt></a> to insert it into. If
2263the <tt>Parent</tt> parameter is specified, the new <tt>BasicBlock</tt> is
2264automatically inserted at the end of the specified <a
2265href="#Function"><tt>Function</tt></a>, if not specified, the BasicBlock must be
2266manually inserted into the <a href="#Function"><tt>Function</tt></a>.</p></li>
2267
2268<li><tt>BasicBlock::iterator</tt> - Typedef for instruction list iterator<br>
2269<tt>BasicBlock::const_iterator</tt> - Typedef for const_iterator.<br>
2270<tt>begin()</tt>, <tt>end()</tt>, <tt>front()</tt>, <tt>back()</tt>,
Chris Lattner77d69242005-03-15 05:19:20 +00002271<tt>size()</tt>, <tt>empty()</tt>
Misha Brukmanb0e7e452004-10-29 04:33:19 +00002272STL-style functions for accessing the instruction list.
2273
2274<p>These methods and typedefs are forwarding functions that have the same
2275semantics as the standard library methods of the same names. These methods
2276expose the underlying instruction list of a basic block in a way that is easy to
2277manipulate. To get the full complement of container operations (including
2278operations to update the list), you must use the <tt>getInstList()</tt>
2279method.</p></li>
2280
2281<li><tt>BasicBlock::InstListType &amp;getInstList()</tt>
2282
2283<p>This method is used to get access to the underlying container that actually
2284holds the Instructions. This method must be used when there isn't a forwarding
2285function in the <tt>BasicBlock</tt> class for the operation that you would like
2286to perform. Because there are no forwarding functions for "updating"
2287operations, you need to use this if you want to update the contents of a
2288<tt>BasicBlock</tt>.</p></li>
2289
2290<li><tt><a href="#Function">Function</a> *getParent()</tt>
2291
2292<p> Returns a pointer to <a href="#Function"><tt>Function</tt></a> the block is
2293embedded into, or a null pointer if it is homeless.</p></li>
2294
2295<li><tt><a href="#TerminatorInst">TerminatorInst</a> *getTerminator()</tt>
2296
2297<p> Returns a pointer to the terminator instruction that appears at the end of
2298the <tt>BasicBlock</tt>. If there is no terminator instruction, or if the last
2299instruction in the block is not a terminator, then a null pointer is
2300returned.</p></li>
2301
Chris Lattner261efe92003-11-25 01:02:51 +00002302</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002303
2304</div>
2305
2306<!-- ======================================================================= -->
2307<div class="doc_subsection">
2308 <a name="GlobalValue">The <tt>GlobalValue</tt> class</a>
2309</div>
2310
2311<div class="doc_text">
2312
2313<p><tt>#include "<a
2314href="/doxygen/GlobalValue_8h-source.html">llvm/GlobalValue.h</a>"</tt><br>
Misha Brukman384047f2004-06-03 23:29:12 +00002315doxygen info: <a href="/doxygen/classllvm_1_1GlobalValue.html">GlobalValue
2316Class</a><br>
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002317Superclasses: <a href="#Constant"><tt>Constant</tt></a>,
2318<a href="#User"><tt>User</tt></a>, <a href="#Value"><tt>Value</tt></a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002319
2320<p>Global values (<a href="#GlobalVariable"><tt>GlobalVariable</tt></a>s or <a
2321href="#Function"><tt>Function</tt></a>s) are the only LLVM values that are
2322visible in the bodies of all <a href="#Function"><tt>Function</tt></a>s.
2323Because they are visible at global scope, they are also subject to linking with
2324other globals defined in different translation units. To control the linking
2325process, <tt>GlobalValue</tt>s know their linkage rules. Specifically,
2326<tt>GlobalValue</tt>s know whether they have internal or external linkage, as
Reid Spencer8b2da7a2004-07-18 13:10:31 +00002327defined by the <tt>LinkageTypes</tt> enumeration.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002328
2329<p>If a <tt>GlobalValue</tt> has internal linkage (equivalent to being
2330<tt>static</tt> in C), it is not visible to code outside the current translation
2331unit, and does not participate in linking. If it has external linkage, it is
2332visible to external code, and does participate in linking. In addition to
2333linkage information, <tt>GlobalValue</tt>s keep track of which <a
2334href="#Module"><tt>Module</tt></a> they are currently part of.</p>
2335
2336<p>Because <tt>GlobalValue</tt>s are memory objects, they are always referred to
2337by their <b>address</b>. As such, the <a href="#Type"><tt>Type</tt></a> of a
2338global is always a pointer to its contents. It is important to remember this
2339when using the <tt>GetElementPtrInst</tt> instruction because this pointer must
2340be dereferenced first. For example, if you have a <tt>GlobalVariable</tt> (a
2341subclass of <tt>GlobalValue)</tt> that is an array of 24 ints, type <tt>[24 x
Reid Spencer06565dc2007-01-12 17:11:23 +00002342i32]</tt>, then the <tt>GlobalVariable</tt> is a pointer to that array. Although
Misha Brukman13fd15c2004-01-15 00:14:41 +00002343the address of the first element of this array and the value of the
2344<tt>GlobalVariable</tt> are the same, they have different types. The
Reid Spencer06565dc2007-01-12 17:11:23 +00002345<tt>GlobalVariable</tt>'s type is <tt>[24 x i32]</tt>. The first element's type
2346is <tt>i32.</tt> Because of this, accessing a global value requires you to
Misha Brukman13fd15c2004-01-15 00:14:41 +00002347dereference the pointer with <tt>GetElementPtrInst</tt> first, then its elements
2348can be accessed. This is explained in the <a href="LangRef.html#globalvars">LLVM
2349Language Reference Manual</a>.</p>
2350
2351</div>
2352
2353<!-- _______________________________________________________________________ -->
2354<div class="doc_subsubsection">
2355 <a name="m_GlobalValue">Important Public Members of the <tt>GlobalValue</tt>
2356 class</a>
2357</div>
2358
2359<div class="doc_text">
2360
Chris Lattner261efe92003-11-25 01:02:51 +00002361<ul>
2362 <li><tt>bool hasInternalLinkage() const</tt><br>
Chris Lattner9355b472002-09-06 02:50:58 +00002363 <tt>bool hasExternalLinkage() const</tt><br>
Chris Lattner261efe92003-11-25 01:02:51 +00002364 <tt>void setInternalLinkage(bool HasInternalLinkage)</tt>
2365 <p> These methods manipulate the linkage characteristics of the <tt>GlobalValue</tt>.</p>
2366 <p> </p>
2367 </li>
2368 <li><tt><a href="#Module">Module</a> *getParent()</tt>
2369 <p> This returns the <a href="#Module"><tt>Module</tt></a> that the
Misha Brukman13fd15c2004-01-15 00:14:41 +00002370GlobalValue is currently embedded into.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002371</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002372
2373</div>
2374
2375<!-- ======================================================================= -->
2376<div class="doc_subsection">
2377 <a name="Function">The <tt>Function</tt> class</a>
2378</div>
2379
2380<div class="doc_text">
2381
2382<p><tt>#include "<a
2383href="/doxygen/Function_8h-source.html">llvm/Function.h</a>"</tt><br> doxygen
Misha Brukman31ca1de2004-06-03 23:35:54 +00002384info: <a href="/doxygen/classllvm_1_1Function.html">Function Class</a><br>
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002385Superclasses: <a href="#GlobalValue"><tt>GlobalValue</tt></a>,
2386<a href="#Constant"><tt>Constant</tt></a>,
2387<a href="#User"><tt>User</tt></a>,
2388<a href="#Value"><tt>Value</tt></a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002389
2390<p>The <tt>Function</tt> class represents a single procedure in LLVM. It is
2391actually one of the more complex classes in the LLVM heirarchy because it must
2392keep track of a large amount of data. The <tt>Function</tt> class keeps track
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002393of a list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s, a list of formal
2394<a href="#Argument"><tt>Argument</tt></a>s, and a
2395<a href="#SymbolTable"><tt>SymbolTable</tt></a>.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002396
2397<p>The list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s is the most
2398commonly used part of <tt>Function</tt> objects. The list imposes an implicit
2399ordering of the blocks in the function, which indicate how the code will be
2400layed out by the backend. Additionally, the first <a
2401href="#BasicBlock"><tt>BasicBlock</tt></a> is the implicit entry node for the
2402<tt>Function</tt>. It is not legal in LLVM to explicitly branch to this initial
2403block. There are no implicit exit nodes, and in fact there may be multiple exit
2404nodes from a single <tt>Function</tt>. If the <a
2405href="#BasicBlock"><tt>BasicBlock</tt></a> list is empty, this indicates that
2406the <tt>Function</tt> is actually a function declaration: the actual body of the
2407function hasn't been linked in yet.</p>
2408
2409<p>In addition to a list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s, the
2410<tt>Function</tt> class also keeps track of the list of formal <a
2411href="#Argument"><tt>Argument</tt></a>s that the function receives. This
2412container manages the lifetime of the <a href="#Argument"><tt>Argument</tt></a>
2413nodes, just like the <a href="#BasicBlock"><tt>BasicBlock</tt></a> list does for
2414the <a href="#BasicBlock"><tt>BasicBlock</tt></a>s.</p>
2415
2416<p>The <a href="#SymbolTable"><tt>SymbolTable</tt></a> is a very rarely used
2417LLVM feature that is only used when you have to look up a value by name. Aside
2418from that, the <a href="#SymbolTable"><tt>SymbolTable</tt></a> is used
2419internally to make sure that there are not conflicts between the names of <a
2420href="#Instruction"><tt>Instruction</tt></a>s, <a
2421href="#BasicBlock"><tt>BasicBlock</tt></a>s, or <a
2422href="#Argument"><tt>Argument</tt></a>s in the function body.</p>
2423
Reid Spencer8b2da7a2004-07-18 13:10:31 +00002424<p>Note that <tt>Function</tt> is a <a href="#GlobalValue">GlobalValue</a>
2425and therefore also a <a href="#Constant">Constant</a>. The value of the function
2426is its address (after linking) which is guaranteed to be constant.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002427</div>
2428
2429<!-- _______________________________________________________________________ -->
2430<div class="doc_subsubsection">
2431 <a name="m_Function">Important Public Members of the <tt>Function</tt>
2432 class</a>
2433</div>
2434
2435<div class="doc_text">
2436
Chris Lattner261efe92003-11-25 01:02:51 +00002437<ul>
2438 <li><tt>Function(const </tt><tt><a href="#FunctionType">FunctionType</a>
Chris Lattnerac479e52004-08-04 05:10:48 +00002439 *Ty, LinkageTypes Linkage, const std::string &amp;N = "", Module* Parent = 0)</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002440
2441 <p>Constructor used when you need to create new <tt>Function</tt>s to add
2442 the the program. The constructor must specify the type of the function to
Chris Lattnerac479e52004-08-04 05:10:48 +00002443 create and what type of linkage the function should have. The <a
2444 href="#FunctionType"><tt>FunctionType</tt></a> argument
Misha Brukman13fd15c2004-01-15 00:14:41 +00002445 specifies the formal arguments and return value for the function. The same
2446 <a href="#FunctionTypel"><tt>FunctionType</tt></a> value can be used to
2447 create multiple functions. The <tt>Parent</tt> argument specifies the Module
2448 in which the function is defined. If this argument is provided, the function
2449 will automatically be inserted into that module's list of
2450 functions.</p></li>
2451
Chris Lattner261efe92003-11-25 01:02:51 +00002452 <li><tt>bool isExternal()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002453
2454 <p>Return whether or not the <tt>Function</tt> has a body defined. If the
2455 function is "external", it does not have a body, and thus must be resolved
2456 by linking with a function defined in a different translation unit.</p></li>
2457
Chris Lattner261efe92003-11-25 01:02:51 +00002458 <li><tt>Function::iterator</tt> - Typedef for basic block list iterator<br>
Chris Lattner9355b472002-09-06 02:50:58 +00002459 <tt>Function::const_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002460
Chris Lattner77d69242005-03-15 05:19:20 +00002461 <tt>begin()</tt>, <tt>end()</tt>
2462 <tt>size()</tt>, <tt>empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002463
2464 <p>These are forwarding methods that make it easy to access the contents of
2465 a <tt>Function</tt> object's <a href="#BasicBlock"><tt>BasicBlock</tt></a>
2466 list.</p></li>
2467
Chris Lattner261efe92003-11-25 01:02:51 +00002468 <li><tt>Function::BasicBlockListType &amp;getBasicBlockList()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002469
2470 <p>Returns the list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s. This
2471 is necessary to use when you need to update the list or perform a complex
2472 action that doesn't have a forwarding method.</p></li>
2473
Chris Lattner89cc2652005-03-15 04:48:32 +00002474 <li><tt>Function::arg_iterator</tt> - Typedef for the argument list
Chris Lattner261efe92003-11-25 01:02:51 +00002475iterator<br>
Chris Lattner89cc2652005-03-15 04:48:32 +00002476 <tt>Function::const_arg_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002477
Chris Lattner77d69242005-03-15 05:19:20 +00002478 <tt>arg_begin()</tt>, <tt>arg_end()</tt>
Chris Lattner89cc2652005-03-15 04:48:32 +00002479 <tt>arg_size()</tt>, <tt>arg_empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002480
2481 <p>These are forwarding methods that make it easy to access the contents of
2482 a <tt>Function</tt> object's <a href="#Argument"><tt>Argument</tt></a>
2483 list.</p></li>
2484
Chris Lattner261efe92003-11-25 01:02:51 +00002485 <li><tt>Function::ArgumentListType &amp;getArgumentList()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002486
2487 <p>Returns the list of <a href="#Argument"><tt>Argument</tt></a>s. This is
2488 necessary to use when you need to update the list or perform a complex
2489 action that doesn't have a forwarding method.</p></li>
2490
Chris Lattner261efe92003-11-25 01:02:51 +00002491 <li><tt><a href="#BasicBlock">BasicBlock</a> &amp;getEntryBlock()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002492
2493 <p>Returns the entry <a href="#BasicBlock"><tt>BasicBlock</tt></a> for the
2494 function. Because the entry block for the function is always the first
2495 block, this returns the first block of the <tt>Function</tt>.</p></li>
2496
Chris Lattner261efe92003-11-25 01:02:51 +00002497 <li><tt><a href="#Type">Type</a> *getReturnType()</tt><br>
2498 <tt><a href="#FunctionType">FunctionType</a> *getFunctionType()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002499
2500 <p>This traverses the <a href="#Type"><tt>Type</tt></a> of the
2501 <tt>Function</tt> and returns the return type of the function, or the <a
2502 href="#FunctionType"><tt>FunctionType</tt></a> of the actual
2503 function.</p></li>
2504
Chris Lattner261efe92003-11-25 01:02:51 +00002505 <li><tt><a href="#SymbolTable">SymbolTable</a> *getSymbolTable()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002506
Chris Lattner261efe92003-11-25 01:02:51 +00002507 <p> Return a pointer to the <a href="#SymbolTable"><tt>SymbolTable</tt></a>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002508 for this <tt>Function</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002509</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002510
2511</div>
2512
2513<!-- ======================================================================= -->
2514<div class="doc_subsection">
2515 <a name="GlobalVariable">The <tt>GlobalVariable</tt> class</a>
2516</div>
2517
2518<div class="doc_text">
2519
2520<p><tt>#include "<a
2521href="/doxygen/GlobalVariable_8h-source.html">llvm/GlobalVariable.h</a>"</tt>
2522<br>
Tanya Lattnera3da7772004-06-22 08:02:25 +00002523doxygen info: <a href="/doxygen/classllvm_1_1GlobalVariable.html">GlobalVariable
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002524 Class</a><br>
2525Superclasses: <a href="#GlobalValue"><tt>GlobalValue</tt></a>,
2526<a href="#Constant"><tt>Constant</tt></a>,
2527<a href="#User"><tt>User</tt></a>,
2528<a href="#Value"><tt>Value</tt></a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002529
2530<p>Global variables are represented with the (suprise suprise)
2531<tt>GlobalVariable</tt> class. Like functions, <tt>GlobalVariable</tt>s are also
2532subclasses of <a href="#GlobalValue"><tt>GlobalValue</tt></a>, and as such are
2533always referenced by their address (global values must live in memory, so their
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002534"name" refers to their constant address). See
2535<a href="#GlobalValue"><tt>GlobalValue</tt></a> for more on this. Global
2536variables may have an initial value (which must be a
2537<a href="#Constant"><tt>Constant</tt></a>), and if they have an initializer,
2538they may be marked as "constant" themselves (indicating that their contents
2539never change at runtime).</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002540</div>
2541
2542<!-- _______________________________________________________________________ -->
2543<div class="doc_subsubsection">
2544 <a name="m_GlobalVariable">Important Public Members of the
2545 <tt>GlobalVariable</tt> class</a>
2546</div>
2547
2548<div class="doc_text">
2549
Chris Lattner261efe92003-11-25 01:02:51 +00002550<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002551 <li><tt>GlobalVariable(const </tt><tt><a href="#Type">Type</a> *Ty, bool
2552 isConstant, LinkageTypes&amp; Linkage, <a href="#Constant">Constant</a>
2553 *Initializer = 0, const std::string &amp;Name = "", Module* Parent = 0)</tt>
2554
2555 <p>Create a new global variable of the specified type. If
2556 <tt>isConstant</tt> is true then the global variable will be marked as
2557 unchanging for the program. The Linkage parameter specifies the type of
2558 linkage (internal, external, weak, linkonce, appending) for the variable. If
2559 the linkage is InternalLinkage, WeakLinkage, or LinkOnceLinkage,&nbsp; then
2560 the resultant global variable will have internal linkage. AppendingLinkage
2561 concatenates together all instances (in different translation units) of the
2562 variable into a single variable but is only applicable to arrays. &nbsp;See
2563 the <a href="LangRef.html#modulestructure">LLVM Language Reference</a> for
2564 further details on linkage types. Optionally an initializer, a name, and the
2565 module to put the variable into may be specified for the global variable as
2566 well.</p></li>
2567
Chris Lattner261efe92003-11-25 01:02:51 +00002568 <li><tt>bool isConstant() const</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002569
2570 <p>Returns true if this is a global variable that is known not to
2571 be modified at runtime.</p></li>
2572
Chris Lattner261efe92003-11-25 01:02:51 +00002573 <li><tt>bool hasInitializer()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002574
2575 <p>Returns true if this <tt>GlobalVariable</tt> has an intializer.</p></li>
2576
Chris Lattner261efe92003-11-25 01:02:51 +00002577 <li><tt><a href="#Constant">Constant</a> *getInitializer()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002578
2579 <p>Returns the intial value for a <tt>GlobalVariable</tt>. It is not legal
2580 to call this method if there is no initializer.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002581</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002582
2583</div>
2584
2585<!-- ======================================================================= -->
2586<div class="doc_subsection">
2587 <a name="Module">The <tt>Module</tt> class</a>
2588</div>
2589
2590<div class="doc_text">
2591
2592<p><tt>#include "<a
2593href="/doxygen/Module_8h-source.html">llvm/Module.h</a>"</tt><br> doxygen info:
Tanya Lattnera3da7772004-06-22 08:02:25 +00002594<a href="/doxygen/classllvm_1_1Module.html">Module Class</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002595
2596<p>The <tt>Module</tt> class represents the top level structure present in LLVM
2597programs. An LLVM module is effectively either a translation unit of the
2598original program or a combination of several translation units merged by the
2599linker. The <tt>Module</tt> class keeps track of a list of <a
2600href="#Function"><tt>Function</tt></a>s, a list of <a
2601href="#GlobalVariable"><tt>GlobalVariable</tt></a>s, and a <a
2602href="#SymbolTable"><tt>SymbolTable</tt></a>. Additionally, it contains a few
2603helpful member functions that try to make common operations easy.</p>
2604
2605</div>
2606
2607<!-- _______________________________________________________________________ -->
2608<div class="doc_subsubsection">
2609 <a name="m_Module">Important Public Members of the <tt>Module</tt> class</a>
2610</div>
2611
2612<div class="doc_text">
2613
Chris Lattner261efe92003-11-25 01:02:51 +00002614<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002615 <li><tt>Module::Module(std::string name = "")</tt></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002616</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002617
2618<p>Constructing a <a href="#Module">Module</a> is easy. You can optionally
2619provide a name for it (probably based on the name of the translation unit).</p>
2620
Chris Lattner261efe92003-11-25 01:02:51 +00002621<ul>
2622 <li><tt>Module::iterator</tt> - Typedef for function list iterator<br>
Chris Lattner0377de42002-09-06 14:50:55 +00002623 <tt>Module::const_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002624
Chris Lattner77d69242005-03-15 05:19:20 +00002625 <tt>begin()</tt>, <tt>end()</tt>
2626 <tt>size()</tt>, <tt>empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002627
2628 <p>These are forwarding methods that make it easy to access the contents of
2629 a <tt>Module</tt> object's <a href="#Function"><tt>Function</tt></a>
2630 list.</p></li>
2631
Chris Lattner261efe92003-11-25 01:02:51 +00002632 <li><tt>Module::FunctionListType &amp;getFunctionList()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002633
2634 <p> Returns the list of <a href="#Function"><tt>Function</tt></a>s. This is
2635 necessary to use when you need to update the list or perform a complex
2636 action that doesn't have a forwarding method.</p>
2637
2638 <p><!-- Global Variable --></p></li>
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00002639</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002640
2641<hr>
2642
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00002643<ul>
Chris Lattner89cc2652005-03-15 04:48:32 +00002644 <li><tt>Module::global_iterator</tt> - Typedef for global variable list iterator<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002645
Chris Lattner89cc2652005-03-15 04:48:32 +00002646 <tt>Module::const_global_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002647
Chris Lattner77d69242005-03-15 05:19:20 +00002648 <tt>global_begin()</tt>, <tt>global_end()</tt>
Chris Lattner89cc2652005-03-15 04:48:32 +00002649 <tt>global_size()</tt>, <tt>global_empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002650
2651 <p> These are forwarding methods that make it easy to access the contents of
2652 a <tt>Module</tt> object's <a
2653 href="#GlobalVariable"><tt>GlobalVariable</tt></a> list.</p></li>
2654
2655 <li><tt>Module::GlobalListType &amp;getGlobalList()</tt>
2656
2657 <p>Returns the list of <a
2658 href="#GlobalVariable"><tt>GlobalVariable</tt></a>s. This is necessary to
2659 use when you need to update the list or perform a complex action that
2660 doesn't have a forwarding method.</p>
2661
2662 <p><!-- Symbol table stuff --> </p></li>
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00002663</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002664
2665<hr>
2666
2667<ul>
2668 <li><tt><a href="#SymbolTable">SymbolTable</a> *getSymbolTable()</tt>
2669
2670 <p>Return a reference to the <a href="#SymbolTable"><tt>SymbolTable</tt></a>
2671 for this <tt>Module</tt>.</p>
2672
2673 <p><!-- Convenience methods --></p></li>
2674</ul>
2675
2676<hr>
2677
2678<ul>
2679 <li><tt><a href="#Function">Function</a> *getFunction(const std::string
2680 &amp;Name, const <a href="#FunctionType">FunctionType</a> *Ty)</tt>
2681
2682 <p>Look up the specified function in the <tt>Module</tt> <a
2683 href="#SymbolTable"><tt>SymbolTable</tt></a>. If it does not exist, return
2684 <tt>null</tt>.</p></li>
2685
2686 <li><tt><a href="#Function">Function</a> *getOrInsertFunction(const
2687 std::string &amp;Name, const <a href="#FunctionType">FunctionType</a> *T)</tt>
2688
2689 <p>Look up the specified function in the <tt>Module</tt> <a
2690 href="#SymbolTable"><tt>SymbolTable</tt></a>. If it does not exist, add an
2691 external declaration for the function and return it.</p></li>
2692
2693 <li><tt>std::string getTypeName(const <a href="#Type">Type</a> *Ty)</tt>
2694
2695 <p>If there is at least one entry in the <a
2696 href="#SymbolTable"><tt>SymbolTable</tt></a> for the specified <a
2697 href="#Type"><tt>Type</tt></a>, return it. Otherwise return the empty
2698 string.</p></li>
2699
2700 <li><tt>bool addTypeName(const std::string &amp;Name, const <a
2701 href="#Type">Type</a> *Ty)</tt>
2702
2703 <p>Insert an entry in the <a href="#SymbolTable"><tt>SymbolTable</tt></a>
2704 mapping <tt>Name</tt> to <tt>Ty</tt>. If there is already an entry for this
2705 name, true is returned and the <a
2706 href="#SymbolTable"><tt>SymbolTable</tt></a> is not modified.</p></li>
2707</ul>
2708
2709</div>
2710
2711<!-- ======================================================================= -->
2712<div class="doc_subsection">
2713 <a name="Constant">The <tt>Constant</tt> class and subclasses</a>
2714</div>
2715
2716<div class="doc_text">
2717
2718<p>Constant represents a base class for different types of constants. It
Reid Spencer53bfebc2007-01-12 03:36:33 +00002719is subclassed by ConstantInt, ConstantArray, etc. for representing
Reid Spencerb83eb642006-10-20 07:07:24 +00002720the various types of Constants.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002721
2722</div>
2723
2724<!-- _______________________________________________________________________ -->
2725<div class="doc_subsubsection">
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002726 <a name="m_Constant">Important Public Methods</a>
2727</div>
2728<div class="doc_text">
Misha Brukman13fd15c2004-01-15 00:14:41 +00002729</div>
2730
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002731<!-- _______________________________________________________________________ -->
2732<div class="doc_subsubsection">Important Subclasses of Constant </div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002733<div class="doc_text">
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00002734<ul>
Reid Spencer53bfebc2007-01-12 03:36:33 +00002735 <li>ConstantInt : This subclass of Constant represents an integer constant of
2736 any width, including boolean (1 bit integer).
Chris Lattner261efe92003-11-25 01:02:51 +00002737 <ul>
Reid Spencerb83eb642006-10-20 07:07:24 +00002738 <li><tt>int64_t getSExtValue() const</tt>: Returns the underlying value of
2739 this constant as a sign extended signed integer value.</li>
2740 <li><tt>uint64_t getZExtValue() const</tt>: Returns the underlying value
2741 of this constant as a zero extended unsigned integer value.</li>
Reid Spencer53bfebc2007-01-12 03:36:33 +00002742 <li><tt>static ConstantInt* get(const Type *Ty, uint64_t Val)</tt>:
2743 Returns the ConstantInt object that represents the value provided by
2744 <tt>Val</tt> for integer type <tt>Ty</tt>.</li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002745 </ul>
2746 </li>
2747 <li>ConstantFP : This class represents a floating point constant.
2748 <ul>
2749 <li><tt>double getValue() const</tt>: Returns the underlying value of
2750 this constant. </li>
2751 </ul>
2752 </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002753 <ul>
2754 <li><tt>bool getValue() const</tt>: Returns the underlying value of this
2755 constant. </li>
2756 </ul>
2757 </li>
2758 <li>ConstantArray : This represents a constant array.
2759 <ul>
2760 <li><tt>const std::vector&lt;Use&gt; &amp;getValues() const</tt>: Returns
Chris Lattner58360822005-01-17 00:12:04 +00002761 a vector of component constants that makeup this array. </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002762 </ul>
2763 </li>
2764 <li>ConstantStruct : This represents a constant struct.
2765 <ul>
2766 <li><tt>const std::vector&lt;Use&gt; &amp;getValues() const</tt>: Returns
Chris Lattner58360822005-01-17 00:12:04 +00002767 a vector of component constants that makeup this array. </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00002768 </ul>
2769 </li>
2770 <li>GlobalValue : This represents either a global variable or a function. In
2771 either case, the value is a constant fixed address (after linking).
2772 </li>
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00002773</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002774</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002775<!-- ======================================================================= -->
2776<div class="doc_subsection">
2777 <a name="Argument">The <tt>Argument</tt> class</a>
2778</div>
2779
2780<div class="doc_text">
2781
2782<p>This subclass of Value defines the interface for incoming formal
Chris Lattner58360822005-01-17 00:12:04 +00002783arguments to a function. A Function maintains a list of its formal
Misha Brukman13fd15c2004-01-15 00:14:41 +00002784arguments. An argument has a pointer to the parent Function.</p>
2785
2786</div>
2787
Chris Lattner9355b472002-09-06 02:50:58 +00002788<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +00002789<hr>
2790<address>
2791 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
2792 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
2793 <a href="http://validator.w3.org/check/referer"><img
2794 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
2795
2796 <a href="mailto:dhurjati@cs.uiuc.edu">Dinakar Dhurjati</a> and
2797 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencer05fe4b02006-03-14 05:39:39 +00002798 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002799 Last modified: $Date$
2800</address>
2801
Chris Lattner261efe92003-11-25 01:02:51 +00002802</body>
2803</html>