blob: 8996bdd30939b8ec331c299e17d3780d717fdb4f [file] [log] [blame]
Misha Brukman13fd15c2004-01-15 00:14:41 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
Chris Lattner261efe92003-11-25 01:02:51 +00003<html>
4<head>
5 <title>LLVM Programmer's Manual</title>
Misha Brukman13fd15c2004-01-15 00:14:41 +00006 <link rel="stylesheet" href="llvm.css" type="text/css">
Chris Lattner261efe92003-11-25 01:02:51 +00007</head>
Misha Brukman13fd15c2004-01-15 00:14:41 +00008<body>
9
10<div class="doc_title">
11 LLVM Programmer's Manual
12</div>
13
Chris Lattner9355b472002-09-06 02:50:58 +000014<ol>
Misha Brukman13fd15c2004-01-15 00:14:41 +000015 <li><a href="#introduction">Introduction</a></li>
Chris Lattner9355b472002-09-06 02:50:58 +000016 <li><a href="#general">General Information</a>
Chris Lattner261efe92003-11-25 01:02:51 +000017 <ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +000018 <li><a href="#stl">The C++ Standard Template Library</a></li>
19<!--
20 <li>The <tt>-time-passes</tt> option</li>
21 <li>How to use the LLVM Makefile system</li>
22 <li>How to write a regression test</li>
Chris Lattner61db4652004-12-08 19:05:44 +000023
Reid Spencerfe8f4ff2004-11-01 09:02:53 +000024-->
Chris Lattner84b7f8d2003-08-01 22:20:59 +000025 </ul>
Chris Lattner261efe92003-11-25 01:02:51 +000026 </li>
27 <li><a href="#apis">Important and useful LLVM APIs</a>
28 <ul>
29 <li><a href="#isa">The <tt>isa&lt;&gt;</tt>, <tt>cast&lt;&gt;</tt>
30and <tt>dyn_cast&lt;&gt;</tt> templates</a> </li>
Misha Brukman2c122ce2005-11-01 21:12:49 +000031 <li><a href="#DEBUG">The <tt>DEBUG()</tt> macro and <tt>-debug</tt>
Chris Lattner261efe92003-11-25 01:02:51 +000032option</a>
33 <ul>
34 <li><a href="#DEBUG_TYPE">Fine grained debug info with <tt>DEBUG_TYPE</tt>
35and the <tt>-debug-only</tt> option</a> </li>
36 </ul>
37 </li>
Chris Lattner0be6fdf2006-12-19 21:46:21 +000038 <li><a href="#Statistic">The <tt>Statistic</tt> class &amp; <tt>-stats</tt>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +000039option</a></li>
40<!--
41 <li>The <tt>InstVisitor</tt> template
42 <li>The general graph API
43-->
Chris Lattnerf623a082005-10-17 01:36:23 +000044 <li><a href="#ViewGraph">Viewing graphs while debugging code</a></li>
Chris Lattner261efe92003-11-25 01:02:51 +000045 </ul>
46 </li>
Chris Lattner098129a2007-02-03 03:04:03 +000047 <li><a href="#datastructure">Picking the Right Data Structure for a Task</a>
48 <ul>
Chris Lattner74c4ca12007-02-03 07:59:07 +000049 <li><a href="#ds_sequential">Sequential Containers (std::vector, std::list, etc)</a>
50 <ul>
51 <li><a href="#dss_fixedarrays">Fixed Size Arrays</a></li>
52 <li><a href="#dss_heaparrays">Heap Allocated Arrays</a></li>
53 <li><a href="#dss_smallvector">"llvm/ADT/SmallVector.h"</a></li>
54 <li><a href="#dss_vector">&lt;vector&gt;</a></li>
55 <li><a href="#dss_deque">&lt;deque&gt;</a></li>
56 <li><a href="#dss_list">&lt;list&gt;</a></li>
57 <li><a href="#dss_ilist">llvm/ADT/ilist</a></li>
Chris Lattnerc5722432007-02-03 19:49:31 +000058 <li><a href="#dss_other">Other Sequential Container Options</a></li>
Chris Lattner098129a2007-02-03 03:04:03 +000059 </ul></li>
Chris Lattner74c4ca12007-02-03 07:59:07 +000060 <li><a href="#ds_set">Set-Like Containers (std::set, SmallSet, SetVector, etc)</a>
61 <ul>
62 <li><a href="#dss_sortedvectorset">A sorted 'vector'</a></li>
63 <li><a href="#dss_smallset">"llvm/ADT/SmallSet.h"</a></li>
64 <li><a href="#dss_smallptrset">"llvm/ADT/SmallPtrSet.h"</a></li>
65 <li><a href="#dss_FoldingSet">"llvm/ADT/FoldingSet.h"</a></li>
66 <li><a href="#dss_set">&lt;set&gt;</a></li>
67 <li><a href="#dss_setvector">"llvm/ADT/SetVector.h"</a></li>
Chris Lattnerc5722432007-02-03 19:49:31 +000068 <li><a href="#dss_uniquevector">"llvm/ADT/UniqueVector.h"</a></li>
69 <li><a href="#dss_otherset">Other Set-Like ContainerOptions</a></li>
Chris Lattner74c4ca12007-02-03 07:59:07 +000070 </ul></li>
Chris Lattner098129a2007-02-03 03:04:03 +000071 <li><a href="#ds_map">Map-Like Containers (std::map, DenseMap, etc)</a></li>
Chris Lattner74c4ca12007-02-03 07:59:07 +000072 </ul>
Chris Lattner098129a2007-02-03 03:04:03 +000073 </li>
Chris Lattnerae7f7592002-09-06 18:31:18 +000074 <li><a href="#common">Helpful Hints for Common Operations</a>
Chris Lattnerae7f7592002-09-06 18:31:18 +000075 <ul>
Chris Lattner261efe92003-11-25 01:02:51 +000076 <li><a href="#inspection">Basic Inspection and Traversal Routines</a>
77 <ul>
78 <li><a href="#iterate_function">Iterating over the <tt>BasicBlock</tt>s
79in a <tt>Function</tt></a> </li>
80 <li><a href="#iterate_basicblock">Iterating over the <tt>Instruction</tt>s
81in a <tt>BasicBlock</tt></a> </li>
82 <li><a href="#iterate_institer">Iterating over the <tt>Instruction</tt>s
83in a <tt>Function</tt></a> </li>
84 <li><a href="#iterate_convert">Turning an iterator into a
85class pointer</a> </li>
86 <li><a href="#iterate_complex">Finding call sites: a more
87complex example</a> </li>
88 <li><a href="#calls_and_invokes">Treating calls and invokes
89the same way</a> </li>
90 <li><a href="#iterate_chains">Iterating over def-use &amp;
91use-def chains</a> </li>
92 </ul>
93 </li>
94 <li><a href="#simplechanges">Making simple changes</a>
95 <ul>
96 <li><a href="#schanges_creating">Creating and inserting new
97 <tt>Instruction</tt>s</a> </li>
98 <li><a href="#schanges_deleting">Deleting <tt>Instruction</tt>s</a> </li>
99 <li><a href="#schanges_replacing">Replacing an <tt>Instruction</tt>
100with another <tt>Value</tt></a> </li>
101 </ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000102 </li>
Chris Lattnerae7f7592002-09-06 18:31:18 +0000103<!--
104 <li>Working with the Control Flow Graph
105 <ul>
106 <li>Accessing predecessors and successors of a <tt>BasicBlock</tt>
107 <li>
108 <li>
109 </ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000110-->
Chris Lattner261efe92003-11-25 01:02:51 +0000111 </ul>
112 </li>
Chris Lattnerd9d6e102005-04-23 16:10:52 +0000113
114 <li><a href="#advanced">Advanced Topics</a>
115 <ul>
Chris Lattnerf1b200b2005-04-23 17:27:36 +0000116 <li><a href="#TypeResolve">LLVM Type Resolution</a>
117 <ul>
118 <li><a href="#BuildRecType">Basic Recursive Type Construction</a></li>
119 <li><a href="#refineAbstractTypeTo">The <tt>refineAbstractTypeTo</tt> method</a></li>
120 <li><a href="#PATypeHolder">The PATypeHolder Class</a></li>
121 <li><a href="#AbstractTypeUser">The AbstractTypeUser Class</a></li>
122 </ul></li>
123
Chris Lattnerd9d6e102005-04-23 16:10:52 +0000124 <li><a href="#SymbolTable">The <tt>SymbolTable</tt> class </a></li>
125 </ul></li>
126
Joel Stanley9b96c442002-09-06 21:55:13 +0000127 <li><a href="#coreclasses">The Core LLVM Class Hierarchy Reference</a>
Chris Lattner9355b472002-09-06 02:50:58 +0000128 <ul>
Reid Spencer303c4b42007-01-12 17:26:25 +0000129 <li><a href="#Type">The <tt>Type</tt> class</a> </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000130 <li><a href="#Value">The <tt>Value</tt> class</a>
Chris Lattner9355b472002-09-06 02:50:58 +0000131 <ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000132 <li><a href="#User">The <tt>User</tt> class</a>
Chris Lattner261efe92003-11-25 01:02:51 +0000133 <ul>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000134 <li><a href="#Instruction">The <tt>Instruction</tt> class</a>
135 <ul>
136 <li><a href="#GetElementPtrInst">The <tt>GetElementPtrInst</tt> class</a></li>
137 </ul>
138 </li>
139 <li><a href="#Module">The <tt>Module</tt> class</a></li>
140 <li><a href="#Constant">The <tt>Constant</tt> class</a>
141 <ul>
142 <li><a href="#GlobalValue">The <tt>GlobalValue</tt> class</a>
143 <ul>
144 <li><a href="#BasicBlock">The <tt>BasicBlock</tt>class</a></li>
145 <li><a href="#Function">The <tt>Function</tt> class</a></li>
146 <li><a href="#GlobalVariable">The <tt>GlobalVariable</tt> class</a></li>
147 </ul>
148 </li>
149 </ul>
150 </li>
Reid Spencer8b2da7a2004-07-18 13:10:31 +0000151 </ul>
152 </li>
Reid Spencer096603a2004-05-26 08:41:35 +0000153 <li><a href="#Argument">The <tt>Argument</tt> class</a></li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +0000154 </ul>
155 </li>
156 </ul>
Chris Lattner261efe92003-11-25 01:02:51 +0000157 </li>
Chris Lattner9355b472002-09-06 02:50:58 +0000158</ol>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000159
Chris Lattner69bf8a92004-05-23 21:06:58 +0000160<div class="doc_author">
161 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>,
Chris Lattner94c43592004-05-26 16:52:55 +0000162 <a href="mailto:dhurjati@cs.uiuc.edu">Dinakar Dhurjati</a>,
163 <a href="mailto:jstanley@cs.uiuc.edu">Joel Stanley</a>, and
164 <a href="mailto:rspencer@x10sys.com">Reid Spencer</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000165</div>
166
Chris Lattner9355b472002-09-06 02:50:58 +0000167<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000168<div class="doc_section">
169 <a name="introduction">Introduction </a>
170</div>
Chris Lattner9355b472002-09-06 02:50:58 +0000171<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000172
173<div class="doc_text">
174
175<p>This document is meant to highlight some of the important classes and
Chris Lattner261efe92003-11-25 01:02:51 +0000176interfaces available in the LLVM source-base. This manual is not
177intended to explain what LLVM is, how it works, and what LLVM code looks
178like. It assumes that you know the basics of LLVM and are interested
179in writing transformations or otherwise analyzing or manipulating the
Misha Brukman13fd15c2004-01-15 00:14:41 +0000180code.</p>
181
182<p>This document should get you oriented so that you can find your
Chris Lattner261efe92003-11-25 01:02:51 +0000183way in the continuously growing source code that makes up the LLVM
184infrastructure. Note that this manual is not intended to serve as a
185replacement for reading the source code, so if you think there should be
186a method in one of these classes to do something, but it's not listed,
187check the source. Links to the <a href="/doxygen/">doxygen</a> sources
188are provided to make this as easy as possible.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000189
190<p>The first section of this document describes general information that is
191useful to know when working in the LLVM infrastructure, and the second describes
192the Core LLVM classes. In the future this manual will be extended with
193information describing how to use extension libraries, such as dominator
194information, CFG traversal routines, and useful utilities like the <tt><a
195href="/doxygen/InstVisitor_8h-source.html">InstVisitor</a></tt> template.</p>
196
197</div>
198
Chris Lattner9355b472002-09-06 02:50:58 +0000199<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000200<div class="doc_section">
201 <a name="general">General Information</a>
202</div>
203<!-- *********************************************************************** -->
204
205<div class="doc_text">
206
207<p>This section contains general information that is useful if you are working
208in the LLVM source-base, but that isn't specific to any particular API.</p>
209
210</div>
211
212<!-- ======================================================================= -->
213<div class="doc_subsection">
214 <a name="stl">The C++ Standard Template Library</a>
215</div>
216
217<div class="doc_text">
218
219<p>LLVM makes heavy use of the C++ Standard Template Library (STL),
Chris Lattner261efe92003-11-25 01:02:51 +0000220perhaps much more than you are used to, or have seen before. Because of
221this, you might want to do a little background reading in the
222techniques used and capabilities of the library. There are many good
223pages that discuss the STL, and several books on the subject that you
Misha Brukman13fd15c2004-01-15 00:14:41 +0000224can get, so it will not be discussed in this document.</p>
225
226<p>Here are some useful links:</p>
227
228<ol>
229
230<li><a href="http://www.dinkumware.com/refxcpp.html">Dinkumware C++ Library
231reference</a> - an excellent reference for the STL and other parts of the
232standard C++ library.</li>
233
234<li><a href="http://www.tempest-sw.com/cpp/">C++ In a Nutshell</a> - This is an
Tanya Lattner09cf73c2004-06-22 04:24:55 +0000235O'Reilly book in the making. It has a decent
236Standard Library
237Reference that rivals Dinkumware's, and is unfortunately no longer free since the book has been
Misha Brukman13fd15c2004-01-15 00:14:41 +0000238published.</li>
239
240<li><a href="http://www.parashift.com/c++-faq-lite/">C++ Frequently Asked
241Questions</a></li>
242
243<li><a href="http://www.sgi.com/tech/stl/">SGI's STL Programmer's Guide</a> -
244Contains a useful <a
245href="http://www.sgi.com/tech/stl/stl_introduction.html">Introduction to the
246STL</a>.</li>
247
248<li><a href="http://www.research.att.com/%7Ebs/C++.html">Bjarne Stroustrup's C++
249Page</a></li>
250
Tanya Lattner79445ba2004-12-08 18:34:56 +0000251<li><a href="http://64.78.49.204/">
Reid Spencer096603a2004-05-26 08:41:35 +0000252Bruce Eckel's Thinking in C++, 2nd ed. Volume 2 Revision 4.0 (even better, get
253the book).</a></li>
254
Misha Brukman13fd15c2004-01-15 00:14:41 +0000255</ol>
256
257<p>You are also encouraged to take a look at the <a
258href="CodingStandards.html">LLVM Coding Standards</a> guide which focuses on how
259to write maintainable code more than where to put your curly braces.</p>
260
261</div>
262
263<!-- ======================================================================= -->
264<div class="doc_subsection">
265 <a name="stl">Other useful references</a>
266</div>
267
268<div class="doc_text">
269
Misha Brukman13fd15c2004-01-15 00:14:41 +0000270<ol>
271<li><a href="http://www.psc.edu/%7Esemke/cvs_branches.html">CVS
Chris Lattner261efe92003-11-25 01:02:51 +0000272Branch and Tag Primer</a></li>
Misha Brukmana0f71e42004-06-18 18:39:00 +0000273<li><a href="http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html">Using
274static and shared libraries across platforms</a></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000275</ol>
276
277</div>
278
Chris Lattner9355b472002-09-06 02:50:58 +0000279<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +0000280<div class="doc_section">
281 <a name="apis">Important and useful LLVM APIs</a>
282</div>
283<!-- *********************************************************************** -->
284
285<div class="doc_text">
286
287<p>Here we highlight some LLVM APIs that are generally useful and good to
288know about when writing transformations.</p>
289
290</div>
291
292<!-- ======================================================================= -->
293<div class="doc_subsection">
Misha Brukman2c122ce2005-11-01 21:12:49 +0000294 <a name="isa">The <tt>isa&lt;&gt;</tt>, <tt>cast&lt;&gt;</tt> and
295 <tt>dyn_cast&lt;&gt;</tt> templates</a>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000296</div>
297
298<div class="doc_text">
299
300<p>The LLVM source-base makes extensive use of a custom form of RTTI.
Chris Lattner261efe92003-11-25 01:02:51 +0000301These templates have many similarities to the C++ <tt>dynamic_cast&lt;&gt;</tt>
302operator, but they don't have some drawbacks (primarily stemming from
303the fact that <tt>dynamic_cast&lt;&gt;</tt> only works on classes that
304have a v-table). Because they are used so often, you must know what they
305do and how they work. All of these templates are defined in the <a
Chris Lattner695b78b2005-04-26 22:56:16 +0000306 href="/doxygen/Casting_8h-source.html"><tt>llvm/Support/Casting.h</tt></a>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000307file (note that you very rarely have to include this file directly).</p>
308
309<dl>
310 <dt><tt>isa&lt;&gt;</tt>: </dt>
311
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000312 <dd><p>The <tt>isa&lt;&gt;</tt> operator works exactly like the Java
Misha Brukman13fd15c2004-01-15 00:14:41 +0000313 "<tt>instanceof</tt>" operator. It returns true or false depending on whether
314 a reference or pointer points to an instance of the specified class. This can
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000315 be very useful for constraint checking of various sorts (example below).</p>
316 </dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000317
318 <dt><tt>cast&lt;&gt;</tt>: </dt>
319
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000320 <dd><p>The <tt>cast&lt;&gt;</tt> operator is a "checked cast" operation. It
Misha Brukman13fd15c2004-01-15 00:14:41 +0000321 converts a pointer or reference from a base class to a derived cast, causing
322 an assertion failure if it is not really an instance of the right type. This
323 should be used in cases where you have some information that makes you believe
324 that something is of the right type. An example of the <tt>isa&lt;&gt;</tt>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000325 and <tt>cast&lt;&gt;</tt> template is:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000326
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000327<div class="doc_code">
328<pre>
329static bool isLoopInvariant(const <a href="#Value">Value</a> *V, const Loop *L) {
330 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))
331 return true;
Chris Lattner69bf8a92004-05-23 21:06:58 +0000332
Bill Wendling82e2eea2006-10-11 18:00:22 +0000333 // <i>Otherwise, it must be an instruction...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000334 return !L-&gt;contains(cast&lt;<a href="#Instruction">Instruction</a>&gt;(V)-&gt;getParent());
335}
336</pre>
337</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000338
339 <p>Note that you should <b>not</b> use an <tt>isa&lt;&gt;</tt> test followed
340 by a <tt>cast&lt;&gt;</tt>, for that use the <tt>dyn_cast&lt;&gt;</tt>
341 operator.</p>
342
343 </dd>
344
345 <dt><tt>dyn_cast&lt;&gt;</tt>:</dt>
346
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000347 <dd><p>The <tt>dyn_cast&lt;&gt;</tt> operator is a "checking cast" operation.
348 It checks to see if the operand is of the specified type, and if so, returns a
Misha Brukman13fd15c2004-01-15 00:14:41 +0000349 pointer to it (this operator does not work with references). If the operand is
350 not of the correct type, a null pointer is returned. Thus, this works very
Misha Brukman2c122ce2005-11-01 21:12:49 +0000351 much like the <tt>dynamic_cast&lt;&gt;</tt> operator in C++, and should be
352 used in the same circumstances. Typically, the <tt>dyn_cast&lt;&gt;</tt>
353 operator is used in an <tt>if</tt> statement or some other flow control
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000354 statement like this:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000355
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000356<div class="doc_code">
357<pre>
358if (<a href="#AllocationInst">AllocationInst</a> *AI = dyn_cast&lt;<a href="#AllocationInst">AllocationInst</a>&gt;(Val)) {
Bill Wendling82e2eea2006-10-11 18:00:22 +0000359 // <i>...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000360}
361</pre>
362</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000363
Misha Brukman2c122ce2005-11-01 21:12:49 +0000364 <p>This form of the <tt>if</tt> statement effectively combines together a call
365 to <tt>isa&lt;&gt;</tt> and a call to <tt>cast&lt;&gt;</tt> into one
366 statement, which is very convenient.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000367
Misha Brukman2c122ce2005-11-01 21:12:49 +0000368 <p>Note that the <tt>dyn_cast&lt;&gt;</tt> operator, like C++'s
369 <tt>dynamic_cast&lt;&gt;</tt> or Java's <tt>instanceof</tt> operator, can be
370 abused. In particular, you should not use big chained <tt>if/then/else</tt>
371 blocks to check for lots of different variants of classes. If you find
372 yourself wanting to do this, it is much cleaner and more efficient to use the
373 <tt>InstVisitor</tt> class to dispatch over the instruction type directly.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000374
Misha Brukman2c122ce2005-11-01 21:12:49 +0000375 </dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000376
Misha Brukman2c122ce2005-11-01 21:12:49 +0000377 <dt><tt>cast_or_null&lt;&gt;</tt>: </dt>
378
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000379 <dd><p>The <tt>cast_or_null&lt;&gt;</tt> operator works just like the
Misha Brukman2c122ce2005-11-01 21:12:49 +0000380 <tt>cast&lt;&gt;</tt> operator, except that it allows for a null pointer as an
381 argument (which it then propagates). This can sometimes be useful, allowing
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000382 you to combine several null checks into one.</p></dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000383
Misha Brukman2c122ce2005-11-01 21:12:49 +0000384 <dt><tt>dyn_cast_or_null&lt;&gt;</tt>: </dt>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000385
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000386 <dd><p>The <tt>dyn_cast_or_null&lt;&gt;</tt> operator works just like the
Misha Brukman2c122ce2005-11-01 21:12:49 +0000387 <tt>dyn_cast&lt;&gt;</tt> operator, except that it allows for a null pointer
388 as an argument (which it then propagates). This can sometimes be useful,
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000389 allowing you to combine several null checks into one.</p></dd>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000390
Misha Brukman2c122ce2005-11-01 21:12:49 +0000391</dl>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000392
393<p>These five templates can be used with any classes, whether they have a
394v-table or not. To add support for these templates, you simply need to add
395<tt>classof</tt> static methods to the class you are interested casting
396to. Describing this is currently outside the scope of this document, but there
397are lots of examples in the LLVM source base.</p>
398
399</div>
400
401<!-- ======================================================================= -->
402<div class="doc_subsection">
Misha Brukman2c122ce2005-11-01 21:12:49 +0000403 <a name="DEBUG">The <tt>DEBUG()</tt> macro and <tt>-debug</tt> option</a>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000404</div>
405
406<div class="doc_text">
407
408<p>Often when working on your pass you will put a bunch of debugging printouts
409and other code into your pass. After you get it working, you want to remove
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000410it, but you may need it again in the future (to work out new bugs that you run
Misha Brukman13fd15c2004-01-15 00:14:41 +0000411across).</p>
412
413<p> Naturally, because of this, you don't want to delete the debug printouts,
414but you don't want them to always be noisy. A standard compromise is to comment
415them out, allowing you to enable them if you need them in the future.</p>
416
Chris Lattner695b78b2005-04-26 22:56:16 +0000417<p>The "<tt><a href="/doxygen/Debug_8h-source.html">llvm/Support/Debug.h</a></tt>"
Misha Brukman13fd15c2004-01-15 00:14:41 +0000418file provides a macro named <tt>DEBUG()</tt> that is a much nicer solution to
419this problem. Basically, you can put arbitrary code into the argument of the
420<tt>DEBUG</tt> macro, and it is only executed if '<tt>opt</tt>' (or any other
421tool) is run with the '<tt>-debug</tt>' command line argument:</p>
422
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000423<div class="doc_code">
424<pre>
Bill Wendling832171c2006-12-07 20:04:42 +0000425DOUT &lt;&lt; "I am here!\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000426</pre>
427</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000428
429<p>Then you can run your pass like this:</p>
430
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000431<div class="doc_code">
432<pre>
433$ opt &lt; a.bc &gt; /dev/null -mypass
Bill Wendling82e2eea2006-10-11 18:00:22 +0000434<i>&lt;no output&gt;</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000435$ opt &lt; a.bc &gt; /dev/null -mypass -debug
436I am here!
437</pre>
438</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000439
440<p>Using the <tt>DEBUG()</tt> macro instead of a home-brewed solution allows you
441to not have to create "yet another" command line option for the debug output for
442your pass. Note that <tt>DEBUG()</tt> macros are disabled for optimized builds,
443so they do not cause a performance impact at all (for the same reason, they
444should also not contain side-effects!).</p>
445
446<p>One additional nice thing about the <tt>DEBUG()</tt> macro is that you can
447enable or disable it directly in gdb. Just use "<tt>set DebugFlag=0</tt>" or
448"<tt>set DebugFlag=1</tt>" from the gdb if the program is running. If the
449program hasn't been started yet, you can always just run it with
450<tt>-debug</tt>.</p>
451
452</div>
453
454<!-- _______________________________________________________________________ -->
455<div class="doc_subsubsection">
Chris Lattnerc9151082005-04-26 22:57:07 +0000456 <a name="DEBUG_TYPE">Fine grained debug info with <tt>DEBUG_TYPE</tt> and
Misha Brukman13fd15c2004-01-15 00:14:41 +0000457 the <tt>-debug-only</tt> option</a>
458</div>
459
460<div class="doc_text">
461
462<p>Sometimes you may find yourself in a situation where enabling <tt>-debug</tt>
463just turns on <b>too much</b> information (such as when working on the code
464generator). If you want to enable debug information with more fine-grained
465control, you define the <tt>DEBUG_TYPE</tt> macro and the <tt>-debug</tt> only
466option as follows:</p>
467
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000468<div class="doc_code">
469<pre>
Bill Wendling832171c2006-12-07 20:04:42 +0000470DOUT &lt;&lt; "No debug type\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000471#undef DEBUG_TYPE
472#define DEBUG_TYPE "foo"
Bill Wendling832171c2006-12-07 20:04:42 +0000473DOUT &lt;&lt; "'foo' debug type\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000474#undef DEBUG_TYPE
475#define DEBUG_TYPE "bar"
Bill Wendling832171c2006-12-07 20:04:42 +0000476DOUT &lt;&lt; "'bar' debug type\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000477#undef DEBUG_TYPE
478#define DEBUG_TYPE ""
Bill Wendling832171c2006-12-07 20:04:42 +0000479DOUT &lt;&lt; "No debug type (2)\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000480</pre>
481</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000482
483<p>Then you can run your pass like this:</p>
484
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000485<div class="doc_code">
486<pre>
487$ opt &lt; a.bc &gt; /dev/null -mypass
Bill Wendling82e2eea2006-10-11 18:00:22 +0000488<i>&lt;no output&gt;</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000489$ opt &lt; a.bc &gt; /dev/null -mypass -debug
490No debug type
491'foo' debug type
492'bar' debug type
493No debug type (2)
494$ opt &lt; a.bc &gt; /dev/null -mypass -debug-only=foo
495'foo' debug type
496$ opt &lt; a.bc &gt; /dev/null -mypass -debug-only=bar
497'bar' debug type
498</pre>
499</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000500
501<p>Of course, in practice, you should only set <tt>DEBUG_TYPE</tt> at the top of
502a file, to specify the debug type for the entire module (if you do this before
Chris Lattner695b78b2005-04-26 22:56:16 +0000503you <tt>#include "llvm/Support/Debug.h"</tt>, you don't have to insert the ugly
Misha Brukman13fd15c2004-01-15 00:14:41 +0000504<tt>#undef</tt>'s). Also, you should use names more meaningful than "foo" and
505"bar", because there is no system in place to ensure that names do not
506conflict. If two different modules use the same string, they will all be turned
507on when the name is specified. This allows, for example, all debug information
508for instruction scheduling to be enabled with <tt>-debug-type=InstrSched</tt>,
Chris Lattner261efe92003-11-25 01:02:51 +0000509even if the source lives in multiple files.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000510
511</div>
512
513<!-- ======================================================================= -->
514<div class="doc_subsection">
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000515 <a name="Statistic">The <tt>Statistic</tt> class &amp; <tt>-stats</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000516 option</a>
517</div>
518
519<div class="doc_text">
520
521<p>The "<tt><a
Chris Lattner695b78b2005-04-26 22:56:16 +0000522href="/doxygen/Statistic_8h-source.html">llvm/ADT/Statistic.h</a></tt>" file
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000523provides a class named <tt>Statistic</tt> that is used as a unified way to
Misha Brukman13fd15c2004-01-15 00:14:41 +0000524keep track of what the LLVM compiler is doing and how effective various
525optimizations are. It is useful to see what optimizations are contributing to
526making a particular program run faster.</p>
527
528<p>Often you may run your pass on some big program, and you're interested to see
529how many times it makes a certain transformation. Although you can do this with
530hand inspection, or some ad-hoc method, this is a real pain and not very useful
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000531for big programs. Using the <tt>Statistic</tt> class makes it very easy to
Misha Brukman13fd15c2004-01-15 00:14:41 +0000532keep track of this information, and the calculated information is presented in a
533uniform manner with the rest of the passes being executed.</p>
534
535<p>There are many examples of <tt>Statistic</tt> uses, but the basics of using
536it are as follows:</p>
537
538<ol>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000539 <li><p>Define your statistic like this:</p>
540
541<div class="doc_code">
542<pre>
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000543#define <a href="#DEBUG_TYPE">DEBUG_TYPE</a> "mypassname" <i>// This goes before any #includes.</i>
544STATISTIC(NumXForms, "The # of times I did stuff");
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000545</pre>
546</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000547
Chris Lattner0be6fdf2006-12-19 21:46:21 +0000548 <p>The <tt>STATISTIC</tt> macro defines a static variable, whose name is
549 specified by the first argument. The pass name is taken from the DEBUG_TYPE
550 macro, and the description is taken from the second argument. The variable
Reid Spencer06565dc2007-01-12 17:11:23 +0000551 defined ("NumXForms" in this case) acts like an unsigned integer.</p></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000552
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000553 <li><p>Whenever you make a transformation, bump the counter:</p>
554
555<div class="doc_code">
556<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +0000557++NumXForms; // <i>I did stuff!</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000558</pre>
559</div>
560
Chris Lattner261efe92003-11-25 01:02:51 +0000561 </li>
562 </ol>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000563
564 <p>That's all you have to do. To get '<tt>opt</tt>' to print out the
565 statistics gathered, use the '<tt>-stats</tt>' option:</p>
566
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000567<div class="doc_code">
568<pre>
569$ opt -stats -mypassname &lt; program.bc &gt; /dev/null
Bill Wendling82e2eea2006-10-11 18:00:22 +0000570<i>... statistics output ...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000571</pre>
572</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000573
Chris Lattner261efe92003-11-25 01:02:51 +0000574 <p> When running <tt>gccas</tt> on a C file from the SPEC benchmark
575suite, it gives a report that looks like this:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000576
Bill Wendling3cd5ca62006-10-11 06:30:10 +0000577<div class="doc_code">
578<pre>
579 7646 bytecodewriter - Number of normal instructions
580 725 bytecodewriter - Number of oversized instructions
581 129996 bytecodewriter - Number of bytecode bytes written
582 2817 raise - Number of insts DCEd or constprop'd
583 3213 raise - Number of cast-of-self removed
584 5046 raise - Number of expression trees converted
585 75 raise - Number of other getelementptr's formed
586 138 raise - Number of load/store peepholes
587 42 deadtypeelim - Number of unused typenames removed from symtab
588 392 funcresolve - Number of varargs functions resolved
589 27 globaldce - Number of global variables removed
590 2 adce - Number of basic blocks removed
591 134 cee - Number of branches revectored
592 49 cee - Number of setcc instruction eliminated
593 532 gcse - Number of loads removed
594 2919 gcse - Number of instructions removed
595 86 indvars - Number of canonical indvars added
596 87 indvars - Number of aux indvars removed
597 25 instcombine - Number of dead inst eliminate
598 434 instcombine - Number of insts combined
599 248 licm - Number of load insts hoisted
600 1298 licm - Number of insts hoisted to a loop pre-header
601 3 licm - Number of insts hoisted to multiple loop preds (bad, no loop pre-header)
602 75 mem2reg - Number of alloca's promoted
603 1444 cfgsimplify - Number of blocks simplified
604</pre>
605</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +0000606
607<p>Obviously, with so many optimizations, having a unified framework for this
608stuff is very nice. Making your pass fit well into the framework makes it more
609maintainable and useful.</p>
610
611</div>
612
Chris Lattnerf623a082005-10-17 01:36:23 +0000613<!-- ======================================================================= -->
614<div class="doc_subsection">
615 <a name="ViewGraph">Viewing graphs while debugging code</a>
616</div>
617
618<div class="doc_text">
619
620<p>Several of the important data structures in LLVM are graphs: for example
621CFGs made out of LLVM <a href="#BasicBlock">BasicBlock</a>s, CFGs made out of
622LLVM <a href="CodeGenerator.html#machinebasicblock">MachineBasicBlock</a>s, and
623<a href="CodeGenerator.html#selectiondag_intro">Instruction Selection
624DAGs</a>. In many cases, while debugging various parts of the compiler, it is
625nice to instantly visualize these graphs.</p>
626
627<p>LLVM provides several callbacks that are available in a debug build to do
628exactly that. If you call the <tt>Function::viewCFG()</tt> method, for example,
629the current LLVM tool will pop up a window containing the CFG for the function
630where each basic block is a node in the graph, and each node contains the
631instructions in the block. Similarly, there also exists
632<tt>Function::viewCFGOnly()</tt> (does not include the instructions), the
633<tt>MachineFunction::viewCFG()</tt> and <tt>MachineFunction::viewCFGOnly()</tt>,
634and the <tt>SelectionDAG::viewGraph()</tt> methods. Within GDB, for example,
Jim Laskey543a0ee2006-10-02 12:28:07 +0000635you can usually use something like <tt>call DAG.viewGraph()</tt> to pop
Chris Lattnerf623a082005-10-17 01:36:23 +0000636up a window. Alternatively, you can sprinkle calls to these functions in your
637code in places you want to debug.</p>
638
639<p>Getting this to work requires a small amount of configuration. On Unix
640systems with X11, install the <a href="http://www.graphviz.org">graphviz</a>
641toolkit, and make sure 'dot' and 'gv' are in your path. If you are running on
642Mac OS/X, download and install the Mac OS/X <a
643href="http://www.pixelglow.com/graphviz/">Graphviz program</a>, and add
644<tt>/Applications/Graphviz.app/Contents/MacOS/</tt> (or whereever you install
645it) to your path. Once in your system and path are set up, rerun the LLVM
646configure script and rebuild LLVM to enable this functionality.</p>
647
Jim Laskey543a0ee2006-10-02 12:28:07 +0000648<p><tt>SelectionDAG</tt> has been extended to make it easier to locate
649<i>interesting</i> nodes in large complex graphs. From gdb, if you
650<tt>call DAG.setGraphColor(<i>node</i>, "<i>color</i>")</tt>, then the
651next <tt>call DAG.viewGraph()</tt> would hilight the node in the
652specified color (choices of colors can be found at <a
Chris Lattner302da1e2007-02-03 03:05:57 +0000653href="http://www.graphviz.org/doc/info/colors.html">colors</a>.) More
Jim Laskey543a0ee2006-10-02 12:28:07 +0000654complex node attributes can be provided with <tt>call
655DAG.setGraphAttrs(<i>node</i>, "<i>attributes</i>")</tt> (choices can be
656found at <a href="http://www.graphviz.org/doc/info/attrs.html">Graph
657Attributes</a>.) If you want to restart and clear all the current graph
658attributes, then you can <tt>call DAG.clearGraphAttrs()</tt>. </p>
659
Chris Lattnerf623a082005-10-17 01:36:23 +0000660</div>
661
Chris Lattner098129a2007-02-03 03:04:03 +0000662<!-- *********************************************************************** -->
663<div class="doc_section">
664 <a name="datastructure">Picking the Right Data Structure for a Task</a>
665</div>
666<!-- *********************************************************************** -->
667
668<div class="doc_text">
669
670<p>LLVM has a plethora of datastructures in the <tt>llvm/ADT/</tt> directory,
671 and we commonly use STL datastructures. This section describes the tradeoffs
672 you should consider when you pick one.</p>
673
674<p>
675The first step is a choose your own adventure: do you want a sequential
676container, a set-like container, or a map-like container? The most important
677thing when choosing a container is the algorithmic properties of how you plan to
678access the container. Based on that, you should use:</p>
679
680<ul>
681<li>a <a href="#ds_map">map-like</a> container if you need efficient lookup
682 of an value based on another value. Map-like containers also support
683 efficient queries for containment (whether a key is in the map). Map-like
684 containers generally do not support efficient reverse mapping (values to
685 keys). If you need that, use two maps. Some map-like containers also
686 support efficient iteration through the keys in sorted order. Map-like
687 containers are the most expensive sort, only use them if you need one of
688 these capabilities.</li>
689
690<li>a <a href="#ds_set">set-like</a> container if you need to put a bunch of
691 stuff into a container that automatically eliminates duplicates. Some
692 set-like containers support efficient iteration through the elements in
693 sorted order. Set-like containers are more expensive than sequential
694 containers.
695</li>
696
697<li>a <a href="#ds_sequential">sequential</a> container provides
698 the most efficient way to add elements and keeps track of the order they are
699 added to the collection. They permit duplicates and support efficient
700 iteration, but do not support efficient lookup based on a key.
701</li>
702
703</ul>
704
705<p>
706Once the proper catagory of container is determined, you can fine tune the
707memory use, constant factors, and cache behaviors of access by intelligently
708picking a member of the catagory. Note that constant factors and cache behavior
709can be a big deal. If you have a vector that usually only contains a few
710elements (but could contain many), for example, it's much better to use
711<a href="#dss_smallvector">SmallVector</a> than <a href="#dss_vector">vector</a>
712. Doing so avoids (relatively) expensive malloc/free calls, which dwarf the
713cost of adding the elements to the container. </p>
714
715</div>
716
717<!-- ======================================================================= -->
718<div class="doc_subsection">
719 <a name="ds_sequential">Sequential Containers (std::vector, std::list, etc)</a>
720</div>
721
722<div class="doc_text">
723There are a variety of sequential containers available for you, based on your
724needs. Pick the first in this section that will do what you want.
725</div>
726
727<!-- _______________________________________________________________________ -->
728<div class="doc_subsubsection">
729 <a name="dss_fixedarrays">Fixed Size Arrays</a>
730</div>
731
732<div class="doc_text">
733<p>Fixed size arrays are very simple and very fast. They are good if you know
734exactly how many elements you have, or you have a (low) upper bound on how many
735you have.</p>
736</div>
737
738<!-- _______________________________________________________________________ -->
739<div class="doc_subsubsection">
740 <a name="dss_heaparrays">Heap Allocated Arrays</a>
741</div>
742
743<div class="doc_text">
744<p>Heap allocated arrays (new[] + delete[]) are also simple. They are good if
745the number of elements is variable, if you know how many elements you will need
746before the array is allocated, and if the array is usually large (if not,
747consider a <a href="#dss_smallvector">SmallVector</a>). The cost of a heap
748allocated array is the cost of the new/delete (aka malloc/free). Also note that
749if you are allocating an array of a type with a constructor, the constructor and
750destructors will be run for every element in the array (resizable vectors only
751construct those elements actually used).</p>
752</div>
753
754<!-- _______________________________________________________________________ -->
755<div class="doc_subsubsection">
756 <a name="dss_smallvector">"llvm/ADT/SmallVector.h"</a>
757</div>
758
759<div class="doc_text">
760<p><tt>SmallVector&lt;Type, N&gt;</tt> is a simple class that looks and smells
761just like <tt>vector&lt;Type&gt;</tt>:
762it supports efficient iteration, lays out elements in memory order (so you can
763do pointer arithmetic between elements), supports efficient push_back/pop_back
764operations, supports efficient random access to its elements, etc.</p>
765
766<p>The advantage of SmallVector is that it allocates space for
767some number of elements (N) <b>in the object itself</b>. Because of this, if
768the SmallVector is dynamically smaller than N, no malloc is performed. This can
769be a big win in cases where the malloc/free call is far more expensive than the
770code that fiddles around with the elements.</p>
771
772<p>This is good for vectors that are "usually small" (e.g. the number of
773predecessors/successors of a block is usually less than 8). On the other hand,
774this makes the size of the SmallVector itself large, so you don't want to
775allocate lots of them (doing so will waste a lot of space). As such,
776SmallVectors are most useful when on the stack.</p>
777
778<p>SmallVector also provides a nice portable and efficient replacement for
779<tt>alloca</tt>.</p>
780
781</div>
782
783<!-- _______________________________________________________________________ -->
784<div class="doc_subsubsection">
785 <a name="dss_vector">&lt;vector&gt;</a>
786</div>
787
788<div class="doc_text">
789<p>
790std::vector is well loved and respected. It is useful when SmallVector isn't:
791when the size of the vector is often large (thus the small optimization will
792rarely be a benefit) or if you will be allocating many instances of the vector
793itself (which would waste space for elements that aren't in the container).
794vector is also useful when interfacing with code that expects vectors :).
795</p>
796</div>
797
798<!-- _______________________________________________________________________ -->
799<div class="doc_subsubsection">
Chris Lattner74c4ca12007-02-03 07:59:07 +0000800 <a name="dss_deque">&lt;deque&gt;</a>
801</div>
802
803<div class="doc_text">
804<p>std::deque is, in some senses, a generalized version of std::vector. Like
805std::vector, it provides constant time random access and other similar
806properties, but it also provides efficient access to the front of the list. It
807does not guarantee continuity of elements within memory.</p>
808
809<p>In exchange for this extra flexibility, std::deque has significantly higher
810constant factor costs than std::vector. If possible, use std::vector or
811something cheaper.</p>
812</div>
813
814<!-- _______________________________________________________________________ -->
815<div class="doc_subsubsection">
Chris Lattner098129a2007-02-03 03:04:03 +0000816 <a name="dss_list">&lt;list&gt;</a>
817</div>
818
819<div class="doc_text">
820<p>std::list is an extremely inefficient class that is rarely useful.
821It performs a heap allocation for every element inserted into it, thus having an
822extremely high constant factor, particularly for small data types. std::list
823also only supports bidirectional iteration, not random access iteration.</p>
824
825<p>In exchange for this high cost, std::list supports efficient access to both
826ends of the list (like std::deque, but unlike std::vector or SmallVector). In
827addition, the iterator invalidation characteristics of std::list are stronger
828than that of a vector class: inserting or removing an element into the list does
829not invalidate iterator or pointers to other elements in the list.</p>
830</div>
831
832<!-- _______________________________________________________________________ -->
833<div class="doc_subsubsection">
834 <a name="dss_ilist">llvm/ADT/ilist</a>
835</div>
836
837<div class="doc_text">
838<p><tt>ilist&lt;T&gt;</tt> implements an 'intrusive' doubly-linked list. It is
839intrusive, because it requires the element to store and provide access to the
840prev/next pointers for the list.</p>
841
842<p>ilist has the same drawbacks as std::list, and additionally requires an
843ilist_traits implementation for the element type, but it provides some novel
844characteristics. In particular, it can efficiently store polymorphic objects,
845the traits class is informed when an element is inserted or removed from the
846list, and ilists are guaranteed to support a constant-time splice operation.
847</p>
848
849<p>These properties are exactly what we want for things like Instructions and
850basic blocks, which is why these are implemented with ilists.</p>
851</div>
852
853<!-- _______________________________________________________________________ -->
854<div class="doc_subsubsection">
Chris Lattnerc5722432007-02-03 19:49:31 +0000855 <a name="dss_other">Other Sequential Container options</a>
Chris Lattner098129a2007-02-03 03:04:03 +0000856</div>
857
858<div class="doc_text">
Chris Lattner74c4ca12007-02-03 07:59:07 +0000859<p>Other STL containers are available, such as std::string.</p>
Chris Lattner098129a2007-02-03 03:04:03 +0000860
861<p>There are also various STL adapter classes such as std::queue,
862std::priority_queue, std::stack, etc. These provide simplified access to an
863underlying container but don't affect the cost of the container itself.</p>
864
865</div>
866
867
868<!-- ======================================================================= -->
869<div class="doc_subsection">
870 <a name="ds_set">Set-Like Containers (std::set, SmallSet, SetVector, etc)</a>
871</div>
872
873<div class="doc_text">
874
Chris Lattner74c4ca12007-02-03 07:59:07 +0000875<p>Set-like containers are useful when you need to canonicalize multiple values
876into a single representation. There are several different choices for how to do
877this, providing various trade-offs.</p>
878
879</div>
880
881
882<!-- _______________________________________________________________________ -->
883<div class="doc_subsubsection">
884 <a name="dss_sortedvectorset">A sorted 'vector'</a>
885</div>
886
887<div class="doc_text">
888
Chris Lattner3b23a8c2007-02-03 08:10:45 +0000889<p>If you intend to insert a lot of elements, then do a lot of queries, a
890great approach is to use a vector (or other sequential container) with
Chris Lattner74c4ca12007-02-03 07:59:07 +0000891std::sort+std::unique to remove duplicates. This approach works really well if
Chris Lattner3b23a8c2007-02-03 08:10:45 +0000892your usage pattern has these two distinct phases (insert then query), and can be
893coupled with a good choice of <a href="#ds_sequential">sequential container</a>.
894</p>
895
896<p>
897This combination provides the several nice properties: the result data is
898contiguous in memory (good for cache locality), has few allocations, is easy to
899address (iterators in the final vector are just indices or pointers), and can be
900efficiently queried with a standard binary or radix search.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +0000901
902</div>
903
904<!-- _______________________________________________________________________ -->
905<div class="doc_subsubsection">
906 <a name="dss_smallset">"llvm/ADT/SmallSet.h"</a>
907</div>
908
909<div class="doc_text">
910
911<p>If you have a set-like datastructure that is usually small and whose elements
Chris Lattner4ddfac12007-02-03 07:59:51 +0000912are reasonably small, a <tt>SmallSet&lt;Type, N&gt;</tt> is a good choice. This set
Chris Lattner74c4ca12007-02-03 07:59:07 +0000913has space for N elements in place (thus, if the set is dynamically smaller than
Chris Lattner14868db2007-02-03 08:20:15 +0000914N, no malloc traffic is required) and accesses them with a simple linear search.
915When the set grows beyond 'N' elements, it allocates a more expensive representation that
Chris Lattner74c4ca12007-02-03 07:59:07 +0000916guarantees efficient access (for most types, it falls back to std::set, but for
Chris Lattner14868db2007-02-03 08:20:15 +0000917pointers it uses something far better, <a
Chris Lattner74c4ca12007-02-03 07:59:07 +0000918href="#dss_smallptrset">SmallPtrSet</a>).</p>
919
920<p>The magic of this class is that it handles small sets extremely efficiently,
921but gracefully handles extremely large sets without loss of efficiency. The
922drawback is that the interface is quite small: it supports insertion, queries
923and erasing, but does not support iteration.</p>
924
925</div>
926
927<!-- _______________________________________________________________________ -->
928<div class="doc_subsubsection">
929 <a name="dss_smallptrset">"llvm/ADT/SmallPtrSet.h"</a>
930</div>
931
932<div class="doc_text">
933
934<p>SmallPtrSet has all the advantages of SmallSet (and a SmallSet of pointers is
935transparently implemented with a SmallPtrSet), but also suports iterators. If
Chris Lattner14868db2007-02-03 08:20:15 +0000936more than 'N' insertions are performed, a single quadratically
Chris Lattner74c4ca12007-02-03 07:59:07 +0000937probed hash table is allocated and grows as needed, providing extremely
938efficient access (constant time insertion/deleting/queries with low constant
939factors) and is very stingy with malloc traffic.</p>
940
941<p>Note that, unlike std::set, the iterators of SmallPtrSet are invalidated
942whenever an insertion occurs. Also, the values visited by the iterators are not
943visited in sorted order.</p>
944
945</div>
946
947<!-- _______________________________________________________________________ -->
948<div class="doc_subsubsection">
949 <a name="dss_FoldingSet">"llvm/ADT/FoldingSet.h"</a>
950</div>
951
952<div class="doc_text">
953
Chris Lattner098129a2007-02-03 03:04:03 +0000954<p>
Chris Lattner74c4ca12007-02-03 07:59:07 +0000955FoldingSet is an aggregate class that is really good at uniquing
956expensive-to-create or polymorphic objects. It is a combination of a chained
957hash table with intrusive links (uniqued objects are required to inherit from
Chris Lattner14868db2007-02-03 08:20:15 +0000958FoldingSetNode) that uses <a href="#dss_smallvector">SmallVector</a> as part of
959its ID process.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +0000960
Chris Lattner14868db2007-02-03 08:20:15 +0000961<p>Consider a case where you want to implement a "getOrCreateFoo" method for
Chris Lattner74c4ca12007-02-03 07:59:07 +0000962a complex object (for example, a node in the code generator). The client has a
963description of *what* it wants to generate (it knows the opcode and all the
964operands), but we don't want to 'new' a node, then try inserting it into a set
Chris Lattner14868db2007-02-03 08:20:15 +0000965only to find out it already exists, at which point we would have to delete it
966and return the node that already exists.
Chris Lattner098129a2007-02-03 03:04:03 +0000967</p>
968
Chris Lattner74c4ca12007-02-03 07:59:07 +0000969<p>To support this style of client, FoldingSet perform a query with a
970FoldingSetNodeID (which wraps SmallVector) that can be used to describe the
971element that we want to query for. The query either returns the element
972matching the ID or it returns an opaque ID that indicates where insertion should
Chris Lattner14868db2007-02-03 08:20:15 +0000973take place. Construction of the ID usually does not require heap traffic.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +0000974
975<p>Because FoldingSet uses intrusive links, it can support polymorphic objects
976in the set (for example, you can have SDNode instances mixed with LoadSDNodes).
977Because the elements are individually allocated, pointers to the elements are
978stable: inserting or removing elements does not invalidate any pointers to other
979elements.
980</p>
981
982</div>
983
984<!-- _______________________________________________________________________ -->
985<div class="doc_subsubsection">
986 <a name="dss_set">&lt;set&gt;</a>
987</div>
988
989<div class="doc_text">
990
Chris Lattnerc5722432007-02-03 19:49:31 +0000991<p><tt>std::set</tt> is a reasonable all-around set class, which is decent at
992many things but great at nothing. std::set allocates memory for each element
Chris Lattner74c4ca12007-02-03 07:59:07 +0000993inserted (thus it is very malloc intensive) and typically stores three pointers
Chris Lattner14868db2007-02-03 08:20:15 +0000994per element in the set (thus adding a large amount of per-element space
995overhead). It offers guaranteed log(n) performance, which is not particularly
Chris Lattnerc5722432007-02-03 19:49:31 +0000996fast from a complexity standpoint (particularly if the elements of the set are
997expensive to compare, like strings), and has extremely high constant factors for
998lookup, insertion and removal.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +0000999
Chris Lattner14868db2007-02-03 08:20:15 +00001000<p>The advantages of std::set are that its iterators are stable (deleting or
Chris Lattner74c4ca12007-02-03 07:59:07 +00001001inserting an element from the set does not affect iterators or pointers to other
1002elements) and that iteration over the set is guaranteed to be in sorted order.
1003If the elements in the set are large, then the relative overhead of the pointers
1004and malloc traffic is not a big deal, but if the elements of the set are small,
1005std::set is almost never a good choice.</p>
1006
1007</div>
1008
1009<!-- _______________________________________________________________________ -->
1010<div class="doc_subsubsection">
1011 <a name="dss_setvector">"llvm/ADT/SetVector.h"</a>
1012</div>
1013
1014<div class="doc_text">
1015<p>LLVM's SetVector&lt;Type&gt; is actually a combination of a set along with
1016a <a href="#ds_sequential">Sequential Container</a>. The important property
1017that this provides is efficient insertion with uniquing (duplicate elements are
1018ignored) with iteration support. It implements this by inserting elements into
1019both a set-like container and the sequential container, using the set-like
1020container for uniquing and the sequential container for iteration.
1021</p>
1022
1023<p>The difference between SetVector and other sets is that the order of
1024iteration is guaranteed to match the order of insertion into the SetVector.
1025This property is really important for things like sets of pointers. Because
1026pointer values are non-deterministic (e.g. vary across runs of the program on
1027different machines), iterating over the pointers in a std::set or other set will
1028not be in a well-defined order.</p>
1029
1030<p>
1031The drawback of SetVector is that it requires twice as much space as a normal
1032set and has the sum of constant factors from the set-like container and the
1033sequential container that it uses. Use it *only* if you need to iterate over
1034the elements in a deterministic order. SetVector is also expensive to delete
1035elements out of (linear time).
1036</p>
1037
1038</div>
1039
1040<!-- _______________________________________________________________________ -->
1041<div class="doc_subsubsection">
Chris Lattnerc5722432007-02-03 19:49:31 +00001042 <a name="dss_uniquevector">"llvm/ADT/UniqueVector.h"</a>
1043</div>
1044
1045<div class="doc_text">
1046
1047<p>
1048UniqueVector is similar to <a href="#dss_setvector">SetVector</a>, but it
1049retains a unique ID for each element inserted into the set. It internally
1050contains a map and a vector, and it assigns a unique ID for each value inserted
1051into the set.</p>
1052
1053<p>UniqueVector is very expensive: its cost is the sum of the cost of
1054maintaining both the map and vector, it has high complexity, high constant
1055factors, and produces a lot of malloc traffic. It should be avoided.</p>
1056
1057</div>
1058
1059
1060<!-- _______________________________________________________________________ -->
1061<div class="doc_subsubsection">
1062 <a name="dss_otherset">Other Set-Like Container Options</a>
Chris Lattner74c4ca12007-02-03 07:59:07 +00001063</div>
1064
1065<div class="doc_text">
1066
1067<p>
1068The STL provides several other options, such as std::multiset and the various
Chris Lattnerc5722432007-02-03 19:49:31 +00001069"hash_set" like containers (whether from C++ TR1 or from the SGI library).</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +00001070
1071<p>std::multiset is useful if you're not interested in elimination of
Chris Lattner14868db2007-02-03 08:20:15 +00001072duplicates, but has all the drawbacks of std::set. A sorted vector (where you
1073don't delete duplicate entries) or some other approach is almost always
1074better.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +00001075
1076<p>The various hash_set implementations (exposed portably by
Chris Lattner14868db2007-02-03 08:20:15 +00001077"llvm/ADT/hash_set") is a simple chained hashtable. This algorithm is as malloc
1078intensive as std::set (performing an allocation for each element inserted,
Chris Lattner74c4ca12007-02-03 07:59:07 +00001079thus having really high constant factors) but (usually) provides O(1)
1080insertion/deletion of elements. This can be useful if your elements are large
Chris Lattner14868db2007-02-03 08:20:15 +00001081(thus making the constant-factor cost relatively low) or if comparisons are
1082expensive. Element iteration does not visit elements in a useful order.</p>
Chris Lattner74c4ca12007-02-03 07:59:07 +00001083
Chris Lattner098129a2007-02-03 03:04:03 +00001084</div>
1085
1086<!-- ======================================================================= -->
1087<div class="doc_subsection">
1088 <a name="ds_map">Map-Like Containers (std::map, DenseMap, etc)</a>
1089</div>
1090
1091<div class="doc_text">
Chris Lattnerc5722432007-02-03 19:49:31 +00001092Map-like containers are useful when you want to associate data to a key. As
1093usual, there are a lot of different ways to do this. :)
1094</div>
1095
1096<!-- _______________________________________________________________________ -->
1097<div class="doc_subsubsection">
1098 <a name="dss_sortedvectormap">A sorted 'vector'</a>
1099</div>
1100
1101<div class="doc_text">
1102
1103<p>
1104If your usage pattern follows a strict insert-then-query approach, you can
1105trivially use the same approach as <a href="#dss_sortedvectorset">sorted vectors
1106for set-like containers</a>. The only difference is that your query function
1107(which uses std::lower_bound to get efficient log(n) lookup) should only compare
1108the key, not both the key and value. This yields the same advantages as sorted
1109vectors for sets.
1110</p>
1111</div>
1112
1113<!-- _______________________________________________________________________ -->
1114<div class="doc_subsubsection">
1115 <a name="dss_cstringmap">"llvm/ADT/CStringMap.h"</a>
1116</div>
1117
1118<div class="doc_text">
1119
1120<p>
1121Strings are commonly used as keys in maps, and they are difficult to support
1122efficiently: they are variable length, inefficient to hash and compare when
1123long, expensive to copy, etc. CStringMap is a specialized container designed to
1124cope with these issues. It supports mapping an arbitrary range of bytes that
1125does not have an embedded nul character in it ("C strings") to an arbitrary
1126other object.</p>
1127
1128<p>The CStringMap implementation uses a quadratically-probed hash table, where
1129the buckets store a pointer to the heap allocated entries (and some other
1130stuff). The entries in the map must be heap allocated because the strings are
1131variable length. The string data (key) and the element object (value) are
1132stored in the same allocation with the string data immediately after the element
1133object. This container guarantees the "<tt>(char*)(&amp;Value+1)</tt>" points
1134to the key string for a value.</p>
1135
1136<p>The CStringMap is very fast for several reasons: quadratic probing is very
1137cache efficient for lookups, the hash value of strings in buckets is not
1138recomputed when lookup up an element, CStringMap rarely has to touch the
1139memory for unrelated objects when looking up a value (even when hash collisions
1140happen), hash table growth does not recompute the hash values for strings
1141already in the table, and each pair in the map is store in a single allocation
1142(the string data is stored in the same allocation as the Value of a pair).</p>
1143
1144<p>CStringMap also provides query methods that take byte ranges, so it only ever
1145copies a string if a value is inserted into the table.</p>
1146</div>
1147
1148<!-- _______________________________________________________________________ -->
1149<div class="doc_subsubsection">
1150 <a name="dss_indexedmap">"llvm/ADT/IndexedMap.h"</a>
1151</div>
1152
1153<div class="doc_text">
1154<p>
1155IndexedMap is a specialized container for mapping small dense integers (or
1156values that can be mapped to small dense integers) to some other type. It is
1157internally implemented as a vector with a mapping function that maps the keys to
1158the dense integer range.
1159</p>
1160
1161<p>
1162This is useful for cases like virtual registers in the LLVM code generator: they
1163have a dense mapping that is offset by a compile-time constant (the first
1164virtual register ID).</p>
1165
1166</div>
1167
1168<!-- _______________________________________________________________________ -->
1169<div class="doc_subsubsection">
1170 <a name="dss_densemap">"llvm/ADT/DenseMap.h"</a>
1171</div>
1172
1173<div class="doc_text">
1174
1175<p>
1176DenseMap is a simple quadratically probed hash table. It excels at supporting
1177small keys and values: it uses a single allocation to hold all of the pairs that
1178are currently inserted in the map. DenseMap is a great way to map pointers to
1179pointers, or map other small types to each other.
1180</p>
1181
1182<p>
1183There are several aspects of DenseMap that you should be aware of, however. The
1184iterators in a densemap are invalidated whenever an insertion occurs, unlike
1185map. Also, because DenseMap allocates space for a large number of key/value
1186pairs (it starts with 64 by default) if you have large keys or values, it can
1187waste a lot of space. Finally, you must implement a partial specialization of
1188DenseMapKeyInfo for the key that you want, if it isn't already supported. This
1189is required to tell DenseMap about two special marker values (which can never be
1190inserted into the map).</p>
1191
1192</div>
1193
1194<!-- _______________________________________________________________________ -->
1195<div class="doc_subsubsection">
1196 <a name="dss_map">&lt;map&gt;</a>
1197</div>
1198
1199<div class="doc_text">
1200
1201<p>
1202std::map has similar characteristics to <a href="#dss_set">std::set</a>: it uses
1203a single allocation per pair inserted into the map, it offers log(n) lookup with
1204an extremely large constant factor, imposes a space penalty of 3 pointers per
1205pair in the map, etc.</p>
1206
1207<p>std::map is most useful when your keys or values are very large, if you need
1208to iterate over the collection in sorted order, or if you need stable iterators
1209into the map (i.e. they don't get invalidated if an insertion or deletion of
1210another element takes place).</p>
1211
1212</div>
1213
1214<!-- _______________________________________________________________________ -->
1215<div class="doc_subsubsection">
1216 <a name="dss_othermap">Other Map-Like Container Options</a>
1217</div>
1218
1219<div class="doc_text">
1220
1221<p>
1222The STL provides several other options, such as std::multimap and the various
1223"hash_map" like containers (whether from C++ TR1 or from the SGI library).</p>
1224
1225<p>std::multimap is useful if you want to map a key to multiple values, but has
1226all the drawbacks of std::map. A sorted vector or some other approach is almost
1227always better.</p>
1228
1229<p>The various hash_map implementations (exposed portably by
1230"llvm/ADT/hash_map") are simple chained hash tables. This algorithm is as
1231malloc intensive as std::map (performing an allocation for each element
1232inserted, thus having really high constant factors) but (usually) provides O(1)
1233insertion/deletion of elements. This can be useful if your elements are large
1234(thus making the constant-factor cost relatively low) or if comparisons are
1235expensive. Element iteration does not visit elements in a useful order.</p>
1236
Chris Lattner098129a2007-02-03 03:04:03 +00001237</div>
1238
Chris Lattnerf623a082005-10-17 01:36:23 +00001239
Misha Brukman13fd15c2004-01-15 00:14:41 +00001240<!-- *********************************************************************** -->
1241<div class="doc_section">
1242 <a name="common">Helpful Hints for Common Operations</a>
1243</div>
1244<!-- *********************************************************************** -->
1245
1246<div class="doc_text">
1247
1248<p>This section describes how to perform some very simple transformations of
1249LLVM code. This is meant to give examples of common idioms used, showing the
1250practical side of LLVM transformations. <p> Because this is a "how-to" section,
1251you should also read about the main classes that you will be working with. The
1252<a href="#coreclasses">Core LLVM Class Hierarchy Reference</a> contains details
1253and descriptions of the main classes that you should know about.</p>
1254
1255</div>
1256
1257<!-- NOTE: this section should be heavy on example code -->
1258<!-- ======================================================================= -->
1259<div class="doc_subsection">
1260 <a name="inspection">Basic Inspection and Traversal Routines</a>
1261</div>
1262
1263<div class="doc_text">
1264
1265<p>The LLVM compiler infrastructure have many different data structures that may
1266be traversed. Following the example of the C++ standard template library, the
1267techniques used to traverse these various data structures are all basically the
1268same. For a enumerable sequence of values, the <tt>XXXbegin()</tt> function (or
1269method) returns an iterator to the start of the sequence, the <tt>XXXend()</tt>
1270function returns an iterator pointing to one past the last valid element of the
1271sequence, and there is some <tt>XXXiterator</tt> data type that is common
1272between the two operations.</p>
1273
1274<p>Because the pattern for iteration is common across many different aspects of
1275the program representation, the standard template library algorithms may be used
1276on them, and it is easier to remember how to iterate. First we show a few common
1277examples of the data structures that need to be traversed. Other data
1278structures are traversed in very similar ways.</p>
1279
1280</div>
1281
1282<!-- _______________________________________________________________________ -->
Chris Lattner69bf8a92004-05-23 21:06:58 +00001283<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00001284 <a name="iterate_function">Iterating over the </a><a
1285 href="#BasicBlock"><tt>BasicBlock</tt></a>s in a <a
1286 href="#Function"><tt>Function</tt></a>
1287</div>
1288
1289<div class="doc_text">
1290
1291<p>It's quite common to have a <tt>Function</tt> instance that you'd like to
1292transform in some way; in particular, you'd like to manipulate its
1293<tt>BasicBlock</tt>s. To facilitate this, you'll need to iterate over all of
1294the <tt>BasicBlock</tt>s that constitute the <tt>Function</tt>. The following is
1295an example that prints the name of a <tt>BasicBlock</tt> and the number of
1296<tt>Instruction</tt>s it contains:</p>
1297
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001298<div class="doc_code">
1299<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001300// <i>func is a pointer to a Function instance</i>
1301for (Function::iterator i = func-&gt;begin(), e = func-&gt;end(); i != e; ++i)
1302 // <i>Print out the name of the basic block if it has one, and then the</i>
1303 // <i>number of instructions that it contains</i>
Bill Wendling832171c2006-12-07 20:04:42 +00001304 llvm::cerr &lt;&lt; "Basic block (name=" &lt;&lt; i-&gt;getName() &lt;&lt; ") has "
1305 &lt;&lt; i-&gt;size() &lt;&lt; " instructions.\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001306</pre>
1307</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001308
1309<p>Note that i can be used as if it were a pointer for the purposes of
Joel Stanley9b96c442002-09-06 21:55:13 +00001310invoking member functions of the <tt>Instruction</tt> class. This is
1311because the indirection operator is overloaded for the iterator
Chris Lattner7496ec52003-08-05 22:54:23 +00001312classes. In the above code, the expression <tt>i-&gt;size()</tt> is
Misha Brukman13fd15c2004-01-15 00:14:41 +00001313exactly equivalent to <tt>(*i).size()</tt> just like you'd expect.</p>
1314
1315</div>
1316
1317<!-- _______________________________________________________________________ -->
Chris Lattner69bf8a92004-05-23 21:06:58 +00001318<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00001319 <a name="iterate_basicblock">Iterating over the </a><a
1320 href="#Instruction"><tt>Instruction</tt></a>s in a <a
1321 href="#BasicBlock"><tt>BasicBlock</tt></a>
1322</div>
1323
1324<div class="doc_text">
1325
1326<p>Just like when dealing with <tt>BasicBlock</tt>s in <tt>Function</tt>s, it's
1327easy to iterate over the individual instructions that make up
1328<tt>BasicBlock</tt>s. Here's a code snippet that prints out each instruction in
1329a <tt>BasicBlock</tt>:</p>
1330
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001331<div class="doc_code">
Chris Lattner55c04612005-03-06 06:00:13 +00001332<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001333// <i>blk is a pointer to a BasicBlock instance</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001334for (BasicBlock::iterator i = blk-&gt;begin(), e = blk-&gt;end(); i != e; ++i)
Bill Wendling82e2eea2006-10-11 18:00:22 +00001335 // <i>The next statement works since operator&lt;&lt;(ostream&amp;,...)</i>
1336 // <i>is overloaded for Instruction&amp;</i>
Bill Wendling832171c2006-12-07 20:04:42 +00001337 llvm::cerr &lt;&lt; *i &lt;&lt; "\n";
Chris Lattner55c04612005-03-06 06:00:13 +00001338</pre>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001339</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001340
1341<p>However, this isn't really the best way to print out the contents of a
1342<tt>BasicBlock</tt>! Since the ostream operators are overloaded for virtually
1343anything you'll care about, you could have just invoked the print routine on the
Bill Wendling832171c2006-12-07 20:04:42 +00001344basic block itself: <tt>llvm::cerr &lt;&lt; *blk &lt;&lt; "\n";</tt>.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001345
1346</div>
1347
1348<!-- _______________________________________________________________________ -->
Chris Lattner69bf8a92004-05-23 21:06:58 +00001349<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00001350 <a name="iterate_institer">Iterating over the </a><a
1351 href="#Instruction"><tt>Instruction</tt></a>s in a <a
1352 href="#Function"><tt>Function</tt></a>
1353</div>
1354
1355<div class="doc_text">
1356
1357<p>If you're finding that you commonly iterate over a <tt>Function</tt>'s
1358<tt>BasicBlock</tt>s and then that <tt>BasicBlock</tt>'s <tt>Instruction</tt>s,
1359<tt>InstIterator</tt> should be used instead. You'll need to include <a
1360href="/doxygen/InstIterator_8h-source.html"><tt>llvm/Support/InstIterator.h</tt></a>,
1361and then instantiate <tt>InstIterator</tt>s explicitly in your code. Here's a
Chris Lattner69bf8a92004-05-23 21:06:58 +00001362small example that shows how to dump all instructions in a function to the standard error stream:<p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001363
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001364<div class="doc_code">
1365<pre>
1366#include "<a href="/doxygen/InstIterator_8h-source.html">llvm/Support/InstIterator.h</a>"
1367
Bill Wendling82e2eea2006-10-11 18:00:22 +00001368// <i>F is a ptr to a Function instance</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001369for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
Bill Wendling832171c2006-12-07 20:04:42 +00001370 llvm::cerr &lt;&lt; *i &lt;&lt; "\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001371</pre>
1372</div>
1373
1374<p>Easy, isn't it? You can also use <tt>InstIterator</tt>s to fill a
Joel Stanleye7be6502002-09-09 15:50:33 +00001375worklist with its initial contents. For example, if you wanted to
Chris Lattner261efe92003-11-25 01:02:51 +00001376initialize a worklist to contain all instructions in a <tt>Function</tt>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001377F, all you would need to do is something like:</p>
1378
1379<div class="doc_code">
1380<pre>
1381std::set&lt;Instruction*&gt; worklist;
1382worklist.insert(inst_begin(F), inst_end(F));
1383</pre>
1384</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001385
1386<p>The STL set <tt>worklist</tt> would now contain all instructions in the
1387<tt>Function</tt> pointed to by F.</p>
1388
1389</div>
1390
1391<!-- _______________________________________________________________________ -->
1392<div class="doc_subsubsection">
1393 <a name="iterate_convert">Turning an iterator into a class pointer (and
1394 vice-versa)</a>
1395</div>
1396
1397<div class="doc_text">
1398
1399<p>Sometimes, it'll be useful to grab a reference (or pointer) to a class
Joel Stanley9b96c442002-09-06 21:55:13 +00001400instance when all you've got at hand is an iterator. Well, extracting
Chris Lattner69bf8a92004-05-23 21:06:58 +00001401a reference or a pointer from an iterator is very straight-forward.
Chris Lattner261efe92003-11-25 01:02:51 +00001402Assuming that <tt>i</tt> is a <tt>BasicBlock::iterator</tt> and <tt>j</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001403is a <tt>BasicBlock::const_iterator</tt>:</p>
1404
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001405<div class="doc_code">
1406<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001407Instruction&amp; inst = *i; // <i>Grab reference to instruction reference</i>
1408Instruction* pinst = &amp;*i; // <i>Grab pointer to instruction reference</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001409const Instruction&amp; inst = *j;
1410</pre>
1411</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001412
1413<p>However, the iterators you'll be working with in the LLVM framework are
1414special: they will automatically convert to a ptr-to-instance type whenever they
1415need to. Instead of dereferencing the iterator and then taking the address of
1416the result, you can simply assign the iterator to the proper pointer type and
1417you get the dereference and address-of operation as a result of the assignment
1418(behind the scenes, this is a result of overloading casting mechanisms). Thus
1419the last line of the last example,</p>
1420
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001421<div class="doc_code">
1422<pre>
1423Instruction* pinst = &amp;*i;
1424</pre>
1425</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001426
1427<p>is semantically equivalent to</p>
1428
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001429<div class="doc_code">
1430<pre>
1431Instruction* pinst = i;
1432</pre>
1433</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001434
Chris Lattner69bf8a92004-05-23 21:06:58 +00001435<p>It's also possible to turn a class pointer into the corresponding iterator,
1436and this is a constant time operation (very efficient). The following code
1437snippet illustrates use of the conversion constructors provided by LLVM
1438iterators. By using these, you can explicitly grab the iterator of something
1439without actually obtaining it via iteration over some structure:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001440
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001441<div class="doc_code">
1442<pre>
1443void printNextInstruction(Instruction* inst) {
1444 BasicBlock::iterator it(inst);
Bill Wendling82e2eea2006-10-11 18:00:22 +00001445 ++it; // <i>After this line, it refers to the instruction after *inst</i>
Bill Wendling832171c2006-12-07 20:04:42 +00001446 if (it != inst-&gt;getParent()-&gt;end()) llvm::cerr &lt;&lt; *it &lt;&lt; "\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001447}
1448</pre>
1449</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001450
Misha Brukman13fd15c2004-01-15 00:14:41 +00001451</div>
1452
1453<!--_______________________________________________________________________-->
1454<div class="doc_subsubsection">
1455 <a name="iterate_complex">Finding call sites: a slightly more complex
1456 example</a>
1457</div>
1458
1459<div class="doc_text">
1460
1461<p>Say that you're writing a FunctionPass and would like to count all the
1462locations in the entire module (that is, across every <tt>Function</tt>) where a
1463certain function (i.e., some <tt>Function</tt>*) is already in scope. As you'll
1464learn later, you may want to use an <tt>InstVisitor</tt> to accomplish this in a
Chris Lattner69bf8a92004-05-23 21:06:58 +00001465much more straight-forward manner, but this example will allow us to explore how
Misha Brukman13fd15c2004-01-15 00:14:41 +00001466you'd do it if you didn't have <tt>InstVisitor</tt> around. In pseudocode, this
1467is what we want to do:</p>
1468
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001469<div class="doc_code">
1470<pre>
1471initialize callCounter to zero
1472for each Function f in the Module
1473 for each BasicBlock b in f
1474 for each Instruction i in b
1475 if (i is a CallInst and calls the given function)
1476 increment callCounter
1477</pre>
1478</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001479
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001480<p>And the actual code is (remember, because we're writing a
Misha Brukman13fd15c2004-01-15 00:14:41 +00001481<tt>FunctionPass</tt>, our <tt>FunctionPass</tt>-derived class simply has to
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001482override the <tt>runOnFunction</tt> method):</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001483
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001484<div class="doc_code">
1485<pre>
1486Function* targetFunc = ...;
1487
1488class OurFunctionPass : public FunctionPass {
1489 public:
1490 OurFunctionPass(): callCounter(0) { }
1491
1492 virtual runOnFunction(Function&amp; F) {
1493 for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) {
1494 for (BasicBlock::iterator i = b-&gt;begin(); ie = b-&gt;end(); i != ie; ++i) {
1495 if (<a href="#CallInst">CallInst</a>* callInst = <a href="#isa">dyn_cast</a>&lt;<a
1496 href="#CallInst">CallInst</a>&gt;(&amp;*i)) {
Bill Wendling82e2eea2006-10-11 18:00:22 +00001497 // <i>We know we've encountered a call instruction, so we</i>
1498 // <i>need to determine if it's a call to the</i>
1499 // <i>function pointed to by m_func or not</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001500
1501 if (callInst-&gt;getCalledFunction() == targetFunc)
1502 ++callCounter;
1503 }
1504 }
1505 }
Bill Wendling82e2eea2006-10-11 18:00:22 +00001506 }
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001507
1508 private:
1509 unsigned callCounter;
1510};
1511</pre>
1512</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001513
1514</div>
1515
Brian Gaekef1972c62003-11-07 19:25:45 +00001516<!--_______________________________________________________________________-->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001517<div class="doc_subsubsection">
1518 <a name="calls_and_invokes">Treating calls and invokes the same way</a>
1519</div>
1520
1521<div class="doc_text">
1522
1523<p>You may have noticed that the previous example was a bit oversimplified in
1524that it did not deal with call sites generated by 'invoke' instructions. In
1525this, and in other situations, you may find that you want to treat
1526<tt>CallInst</tt>s and <tt>InvokeInst</tt>s the same way, even though their
1527most-specific common base class is <tt>Instruction</tt>, which includes lots of
1528less closely-related things. For these cases, LLVM provides a handy wrapper
1529class called <a
Reid Spencer05fe4b02006-03-14 05:39:39 +00001530href="http://llvm.org/doxygen/classllvm_1_1CallSite.html"><tt>CallSite</tt></a>.
Chris Lattner69bf8a92004-05-23 21:06:58 +00001531It is essentially a wrapper around an <tt>Instruction</tt> pointer, with some
1532methods that provide functionality common to <tt>CallInst</tt>s and
Misha Brukman13fd15c2004-01-15 00:14:41 +00001533<tt>InvokeInst</tt>s.</p>
1534
Chris Lattner69bf8a92004-05-23 21:06:58 +00001535<p>This class has "value semantics": it should be passed by value, not by
1536reference and it should not be dynamically allocated or deallocated using
1537<tt>operator new</tt> or <tt>operator delete</tt>. It is efficiently copyable,
1538assignable and constructable, with costs equivalents to that of a bare pointer.
1539If you look at its definition, it has only a single pointer member.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001540
1541</div>
1542
Chris Lattner1a3105b2002-09-09 05:49:39 +00001543<!--_______________________________________________________________________-->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001544<div class="doc_subsubsection">
1545 <a name="iterate_chains">Iterating over def-use &amp; use-def chains</a>
1546</div>
1547
1548<div class="doc_text">
1549
1550<p>Frequently, we might have an instance of the <a
Chris Lattner00815172007-01-04 22:01:45 +00001551href="/doxygen/classllvm_1_1Value.html">Value Class</a> and we want to
Misha Brukman384047f2004-06-03 23:29:12 +00001552determine which <tt>User</tt>s use the <tt>Value</tt>. The list of all
1553<tt>User</tt>s of a particular <tt>Value</tt> is called a <i>def-use</i> chain.
1554For example, let's say we have a <tt>Function*</tt> named <tt>F</tt> to a
1555particular function <tt>foo</tt>. Finding all of the instructions that
1556<i>use</i> <tt>foo</tt> is as simple as iterating over the <i>def-use</i> chain
1557of <tt>F</tt>:</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001558
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001559<div class="doc_code">
1560<pre>
1561Function* F = ...;
1562
Bill Wendling82e2eea2006-10-11 18:00:22 +00001563for (Value::use_iterator i = F-&gt;use_begin(), e = F-&gt;use_end(); i != e; ++i)
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001564 if (Instruction *Inst = dyn_cast&lt;Instruction&gt;(*i)) {
Bill Wendling832171c2006-12-07 20:04:42 +00001565 llvm::cerr &lt;&lt; "F is used in instruction:\n";
1566 llvm::cerr &lt;&lt; *Inst &lt;&lt; "\n";
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001567 }
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001568</pre>
1569</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001570
1571<p>Alternately, it's common to have an instance of the <a
Misha Brukman384047f2004-06-03 23:29:12 +00001572href="/doxygen/classllvm_1_1User.html">User Class</a> and need to know what
Misha Brukman13fd15c2004-01-15 00:14:41 +00001573<tt>Value</tt>s are used by it. The list of all <tt>Value</tt>s used by a
1574<tt>User</tt> is known as a <i>use-def</i> chain. Instances of class
1575<tt>Instruction</tt> are common <tt>User</tt>s, so we might want to iterate over
1576all of the values that a particular instruction uses (that is, the operands of
1577the particular <tt>Instruction</tt>):</p>
1578
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001579<div class="doc_code">
1580<pre>
1581Instruction* pi = ...;
1582
1583for (User::op_iterator i = pi-&gt;op_begin(), e = pi-&gt;op_end(); i != e; ++i) {
1584 Value* v = *i;
Bill Wendling82e2eea2006-10-11 18:00:22 +00001585 // <i>...</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001586}
1587</pre>
1588</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001589
Chris Lattner1a3105b2002-09-09 05:49:39 +00001590<!--
1591 def-use chains ("finding all users of"): Value::use_begin/use_end
1592 use-def chains ("finding all values used"): User::op_begin/op_end [op=operand]
Misha Brukman13fd15c2004-01-15 00:14:41 +00001593-->
1594
1595</div>
1596
1597<!-- ======================================================================= -->
1598<div class="doc_subsection">
1599 <a name="simplechanges">Making simple changes</a>
1600</div>
1601
1602<div class="doc_text">
1603
1604<p>There are some primitive transformation operations present in the LLVM
Joel Stanley753eb712002-09-11 22:32:24 +00001605infrastructure that are worth knowing about. When performing
Chris Lattner261efe92003-11-25 01:02:51 +00001606transformations, it's fairly common to manipulate the contents of basic
1607blocks. This section describes some of the common methods for doing so
Misha Brukman13fd15c2004-01-15 00:14:41 +00001608and gives example code.</p>
1609
1610</div>
1611
Chris Lattner261efe92003-11-25 01:02:51 +00001612<!--_______________________________________________________________________-->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001613<div class="doc_subsubsection">
1614 <a name="schanges_creating">Creating and inserting new
1615 <tt>Instruction</tt>s</a>
1616</div>
1617
1618<div class="doc_text">
1619
1620<p><i>Instantiating Instructions</i></p>
1621
Chris Lattner69bf8a92004-05-23 21:06:58 +00001622<p>Creation of <tt>Instruction</tt>s is straight-forward: simply call the
Misha Brukman13fd15c2004-01-15 00:14:41 +00001623constructor for the kind of instruction to instantiate and provide the necessary
1624parameters. For example, an <tt>AllocaInst</tt> only <i>requires</i> a
1625(const-ptr-to) <tt>Type</tt>. Thus:</p>
1626
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001627<div class="doc_code">
1628<pre>
1629AllocaInst* ai = new AllocaInst(Type::IntTy);
1630</pre>
1631</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001632
1633<p>will create an <tt>AllocaInst</tt> instance that represents the allocation of
1634one integer in the current stack frame, at runtime. Each <tt>Instruction</tt>
1635subclass is likely to have varying default parameters which change the semantics
1636of the instruction, so refer to the <a
Misha Brukman31ca1de2004-06-03 23:35:54 +00001637href="/doxygen/classllvm_1_1Instruction.html">doxygen documentation for the subclass of
Misha Brukman13fd15c2004-01-15 00:14:41 +00001638Instruction</a> that you're interested in instantiating.</p>
1639
1640<p><i>Naming values</i></p>
1641
1642<p>It is very useful to name the values of instructions when you're able to, as
1643this facilitates the debugging of your transformations. If you end up looking
1644at generated LLVM machine code, you definitely want to have logical names
1645associated with the results of instructions! By supplying a value for the
1646<tt>Name</tt> (default) parameter of the <tt>Instruction</tt> constructor, you
1647associate a logical name with the result of the instruction's execution at
1648runtime. For example, say that I'm writing a transformation that dynamically
1649allocates space for an integer on the stack, and that integer is going to be
1650used as some kind of index by some other code. To accomplish this, I place an
1651<tt>AllocaInst</tt> at the first point in the first <tt>BasicBlock</tt> of some
1652<tt>Function</tt>, and I'm intending to use it within the same
1653<tt>Function</tt>. I might do:</p>
1654
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001655<div class="doc_code">
1656<pre>
1657AllocaInst* pa = new AllocaInst(Type::IntTy, 0, "indexLoc");
1658</pre>
1659</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001660
1661<p>where <tt>indexLoc</tt> is now the logical name of the instruction's
1662execution value, which is a pointer to an integer on the runtime stack.</p>
1663
1664<p><i>Inserting instructions</i></p>
1665
1666<p>There are essentially two ways to insert an <tt>Instruction</tt>
1667into an existing sequence of instructions that form a <tt>BasicBlock</tt>:</p>
1668
Joel Stanley9dd1ad62002-09-18 03:17:23 +00001669<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001670 <li>Insertion into an explicit instruction list
1671
1672 <p>Given a <tt>BasicBlock* pb</tt>, an <tt>Instruction* pi</tt> within that
1673 <tt>BasicBlock</tt>, and a newly-created instruction we wish to insert
1674 before <tt>*pi</tt>, we do the following: </p>
1675
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001676<div class="doc_code">
1677<pre>
1678BasicBlock *pb = ...;
1679Instruction *pi = ...;
1680Instruction *newInst = new Instruction(...);
1681
Bill Wendling82e2eea2006-10-11 18:00:22 +00001682pb-&gt;getInstList().insert(pi, newInst); // <i>Inserts newInst before pi in pb</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001683</pre>
1684</div>
Alkis Evlogimenos9a5dc4f2004-05-27 00:57:51 +00001685
1686 <p>Appending to the end of a <tt>BasicBlock</tt> is so common that
1687 the <tt>Instruction</tt> class and <tt>Instruction</tt>-derived
1688 classes provide constructors which take a pointer to a
1689 <tt>BasicBlock</tt> to be appended to. For example code that
1690 looked like: </p>
1691
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001692<div class="doc_code">
1693<pre>
1694BasicBlock *pb = ...;
1695Instruction *newInst = new Instruction(...);
1696
Bill Wendling82e2eea2006-10-11 18:00:22 +00001697pb-&gt;getInstList().push_back(newInst); // <i>Appends newInst to pb</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001698</pre>
1699</div>
Alkis Evlogimenos9a5dc4f2004-05-27 00:57:51 +00001700
1701 <p>becomes: </p>
1702
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001703<div class="doc_code">
1704<pre>
1705BasicBlock *pb = ...;
1706Instruction *newInst = new Instruction(..., pb);
1707</pre>
1708</div>
Alkis Evlogimenos9a5dc4f2004-05-27 00:57:51 +00001709
1710 <p>which is much cleaner, especially if you are creating
1711 long instruction streams.</p></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001712
1713 <li>Insertion into an implicit instruction list
1714
1715 <p><tt>Instruction</tt> instances that are already in <tt>BasicBlock</tt>s
1716 are implicitly associated with an existing instruction list: the instruction
1717 list of the enclosing basic block. Thus, we could have accomplished the same
1718 thing as the above code without being given a <tt>BasicBlock</tt> by doing:
1719 </p>
1720
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001721<div class="doc_code">
1722<pre>
1723Instruction *pi = ...;
1724Instruction *newInst = new Instruction(...);
1725
1726pi-&gt;getParent()-&gt;getInstList().insert(pi, newInst);
1727</pre>
1728</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001729
1730 <p>In fact, this sequence of steps occurs so frequently that the
1731 <tt>Instruction</tt> class and <tt>Instruction</tt>-derived classes provide
1732 constructors which take (as a default parameter) a pointer to an
1733 <tt>Instruction</tt> which the newly-created <tt>Instruction</tt> should
1734 precede. That is, <tt>Instruction</tt> constructors are capable of
1735 inserting the newly-created instance into the <tt>BasicBlock</tt> of a
1736 provided instruction, immediately before that instruction. Using an
1737 <tt>Instruction</tt> constructor with a <tt>insertBefore</tt> (default)
1738 parameter, the above code becomes:</p>
1739
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001740<div class="doc_code">
1741<pre>
1742Instruction* pi = ...;
1743Instruction* newInst = new Instruction(..., pi);
1744</pre>
1745</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001746
1747 <p>which is much cleaner, especially if you're creating a lot of
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001748 instructions and adding them to <tt>BasicBlock</tt>s.</p></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001749</ul>
1750
1751</div>
1752
1753<!--_______________________________________________________________________-->
1754<div class="doc_subsubsection">
1755 <a name="schanges_deleting">Deleting <tt>Instruction</tt>s</a>
1756</div>
1757
1758<div class="doc_text">
1759
1760<p>Deleting an instruction from an existing sequence of instructions that form a
Chris Lattner69bf8a92004-05-23 21:06:58 +00001761<a href="#BasicBlock"><tt>BasicBlock</tt></a> is very straight-forward. First,
Misha Brukman13fd15c2004-01-15 00:14:41 +00001762you must have a pointer to the instruction that you wish to delete. Second, you
1763need to obtain the pointer to that instruction's basic block. You use the
1764pointer to the basic block to get its list of instructions and then use the
1765erase function to remove your instruction. For example:</p>
1766
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001767<div class="doc_code">
1768<pre>
1769<a href="#Instruction">Instruction</a> *I = .. ;
1770<a href="#BasicBlock">BasicBlock</a> *BB = I-&gt;getParent();
1771
1772BB-&gt;getInstList().erase(I);
1773</pre>
1774</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001775
1776</div>
1777
1778<!--_______________________________________________________________________-->
1779<div class="doc_subsubsection">
1780 <a name="schanges_replacing">Replacing an <tt>Instruction</tt> with another
1781 <tt>Value</tt></a>
1782</div>
1783
1784<div class="doc_text">
1785
1786<p><i>Replacing individual instructions</i></p>
1787
1788<p>Including "<a href="/doxygen/BasicBlockUtils_8h-source.html">llvm/Transforms/Utils/BasicBlockUtils.h</a>"
Chris Lattner261efe92003-11-25 01:02:51 +00001789permits use of two very useful replace functions: <tt>ReplaceInstWithValue</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001790and <tt>ReplaceInstWithInst</tt>.</p>
1791
Chris Lattner261efe92003-11-25 01:02:51 +00001792<h4><a name="schanges_deleting">Deleting <tt>Instruction</tt>s</a></h4>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001793
Chris Lattner261efe92003-11-25 01:02:51 +00001794<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001795 <li><tt>ReplaceInstWithValue</tt>
1796
1797 <p>This function replaces all uses (within a basic block) of a given
1798 instruction with a value, and then removes the original instruction. The
1799 following example illustrates the replacement of the result of a particular
Chris Lattner58360822005-01-17 00:12:04 +00001800 <tt>AllocaInst</tt> that allocates memory for a single integer with a null
Misha Brukman13fd15c2004-01-15 00:14:41 +00001801 pointer to an integer.</p>
1802
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001803<div class="doc_code">
1804<pre>
1805AllocaInst* instToReplace = ...;
1806BasicBlock::iterator ii(instToReplace);
1807
1808ReplaceInstWithValue(instToReplace-&gt;getParent()-&gt;getInstList(), ii,
1809 Constant::getNullValue(PointerType::get(Type::IntTy)));
1810</pre></div></li>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001811
1812 <li><tt>ReplaceInstWithInst</tt>
1813
1814 <p>This function replaces a particular instruction with another
1815 instruction. The following example illustrates the replacement of one
1816 <tt>AllocaInst</tt> with another.</p>
1817
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001818<div class="doc_code">
1819<pre>
1820AllocaInst* instToReplace = ...;
1821BasicBlock::iterator ii(instToReplace);
1822
1823ReplaceInstWithInst(instToReplace-&gt;getParent()-&gt;getInstList(), ii,
1824 new AllocaInst(Type::IntTy, 0, "ptrToReplacedInt"));
1825</pre></div></li>
Chris Lattner261efe92003-11-25 01:02:51 +00001826</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00001827
1828<p><i>Replacing multiple uses of <tt>User</tt>s and <tt>Value</tt>s</i></p>
1829
1830<p>You can use <tt>Value::replaceAllUsesWith</tt> and
1831<tt>User::replaceUsesOfWith</tt> to change more than one use at a time. See the
Chris Lattner00815172007-01-04 22:01:45 +00001832doxygen documentation for the <a href="/doxygen/classllvm_1_1Value.html">Value Class</a>
Misha Brukman384047f2004-06-03 23:29:12 +00001833and <a href="/doxygen/classllvm_1_1User.html">User Class</a>, respectively, for more
Misha Brukman13fd15c2004-01-15 00:14:41 +00001834information.</p>
1835
1836<!-- Value::replaceAllUsesWith User::replaceUsesOfWith Point out:
1837include/llvm/Transforms/Utils/ especially BasicBlockUtils.h with:
1838ReplaceInstWithValue, ReplaceInstWithInst -->
1839
1840</div>
1841
Chris Lattner9355b472002-09-06 02:50:58 +00001842<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +00001843<div class="doc_section">
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001844 <a name="advanced">Advanced Topics</a>
1845</div>
1846<!-- *********************************************************************** -->
1847
1848<div class="doc_text">
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001849<p>
1850This section describes some of the advanced or obscure API's that most clients
1851do not need to be aware of. These API's tend manage the inner workings of the
1852LLVM system, and only need to be accessed in unusual circumstances.
1853</p>
1854</div>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001855
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001856<!-- ======================================================================= -->
1857<div class="doc_subsection">
1858 <a name="TypeResolve">LLVM Type Resolution</a>
1859</div>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00001860
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001861<div class="doc_text">
1862
1863<p>
1864The LLVM type system has a very simple goal: allow clients to compare types for
1865structural equality with a simple pointer comparison (aka a shallow compare).
1866This goal makes clients much simpler and faster, and is used throughout the LLVM
1867system.
1868</p>
1869
1870<p>
1871Unfortunately achieving this goal is not a simple matter. In particular,
1872recursive types and late resolution of opaque types makes the situation very
1873difficult to handle. Fortunately, for the most part, our implementation makes
1874most clients able to be completely unaware of the nasty internal details. The
1875primary case where clients are exposed to the inner workings of it are when
1876building a recursive type. In addition to this case, the LLVM bytecode reader,
1877assembly parser, and linker also have to be aware of the inner workings of this
1878system.
1879</p>
1880
Chris Lattner0f876db2005-04-25 15:47:57 +00001881<p>
1882For our purposes below, we need three concepts. First, an "Opaque Type" is
1883exactly as defined in the <a href="LangRef.html#t_opaque">language
1884reference</a>. Second an "Abstract Type" is any type which includes an
Reid Spencer06565dc2007-01-12 17:11:23 +00001885opaque type as part of its type graph (for example "<tt>{ opaque, i32 }</tt>").
1886Third, a concrete type is a type that is not an abstract type (e.g. "<tt>{ i32,
Chris Lattner0f876db2005-04-25 15:47:57 +00001887float }</tt>").
1888</p>
1889
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001890</div>
1891
1892<!-- ______________________________________________________________________ -->
1893<div class="doc_subsubsection">
1894 <a name="BuildRecType">Basic Recursive Type Construction</a>
1895</div>
1896
1897<div class="doc_text">
1898
1899<p>
1900Because the most common question is "how do I build a recursive type with LLVM",
1901we answer it now and explain it as we go. Here we include enough to cause this
1902to be emitted to an output .ll file:
1903</p>
1904
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001905<div class="doc_code">
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001906<pre>
Reid Spencer06565dc2007-01-12 17:11:23 +00001907%mylist = type { %mylist*, i32 }
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001908</pre>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001909</div>
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001910
1911<p>
1912To build this, use the following LLVM APIs:
1913</p>
1914
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001915<div class="doc_code">
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001916<pre>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001917// <i>Create the initial outer struct</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001918<a href="#PATypeHolder">PATypeHolder</a> StructTy = OpaqueType::get();
1919std::vector&lt;const Type*&gt; Elts;
1920Elts.push_back(PointerType::get(StructTy));
1921Elts.push_back(Type::IntTy);
1922StructType *NewSTy = StructType::get(Elts);
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001923
Reid Spencer06565dc2007-01-12 17:11:23 +00001924// <i>At this point, NewSTy = "{ opaque*, i32 }". Tell VMCore that</i>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001925// <i>the struct and the opaque type are actually the same.</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001926cast&lt;OpaqueType&gt;(StructTy.get())-&gt;<a href="#refineAbstractTypeTo">refineAbstractTypeTo</a>(NewSTy);
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001927
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001928// <i>NewSTy is potentially invalidated, but StructTy (a <a href="#PATypeHolder">PATypeHolder</a>) is</i>
Bill Wendling82e2eea2006-10-11 18:00:22 +00001929// <i>kept up-to-date</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001930NewSTy = cast&lt;StructType&gt;(StructTy.get());
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001931
Bill Wendling82e2eea2006-10-11 18:00:22 +00001932// <i>Add a name for the type to the module symbol table (optional)</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001933MyModule-&gt;addTypeName("mylist", NewSTy);
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001934</pre>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00001935</div>
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001936
1937<p>
1938This code shows the basic approach used to build recursive types: build a
1939non-recursive type using 'opaque', then use type unification to close the cycle.
1940The type unification step is performed by the <tt><a
Chris Lattneraff26d12007-02-03 03:06:52 +00001941href="#refineAbstractTypeTo">refineAbstractTypeTo</a></tt> method, which is
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001942described next. After that, we describe the <a
1943href="#PATypeHolder">PATypeHolder class</a>.
1944</p>
1945
1946</div>
1947
1948<!-- ______________________________________________________________________ -->
1949<div class="doc_subsubsection">
1950 <a name="refineAbstractTypeTo">The <tt>refineAbstractTypeTo</tt> method</a>
1951</div>
1952
1953<div class="doc_text">
1954<p>
1955The <tt>refineAbstractTypeTo</tt> method starts the type unification process.
1956While this method is actually a member of the DerivedType class, it is most
1957often used on OpaqueType instances. Type unification is actually a recursive
1958process. After unification, types can become structurally isomorphic to
1959existing types, and all duplicates are deleted (to preserve pointer equality).
1960</p>
1961
1962<p>
1963In the example above, the OpaqueType object is definitely deleted.
Reid Spencer06565dc2007-01-12 17:11:23 +00001964Additionally, if there is an "{ \2*, i32}" type already created in the system,
Chris Lattnerf1b200b2005-04-23 17:27:36 +00001965the pointer and struct type created are <b>also</b> deleted. Obviously whenever
1966a type is deleted, any "Type*" pointers in the program are invalidated. As
1967such, it is safest to avoid having <i>any</i> "Type*" pointers to abstract types
1968live across a call to <tt>refineAbstractTypeTo</tt> (note that non-abstract
1969types can never move or be deleted). To deal with this, the <a
1970href="#PATypeHolder">PATypeHolder</a> class is used to maintain a stable
1971reference to a possibly refined type, and the <a
1972href="#AbstractTypeUser">AbstractTypeUser</a> class is used to update more
1973complex datastructures.
1974</p>
1975
1976</div>
1977
1978<!-- ______________________________________________________________________ -->
1979<div class="doc_subsubsection">
1980 <a name="PATypeHolder">The PATypeHolder Class</a>
1981</div>
1982
1983<div class="doc_text">
1984<p>
1985PATypeHolder is a form of a "smart pointer" for Type objects. When VMCore
1986happily goes about nuking types that become isomorphic to existing types, it
1987automatically updates all PATypeHolder objects to point to the new type. In the
1988example above, this allows the code to maintain a pointer to the resultant
1989resolved recursive type, even though the Type*'s are potentially invalidated.
1990</p>
1991
1992<p>
1993PATypeHolder is an extremely light-weight object that uses a lazy union-find
1994implementation to update pointers. For example the pointer from a Value to its
1995Type is maintained by PATypeHolder objects.
1996</p>
1997
1998</div>
1999
2000<!-- ______________________________________________________________________ -->
2001<div class="doc_subsubsection">
2002 <a name="AbstractTypeUser">The AbstractTypeUser Class</a>
2003</div>
2004
2005<div class="doc_text">
2006
2007<p>
2008Some data structures need more to perform more complex updates when types get
2009resolved. The <a href="#SymbolTable">SymbolTable</a> class, for example, needs
2010move and potentially merge type planes in its representation when a pointer
2011changes.</p>
2012
2013<p>
2014To support this, a class can derive from the AbstractTypeUser class. This class
2015allows it to get callbacks when certain types are resolved. To register to get
2016callbacks for a particular type, the DerivedType::{add/remove}AbstractTypeUser
Chris Lattner0f876db2005-04-25 15:47:57 +00002017methods can be called on a type. Note that these methods only work for <i>
Reid Spencer06565dc2007-01-12 17:11:23 +00002018 abstract</i> types. Concrete types (those that do not include any opaque
2019objects) can never be refined.
Chris Lattnerf1b200b2005-04-23 17:27:36 +00002020</p>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002021</div>
2022
2023
2024<!-- ======================================================================= -->
2025<div class="doc_subsection">
2026 <a name="SymbolTable">The <tt>SymbolTable</tt> class</a>
2027</div>
Chris Lattnerf1b200b2005-04-23 17:27:36 +00002028
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002029<div class="doc_text">
2030<p>This class provides a symbol table that the <a
2031href="#Function"><tt>Function</tt></a> and <a href="#Module">
2032<tt>Module</tt></a> classes use for naming definitions. The symbol table can
Reid Spencera6362242007-01-07 00:41:39 +00002033provide a name for any <a href="#Value"><tt>Value</tt></a>.
2034<tt>SymbolTable</tt> is an abstract data type. It hides the data it contains
2035and provides access to it through a controlled interface.</p>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002036
Reid Spencera6362242007-01-07 00:41:39 +00002037<p>Note that the <tt>SymbolTable</tt> class should not be directly accessed
2038by most clients. It should only be used when iteration over the symbol table
2039names themselves are required, which is very special purpose. Note that not
2040all LLVM
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002041<a href="#Value">Value</a>s have names, and those without names (i.e. they have
2042an empty name) do not exist in the symbol table.
2043</p>
2044
2045<p>To use the <tt>SymbolTable</tt> well, you need to understand the
2046structure of the information it holds. The class contains two
2047<tt>std::map</tt> objects. The first, <tt>pmap</tt>, is a map of
2048<tt>Type*</tt> to maps of name (<tt>std::string</tt>) to <tt>Value*</tt>.
Reid Spencera6362242007-01-07 00:41:39 +00002049Thus, Values are stored in two-dimensions and accessed by <tt>Type</tt> and
2050name.</p>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002051
2052<p>The interface of this class provides three basic types of operations:
2053<ol>
2054 <li><em>Accessors</em>. Accessors provide read-only access to information
2055 such as finding a value for a name with the
2056 <a href="#SymbolTable_lookup">lookup</a> method.</li>
2057 <li><em>Mutators</em>. Mutators allow the user to add information to the
2058 <tt>SymbolTable</tt> with methods like
2059 <a href="#SymbolTable_insert"><tt>insert</tt></a>.</li>
2060 <li><em>Iterators</em>. Iterators allow the user to traverse the content
2061 of the symbol table in well defined ways, such as the method
Reid Spencera6362242007-01-07 00:41:39 +00002062 <a href="#SymbolTable_plane_begin"><tt>plane_begin</tt></a>.</li>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002063</ol>
2064
2065<h3>Accessors</h3>
2066<dl>
2067 <dt><tt>Value* lookup(const Type* Ty, const std::string&amp; name) const</tt>:
2068 </dt>
2069 <dd>The <tt>lookup</tt> method searches the type plane given by the
2070 <tt>Ty</tt> parameter for a <tt>Value</tt> with the provided <tt>name</tt>.
2071 If a suitable <tt>Value</tt> is not found, null is returned.</dd>
2072
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002073 <dt><tt>bool isEmpty() const</tt>:</dt>
2074 <dd>This function returns true if both the value and types maps are
2075 empty</dd>
2076</dl>
2077
2078<h3>Mutators</h3>
2079<dl>
2080 <dt><tt>void insert(Value *Val)</tt>:</dt>
2081 <dd>This method adds the provided value to the symbol table. The Value must
2082 have both a name and a type which are extracted and used to place the value
2083 in the correct type plane under the value's name.</dd>
2084
2085 <dt><tt>void insert(const std::string&amp; Name, Value *Val)</tt>:</dt>
2086 <dd> Inserts a constant or type into the symbol table with the specified
2087 name. There can be a many to one mapping between names and constants
2088 or types.</dd>
2089
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002090 <dt><tt>void remove(Value* Val)</tt>:</dt>
2091 <dd> This method removes a named value from the symbol table. The
2092 type and name of the Value are extracted from \p N and used to
2093 lookup the Value in the correct type plane. If the Value is
2094 not in the symbol table, this method silently ignores the
2095 request.</dd>
2096
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002097 <dt><tt>Value* remove(const std::string&amp; Name, Value *Val)</tt>:</dt>
2098 <dd> Remove a constant or type with the specified name from the
2099 symbol table.</dd>
2100
Reid Spencera6362242007-01-07 00:41:39 +00002101 <dt><tt>Value *remove(const value_iterator&amp; It)</tt>:</dt>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002102 <dd> Removes a specific value from the symbol table.
2103 Returns the removed value.</dd>
2104
2105 <dt><tt>bool strip()</tt>:</dt>
2106 <dd> This method will strip the symbol table of its names leaving
2107 the type and values. </dd>
2108
2109 <dt><tt>void clear()</tt>:</dt>
2110 <dd>Empty the symbol table completely.</dd>
2111</dl>
2112
2113<h3>Iteration</h3>
2114<p>The following functions describe three types of iterators you can obtain
2115the beginning or end of the sequence for both const and non-const. It is
2116important to keep track of the different kinds of iterators. There are
2117three idioms worth pointing out:</p>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002118
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002119<table>
2120 <tr><th>Units</th><th>Iterator</th><th>Idiom</th></tr>
2121 <tr>
2122 <td align="left">Planes Of name/Value maps</td><td>PI</td>
2123 <td align="left"><pre><tt>
2124for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
2125 PE = ST.plane_end(); PI != PE; ++PI ) {
Bill Wendling82e2eea2006-10-11 18:00:22 +00002126 PI-&gt;first // <i>This is the Type* of the plane</i>
2127 PI-&gt;second // <i>This is the SymbolTable::ValueMap of name/Value pairs</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002128}
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002129 </tt></pre></td>
2130 </tr>
2131 <tr>
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002132 <td align="left">name/Value pairs in a plane</td><td>VI</td>
2133 <td align="left"><pre><tt>
2134for (SymbolTable::value_const_iterator VI = ST.value_begin(SomeType),
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002135 VE = ST.value_end(SomeType); VI != VE; ++VI ) {
Bill Wendling82e2eea2006-10-11 18:00:22 +00002136 VI-&gt;first // <i>This is the name of the Value</i>
2137 VI-&gt;second // <i>This is the Value* value associated with the name</i>
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002138}
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002139 </tt></pre></td>
2140 </tr>
2141</table>
2142
2143<p>Using the recommended iterator names and idioms will help you avoid
2144making mistakes. Of particular note, make sure that whenever you use
2145value_begin(SomeType) that you always compare the resulting iterator
2146with value_end(SomeType) not value_end(SomeOtherType) or else you
2147will loop infinitely.</p>
2148
2149<dl>
2150
2151 <dt><tt>plane_iterator plane_begin()</tt>:</dt>
2152 <dd>Get an iterator that starts at the beginning of the type planes.
2153 The iterator will iterate over the Type/ValueMap pairs in the
2154 type planes. </dd>
2155
2156 <dt><tt>plane_const_iterator plane_begin() const</tt>:</dt>
2157 <dd>Get a const_iterator that starts at the beginning of the type
2158 planes. The iterator will iterate over the Type/ValueMap pairs
2159 in the type planes. </dd>
2160
2161 <dt><tt>plane_iterator plane_end()</tt>:</dt>
2162 <dd>Get an iterator at the end of the type planes. This serves as
2163 the marker for end of iteration over the type planes.</dd>
2164
2165 <dt><tt>plane_const_iterator plane_end() const</tt>:</dt>
2166 <dd>Get a const_iterator at the end of the type planes. This serves as
2167 the marker for end of iteration over the type planes.</dd>
2168
2169 <dt><tt>value_iterator value_begin(const Type *Typ)</tt>:</dt>
2170 <dd>Get an iterator that starts at the beginning of a type plane.
2171 The iterator will iterate over the name/value pairs in the type plane.
2172 Note: The type plane must already exist before using this.</dd>
2173
2174 <dt><tt>value_const_iterator value_begin(const Type *Typ) const</tt>:</dt>
2175 <dd>Get a const_iterator that starts at the beginning of a type plane.
2176 The iterator will iterate over the name/value pairs in the type plane.
2177 Note: The type plane must already exist before using this.</dd>
2178
2179 <dt><tt>value_iterator value_end(const Type *Typ)</tt>:</dt>
2180 <dd>Get an iterator to the end of a type plane. This serves as the marker
2181 for end of iteration of the type plane.
2182 Note: The type plane must already exist before using this.</dd>
2183
2184 <dt><tt>value_const_iterator value_end(const Type *Typ) const</tt>:</dt>
2185 <dd>Get a const_iterator to the end of a type plane. This serves as the
2186 marker for end of iteration of the type plane.
2187 Note: the type plane must already exist before using this.</dd>
2188
Chris Lattnerd9d6e102005-04-23 16:10:52 +00002189 <dt><tt>plane_const_iterator find(const Type* Typ ) const</tt>:</dt>
2190 <dd>This method returns a plane_const_iterator for iteration over
2191 the type planes starting at a specific plane, given by \p Ty.</dd>
2192
2193 <dt><tt>plane_iterator find( const Type* Typ </tt>:</dt>
2194 <dd>This method returns a plane_iterator for iteration over the
2195 type planes starting at a specific plane, given by \p Ty.</dd>
2196
2197</dl>
2198</div>
2199
2200
2201
2202<!-- *********************************************************************** -->
2203<div class="doc_section">
Misha Brukman13fd15c2004-01-15 00:14:41 +00002204 <a name="coreclasses">The Core LLVM Class Hierarchy Reference </a>
2205</div>
2206<!-- *********************************************************************** -->
2207
2208<div class="doc_text">
Reid Spencer303c4b42007-01-12 17:26:25 +00002209<p><tt>#include "<a href="/doxygen/Type_8h-source.html">llvm/Type.h</a>"</tt>
2210<br>doxygen info: <a href="/doxygen/classllvm_1_1Type.html">Type Class</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002211
2212<p>The Core LLVM classes are the primary means of representing the program
Chris Lattner261efe92003-11-25 01:02:51 +00002213being inspected or transformed. The core LLVM classes are defined in
2214header files in the <tt>include/llvm/</tt> directory, and implemented in
Misha Brukman13fd15c2004-01-15 00:14:41 +00002215the <tt>lib/VMCore</tt> directory.</p>
2216
2217</div>
2218
2219<!-- ======================================================================= -->
2220<div class="doc_subsection">
Reid Spencer303c4b42007-01-12 17:26:25 +00002221 <a name="Type">The <tt>Type</tt> class and Derived Types</a>
2222</div>
2223
2224<div class="doc_text">
2225
2226 <p><tt>Type</tt> is a superclass of all type classes. Every <tt>Value</tt> has
2227 a <tt>Type</tt>. <tt>Type</tt> cannot be instantiated directly but only
2228 through its subclasses. Certain primitive types (<tt>VoidType</tt>,
2229 <tt>LabelType</tt>, <tt>FloatType</tt> and <tt>DoubleType</tt>) have hidden
2230 subclasses. They are hidden because they offer no useful functionality beyond
2231 what the <tt>Type</tt> class offers except to distinguish themselves from
2232 other subclasses of <tt>Type</tt>.</p>
2233 <p>All other types are subclasses of <tt>DerivedType</tt>. Types can be
2234 named, but this is not a requirement. There exists exactly
2235 one instance of a given shape at any one time. This allows type equality to
2236 be performed with address equality of the Type Instance. That is, given two
2237 <tt>Type*</tt> values, the types are identical if the pointers are identical.
2238 </p>
2239</div>
2240
2241<!-- _______________________________________________________________________ -->
2242<div class="doc_subsubsection">
2243 <a name="m_Value">Important Public Methods</a>
2244</div>
2245
2246<div class="doc_text">
2247
2248<ul>
Chris Lattner8f79df32007-01-15 01:55:32 +00002249 <li><tt>bool isInteger() const</tt>: Returns true for any integer type.</li>
Reid Spencer303c4b42007-01-12 17:26:25 +00002250
2251 <li><tt>bool isFloatingPoint()</tt>: Return true if this is one of the two
2252 floating point types.</li>
2253
2254 <li><tt>bool isAbstract()</tt>: Return true if the type is abstract (contains
2255 an OpaqueType anywhere in its definition).</li>
2256
2257 <li><tt>bool isSized()</tt>: Return true if the type has known size. Things
2258 that don't have a size are abstract types, labels and void.</li>
2259
2260</ul>
2261</div>
2262
2263<!-- _______________________________________________________________________ -->
2264<div class="doc_subsubsection">
2265 <a name="m_Value">Important Derived Types</a>
2266</div>
2267<div class="doc_text">
2268<dl>
2269 <dt><tt>IntegerType</tt></dt>
2270 <dd>Subclass of DerivedType that represents integer types of any bit width.
2271 Any bit width between <tt>IntegerType::MIN_INT_BITS</tt> (1) and
2272 <tt>IntegerType::MAX_INT_BITS</tt> (~8 million) can be represented.
2273 <ul>
2274 <li><tt>static const IntegerType* get(unsigned NumBits)</tt>: get an integer
2275 type of a specific bit width.</li>
2276 <li><tt>unsigned getBitWidth() const</tt>: Get the bit width of an integer
2277 type.</li>
2278 </ul>
2279 </dd>
2280 <dt><tt>SequentialType</tt></dt>
2281 <dd>This is subclassed by ArrayType and PointerType
2282 <ul>
2283 <li><tt>const Type * getElementType() const</tt>: Returns the type of each
2284 of the elements in the sequential type. </li>
2285 </ul>
2286 </dd>
2287 <dt><tt>ArrayType</tt></dt>
2288 <dd>This is a subclass of SequentialType and defines the interface for array
2289 types.
2290 <ul>
2291 <li><tt>unsigned getNumElements() const</tt>: Returns the number of
2292 elements in the array. </li>
2293 </ul>
2294 </dd>
2295 <dt><tt>PointerType</tt></dt>
Chris Lattner302da1e2007-02-03 03:05:57 +00002296 <dd>Subclass of SequentialType for pointer types.</dd>
Reid Spencer303c4b42007-01-12 17:26:25 +00002297 <dt><tt>PackedType</tt></dt>
2298 <dd>Subclass of SequentialType for packed (vector) types. A
2299 packed type is similar to an ArrayType but is distinguished because it is
2300 a first class type wherease ArrayType is not. Packed types are used for
2301 vector operations and are usually small vectors of of an integer or floating
2302 point type.</dd>
2303 <dt><tt>StructType</tt></dt>
2304 <dd>Subclass of DerivedTypes for struct types.</dd>
2305 <dt><tt>FunctionType</tt></dt>
2306 <dd>Subclass of DerivedTypes for function types.
2307 <ul>
2308 <li><tt>bool isVarArg() const</tt>: Returns true if its a vararg
2309 function</li>
2310 <li><tt> const Type * getReturnType() const</tt>: Returns the
2311 return type of the function.</li>
2312 <li><tt>const Type * getParamType (unsigned i)</tt>: Returns
2313 the type of the ith parameter.</li>
2314 <li><tt> const unsigned getNumParams() const</tt>: Returns the
2315 number of formal parameters.</li>
2316 </ul>
2317 </dd>
2318 <dt><tt>OpaqueType</tt></dt>
2319 <dd>Sublcass of DerivedType for abstract types. This class
2320 defines no content and is used as a placeholder for some other type. Note
2321 that OpaqueType is used (temporarily) during type resolution for forward
2322 references of types. Once the referenced type is resolved, the OpaqueType
2323 is replaced with the actual type. OpaqueType can also be used for data
2324 abstraction. At link time opaque types can be resolved to actual types
2325 of the same name.</dd>
2326</dl>
2327</div>
2328
2329<!-- ======================================================================= -->
2330<div class="doc_subsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00002331 <a name="Value">The <tt>Value</tt> class</a>
2332</div>
2333
2334<div>
2335
2336<p><tt>#include "<a href="/doxygen/Value_8h-source.html">llvm/Value.h</a>"</tt>
2337<br>
Chris Lattner00815172007-01-04 22:01:45 +00002338doxygen info: <a href="/doxygen/classllvm_1_1Value.html">Value Class</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002339
2340<p>The <tt>Value</tt> class is the most important class in the LLVM Source
2341base. It represents a typed value that may be used (among other things) as an
2342operand to an instruction. There are many different types of <tt>Value</tt>s,
2343such as <a href="#Constant"><tt>Constant</tt></a>s,<a
2344href="#Argument"><tt>Argument</tt></a>s. Even <a
2345href="#Instruction"><tt>Instruction</tt></a>s and <a
2346href="#Function"><tt>Function</tt></a>s are <tt>Value</tt>s.</p>
2347
2348<p>A particular <tt>Value</tt> may be used many times in the LLVM representation
2349for a program. For example, an incoming argument to a function (represented
2350with an instance of the <a href="#Argument">Argument</a> class) is "used" by
2351every instruction in the function that references the argument. To keep track
2352of this relationship, the <tt>Value</tt> class keeps a list of all of the <a
2353href="#User"><tt>User</tt></a>s that is using it (the <a
2354href="#User"><tt>User</tt></a> class is a base class for all nodes in the LLVM
2355graph that can refer to <tt>Value</tt>s). This use list is how LLVM represents
2356def-use information in the program, and is accessible through the <tt>use_</tt>*
2357methods, shown below.</p>
2358
2359<p>Because LLVM is a typed representation, every LLVM <tt>Value</tt> is typed,
2360and this <a href="#Type">Type</a> is available through the <tt>getType()</tt>
2361method. In addition, all LLVM values can be named. The "name" of the
2362<tt>Value</tt> is a symbolic string printed in the LLVM code:</p>
2363
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002364<div class="doc_code">
2365<pre>
Reid Spencer06565dc2007-01-12 17:11:23 +00002366%<b>foo</b> = add i32 1, 2
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002367</pre>
2368</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002369
2370<p><a name="#nameWarning">The name of this instruction is "foo".</a> <b>NOTE</b>
2371that the name of any value may be missing (an empty string), so names should
2372<b>ONLY</b> be used for debugging (making the source code easier to read,
2373debugging printouts), they should not be used to keep track of values or map
2374between them. For this purpose, use a <tt>std::map</tt> of pointers to the
2375<tt>Value</tt> itself instead.</p>
2376
2377<p>One important aspect of LLVM is that there is no distinction between an SSA
2378variable and the operation that produces it. Because of this, any reference to
2379the value produced by an instruction (or the value available as an incoming
Chris Lattnerd5fc4fc2004-03-18 14:58:55 +00002380argument, for example) is represented as a direct pointer to the instance of
2381the class that
Misha Brukman13fd15c2004-01-15 00:14:41 +00002382represents this value. Although this may take some getting used to, it
2383simplifies the representation and makes it easier to manipulate.</p>
2384
2385</div>
2386
2387<!-- _______________________________________________________________________ -->
2388<div class="doc_subsubsection">
2389 <a name="m_Value">Important Public Members of the <tt>Value</tt> class</a>
2390</div>
2391
2392<div class="doc_text">
2393
Chris Lattner261efe92003-11-25 01:02:51 +00002394<ul>
2395 <li><tt>Value::use_iterator</tt> - Typedef for iterator over the
2396use-list<br>
2397 <tt>Value::use_const_iterator</tt> - Typedef for const_iterator over
2398the use-list<br>
2399 <tt>unsigned use_size()</tt> - Returns the number of users of the
2400value.<br>
Chris Lattner9355b472002-09-06 02:50:58 +00002401 <tt>bool use_empty()</tt> - Returns true if there are no users.<br>
Chris Lattner261efe92003-11-25 01:02:51 +00002402 <tt>use_iterator use_begin()</tt> - Get an iterator to the start of
2403the use-list.<br>
2404 <tt>use_iterator use_end()</tt> - Get an iterator to the end of the
2405use-list.<br>
2406 <tt><a href="#User">User</a> *use_back()</tt> - Returns the last
2407element in the list.
2408 <p> These methods are the interface to access the def-use
2409information in LLVM. As with all other iterators in LLVM, the naming
2410conventions follow the conventions defined by the <a href="#stl">STL</a>.</p>
Chris Lattner261efe92003-11-25 01:02:51 +00002411 </li>
2412 <li><tt><a href="#Type">Type</a> *getType() const</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002413 <p>This method returns the Type of the Value.</p>
Chris Lattner261efe92003-11-25 01:02:51 +00002414 </li>
2415 <li><tt>bool hasName() const</tt><br>
Chris Lattner9355b472002-09-06 02:50:58 +00002416 <tt>std::string getName() const</tt><br>
Chris Lattner261efe92003-11-25 01:02:51 +00002417 <tt>void setName(const std::string &amp;Name)</tt>
2418 <p> This family of methods is used to access and assign a name to a <tt>Value</tt>,
2419be aware of the <a href="#nameWarning">precaution above</a>.</p>
Chris Lattner261efe92003-11-25 01:02:51 +00002420 </li>
2421 <li><tt>void replaceAllUsesWith(Value *V)</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002422
2423 <p>This method traverses the use list of a <tt>Value</tt> changing all <a
2424 href="#User"><tt>User</tt>s</a> of the current value to refer to
2425 "<tt>V</tt>" instead. For example, if you detect that an instruction always
2426 produces a constant value (for example through constant folding), you can
2427 replace all uses of the instruction with the constant like this:</p>
2428
Bill Wendling3cd5ca62006-10-11 06:30:10 +00002429<div class="doc_code">
2430<pre>
2431Inst-&gt;replaceAllUsesWith(ConstVal);
2432</pre>
2433</div>
2434
Chris Lattner261efe92003-11-25 01:02:51 +00002435</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002436
2437</div>
2438
2439<!-- ======================================================================= -->
2440<div class="doc_subsection">
2441 <a name="User">The <tt>User</tt> class</a>
2442</div>
2443
2444<div class="doc_text">
2445
2446<p>
2447<tt>#include "<a href="/doxygen/User_8h-source.html">llvm/User.h</a>"</tt><br>
Misha Brukman384047f2004-06-03 23:29:12 +00002448doxygen info: <a href="/doxygen/classllvm_1_1User.html">User Class</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002449Superclass: <a href="#Value"><tt>Value</tt></a></p>
2450
2451<p>The <tt>User</tt> class is the common base class of all LLVM nodes that may
2452refer to <a href="#Value"><tt>Value</tt></a>s. It exposes a list of "Operands"
2453that are all of the <a href="#Value"><tt>Value</tt></a>s that the User is
2454referring to. The <tt>User</tt> class itself is a subclass of
2455<tt>Value</tt>.</p>
2456
2457<p>The operands of a <tt>User</tt> point directly to the LLVM <a
2458href="#Value"><tt>Value</tt></a> that it refers to. Because LLVM uses Static
2459Single Assignment (SSA) form, there can only be one definition referred to,
2460allowing this direct connection. This connection provides the use-def
2461information in LLVM.</p>
2462
2463</div>
2464
2465<!-- _______________________________________________________________________ -->
2466<div class="doc_subsubsection">
2467 <a name="m_User">Important Public Members of the <tt>User</tt> class</a>
2468</div>
2469
2470<div class="doc_text">
2471
2472<p>The <tt>User</tt> class exposes the operand list in two ways: through
2473an index access interface and through an iterator based interface.</p>
2474
Chris Lattner261efe92003-11-25 01:02:51 +00002475<ul>
Chris Lattner261efe92003-11-25 01:02:51 +00002476 <li><tt>Value *getOperand(unsigned i)</tt><br>
2477 <tt>unsigned getNumOperands()</tt>
2478 <p> These two methods expose the operands of the <tt>User</tt> in a
Misha Brukman13fd15c2004-01-15 00:14:41 +00002479convenient form for direct access.</p></li>
2480
Chris Lattner261efe92003-11-25 01:02:51 +00002481 <li><tt>User::op_iterator</tt> - Typedef for iterator over the operand
2482list<br>
Chris Lattner58360822005-01-17 00:12:04 +00002483 <tt>op_iterator op_begin()</tt> - Get an iterator to the start of
2484the operand list.<br>
2485 <tt>op_iterator op_end()</tt> - Get an iterator to the end of the
Chris Lattner261efe92003-11-25 01:02:51 +00002486operand list.
2487 <p> Together, these methods make up the iterator based interface to
Misha Brukman13fd15c2004-01-15 00:14:41 +00002488the operands of a <tt>User</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002489</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002490
2491</div>
2492
2493<!-- ======================================================================= -->
2494<div class="doc_subsection">
2495 <a name="Instruction">The <tt>Instruction</tt> class</a>
2496</div>
2497
2498<div class="doc_text">
2499
2500<p><tt>#include "</tt><tt><a
2501href="/doxygen/Instruction_8h-source.html">llvm/Instruction.h</a>"</tt><br>
Misha Brukman31ca1de2004-06-03 23:35:54 +00002502doxygen info: <a href="/doxygen/classllvm_1_1Instruction.html">Instruction Class</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002503Superclasses: <a href="#User"><tt>User</tt></a>, <a
2504href="#Value"><tt>Value</tt></a></p>
2505
2506<p>The <tt>Instruction</tt> class is the common base class for all LLVM
2507instructions. It provides only a few methods, but is a very commonly used
2508class. The primary data tracked by the <tt>Instruction</tt> class itself is the
2509opcode (instruction type) and the parent <a
2510href="#BasicBlock"><tt>BasicBlock</tt></a> the <tt>Instruction</tt> is embedded
2511into. To represent a specific type of instruction, one of many subclasses of
2512<tt>Instruction</tt> are used.</p>
2513
2514<p> Because the <tt>Instruction</tt> class subclasses the <a
2515href="#User"><tt>User</tt></a> class, its operands can be accessed in the same
2516way as for other <a href="#User"><tt>User</tt></a>s (with the
2517<tt>getOperand()</tt>/<tt>getNumOperands()</tt> and
2518<tt>op_begin()</tt>/<tt>op_end()</tt> methods).</p> <p> An important file for
2519the <tt>Instruction</tt> class is the <tt>llvm/Instruction.def</tt> file. This
2520file contains some meta-data about the various different types of instructions
2521in LLVM. It describes the enum values that are used as opcodes (for example
Reid Spencerc92d25d2006-12-19 19:47:19 +00002522<tt>Instruction::Add</tt> and <tt>Instruction::ICmp</tt>), as well as the
Misha Brukman13fd15c2004-01-15 00:14:41 +00002523concrete sub-classes of <tt>Instruction</tt> that implement the instruction (for
2524example <tt><a href="#BinaryOperator">BinaryOperator</a></tt> and <tt><a
Reid Spencerc92d25d2006-12-19 19:47:19 +00002525href="#CmpInst">CmpInst</a></tt>). Unfortunately, the use of macros in
Misha Brukman13fd15c2004-01-15 00:14:41 +00002526this file confuses doxygen, so these enum values don't show up correctly in the
Misha Brukman31ca1de2004-06-03 23:35:54 +00002527<a href="/doxygen/classllvm_1_1Instruction.html">doxygen output</a>.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002528
2529</div>
2530
2531<!-- _______________________________________________________________________ -->
2532<div class="doc_subsubsection">
Reid Spencerc92d25d2006-12-19 19:47:19 +00002533 <a name="s_Instruction">Important Subclasses of the <tt>Instruction</tt>
2534 class</a>
2535</div>
2536<div class="doc_text">
2537 <ul>
2538 <li><tt><a name="BinaryOperator">BinaryOperator</a></tt>
2539 <p>This subclasses represents all two operand instructions whose operands
2540 must be the same type, except for the comparison instructions.</p></li>
2541 <li><tt><a name="CastInst">CastInst</a></tt>
2542 <p>This subclass is the parent of the 12 casting instructions. It provides
2543 common operations on cast instructions.</p>
2544 <li><tt><a name="CmpInst">CmpInst</a></tt>
2545 <p>This subclass respresents the two comparison instructions,
2546 <a href="LangRef.html#i_icmp">ICmpInst</a> (integer opreands), and
2547 <a href="LangRef.html#i_fcmp">FCmpInst</a> (floating point operands).</p>
2548 <li><tt><a name="TerminatorInst">TerminatorInst</a></tt>
2549 <p>This subclass is the parent of all terminator instructions (those which
2550 can terminate a block).</p>
2551 </ul>
2552 </div>
2553
2554<!-- _______________________________________________________________________ -->
2555<div class="doc_subsubsection">
Misha Brukman13fd15c2004-01-15 00:14:41 +00002556 <a name="m_Instruction">Important Public Members of the <tt>Instruction</tt>
2557 class</a>
2558</div>
2559
2560<div class="doc_text">
2561
Chris Lattner261efe92003-11-25 01:02:51 +00002562<ul>
2563 <li><tt><a href="#BasicBlock">BasicBlock</a> *getParent()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002564 <p>Returns the <a href="#BasicBlock"><tt>BasicBlock</tt></a> that
2565this <tt>Instruction</tt> is embedded into.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002566 <li><tt>bool mayWriteToMemory()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002567 <p>Returns true if the instruction writes to memory, i.e. it is a
2568 <tt>call</tt>,<tt>free</tt>,<tt>invoke</tt>, or <tt>store</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002569 <li><tt>unsigned getOpcode()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002570 <p>Returns the opcode for the <tt>Instruction</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002571 <li><tt><a href="#Instruction">Instruction</a> *clone() const</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002572 <p>Returns another instance of the specified instruction, identical
Chris Lattner261efe92003-11-25 01:02:51 +00002573in all ways to the original except that the instruction has no parent
2574(ie it's not embedded into a <a href="#BasicBlock"><tt>BasicBlock</tt></a>),
Misha Brukman13fd15c2004-01-15 00:14:41 +00002575and it has no name</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002576</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002577
2578</div>
2579
2580<!-- ======================================================================= -->
2581<div class="doc_subsection">
2582 <a name="BasicBlock">The <tt>BasicBlock</tt> class</a>
2583</div>
2584
2585<div class="doc_text">
2586
Misha Brukman384047f2004-06-03 23:29:12 +00002587<p><tt>#include "<a
2588href="/doxygen/BasicBlock_8h-source.html">llvm/BasicBlock.h</a>"</tt><br>
2589doxygen info: <a href="/doxygen/structllvm_1_1BasicBlock.html">BasicBlock
2590Class</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002591Superclass: <a href="#Value"><tt>Value</tt></a></p>
2592
2593<p>This class represents a single entry multiple exit section of the code,
2594commonly known as a basic block by the compiler community. The
2595<tt>BasicBlock</tt> class maintains a list of <a
2596href="#Instruction"><tt>Instruction</tt></a>s, which form the body of the block.
2597Matching the language definition, the last element of this list of instructions
2598is always a terminator instruction (a subclass of the <a
2599href="#TerminatorInst"><tt>TerminatorInst</tt></a> class).</p>
2600
2601<p>In addition to tracking the list of instructions that make up the block, the
2602<tt>BasicBlock</tt> class also keeps track of the <a
2603href="#Function"><tt>Function</tt></a> that it is embedded into.</p>
2604
2605<p>Note that <tt>BasicBlock</tt>s themselves are <a
2606href="#Value"><tt>Value</tt></a>s, because they are referenced by instructions
2607like branches and can go in the switch tables. <tt>BasicBlock</tt>s have type
2608<tt>label</tt>.</p>
2609
2610</div>
2611
2612<!-- _______________________________________________________________________ -->
2613<div class="doc_subsubsection">
2614 <a name="m_BasicBlock">Important Public Members of the <tt>BasicBlock</tt>
2615 class</a>
2616</div>
2617
2618<div class="doc_text">
2619
Chris Lattner261efe92003-11-25 01:02:51 +00002620<ul>
Misha Brukmanb0e7e452004-10-29 04:33:19 +00002621
2622<li><tt>BasicBlock(const std::string &amp;Name = "", </tt><tt><a
Chris Lattner261efe92003-11-25 01:02:51 +00002623 href="#Function">Function</a> *Parent = 0)</tt>
Misha Brukmanb0e7e452004-10-29 04:33:19 +00002624
2625<p>The <tt>BasicBlock</tt> constructor is used to create new basic blocks for
2626insertion into a function. The constructor optionally takes a name for the new
2627block, and a <a href="#Function"><tt>Function</tt></a> to insert it into. If
2628the <tt>Parent</tt> parameter is specified, the new <tt>BasicBlock</tt> is
2629automatically inserted at the end of the specified <a
2630href="#Function"><tt>Function</tt></a>, if not specified, the BasicBlock must be
2631manually inserted into the <a href="#Function"><tt>Function</tt></a>.</p></li>
2632
2633<li><tt>BasicBlock::iterator</tt> - Typedef for instruction list iterator<br>
2634<tt>BasicBlock::const_iterator</tt> - Typedef for const_iterator.<br>
2635<tt>begin()</tt>, <tt>end()</tt>, <tt>front()</tt>, <tt>back()</tt>,
Chris Lattner77d69242005-03-15 05:19:20 +00002636<tt>size()</tt>, <tt>empty()</tt>
Misha Brukmanb0e7e452004-10-29 04:33:19 +00002637STL-style functions for accessing the instruction list.
2638
2639<p>These methods and typedefs are forwarding functions that have the same
2640semantics as the standard library methods of the same names. These methods
2641expose the underlying instruction list of a basic block in a way that is easy to
2642manipulate. To get the full complement of container operations (including
2643operations to update the list), you must use the <tt>getInstList()</tt>
2644method.</p></li>
2645
2646<li><tt>BasicBlock::InstListType &amp;getInstList()</tt>
2647
2648<p>This method is used to get access to the underlying container that actually
2649holds the Instructions. This method must be used when there isn't a forwarding
2650function in the <tt>BasicBlock</tt> class for the operation that you would like
2651to perform. Because there are no forwarding functions for "updating"
2652operations, you need to use this if you want to update the contents of a
2653<tt>BasicBlock</tt>.</p></li>
2654
2655<li><tt><a href="#Function">Function</a> *getParent()</tt>
2656
2657<p> Returns a pointer to <a href="#Function"><tt>Function</tt></a> the block is
2658embedded into, or a null pointer if it is homeless.</p></li>
2659
2660<li><tt><a href="#TerminatorInst">TerminatorInst</a> *getTerminator()</tt>
2661
2662<p> Returns a pointer to the terminator instruction that appears at the end of
2663the <tt>BasicBlock</tt>. If there is no terminator instruction, or if the last
2664instruction in the block is not a terminator, then a null pointer is
2665returned.</p></li>
2666
Chris Lattner261efe92003-11-25 01:02:51 +00002667</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002668
2669</div>
2670
2671<!-- ======================================================================= -->
2672<div class="doc_subsection">
2673 <a name="GlobalValue">The <tt>GlobalValue</tt> class</a>
2674</div>
2675
2676<div class="doc_text">
2677
2678<p><tt>#include "<a
2679href="/doxygen/GlobalValue_8h-source.html">llvm/GlobalValue.h</a>"</tt><br>
Misha Brukman384047f2004-06-03 23:29:12 +00002680doxygen info: <a href="/doxygen/classllvm_1_1GlobalValue.html">GlobalValue
2681Class</a><br>
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002682Superclasses: <a href="#Constant"><tt>Constant</tt></a>,
2683<a href="#User"><tt>User</tt></a>, <a href="#Value"><tt>Value</tt></a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002684
2685<p>Global values (<a href="#GlobalVariable"><tt>GlobalVariable</tt></a>s or <a
2686href="#Function"><tt>Function</tt></a>s) are the only LLVM values that are
2687visible in the bodies of all <a href="#Function"><tt>Function</tt></a>s.
2688Because they are visible at global scope, they are also subject to linking with
2689other globals defined in different translation units. To control the linking
2690process, <tt>GlobalValue</tt>s know their linkage rules. Specifically,
2691<tt>GlobalValue</tt>s know whether they have internal or external linkage, as
Reid Spencer8b2da7a2004-07-18 13:10:31 +00002692defined by the <tt>LinkageTypes</tt> enumeration.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002693
2694<p>If a <tt>GlobalValue</tt> has internal linkage (equivalent to being
2695<tt>static</tt> in C), it is not visible to code outside the current translation
2696unit, and does not participate in linking. If it has external linkage, it is
2697visible to external code, and does participate in linking. In addition to
2698linkage information, <tt>GlobalValue</tt>s keep track of which <a
2699href="#Module"><tt>Module</tt></a> they are currently part of.</p>
2700
2701<p>Because <tt>GlobalValue</tt>s are memory objects, they are always referred to
2702by their <b>address</b>. As such, the <a href="#Type"><tt>Type</tt></a> of a
2703global is always a pointer to its contents. It is important to remember this
2704when using the <tt>GetElementPtrInst</tt> instruction because this pointer must
2705be dereferenced first. For example, if you have a <tt>GlobalVariable</tt> (a
2706subclass of <tt>GlobalValue)</tt> that is an array of 24 ints, type <tt>[24 x
Reid Spencer06565dc2007-01-12 17:11:23 +00002707i32]</tt>, then the <tt>GlobalVariable</tt> is a pointer to that array. Although
Misha Brukman13fd15c2004-01-15 00:14:41 +00002708the address of the first element of this array and the value of the
2709<tt>GlobalVariable</tt> are the same, they have different types. The
Reid Spencer06565dc2007-01-12 17:11:23 +00002710<tt>GlobalVariable</tt>'s type is <tt>[24 x i32]</tt>. The first element's type
2711is <tt>i32.</tt> Because of this, accessing a global value requires you to
Misha Brukman13fd15c2004-01-15 00:14:41 +00002712dereference the pointer with <tt>GetElementPtrInst</tt> first, then its elements
2713can be accessed. This is explained in the <a href="LangRef.html#globalvars">LLVM
2714Language Reference Manual</a>.</p>
2715
2716</div>
2717
2718<!-- _______________________________________________________________________ -->
2719<div class="doc_subsubsection">
2720 <a name="m_GlobalValue">Important Public Members of the <tt>GlobalValue</tt>
2721 class</a>
2722</div>
2723
2724<div class="doc_text">
2725
Chris Lattner261efe92003-11-25 01:02:51 +00002726<ul>
2727 <li><tt>bool hasInternalLinkage() const</tt><br>
Chris Lattner9355b472002-09-06 02:50:58 +00002728 <tt>bool hasExternalLinkage() const</tt><br>
Chris Lattner261efe92003-11-25 01:02:51 +00002729 <tt>void setInternalLinkage(bool HasInternalLinkage)</tt>
2730 <p> These methods manipulate the linkage characteristics of the <tt>GlobalValue</tt>.</p>
2731 <p> </p>
2732 </li>
2733 <li><tt><a href="#Module">Module</a> *getParent()</tt>
2734 <p> This returns the <a href="#Module"><tt>Module</tt></a> that the
Misha Brukman13fd15c2004-01-15 00:14:41 +00002735GlobalValue is currently embedded into.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002736</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002737
2738</div>
2739
2740<!-- ======================================================================= -->
2741<div class="doc_subsection">
2742 <a name="Function">The <tt>Function</tt> class</a>
2743</div>
2744
2745<div class="doc_text">
2746
2747<p><tt>#include "<a
2748href="/doxygen/Function_8h-source.html">llvm/Function.h</a>"</tt><br> doxygen
Misha Brukman31ca1de2004-06-03 23:35:54 +00002749info: <a href="/doxygen/classllvm_1_1Function.html">Function Class</a><br>
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002750Superclasses: <a href="#GlobalValue"><tt>GlobalValue</tt></a>,
2751<a href="#Constant"><tt>Constant</tt></a>,
2752<a href="#User"><tt>User</tt></a>,
2753<a href="#Value"><tt>Value</tt></a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002754
2755<p>The <tt>Function</tt> class represents a single procedure in LLVM. It is
2756actually one of the more complex classes in the LLVM heirarchy because it must
2757keep track of a large amount of data. The <tt>Function</tt> class keeps track
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002758of a list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s, a list of formal
2759<a href="#Argument"><tt>Argument</tt></a>s, and a
2760<a href="#SymbolTable"><tt>SymbolTable</tt></a>.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002761
2762<p>The list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s is the most
2763commonly used part of <tt>Function</tt> objects. The list imposes an implicit
2764ordering of the blocks in the function, which indicate how the code will be
2765layed out by the backend. Additionally, the first <a
2766href="#BasicBlock"><tt>BasicBlock</tt></a> is the implicit entry node for the
2767<tt>Function</tt>. It is not legal in LLVM to explicitly branch to this initial
2768block. There are no implicit exit nodes, and in fact there may be multiple exit
2769nodes from a single <tt>Function</tt>. If the <a
2770href="#BasicBlock"><tt>BasicBlock</tt></a> list is empty, this indicates that
2771the <tt>Function</tt> is actually a function declaration: the actual body of the
2772function hasn't been linked in yet.</p>
2773
2774<p>In addition to a list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s, the
2775<tt>Function</tt> class also keeps track of the list of formal <a
2776href="#Argument"><tt>Argument</tt></a>s that the function receives. This
2777container manages the lifetime of the <a href="#Argument"><tt>Argument</tt></a>
2778nodes, just like the <a href="#BasicBlock"><tt>BasicBlock</tt></a> list does for
2779the <a href="#BasicBlock"><tt>BasicBlock</tt></a>s.</p>
2780
2781<p>The <a href="#SymbolTable"><tt>SymbolTable</tt></a> is a very rarely used
2782LLVM feature that is only used when you have to look up a value by name. Aside
2783from that, the <a href="#SymbolTable"><tt>SymbolTable</tt></a> is used
2784internally to make sure that there are not conflicts between the names of <a
2785href="#Instruction"><tt>Instruction</tt></a>s, <a
2786href="#BasicBlock"><tt>BasicBlock</tt></a>s, or <a
2787href="#Argument"><tt>Argument</tt></a>s in the function body.</p>
2788
Reid Spencer8b2da7a2004-07-18 13:10:31 +00002789<p>Note that <tt>Function</tt> is a <a href="#GlobalValue">GlobalValue</a>
2790and therefore also a <a href="#Constant">Constant</a>. The value of the function
2791is its address (after linking) which is guaranteed to be constant.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002792</div>
2793
2794<!-- _______________________________________________________________________ -->
2795<div class="doc_subsubsection">
2796 <a name="m_Function">Important Public Members of the <tt>Function</tt>
2797 class</a>
2798</div>
2799
2800<div class="doc_text">
2801
Chris Lattner261efe92003-11-25 01:02:51 +00002802<ul>
2803 <li><tt>Function(const </tt><tt><a href="#FunctionType">FunctionType</a>
Chris Lattnerac479e52004-08-04 05:10:48 +00002804 *Ty, LinkageTypes Linkage, const std::string &amp;N = "", Module* Parent = 0)</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002805
2806 <p>Constructor used when you need to create new <tt>Function</tt>s to add
2807 the the program. The constructor must specify the type of the function to
Chris Lattnerac479e52004-08-04 05:10:48 +00002808 create and what type of linkage the function should have. The <a
2809 href="#FunctionType"><tt>FunctionType</tt></a> argument
Misha Brukman13fd15c2004-01-15 00:14:41 +00002810 specifies the formal arguments and return value for the function. The same
2811 <a href="#FunctionTypel"><tt>FunctionType</tt></a> value can be used to
2812 create multiple functions. The <tt>Parent</tt> argument specifies the Module
2813 in which the function is defined. If this argument is provided, the function
2814 will automatically be inserted into that module's list of
2815 functions.</p></li>
2816
Chris Lattner261efe92003-11-25 01:02:51 +00002817 <li><tt>bool isExternal()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002818
2819 <p>Return whether or not the <tt>Function</tt> has a body defined. If the
2820 function is "external", it does not have a body, and thus must be resolved
2821 by linking with a function defined in a different translation unit.</p></li>
2822
Chris Lattner261efe92003-11-25 01:02:51 +00002823 <li><tt>Function::iterator</tt> - Typedef for basic block list iterator<br>
Chris Lattner9355b472002-09-06 02:50:58 +00002824 <tt>Function::const_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002825
Chris Lattner77d69242005-03-15 05:19:20 +00002826 <tt>begin()</tt>, <tt>end()</tt>
2827 <tt>size()</tt>, <tt>empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002828
2829 <p>These are forwarding methods that make it easy to access the contents of
2830 a <tt>Function</tt> object's <a href="#BasicBlock"><tt>BasicBlock</tt></a>
2831 list.</p></li>
2832
Chris Lattner261efe92003-11-25 01:02:51 +00002833 <li><tt>Function::BasicBlockListType &amp;getBasicBlockList()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002834
2835 <p>Returns the list of <a href="#BasicBlock"><tt>BasicBlock</tt></a>s. This
2836 is necessary to use when you need to update the list or perform a complex
2837 action that doesn't have a forwarding method.</p></li>
2838
Chris Lattner89cc2652005-03-15 04:48:32 +00002839 <li><tt>Function::arg_iterator</tt> - Typedef for the argument list
Chris Lattner261efe92003-11-25 01:02:51 +00002840iterator<br>
Chris Lattner89cc2652005-03-15 04:48:32 +00002841 <tt>Function::const_arg_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002842
Chris Lattner77d69242005-03-15 05:19:20 +00002843 <tt>arg_begin()</tt>, <tt>arg_end()</tt>
Chris Lattner89cc2652005-03-15 04:48:32 +00002844 <tt>arg_size()</tt>, <tt>arg_empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002845
2846 <p>These are forwarding methods that make it easy to access the contents of
2847 a <tt>Function</tt> object's <a href="#Argument"><tt>Argument</tt></a>
2848 list.</p></li>
2849
Chris Lattner261efe92003-11-25 01:02:51 +00002850 <li><tt>Function::ArgumentListType &amp;getArgumentList()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002851
2852 <p>Returns the list of <a href="#Argument"><tt>Argument</tt></a>s. This is
2853 necessary to use when you need to update the list or perform a complex
2854 action that doesn't have a forwarding method.</p></li>
2855
Chris Lattner261efe92003-11-25 01:02:51 +00002856 <li><tt><a href="#BasicBlock">BasicBlock</a> &amp;getEntryBlock()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002857
2858 <p>Returns the entry <a href="#BasicBlock"><tt>BasicBlock</tt></a> for the
2859 function. Because the entry block for the function is always the first
2860 block, this returns the first block of the <tt>Function</tt>.</p></li>
2861
Chris Lattner261efe92003-11-25 01:02:51 +00002862 <li><tt><a href="#Type">Type</a> *getReturnType()</tt><br>
2863 <tt><a href="#FunctionType">FunctionType</a> *getFunctionType()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002864
2865 <p>This traverses the <a href="#Type"><tt>Type</tt></a> of the
2866 <tt>Function</tt> and returns the return type of the function, or the <a
2867 href="#FunctionType"><tt>FunctionType</tt></a> of the actual
2868 function.</p></li>
2869
Chris Lattner261efe92003-11-25 01:02:51 +00002870 <li><tt><a href="#SymbolTable">SymbolTable</a> *getSymbolTable()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002871
Chris Lattner261efe92003-11-25 01:02:51 +00002872 <p> Return a pointer to the <a href="#SymbolTable"><tt>SymbolTable</tt></a>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002873 for this <tt>Function</tt>.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002874</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002875
2876</div>
2877
2878<!-- ======================================================================= -->
2879<div class="doc_subsection">
2880 <a name="GlobalVariable">The <tt>GlobalVariable</tt> class</a>
2881</div>
2882
2883<div class="doc_text">
2884
2885<p><tt>#include "<a
2886href="/doxygen/GlobalVariable_8h-source.html">llvm/GlobalVariable.h</a>"</tt>
2887<br>
Tanya Lattnera3da7772004-06-22 08:02:25 +00002888doxygen info: <a href="/doxygen/classllvm_1_1GlobalVariable.html">GlobalVariable
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002889 Class</a><br>
2890Superclasses: <a href="#GlobalValue"><tt>GlobalValue</tt></a>,
2891<a href="#Constant"><tt>Constant</tt></a>,
2892<a href="#User"><tt>User</tt></a>,
2893<a href="#Value"><tt>Value</tt></a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002894
2895<p>Global variables are represented with the (suprise suprise)
2896<tt>GlobalVariable</tt> class. Like functions, <tt>GlobalVariable</tt>s are also
2897subclasses of <a href="#GlobalValue"><tt>GlobalValue</tt></a>, and as such are
2898always referenced by their address (global values must live in memory, so their
Reid Spencerbe5e85e2006-04-14 14:11:48 +00002899"name" refers to their constant address). See
2900<a href="#GlobalValue"><tt>GlobalValue</tt></a> for more on this. Global
2901variables may have an initial value (which must be a
2902<a href="#Constant"><tt>Constant</tt></a>), and if they have an initializer,
2903they may be marked as "constant" themselves (indicating that their contents
2904never change at runtime).</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002905</div>
2906
2907<!-- _______________________________________________________________________ -->
2908<div class="doc_subsubsection">
2909 <a name="m_GlobalVariable">Important Public Members of the
2910 <tt>GlobalVariable</tt> class</a>
2911</div>
2912
2913<div class="doc_text">
2914
Chris Lattner261efe92003-11-25 01:02:51 +00002915<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002916 <li><tt>GlobalVariable(const </tt><tt><a href="#Type">Type</a> *Ty, bool
2917 isConstant, LinkageTypes&amp; Linkage, <a href="#Constant">Constant</a>
2918 *Initializer = 0, const std::string &amp;Name = "", Module* Parent = 0)</tt>
2919
2920 <p>Create a new global variable of the specified type. If
2921 <tt>isConstant</tt> is true then the global variable will be marked as
2922 unchanging for the program. The Linkage parameter specifies the type of
2923 linkage (internal, external, weak, linkonce, appending) for the variable. If
2924 the linkage is InternalLinkage, WeakLinkage, or LinkOnceLinkage,&nbsp; then
2925 the resultant global variable will have internal linkage. AppendingLinkage
2926 concatenates together all instances (in different translation units) of the
2927 variable into a single variable but is only applicable to arrays. &nbsp;See
2928 the <a href="LangRef.html#modulestructure">LLVM Language Reference</a> for
2929 further details on linkage types. Optionally an initializer, a name, and the
2930 module to put the variable into may be specified for the global variable as
2931 well.</p></li>
2932
Chris Lattner261efe92003-11-25 01:02:51 +00002933 <li><tt>bool isConstant() const</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002934
2935 <p>Returns true if this is a global variable that is known not to
2936 be modified at runtime.</p></li>
2937
Chris Lattner261efe92003-11-25 01:02:51 +00002938 <li><tt>bool hasInitializer()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002939
2940 <p>Returns true if this <tt>GlobalVariable</tt> has an intializer.</p></li>
2941
Chris Lattner261efe92003-11-25 01:02:51 +00002942 <li><tt><a href="#Constant">Constant</a> *getInitializer()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002943
2944 <p>Returns the intial value for a <tt>GlobalVariable</tt>. It is not legal
2945 to call this method if there is no initializer.</p></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002946</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002947
2948</div>
2949
2950<!-- ======================================================================= -->
2951<div class="doc_subsection">
2952 <a name="Module">The <tt>Module</tt> class</a>
2953</div>
2954
2955<div class="doc_text">
2956
2957<p><tt>#include "<a
2958href="/doxygen/Module_8h-source.html">llvm/Module.h</a>"</tt><br> doxygen info:
Tanya Lattnera3da7772004-06-22 08:02:25 +00002959<a href="/doxygen/classllvm_1_1Module.html">Module Class</a></p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002960
2961<p>The <tt>Module</tt> class represents the top level structure present in LLVM
2962programs. An LLVM module is effectively either a translation unit of the
2963original program or a combination of several translation units merged by the
2964linker. The <tt>Module</tt> class keeps track of a list of <a
2965href="#Function"><tt>Function</tt></a>s, a list of <a
2966href="#GlobalVariable"><tt>GlobalVariable</tt></a>s, and a <a
2967href="#SymbolTable"><tt>SymbolTable</tt></a>. Additionally, it contains a few
2968helpful member functions that try to make common operations easy.</p>
2969
2970</div>
2971
2972<!-- _______________________________________________________________________ -->
2973<div class="doc_subsubsection">
2974 <a name="m_Module">Important Public Members of the <tt>Module</tt> class</a>
2975</div>
2976
2977<div class="doc_text">
2978
Chris Lattner261efe92003-11-25 01:02:51 +00002979<ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002980 <li><tt>Module::Module(std::string name = "")</tt></li>
Chris Lattner261efe92003-11-25 01:02:51 +00002981</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002982
2983<p>Constructing a <a href="#Module">Module</a> is easy. You can optionally
2984provide a name for it (probably based on the name of the translation unit).</p>
2985
Chris Lattner261efe92003-11-25 01:02:51 +00002986<ul>
2987 <li><tt>Module::iterator</tt> - Typedef for function list iterator<br>
Chris Lattner0377de42002-09-06 14:50:55 +00002988 <tt>Module::const_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002989
Chris Lattner77d69242005-03-15 05:19:20 +00002990 <tt>begin()</tt>, <tt>end()</tt>
2991 <tt>size()</tt>, <tt>empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002992
2993 <p>These are forwarding methods that make it easy to access the contents of
2994 a <tt>Module</tt> object's <a href="#Function"><tt>Function</tt></a>
2995 list.</p></li>
2996
Chris Lattner261efe92003-11-25 01:02:51 +00002997 <li><tt>Module::FunctionListType &amp;getFunctionList()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00002998
2999 <p> Returns the list of <a href="#Function"><tt>Function</tt></a>s. This is
3000 necessary to use when you need to update the list or perform a complex
3001 action that doesn't have a forwarding method.</p>
3002
3003 <p><!-- Global Variable --></p></li>
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00003004</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00003005
3006<hr>
3007
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00003008<ul>
Chris Lattner89cc2652005-03-15 04:48:32 +00003009 <li><tt>Module::global_iterator</tt> - Typedef for global variable list iterator<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00003010
Chris Lattner89cc2652005-03-15 04:48:32 +00003011 <tt>Module::const_global_iterator</tt> - Typedef for const_iterator.<br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00003012
Chris Lattner77d69242005-03-15 05:19:20 +00003013 <tt>global_begin()</tt>, <tt>global_end()</tt>
Chris Lattner89cc2652005-03-15 04:48:32 +00003014 <tt>global_size()</tt>, <tt>global_empty()</tt>
Misha Brukman13fd15c2004-01-15 00:14:41 +00003015
3016 <p> These are forwarding methods that make it easy to access the contents of
3017 a <tt>Module</tt> object's <a
3018 href="#GlobalVariable"><tt>GlobalVariable</tt></a> list.</p></li>
3019
3020 <li><tt>Module::GlobalListType &amp;getGlobalList()</tt>
3021
3022 <p>Returns the list of <a
3023 href="#GlobalVariable"><tt>GlobalVariable</tt></a>s. This is necessary to
3024 use when you need to update the list or perform a complex action that
3025 doesn't have a forwarding method.</p>
3026
3027 <p><!-- Symbol table stuff --> </p></li>
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00003028</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00003029
3030<hr>
3031
3032<ul>
3033 <li><tt><a href="#SymbolTable">SymbolTable</a> *getSymbolTable()</tt>
3034
3035 <p>Return a reference to the <a href="#SymbolTable"><tt>SymbolTable</tt></a>
3036 for this <tt>Module</tt>.</p>
3037
3038 <p><!-- Convenience methods --></p></li>
3039</ul>
3040
3041<hr>
3042
3043<ul>
3044 <li><tt><a href="#Function">Function</a> *getFunction(const std::string
3045 &amp;Name, const <a href="#FunctionType">FunctionType</a> *Ty)</tt>
3046
3047 <p>Look up the specified function in the <tt>Module</tt> <a
3048 href="#SymbolTable"><tt>SymbolTable</tt></a>. If it does not exist, return
3049 <tt>null</tt>.</p></li>
3050
3051 <li><tt><a href="#Function">Function</a> *getOrInsertFunction(const
3052 std::string &amp;Name, const <a href="#FunctionType">FunctionType</a> *T)</tt>
3053
3054 <p>Look up the specified function in the <tt>Module</tt> <a
3055 href="#SymbolTable"><tt>SymbolTable</tt></a>. If it does not exist, add an
3056 external declaration for the function and return it.</p></li>
3057
3058 <li><tt>std::string getTypeName(const <a href="#Type">Type</a> *Ty)</tt>
3059
3060 <p>If there is at least one entry in the <a
3061 href="#SymbolTable"><tt>SymbolTable</tt></a> for the specified <a
3062 href="#Type"><tt>Type</tt></a>, return it. Otherwise return the empty
3063 string.</p></li>
3064
3065 <li><tt>bool addTypeName(const std::string &amp;Name, const <a
3066 href="#Type">Type</a> *Ty)</tt>
3067
3068 <p>Insert an entry in the <a href="#SymbolTable"><tt>SymbolTable</tt></a>
3069 mapping <tt>Name</tt> to <tt>Ty</tt>. If there is already an entry for this
3070 name, true is returned and the <a
3071 href="#SymbolTable"><tt>SymbolTable</tt></a> is not modified.</p></li>
3072</ul>
3073
3074</div>
3075
3076<!-- ======================================================================= -->
3077<div class="doc_subsection">
3078 <a name="Constant">The <tt>Constant</tt> class and subclasses</a>
3079</div>
3080
3081<div class="doc_text">
3082
3083<p>Constant represents a base class for different types of constants. It
Reid Spencer53bfebc2007-01-12 03:36:33 +00003084is subclassed by ConstantInt, ConstantArray, etc. for representing
Reid Spencerb83eb642006-10-20 07:07:24 +00003085the various types of Constants.</p>
Misha Brukman13fd15c2004-01-15 00:14:41 +00003086
3087</div>
3088
3089<!-- _______________________________________________________________________ -->
3090<div class="doc_subsubsection">
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00003091 <a name="m_Constant">Important Public Methods</a>
3092</div>
3093<div class="doc_text">
Misha Brukman13fd15c2004-01-15 00:14:41 +00003094</div>
3095
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00003096<!-- _______________________________________________________________________ -->
3097<div class="doc_subsubsection">Important Subclasses of Constant </div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00003098<div class="doc_text">
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00003099<ul>
Reid Spencer53bfebc2007-01-12 03:36:33 +00003100 <li>ConstantInt : This subclass of Constant represents an integer constant of
3101 any width, including boolean (1 bit integer).
Chris Lattner261efe92003-11-25 01:02:51 +00003102 <ul>
Reid Spencerb83eb642006-10-20 07:07:24 +00003103 <li><tt>int64_t getSExtValue() const</tt>: Returns the underlying value of
3104 this constant as a sign extended signed integer value.</li>
3105 <li><tt>uint64_t getZExtValue() const</tt>: Returns the underlying value
3106 of this constant as a zero extended unsigned integer value.</li>
Reid Spencer53bfebc2007-01-12 03:36:33 +00003107 <li><tt>static ConstantInt* get(const Type *Ty, uint64_t Val)</tt>:
3108 Returns the ConstantInt object that represents the value provided by
3109 <tt>Val</tt> for integer type <tt>Ty</tt>.</li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00003110 </ul>
3111 </li>
3112 <li>ConstantFP : This class represents a floating point constant.
3113 <ul>
3114 <li><tt>double getValue() const</tt>: Returns the underlying value of
3115 this constant. </li>
3116 </ul>
3117 </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00003118 <li>ConstantArray : This represents a constant array.
3119 <ul>
3120 <li><tt>const std::vector&lt;Use&gt; &amp;getValues() const</tt>: Returns
Chris Lattner58360822005-01-17 00:12:04 +00003121 a vector of component constants that makeup this array. </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00003122 </ul>
3123 </li>
3124 <li>ConstantStruct : This represents a constant struct.
3125 <ul>
3126 <li><tt>const std::vector&lt;Use&gt; &amp;getValues() const</tt>: Returns
Chris Lattner58360822005-01-17 00:12:04 +00003127 a vector of component constants that makeup this array. </li>
Reid Spencerfe8f4ff2004-11-01 09:02:53 +00003128 </ul>
3129 </li>
3130 <li>GlobalValue : This represents either a global variable or a function. In
3131 either case, the value is a constant fixed address (after linking).
3132 </li>
Chris Lattnerc75ff9a2002-10-01 23:17:09 +00003133</ul>
Misha Brukman13fd15c2004-01-15 00:14:41 +00003134</div>
Misha Brukman13fd15c2004-01-15 00:14:41 +00003135<!-- ======================================================================= -->
3136<div class="doc_subsection">
3137 <a name="Argument">The <tt>Argument</tt> class</a>
3138</div>
3139
3140<div class="doc_text">
3141
3142<p>This subclass of Value defines the interface for incoming formal
Chris Lattner58360822005-01-17 00:12:04 +00003143arguments to a function. A Function maintains a list of its formal
Misha Brukman13fd15c2004-01-15 00:14:41 +00003144arguments. An argument has a pointer to the parent Function.</p>
3145
3146</div>
3147
Chris Lattner9355b472002-09-06 02:50:58 +00003148<!-- *********************************************************************** -->
Misha Brukman13fd15c2004-01-15 00:14:41 +00003149<hr>
3150<address>
3151 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
3152 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
3153 <a href="http://validator.w3.org/check/referer"><img
3154 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
3155
3156 <a href="mailto:dhurjati@cs.uiuc.edu">Dinakar Dhurjati</a> and
3157 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
Reid Spencer05fe4b02006-03-14 05:39:39 +00003158 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
Misha Brukman13fd15c2004-01-15 00:14:41 +00003159 Last modified: $Date$
3160</address>
3161
Chris Lattner261efe92003-11-25 01:02:51 +00003162</body>
3163</html>