xref: /minix3/external/bsd/llvm/dist/llvm/docs/ProgrammersManual.rst (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc========================
2*f4a2713aSLionel SambucLLVM Programmer's Manual
3*f4a2713aSLionel Sambuc========================
4*f4a2713aSLionel Sambuc
5*f4a2713aSLionel Sambuc.. contents::
6*f4a2713aSLionel Sambuc   :local:
7*f4a2713aSLionel Sambuc
8*f4a2713aSLionel Sambuc.. warning::
9*f4a2713aSLionel Sambuc   This is always a work in progress.
10*f4a2713aSLionel Sambuc
11*f4a2713aSLionel Sambuc.. _introduction:
12*f4a2713aSLionel Sambuc
13*f4a2713aSLionel SambucIntroduction
14*f4a2713aSLionel Sambuc============
15*f4a2713aSLionel Sambuc
16*f4a2713aSLionel SambucThis document is meant to highlight some of the important classes and interfaces
17*f4a2713aSLionel Sambucavailable in the LLVM source-base.  This manual is not intended to explain what
18*f4a2713aSLionel SambucLLVM is, how it works, and what LLVM code looks like.  It assumes that you know
19*f4a2713aSLionel Sambucthe basics of LLVM and are interested in writing transformations or otherwise
20*f4a2713aSLionel Sambucanalyzing or manipulating the code.
21*f4a2713aSLionel Sambuc
22*f4a2713aSLionel SambucThis document should get you oriented so that you can find your way in the
23*f4a2713aSLionel Sambuccontinuously growing source code that makes up the LLVM infrastructure.  Note
24*f4a2713aSLionel Sambucthat this manual is not intended to serve as a replacement for reading the
25*f4a2713aSLionel Sambucsource code, so if you think there should be a method in one of these classes to
26*f4a2713aSLionel Sambucdo something, but it's not listed, check the source.  Links to the `doxygen
27*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/>`__ sources are provided to make this as easy as
28*f4a2713aSLionel Sambucpossible.
29*f4a2713aSLionel Sambuc
30*f4a2713aSLionel SambucThe first section of this document describes general information that is useful
31*f4a2713aSLionel Sambucto know when working in the LLVM infrastructure, and the second describes the
32*f4a2713aSLionel SambucCore LLVM classes.  In the future this manual will be extended with information
33*f4a2713aSLionel Sambucdescribing how to use extension libraries, such as dominator information, CFG
34*f4a2713aSLionel Sambuctraversal routines, and useful utilities like the ``InstVisitor`` (`doxygen
35*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/InstVisitor_8h-source.html>`__) template.
36*f4a2713aSLionel Sambuc
37*f4a2713aSLionel Sambuc.. _general:
38*f4a2713aSLionel Sambuc
39*f4a2713aSLionel SambucGeneral Information
40*f4a2713aSLionel Sambuc===================
41*f4a2713aSLionel Sambuc
42*f4a2713aSLionel SambucThis section contains general information that is useful if you are working in
43*f4a2713aSLionel Sambucthe LLVM source-base, but that isn't specific to any particular API.
44*f4a2713aSLionel Sambuc
45*f4a2713aSLionel Sambuc.. _stl:
46*f4a2713aSLionel Sambuc
47*f4a2713aSLionel SambucThe C++ Standard Template Library
48*f4a2713aSLionel Sambuc---------------------------------
49*f4a2713aSLionel Sambuc
50*f4a2713aSLionel SambucLLVM makes heavy use of the C++ Standard Template Library (STL), perhaps much
51*f4a2713aSLionel Sambucmore than you are used to, or have seen before.  Because of this, you might want
52*f4a2713aSLionel Sambucto do a little background reading in the techniques used and capabilities of the
53*f4a2713aSLionel Sambuclibrary.  There are many good pages that discuss the STL, and several books on
54*f4a2713aSLionel Sambucthe subject that you can get, so it will not be discussed in this document.
55*f4a2713aSLionel Sambuc
56*f4a2713aSLionel SambucHere are some useful links:
57*f4a2713aSLionel Sambuc
58*f4a2713aSLionel Sambuc#. `cppreference.com
59*f4a2713aSLionel Sambuc   <http://en.cppreference.com/w/>`_ - an excellent
60*f4a2713aSLionel Sambuc   reference for the STL and other parts of the standard C++ library.
61*f4a2713aSLionel Sambuc
62*f4a2713aSLionel Sambuc#. `C++ In a Nutshell <http://www.tempest-sw.com/cpp/>`_ - This is an O'Reilly
63*f4a2713aSLionel Sambuc   book in the making.  It has a decent Standard Library Reference that rivals
64*f4a2713aSLionel Sambuc   Dinkumware's, and is unfortunately no longer free since the book has been
65*f4a2713aSLionel Sambuc   published.
66*f4a2713aSLionel Sambuc
67*f4a2713aSLionel Sambuc#. `C++ Frequently Asked Questions <http://www.parashift.com/c++-faq-lite/>`_.
68*f4a2713aSLionel Sambuc
69*f4a2713aSLionel Sambuc#. `SGI's STL Programmer's Guide <http://www.sgi.com/tech/stl/>`_ - Contains a
70*f4a2713aSLionel Sambuc   useful `Introduction to the STL
71*f4a2713aSLionel Sambuc   <http://www.sgi.com/tech/stl/stl_introduction.html>`_.
72*f4a2713aSLionel Sambuc
73*f4a2713aSLionel Sambuc#. `Bjarne Stroustrup's C++ Page
74*f4a2713aSLionel Sambuc   <http://www.research.att.com/%7Ebs/C++.html>`_.
75*f4a2713aSLionel Sambuc
76*f4a2713aSLionel Sambuc#. `Bruce Eckel's Thinking in C++, 2nd ed. Volume 2 Revision 4.0
77*f4a2713aSLionel Sambuc   (even better, get the book)
78*f4a2713aSLionel Sambuc   <http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html>`_.
79*f4a2713aSLionel Sambuc
80*f4a2713aSLionel SambucYou are also encouraged to take a look at the :doc:`LLVM Coding Standards
81*f4a2713aSLionel Sambuc<CodingStandards>` guide which focuses on how to write maintainable code more
82*f4a2713aSLionel Sambucthan where to put your curly braces.
83*f4a2713aSLionel Sambuc
84*f4a2713aSLionel Sambuc.. _resources:
85*f4a2713aSLionel Sambuc
86*f4a2713aSLionel SambucOther useful references
87*f4a2713aSLionel Sambuc-----------------------
88*f4a2713aSLionel Sambuc
89*f4a2713aSLionel Sambuc#. `Using static and shared libraries across platforms
90*f4a2713aSLionel Sambuc   <http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html>`_
91*f4a2713aSLionel Sambuc
92*f4a2713aSLionel Sambuc.. _apis:
93*f4a2713aSLionel Sambuc
94*f4a2713aSLionel SambucImportant and useful LLVM APIs
95*f4a2713aSLionel Sambuc==============================
96*f4a2713aSLionel Sambuc
97*f4a2713aSLionel SambucHere we highlight some LLVM APIs that are generally useful and good to know
98*f4a2713aSLionel Sambucabout when writing transformations.
99*f4a2713aSLionel Sambuc
100*f4a2713aSLionel Sambuc.. _isa:
101*f4a2713aSLionel Sambuc
102*f4a2713aSLionel SambucThe ``isa<>``, ``cast<>`` and ``dyn_cast<>`` templates
103*f4a2713aSLionel Sambuc------------------------------------------------------
104*f4a2713aSLionel Sambuc
105*f4a2713aSLionel SambucThe LLVM source-base makes extensive use of a custom form of RTTI.  These
106*f4a2713aSLionel Sambuctemplates have many similarities to the C++ ``dynamic_cast<>`` operator, but
107*f4a2713aSLionel Sambucthey don't have some drawbacks (primarily stemming from the fact that
108*f4a2713aSLionel Sambuc``dynamic_cast<>`` only works on classes that have a v-table).  Because they are
109*f4a2713aSLionel Sambucused so often, you must know what they do and how they work.  All of these
110*f4a2713aSLionel Sambuctemplates are defined in the ``llvm/Support/Casting.h`` (`doxygen
111*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/Casting_8h-source.html>`__) file (note that you very
112*f4a2713aSLionel Sambucrarely have to include this file directly).
113*f4a2713aSLionel Sambuc
114*f4a2713aSLionel Sambuc``isa<>``:
115*f4a2713aSLionel Sambuc  The ``isa<>`` operator works exactly like the Java "``instanceof``" operator.
116*f4a2713aSLionel Sambuc  It returns true or false depending on whether a reference or pointer points to
117*f4a2713aSLionel Sambuc  an instance of the specified class.  This can be very useful for constraint
118*f4a2713aSLionel Sambuc  checking of various sorts (example below).
119*f4a2713aSLionel Sambuc
120*f4a2713aSLionel Sambuc``cast<>``:
121*f4a2713aSLionel Sambuc  The ``cast<>`` operator is a "checked cast" operation.  It converts a pointer
122*f4a2713aSLionel Sambuc  or reference from a base class to a derived class, causing an assertion
123*f4a2713aSLionel Sambuc  failure if it is not really an instance of the right type.  This should be
124*f4a2713aSLionel Sambuc  used in cases where you have some information that makes you believe that
125*f4a2713aSLionel Sambuc  something is of the right type.  An example of the ``isa<>`` and ``cast<>``
126*f4a2713aSLionel Sambuc  template is:
127*f4a2713aSLionel Sambuc
128*f4a2713aSLionel Sambuc  .. code-block:: c++
129*f4a2713aSLionel Sambuc
130*f4a2713aSLionel Sambuc    static bool isLoopInvariant(const Value *V, const Loop *L) {
131*f4a2713aSLionel Sambuc      if (isa<Constant>(V) || isa<Argument>(V) || isa<GlobalValue>(V))
132*f4a2713aSLionel Sambuc        return true;
133*f4a2713aSLionel Sambuc
134*f4a2713aSLionel Sambuc      // Otherwise, it must be an instruction...
135*f4a2713aSLionel Sambuc      return !L->contains(cast<Instruction>(V)->getParent());
136*f4a2713aSLionel Sambuc    }
137*f4a2713aSLionel Sambuc
138*f4a2713aSLionel Sambuc  Note that you should **not** use an ``isa<>`` test followed by a ``cast<>``,
139*f4a2713aSLionel Sambuc  for that use the ``dyn_cast<>`` operator.
140*f4a2713aSLionel Sambuc
141*f4a2713aSLionel Sambuc``dyn_cast<>``:
142*f4a2713aSLionel Sambuc  The ``dyn_cast<>`` operator is a "checking cast" operation.  It checks to see
143*f4a2713aSLionel Sambuc  if the operand is of the specified type, and if so, returns a pointer to it
144*f4a2713aSLionel Sambuc  (this operator does not work with references).  If the operand is not of the
145*f4a2713aSLionel Sambuc  correct type, a null pointer is returned.  Thus, this works very much like
146*f4a2713aSLionel Sambuc  the ``dynamic_cast<>`` operator in C++, and should be used in the same
147*f4a2713aSLionel Sambuc  circumstances.  Typically, the ``dyn_cast<>`` operator is used in an ``if``
148*f4a2713aSLionel Sambuc  statement or some other flow control statement like this:
149*f4a2713aSLionel Sambuc
150*f4a2713aSLionel Sambuc  .. code-block:: c++
151*f4a2713aSLionel Sambuc
152*f4a2713aSLionel Sambuc    if (AllocationInst *AI = dyn_cast<AllocationInst>(Val)) {
153*f4a2713aSLionel Sambuc      // ...
154*f4a2713aSLionel Sambuc    }
155*f4a2713aSLionel Sambuc
156*f4a2713aSLionel Sambuc  This form of the ``if`` statement effectively combines together a call to
157*f4a2713aSLionel Sambuc  ``isa<>`` and a call to ``cast<>`` into one statement, which is very
158*f4a2713aSLionel Sambuc  convenient.
159*f4a2713aSLionel Sambuc
160*f4a2713aSLionel Sambuc  Note that the ``dyn_cast<>`` operator, like C++'s ``dynamic_cast<>`` or Java's
161*f4a2713aSLionel Sambuc  ``instanceof`` operator, can be abused.  In particular, you should not use big
162*f4a2713aSLionel Sambuc  chained ``if/then/else`` blocks to check for lots of different variants of
163*f4a2713aSLionel Sambuc  classes.  If you find yourself wanting to do this, it is much cleaner and more
164*f4a2713aSLionel Sambuc  efficient to use the ``InstVisitor`` class to dispatch over the instruction
165*f4a2713aSLionel Sambuc  type directly.
166*f4a2713aSLionel Sambuc
167*f4a2713aSLionel Sambuc``cast_or_null<>``:
168*f4a2713aSLionel Sambuc  The ``cast_or_null<>`` operator works just like the ``cast<>`` operator,
169*f4a2713aSLionel Sambuc  except that it allows for a null pointer as an argument (which it then
170*f4a2713aSLionel Sambuc  propagates).  This can sometimes be useful, allowing you to combine several
171*f4a2713aSLionel Sambuc  null checks into one.
172*f4a2713aSLionel Sambuc
173*f4a2713aSLionel Sambuc``dyn_cast_or_null<>``:
174*f4a2713aSLionel Sambuc  The ``dyn_cast_or_null<>`` operator works just like the ``dyn_cast<>``
175*f4a2713aSLionel Sambuc  operator, except that it allows for a null pointer as an argument (which it
176*f4a2713aSLionel Sambuc  then propagates).  This can sometimes be useful, allowing you to combine
177*f4a2713aSLionel Sambuc  several null checks into one.
178*f4a2713aSLionel Sambuc
179*f4a2713aSLionel SambucThese five templates can be used with any classes, whether they have a v-table
180*f4a2713aSLionel Sambucor not.  If you want to add support for these templates, see the document
181*f4a2713aSLionel Sambuc:doc:`How to set up LLVM-style RTTI for your class hierarchy
182*f4a2713aSLionel Sambuc<HowToSetUpLLVMStyleRTTI>`
183*f4a2713aSLionel Sambuc
184*f4a2713aSLionel Sambuc.. _string_apis:
185*f4a2713aSLionel Sambuc
186*f4a2713aSLionel SambucPassing strings (the ``StringRef`` and ``Twine`` classes)
187*f4a2713aSLionel Sambuc---------------------------------------------------------
188*f4a2713aSLionel Sambuc
189*f4a2713aSLionel SambucAlthough LLVM generally does not do much string manipulation, we do have several
190*f4a2713aSLionel Sambucimportant APIs which take strings.  Two important examples are the Value class
191*f4a2713aSLionel Sambuc-- which has names for instructions, functions, etc. -- and the ``StringMap``
192*f4a2713aSLionel Sambucclass which is used extensively in LLVM and Clang.
193*f4a2713aSLionel Sambuc
194*f4a2713aSLionel SambucThese are generic classes, and they need to be able to accept strings which may
195*f4a2713aSLionel Sambuchave embedded null characters.  Therefore, they cannot simply take a ``const
196*f4a2713aSLionel Sambucchar *``, and taking a ``const std::string&`` requires clients to perform a heap
197*f4a2713aSLionel Sambucallocation which is usually unnecessary.  Instead, many LLVM APIs use a
198*f4a2713aSLionel Sambuc``StringRef`` or a ``const Twine&`` for passing strings efficiently.
199*f4a2713aSLionel Sambuc
200*f4a2713aSLionel Sambuc.. _StringRef:
201*f4a2713aSLionel Sambuc
202*f4a2713aSLionel SambucThe ``StringRef`` class
203*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^
204*f4a2713aSLionel Sambuc
205*f4a2713aSLionel SambucThe ``StringRef`` data type represents a reference to a constant string (a
206*f4a2713aSLionel Sambuccharacter array and a length) and supports the common operations available on
207*f4a2713aSLionel Sambuc``std::string``, but does not require heap allocation.
208*f4a2713aSLionel Sambuc
209*f4a2713aSLionel SambucIt can be implicitly constructed using a C style null-terminated string, an
210*f4a2713aSLionel Sambuc``std::string``, or explicitly with a character pointer and length.  For
211*f4a2713aSLionel Sambucexample, the ``StringRef`` find function is declared as:
212*f4a2713aSLionel Sambuc
213*f4a2713aSLionel Sambuc.. code-block:: c++
214*f4a2713aSLionel Sambuc
215*f4a2713aSLionel Sambuc  iterator find(StringRef Key);
216*f4a2713aSLionel Sambuc
217*f4a2713aSLionel Sambucand clients can call it using any one of:
218*f4a2713aSLionel Sambuc
219*f4a2713aSLionel Sambuc.. code-block:: c++
220*f4a2713aSLionel Sambuc
221*f4a2713aSLionel Sambuc  Map.find("foo");                 // Lookup "foo"
222*f4a2713aSLionel Sambuc  Map.find(std::string("bar"));    // Lookup "bar"
223*f4a2713aSLionel Sambuc  Map.find(StringRef("\0baz", 4)); // Lookup "\0baz"
224*f4a2713aSLionel Sambuc
225*f4a2713aSLionel SambucSimilarly, APIs which need to return a string may return a ``StringRef``
226*f4a2713aSLionel Sambucinstance, which can be used directly or converted to an ``std::string`` using
227*f4a2713aSLionel Sambucthe ``str`` member function.  See ``llvm/ADT/StringRef.h`` (`doxygen
228*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/classllvm_1_1StringRef_8h-source.html>`__) for more
229*f4a2713aSLionel Sambucinformation.
230*f4a2713aSLionel Sambuc
231*f4a2713aSLionel SambucYou should rarely use the ``StringRef`` class directly, because it contains
232*f4a2713aSLionel Sambucpointers to external memory it is not generally safe to store an instance of the
233*f4a2713aSLionel Sambucclass (unless you know that the external storage will not be freed).
234*f4a2713aSLionel Sambuc``StringRef`` is small and pervasive enough in LLVM that it should always be
235*f4a2713aSLionel Sambucpassed by value.
236*f4a2713aSLionel Sambuc
237*f4a2713aSLionel SambucThe ``Twine`` class
238*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^
239*f4a2713aSLionel Sambuc
240*f4a2713aSLionel SambucThe ``Twine`` (`doxygen <http://llvm.org/doxygen/classllvm_1_1Twine.html>`__)
241*f4a2713aSLionel Sambucclass is an efficient way for APIs to accept concatenated strings.  For example,
242*f4a2713aSLionel Sambuca common LLVM paradigm is to name one instruction based on the name of another
243*f4a2713aSLionel Sambucinstruction with a suffix, for example:
244*f4a2713aSLionel Sambuc
245*f4a2713aSLionel Sambuc.. code-block:: c++
246*f4a2713aSLionel Sambuc
247*f4a2713aSLionel Sambuc    New = CmpInst::Create(..., SO->getName() + ".cmp");
248*f4a2713aSLionel Sambuc
249*f4a2713aSLionel SambucThe ``Twine`` class is effectively a lightweight `rope
250*f4a2713aSLionel Sambuc<http://en.wikipedia.org/wiki/Rope_(computer_science)>`_ which points to
251*f4a2713aSLionel Sambuctemporary (stack allocated) objects.  Twines can be implicitly constructed as
252*f4a2713aSLionel Sambucthe result of the plus operator applied to strings (i.e., a C strings, an
253*f4a2713aSLionel Sambuc``std::string``, or a ``StringRef``).  The twine delays the actual concatenation
254*f4a2713aSLionel Sambucof strings until it is actually required, at which point it can be efficiently
255*f4a2713aSLionel Sambucrendered directly into a character array.  This avoids unnecessary heap
256*f4a2713aSLionel Sambucallocation involved in constructing the temporary results of string
257*f4a2713aSLionel Sambucconcatenation.  See ``llvm/ADT/Twine.h`` (`doxygen
258*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/Twine_8h_source.html>`__) and :ref:`here <dss_twine>`
259*f4a2713aSLionel Sambucfor more information.
260*f4a2713aSLionel Sambuc
261*f4a2713aSLionel SambucAs with a ``StringRef``, ``Twine`` objects point to external memory and should
262*f4a2713aSLionel Sambucalmost never be stored or mentioned directly.  They are intended solely for use
263*f4a2713aSLionel Sambucwhen defining a function which should be able to efficiently accept concatenated
264*f4a2713aSLionel Sambucstrings.
265*f4a2713aSLionel Sambuc
266*f4a2713aSLionel Sambuc.. _DEBUG:
267*f4a2713aSLionel Sambuc
268*f4a2713aSLionel SambucThe ``DEBUG()`` macro and ``-debug`` option
269*f4a2713aSLionel Sambuc-------------------------------------------
270*f4a2713aSLionel Sambuc
271*f4a2713aSLionel SambucOften when working on your pass you will put a bunch of debugging printouts and
272*f4a2713aSLionel Sambucother code into your pass.  After you get it working, you want to remove it, but
273*f4a2713aSLionel Sambucyou may need it again in the future (to work out new bugs that you run across).
274*f4a2713aSLionel Sambuc
275*f4a2713aSLionel SambucNaturally, because of this, you don't want to delete the debug printouts, but
276*f4a2713aSLionel Sambucyou don't want them to always be noisy.  A standard compromise is to comment
277*f4a2713aSLionel Sambucthem out, allowing you to enable them if you need them in the future.
278*f4a2713aSLionel Sambuc
279*f4a2713aSLionel SambucThe ``llvm/Support/Debug.h`` (`doxygen
280*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/Debug_8h-source.html>`__) file provides a macro named
281*f4a2713aSLionel Sambuc``DEBUG()`` that is a much nicer solution to this problem.  Basically, you can
282*f4a2713aSLionel Sambucput arbitrary code into the argument of the ``DEBUG`` macro, and it is only
283*f4a2713aSLionel Sambucexecuted if '``opt``' (or any other tool) is run with the '``-debug``' command
284*f4a2713aSLionel Sambucline argument:
285*f4a2713aSLionel Sambuc
286*f4a2713aSLionel Sambuc.. code-block:: c++
287*f4a2713aSLionel Sambuc
288*f4a2713aSLionel Sambuc  DEBUG(errs() << "I am here!\n");
289*f4a2713aSLionel Sambuc
290*f4a2713aSLionel SambucThen you can run your pass like this:
291*f4a2713aSLionel Sambuc
292*f4a2713aSLionel Sambuc.. code-block:: none
293*f4a2713aSLionel Sambuc
294*f4a2713aSLionel Sambuc  $ opt < a.bc > /dev/null -mypass
295*f4a2713aSLionel Sambuc  <no output>
296*f4a2713aSLionel Sambuc  $ opt < a.bc > /dev/null -mypass -debug
297*f4a2713aSLionel Sambuc  I am here!
298*f4a2713aSLionel Sambuc
299*f4a2713aSLionel SambucUsing the ``DEBUG()`` macro instead of a home-brewed solution allows you to not
300*f4a2713aSLionel Sambuchave to create "yet another" command line option for the debug output for your
301*f4a2713aSLionel Sambucpass.  Note that ``DEBUG()`` macros are disabled for optimized builds, so they
302*f4a2713aSLionel Sambucdo not cause a performance impact at all (for the same reason, they should also
303*f4a2713aSLionel Sambucnot contain side-effects!).
304*f4a2713aSLionel Sambuc
305*f4a2713aSLionel SambucOne additional nice thing about the ``DEBUG()`` macro is that you can enable or
306*f4a2713aSLionel Sambucdisable it directly in gdb.  Just use "``set DebugFlag=0``" or "``set
307*f4a2713aSLionel SambucDebugFlag=1``" from the gdb if the program is running.  If the program hasn't
308*f4a2713aSLionel Sambucbeen started yet, you can always just run it with ``-debug``.
309*f4a2713aSLionel Sambuc
310*f4a2713aSLionel Sambuc.. _DEBUG_TYPE:
311*f4a2713aSLionel Sambuc
312*f4a2713aSLionel SambucFine grained debug info with ``DEBUG_TYPE`` and the ``-debug-only`` option
313*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
314*f4a2713aSLionel Sambuc
315*f4a2713aSLionel SambucSometimes you may find yourself in a situation where enabling ``-debug`` just
316*f4a2713aSLionel Sambucturns on **too much** information (such as when working on the code generator).
317*f4a2713aSLionel SambucIf you want to enable debug information with more fine-grained control, you
318*f4a2713aSLionel Sambucdefine the ``DEBUG_TYPE`` macro and the ``-debug`` only option as follows:
319*f4a2713aSLionel Sambuc
320*f4a2713aSLionel Sambuc.. code-block:: c++
321*f4a2713aSLionel Sambuc
322*f4a2713aSLionel Sambuc  #undef  DEBUG_TYPE
323*f4a2713aSLionel Sambuc  DEBUG(errs() << "No debug type\n");
324*f4a2713aSLionel Sambuc  #define DEBUG_TYPE "foo"
325*f4a2713aSLionel Sambuc  DEBUG(errs() << "'foo' debug type\n");
326*f4a2713aSLionel Sambuc  #undef  DEBUG_TYPE
327*f4a2713aSLionel Sambuc  #define DEBUG_TYPE "bar"
328*f4a2713aSLionel Sambuc  DEBUG(errs() << "'bar' debug type\n"));
329*f4a2713aSLionel Sambuc  #undef  DEBUG_TYPE
330*f4a2713aSLionel Sambuc  #define DEBUG_TYPE ""
331*f4a2713aSLionel Sambuc  DEBUG(errs() << "No debug type (2)\n");
332*f4a2713aSLionel Sambuc
333*f4a2713aSLionel SambucThen you can run your pass like this:
334*f4a2713aSLionel Sambuc
335*f4a2713aSLionel Sambuc.. code-block:: none
336*f4a2713aSLionel Sambuc
337*f4a2713aSLionel Sambuc  $ opt < a.bc > /dev/null -mypass
338*f4a2713aSLionel Sambuc  <no output>
339*f4a2713aSLionel Sambuc  $ opt < a.bc > /dev/null -mypass -debug
340*f4a2713aSLionel Sambuc  No debug type
341*f4a2713aSLionel Sambuc  'foo' debug type
342*f4a2713aSLionel Sambuc  'bar' debug type
343*f4a2713aSLionel Sambuc  No debug type (2)
344*f4a2713aSLionel Sambuc  $ opt < a.bc > /dev/null -mypass -debug-only=foo
345*f4a2713aSLionel Sambuc  'foo' debug type
346*f4a2713aSLionel Sambuc  $ opt < a.bc > /dev/null -mypass -debug-only=bar
347*f4a2713aSLionel Sambuc  'bar' debug type
348*f4a2713aSLionel Sambuc
349*f4a2713aSLionel SambucOf course, in practice, you should only set ``DEBUG_TYPE`` at the top of a file,
350*f4a2713aSLionel Sambucto specify the debug type for the entire module (if you do this before you
351*f4a2713aSLionel Sambuc``#include "llvm/Support/Debug.h"``, you don't have to insert the ugly
352*f4a2713aSLionel Sambuc``#undef``'s).  Also, you should use names more meaningful than "foo" and "bar",
353*f4a2713aSLionel Sambucbecause there is no system in place to ensure that names do not conflict.  If
354*f4a2713aSLionel Sambuctwo different modules use the same string, they will all be turned on when the
355*f4a2713aSLionel Sambucname is specified.  This allows, for example, all debug information for
356*f4a2713aSLionel Sambucinstruction scheduling to be enabled with ``-debug-type=InstrSched``, even if
357*f4a2713aSLionel Sambucthe source lives in multiple files.
358*f4a2713aSLionel Sambuc
359*f4a2713aSLionel SambucThe ``DEBUG_WITH_TYPE`` macro is also available for situations where you would
360*f4a2713aSLionel Sambuclike to set ``DEBUG_TYPE``, but only for one specific ``DEBUG`` statement.  It
361*f4a2713aSLionel Sambuctakes an additional first parameter, which is the type to use.  For example, the
362*f4a2713aSLionel Sambucpreceding example could be written as:
363*f4a2713aSLionel Sambuc
364*f4a2713aSLionel Sambuc.. code-block:: c++
365*f4a2713aSLionel Sambuc
366*f4a2713aSLionel Sambuc  DEBUG_WITH_TYPE("", errs() << "No debug type\n");
367*f4a2713aSLionel Sambuc  DEBUG_WITH_TYPE("foo", errs() << "'foo' debug type\n");
368*f4a2713aSLionel Sambuc  DEBUG_WITH_TYPE("bar", errs() << "'bar' debug type\n"));
369*f4a2713aSLionel Sambuc  DEBUG_WITH_TYPE("", errs() << "No debug type (2)\n");
370*f4a2713aSLionel Sambuc
371*f4a2713aSLionel Sambuc.. _Statistic:
372*f4a2713aSLionel Sambuc
373*f4a2713aSLionel SambucThe ``Statistic`` class & ``-stats`` option
374*f4a2713aSLionel Sambuc-------------------------------------------
375*f4a2713aSLionel Sambuc
376*f4a2713aSLionel SambucThe ``llvm/ADT/Statistic.h`` (`doxygen
377*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/Statistic_8h-source.html>`__) file provides a class
378*f4a2713aSLionel Sambucnamed ``Statistic`` that is used as a unified way to keep track of what the LLVM
379*f4a2713aSLionel Sambuccompiler is doing and how effective various optimizations are.  It is useful to
380*f4a2713aSLionel Sambucsee what optimizations are contributing to making a particular program run
381*f4a2713aSLionel Sambucfaster.
382*f4a2713aSLionel Sambuc
383*f4a2713aSLionel SambucOften you may run your pass on some big program, and you're interested to see
384*f4a2713aSLionel Sambuchow many times it makes a certain transformation.  Although you can do this with
385*f4a2713aSLionel Sambuchand inspection, or some ad-hoc method, this is a real pain and not very useful
386*f4a2713aSLionel Sambucfor big programs.  Using the ``Statistic`` class makes it very easy to keep
387*f4a2713aSLionel Sambuctrack of this information, and the calculated information is presented in a
388*f4a2713aSLionel Sambucuniform manner with the rest of the passes being executed.
389*f4a2713aSLionel Sambuc
390*f4a2713aSLionel SambucThere are many examples of ``Statistic`` uses, but the basics of using it are as
391*f4a2713aSLionel Sambucfollows:
392*f4a2713aSLionel Sambuc
393*f4a2713aSLionel Sambuc#. Define your statistic like this:
394*f4a2713aSLionel Sambuc
395*f4a2713aSLionel Sambuc  .. code-block:: c++
396*f4a2713aSLionel Sambuc
397*f4a2713aSLionel Sambuc    #define DEBUG_TYPE "mypassname"   // This goes before any #includes.
398*f4a2713aSLionel Sambuc    STATISTIC(NumXForms, "The # of times I did stuff");
399*f4a2713aSLionel Sambuc
400*f4a2713aSLionel Sambuc  The ``STATISTIC`` macro defines a static variable, whose name is specified by
401*f4a2713aSLionel Sambuc  the first argument.  The pass name is taken from the ``DEBUG_TYPE`` macro, and
402*f4a2713aSLionel Sambuc  the description is taken from the second argument.  The variable defined
403*f4a2713aSLionel Sambuc  ("NumXForms" in this case) acts like an unsigned integer.
404*f4a2713aSLionel Sambuc
405*f4a2713aSLionel Sambuc#. Whenever you make a transformation, bump the counter:
406*f4a2713aSLionel Sambuc
407*f4a2713aSLionel Sambuc  .. code-block:: c++
408*f4a2713aSLionel Sambuc
409*f4a2713aSLionel Sambuc    ++NumXForms;   // I did stuff!
410*f4a2713aSLionel Sambuc
411*f4a2713aSLionel SambucThat's all you have to do.  To get '``opt``' to print out the statistics
412*f4a2713aSLionel Sambucgathered, use the '``-stats``' option:
413*f4a2713aSLionel Sambuc
414*f4a2713aSLionel Sambuc.. code-block:: none
415*f4a2713aSLionel Sambuc
416*f4a2713aSLionel Sambuc  $ opt -stats -mypassname < program.bc > /dev/null
417*f4a2713aSLionel Sambuc  ... statistics output ...
418*f4a2713aSLionel Sambuc
419*f4a2713aSLionel SambucWhen running ``opt`` on a C file from the SPEC benchmark suite, it gives a
420*f4a2713aSLionel Sambucreport that looks like this:
421*f4a2713aSLionel Sambuc
422*f4a2713aSLionel Sambuc.. code-block:: none
423*f4a2713aSLionel Sambuc
424*f4a2713aSLionel Sambuc   7646 bitcodewriter   - Number of normal instructions
425*f4a2713aSLionel Sambuc    725 bitcodewriter   - Number of oversized instructions
426*f4a2713aSLionel Sambuc 129996 bitcodewriter   - Number of bitcode bytes written
427*f4a2713aSLionel Sambuc   2817 raise           - Number of insts DCEd or constprop'd
428*f4a2713aSLionel Sambuc   3213 raise           - Number of cast-of-self removed
429*f4a2713aSLionel Sambuc   5046 raise           - Number of expression trees converted
430*f4a2713aSLionel Sambuc     75 raise           - Number of other getelementptr's formed
431*f4a2713aSLionel Sambuc    138 raise           - Number of load/store peepholes
432*f4a2713aSLionel Sambuc     42 deadtypeelim    - Number of unused typenames removed from symtab
433*f4a2713aSLionel Sambuc    392 funcresolve     - Number of varargs functions resolved
434*f4a2713aSLionel Sambuc     27 globaldce       - Number of global variables removed
435*f4a2713aSLionel Sambuc      2 adce            - Number of basic blocks removed
436*f4a2713aSLionel Sambuc    134 cee             - Number of branches revectored
437*f4a2713aSLionel Sambuc     49 cee             - Number of setcc instruction eliminated
438*f4a2713aSLionel Sambuc    532 gcse            - Number of loads removed
439*f4a2713aSLionel Sambuc   2919 gcse            - Number of instructions removed
440*f4a2713aSLionel Sambuc     86 indvars         - Number of canonical indvars added
441*f4a2713aSLionel Sambuc     87 indvars         - Number of aux indvars removed
442*f4a2713aSLionel Sambuc     25 instcombine     - Number of dead inst eliminate
443*f4a2713aSLionel Sambuc    434 instcombine     - Number of insts combined
444*f4a2713aSLionel Sambuc    248 licm            - Number of load insts hoisted
445*f4a2713aSLionel Sambuc   1298 licm            - Number of insts hoisted to a loop pre-header
446*f4a2713aSLionel Sambuc      3 licm            - Number of insts hoisted to multiple loop preds (bad, no loop pre-header)
447*f4a2713aSLionel Sambuc     75 mem2reg         - Number of alloca's promoted
448*f4a2713aSLionel Sambuc   1444 cfgsimplify     - Number of blocks simplified
449*f4a2713aSLionel Sambuc
450*f4a2713aSLionel SambucObviously, with so many optimizations, having a unified framework for this stuff
451*f4a2713aSLionel Sambucis very nice.  Making your pass fit well into the framework makes it more
452*f4a2713aSLionel Sambucmaintainable and useful.
453*f4a2713aSLionel Sambuc
454*f4a2713aSLionel Sambuc.. _ViewGraph:
455*f4a2713aSLionel Sambuc
456*f4a2713aSLionel SambucViewing graphs while debugging code
457*f4a2713aSLionel Sambuc-----------------------------------
458*f4a2713aSLionel Sambuc
459*f4a2713aSLionel SambucSeveral of the important data structures in LLVM are graphs: for example CFGs
460*f4a2713aSLionel Sambucmade out of LLVM :ref:`BasicBlocks <BasicBlock>`, CFGs made out of LLVM
461*f4a2713aSLionel Sambuc:ref:`MachineBasicBlocks <MachineBasicBlock>`, and :ref:`Instruction Selection
462*f4a2713aSLionel SambucDAGs <SelectionDAG>`.  In many cases, while debugging various parts of the
463*f4a2713aSLionel Sambuccompiler, it is nice to instantly visualize these graphs.
464*f4a2713aSLionel Sambuc
465*f4a2713aSLionel SambucLLVM provides several callbacks that are available in a debug build to do
466*f4a2713aSLionel Sambucexactly that.  If you call the ``Function::viewCFG()`` method, for example, the
467*f4a2713aSLionel Sambuccurrent LLVM tool will pop up a window containing the CFG for the function where
468*f4a2713aSLionel Sambuceach basic block is a node in the graph, and each node contains the instructions
469*f4a2713aSLionel Sambucin the block.  Similarly, there also exists ``Function::viewCFGOnly()`` (does
470*f4a2713aSLionel Sambucnot include the instructions), the ``MachineFunction::viewCFG()`` and
471*f4a2713aSLionel Sambuc``MachineFunction::viewCFGOnly()``, and the ``SelectionDAG::viewGraph()``
472*f4a2713aSLionel Sambucmethods.  Within GDB, for example, you can usually use something like ``call
473*f4a2713aSLionel SambucDAG.viewGraph()`` to pop up a window.  Alternatively, you can sprinkle calls to
474*f4a2713aSLionel Sambucthese functions in your code in places you want to debug.
475*f4a2713aSLionel Sambuc
476*f4a2713aSLionel SambucGetting this to work requires a small amount of configuration.  On Unix systems
477*f4a2713aSLionel Sambucwith X11, install the `graphviz <http://www.graphviz.org>`_ toolkit, and make
478*f4a2713aSLionel Sambucsure 'dot' and 'gv' are in your path.  If you are running on Mac OS/X, download
479*f4a2713aSLionel Sambucand install the Mac OS/X `Graphviz program
480*f4a2713aSLionel Sambuc<http://www.pixelglow.com/graphviz/>`_ and add
481*f4a2713aSLionel Sambuc``/Applications/Graphviz.app/Contents/MacOS/`` (or wherever you install it) to
482*f4a2713aSLionel Sambucyour path.  Once in your system and path are set up, rerun the LLVM configure
483*f4a2713aSLionel Sambucscript and rebuild LLVM to enable this functionality.
484*f4a2713aSLionel Sambuc
485*f4a2713aSLionel Sambuc``SelectionDAG`` has been extended to make it easier to locate *interesting*
486*f4a2713aSLionel Sambucnodes in large complex graphs.  From gdb, if you ``call DAG.setGraphColor(node,
487*f4a2713aSLionel Sambuc"color")``, then the next ``call DAG.viewGraph()`` would highlight the node in
488*f4a2713aSLionel Sambucthe specified color (choices of colors can be found at `colors
489*f4a2713aSLionel Sambuc<http://www.graphviz.org/doc/info/colors.html>`_.) More complex node attributes
490*f4a2713aSLionel Sambuccan be provided with ``call DAG.setGraphAttrs(node, "attributes")`` (choices can
491*f4a2713aSLionel Sambucbe found at `Graph attributes <http://www.graphviz.org/doc/info/attrs.html>`_.)
492*f4a2713aSLionel SambucIf you want to restart and clear all the current graph attributes, then you can
493*f4a2713aSLionel Sambuc``call DAG.clearGraphAttrs()``.
494*f4a2713aSLionel Sambuc
495*f4a2713aSLionel SambucNote that graph visualization features are compiled out of Release builds to
496*f4a2713aSLionel Sambucreduce file size.  This means that you need a Debug+Asserts or Release+Asserts
497*f4a2713aSLionel Sambucbuild to use these features.
498*f4a2713aSLionel Sambuc
499*f4a2713aSLionel Sambuc.. _datastructure:
500*f4a2713aSLionel Sambuc
501*f4a2713aSLionel SambucPicking the Right Data Structure for a Task
502*f4a2713aSLionel Sambuc===========================================
503*f4a2713aSLionel Sambuc
504*f4a2713aSLionel SambucLLVM has a plethora of data structures in the ``llvm/ADT/`` directory, and we
505*f4a2713aSLionel Sambuccommonly use STL data structures.  This section describes the trade-offs you
506*f4a2713aSLionel Sambucshould consider when you pick one.
507*f4a2713aSLionel Sambuc
508*f4a2713aSLionel SambucThe first step is a choose your own adventure: do you want a sequential
509*f4a2713aSLionel Sambuccontainer, a set-like container, or a map-like container?  The most important
510*f4a2713aSLionel Sambucthing when choosing a container is the algorithmic properties of how you plan to
511*f4a2713aSLionel Sambucaccess the container.  Based on that, you should use:
512*f4a2713aSLionel Sambuc
513*f4a2713aSLionel Sambuc
514*f4a2713aSLionel Sambuc* a :ref:`map-like <ds_map>` container if you need efficient look-up of a
515*f4a2713aSLionel Sambuc  value based on another value.  Map-like containers also support efficient
516*f4a2713aSLionel Sambuc  queries for containment (whether a key is in the map).  Map-like containers
517*f4a2713aSLionel Sambuc  generally do not support efficient reverse mapping (values to keys).  If you
518*f4a2713aSLionel Sambuc  need that, use two maps.  Some map-like containers also support efficient
519*f4a2713aSLionel Sambuc  iteration through the keys in sorted order.  Map-like containers are the most
520*f4a2713aSLionel Sambuc  expensive sort, only use them if you need one of these capabilities.
521*f4a2713aSLionel Sambuc
522*f4a2713aSLionel Sambuc* a :ref:`set-like <ds_set>` container if you need to put a bunch of stuff into
523*f4a2713aSLionel Sambuc  a container that automatically eliminates duplicates.  Some set-like
524*f4a2713aSLionel Sambuc  containers support efficient iteration through the elements in sorted order.
525*f4a2713aSLionel Sambuc  Set-like containers are more expensive than sequential containers.
526*f4a2713aSLionel Sambuc
527*f4a2713aSLionel Sambuc* a :ref:`sequential <ds_sequential>` container provides the most efficient way
528*f4a2713aSLionel Sambuc  to add elements and keeps track of the order they are added to the collection.
529*f4a2713aSLionel Sambuc  They permit duplicates and support efficient iteration, but do not support
530*f4a2713aSLionel Sambuc  efficient look-up based on a key.
531*f4a2713aSLionel Sambuc
532*f4a2713aSLionel Sambuc* a :ref:`string <ds_string>` container is a specialized sequential container or
533*f4a2713aSLionel Sambuc  reference structure that is used for character or byte arrays.
534*f4a2713aSLionel Sambuc
535*f4a2713aSLionel Sambuc* a :ref:`bit <ds_bit>` container provides an efficient way to store and
536*f4a2713aSLionel Sambuc  perform set operations on sets of numeric id's, while automatically
537*f4a2713aSLionel Sambuc  eliminating duplicates.  Bit containers require a maximum of 1 bit for each
538*f4a2713aSLionel Sambuc  identifier you want to store.
539*f4a2713aSLionel Sambuc
540*f4a2713aSLionel SambucOnce the proper category of container is determined, you can fine tune the
541*f4a2713aSLionel Sambucmemory use, constant factors, and cache behaviors of access by intelligently
542*f4a2713aSLionel Sambucpicking a member of the category.  Note that constant factors and cache behavior
543*f4a2713aSLionel Sambuccan be a big deal.  If you have a vector that usually only contains a few
544*f4a2713aSLionel Sambucelements (but could contain many), for example, it's much better to use
545*f4a2713aSLionel Sambuc:ref:`SmallVector <dss_smallvector>` than :ref:`vector <dss_vector>`.  Doing so
546*f4a2713aSLionel Sambucavoids (relatively) expensive malloc/free calls, which dwarf the cost of adding
547*f4a2713aSLionel Sambucthe elements to the container.
548*f4a2713aSLionel Sambuc
549*f4a2713aSLionel Sambuc.. _ds_sequential:
550*f4a2713aSLionel Sambuc
551*f4a2713aSLionel SambucSequential Containers (std::vector, std::list, etc)
552*f4a2713aSLionel Sambuc---------------------------------------------------
553*f4a2713aSLionel Sambuc
554*f4a2713aSLionel SambucThere are a variety of sequential containers available for you, based on your
555*f4a2713aSLionel Sambucneeds.  Pick the first in this section that will do what you want.
556*f4a2713aSLionel Sambuc
557*f4a2713aSLionel Sambuc.. _dss_arrayref:
558*f4a2713aSLionel Sambuc
559*f4a2713aSLionel Sambucllvm/ADT/ArrayRef.h
560*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^
561*f4a2713aSLionel Sambuc
562*f4a2713aSLionel SambucThe ``llvm::ArrayRef`` class is the preferred class to use in an interface that
563*f4a2713aSLionel Sambucaccepts a sequential list of elements in memory and just reads from them.  By
564*f4a2713aSLionel Sambuctaking an ``ArrayRef``, the API can be passed a fixed size array, an
565*f4a2713aSLionel Sambuc``std::vector``, an ``llvm::SmallVector`` and anything else that is contiguous
566*f4a2713aSLionel Sambucin memory.
567*f4a2713aSLionel Sambuc
568*f4a2713aSLionel Sambuc.. _dss_fixedarrays:
569*f4a2713aSLionel Sambuc
570*f4a2713aSLionel SambucFixed Size Arrays
571*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^
572*f4a2713aSLionel Sambuc
573*f4a2713aSLionel SambucFixed size arrays are very simple and very fast.  They are good if you know
574*f4a2713aSLionel Sambucexactly how many elements you have, or you have a (low) upper bound on how many
575*f4a2713aSLionel Sambucyou have.
576*f4a2713aSLionel Sambuc
577*f4a2713aSLionel Sambuc.. _dss_heaparrays:
578*f4a2713aSLionel Sambuc
579*f4a2713aSLionel SambucHeap Allocated Arrays
580*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
581*f4a2713aSLionel Sambuc
582*f4a2713aSLionel SambucHeap allocated arrays (``new[]`` + ``delete[]``) are also simple.  They are good
583*f4a2713aSLionel Sambucif the number of elements is variable, if you know how many elements you will
584*f4a2713aSLionel Sambucneed before the array is allocated, and if the array is usually large (if not,
585*f4a2713aSLionel Sambucconsider a :ref:`SmallVector <dss_smallvector>`).  The cost of a heap allocated
586*f4a2713aSLionel Sambucarray is the cost of the new/delete (aka malloc/free).  Also note that if you
587*f4a2713aSLionel Sambucare allocating an array of a type with a constructor, the constructor and
588*f4a2713aSLionel Sambucdestructors will be run for every element in the array (re-sizable vectors only
589*f4a2713aSLionel Sambucconstruct those elements actually used).
590*f4a2713aSLionel Sambuc
591*f4a2713aSLionel Sambuc.. _dss_tinyptrvector:
592*f4a2713aSLionel Sambuc
593*f4a2713aSLionel Sambucllvm/ADT/TinyPtrVector.h
594*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^
595*f4a2713aSLionel Sambuc
596*f4a2713aSLionel Sambuc``TinyPtrVector<Type>`` is a highly specialized collection class that is
597*f4a2713aSLionel Sambucoptimized to avoid allocation in the case when a vector has zero or one
598*f4a2713aSLionel Sambucelements.  It has two major restrictions: 1) it can only hold values of pointer
599*f4a2713aSLionel Sambuctype, and 2) it cannot hold a null pointer.
600*f4a2713aSLionel Sambuc
601*f4a2713aSLionel SambucSince this container is highly specialized, it is rarely used.
602*f4a2713aSLionel Sambuc
603*f4a2713aSLionel Sambuc.. _dss_smallvector:
604*f4a2713aSLionel Sambuc
605*f4a2713aSLionel Sambucllvm/ADT/SmallVector.h
606*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^
607*f4a2713aSLionel Sambuc
608*f4a2713aSLionel Sambuc``SmallVector<Type, N>`` is a simple class that looks and smells just like
609*f4a2713aSLionel Sambuc``vector<Type>``: it supports efficient iteration, lays out elements in memory
610*f4a2713aSLionel Sambucorder (so you can do pointer arithmetic between elements), supports efficient
611*f4a2713aSLionel Sambucpush_back/pop_back operations, supports efficient random access to its elements,
612*f4a2713aSLionel Sambucetc.
613*f4a2713aSLionel Sambuc
614*f4a2713aSLionel SambucThe advantage of SmallVector is that it allocates space for some number of
615*f4a2713aSLionel Sambucelements (N) **in the object itself**.  Because of this, if the SmallVector is
616*f4a2713aSLionel Sambucdynamically smaller than N, no malloc is performed.  This can be a big win in
617*f4a2713aSLionel Sambuccases where the malloc/free call is far more expensive than the code that
618*f4a2713aSLionel Sambucfiddles around with the elements.
619*f4a2713aSLionel Sambuc
620*f4a2713aSLionel SambucThis is good for vectors that are "usually small" (e.g. the number of
621*f4a2713aSLionel Sambucpredecessors/successors of a block is usually less than 8).  On the other hand,
622*f4a2713aSLionel Sambucthis makes the size of the SmallVector itself large, so you don't want to
623*f4a2713aSLionel Sambucallocate lots of them (doing so will waste a lot of space).  As such,
624*f4a2713aSLionel SambucSmallVectors are most useful when on the stack.
625*f4a2713aSLionel Sambuc
626*f4a2713aSLionel SambucSmallVector also provides a nice portable and efficient replacement for
627*f4a2713aSLionel Sambuc``alloca``.
628*f4a2713aSLionel Sambuc
629*f4a2713aSLionel Sambuc.. note::
630*f4a2713aSLionel Sambuc
631*f4a2713aSLionel Sambuc   Prefer to use ``SmallVectorImpl<T>`` as a parameter type.
632*f4a2713aSLionel Sambuc
633*f4a2713aSLionel Sambuc   In APIs that don't care about the "small size" (most?), prefer to use
634*f4a2713aSLionel Sambuc   the ``SmallVectorImpl<T>`` class, which is basically just the "vector
635*f4a2713aSLionel Sambuc   header" (and methods) without the elements allocated after it. Note that
636*f4a2713aSLionel Sambuc   ``SmallVector<T, N>`` inherits from ``SmallVectorImpl<T>`` so the
637*f4a2713aSLionel Sambuc   conversion is implicit and costs nothing. E.g.
638*f4a2713aSLionel Sambuc
639*f4a2713aSLionel Sambuc   .. code-block:: c++
640*f4a2713aSLionel Sambuc
641*f4a2713aSLionel Sambuc      // BAD: Clients cannot pass e.g. SmallVector<Foo, 4>.
642*f4a2713aSLionel Sambuc      hardcodedSmallSize(SmallVector<Foo, 2> &Out);
643*f4a2713aSLionel Sambuc      // GOOD: Clients can pass any SmallVector<Foo, N>.
644*f4a2713aSLionel Sambuc      allowsAnySmallSize(SmallVectorImpl<Foo> &Out);
645*f4a2713aSLionel Sambuc
646*f4a2713aSLionel Sambuc      void someFunc() {
647*f4a2713aSLionel Sambuc        SmallVector<Foo, 8> Vec;
648*f4a2713aSLionel Sambuc        hardcodedSmallSize(Vec); // Error.
649*f4a2713aSLionel Sambuc        allowsAnySmallSize(Vec); // Works.
650*f4a2713aSLionel Sambuc      }
651*f4a2713aSLionel Sambuc
652*f4a2713aSLionel Sambuc   Even though it has "``Impl``" in the name, this is so widely used that
653*f4a2713aSLionel Sambuc   it really isn't "private to the implementation" anymore. A name like
654*f4a2713aSLionel Sambuc   ``SmallVectorHeader`` would be more appropriate.
655*f4a2713aSLionel Sambuc
656*f4a2713aSLionel Sambuc.. _dss_vector:
657*f4a2713aSLionel Sambuc
658*f4a2713aSLionel Sambuc<vector>
659*f4a2713aSLionel Sambuc^^^^^^^^
660*f4a2713aSLionel Sambuc
661*f4a2713aSLionel Sambuc``std::vector`` is well loved and respected.  It is useful when SmallVector
662*f4a2713aSLionel Sambucisn't: when the size of the vector is often large (thus the small optimization
663*f4a2713aSLionel Sambucwill rarely be a benefit) or if you will be allocating many instances of the
664*f4a2713aSLionel Sambucvector itself (which would waste space for elements that aren't in the
665*f4a2713aSLionel Sambuccontainer).  vector is also useful when interfacing with code that expects
666*f4a2713aSLionel Sambucvectors :).
667*f4a2713aSLionel Sambuc
668*f4a2713aSLionel SambucOne worthwhile note about std::vector: avoid code like this:
669*f4a2713aSLionel Sambuc
670*f4a2713aSLionel Sambuc.. code-block:: c++
671*f4a2713aSLionel Sambuc
672*f4a2713aSLionel Sambuc  for ( ... ) {
673*f4a2713aSLionel Sambuc     std::vector<foo> V;
674*f4a2713aSLionel Sambuc     // make use of V.
675*f4a2713aSLionel Sambuc  }
676*f4a2713aSLionel Sambuc
677*f4a2713aSLionel SambucInstead, write this as:
678*f4a2713aSLionel Sambuc
679*f4a2713aSLionel Sambuc.. code-block:: c++
680*f4a2713aSLionel Sambuc
681*f4a2713aSLionel Sambuc  std::vector<foo> V;
682*f4a2713aSLionel Sambuc  for ( ... ) {
683*f4a2713aSLionel Sambuc     // make use of V.
684*f4a2713aSLionel Sambuc     V.clear();
685*f4a2713aSLionel Sambuc  }
686*f4a2713aSLionel Sambuc
687*f4a2713aSLionel SambucDoing so will save (at least) one heap allocation and free per iteration of the
688*f4a2713aSLionel Sambucloop.
689*f4a2713aSLionel Sambuc
690*f4a2713aSLionel Sambuc.. _dss_deque:
691*f4a2713aSLionel Sambuc
692*f4a2713aSLionel Sambuc<deque>
693*f4a2713aSLionel Sambuc^^^^^^^
694*f4a2713aSLionel Sambuc
695*f4a2713aSLionel Sambuc``std::deque`` is, in some senses, a generalized version of ``std::vector``.
696*f4a2713aSLionel SambucLike ``std::vector``, it provides constant time random access and other similar
697*f4a2713aSLionel Sambucproperties, but it also provides efficient access to the front of the list.  It
698*f4a2713aSLionel Sambucdoes not guarantee continuity of elements within memory.
699*f4a2713aSLionel Sambuc
700*f4a2713aSLionel SambucIn exchange for this extra flexibility, ``std::deque`` has significantly higher
701*f4a2713aSLionel Sambucconstant factor costs than ``std::vector``.  If possible, use ``std::vector`` or
702*f4a2713aSLionel Sambucsomething cheaper.
703*f4a2713aSLionel Sambuc
704*f4a2713aSLionel Sambuc.. _dss_list:
705*f4a2713aSLionel Sambuc
706*f4a2713aSLionel Sambuc<list>
707*f4a2713aSLionel Sambuc^^^^^^
708*f4a2713aSLionel Sambuc
709*f4a2713aSLionel Sambuc``std::list`` is an extremely inefficient class that is rarely useful.  It
710*f4a2713aSLionel Sambucperforms a heap allocation for every element inserted into it, thus having an
711*f4a2713aSLionel Sambucextremely high constant factor, particularly for small data types.
712*f4a2713aSLionel Sambuc``std::list`` also only supports bidirectional iteration, not random access
713*f4a2713aSLionel Sambuciteration.
714*f4a2713aSLionel Sambuc
715*f4a2713aSLionel SambucIn exchange for this high cost, std::list supports efficient access to both ends
716*f4a2713aSLionel Sambucof the list (like ``std::deque``, but unlike ``std::vector`` or
717*f4a2713aSLionel Sambuc``SmallVector``).  In addition, the iterator invalidation characteristics of
718*f4a2713aSLionel Sambucstd::list are stronger than that of a vector class: inserting or removing an
719*f4a2713aSLionel Sambucelement into the list does not invalidate iterator or pointers to other elements
720*f4a2713aSLionel Sambucin the list.
721*f4a2713aSLionel Sambuc
722*f4a2713aSLionel Sambuc.. _dss_ilist:
723*f4a2713aSLionel Sambuc
724*f4a2713aSLionel Sambucllvm/ADT/ilist.h
725*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^
726*f4a2713aSLionel Sambuc
727*f4a2713aSLionel Sambuc``ilist<T>`` implements an 'intrusive' doubly-linked list.  It is intrusive,
728*f4a2713aSLionel Sambucbecause it requires the element to store and provide access to the prev/next
729*f4a2713aSLionel Sambucpointers for the list.
730*f4a2713aSLionel Sambuc
731*f4a2713aSLionel Sambuc``ilist`` has the same drawbacks as ``std::list``, and additionally requires an
732*f4a2713aSLionel Sambuc``ilist_traits`` implementation for the element type, but it provides some novel
733*f4a2713aSLionel Sambuccharacteristics.  In particular, it can efficiently store polymorphic objects,
734*f4a2713aSLionel Sambucthe traits class is informed when an element is inserted or removed from the
735*f4a2713aSLionel Sambuclist, and ``ilist``\ s are guaranteed to support a constant-time splice
736*f4a2713aSLionel Sambucoperation.
737*f4a2713aSLionel Sambuc
738*f4a2713aSLionel SambucThese properties are exactly what we want for things like ``Instruction``\ s and
739*f4a2713aSLionel Sambucbasic blocks, which is why these are implemented with ``ilist``\ s.
740*f4a2713aSLionel Sambuc
741*f4a2713aSLionel SambucRelated classes of interest are explained in the following subsections:
742*f4a2713aSLionel Sambuc
743*f4a2713aSLionel Sambuc* :ref:`ilist_traits <dss_ilist_traits>`
744*f4a2713aSLionel Sambuc
745*f4a2713aSLionel Sambuc* :ref:`iplist <dss_iplist>`
746*f4a2713aSLionel Sambuc
747*f4a2713aSLionel Sambuc* :ref:`llvm/ADT/ilist_node.h <dss_ilist_node>`
748*f4a2713aSLionel Sambuc
749*f4a2713aSLionel Sambuc* :ref:`Sentinels <dss_ilist_sentinel>`
750*f4a2713aSLionel Sambuc
751*f4a2713aSLionel Sambuc.. _dss_packedvector:
752*f4a2713aSLionel Sambuc
753*f4a2713aSLionel Sambucllvm/ADT/PackedVector.h
754*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^
755*f4a2713aSLionel Sambuc
756*f4a2713aSLionel SambucUseful for storing a vector of values using only a few number of bits for each
757*f4a2713aSLionel Sambucvalue.  Apart from the standard operations of a vector-like container, it can
758*f4a2713aSLionel Sambucalso perform an 'or' set operation.
759*f4a2713aSLionel Sambuc
760*f4a2713aSLionel SambucFor example:
761*f4a2713aSLionel Sambuc
762*f4a2713aSLionel Sambuc.. code-block:: c++
763*f4a2713aSLionel Sambuc
764*f4a2713aSLionel Sambuc  enum State {
765*f4a2713aSLionel Sambuc      None = 0x0,
766*f4a2713aSLionel Sambuc      FirstCondition = 0x1,
767*f4a2713aSLionel Sambuc      SecondCondition = 0x2,
768*f4a2713aSLionel Sambuc      Both = 0x3
769*f4a2713aSLionel Sambuc  };
770*f4a2713aSLionel Sambuc
771*f4a2713aSLionel Sambuc  State get() {
772*f4a2713aSLionel Sambuc      PackedVector<State, 2> Vec1;
773*f4a2713aSLionel Sambuc      Vec1.push_back(FirstCondition);
774*f4a2713aSLionel Sambuc
775*f4a2713aSLionel Sambuc      PackedVector<State, 2> Vec2;
776*f4a2713aSLionel Sambuc      Vec2.push_back(SecondCondition);
777*f4a2713aSLionel Sambuc
778*f4a2713aSLionel Sambuc      Vec1 |= Vec2;
779*f4a2713aSLionel Sambuc      return Vec1[0]; // returns 'Both'.
780*f4a2713aSLionel Sambuc  }
781*f4a2713aSLionel Sambuc
782*f4a2713aSLionel Sambuc.. _dss_ilist_traits:
783*f4a2713aSLionel Sambuc
784*f4a2713aSLionel Sambucilist_traits
785*f4a2713aSLionel Sambuc^^^^^^^^^^^^
786*f4a2713aSLionel Sambuc
787*f4a2713aSLionel Sambuc``ilist_traits<T>`` is ``ilist<T>``'s customization mechanism. ``iplist<T>``
788*f4a2713aSLionel Sambuc(and consequently ``ilist<T>``) publicly derive from this traits class.
789*f4a2713aSLionel Sambuc
790*f4a2713aSLionel Sambuc.. _dss_iplist:
791*f4a2713aSLionel Sambuc
792*f4a2713aSLionel Sambuciplist
793*f4a2713aSLionel Sambuc^^^^^^
794*f4a2713aSLionel Sambuc
795*f4a2713aSLionel Sambuc``iplist<T>`` is ``ilist<T>``'s base and as such supports a slightly narrower
796*f4a2713aSLionel Sambucinterface.  Notably, inserters from ``T&`` are absent.
797*f4a2713aSLionel Sambuc
798*f4a2713aSLionel Sambuc``ilist_traits<T>`` is a public base of this class and can be used for a wide
799*f4a2713aSLionel Sambucvariety of customizations.
800*f4a2713aSLionel Sambuc
801*f4a2713aSLionel Sambuc.. _dss_ilist_node:
802*f4a2713aSLionel Sambuc
803*f4a2713aSLionel Sambucllvm/ADT/ilist_node.h
804*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
805*f4a2713aSLionel Sambuc
806*f4a2713aSLionel Sambuc``ilist_node<T>`` implements a the forward and backward links that are expected
807*f4a2713aSLionel Sambucby the ``ilist<T>`` (and analogous containers) in the default manner.
808*f4a2713aSLionel Sambuc
809*f4a2713aSLionel Sambuc``ilist_node<T>``\ s are meant to be embedded in the node type ``T``, usually
810*f4a2713aSLionel Sambuc``T`` publicly derives from ``ilist_node<T>``.
811*f4a2713aSLionel Sambuc
812*f4a2713aSLionel Sambuc.. _dss_ilist_sentinel:
813*f4a2713aSLionel Sambuc
814*f4a2713aSLionel SambucSentinels
815*f4a2713aSLionel Sambuc^^^^^^^^^
816*f4a2713aSLionel Sambuc
817*f4a2713aSLionel Sambuc``ilist``\ s have another specialty that must be considered.  To be a good
818*f4a2713aSLionel Sambuccitizen in the C++ ecosystem, it needs to support the standard container
819*f4a2713aSLionel Sambucoperations, such as ``begin`` and ``end`` iterators, etc.  Also, the
820*f4a2713aSLionel Sambuc``operator--`` must work correctly on the ``end`` iterator in the case of
821*f4a2713aSLionel Sambucnon-empty ``ilist``\ s.
822*f4a2713aSLionel Sambuc
823*f4a2713aSLionel SambucThe only sensible solution to this problem is to allocate a so-called *sentinel*
824*f4a2713aSLionel Sambucalong with the intrusive list, which serves as the ``end`` iterator, providing
825*f4a2713aSLionel Sambucthe back-link to the last element.  However conforming to the C++ convention it
826*f4a2713aSLionel Sambucis illegal to ``operator++`` beyond the sentinel and it also must not be
827*f4a2713aSLionel Sambucdereferenced.
828*f4a2713aSLionel Sambuc
829*f4a2713aSLionel SambucThese constraints allow for some implementation freedom to the ``ilist`` how to
830*f4a2713aSLionel Sambucallocate and store the sentinel.  The corresponding policy is dictated by
831*f4a2713aSLionel Sambuc``ilist_traits<T>``.  By default a ``T`` gets heap-allocated whenever the need
832*f4a2713aSLionel Sambucfor a sentinel arises.
833*f4a2713aSLionel Sambuc
834*f4a2713aSLionel SambucWhile the default policy is sufficient in most cases, it may break down when
835*f4a2713aSLionel Sambuc``T`` does not provide a default constructor.  Also, in the case of many
836*f4a2713aSLionel Sambucinstances of ``ilist``\ s, the memory overhead of the associated sentinels is
837*f4a2713aSLionel Sambucwasted.  To alleviate the situation with numerous and voluminous
838*f4a2713aSLionel Sambuc``T``-sentinels, sometimes a trick is employed, leading to *ghostly sentinels*.
839*f4a2713aSLionel Sambuc
840*f4a2713aSLionel SambucGhostly sentinels are obtained by specially-crafted ``ilist_traits<T>`` which
841*f4a2713aSLionel Sambucsuperpose the sentinel with the ``ilist`` instance in memory.  Pointer
842*f4a2713aSLionel Sambucarithmetic is used to obtain the sentinel, which is relative to the ``ilist``'s
843*f4a2713aSLionel Sambuc``this`` pointer.  The ``ilist`` is augmented by an extra pointer, which serves
844*f4a2713aSLionel Sambucas the back-link of the sentinel.  This is the only field in the ghostly
845*f4a2713aSLionel Sambucsentinel which can be legally accessed.
846*f4a2713aSLionel Sambuc
847*f4a2713aSLionel Sambuc.. _dss_other:
848*f4a2713aSLionel Sambuc
849*f4a2713aSLionel SambucOther Sequential Container options
850*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
851*f4a2713aSLionel Sambuc
852*f4a2713aSLionel SambucOther STL containers are available, such as ``std::string``.
853*f4a2713aSLionel Sambuc
854*f4a2713aSLionel SambucThere are also various STL adapter classes such as ``std::queue``,
855*f4a2713aSLionel Sambuc``std::priority_queue``, ``std::stack``, etc.  These provide simplified access
856*f4a2713aSLionel Sambucto an underlying container but don't affect the cost of the container itself.
857*f4a2713aSLionel Sambuc
858*f4a2713aSLionel Sambuc.. _ds_string:
859*f4a2713aSLionel Sambuc
860*f4a2713aSLionel SambucString-like containers
861*f4a2713aSLionel Sambuc----------------------
862*f4a2713aSLionel Sambuc
863*f4a2713aSLionel SambucThere are a variety of ways to pass around and use strings in C and C++, and
864*f4a2713aSLionel SambucLLVM adds a few new options to choose from.  Pick the first option on this list
865*f4a2713aSLionel Sambucthat will do what you need, they are ordered according to their relative cost.
866*f4a2713aSLionel Sambuc
867*f4a2713aSLionel SambucNote that is is generally preferred to *not* pass strings around as ``const
868*f4a2713aSLionel Sambucchar*``'s.  These have a number of problems, including the fact that they
869*f4a2713aSLionel Sambuccannot represent embedded nul ("\0") characters, and do not have a length
870*f4a2713aSLionel Sambucavailable efficiently.  The general replacement for '``const char*``' is
871*f4a2713aSLionel SambucStringRef.
872*f4a2713aSLionel Sambuc
873*f4a2713aSLionel SambucFor more information on choosing string containers for APIs, please see
874*f4a2713aSLionel Sambuc:ref:`Passing Strings <string_apis>`.
875*f4a2713aSLionel Sambuc
876*f4a2713aSLionel Sambuc.. _dss_stringref:
877*f4a2713aSLionel Sambuc
878*f4a2713aSLionel Sambucllvm/ADT/StringRef.h
879*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^
880*f4a2713aSLionel Sambuc
881*f4a2713aSLionel SambucThe StringRef class is a simple value class that contains a pointer to a
882*f4a2713aSLionel Sambuccharacter and a length, and is quite related to the :ref:`ArrayRef
883*f4a2713aSLionel Sambuc<dss_arrayref>` class (but specialized for arrays of characters).  Because
884*f4a2713aSLionel SambucStringRef carries a length with it, it safely handles strings with embedded nul
885*f4a2713aSLionel Sambuccharacters in it, getting the length does not require a strlen call, and it even
886*f4a2713aSLionel Sambuchas very convenient APIs for slicing and dicing the character range that it
887*f4a2713aSLionel Sambucrepresents.
888*f4a2713aSLionel Sambuc
889*f4a2713aSLionel SambucStringRef is ideal for passing simple strings around that are known to be live,
890*f4a2713aSLionel Sambuceither because they are C string literals, std::string, a C array, or a
891*f4a2713aSLionel SambucSmallVector.  Each of these cases has an efficient implicit conversion to
892*f4a2713aSLionel SambucStringRef, which doesn't result in a dynamic strlen being executed.
893*f4a2713aSLionel Sambuc
894*f4a2713aSLionel SambucStringRef has a few major limitations which make more powerful string containers
895*f4a2713aSLionel Sambucuseful:
896*f4a2713aSLionel Sambuc
897*f4a2713aSLionel Sambuc#. You cannot directly convert a StringRef to a 'const char*' because there is
898*f4a2713aSLionel Sambuc   no way to add a trailing nul (unlike the .c_str() method on various stronger
899*f4a2713aSLionel Sambuc   classes).
900*f4a2713aSLionel Sambuc
901*f4a2713aSLionel Sambuc#. StringRef doesn't own or keep alive the underlying string bytes.
902*f4a2713aSLionel Sambuc   As such it can easily lead to dangling pointers, and is not suitable for
903*f4a2713aSLionel Sambuc   embedding in datastructures in most cases (instead, use an std::string or
904*f4a2713aSLionel Sambuc   something like that).
905*f4a2713aSLionel Sambuc
906*f4a2713aSLionel Sambuc#. For the same reason, StringRef cannot be used as the return value of a
907*f4a2713aSLionel Sambuc   method if the method "computes" the result string.  Instead, use std::string.
908*f4a2713aSLionel Sambuc
909*f4a2713aSLionel Sambuc#. StringRef's do not allow you to mutate the pointed-to string bytes and it
910*f4a2713aSLionel Sambuc   doesn't allow you to insert or remove bytes from the range.  For editing
911*f4a2713aSLionel Sambuc   operations like this, it interoperates with the :ref:`Twine <dss_twine>`
912*f4a2713aSLionel Sambuc   class.
913*f4a2713aSLionel Sambuc
914*f4a2713aSLionel SambucBecause of its strengths and limitations, it is very common for a function to
915*f4a2713aSLionel Sambuctake a StringRef and for a method on an object to return a StringRef that points
916*f4a2713aSLionel Sambucinto some string that it owns.
917*f4a2713aSLionel Sambuc
918*f4a2713aSLionel Sambuc.. _dss_twine:
919*f4a2713aSLionel Sambuc
920*f4a2713aSLionel Sambucllvm/ADT/Twine.h
921*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^
922*f4a2713aSLionel Sambuc
923*f4a2713aSLionel SambucThe Twine class is used as an intermediary datatype for APIs that want to take a
924*f4a2713aSLionel Sambucstring that can be constructed inline with a series of concatenations.  Twine
925*f4a2713aSLionel Sambucworks by forming recursive instances of the Twine datatype (a simple value
926*f4a2713aSLionel Sambucobject) on the stack as temporary objects, linking them together into a tree
927*f4a2713aSLionel Sambucwhich is then linearized when the Twine is consumed.  Twine is only safe to use
928*f4a2713aSLionel Sambucas the argument to a function, and should always be a const reference, e.g.:
929*f4a2713aSLionel Sambuc
930*f4a2713aSLionel Sambuc.. code-block:: c++
931*f4a2713aSLionel Sambuc
932*f4a2713aSLionel Sambuc  void foo(const Twine &T);
933*f4a2713aSLionel Sambuc  ...
934*f4a2713aSLionel Sambuc  StringRef X = ...
935*f4a2713aSLionel Sambuc  unsigned i = ...
936*f4a2713aSLionel Sambuc  foo(X + "." + Twine(i));
937*f4a2713aSLionel Sambuc
938*f4a2713aSLionel SambucThis example forms a string like "blarg.42" by concatenating the values
939*f4a2713aSLionel Sambuctogether, and does not form intermediate strings containing "blarg" or "blarg.".
940*f4a2713aSLionel Sambuc
941*f4a2713aSLionel SambucBecause Twine is constructed with temporary objects on the stack, and because
942*f4a2713aSLionel Sambucthese instances are destroyed at the end of the current statement, it is an
943*f4a2713aSLionel Sambucinherently dangerous API.  For example, this simple variant contains undefined
944*f4a2713aSLionel Sambucbehavior and will probably crash:
945*f4a2713aSLionel Sambuc
946*f4a2713aSLionel Sambuc.. code-block:: c++
947*f4a2713aSLionel Sambuc
948*f4a2713aSLionel Sambuc  void foo(const Twine &T);
949*f4a2713aSLionel Sambuc  ...
950*f4a2713aSLionel Sambuc  StringRef X = ...
951*f4a2713aSLionel Sambuc  unsigned i = ...
952*f4a2713aSLionel Sambuc  const Twine &Tmp = X + "." + Twine(i);
953*f4a2713aSLionel Sambuc  foo(Tmp);
954*f4a2713aSLionel Sambuc
955*f4a2713aSLionel Sambuc... because the temporaries are destroyed before the call.  That said, Twine's
956*f4a2713aSLionel Sambucare much more efficient than intermediate std::string temporaries, and they work
957*f4a2713aSLionel Sambucreally well with StringRef.  Just be aware of their limitations.
958*f4a2713aSLionel Sambuc
959*f4a2713aSLionel Sambuc.. _dss_smallstring:
960*f4a2713aSLionel Sambuc
961*f4a2713aSLionel Sambucllvm/ADT/SmallString.h
962*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^
963*f4a2713aSLionel Sambuc
964*f4a2713aSLionel SambucSmallString is a subclass of :ref:`SmallVector <dss_smallvector>` that adds some
965*f4a2713aSLionel Sambucconvenience APIs like += that takes StringRef's.  SmallString avoids allocating
966*f4a2713aSLionel Sambucmemory in the case when the preallocated space is enough to hold its data, and
967*f4a2713aSLionel Sambucit calls back to general heap allocation when required.  Since it owns its data,
968*f4a2713aSLionel Sambucit is very safe to use and supports full mutation of the string.
969*f4a2713aSLionel Sambuc
970*f4a2713aSLionel SambucLike SmallVector's, the big downside to SmallString is their sizeof.  While they
971*f4a2713aSLionel Sambucare optimized for small strings, they themselves are not particularly small.
972*f4a2713aSLionel SambucThis means that they work great for temporary scratch buffers on the stack, but
973*f4a2713aSLionel Sambucshould not generally be put into the heap: it is very rare to see a SmallString
974*f4a2713aSLionel Sambucas the member of a frequently-allocated heap data structure or returned
975*f4a2713aSLionel Sambucby-value.
976*f4a2713aSLionel Sambuc
977*f4a2713aSLionel Sambuc.. _dss_stdstring:
978*f4a2713aSLionel Sambuc
979*f4a2713aSLionel Sambucstd::string
980*f4a2713aSLionel Sambuc^^^^^^^^^^^
981*f4a2713aSLionel Sambuc
982*f4a2713aSLionel SambucThe standard C++ std::string class is a very general class that (like
983*f4a2713aSLionel SambucSmallString) owns its underlying data.  sizeof(std::string) is very reasonable
984*f4a2713aSLionel Sambucso it can be embedded into heap data structures and returned by-value.  On the
985*f4a2713aSLionel Sambucother hand, std::string is highly inefficient for inline editing (e.g.
986*f4a2713aSLionel Sambucconcatenating a bunch of stuff together) and because it is provided by the
987*f4a2713aSLionel Sambucstandard library, its performance characteristics depend a lot of the host
988*f4a2713aSLionel Sambucstandard library (e.g. libc++ and MSVC provide a highly optimized string class,
989*f4a2713aSLionel SambucGCC contains a really slow implementation).
990*f4a2713aSLionel Sambuc
991*f4a2713aSLionel SambucThe major disadvantage of std::string is that almost every operation that makes
992*f4a2713aSLionel Sambucthem larger can allocate memory, which is slow.  As such, it is better to use
993*f4a2713aSLionel SambucSmallVector or Twine as a scratch buffer, but then use std::string to persist
994*f4a2713aSLionel Sambucthe result.
995*f4a2713aSLionel Sambuc
996*f4a2713aSLionel Sambuc.. _ds_set:
997*f4a2713aSLionel Sambuc
998*f4a2713aSLionel SambucSet-Like Containers (std::set, SmallSet, SetVector, etc)
999*f4a2713aSLionel Sambuc--------------------------------------------------------
1000*f4a2713aSLionel Sambuc
1001*f4a2713aSLionel SambucSet-like containers are useful when you need to canonicalize multiple values
1002*f4a2713aSLionel Sambucinto a single representation.  There are several different choices for how to do
1003*f4a2713aSLionel Sambucthis, providing various trade-offs.
1004*f4a2713aSLionel Sambuc
1005*f4a2713aSLionel Sambuc.. _dss_sortedvectorset:
1006*f4a2713aSLionel Sambuc
1007*f4a2713aSLionel SambucA sorted 'vector'
1008*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^
1009*f4a2713aSLionel Sambuc
1010*f4a2713aSLionel SambucIf you intend to insert a lot of elements, then do a lot of queries, a great
1011*f4a2713aSLionel Sambucapproach is to use a vector (or other sequential container) with
1012*f4a2713aSLionel Sambucstd::sort+std::unique to remove duplicates.  This approach works really well if
1013*f4a2713aSLionel Sambucyour usage pattern has these two distinct phases (insert then query), and can be
1014*f4a2713aSLionel Sambuccoupled with a good choice of :ref:`sequential container <ds_sequential>`.
1015*f4a2713aSLionel Sambuc
1016*f4a2713aSLionel SambucThis combination provides the several nice properties: the result data is
1017*f4a2713aSLionel Sambuccontiguous in memory (good for cache locality), has few allocations, is easy to
1018*f4a2713aSLionel Sambucaddress (iterators in the final vector are just indices or pointers), and can be
1019*f4a2713aSLionel Sambucefficiently queried with a standard binary search (e.g.
1020*f4a2713aSLionel Sambuc``std::lower_bound``; if you want the whole range of elements comparing
1021*f4a2713aSLionel Sambucequal, use ``std::equal_range``).
1022*f4a2713aSLionel Sambuc
1023*f4a2713aSLionel Sambuc.. _dss_smallset:
1024*f4a2713aSLionel Sambuc
1025*f4a2713aSLionel Sambucllvm/ADT/SmallSet.h
1026*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^
1027*f4a2713aSLionel Sambuc
1028*f4a2713aSLionel SambucIf you have a set-like data structure that is usually small and whose elements
1029*f4a2713aSLionel Sambucare reasonably small, a ``SmallSet<Type, N>`` is a good choice.  This set has
1030*f4a2713aSLionel Sambucspace for N elements in place (thus, if the set is dynamically smaller than N,
1031*f4a2713aSLionel Sambucno malloc traffic is required) and accesses them with a simple linear search.
1032*f4a2713aSLionel SambucWhen the set grows beyond 'N' elements, it allocates a more expensive
1033*f4a2713aSLionel Sambucrepresentation that guarantees efficient access (for most types, it falls back
1034*f4a2713aSLionel Sambucto std::set, but for pointers it uses something far better, :ref:`SmallPtrSet
1035*f4a2713aSLionel Sambuc<dss_smallptrset>`.
1036*f4a2713aSLionel Sambuc
1037*f4a2713aSLionel SambucThe magic of this class is that it handles small sets extremely efficiently, but
1038*f4a2713aSLionel Sambucgracefully handles extremely large sets without loss of efficiency.  The
1039*f4a2713aSLionel Sambucdrawback is that the interface is quite small: it supports insertion, queries
1040*f4a2713aSLionel Sambucand erasing, but does not support iteration.
1041*f4a2713aSLionel Sambuc
1042*f4a2713aSLionel Sambuc.. _dss_smallptrset:
1043*f4a2713aSLionel Sambuc
1044*f4a2713aSLionel Sambucllvm/ADT/SmallPtrSet.h
1045*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^
1046*f4a2713aSLionel Sambuc
1047*f4a2713aSLionel SambucSmallPtrSet has all the advantages of ``SmallSet`` (and a ``SmallSet`` of
1048*f4a2713aSLionel Sambucpointers is transparently implemented with a ``SmallPtrSet``), but also supports
1049*f4a2713aSLionel Sambuciterators.  If more than 'N' insertions are performed, a single quadratically
1050*f4a2713aSLionel Sambucprobed hash table is allocated and grows as needed, providing extremely
1051*f4a2713aSLionel Sambucefficient access (constant time insertion/deleting/queries with low constant
1052*f4a2713aSLionel Sambucfactors) and is very stingy with malloc traffic.
1053*f4a2713aSLionel Sambuc
1054*f4a2713aSLionel SambucNote that, unlike ``std::set``, the iterators of ``SmallPtrSet`` are invalidated
1055*f4a2713aSLionel Sambucwhenever an insertion occurs.  Also, the values visited by the iterators are not
1056*f4a2713aSLionel Sambucvisited in sorted order.
1057*f4a2713aSLionel Sambuc
1058*f4a2713aSLionel Sambuc.. _dss_denseset:
1059*f4a2713aSLionel Sambuc
1060*f4a2713aSLionel Sambucllvm/ADT/DenseSet.h
1061*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^
1062*f4a2713aSLionel Sambuc
1063*f4a2713aSLionel SambucDenseSet is a simple quadratically probed hash table.  It excels at supporting
1064*f4a2713aSLionel Sambucsmall values: it uses a single allocation to hold all of the pairs that are
1065*f4a2713aSLionel Sambuccurrently inserted in the set.  DenseSet is a great way to unique small values
1066*f4a2713aSLionel Sambucthat are not simple pointers (use :ref:`SmallPtrSet <dss_smallptrset>` for
1067*f4a2713aSLionel Sambucpointers).  Note that DenseSet has the same requirements for the value type that
1068*f4a2713aSLionel Sambuc:ref:`DenseMap <dss_densemap>` has.
1069*f4a2713aSLionel Sambuc
1070*f4a2713aSLionel Sambuc.. _dss_sparseset:
1071*f4a2713aSLionel Sambuc
1072*f4a2713aSLionel Sambucllvm/ADT/SparseSet.h
1073*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^
1074*f4a2713aSLionel Sambuc
1075*f4a2713aSLionel SambucSparseSet holds a small number of objects identified by unsigned keys of
1076*f4a2713aSLionel Sambucmoderate size.  It uses a lot of memory, but provides operations that are almost
1077*f4a2713aSLionel Sambucas fast as a vector.  Typical keys are physical registers, virtual registers, or
1078*f4a2713aSLionel Sambucnumbered basic blocks.
1079*f4a2713aSLionel Sambuc
1080*f4a2713aSLionel SambucSparseSet is useful for algorithms that need very fast clear/find/insert/erase
1081*f4a2713aSLionel Sambucand fast iteration over small sets.  It is not intended for building composite
1082*f4a2713aSLionel Sambucdata structures.
1083*f4a2713aSLionel Sambuc
1084*f4a2713aSLionel Sambuc.. _dss_sparsemultiset:
1085*f4a2713aSLionel Sambuc
1086*f4a2713aSLionel Sambucllvm/ADT/SparseMultiSet.h
1087*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1088*f4a2713aSLionel Sambuc
1089*f4a2713aSLionel SambucSparseMultiSet adds multiset behavior to SparseSet, while retaining SparseSet's
1090*f4a2713aSLionel Sambucdesirable attributes. Like SparseSet, it typically uses a lot of memory, but
1091*f4a2713aSLionel Sambucprovides operations that are almost as fast as a vector.  Typical keys are
1092*f4a2713aSLionel Sambucphysical registers, virtual registers, or numbered basic blocks.
1093*f4a2713aSLionel Sambuc
1094*f4a2713aSLionel SambucSparseMultiSet is useful for algorithms that need very fast
1095*f4a2713aSLionel Sambucclear/find/insert/erase of the entire collection, and iteration over sets of
1096*f4a2713aSLionel Sambucelements sharing a key. It is often a more efficient choice than using composite
1097*f4a2713aSLionel Sambucdata structures (e.g. vector-of-vectors, map-of-vectors). It is not intended for
1098*f4a2713aSLionel Sambucbuilding composite data structures.
1099*f4a2713aSLionel Sambuc
1100*f4a2713aSLionel Sambuc.. _dss_FoldingSet:
1101*f4a2713aSLionel Sambuc
1102*f4a2713aSLionel Sambucllvm/ADT/FoldingSet.h
1103*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
1104*f4a2713aSLionel Sambuc
1105*f4a2713aSLionel SambucFoldingSet is an aggregate class that is really good at uniquing
1106*f4a2713aSLionel Sambucexpensive-to-create or polymorphic objects.  It is a combination of a chained
1107*f4a2713aSLionel Sambuchash table with intrusive links (uniqued objects are required to inherit from
1108*f4a2713aSLionel SambucFoldingSetNode) that uses :ref:`SmallVector <dss_smallvector>` as part of its ID
1109*f4a2713aSLionel Sambucprocess.
1110*f4a2713aSLionel Sambuc
1111*f4a2713aSLionel SambucConsider a case where you want to implement a "getOrCreateFoo" method for a
1112*f4a2713aSLionel Sambuccomplex object (for example, a node in the code generator).  The client has a
1113*f4a2713aSLionel Sambucdescription of **what** it wants to generate (it knows the opcode and all the
1114*f4a2713aSLionel Sambucoperands), but we don't want to 'new' a node, then try inserting it into a set
1115*f4a2713aSLionel Sambuconly to find out it already exists, at which point we would have to delete it
1116*f4a2713aSLionel Sambucand return the node that already exists.
1117*f4a2713aSLionel Sambuc
1118*f4a2713aSLionel SambucTo support this style of client, FoldingSet perform a query with a
1119*f4a2713aSLionel SambucFoldingSetNodeID (which wraps SmallVector) that can be used to describe the
1120*f4a2713aSLionel Sambucelement that we want to query for.  The query either returns the element
1121*f4a2713aSLionel Sambucmatching the ID or it returns an opaque ID that indicates where insertion should
1122*f4a2713aSLionel Sambuctake place.  Construction of the ID usually does not require heap traffic.
1123*f4a2713aSLionel Sambuc
1124*f4a2713aSLionel SambucBecause FoldingSet uses intrusive links, it can support polymorphic objects in
1125*f4a2713aSLionel Sambucthe set (for example, you can have SDNode instances mixed with LoadSDNodes).
1126*f4a2713aSLionel SambucBecause the elements are individually allocated, pointers to the elements are
1127*f4a2713aSLionel Sambucstable: inserting or removing elements does not invalidate any pointers to other
1128*f4a2713aSLionel Sambucelements.
1129*f4a2713aSLionel Sambuc
1130*f4a2713aSLionel Sambuc.. _dss_set:
1131*f4a2713aSLionel Sambuc
1132*f4a2713aSLionel Sambuc<set>
1133*f4a2713aSLionel Sambuc^^^^^
1134*f4a2713aSLionel Sambuc
1135*f4a2713aSLionel Sambuc``std::set`` is a reasonable all-around set class, which is decent at many
1136*f4a2713aSLionel Sambucthings but great at nothing.  std::set allocates memory for each element
1137*f4a2713aSLionel Sambucinserted (thus it is very malloc intensive) and typically stores three pointers
1138*f4a2713aSLionel Sambucper element in the set (thus adding a large amount of per-element space
1139*f4a2713aSLionel Sambucoverhead).  It offers guaranteed log(n) performance, which is not particularly
1140*f4a2713aSLionel Sambucfast from a complexity standpoint (particularly if the elements of the set are
1141*f4a2713aSLionel Sambucexpensive to compare, like strings), and has extremely high constant factors for
1142*f4a2713aSLionel Sambuclookup, insertion and removal.
1143*f4a2713aSLionel Sambuc
1144*f4a2713aSLionel SambucThe advantages of std::set are that its iterators are stable (deleting or
1145*f4a2713aSLionel Sambucinserting an element from the set does not affect iterators or pointers to other
1146*f4a2713aSLionel Sambucelements) and that iteration over the set is guaranteed to be in sorted order.
1147*f4a2713aSLionel SambucIf the elements in the set are large, then the relative overhead of the pointers
1148*f4a2713aSLionel Sambucand malloc traffic is not a big deal, but if the elements of the set are small,
1149*f4a2713aSLionel Sambucstd::set is almost never a good choice.
1150*f4a2713aSLionel Sambuc
1151*f4a2713aSLionel Sambuc.. _dss_setvector:
1152*f4a2713aSLionel Sambuc
1153*f4a2713aSLionel Sambucllvm/ADT/SetVector.h
1154*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^
1155*f4a2713aSLionel Sambuc
1156*f4a2713aSLionel SambucLLVM's ``SetVector<Type>`` is an adapter class that combines your choice of a
1157*f4a2713aSLionel Sambucset-like container along with a :ref:`Sequential Container <ds_sequential>` The
1158*f4a2713aSLionel Sambucimportant property that this provides is efficient insertion with uniquing
1159*f4a2713aSLionel Sambuc(duplicate elements are ignored) with iteration support.  It implements this by
1160*f4a2713aSLionel Sambucinserting elements into both a set-like container and the sequential container,
1161*f4a2713aSLionel Sambucusing the set-like container for uniquing and the sequential container for
1162*f4a2713aSLionel Sambuciteration.
1163*f4a2713aSLionel Sambuc
1164*f4a2713aSLionel SambucThe difference between SetVector and other sets is that the order of iteration
1165*f4a2713aSLionel Sambucis guaranteed to match the order of insertion into the SetVector.  This property
1166*f4a2713aSLionel Sambucis really important for things like sets of pointers.  Because pointer values
1167*f4a2713aSLionel Sambucare non-deterministic (e.g. vary across runs of the program on different
1168*f4a2713aSLionel Sambucmachines), iterating over the pointers in the set will not be in a well-defined
1169*f4a2713aSLionel Sambucorder.
1170*f4a2713aSLionel Sambuc
1171*f4a2713aSLionel SambucThe drawback of SetVector is that it requires twice as much space as a normal
1172*f4a2713aSLionel Sambucset and has the sum of constant factors from the set-like container and the
1173*f4a2713aSLionel Sambucsequential container that it uses.  Use it **only** if you need to iterate over
1174*f4a2713aSLionel Sambucthe elements in a deterministic order.  SetVector is also expensive to delete
1175*f4a2713aSLionel Sambucelements out of (linear time), unless you use its "pop_back" method, which is
1176*f4a2713aSLionel Sambucfaster.
1177*f4a2713aSLionel Sambuc
1178*f4a2713aSLionel Sambuc``SetVector`` is an adapter class that defaults to using ``std::vector`` and a
1179*f4a2713aSLionel Sambucsize 16 ``SmallSet`` for the underlying containers, so it is quite expensive.
1180*f4a2713aSLionel SambucHowever, ``"llvm/ADT/SetVector.h"`` also provides a ``SmallSetVector`` class,
1181*f4a2713aSLionel Sambucwhich defaults to using a ``SmallVector`` and ``SmallSet`` of a specified size.
1182*f4a2713aSLionel SambucIf you use this, and if your sets are dynamically smaller than ``N``, you will
1183*f4a2713aSLionel Sambucsave a lot of heap traffic.
1184*f4a2713aSLionel Sambuc
1185*f4a2713aSLionel Sambuc.. _dss_uniquevector:
1186*f4a2713aSLionel Sambuc
1187*f4a2713aSLionel Sambucllvm/ADT/UniqueVector.h
1188*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^
1189*f4a2713aSLionel Sambuc
1190*f4a2713aSLionel SambucUniqueVector is similar to :ref:`SetVector <dss_setvector>` but it retains a
1191*f4a2713aSLionel Sambucunique ID for each element inserted into the set.  It internally contains a map
1192*f4a2713aSLionel Sambucand a vector, and it assigns a unique ID for each value inserted into the set.
1193*f4a2713aSLionel Sambuc
1194*f4a2713aSLionel SambucUniqueVector is very expensive: its cost is the sum of the cost of maintaining
1195*f4a2713aSLionel Sambucboth the map and vector, it has high complexity, high constant factors, and
1196*f4a2713aSLionel Sambucproduces a lot of malloc traffic.  It should be avoided.
1197*f4a2713aSLionel Sambuc
1198*f4a2713aSLionel Sambuc.. _dss_immutableset:
1199*f4a2713aSLionel Sambuc
1200*f4a2713aSLionel Sambucllvm/ADT/ImmutableSet.h
1201*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^
1202*f4a2713aSLionel Sambuc
1203*f4a2713aSLionel SambucImmutableSet is an immutable (functional) set implementation based on an AVL
1204*f4a2713aSLionel Sambuctree.  Adding or removing elements is done through a Factory object and results
1205*f4a2713aSLionel Sambucin the creation of a new ImmutableSet object.  If an ImmutableSet already exists
1206*f4a2713aSLionel Sambucwith the given contents, then the existing one is returned; equality is compared
1207*f4a2713aSLionel Sambucwith a FoldingSetNodeID.  The time and space complexity of add or remove
1208*f4a2713aSLionel Sambucoperations is logarithmic in the size of the original set.
1209*f4a2713aSLionel Sambuc
1210*f4a2713aSLionel SambucThere is no method for returning an element of the set, you can only check for
1211*f4a2713aSLionel Sambucmembership.
1212*f4a2713aSLionel Sambuc
1213*f4a2713aSLionel Sambuc.. _dss_otherset:
1214*f4a2713aSLionel Sambuc
1215*f4a2713aSLionel SambucOther Set-Like Container Options
1216*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1217*f4a2713aSLionel Sambuc
1218*f4a2713aSLionel SambucThe STL provides several other options, such as std::multiset and the various
1219*f4a2713aSLionel Sambuc"hash_set" like containers (whether from C++ TR1 or from the SGI library).  We
1220*f4a2713aSLionel Sambucnever use hash_set and unordered_set because they are generally very expensive
1221*f4a2713aSLionel Sambuc(each insertion requires a malloc) and very non-portable.
1222*f4a2713aSLionel Sambuc
1223*f4a2713aSLionel Sambucstd::multiset is useful if you're not interested in elimination of duplicates,
1224*f4a2713aSLionel Sambucbut has all the drawbacks of std::set.  A sorted vector (where you don't delete
1225*f4a2713aSLionel Sambucduplicate entries) or some other approach is almost always better.
1226*f4a2713aSLionel Sambuc
1227*f4a2713aSLionel Sambuc.. _ds_map:
1228*f4a2713aSLionel Sambuc
1229*f4a2713aSLionel SambucMap-Like Containers (std::map, DenseMap, etc)
1230*f4a2713aSLionel Sambuc---------------------------------------------
1231*f4a2713aSLionel Sambuc
1232*f4a2713aSLionel SambucMap-like containers are useful when you want to associate data to a key.  As
1233*f4a2713aSLionel Sambucusual, there are a lot of different ways to do this. :)
1234*f4a2713aSLionel Sambuc
1235*f4a2713aSLionel Sambuc.. _dss_sortedvectormap:
1236*f4a2713aSLionel Sambuc
1237*f4a2713aSLionel SambucA sorted 'vector'
1238*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^
1239*f4a2713aSLionel Sambuc
1240*f4a2713aSLionel SambucIf your usage pattern follows a strict insert-then-query approach, you can
1241*f4a2713aSLionel Sambuctrivially use the same approach as :ref:`sorted vectors for set-like containers
1242*f4a2713aSLionel Sambuc<dss_sortedvectorset>`.  The only difference is that your query function (which
1243*f4a2713aSLionel Sambucuses std::lower_bound to get efficient log(n) lookup) should only compare the
1244*f4a2713aSLionel Sambuckey, not both the key and value.  This yields the same advantages as sorted
1245*f4a2713aSLionel Sambucvectors for sets.
1246*f4a2713aSLionel Sambuc
1247*f4a2713aSLionel Sambuc.. _dss_stringmap:
1248*f4a2713aSLionel Sambuc
1249*f4a2713aSLionel Sambucllvm/ADT/StringMap.h
1250*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^
1251*f4a2713aSLionel Sambuc
1252*f4a2713aSLionel SambucStrings are commonly used as keys in maps, and they are difficult to support
1253*f4a2713aSLionel Sambucefficiently: they are variable length, inefficient to hash and compare when
1254*f4a2713aSLionel Sambuclong, expensive to copy, etc.  StringMap is a specialized container designed to
1255*f4a2713aSLionel Sambuccope with these issues.  It supports mapping an arbitrary range of bytes to an
1256*f4a2713aSLionel Sambucarbitrary other object.
1257*f4a2713aSLionel Sambuc
1258*f4a2713aSLionel SambucThe StringMap implementation uses a quadratically-probed hash table, where the
1259*f4a2713aSLionel Sambucbuckets store a pointer to the heap allocated entries (and some other stuff).
1260*f4a2713aSLionel SambucThe entries in the map must be heap allocated because the strings are variable
1261*f4a2713aSLionel Sambuclength.  The string data (key) and the element object (value) are stored in the
1262*f4a2713aSLionel Sambucsame allocation with the string data immediately after the element object.
1263*f4a2713aSLionel SambucThis container guarantees the "``(char*)(&Value+1)``" points to the key string
1264*f4a2713aSLionel Sambucfor a value.
1265*f4a2713aSLionel Sambuc
1266*f4a2713aSLionel SambucThe StringMap is very fast for several reasons: quadratic probing is very cache
1267*f4a2713aSLionel Sambucefficient for lookups, the hash value of strings in buckets is not recomputed
1268*f4a2713aSLionel Sambucwhen looking up an element, StringMap rarely has to touch the memory for
1269*f4a2713aSLionel Sambucunrelated objects when looking up a value (even when hash collisions happen),
1270*f4a2713aSLionel Sambuchash table growth does not recompute the hash values for strings already in the
1271*f4a2713aSLionel Sambuctable, and each pair in the map is store in a single allocation (the string data
1272*f4a2713aSLionel Sambucis stored in the same allocation as the Value of a pair).
1273*f4a2713aSLionel Sambuc
1274*f4a2713aSLionel SambucStringMap also provides query methods that take byte ranges, so it only ever
1275*f4a2713aSLionel Sambuccopies a string if a value is inserted into the table.
1276*f4a2713aSLionel Sambuc
1277*f4a2713aSLionel SambucStringMap iteratation order, however, is not guaranteed to be deterministic, so
1278*f4a2713aSLionel Sambucany uses which require that should instead use a std::map.
1279*f4a2713aSLionel Sambuc
1280*f4a2713aSLionel Sambuc.. _dss_indexmap:
1281*f4a2713aSLionel Sambuc
1282*f4a2713aSLionel Sambucllvm/ADT/IndexedMap.h
1283*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
1284*f4a2713aSLionel Sambuc
1285*f4a2713aSLionel SambucIndexedMap is a specialized container for mapping small dense integers (or
1286*f4a2713aSLionel Sambucvalues that can be mapped to small dense integers) to some other type.  It is
1287*f4a2713aSLionel Sambucinternally implemented as a vector with a mapping function that maps the keys
1288*f4a2713aSLionel Sambucto the dense integer range.
1289*f4a2713aSLionel Sambuc
1290*f4a2713aSLionel SambucThis is useful for cases like virtual registers in the LLVM code generator: they
1291*f4a2713aSLionel Sambuchave a dense mapping that is offset by a compile-time constant (the first
1292*f4a2713aSLionel Sambucvirtual register ID).
1293*f4a2713aSLionel Sambuc
1294*f4a2713aSLionel Sambuc.. _dss_densemap:
1295*f4a2713aSLionel Sambuc
1296*f4a2713aSLionel Sambucllvm/ADT/DenseMap.h
1297*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^
1298*f4a2713aSLionel Sambuc
1299*f4a2713aSLionel SambucDenseMap is a simple quadratically probed hash table.  It excels at supporting
1300*f4a2713aSLionel Sambucsmall keys and values: it uses a single allocation to hold all of the pairs
1301*f4a2713aSLionel Sambucthat are currently inserted in the map.  DenseMap is a great way to map
1302*f4a2713aSLionel Sambucpointers to pointers, or map other small types to each other.
1303*f4a2713aSLionel Sambuc
1304*f4a2713aSLionel SambucThere are several aspects of DenseMap that you should be aware of, however.
1305*f4a2713aSLionel SambucThe iterators in a DenseMap are invalidated whenever an insertion occurs,
1306*f4a2713aSLionel Sambucunlike map.  Also, because DenseMap allocates space for a large number of
1307*f4a2713aSLionel Sambuckey/value pairs (it starts with 64 by default), it will waste a lot of space if
1308*f4a2713aSLionel Sambucyour keys or values are large.  Finally, you must implement a partial
1309*f4a2713aSLionel Sambucspecialization of DenseMapInfo for the key that you want, if it isn't already
1310*f4a2713aSLionel Sambucsupported.  This is required to tell DenseMap about two special marker values
1311*f4a2713aSLionel Sambuc(which can never be inserted into the map) that it needs internally.
1312*f4a2713aSLionel Sambuc
1313*f4a2713aSLionel SambucDenseMap's find_as() method supports lookup operations using an alternate key
1314*f4a2713aSLionel Sambuctype.  This is useful in cases where the normal key type is expensive to
1315*f4a2713aSLionel Sambucconstruct, but cheap to compare against.  The DenseMapInfo is responsible for
1316*f4a2713aSLionel Sambucdefining the appropriate comparison and hashing methods for each alternate key
1317*f4a2713aSLionel Sambuctype used.
1318*f4a2713aSLionel Sambuc
1319*f4a2713aSLionel Sambuc.. _dss_valuemap:
1320*f4a2713aSLionel Sambuc
1321*f4a2713aSLionel Sambucllvm/ADT/ValueMap.h
1322*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^
1323*f4a2713aSLionel Sambuc
1324*f4a2713aSLionel SambucValueMap is a wrapper around a :ref:`DenseMap <dss_densemap>` mapping
1325*f4a2713aSLionel Sambuc``Value*``\ s (or subclasses) to another type.  When a Value is deleted or
1326*f4a2713aSLionel SambucRAUW'ed, ValueMap will update itself so the new version of the key is mapped to
1327*f4a2713aSLionel Sambucthe same value, just as if the key were a WeakVH.  You can configure exactly how
1328*f4a2713aSLionel Sambucthis happens, and what else happens on these two events, by passing a ``Config``
1329*f4a2713aSLionel Sambucparameter to the ValueMap template.
1330*f4a2713aSLionel Sambuc
1331*f4a2713aSLionel Sambuc.. _dss_intervalmap:
1332*f4a2713aSLionel Sambuc
1333*f4a2713aSLionel Sambucllvm/ADT/IntervalMap.h
1334*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^
1335*f4a2713aSLionel Sambuc
1336*f4a2713aSLionel SambucIntervalMap is a compact map for small keys and values.  It maps key intervals
1337*f4a2713aSLionel Sambucinstead of single keys, and it will automatically coalesce adjacent intervals.
1338*f4a2713aSLionel SambucWhen then map only contains a few intervals, they are stored in the map object
1339*f4a2713aSLionel Sambucitself to avoid allocations.
1340*f4a2713aSLionel Sambuc
1341*f4a2713aSLionel SambucThe IntervalMap iterators are quite big, so they should not be passed around as
1342*f4a2713aSLionel SambucSTL iterators.  The heavyweight iterators allow a smaller data structure.
1343*f4a2713aSLionel Sambuc
1344*f4a2713aSLionel Sambuc.. _dss_map:
1345*f4a2713aSLionel Sambuc
1346*f4a2713aSLionel Sambuc<map>
1347*f4a2713aSLionel Sambuc^^^^^
1348*f4a2713aSLionel Sambuc
1349*f4a2713aSLionel Sambucstd::map has similar characteristics to :ref:`std::set <dss_set>`: it uses a
1350*f4a2713aSLionel Sambucsingle allocation per pair inserted into the map, it offers log(n) lookup with
1351*f4a2713aSLionel Sambucan extremely large constant factor, imposes a space penalty of 3 pointers per
1352*f4a2713aSLionel Sambucpair in the map, etc.
1353*f4a2713aSLionel Sambuc
1354*f4a2713aSLionel Sambucstd::map is most useful when your keys or values are very large, if you need to
1355*f4a2713aSLionel Sambuciterate over the collection in sorted order, or if you need stable iterators
1356*f4a2713aSLionel Sambucinto the map (i.e. they don't get invalidated if an insertion or deletion of
1357*f4a2713aSLionel Sambucanother element takes place).
1358*f4a2713aSLionel Sambuc
1359*f4a2713aSLionel Sambuc.. _dss_mapvector:
1360*f4a2713aSLionel Sambuc
1361*f4a2713aSLionel Sambucllvm/ADT/MapVector.h
1362*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^
1363*f4a2713aSLionel Sambuc
1364*f4a2713aSLionel Sambuc``MapVector<KeyT,ValueT>`` provides a subset of the DenseMap interface.  The
1365*f4a2713aSLionel Sambucmain difference is that the iteration order is guaranteed to be the insertion
1366*f4a2713aSLionel Sambucorder, making it an easy (but somewhat expensive) solution for non-deterministic
1367*f4a2713aSLionel Sambuciteration over maps of pointers.
1368*f4a2713aSLionel Sambuc
1369*f4a2713aSLionel SambucIt is implemented by mapping from key to an index in a vector of key,value
1370*f4a2713aSLionel Sambucpairs.  This provides fast lookup and iteration, but has two main drawbacks: The
1371*f4a2713aSLionel Sambuckey is stored twice and it doesn't support removing elements.
1372*f4a2713aSLionel Sambuc
1373*f4a2713aSLionel Sambuc.. _dss_inteqclasses:
1374*f4a2713aSLionel Sambuc
1375*f4a2713aSLionel Sambucllvm/ADT/IntEqClasses.h
1376*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^
1377*f4a2713aSLionel Sambuc
1378*f4a2713aSLionel SambucIntEqClasses provides a compact representation of equivalence classes of small
1379*f4a2713aSLionel Sambucintegers.  Initially, each integer in the range 0..n-1 has its own equivalence
1380*f4a2713aSLionel Sambucclass.  Classes can be joined by passing two class representatives to the
1381*f4a2713aSLionel Sambucjoin(a, b) method.  Two integers are in the same class when findLeader() returns
1382*f4a2713aSLionel Sambucthe same representative.
1383*f4a2713aSLionel Sambuc
1384*f4a2713aSLionel SambucOnce all equivalence classes are formed, the map can be compressed so each
1385*f4a2713aSLionel Sambucinteger 0..n-1 maps to an equivalence class number in the range 0..m-1, where m
1386*f4a2713aSLionel Sambucis the total number of equivalence classes.  The map must be uncompressed before
1387*f4a2713aSLionel Sambucit can be edited again.
1388*f4a2713aSLionel Sambuc
1389*f4a2713aSLionel Sambuc.. _dss_immutablemap:
1390*f4a2713aSLionel Sambuc
1391*f4a2713aSLionel Sambucllvm/ADT/ImmutableMap.h
1392*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^
1393*f4a2713aSLionel Sambuc
1394*f4a2713aSLionel SambucImmutableMap is an immutable (functional) map implementation based on an AVL
1395*f4a2713aSLionel Sambuctree.  Adding or removing elements is done through a Factory object and results
1396*f4a2713aSLionel Sambucin the creation of a new ImmutableMap object.  If an ImmutableMap already exists
1397*f4a2713aSLionel Sambucwith the given key set, then the existing one is returned; equality is compared
1398*f4a2713aSLionel Sambucwith a FoldingSetNodeID.  The time and space complexity of add or remove
1399*f4a2713aSLionel Sambucoperations is logarithmic in the size of the original map.
1400*f4a2713aSLionel Sambuc
1401*f4a2713aSLionel Sambuc.. _dss_othermap:
1402*f4a2713aSLionel Sambuc
1403*f4a2713aSLionel SambucOther Map-Like Container Options
1404*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1405*f4a2713aSLionel Sambuc
1406*f4a2713aSLionel SambucThe STL provides several other options, such as std::multimap and the various
1407*f4a2713aSLionel Sambuc"hash_map" like containers (whether from C++ TR1 or from the SGI library).  We
1408*f4a2713aSLionel Sambucnever use hash_set and unordered_set because they are generally very expensive
1409*f4a2713aSLionel Sambuc(each insertion requires a malloc) and very non-portable.
1410*f4a2713aSLionel Sambuc
1411*f4a2713aSLionel Sambucstd::multimap is useful if you want to map a key to multiple values, but has all
1412*f4a2713aSLionel Sambucthe drawbacks of std::map.  A sorted vector or some other approach is almost
1413*f4a2713aSLionel Sambucalways better.
1414*f4a2713aSLionel Sambuc
1415*f4a2713aSLionel Sambuc.. _ds_bit:
1416*f4a2713aSLionel Sambuc
1417*f4a2713aSLionel SambucBit storage containers (BitVector, SparseBitVector)
1418*f4a2713aSLionel Sambuc---------------------------------------------------
1419*f4a2713aSLionel Sambuc
1420*f4a2713aSLionel SambucUnlike the other containers, there are only two bit storage containers, and
1421*f4a2713aSLionel Sambucchoosing when to use each is relatively straightforward.
1422*f4a2713aSLionel Sambuc
1423*f4a2713aSLionel SambucOne additional option is ``std::vector<bool>``: we discourage its use for two
1424*f4a2713aSLionel Sambucreasons 1) the implementation in many common compilers (e.g.  commonly
1425*f4a2713aSLionel Sambucavailable versions of GCC) is extremely inefficient and 2) the C++ standards
1426*f4a2713aSLionel Sambuccommittee is likely to deprecate this container and/or change it significantly
1427*f4a2713aSLionel Sambucsomehow.  In any case, please don't use it.
1428*f4a2713aSLionel Sambuc
1429*f4a2713aSLionel Sambuc.. _dss_bitvector:
1430*f4a2713aSLionel Sambuc
1431*f4a2713aSLionel SambucBitVector
1432*f4a2713aSLionel Sambuc^^^^^^^^^
1433*f4a2713aSLionel Sambuc
1434*f4a2713aSLionel SambucThe BitVector container provides a dynamic size set of bits for manipulation.
1435*f4a2713aSLionel SambucIt supports individual bit setting/testing, as well as set operations.  The set
1436*f4a2713aSLionel Sambucoperations take time O(size of bitvector), but operations are performed one word
1437*f4a2713aSLionel Sambucat a time, instead of one bit at a time.  This makes the BitVector very fast for
1438*f4a2713aSLionel Sambucset operations compared to other containers.  Use the BitVector when you expect
1439*f4a2713aSLionel Sambucthe number of set bits to be high (i.e. a dense set).
1440*f4a2713aSLionel Sambuc
1441*f4a2713aSLionel Sambuc.. _dss_smallbitvector:
1442*f4a2713aSLionel Sambuc
1443*f4a2713aSLionel SambucSmallBitVector
1444*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^
1445*f4a2713aSLionel Sambuc
1446*f4a2713aSLionel SambucThe SmallBitVector container provides the same interface as BitVector, but it is
1447*f4a2713aSLionel Sambucoptimized for the case where only a small number of bits, less than 25 or so,
1448*f4a2713aSLionel Sambucare needed.  It also transparently supports larger bit counts, but slightly less
1449*f4a2713aSLionel Sambucefficiently than a plain BitVector, so SmallBitVector should only be used when
1450*f4a2713aSLionel Sambuclarger counts are rare.
1451*f4a2713aSLionel Sambuc
1452*f4a2713aSLionel SambucAt this time, SmallBitVector does not support set operations (and, or, xor), and
1453*f4a2713aSLionel Sambucits operator[] does not provide an assignable lvalue.
1454*f4a2713aSLionel Sambuc
1455*f4a2713aSLionel Sambuc.. _dss_sparsebitvector:
1456*f4a2713aSLionel Sambuc
1457*f4a2713aSLionel SambucSparseBitVector
1458*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^
1459*f4a2713aSLionel Sambuc
1460*f4a2713aSLionel SambucThe SparseBitVector container is much like BitVector, with one major difference:
1461*f4a2713aSLionel SambucOnly the bits that are set, are stored.  This makes the SparseBitVector much
1462*f4a2713aSLionel Sambucmore space efficient than BitVector when the set is sparse, as well as making
1463*f4a2713aSLionel Sambucset operations O(number of set bits) instead of O(size of universe).  The
1464*f4a2713aSLionel Sambucdownside to the SparseBitVector is that setting and testing of random bits is
1465*f4a2713aSLionel SambucO(N), and on large SparseBitVectors, this can be slower than BitVector.  In our
1466*f4a2713aSLionel Sambucimplementation, setting or testing bits in sorted order (either forwards or
1467*f4a2713aSLionel Sambucreverse) is O(1) worst case.  Testing and setting bits within 128 bits (depends
1468*f4a2713aSLionel Sambucon size) of the current bit is also O(1).  As a general statement,
1469*f4a2713aSLionel Sambuctesting/setting bits in a SparseBitVector is O(distance away from last set bit).
1470*f4a2713aSLionel Sambuc
1471*f4a2713aSLionel Sambuc.. _common:
1472*f4a2713aSLionel Sambuc
1473*f4a2713aSLionel SambucHelpful Hints for Common Operations
1474*f4a2713aSLionel Sambuc===================================
1475*f4a2713aSLionel Sambuc
1476*f4a2713aSLionel SambucThis section describes how to perform some very simple transformations of LLVM
1477*f4a2713aSLionel Sambuccode.  This is meant to give examples of common idioms used, showing the
1478*f4a2713aSLionel Sambucpractical side of LLVM transformations.
1479*f4a2713aSLionel Sambuc
1480*f4a2713aSLionel SambucBecause this is a "how-to" section, you should also read about the main classes
1481*f4a2713aSLionel Sambucthat you will be working with.  The :ref:`Core LLVM Class Hierarchy Reference
1482*f4a2713aSLionel Sambuc<coreclasses>` contains details and descriptions of the main classes that you
1483*f4a2713aSLionel Sambucshould know about.
1484*f4a2713aSLionel Sambuc
1485*f4a2713aSLionel Sambuc.. _inspection:
1486*f4a2713aSLionel Sambuc
1487*f4a2713aSLionel SambucBasic Inspection and Traversal Routines
1488*f4a2713aSLionel Sambuc---------------------------------------
1489*f4a2713aSLionel Sambuc
1490*f4a2713aSLionel SambucThe LLVM compiler infrastructure have many different data structures that may be
1491*f4a2713aSLionel Sambuctraversed.  Following the example of the C++ standard template library, the
1492*f4a2713aSLionel Sambuctechniques used to traverse these various data structures are all basically the
1493*f4a2713aSLionel Sambucsame.  For a enumerable sequence of values, the ``XXXbegin()`` function (or
1494*f4a2713aSLionel Sambucmethod) returns an iterator to the start of the sequence, the ``XXXend()``
1495*f4a2713aSLionel Sambucfunction returns an iterator pointing to one past the last valid element of the
1496*f4a2713aSLionel Sambucsequence, and there is some ``XXXiterator`` data type that is common between the
1497*f4a2713aSLionel Sambuctwo operations.
1498*f4a2713aSLionel Sambuc
1499*f4a2713aSLionel SambucBecause the pattern for iteration is common across many different aspects of the
1500*f4a2713aSLionel Sambucprogram representation, the standard template library algorithms may be used on
1501*f4a2713aSLionel Sambucthem, and it is easier to remember how to iterate.  First we show a few common
1502*f4a2713aSLionel Sambucexamples of the data structures that need to be traversed.  Other data
1503*f4a2713aSLionel Sambucstructures are traversed in very similar ways.
1504*f4a2713aSLionel Sambuc
1505*f4a2713aSLionel Sambuc.. _iterate_function:
1506*f4a2713aSLionel Sambuc
1507*f4a2713aSLionel SambucIterating over the ``BasicBlock`` in a ``Function``
1508*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1509*f4a2713aSLionel Sambuc
1510*f4a2713aSLionel SambucIt's quite common to have a ``Function`` instance that you'd like to transform
1511*f4a2713aSLionel Sambucin some way; in particular, you'd like to manipulate its ``BasicBlock``\ s.  To
1512*f4a2713aSLionel Sambucfacilitate this, you'll need to iterate over all of the ``BasicBlock``\ s that
1513*f4a2713aSLionel Sambucconstitute the ``Function``.  The following is an example that prints the name
1514*f4a2713aSLionel Sambucof a ``BasicBlock`` and the number of ``Instruction``\ s it contains:
1515*f4a2713aSLionel Sambuc
1516*f4a2713aSLionel Sambuc.. code-block:: c++
1517*f4a2713aSLionel Sambuc
1518*f4a2713aSLionel Sambuc  // func is a pointer to a Function instance
1519*f4a2713aSLionel Sambuc  for (Function::iterator i = func->begin(), e = func->end(); i != e; ++i)
1520*f4a2713aSLionel Sambuc    // Print out the name of the basic block if it has one, and then the
1521*f4a2713aSLionel Sambuc    // number of instructions that it contains
1522*f4a2713aSLionel Sambuc    errs() << "Basic block (name=" << i->getName() << ") has "
1523*f4a2713aSLionel Sambuc               << i->size() << " instructions.\n";
1524*f4a2713aSLionel Sambuc
1525*f4a2713aSLionel SambucNote that i can be used as if it were a pointer for the purposes of invoking
1526*f4a2713aSLionel Sambucmember functions of the ``Instruction`` class.  This is because the indirection
1527*f4a2713aSLionel Sambucoperator is overloaded for the iterator classes.  In the above code, the
1528*f4a2713aSLionel Sambucexpression ``i->size()`` is exactly equivalent to ``(*i).size()`` just like
1529*f4a2713aSLionel Sambucyou'd expect.
1530*f4a2713aSLionel Sambuc
1531*f4a2713aSLionel Sambuc.. _iterate_basicblock:
1532*f4a2713aSLionel Sambuc
1533*f4a2713aSLionel SambucIterating over the ``Instruction`` in a ``BasicBlock``
1534*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1535*f4a2713aSLionel Sambuc
1536*f4a2713aSLionel SambucJust like when dealing with ``BasicBlock``\ s in ``Function``\ s, it's easy to
1537*f4a2713aSLionel Sambuciterate over the individual instructions that make up ``BasicBlock``\ s.  Here's
1538*f4a2713aSLionel Sambuca code snippet that prints out each instruction in a ``BasicBlock``:
1539*f4a2713aSLionel Sambuc
1540*f4a2713aSLionel Sambuc.. code-block:: c++
1541*f4a2713aSLionel Sambuc
1542*f4a2713aSLionel Sambuc  // blk is a pointer to a BasicBlock instance
1543*f4a2713aSLionel Sambuc  for (BasicBlock::iterator i = blk->begin(), e = blk->end(); i != e; ++i)
1544*f4a2713aSLionel Sambuc     // The next statement works since operator<<(ostream&,...)
1545*f4a2713aSLionel Sambuc     // is overloaded for Instruction&
1546*f4a2713aSLionel Sambuc     errs() << *i << "\n";
1547*f4a2713aSLionel Sambuc
1548*f4a2713aSLionel Sambuc
1549*f4a2713aSLionel SambucHowever, this isn't really the best way to print out the contents of a
1550*f4a2713aSLionel Sambuc``BasicBlock``!  Since the ostream operators are overloaded for virtually
1551*f4a2713aSLionel Sambucanything you'll care about, you could have just invoked the print routine on the
1552*f4a2713aSLionel Sambucbasic block itself: ``errs() << *blk << "\n";``.
1553*f4a2713aSLionel Sambuc
1554*f4a2713aSLionel Sambuc.. _iterate_insiter:
1555*f4a2713aSLionel Sambuc
1556*f4a2713aSLionel SambucIterating over the ``Instruction`` in a ``Function``
1557*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1558*f4a2713aSLionel Sambuc
1559*f4a2713aSLionel SambucIf you're finding that you commonly iterate over a ``Function``'s
1560*f4a2713aSLionel Sambuc``BasicBlock``\ s and then that ``BasicBlock``'s ``Instruction``\ s,
1561*f4a2713aSLionel Sambuc``InstIterator`` should be used instead.  You'll need to include
1562*f4a2713aSLionel Sambuc``llvm/Support/InstIterator.h`` (`doxygen
1563*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/InstIterator_8h-source.html>`__) and then instantiate
1564*f4a2713aSLionel Sambuc``InstIterator``\ s explicitly in your code.  Here's a small example that shows
1565*f4a2713aSLionel Sambuchow to dump all instructions in a function to the standard error stream:
1566*f4a2713aSLionel Sambuc
1567*f4a2713aSLionel Sambuc.. code-block:: c++
1568*f4a2713aSLionel Sambuc
1569*f4a2713aSLionel Sambuc  #include "llvm/Support/InstIterator.h"
1570*f4a2713aSLionel Sambuc
1571*f4a2713aSLionel Sambuc  // F is a pointer to a Function instance
1572*f4a2713aSLionel Sambuc  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
1573*f4a2713aSLionel Sambuc    errs() << *I << "\n";
1574*f4a2713aSLionel Sambuc
1575*f4a2713aSLionel SambucEasy, isn't it?  You can also use ``InstIterator``\ s to fill a work list with
1576*f4a2713aSLionel Sambucits initial contents.  For example, if you wanted to initialize a work list to
1577*f4a2713aSLionel Sambuccontain all instructions in a ``Function`` F, all you would need to do is
1578*f4a2713aSLionel Sambucsomething like:
1579*f4a2713aSLionel Sambuc
1580*f4a2713aSLionel Sambuc.. code-block:: c++
1581*f4a2713aSLionel Sambuc
1582*f4a2713aSLionel Sambuc  std::set<Instruction*> worklist;
1583*f4a2713aSLionel Sambuc  // or better yet, SmallPtrSet<Instruction*, 64> worklist;
1584*f4a2713aSLionel Sambuc
1585*f4a2713aSLionel Sambuc  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
1586*f4a2713aSLionel Sambuc    worklist.insert(&*I);
1587*f4a2713aSLionel Sambuc
1588*f4a2713aSLionel SambucThe STL set ``worklist`` would now contain all instructions in the ``Function``
1589*f4a2713aSLionel Sambucpointed to by F.
1590*f4a2713aSLionel Sambuc
1591*f4a2713aSLionel Sambuc.. _iterate_convert:
1592*f4a2713aSLionel Sambuc
1593*f4a2713aSLionel SambucTurning an iterator into a class pointer (and vice-versa)
1594*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1595*f4a2713aSLionel Sambuc
1596*f4a2713aSLionel SambucSometimes, it'll be useful to grab a reference (or pointer) to a class instance
1597*f4a2713aSLionel Sambucwhen all you've got at hand is an iterator.  Well, extracting a reference or a
1598*f4a2713aSLionel Sambucpointer from an iterator is very straight-forward.  Assuming that ``i`` is a
1599*f4a2713aSLionel Sambuc``BasicBlock::iterator`` and ``j`` is a ``BasicBlock::const_iterator``:
1600*f4a2713aSLionel Sambuc
1601*f4a2713aSLionel Sambuc.. code-block:: c++
1602*f4a2713aSLionel Sambuc
1603*f4a2713aSLionel Sambuc  Instruction& inst = *i;   // Grab reference to instruction reference
1604*f4a2713aSLionel Sambuc  Instruction* pinst = &*i; // Grab pointer to instruction reference
1605*f4a2713aSLionel Sambuc  const Instruction& inst = *j;
1606*f4a2713aSLionel Sambuc
1607*f4a2713aSLionel SambucHowever, the iterators you'll be working with in the LLVM framework are special:
1608*f4a2713aSLionel Sambucthey will automatically convert to a ptr-to-instance type whenever they need to.
1609*f4a2713aSLionel SambucInstead of derferencing the iterator and then taking the address of the result,
1610*f4a2713aSLionel Sambucyou can simply assign the iterator to the proper pointer type and you get the
1611*f4a2713aSLionel Sambucdereference and address-of operation as a result of the assignment (behind the
1612*f4a2713aSLionel Sambucscenes, this is a result of overloading casting mechanisms).  Thus the last line
1613*f4a2713aSLionel Sambucof the last example,
1614*f4a2713aSLionel Sambuc
1615*f4a2713aSLionel Sambuc.. code-block:: c++
1616*f4a2713aSLionel Sambuc
1617*f4a2713aSLionel Sambuc  Instruction *pinst = &*i;
1618*f4a2713aSLionel Sambuc
1619*f4a2713aSLionel Sambucis semantically equivalent to
1620*f4a2713aSLionel Sambuc
1621*f4a2713aSLionel Sambuc.. code-block:: c++
1622*f4a2713aSLionel Sambuc
1623*f4a2713aSLionel Sambuc  Instruction *pinst = i;
1624*f4a2713aSLionel Sambuc
1625*f4a2713aSLionel SambucIt's also possible to turn a class pointer into the corresponding iterator, and
1626*f4a2713aSLionel Sambucthis is a constant time operation (very efficient).  The following code snippet
1627*f4a2713aSLionel Sambucillustrates use of the conversion constructors provided by LLVM iterators.  By
1628*f4a2713aSLionel Sambucusing these, you can explicitly grab the iterator of something without actually
1629*f4a2713aSLionel Sambucobtaining it via iteration over some structure:
1630*f4a2713aSLionel Sambuc
1631*f4a2713aSLionel Sambuc.. code-block:: c++
1632*f4a2713aSLionel Sambuc
1633*f4a2713aSLionel Sambuc  void printNextInstruction(Instruction* inst) {
1634*f4a2713aSLionel Sambuc    BasicBlock::iterator it(inst);
1635*f4a2713aSLionel Sambuc    ++it; // After this line, it refers to the instruction after *inst
1636*f4a2713aSLionel Sambuc    if (it != inst->getParent()->end()) errs() << *it << "\n";
1637*f4a2713aSLionel Sambuc  }
1638*f4a2713aSLionel Sambuc
1639*f4a2713aSLionel SambucUnfortunately, these implicit conversions come at a cost; they prevent these
1640*f4a2713aSLionel Sambuciterators from conforming to standard iterator conventions, and thus from being
1641*f4a2713aSLionel Sambucusable with standard algorithms and containers.  For example, they prevent the
1642*f4a2713aSLionel Sambucfollowing code, where ``B`` is a ``BasicBlock``, from compiling:
1643*f4a2713aSLionel Sambuc
1644*f4a2713aSLionel Sambuc.. code-block:: c++
1645*f4a2713aSLionel Sambuc
1646*f4a2713aSLionel Sambuc  llvm::SmallVector<llvm::Instruction *, 16>(B->begin(), B->end());
1647*f4a2713aSLionel Sambuc
1648*f4a2713aSLionel SambucBecause of this, these implicit conversions may be removed some day, and
1649*f4a2713aSLionel Sambuc``operator*`` changed to return a pointer instead of a reference.
1650*f4a2713aSLionel Sambuc
1651*f4a2713aSLionel Sambuc.. _iterate_complex:
1652*f4a2713aSLionel Sambuc
1653*f4a2713aSLionel SambucFinding call sites: a slightly more complex example
1654*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1655*f4a2713aSLionel Sambuc
1656*f4a2713aSLionel SambucSay that you're writing a FunctionPass and would like to count all the locations
1657*f4a2713aSLionel Sambucin the entire module (that is, across every ``Function``) where a certain
1658*f4a2713aSLionel Sambucfunction (i.e., some ``Function *``) is already in scope.  As you'll learn
1659*f4a2713aSLionel Sambuclater, you may want to use an ``InstVisitor`` to accomplish this in a much more
1660*f4a2713aSLionel Sambucstraight-forward manner, but this example will allow us to explore how you'd do
1661*f4a2713aSLionel Sambucit if you didn't have ``InstVisitor`` around.  In pseudo-code, this is what we
1662*f4a2713aSLionel Sambucwant to do:
1663*f4a2713aSLionel Sambuc
1664*f4a2713aSLionel Sambuc.. code-block:: none
1665*f4a2713aSLionel Sambuc
1666*f4a2713aSLionel Sambuc  initialize callCounter to zero
1667*f4a2713aSLionel Sambuc  for each Function f in the Module
1668*f4a2713aSLionel Sambuc    for each BasicBlock b in f
1669*f4a2713aSLionel Sambuc      for each Instruction i in b
1670*f4a2713aSLionel Sambuc        if (i is a CallInst and calls the given function)
1671*f4a2713aSLionel Sambuc          increment callCounter
1672*f4a2713aSLionel Sambuc
1673*f4a2713aSLionel SambucAnd the actual code is (remember, because we're writing a ``FunctionPass``, our
1674*f4a2713aSLionel Sambuc``FunctionPass``-derived class simply has to override the ``runOnFunction``
1675*f4a2713aSLionel Sambucmethod):
1676*f4a2713aSLionel Sambuc
1677*f4a2713aSLionel Sambuc.. code-block:: c++
1678*f4a2713aSLionel Sambuc
1679*f4a2713aSLionel Sambuc  Function* targetFunc = ...;
1680*f4a2713aSLionel Sambuc
1681*f4a2713aSLionel Sambuc  class OurFunctionPass : public FunctionPass {
1682*f4a2713aSLionel Sambuc    public:
1683*f4a2713aSLionel Sambuc      OurFunctionPass(): callCounter(0) { }
1684*f4a2713aSLionel Sambuc
1685*f4a2713aSLionel Sambuc      virtual runOnFunction(Function& F) {
1686*f4a2713aSLionel Sambuc        for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) {
1687*f4a2713aSLionel Sambuc          for (BasicBlock::iterator i = b->begin(), ie = b->end(); i != ie; ++i) {
1688*f4a2713aSLionel Sambuc            if (CallInst* callInst = dyn_cast<CallInst>(&*i)) {
1689*f4a2713aSLionel Sambuc              // We know we've encountered a call instruction, so we
1690*f4a2713aSLionel Sambuc              // need to determine if it's a call to the
1691*f4a2713aSLionel Sambuc              // function pointed to by m_func or not.
1692*f4a2713aSLionel Sambuc              if (callInst->getCalledFunction() == targetFunc)
1693*f4a2713aSLionel Sambuc                ++callCounter;
1694*f4a2713aSLionel Sambuc            }
1695*f4a2713aSLionel Sambuc          }
1696*f4a2713aSLionel Sambuc        }
1697*f4a2713aSLionel Sambuc      }
1698*f4a2713aSLionel Sambuc
1699*f4a2713aSLionel Sambuc    private:
1700*f4a2713aSLionel Sambuc      unsigned callCounter;
1701*f4a2713aSLionel Sambuc  };
1702*f4a2713aSLionel Sambuc
1703*f4a2713aSLionel Sambuc.. _calls_and_invokes:
1704*f4a2713aSLionel Sambuc
1705*f4a2713aSLionel SambucTreating calls and invokes the same way
1706*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1707*f4a2713aSLionel Sambuc
1708*f4a2713aSLionel SambucYou may have noticed that the previous example was a bit oversimplified in that
1709*f4a2713aSLionel Sambucit did not deal with call sites generated by 'invoke' instructions.  In this,
1710*f4a2713aSLionel Sambucand in other situations, you may find that you want to treat ``CallInst``\ s and
1711*f4a2713aSLionel Sambuc``InvokeInst``\ s the same way, even though their most-specific common base
1712*f4a2713aSLionel Sambucclass is ``Instruction``, which includes lots of less closely-related things.
1713*f4a2713aSLionel SambucFor these cases, LLVM provides a handy wrapper class called ``CallSite``
1714*f4a2713aSLionel Sambuc(`doxygen <http://llvm.org/doxygen/classllvm_1_1CallSite.html>`__) It is
1715*f4a2713aSLionel Sambucessentially a wrapper around an ``Instruction`` pointer, with some methods that
1716*f4a2713aSLionel Sambucprovide functionality common to ``CallInst``\ s and ``InvokeInst``\ s.
1717*f4a2713aSLionel Sambuc
1718*f4a2713aSLionel SambucThis class has "value semantics": it should be passed by value, not by reference
1719*f4a2713aSLionel Sambucand it should not be dynamically allocated or deallocated using ``operator new``
1720*f4a2713aSLionel Sambucor ``operator delete``.  It is efficiently copyable, assignable and
1721*f4a2713aSLionel Sambucconstructable, with costs equivalents to that of a bare pointer.  If you look at
1722*f4a2713aSLionel Sambucits definition, it has only a single pointer member.
1723*f4a2713aSLionel Sambuc
1724*f4a2713aSLionel Sambuc.. _iterate_chains:
1725*f4a2713aSLionel Sambuc
1726*f4a2713aSLionel SambucIterating over def-use & use-def chains
1727*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1728*f4a2713aSLionel Sambuc
1729*f4a2713aSLionel SambucFrequently, we might have an instance of the ``Value`` class (`doxygen
1730*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/classllvm_1_1Value.html>`__) and we want to determine
1731*f4a2713aSLionel Sambucwhich ``User`` s use the ``Value``.  The list of all ``User``\ s of a particular
1732*f4a2713aSLionel Sambuc``Value`` is called a *def-use* chain.  For example, let's say we have a
1733*f4a2713aSLionel Sambuc``Function*`` named ``F`` to a particular function ``foo``.  Finding all of the
1734*f4a2713aSLionel Sambucinstructions that *use* ``foo`` is as simple as iterating over the *def-use*
1735*f4a2713aSLionel Sambucchain of ``F``:
1736*f4a2713aSLionel Sambuc
1737*f4a2713aSLionel Sambuc.. code-block:: c++
1738*f4a2713aSLionel Sambuc
1739*f4a2713aSLionel Sambuc  Function *F = ...;
1740*f4a2713aSLionel Sambuc
1741*f4a2713aSLionel Sambuc  for (Value::use_iterator i = F->use_begin(), e = F->use_end(); i != e; ++i)
1742*f4a2713aSLionel Sambuc    if (Instruction *Inst = dyn_cast<Instruction>(*i)) {
1743*f4a2713aSLionel Sambuc      errs() << "F is used in instruction:\n";
1744*f4a2713aSLionel Sambuc      errs() << *Inst << "\n";
1745*f4a2713aSLionel Sambuc    }
1746*f4a2713aSLionel Sambuc
1747*f4a2713aSLionel SambucNote that dereferencing a ``Value::use_iterator`` is not a very cheap operation.
1748*f4a2713aSLionel SambucInstead of performing ``*i`` above several times, consider doing it only once in
1749*f4a2713aSLionel Sambucthe loop body and reusing its result.
1750*f4a2713aSLionel Sambuc
1751*f4a2713aSLionel SambucAlternatively, it's common to have an instance of the ``User`` Class (`doxygen
1752*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/classllvm_1_1User.html>`__) and need to know what
1753*f4a2713aSLionel Sambuc``Value``\ s are used by it.  The list of all ``Value``\ s used by a ``User`` is
1754*f4a2713aSLionel Sambucknown as a *use-def* chain.  Instances of class ``Instruction`` are common
1755*f4a2713aSLionel Sambuc``User`` s, so we might want to iterate over all of the values that a particular
1756*f4a2713aSLionel Sambucinstruction uses (that is, the operands of the particular ``Instruction``):
1757*f4a2713aSLionel Sambuc
1758*f4a2713aSLionel Sambuc.. code-block:: c++
1759*f4a2713aSLionel Sambuc
1760*f4a2713aSLionel Sambuc  Instruction *pi = ...;
1761*f4a2713aSLionel Sambuc
1762*f4a2713aSLionel Sambuc  for (User::op_iterator i = pi->op_begin(), e = pi->op_end(); i != e; ++i) {
1763*f4a2713aSLionel Sambuc    Value *v = *i;
1764*f4a2713aSLionel Sambuc    // ...
1765*f4a2713aSLionel Sambuc  }
1766*f4a2713aSLionel Sambuc
1767*f4a2713aSLionel SambucDeclaring objects as ``const`` is an important tool of enforcing mutation free
1768*f4a2713aSLionel Sambucalgorithms (such as analyses, etc.).  For this purpose above iterators come in
1769*f4a2713aSLionel Sambucconstant flavors as ``Value::const_use_iterator`` and
1770*f4a2713aSLionel Sambuc``Value::const_op_iterator``.  They automatically arise when calling
1771*f4a2713aSLionel Sambuc``use/op_begin()`` on ``const Value*``\ s or ``const User*``\ s respectively.
1772*f4a2713aSLionel SambucUpon dereferencing, they return ``const Use*``\ s.  Otherwise the above patterns
1773*f4a2713aSLionel Sambucremain unchanged.
1774*f4a2713aSLionel Sambuc
1775*f4a2713aSLionel Sambuc.. _iterate_preds:
1776*f4a2713aSLionel Sambuc
1777*f4a2713aSLionel SambucIterating over predecessors & successors of blocks
1778*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1779*f4a2713aSLionel Sambuc
1780*f4a2713aSLionel SambucIterating over the predecessors and successors of a block is quite easy with the
1781*f4a2713aSLionel Sambucroutines defined in ``"llvm/Support/CFG.h"``.  Just use code like this to
1782*f4a2713aSLionel Sambuciterate over all predecessors of BB:
1783*f4a2713aSLionel Sambuc
1784*f4a2713aSLionel Sambuc.. code-block:: c++
1785*f4a2713aSLionel Sambuc
1786*f4a2713aSLionel Sambuc  #include "llvm/Support/CFG.h"
1787*f4a2713aSLionel Sambuc  BasicBlock *BB = ...;
1788*f4a2713aSLionel Sambuc
1789*f4a2713aSLionel Sambuc  for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
1790*f4a2713aSLionel Sambuc    BasicBlock *Pred = *PI;
1791*f4a2713aSLionel Sambuc    // ...
1792*f4a2713aSLionel Sambuc  }
1793*f4a2713aSLionel Sambuc
1794*f4a2713aSLionel SambucSimilarly, to iterate over successors use ``succ_iterator/succ_begin/succ_end``.
1795*f4a2713aSLionel Sambuc
1796*f4a2713aSLionel Sambuc.. _simplechanges:
1797*f4a2713aSLionel Sambuc
1798*f4a2713aSLionel SambucMaking simple changes
1799*f4a2713aSLionel Sambuc---------------------
1800*f4a2713aSLionel Sambuc
1801*f4a2713aSLionel SambucThere are some primitive transformation operations present in the LLVM
1802*f4a2713aSLionel Sambucinfrastructure that are worth knowing about.  When performing transformations,
1803*f4a2713aSLionel Sambucit's fairly common to manipulate the contents of basic blocks.  This section
1804*f4a2713aSLionel Sambucdescribes some of the common methods for doing so and gives example code.
1805*f4a2713aSLionel Sambuc
1806*f4a2713aSLionel Sambuc.. _schanges_creating:
1807*f4a2713aSLionel Sambuc
1808*f4a2713aSLionel SambucCreating and inserting new ``Instruction``\ s
1809*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1810*f4a2713aSLionel Sambuc
1811*f4a2713aSLionel Sambuc*Instantiating Instructions*
1812*f4a2713aSLionel Sambuc
1813*f4a2713aSLionel SambucCreation of ``Instruction``\ s is straight-forward: simply call the constructor
1814*f4a2713aSLionel Sambucfor the kind of instruction to instantiate and provide the necessary parameters.
1815*f4a2713aSLionel SambucFor example, an ``AllocaInst`` only *requires* a (const-ptr-to) ``Type``.  Thus:
1816*f4a2713aSLionel Sambuc
1817*f4a2713aSLionel Sambuc.. code-block:: c++
1818*f4a2713aSLionel Sambuc
1819*f4a2713aSLionel Sambuc  AllocaInst* ai = new AllocaInst(Type::Int32Ty);
1820*f4a2713aSLionel Sambuc
1821*f4a2713aSLionel Sambucwill create an ``AllocaInst`` instance that represents the allocation of one
1822*f4a2713aSLionel Sambucinteger in the current stack frame, at run time.  Each ``Instruction`` subclass
1823*f4a2713aSLionel Sambucis likely to have varying default parameters which change the semantics of the
1824*f4a2713aSLionel Sambucinstruction, so refer to the `doxygen documentation for the subclass of
1825*f4a2713aSLionel SambucInstruction <http://llvm.org/doxygen/classllvm_1_1Instruction.html>`_ that
1826*f4a2713aSLionel Sambucyou're interested in instantiating.
1827*f4a2713aSLionel Sambuc
1828*f4a2713aSLionel Sambuc*Naming values*
1829*f4a2713aSLionel Sambuc
1830*f4a2713aSLionel SambucIt is very useful to name the values of instructions when you're able to, as
1831*f4a2713aSLionel Sambucthis facilitates the debugging of your transformations.  If you end up looking
1832*f4a2713aSLionel Sambucat generated LLVM machine code, you definitely want to have logical names
1833*f4a2713aSLionel Sambucassociated with the results of instructions!  By supplying a value for the
1834*f4a2713aSLionel Sambuc``Name`` (default) parameter of the ``Instruction`` constructor, you associate a
1835*f4a2713aSLionel Sambuclogical name with the result of the instruction's execution at run time.  For
1836*f4a2713aSLionel Sambucexample, say that I'm writing a transformation that dynamically allocates space
1837*f4a2713aSLionel Sambucfor an integer on the stack, and that integer is going to be used as some kind
1838*f4a2713aSLionel Sambucof index by some other code.  To accomplish this, I place an ``AllocaInst`` at
1839*f4a2713aSLionel Sambucthe first point in the first ``BasicBlock`` of some ``Function``, and I'm
1840*f4a2713aSLionel Sambucintending to use it within the same ``Function``.  I might do:
1841*f4a2713aSLionel Sambuc
1842*f4a2713aSLionel Sambuc.. code-block:: c++
1843*f4a2713aSLionel Sambuc
1844*f4a2713aSLionel Sambuc  AllocaInst* pa = new AllocaInst(Type::Int32Ty, 0, "indexLoc");
1845*f4a2713aSLionel Sambuc
1846*f4a2713aSLionel Sambucwhere ``indexLoc`` is now the logical name of the instruction's execution value,
1847*f4a2713aSLionel Sambucwhich is a pointer to an integer on the run time stack.
1848*f4a2713aSLionel Sambuc
1849*f4a2713aSLionel Sambuc*Inserting instructions*
1850*f4a2713aSLionel Sambuc
1851*f4a2713aSLionel SambucThere are essentially two ways to insert an ``Instruction`` into an existing
1852*f4a2713aSLionel Sambucsequence of instructions that form a ``BasicBlock``:
1853*f4a2713aSLionel Sambuc
1854*f4a2713aSLionel Sambuc* Insertion into an explicit instruction list
1855*f4a2713aSLionel Sambuc
1856*f4a2713aSLionel Sambuc  Given a ``BasicBlock* pb``, an ``Instruction* pi`` within that ``BasicBlock``,
1857*f4a2713aSLionel Sambuc  and a newly-created instruction we wish to insert before ``*pi``, we do the
1858*f4a2713aSLionel Sambuc  following:
1859*f4a2713aSLionel Sambuc
1860*f4a2713aSLionel Sambuc  .. code-block:: c++
1861*f4a2713aSLionel Sambuc
1862*f4a2713aSLionel Sambuc      BasicBlock *pb = ...;
1863*f4a2713aSLionel Sambuc      Instruction *pi = ...;
1864*f4a2713aSLionel Sambuc      Instruction *newInst = new Instruction(...);
1865*f4a2713aSLionel Sambuc
1866*f4a2713aSLionel Sambuc      pb->getInstList().insert(pi, newInst); // Inserts newInst before pi in pb
1867*f4a2713aSLionel Sambuc
1868*f4a2713aSLionel Sambuc  Appending to the end of a ``BasicBlock`` is so common that the ``Instruction``
1869*f4a2713aSLionel Sambuc  class and ``Instruction``-derived classes provide constructors which take a
1870*f4a2713aSLionel Sambuc  pointer to a ``BasicBlock`` to be appended to.  For example code that looked
1871*f4a2713aSLionel Sambuc  like:
1872*f4a2713aSLionel Sambuc
1873*f4a2713aSLionel Sambuc  .. code-block:: c++
1874*f4a2713aSLionel Sambuc
1875*f4a2713aSLionel Sambuc    BasicBlock *pb = ...;
1876*f4a2713aSLionel Sambuc    Instruction *newInst = new Instruction(...);
1877*f4a2713aSLionel Sambuc
1878*f4a2713aSLionel Sambuc    pb->getInstList().push_back(newInst); // Appends newInst to pb
1879*f4a2713aSLionel Sambuc
1880*f4a2713aSLionel Sambuc  becomes:
1881*f4a2713aSLionel Sambuc
1882*f4a2713aSLionel Sambuc  .. code-block:: c++
1883*f4a2713aSLionel Sambuc
1884*f4a2713aSLionel Sambuc    BasicBlock *pb = ...;
1885*f4a2713aSLionel Sambuc    Instruction *newInst = new Instruction(..., pb);
1886*f4a2713aSLionel Sambuc
1887*f4a2713aSLionel Sambuc  which is much cleaner, especially if you are creating long instruction
1888*f4a2713aSLionel Sambuc  streams.
1889*f4a2713aSLionel Sambuc
1890*f4a2713aSLionel Sambuc* Insertion into an implicit instruction list
1891*f4a2713aSLionel Sambuc
1892*f4a2713aSLionel Sambuc  ``Instruction`` instances that are already in ``BasicBlock``\ s are implicitly
1893*f4a2713aSLionel Sambuc  associated with an existing instruction list: the instruction list of the
1894*f4a2713aSLionel Sambuc  enclosing basic block.  Thus, we could have accomplished the same thing as the
1895*f4a2713aSLionel Sambuc  above code without being given a ``BasicBlock`` by doing:
1896*f4a2713aSLionel Sambuc
1897*f4a2713aSLionel Sambuc  .. code-block:: c++
1898*f4a2713aSLionel Sambuc
1899*f4a2713aSLionel Sambuc    Instruction *pi = ...;
1900*f4a2713aSLionel Sambuc    Instruction *newInst = new Instruction(...);
1901*f4a2713aSLionel Sambuc
1902*f4a2713aSLionel Sambuc    pi->getParent()->getInstList().insert(pi, newInst);
1903*f4a2713aSLionel Sambuc
1904*f4a2713aSLionel Sambuc  In fact, this sequence of steps occurs so frequently that the ``Instruction``
1905*f4a2713aSLionel Sambuc  class and ``Instruction``-derived classes provide constructors which take (as
1906*f4a2713aSLionel Sambuc  a default parameter) a pointer to an ``Instruction`` which the newly-created
1907*f4a2713aSLionel Sambuc  ``Instruction`` should precede.  That is, ``Instruction`` constructors are
1908*f4a2713aSLionel Sambuc  capable of inserting the newly-created instance into the ``BasicBlock`` of a
1909*f4a2713aSLionel Sambuc  provided instruction, immediately before that instruction.  Using an
1910*f4a2713aSLionel Sambuc  ``Instruction`` constructor with a ``insertBefore`` (default) parameter, the
1911*f4a2713aSLionel Sambuc  above code becomes:
1912*f4a2713aSLionel Sambuc
1913*f4a2713aSLionel Sambuc  .. code-block:: c++
1914*f4a2713aSLionel Sambuc
1915*f4a2713aSLionel Sambuc    Instruction* pi = ...;
1916*f4a2713aSLionel Sambuc    Instruction* newInst = new Instruction(..., pi);
1917*f4a2713aSLionel Sambuc
1918*f4a2713aSLionel Sambuc  which is much cleaner, especially if you're creating a lot of instructions and
1919*f4a2713aSLionel Sambuc  adding them to ``BasicBlock``\ s.
1920*f4a2713aSLionel Sambuc
1921*f4a2713aSLionel Sambuc.. _schanges_deleting:
1922*f4a2713aSLionel Sambuc
1923*f4a2713aSLionel SambucDeleting Instructions
1924*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^
1925*f4a2713aSLionel Sambuc
1926*f4a2713aSLionel SambucDeleting an instruction from an existing sequence of instructions that form a
1927*f4a2713aSLionel SambucBasicBlock_ is very straight-forward: just call the instruction's
1928*f4a2713aSLionel Sambuc``eraseFromParent()`` method.  For example:
1929*f4a2713aSLionel Sambuc
1930*f4a2713aSLionel Sambuc.. code-block:: c++
1931*f4a2713aSLionel Sambuc
1932*f4a2713aSLionel Sambuc  Instruction *I = .. ;
1933*f4a2713aSLionel Sambuc  I->eraseFromParent();
1934*f4a2713aSLionel Sambuc
1935*f4a2713aSLionel SambucThis unlinks the instruction from its containing basic block and deletes it.  If
1936*f4a2713aSLionel Sambucyou'd just like to unlink the instruction from its containing basic block but
1937*f4a2713aSLionel Sambucnot delete it, you can use the ``removeFromParent()`` method.
1938*f4a2713aSLionel Sambuc
1939*f4a2713aSLionel Sambuc.. _schanges_replacing:
1940*f4a2713aSLionel Sambuc
1941*f4a2713aSLionel SambucReplacing an Instruction with another Value
1942*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1943*f4a2713aSLionel Sambuc
1944*f4a2713aSLionel SambucReplacing individual instructions
1945*f4a2713aSLionel Sambuc"""""""""""""""""""""""""""""""""
1946*f4a2713aSLionel Sambuc
1947*f4a2713aSLionel SambucIncluding "`llvm/Transforms/Utils/BasicBlockUtils.h
1948*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/BasicBlockUtils_8h-source.html>`_" permits use of two
1949*f4a2713aSLionel Sambucvery useful replace functions: ``ReplaceInstWithValue`` and
1950*f4a2713aSLionel Sambuc``ReplaceInstWithInst``.
1951*f4a2713aSLionel Sambuc
1952*f4a2713aSLionel Sambuc.. _schanges_deleting_sub:
1953*f4a2713aSLionel Sambuc
1954*f4a2713aSLionel SambucDeleting Instructions
1955*f4a2713aSLionel Sambuc"""""""""""""""""""""
1956*f4a2713aSLionel Sambuc
1957*f4a2713aSLionel Sambuc* ``ReplaceInstWithValue``
1958*f4a2713aSLionel Sambuc
1959*f4a2713aSLionel Sambuc  This function replaces all uses of a given instruction with a value, and then
1960*f4a2713aSLionel Sambuc  removes the original instruction.  The following example illustrates the
1961*f4a2713aSLionel Sambuc  replacement of the result of a particular ``AllocaInst`` that allocates memory
1962*f4a2713aSLionel Sambuc  for a single integer with a null pointer to an integer.
1963*f4a2713aSLionel Sambuc
1964*f4a2713aSLionel Sambuc  .. code-block:: c++
1965*f4a2713aSLionel Sambuc
1966*f4a2713aSLionel Sambuc    AllocaInst* instToReplace = ...;
1967*f4a2713aSLionel Sambuc    BasicBlock::iterator ii(instToReplace);
1968*f4a2713aSLionel Sambuc
1969*f4a2713aSLionel Sambuc    ReplaceInstWithValue(instToReplace->getParent()->getInstList(), ii,
1970*f4a2713aSLionel Sambuc                         Constant::getNullValue(PointerType::getUnqual(Type::Int32Ty)));
1971*f4a2713aSLionel Sambuc
1972*f4a2713aSLionel Sambuc* ``ReplaceInstWithInst``
1973*f4a2713aSLionel Sambuc
1974*f4a2713aSLionel Sambuc  This function replaces a particular instruction with another instruction,
1975*f4a2713aSLionel Sambuc  inserting the new instruction into the basic block at the location where the
1976*f4a2713aSLionel Sambuc  old instruction was, and replacing any uses of the old instruction with the
1977*f4a2713aSLionel Sambuc  new instruction.  The following example illustrates the replacement of one
1978*f4a2713aSLionel Sambuc  ``AllocaInst`` with another.
1979*f4a2713aSLionel Sambuc
1980*f4a2713aSLionel Sambuc  .. code-block:: c++
1981*f4a2713aSLionel Sambuc
1982*f4a2713aSLionel Sambuc    AllocaInst* instToReplace = ...;
1983*f4a2713aSLionel Sambuc    BasicBlock::iterator ii(instToReplace);
1984*f4a2713aSLionel Sambuc
1985*f4a2713aSLionel Sambuc    ReplaceInstWithInst(instToReplace->getParent()->getInstList(), ii,
1986*f4a2713aSLionel Sambuc                        new AllocaInst(Type::Int32Ty, 0, "ptrToReplacedInt"));
1987*f4a2713aSLionel Sambuc
1988*f4a2713aSLionel Sambuc
1989*f4a2713aSLionel SambucReplacing multiple uses of Users and Values
1990*f4a2713aSLionel Sambuc"""""""""""""""""""""""""""""""""""""""""""
1991*f4a2713aSLionel Sambuc
1992*f4a2713aSLionel SambucYou can use ``Value::replaceAllUsesWith`` and ``User::replaceUsesOfWith`` to
1993*f4a2713aSLionel Sambucchange more than one use at a time.  See the doxygen documentation for the
1994*f4a2713aSLionel Sambuc`Value Class <http://llvm.org/doxygen/classllvm_1_1Value.html>`_ and `User Class
1995*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/classllvm_1_1User.html>`_, respectively, for more
1996*f4a2713aSLionel Sambucinformation.
1997*f4a2713aSLionel Sambuc
1998*f4a2713aSLionel Sambuc.. _schanges_deletingGV:
1999*f4a2713aSLionel Sambuc
2000*f4a2713aSLionel SambucDeleting GlobalVariables
2001*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^
2002*f4a2713aSLionel Sambuc
2003*f4a2713aSLionel SambucDeleting a global variable from a module is just as easy as deleting an
2004*f4a2713aSLionel SambucInstruction.  First, you must have a pointer to the global variable that you
2005*f4a2713aSLionel Sambucwish to delete.  You use this pointer to erase it from its parent, the module.
2006*f4a2713aSLionel SambucFor example:
2007*f4a2713aSLionel Sambuc
2008*f4a2713aSLionel Sambuc.. code-block:: c++
2009*f4a2713aSLionel Sambuc
2010*f4a2713aSLionel Sambuc  GlobalVariable *GV = .. ;
2011*f4a2713aSLionel Sambuc
2012*f4a2713aSLionel Sambuc  GV->eraseFromParent();
2013*f4a2713aSLionel Sambuc
2014*f4a2713aSLionel Sambuc
2015*f4a2713aSLionel Sambuc.. _create_types:
2016*f4a2713aSLionel Sambuc
2017*f4a2713aSLionel SambucHow to Create Types
2018*f4a2713aSLionel Sambuc-------------------
2019*f4a2713aSLionel Sambuc
2020*f4a2713aSLionel SambucIn generating IR, you may need some complex types.  If you know these types
2021*f4a2713aSLionel Sambucstatically, you can use ``TypeBuilder<...>::get()``, defined in
2022*f4a2713aSLionel Sambuc``llvm/Support/TypeBuilder.h``, to retrieve them.  ``TypeBuilder`` has two forms
2023*f4a2713aSLionel Sambucdepending on whether you're building types for cross-compilation or native
2024*f4a2713aSLionel Sambuclibrary use.  ``TypeBuilder<T, true>`` requires that ``T`` be independent of the
2025*f4a2713aSLionel Sambuchost environment, meaning that it's built out of types from the ``llvm::types``
2026*f4a2713aSLionel Sambuc(`doxygen <http://llvm.org/doxygen/namespacellvm_1_1types.html>`__) namespace
2027*f4a2713aSLionel Sambucand pointers, functions, arrays, etc. built of those.  ``TypeBuilder<T, false>``
2028*f4a2713aSLionel Sambucadditionally allows native C types whose size may depend on the host compiler.
2029*f4a2713aSLionel SambucFor example,
2030*f4a2713aSLionel Sambuc
2031*f4a2713aSLionel Sambuc.. code-block:: c++
2032*f4a2713aSLionel Sambuc
2033*f4a2713aSLionel Sambuc  FunctionType *ft = TypeBuilder<types::i<8>(types::i<32>*), true>::get();
2034*f4a2713aSLionel Sambuc
2035*f4a2713aSLionel Sambucis easier to read and write than the equivalent
2036*f4a2713aSLionel Sambuc
2037*f4a2713aSLionel Sambuc.. code-block:: c++
2038*f4a2713aSLionel Sambuc
2039*f4a2713aSLionel Sambuc  std::vector<const Type*> params;
2040*f4a2713aSLionel Sambuc  params.push_back(PointerType::getUnqual(Type::Int32Ty));
2041*f4a2713aSLionel Sambuc  FunctionType *ft = FunctionType::get(Type::Int8Ty, params, false);
2042*f4a2713aSLionel Sambuc
2043*f4a2713aSLionel SambucSee the `class comment
2044*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/TypeBuilder_8h-source.html#l00001>`_ for more details.
2045*f4a2713aSLionel Sambuc
2046*f4a2713aSLionel Sambuc.. _threading:
2047*f4a2713aSLionel Sambuc
2048*f4a2713aSLionel SambucThreads and LLVM
2049*f4a2713aSLionel Sambuc================
2050*f4a2713aSLionel Sambuc
2051*f4a2713aSLionel SambucThis section describes the interaction of the LLVM APIs with multithreading,
2052*f4a2713aSLionel Sambucboth on the part of client applications, and in the JIT, in the hosted
2053*f4a2713aSLionel Sambucapplication.
2054*f4a2713aSLionel Sambuc
2055*f4a2713aSLionel SambucNote that LLVM's support for multithreading is still relatively young.  Up
2056*f4a2713aSLionel Sambucthrough version 2.5, the execution of threaded hosted applications was
2057*f4a2713aSLionel Sambucsupported, but not threaded client access to the APIs.  While this use case is
2058*f4a2713aSLionel Sambucnow supported, clients *must* adhere to the guidelines specified below to ensure
2059*f4a2713aSLionel Sambucproper operation in multithreaded mode.
2060*f4a2713aSLionel Sambuc
2061*f4a2713aSLionel SambucNote that, on Unix-like platforms, LLVM requires the presence of GCC's atomic
2062*f4a2713aSLionel Sambucintrinsics in order to support threaded operation.  If you need a
2063*f4a2713aSLionel Sambucmulthreading-capable LLVM on a platform without a suitably modern system
2064*f4a2713aSLionel Sambuccompiler, consider compiling LLVM and LLVM-GCC in single-threaded mode, and
2065*f4a2713aSLionel Sambucusing the resultant compiler to build a copy of LLVM with multithreading
2066*f4a2713aSLionel Sambucsupport.
2067*f4a2713aSLionel Sambuc
2068*f4a2713aSLionel Sambuc.. _startmultithreaded:
2069*f4a2713aSLionel Sambuc
2070*f4a2713aSLionel SambucEntering and Exiting Multithreaded Mode
2071*f4a2713aSLionel Sambuc---------------------------------------
2072*f4a2713aSLionel Sambuc
2073*f4a2713aSLionel SambucIn order to properly protect its internal data structures while avoiding
2074*f4a2713aSLionel Sambucexcessive locking overhead in the single-threaded case, the LLVM must intialize
2075*f4a2713aSLionel Sambuccertain data structures necessary to provide guards around its internals.  To do
2076*f4a2713aSLionel Sambucso, the client program must invoke ``llvm_start_multithreaded()`` before making
2077*f4a2713aSLionel Sambucany concurrent LLVM API calls.  To subsequently tear down these structures, use
2078*f4a2713aSLionel Sambucthe ``llvm_stop_multithreaded()`` call.  You can also use the
2079*f4a2713aSLionel Sambuc``llvm_is_multithreaded()`` call to check the status of multithreaded mode.
2080*f4a2713aSLionel Sambuc
2081*f4a2713aSLionel SambucNote that both of these calls must be made *in isolation*.  That is to say that
2082*f4a2713aSLionel Sambucno other LLVM API calls may be executing at any time during the execution of
2083*f4a2713aSLionel Sambuc``llvm_start_multithreaded()`` or ``llvm_stop_multithreaded``.  It is the
2084*f4a2713aSLionel Sambucclient's responsibility to enforce this isolation.
2085*f4a2713aSLionel Sambuc
2086*f4a2713aSLionel SambucThe return value of ``llvm_start_multithreaded()`` indicates the success or
2087*f4a2713aSLionel Sambucfailure of the initialization.  Failure typically indicates that your copy of
2088*f4a2713aSLionel SambucLLVM was built without multithreading support, typically because GCC atomic
2089*f4a2713aSLionel Sambucintrinsics were not found in your system compiler.  In this case, the LLVM API
2090*f4a2713aSLionel Sambucwill not be safe for concurrent calls.  However, it *will* be safe for hosting
2091*f4a2713aSLionel Sambucthreaded applications in the JIT, though :ref:`care must be taken
2092*f4a2713aSLionel Sambuc<jitthreading>` to ensure that side exits and the like do not accidentally
2093*f4a2713aSLionel Sambucresult in concurrent LLVM API calls.
2094*f4a2713aSLionel Sambuc
2095*f4a2713aSLionel Sambuc.. _shutdown:
2096*f4a2713aSLionel Sambuc
2097*f4a2713aSLionel SambucEnding Execution with ``llvm_shutdown()``
2098*f4a2713aSLionel Sambuc-----------------------------------------
2099*f4a2713aSLionel Sambuc
2100*f4a2713aSLionel SambucWhen you are done using the LLVM APIs, you should call ``llvm_shutdown()`` to
2101*f4a2713aSLionel Sambucdeallocate memory used for internal structures.  This will also invoke
2102*f4a2713aSLionel Sambuc``llvm_stop_multithreaded()`` if LLVM is operating in multithreaded mode.  As
2103*f4a2713aSLionel Sambucsuch, ``llvm_shutdown()`` requires the same isolation guarantees as
2104*f4a2713aSLionel Sambuc``llvm_stop_multithreaded()``.
2105*f4a2713aSLionel Sambuc
2106*f4a2713aSLionel SambucNote that, if you use scope-based shutdown, you can use the
2107*f4a2713aSLionel Sambuc``llvm_shutdown_obj`` class, which calls ``llvm_shutdown()`` in its destructor.
2108*f4a2713aSLionel Sambuc
2109*f4a2713aSLionel Sambuc.. _managedstatic:
2110*f4a2713aSLionel Sambuc
2111*f4a2713aSLionel SambucLazy Initialization with ``ManagedStatic``
2112*f4a2713aSLionel Sambuc------------------------------------------
2113*f4a2713aSLionel Sambuc
2114*f4a2713aSLionel Sambuc``ManagedStatic`` is a utility class in LLVM used to implement static
2115*f4a2713aSLionel Sambucinitialization of static resources, such as the global type tables.  Before the
2116*f4a2713aSLionel Sambucinvocation of ``llvm_shutdown()``, it implements a simple lazy initialization
2117*f4a2713aSLionel Sambucscheme.  Once ``llvm_start_multithreaded()`` returns, however, it uses
2118*f4a2713aSLionel Sambucdouble-checked locking to implement thread-safe lazy initialization.
2119*f4a2713aSLionel Sambuc
2120*f4a2713aSLionel SambucNote that, because no other threads are allowed to issue LLVM API calls before
2121*f4a2713aSLionel Sambuc``llvm_start_multithreaded()`` returns, it is possible to have
2122*f4a2713aSLionel Sambuc``ManagedStatic``\ s of ``llvm::sys::Mutex``\ s.
2123*f4a2713aSLionel Sambuc
2124*f4a2713aSLionel SambucThe ``llvm_acquire_global_lock()`` and ``llvm_release_global_lock`` APIs provide
2125*f4a2713aSLionel Sambucaccess to the global lock used to implement the double-checked locking for lazy
2126*f4a2713aSLionel Sambucinitialization.  These should only be used internally to LLVM, and only if you
2127*f4a2713aSLionel Sambucknow what you're doing!
2128*f4a2713aSLionel Sambuc
2129*f4a2713aSLionel Sambuc.. _llvmcontext:
2130*f4a2713aSLionel Sambuc
2131*f4a2713aSLionel SambucAchieving Isolation with ``LLVMContext``
2132*f4a2713aSLionel Sambuc----------------------------------------
2133*f4a2713aSLionel Sambuc
2134*f4a2713aSLionel Sambuc``LLVMContext`` is an opaque class in the LLVM API which clients can use to
2135*f4a2713aSLionel Sambucoperate multiple, isolated instances of LLVM concurrently within the same
2136*f4a2713aSLionel Sambucaddress space.  For instance, in a hypothetical compile-server, the compilation
2137*f4a2713aSLionel Sambucof an individual translation unit is conceptually independent from all the
2138*f4a2713aSLionel Sambucothers, and it would be desirable to be able to compile incoming translation
2139*f4a2713aSLionel Sambucunits concurrently on independent server threads.  Fortunately, ``LLVMContext``
2140*f4a2713aSLionel Sambucexists to enable just this kind of scenario!
2141*f4a2713aSLionel Sambuc
2142*f4a2713aSLionel SambucConceptually, ``LLVMContext`` provides isolation.  Every LLVM entity
2143*f4a2713aSLionel Sambuc(``Module``\ s, ``Value``\ s, ``Type``\ s, ``Constant``\ s, etc.) in LLVM's
2144*f4a2713aSLionel Sambucin-memory IR belongs to an ``LLVMContext``.  Entities in different contexts
2145*f4a2713aSLionel Sambuc*cannot* interact with each other: ``Module``\ s in different contexts cannot be
2146*f4a2713aSLionel Sambuclinked together, ``Function``\ s cannot be added to ``Module``\ s in different
2147*f4a2713aSLionel Sambuccontexts, etc.  What this means is that is is safe to compile on multiple
2148*f4a2713aSLionel Sambucthreads simultaneously, as long as no two threads operate on entities within the
2149*f4a2713aSLionel Sambucsame context.
2150*f4a2713aSLionel Sambuc
2151*f4a2713aSLionel SambucIn practice, very few places in the API require the explicit specification of a
2152*f4a2713aSLionel Sambuc``LLVMContext``, other than the ``Type`` creation/lookup APIs.  Because every
2153*f4a2713aSLionel Sambuc``Type`` carries a reference to its owning context, most other entities can
2154*f4a2713aSLionel Sambucdetermine what context they belong to by looking at their own ``Type``.  If you
2155*f4a2713aSLionel Sambucare adding new entities to LLVM IR, please try to maintain this interface
2156*f4a2713aSLionel Sambucdesign.
2157*f4a2713aSLionel Sambuc
2158*f4a2713aSLionel SambucFor clients that do *not* require the benefits of isolation, LLVM provides a
2159*f4a2713aSLionel Sambucconvenience API ``getGlobalContext()``.  This returns a global, lazily
2160*f4a2713aSLionel Sambucinitialized ``LLVMContext`` that may be used in situations where isolation is
2161*f4a2713aSLionel Sambucnot a concern.
2162*f4a2713aSLionel Sambuc
2163*f4a2713aSLionel Sambuc.. _jitthreading:
2164*f4a2713aSLionel Sambuc
2165*f4a2713aSLionel SambucThreads and the JIT
2166*f4a2713aSLionel Sambuc-------------------
2167*f4a2713aSLionel Sambuc
2168*f4a2713aSLionel SambucLLVM's "eager" JIT compiler is safe to use in threaded programs.  Multiple
2169*f4a2713aSLionel Sambucthreads can call ``ExecutionEngine::getPointerToFunction()`` or
2170*f4a2713aSLionel Sambuc``ExecutionEngine::runFunction()`` concurrently, and multiple threads can run
2171*f4a2713aSLionel Sambuccode output by the JIT concurrently.  The user must still ensure that only one
2172*f4a2713aSLionel Sambucthread accesses IR in a given ``LLVMContext`` while another thread might be
2173*f4a2713aSLionel Sambucmodifying it.  One way to do that is to always hold the JIT lock while accessing
2174*f4a2713aSLionel SambucIR outside the JIT (the JIT *modifies* the IR by adding ``CallbackVH``\ s).
2175*f4a2713aSLionel SambucAnother way is to only call ``getPointerToFunction()`` from the
2176*f4a2713aSLionel Sambuc``LLVMContext``'s thread.
2177*f4a2713aSLionel Sambuc
2178*f4a2713aSLionel SambucWhen the JIT is configured to compile lazily (using
2179*f4a2713aSLionel Sambuc``ExecutionEngine::DisableLazyCompilation(false)``), there is currently a `race
2180*f4a2713aSLionel Sambuccondition <http://llvm.org/bugs/show_bug.cgi?id=5184>`_ in updating call sites
2181*f4a2713aSLionel Sambucafter a function is lazily-jitted.  It's still possible to use the lazy JIT in a
2182*f4a2713aSLionel Sambucthreaded program if you ensure that only one thread at a time can call any
2183*f4a2713aSLionel Sambucparticular lazy stub and that the JIT lock guards any IR access, but we suggest
2184*f4a2713aSLionel Sambucusing only the eager JIT in threaded programs.
2185*f4a2713aSLionel Sambuc
2186*f4a2713aSLionel Sambuc.. _advanced:
2187*f4a2713aSLionel Sambuc
2188*f4a2713aSLionel SambucAdvanced Topics
2189*f4a2713aSLionel Sambuc===============
2190*f4a2713aSLionel Sambuc
2191*f4a2713aSLionel SambucThis section describes some of the advanced or obscure API's that most clients
2192*f4a2713aSLionel Sambucdo not need to be aware of.  These API's tend manage the inner workings of the
2193*f4a2713aSLionel SambucLLVM system, and only need to be accessed in unusual circumstances.
2194*f4a2713aSLionel Sambuc
2195*f4a2713aSLionel Sambuc.. _SymbolTable:
2196*f4a2713aSLionel Sambuc
2197*f4a2713aSLionel SambucThe ``ValueSymbolTable`` class
2198*f4a2713aSLionel Sambuc------------------------------
2199*f4a2713aSLionel Sambuc
2200*f4a2713aSLionel SambucThe ``ValueSymbolTable`` (`doxygen
2201*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/classllvm_1_1ValueSymbolTable.html>`__) class provides
2202*f4a2713aSLionel Sambuca symbol table that the :ref:`Function <c_Function>` and Module_ classes use for
2203*f4a2713aSLionel Sambucnaming value definitions.  The symbol table can provide a name for any Value_.
2204*f4a2713aSLionel Sambuc
2205*f4a2713aSLionel SambucNote that the ``SymbolTable`` class should not be directly accessed by most
2206*f4a2713aSLionel Sambucclients.  It should only be used when iteration over the symbol table names
2207*f4a2713aSLionel Sambucthemselves are required, which is very special purpose.  Note that not all LLVM
2208*f4a2713aSLionel SambucValue_\ s have names, and those without names (i.e. they have an empty name) do
2209*f4a2713aSLionel Sambucnot exist in the symbol table.
2210*f4a2713aSLionel Sambuc
2211*f4a2713aSLionel SambucSymbol tables support iteration over the values in the symbol table with
2212*f4a2713aSLionel Sambuc``begin/end/iterator`` and supports querying to see if a specific name is in the
2213*f4a2713aSLionel Sambucsymbol table (with ``lookup``).  The ``ValueSymbolTable`` class exposes no
2214*f4a2713aSLionel Sambucpublic mutator methods, instead, simply call ``setName`` on a value, which will
2215*f4a2713aSLionel Sambucautoinsert it into the appropriate symbol table.
2216*f4a2713aSLionel Sambuc
2217*f4a2713aSLionel Sambuc.. _UserLayout:
2218*f4a2713aSLionel Sambuc
2219*f4a2713aSLionel SambucThe ``User`` and owned ``Use`` classes' memory layout
2220*f4a2713aSLionel Sambuc-----------------------------------------------------
2221*f4a2713aSLionel Sambuc
2222*f4a2713aSLionel SambucThe ``User`` (`doxygen <http://llvm.org/doxygen/classllvm_1_1User.html>`__)
2223*f4a2713aSLionel Sambucclass provides a basis for expressing the ownership of ``User`` towards other
2224*f4a2713aSLionel Sambuc`Value instance <http://llvm.org/doxygen/classllvm_1_1Value.html>`_\ s.  The
2225*f4a2713aSLionel Sambuc``Use`` (`doxygen <http://llvm.org/doxygen/classllvm_1_1Use.html>`__) helper
2226*f4a2713aSLionel Sambucclass is employed to do the bookkeeping and to facilitate *O(1)* addition and
2227*f4a2713aSLionel Sambucremoval.
2228*f4a2713aSLionel Sambuc
2229*f4a2713aSLionel Sambuc.. _Use2User:
2230*f4a2713aSLionel Sambuc
2231*f4a2713aSLionel SambucInteraction and relationship between ``User`` and ``Use`` objects
2232*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2233*f4a2713aSLionel Sambuc
2234*f4a2713aSLionel SambucA subclass of ``User`` can choose between incorporating its ``Use`` objects or
2235*f4a2713aSLionel Sambucrefer to them out-of-line by means of a pointer.  A mixed variant (some ``Use``
2236*f4a2713aSLionel Sambucs inline others hung off) is impractical and breaks the invariant that the
2237*f4a2713aSLionel Sambuc``Use`` objects belonging to the same ``User`` form a contiguous array.
2238*f4a2713aSLionel Sambuc
2239*f4a2713aSLionel SambucWe have 2 different layouts in the ``User`` (sub)classes:
2240*f4a2713aSLionel Sambuc
2241*f4a2713aSLionel Sambuc* Layout a)
2242*f4a2713aSLionel Sambuc
2243*f4a2713aSLionel Sambuc  The ``Use`` object(s) are inside (resp. at fixed offset) of the ``User``
2244*f4a2713aSLionel Sambuc  object and there are a fixed number of them.
2245*f4a2713aSLionel Sambuc
2246*f4a2713aSLionel Sambuc* Layout b)
2247*f4a2713aSLionel Sambuc
2248*f4a2713aSLionel Sambuc  The ``Use`` object(s) are referenced by a pointer to an array from the
2249*f4a2713aSLionel Sambuc  ``User`` object and there may be a variable number of them.
2250*f4a2713aSLionel Sambuc
2251*f4a2713aSLionel SambucAs of v2.4 each layout still possesses a direct pointer to the start of the
2252*f4a2713aSLionel Sambucarray of ``Use``\ s.  Though not mandatory for layout a), we stick to this
2253*f4a2713aSLionel Sambucredundancy for the sake of simplicity.  The ``User`` object also stores the
2254*f4a2713aSLionel Sambucnumber of ``Use`` objects it has. (Theoretically this information can also be
2255*f4a2713aSLionel Sambuccalculated given the scheme presented below.)
2256*f4a2713aSLionel Sambuc
2257*f4a2713aSLionel SambucSpecial forms of allocation operators (``operator new``) enforce the following
2258*f4a2713aSLionel Sambucmemory layouts:
2259*f4a2713aSLionel Sambuc
2260*f4a2713aSLionel Sambuc* Layout a) is modelled by prepending the ``User`` object by the ``Use[]``
2261*f4a2713aSLionel Sambuc  array.
2262*f4a2713aSLionel Sambuc
2263*f4a2713aSLionel Sambuc  .. code-block:: none
2264*f4a2713aSLionel Sambuc
2265*f4a2713aSLionel Sambuc    ...---.---.---.---.-------...
2266*f4a2713aSLionel Sambuc      | P | P | P | P | User
2267*f4a2713aSLionel Sambuc    '''---'---'---'---'-------'''
2268*f4a2713aSLionel Sambuc
2269*f4a2713aSLionel Sambuc* Layout b) is modelled by pointing at the ``Use[]`` array.
2270*f4a2713aSLionel Sambuc
2271*f4a2713aSLionel Sambuc  .. code-block:: none
2272*f4a2713aSLionel Sambuc
2273*f4a2713aSLionel Sambuc    .-------...
2274*f4a2713aSLionel Sambuc    | User
2275*f4a2713aSLionel Sambuc    '-------'''
2276*f4a2713aSLionel Sambuc        |
2277*f4a2713aSLionel Sambuc        v
2278*f4a2713aSLionel Sambuc        .---.---.---.---...
2279*f4a2713aSLionel Sambuc        | P | P | P | P |
2280*f4a2713aSLionel Sambuc        '---'---'---'---'''
2281*f4a2713aSLionel Sambuc
2282*f4a2713aSLionel Sambuc*(In the above figures* '``P``' *stands for the* ``Use**`` *that is stored in
2283*f4a2713aSLionel Sambuceach* ``Use`` *object in the member* ``Use::Prev`` *)*
2284*f4a2713aSLionel Sambuc
2285*f4a2713aSLionel Sambuc.. _Waymarking:
2286*f4a2713aSLionel Sambuc
2287*f4a2713aSLionel SambucThe waymarking algorithm
2288*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^
2289*f4a2713aSLionel Sambuc
2290*f4a2713aSLionel SambucSince the ``Use`` objects are deprived of the direct (back)pointer to their
2291*f4a2713aSLionel Sambuc``User`` objects, there must be a fast and exact method to recover it.  This is
2292*f4a2713aSLionel Sambucaccomplished by the following scheme:
2293*f4a2713aSLionel Sambuc
2294*f4a2713aSLionel SambucA bit-encoding in the 2 LSBits (least significant bits) of the ``Use::Prev``
2295*f4a2713aSLionel Sambucallows to find the start of the ``User`` object:
2296*f4a2713aSLionel Sambuc
2297*f4a2713aSLionel Sambuc* ``00`` --- binary digit 0
2298*f4a2713aSLionel Sambuc
2299*f4a2713aSLionel Sambuc* ``01`` --- binary digit 1
2300*f4a2713aSLionel Sambuc
2301*f4a2713aSLionel Sambuc* ``10`` --- stop and calculate (``s``)
2302*f4a2713aSLionel Sambuc
2303*f4a2713aSLionel Sambuc* ``11`` --- full stop (``S``)
2304*f4a2713aSLionel Sambuc
2305*f4a2713aSLionel SambucGiven a ``Use*``, all we have to do is to walk till we get a stop and we either
2306*f4a2713aSLionel Sambuchave a ``User`` immediately behind or we have to walk to the next stop picking
2307*f4a2713aSLionel Sambucup digits and calculating the offset:
2308*f4a2713aSLionel Sambuc
2309*f4a2713aSLionel Sambuc.. code-block:: none
2310*f4a2713aSLionel Sambuc
2311*f4a2713aSLionel Sambuc  .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.----------------
2312*f4a2713aSLionel Sambuc  | 1 | s | 1 | 0 | 1 | 0 | s | 1 | 1 | 0 | s | 1 | 1 | s | 1 | S | User (or User*)
2313*f4a2713aSLionel Sambuc  '---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'----------------
2314*f4a2713aSLionel Sambuc      |+15                |+10            |+6         |+3     |+1
2315*f4a2713aSLionel Sambuc      |                   |               |           |       | __>
2316*f4a2713aSLionel Sambuc      |                   |               |           | __________>
2317*f4a2713aSLionel Sambuc      |                   |               | ______________________>
2318*f4a2713aSLionel Sambuc      |                   | ______________________________________>
2319*f4a2713aSLionel Sambuc      | __________________________________________________________>
2320*f4a2713aSLionel Sambuc
2321*f4a2713aSLionel SambucOnly the significant number of bits need to be stored between the stops, so that
2322*f4a2713aSLionel Sambucthe *worst case is 20 memory accesses* when there are 1000 ``Use`` objects
2323*f4a2713aSLionel Sambucassociated with a ``User``.
2324*f4a2713aSLionel Sambuc
2325*f4a2713aSLionel Sambuc.. _ReferenceImpl:
2326*f4a2713aSLionel Sambuc
2327*f4a2713aSLionel SambucReference implementation
2328*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^
2329*f4a2713aSLionel Sambuc
2330*f4a2713aSLionel SambucThe following literate Haskell fragment demonstrates the concept:
2331*f4a2713aSLionel Sambuc
2332*f4a2713aSLionel Sambuc.. code-block:: haskell
2333*f4a2713aSLionel Sambuc
2334*f4a2713aSLionel Sambuc  > import Test.QuickCheck
2335*f4a2713aSLionel Sambuc  >
2336*f4a2713aSLionel Sambuc  > digits :: Int -> [Char] -> [Char]
2337*f4a2713aSLionel Sambuc  > digits 0 acc = '0' : acc
2338*f4a2713aSLionel Sambuc  > digits 1 acc = '1' : acc
2339*f4a2713aSLionel Sambuc  > digits n acc = digits (n `div` 2) $ digits (n `mod` 2) acc
2340*f4a2713aSLionel Sambuc  >
2341*f4a2713aSLionel Sambuc  > dist :: Int -> [Char] -> [Char]
2342*f4a2713aSLionel Sambuc  > dist 0 [] = ['S']
2343*f4a2713aSLionel Sambuc  > dist 0 acc = acc
2344*f4a2713aSLionel Sambuc  > dist 1 acc = let r = dist 0 acc in 's' : digits (length r) r
2345*f4a2713aSLionel Sambuc  > dist n acc = dist (n - 1) $ dist 1 acc
2346*f4a2713aSLionel Sambuc  >
2347*f4a2713aSLionel Sambuc  > takeLast n ss = reverse $ take n $ reverse ss
2348*f4a2713aSLionel Sambuc  >
2349*f4a2713aSLionel Sambuc  > test = takeLast 40 $ dist 20 []
2350*f4a2713aSLionel Sambuc  >
2351*f4a2713aSLionel Sambuc
2352*f4a2713aSLionel SambucPrinting <test> gives: ``"1s100000s11010s10100s1111s1010s110s11s1S"``
2353*f4a2713aSLionel Sambuc
2354*f4a2713aSLionel SambucThe reverse algorithm computes the length of the string just by examining a
2355*f4a2713aSLionel Sambuccertain prefix:
2356*f4a2713aSLionel Sambuc
2357*f4a2713aSLionel Sambuc.. code-block:: haskell
2358*f4a2713aSLionel Sambuc
2359*f4a2713aSLionel Sambuc  > pref :: [Char] -> Int
2360*f4a2713aSLionel Sambuc  > pref "S" = 1
2361*f4a2713aSLionel Sambuc  > pref ('s':'1':rest) = decode 2 1 rest
2362*f4a2713aSLionel Sambuc  > pref (_:rest) = 1 + pref rest
2363*f4a2713aSLionel Sambuc  >
2364*f4a2713aSLionel Sambuc  > decode walk acc ('0':rest) = decode (walk + 1) (acc * 2) rest
2365*f4a2713aSLionel Sambuc  > decode walk acc ('1':rest) = decode (walk + 1) (acc * 2 + 1) rest
2366*f4a2713aSLionel Sambuc  > decode walk acc _ = walk + acc
2367*f4a2713aSLionel Sambuc  >
2368*f4a2713aSLionel Sambuc
2369*f4a2713aSLionel SambucNow, as expected, printing <pref test> gives ``40``.
2370*f4a2713aSLionel Sambuc
2371*f4a2713aSLionel SambucWe can *quickCheck* this with following property:
2372*f4a2713aSLionel Sambuc
2373*f4a2713aSLionel Sambuc.. code-block:: haskell
2374*f4a2713aSLionel Sambuc
2375*f4a2713aSLionel Sambuc  > testcase = dist 2000 []
2376*f4a2713aSLionel Sambuc  > testcaseLength = length testcase
2377*f4a2713aSLionel Sambuc  >
2378*f4a2713aSLionel Sambuc  > identityProp n = n > 0 && n <= testcaseLength ==> length arr == pref arr
2379*f4a2713aSLionel Sambuc  >     where arr = takeLast n testcase
2380*f4a2713aSLionel Sambuc  >
2381*f4a2713aSLionel Sambuc
2382*f4a2713aSLionel SambucAs expected <quickCheck identityProp> gives:
2383*f4a2713aSLionel Sambuc
2384*f4a2713aSLionel Sambuc::
2385*f4a2713aSLionel Sambuc
2386*f4a2713aSLionel Sambuc  *Main> quickCheck identityProp
2387*f4a2713aSLionel Sambuc  OK, passed 100 tests.
2388*f4a2713aSLionel Sambuc
2389*f4a2713aSLionel SambucLet's be a bit more exhaustive:
2390*f4a2713aSLionel Sambuc
2391*f4a2713aSLionel Sambuc.. code-block:: haskell
2392*f4a2713aSLionel Sambuc
2393*f4a2713aSLionel Sambuc  >
2394*f4a2713aSLionel Sambuc  > deepCheck p = check (defaultConfig { configMaxTest = 500 }) p
2395*f4a2713aSLionel Sambuc  >
2396*f4a2713aSLionel Sambuc
2397*f4a2713aSLionel SambucAnd here is the result of <deepCheck identityProp>:
2398*f4a2713aSLionel Sambuc
2399*f4a2713aSLionel Sambuc::
2400*f4a2713aSLionel Sambuc
2401*f4a2713aSLionel Sambuc  *Main> deepCheck identityProp
2402*f4a2713aSLionel Sambuc  OK, passed 500 tests.
2403*f4a2713aSLionel Sambuc
2404*f4a2713aSLionel Sambuc.. _Tagging:
2405*f4a2713aSLionel Sambuc
2406*f4a2713aSLionel SambucTagging considerations
2407*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^
2408*f4a2713aSLionel Sambuc
2409*f4a2713aSLionel SambucTo maintain the invariant that the 2 LSBits of each ``Use**`` in ``Use`` never
2410*f4a2713aSLionel Sambucchange after being set up, setters of ``Use::Prev`` must re-tag the new
2411*f4a2713aSLionel Sambuc``Use**`` on every modification.  Accordingly getters must strip the tag bits.
2412*f4a2713aSLionel Sambuc
2413*f4a2713aSLionel SambucFor layout b) instead of the ``User`` we find a pointer (``User*`` with LSBit
2414*f4a2713aSLionel Sambucset).  Following this pointer brings us to the ``User``.  A portable trick
2415*f4a2713aSLionel Sambucensures that the first bytes of ``User`` (if interpreted as a pointer) never has
2416*f4a2713aSLionel Sambucthe LSBit set. (Portability is relying on the fact that all known compilers
2417*f4a2713aSLionel Sambucplace the ``vptr`` in the first word of the instances.)
2418*f4a2713aSLionel Sambuc
2419*f4a2713aSLionel Sambuc.. _coreclasses:
2420*f4a2713aSLionel Sambuc
2421*f4a2713aSLionel SambucThe Core LLVM Class Hierarchy Reference
2422*f4a2713aSLionel Sambuc=======================================
2423*f4a2713aSLionel Sambuc
2424*f4a2713aSLionel Sambuc``#include "llvm/IR/Type.h"``
2425*f4a2713aSLionel Sambuc
2426*f4a2713aSLionel Sambucheader source: `Type.h <http://llvm.org/doxygen/Type_8h-source.html>`_
2427*f4a2713aSLionel Sambuc
2428*f4a2713aSLionel Sambucdoxygen info: `Type Clases <http://llvm.org/doxygen/classllvm_1_1Type.html>`_
2429*f4a2713aSLionel Sambuc
2430*f4a2713aSLionel SambucThe Core LLVM classes are the primary means of representing the program being
2431*f4a2713aSLionel Sambucinspected or transformed.  The core LLVM classes are defined in header files in
2432*f4a2713aSLionel Sambucthe ``include/llvm/`` directory, and implemented in the ``lib/VMCore``
2433*f4a2713aSLionel Sambucdirectory.
2434*f4a2713aSLionel Sambuc
2435*f4a2713aSLionel Sambuc.. _Type:
2436*f4a2713aSLionel Sambuc
2437*f4a2713aSLionel SambucThe Type class and Derived Types
2438*f4a2713aSLionel Sambuc--------------------------------
2439*f4a2713aSLionel Sambuc
2440*f4a2713aSLionel Sambuc``Type`` is a superclass of all type classes.  Every ``Value`` has a ``Type``.
2441*f4a2713aSLionel Sambuc``Type`` cannot be instantiated directly but only through its subclasses.
2442*f4a2713aSLionel SambucCertain primitive types (``VoidType``, ``LabelType``, ``FloatType`` and
2443*f4a2713aSLionel Sambuc``DoubleType``) have hidden subclasses.  They are hidden because they offer no
2444*f4a2713aSLionel Sambucuseful functionality beyond what the ``Type`` class offers except to distinguish
2445*f4a2713aSLionel Sambucthemselves from other subclasses of ``Type``.
2446*f4a2713aSLionel Sambuc
2447*f4a2713aSLionel SambucAll other types are subclasses of ``DerivedType``.  Types can be named, but this
2448*f4a2713aSLionel Sambucis not a requirement.  There exists exactly one instance of a given shape at any
2449*f4a2713aSLionel Sambucone time.  This allows type equality to be performed with address equality of
2450*f4a2713aSLionel Sambucthe Type Instance.  That is, given two ``Type*`` values, the types are identical
2451*f4a2713aSLionel Sambucif the pointers are identical.
2452*f4a2713aSLionel Sambuc
2453*f4a2713aSLionel Sambuc.. _m_Type:
2454*f4a2713aSLionel Sambuc
2455*f4a2713aSLionel SambucImportant Public Methods
2456*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^
2457*f4a2713aSLionel Sambuc
2458*f4a2713aSLionel Sambuc* ``bool isIntegerTy() const``: Returns true for any integer type.
2459*f4a2713aSLionel Sambuc
2460*f4a2713aSLionel Sambuc* ``bool isFloatingPointTy()``: Return true if this is one of the five
2461*f4a2713aSLionel Sambuc  floating point types.
2462*f4a2713aSLionel Sambuc
2463*f4a2713aSLionel Sambuc* ``bool isSized()``: Return true if the type has known size.  Things
2464*f4a2713aSLionel Sambuc  that don't have a size are abstract types, labels and void.
2465*f4a2713aSLionel Sambuc
2466*f4a2713aSLionel Sambuc.. _derivedtypes:
2467*f4a2713aSLionel Sambuc
2468*f4a2713aSLionel SambucImportant Derived Types
2469*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^
2470*f4a2713aSLionel Sambuc
2471*f4a2713aSLionel Sambuc``IntegerType``
2472*f4a2713aSLionel Sambuc  Subclass of DerivedType that represents integer types of any bit width.  Any
2473*f4a2713aSLionel Sambuc  bit width between ``IntegerType::MIN_INT_BITS`` (1) and
2474*f4a2713aSLionel Sambuc  ``IntegerType::MAX_INT_BITS`` (~8 million) can be represented.
2475*f4a2713aSLionel Sambuc
2476*f4a2713aSLionel Sambuc  * ``static const IntegerType* get(unsigned NumBits)``: get an integer
2477*f4a2713aSLionel Sambuc    type of a specific bit width.
2478*f4a2713aSLionel Sambuc
2479*f4a2713aSLionel Sambuc  * ``unsigned getBitWidth() const``: Get the bit width of an integer type.
2480*f4a2713aSLionel Sambuc
2481*f4a2713aSLionel Sambuc``SequentialType``
2482*f4a2713aSLionel Sambuc  This is subclassed by ArrayType, PointerType and VectorType.
2483*f4a2713aSLionel Sambuc
2484*f4a2713aSLionel Sambuc  * ``const Type * getElementType() const``: Returns the type of each
2485*f4a2713aSLionel Sambuc    of the elements in the sequential type.
2486*f4a2713aSLionel Sambuc
2487*f4a2713aSLionel Sambuc``ArrayType``
2488*f4a2713aSLionel Sambuc  This is a subclass of SequentialType and defines the interface for array
2489*f4a2713aSLionel Sambuc  types.
2490*f4a2713aSLionel Sambuc
2491*f4a2713aSLionel Sambuc  * ``unsigned getNumElements() const``: Returns the number of elements
2492*f4a2713aSLionel Sambuc    in the array.
2493*f4a2713aSLionel Sambuc
2494*f4a2713aSLionel Sambuc``PointerType``
2495*f4a2713aSLionel Sambuc  Subclass of SequentialType for pointer types.
2496*f4a2713aSLionel Sambuc
2497*f4a2713aSLionel Sambuc``VectorType``
2498*f4a2713aSLionel Sambuc  Subclass of SequentialType for vector types.  A vector type is similar to an
2499*f4a2713aSLionel Sambuc  ArrayType but is distinguished because it is a first class type whereas
2500*f4a2713aSLionel Sambuc  ArrayType is not.  Vector types are used for vector operations and are usually
2501*f4a2713aSLionel Sambuc  small vectors of of an integer or floating point type.
2502*f4a2713aSLionel Sambuc
2503*f4a2713aSLionel Sambuc``StructType``
2504*f4a2713aSLionel Sambuc  Subclass of DerivedTypes for struct types.
2505*f4a2713aSLionel Sambuc
2506*f4a2713aSLionel Sambuc.. _FunctionType:
2507*f4a2713aSLionel Sambuc
2508*f4a2713aSLionel Sambuc``FunctionType``
2509*f4a2713aSLionel Sambuc  Subclass of DerivedTypes for function types.
2510*f4a2713aSLionel Sambuc
2511*f4a2713aSLionel Sambuc  * ``bool isVarArg() const``: Returns true if it's a vararg function.
2512*f4a2713aSLionel Sambuc
2513*f4a2713aSLionel Sambuc  * ``const Type * getReturnType() const``: Returns the return type of the
2514*f4a2713aSLionel Sambuc    function.
2515*f4a2713aSLionel Sambuc
2516*f4a2713aSLionel Sambuc  * ``const Type * getParamType (unsigned i)``: Returns the type of the ith
2517*f4a2713aSLionel Sambuc    parameter.
2518*f4a2713aSLionel Sambuc
2519*f4a2713aSLionel Sambuc  * ``const unsigned getNumParams() const``: Returns the number of formal
2520*f4a2713aSLionel Sambuc    parameters.
2521*f4a2713aSLionel Sambuc
2522*f4a2713aSLionel Sambuc.. _Module:
2523*f4a2713aSLionel Sambuc
2524*f4a2713aSLionel SambucThe ``Module`` class
2525*f4a2713aSLionel Sambuc--------------------
2526*f4a2713aSLionel Sambuc
2527*f4a2713aSLionel Sambuc``#include "llvm/IR/Module.h"``
2528*f4a2713aSLionel Sambuc
2529*f4a2713aSLionel Sambucheader source: `Module.h <http://llvm.org/doxygen/Module_8h-source.html>`_
2530*f4a2713aSLionel Sambuc
2531*f4a2713aSLionel Sambucdoxygen info: `Module Class <http://llvm.org/doxygen/classllvm_1_1Module.html>`_
2532*f4a2713aSLionel Sambuc
2533*f4a2713aSLionel SambucThe ``Module`` class represents the top level structure present in LLVM
2534*f4a2713aSLionel Sambucprograms.  An LLVM module is effectively either a translation unit of the
2535*f4a2713aSLionel Sambucoriginal program or a combination of several translation units merged by the
2536*f4a2713aSLionel Sambuclinker.  The ``Module`` class keeps track of a list of :ref:`Function
2537*f4a2713aSLionel Sambuc<c_Function>`\ s, a list of GlobalVariable_\ s, and a SymbolTable_.
2538*f4a2713aSLionel SambucAdditionally, it contains a few helpful member functions that try to make common
2539*f4a2713aSLionel Sambucoperations easy.
2540*f4a2713aSLionel Sambuc
2541*f4a2713aSLionel Sambuc.. _m_Module:
2542*f4a2713aSLionel Sambuc
2543*f4a2713aSLionel SambucImportant Public Members of the ``Module`` class
2544*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2545*f4a2713aSLionel Sambuc
2546*f4a2713aSLionel Sambuc* ``Module::Module(std::string name = "")``
2547*f4a2713aSLionel Sambuc
2548*f4a2713aSLionel Sambuc  Constructing a Module_ is easy.  You can optionally provide a name for it
2549*f4a2713aSLionel Sambuc  (probably based on the name of the translation unit).
2550*f4a2713aSLionel Sambuc
2551*f4a2713aSLionel Sambuc* | ``Module::iterator`` - Typedef for function list iterator
2552*f4a2713aSLionel Sambuc  | ``Module::const_iterator`` - Typedef for const_iterator.
2553*f4a2713aSLionel Sambuc  | ``begin()``, ``end()``, ``size()``, ``empty()``
2554*f4a2713aSLionel Sambuc
2555*f4a2713aSLionel Sambuc  These are forwarding methods that make it easy to access the contents of a
2556*f4a2713aSLionel Sambuc  ``Module`` object's :ref:`Function <c_Function>` list.
2557*f4a2713aSLionel Sambuc
2558*f4a2713aSLionel Sambuc* ``Module::FunctionListType &getFunctionList()``
2559*f4a2713aSLionel Sambuc
2560*f4a2713aSLionel Sambuc  Returns the list of :ref:`Function <c_Function>`\ s.  This is necessary to use
2561*f4a2713aSLionel Sambuc  when you need to update the list or perform a complex action that doesn't have
2562*f4a2713aSLionel Sambuc  a forwarding method.
2563*f4a2713aSLionel Sambuc
2564*f4a2713aSLionel Sambuc----------------
2565*f4a2713aSLionel Sambuc
2566*f4a2713aSLionel Sambuc* | ``Module::global_iterator`` - Typedef for global variable list iterator
2567*f4a2713aSLionel Sambuc  | ``Module::const_global_iterator`` - Typedef for const_iterator.
2568*f4a2713aSLionel Sambuc  | ``global_begin()``, ``global_end()``, ``global_size()``, ``global_empty()``
2569*f4a2713aSLionel Sambuc
2570*f4a2713aSLionel Sambuc  These are forwarding methods that make it easy to access the contents of a
2571*f4a2713aSLionel Sambuc  ``Module`` object's GlobalVariable_ list.
2572*f4a2713aSLionel Sambuc
2573*f4a2713aSLionel Sambuc* ``Module::GlobalListType &getGlobalList()``
2574*f4a2713aSLionel Sambuc
2575*f4a2713aSLionel Sambuc  Returns the list of GlobalVariable_\ s.  This is necessary to use when you
2576*f4a2713aSLionel Sambuc  need to update the list or perform a complex action that doesn't have a
2577*f4a2713aSLionel Sambuc  forwarding method.
2578*f4a2713aSLionel Sambuc
2579*f4a2713aSLionel Sambuc----------------
2580*f4a2713aSLionel Sambuc
2581*f4a2713aSLionel Sambuc* ``SymbolTable *getSymbolTable()``
2582*f4a2713aSLionel Sambuc
2583*f4a2713aSLionel Sambuc  Return a reference to the SymbolTable_ for this ``Module``.
2584*f4a2713aSLionel Sambuc
2585*f4a2713aSLionel Sambuc----------------
2586*f4a2713aSLionel Sambuc
2587*f4a2713aSLionel Sambuc* ``Function *getFunction(StringRef Name) const``
2588*f4a2713aSLionel Sambuc
2589*f4a2713aSLionel Sambuc  Look up the specified function in the ``Module`` SymbolTable_.  If it does not
2590*f4a2713aSLionel Sambuc  exist, return ``null``.
2591*f4a2713aSLionel Sambuc
2592*f4a2713aSLionel Sambuc* ``Function *getOrInsertFunction(const std::string &Name, const FunctionType
2593*f4a2713aSLionel Sambuc  *T)``
2594*f4a2713aSLionel Sambuc
2595*f4a2713aSLionel Sambuc  Look up the specified function in the ``Module`` SymbolTable_.  If it does not
2596*f4a2713aSLionel Sambuc  exist, add an external declaration for the function and return it.
2597*f4a2713aSLionel Sambuc
2598*f4a2713aSLionel Sambuc* ``std::string getTypeName(const Type *Ty)``
2599*f4a2713aSLionel Sambuc
2600*f4a2713aSLionel Sambuc  If there is at least one entry in the SymbolTable_ for the specified Type_,
2601*f4a2713aSLionel Sambuc  return it.  Otherwise return the empty string.
2602*f4a2713aSLionel Sambuc
2603*f4a2713aSLionel Sambuc* ``bool addTypeName(const std::string &Name, const Type *Ty)``
2604*f4a2713aSLionel Sambuc
2605*f4a2713aSLionel Sambuc  Insert an entry in the SymbolTable_ mapping ``Name`` to ``Ty``.  If there is
2606*f4a2713aSLionel Sambuc  already an entry for this name, true is returned and the SymbolTable_ is not
2607*f4a2713aSLionel Sambuc  modified.
2608*f4a2713aSLionel Sambuc
2609*f4a2713aSLionel Sambuc.. _Value:
2610*f4a2713aSLionel Sambuc
2611*f4a2713aSLionel SambucThe ``Value`` class
2612*f4a2713aSLionel Sambuc-------------------
2613*f4a2713aSLionel Sambuc
2614*f4a2713aSLionel Sambuc``#include "llvm/IR/Value.h"``
2615*f4a2713aSLionel Sambuc
2616*f4a2713aSLionel Sambucheader source: `Value.h <http://llvm.org/doxygen/Value_8h-source.html>`_
2617*f4a2713aSLionel Sambuc
2618*f4a2713aSLionel Sambucdoxygen info: `Value Class <http://llvm.org/doxygen/classllvm_1_1Value.html>`_
2619*f4a2713aSLionel Sambuc
2620*f4a2713aSLionel SambucThe ``Value`` class is the most important class in the LLVM Source base.  It
2621*f4a2713aSLionel Sambucrepresents a typed value that may be used (among other things) as an operand to
2622*f4a2713aSLionel Sambucan instruction.  There are many different types of ``Value``\ s, such as
2623*f4a2713aSLionel SambucConstant_\ s, Argument_\ s.  Even Instruction_\ s and :ref:`Function
2624*f4a2713aSLionel Sambuc<c_Function>`\ s are ``Value``\ s.
2625*f4a2713aSLionel Sambuc
2626*f4a2713aSLionel SambucA particular ``Value`` may be used many times in the LLVM representation for a
2627*f4a2713aSLionel Sambucprogram.  For example, an incoming argument to a function (represented with an
2628*f4a2713aSLionel Sambucinstance of the Argument_ class) is "used" by every instruction in the function
2629*f4a2713aSLionel Sambucthat references the argument.  To keep track of this relationship, the ``Value``
2630*f4a2713aSLionel Sambucclass keeps a list of all of the ``User``\ s that is using it (the User_ class
2631*f4a2713aSLionel Sambucis a base class for all nodes in the LLVM graph that can refer to ``Value``\ s).
2632*f4a2713aSLionel SambucThis use list is how LLVM represents def-use information in the program, and is
2633*f4a2713aSLionel Sambucaccessible through the ``use_*`` methods, shown below.
2634*f4a2713aSLionel Sambuc
2635*f4a2713aSLionel SambucBecause LLVM is a typed representation, every LLVM ``Value`` is typed, and this
2636*f4a2713aSLionel SambucType_ is available through the ``getType()`` method.  In addition, all LLVM
2637*f4a2713aSLionel Sambucvalues can be named.  The "name" of the ``Value`` is a symbolic string printed
2638*f4a2713aSLionel Sambucin the LLVM code:
2639*f4a2713aSLionel Sambuc
2640*f4a2713aSLionel Sambuc.. code-block:: llvm
2641*f4a2713aSLionel Sambuc
2642*f4a2713aSLionel Sambuc  %foo = add i32 1, 2
2643*f4a2713aSLionel Sambuc
2644*f4a2713aSLionel Sambuc.. _nameWarning:
2645*f4a2713aSLionel Sambuc
2646*f4a2713aSLionel SambucThe name of this instruction is "foo". **NOTE** that the name of any value may
2647*f4a2713aSLionel Sambucbe missing (an empty string), so names should **ONLY** be used for debugging
2648*f4a2713aSLionel Sambuc(making the source code easier to read, debugging printouts), they should not be
2649*f4a2713aSLionel Sambucused to keep track of values or map between them.  For this purpose, use a
2650*f4a2713aSLionel Sambuc``std::map`` of pointers to the ``Value`` itself instead.
2651*f4a2713aSLionel Sambuc
2652*f4a2713aSLionel SambucOne important aspect of LLVM is that there is no distinction between an SSA
2653*f4a2713aSLionel Sambucvariable and the operation that produces it.  Because of this, any reference to
2654*f4a2713aSLionel Sambucthe value produced by an instruction (or the value available as an incoming
2655*f4a2713aSLionel Sambucargument, for example) is represented as a direct pointer to the instance of the
2656*f4a2713aSLionel Sambucclass that represents this value.  Although this may take some getting used to,
2657*f4a2713aSLionel Sambucit simplifies the representation and makes it easier to manipulate.
2658*f4a2713aSLionel Sambuc
2659*f4a2713aSLionel Sambuc.. _m_Value:
2660*f4a2713aSLionel Sambuc
2661*f4a2713aSLionel SambucImportant Public Members of the ``Value`` class
2662*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2663*f4a2713aSLionel Sambuc
2664*f4a2713aSLionel Sambuc* | ``Value::use_iterator`` - Typedef for iterator over the use-list
2665*f4a2713aSLionel Sambuc  | ``Value::const_use_iterator`` - Typedef for const_iterator over the
2666*f4a2713aSLionel Sambuc    use-list
2667*f4a2713aSLionel Sambuc  | ``unsigned use_size()`` - Returns the number of users of the value.
2668*f4a2713aSLionel Sambuc  | ``bool use_empty()`` - Returns true if there are no users.
2669*f4a2713aSLionel Sambuc  | ``use_iterator use_begin()`` - Get an iterator to the start of the
2670*f4a2713aSLionel Sambuc    use-list.
2671*f4a2713aSLionel Sambuc  | ``use_iterator use_end()`` - Get an iterator to the end of the use-list.
2672*f4a2713aSLionel Sambuc  | ``User *use_back()`` - Returns the last element in the list.
2673*f4a2713aSLionel Sambuc
2674*f4a2713aSLionel Sambuc  These methods are the interface to access the def-use information in LLVM.
2675*f4a2713aSLionel Sambuc  As with all other iterators in LLVM, the naming conventions follow the
2676*f4a2713aSLionel Sambuc  conventions defined by the STL_.
2677*f4a2713aSLionel Sambuc
2678*f4a2713aSLionel Sambuc* ``Type *getType() const``
2679*f4a2713aSLionel Sambuc  This method returns the Type of the Value.
2680*f4a2713aSLionel Sambuc
2681*f4a2713aSLionel Sambuc* | ``bool hasName() const``
2682*f4a2713aSLionel Sambuc  | ``std::string getName() const``
2683*f4a2713aSLionel Sambuc  | ``void setName(const std::string &Name)``
2684*f4a2713aSLionel Sambuc
2685*f4a2713aSLionel Sambuc  This family of methods is used to access and assign a name to a ``Value``, be
2686*f4a2713aSLionel Sambuc  aware of the :ref:`precaution above <nameWarning>`.
2687*f4a2713aSLionel Sambuc
2688*f4a2713aSLionel Sambuc* ``void replaceAllUsesWith(Value *V)``
2689*f4a2713aSLionel Sambuc
2690*f4a2713aSLionel Sambuc  This method traverses the use list of a ``Value`` changing all User_\ s of the
2691*f4a2713aSLionel Sambuc  current value to refer to "``V``" instead.  For example, if you detect that an
2692*f4a2713aSLionel Sambuc  instruction always produces a constant value (for example through constant
2693*f4a2713aSLionel Sambuc  folding), you can replace all uses of the instruction with the constant like
2694*f4a2713aSLionel Sambuc  this:
2695*f4a2713aSLionel Sambuc
2696*f4a2713aSLionel Sambuc  .. code-block:: c++
2697*f4a2713aSLionel Sambuc
2698*f4a2713aSLionel Sambuc    Inst->replaceAllUsesWith(ConstVal);
2699*f4a2713aSLionel Sambuc
2700*f4a2713aSLionel Sambuc.. _User:
2701*f4a2713aSLionel Sambuc
2702*f4a2713aSLionel SambucThe ``User`` class
2703*f4a2713aSLionel Sambuc------------------
2704*f4a2713aSLionel Sambuc
2705*f4a2713aSLionel Sambuc``#include "llvm/IR/User.h"``
2706*f4a2713aSLionel Sambuc
2707*f4a2713aSLionel Sambucheader source: `User.h <http://llvm.org/doxygen/User_8h-source.html>`_
2708*f4a2713aSLionel Sambuc
2709*f4a2713aSLionel Sambucdoxygen info: `User Class <http://llvm.org/doxygen/classllvm_1_1User.html>`_
2710*f4a2713aSLionel Sambuc
2711*f4a2713aSLionel SambucSuperclass: Value_
2712*f4a2713aSLionel Sambuc
2713*f4a2713aSLionel SambucThe ``User`` class is the common base class of all LLVM nodes that may refer to
2714*f4a2713aSLionel Sambuc``Value``\ s.  It exposes a list of "Operands" that are all of the ``Value``\ s
2715*f4a2713aSLionel Sambucthat the User is referring to.  The ``User`` class itself is a subclass of
2716*f4a2713aSLionel Sambuc``Value``.
2717*f4a2713aSLionel Sambuc
2718*f4a2713aSLionel SambucThe operands of a ``User`` point directly to the LLVM ``Value`` that it refers
2719*f4a2713aSLionel Sambucto.  Because LLVM uses Static Single Assignment (SSA) form, there can only be
2720*f4a2713aSLionel Sambucone definition referred to, allowing this direct connection.  This connection
2721*f4a2713aSLionel Sambucprovides the use-def information in LLVM.
2722*f4a2713aSLionel Sambuc
2723*f4a2713aSLionel Sambuc.. _m_User:
2724*f4a2713aSLionel Sambuc
2725*f4a2713aSLionel SambucImportant Public Members of the ``User`` class
2726*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727*f4a2713aSLionel Sambuc
2728*f4a2713aSLionel SambucThe ``User`` class exposes the operand list in two ways: through an index access
2729*f4a2713aSLionel Sambucinterface and through an iterator based interface.
2730*f4a2713aSLionel Sambuc
2731*f4a2713aSLionel Sambuc* | ``Value *getOperand(unsigned i)``
2732*f4a2713aSLionel Sambuc  | ``unsigned getNumOperands()``
2733*f4a2713aSLionel Sambuc
2734*f4a2713aSLionel Sambuc  These two methods expose the operands of the ``User`` in a convenient form for
2735*f4a2713aSLionel Sambuc  direct access.
2736*f4a2713aSLionel Sambuc
2737*f4a2713aSLionel Sambuc* | ``User::op_iterator`` - Typedef for iterator over the operand list
2738*f4a2713aSLionel Sambuc  | ``op_iterator op_begin()`` - Get an iterator to the start of the operand
2739*f4a2713aSLionel Sambuc    list.
2740*f4a2713aSLionel Sambuc  | ``op_iterator op_end()`` - Get an iterator to the end of the operand list.
2741*f4a2713aSLionel Sambuc
2742*f4a2713aSLionel Sambuc  Together, these methods make up the iterator based interface to the operands
2743*f4a2713aSLionel Sambuc  of a ``User``.
2744*f4a2713aSLionel Sambuc
2745*f4a2713aSLionel Sambuc
2746*f4a2713aSLionel Sambuc.. _Instruction:
2747*f4a2713aSLionel Sambuc
2748*f4a2713aSLionel SambucThe ``Instruction`` class
2749*f4a2713aSLionel Sambuc-------------------------
2750*f4a2713aSLionel Sambuc
2751*f4a2713aSLionel Sambuc``#include "llvm/IR/Instruction.h"``
2752*f4a2713aSLionel Sambuc
2753*f4a2713aSLionel Sambucheader source: `Instruction.h
2754*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/Instruction_8h-source.html>`_
2755*f4a2713aSLionel Sambuc
2756*f4a2713aSLionel Sambucdoxygen info: `Instruction Class
2757*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/classllvm_1_1Instruction.html>`_
2758*f4a2713aSLionel Sambuc
2759*f4a2713aSLionel SambucSuperclasses: User_, Value_
2760*f4a2713aSLionel Sambuc
2761*f4a2713aSLionel SambucThe ``Instruction`` class is the common base class for all LLVM instructions.
2762*f4a2713aSLionel SambucIt provides only a few methods, but is a very commonly used class.  The primary
2763*f4a2713aSLionel Sambucdata tracked by the ``Instruction`` class itself is the opcode (instruction
2764*f4a2713aSLionel Sambuctype) and the parent BasicBlock_ the ``Instruction`` is embedded into.  To
2765*f4a2713aSLionel Sambucrepresent a specific type of instruction, one of many subclasses of
2766*f4a2713aSLionel Sambuc``Instruction`` are used.
2767*f4a2713aSLionel Sambuc
2768*f4a2713aSLionel SambucBecause the ``Instruction`` class subclasses the User_ class, its operands can
2769*f4a2713aSLionel Sambucbe accessed in the same way as for other ``User``\ s (with the
2770*f4a2713aSLionel Sambuc``getOperand()``/``getNumOperands()`` and ``op_begin()``/``op_end()`` methods).
2771*f4a2713aSLionel SambucAn important file for the ``Instruction`` class is the ``llvm/Instruction.def``
2772*f4a2713aSLionel Sambucfile.  This file contains some meta-data about the various different types of
2773*f4a2713aSLionel Sambucinstructions in LLVM.  It describes the enum values that are used as opcodes
2774*f4a2713aSLionel Sambuc(for example ``Instruction::Add`` and ``Instruction::ICmp``), as well as the
2775*f4a2713aSLionel Sambucconcrete sub-classes of ``Instruction`` that implement the instruction (for
2776*f4a2713aSLionel Sambucexample BinaryOperator_ and CmpInst_).  Unfortunately, the use of macros in this
2777*f4a2713aSLionel Sambucfile confuses doxygen, so these enum values don't show up correctly in the
2778*f4a2713aSLionel Sambuc`doxygen output <http://llvm.org/doxygen/classllvm_1_1Instruction.html>`_.
2779*f4a2713aSLionel Sambuc
2780*f4a2713aSLionel Sambuc.. _s_Instruction:
2781*f4a2713aSLionel Sambuc
2782*f4a2713aSLionel SambucImportant Subclasses of the ``Instruction`` class
2783*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2784*f4a2713aSLionel Sambuc
2785*f4a2713aSLionel Sambuc.. _BinaryOperator:
2786*f4a2713aSLionel Sambuc
2787*f4a2713aSLionel Sambuc* ``BinaryOperator``
2788*f4a2713aSLionel Sambuc
2789*f4a2713aSLionel Sambuc  This subclasses represents all two operand instructions whose operands must be
2790*f4a2713aSLionel Sambuc  the same type, except for the comparison instructions.
2791*f4a2713aSLionel Sambuc
2792*f4a2713aSLionel Sambuc.. _CastInst:
2793*f4a2713aSLionel Sambuc
2794*f4a2713aSLionel Sambuc* ``CastInst``
2795*f4a2713aSLionel Sambuc  This subclass is the parent of the 12 casting instructions.  It provides
2796*f4a2713aSLionel Sambuc  common operations on cast instructions.
2797*f4a2713aSLionel Sambuc
2798*f4a2713aSLionel Sambuc.. _CmpInst:
2799*f4a2713aSLionel Sambuc
2800*f4a2713aSLionel Sambuc* ``CmpInst``
2801*f4a2713aSLionel Sambuc
2802*f4a2713aSLionel Sambuc  This subclass respresents the two comparison instructions,
2803*f4a2713aSLionel Sambuc  `ICmpInst <LangRef.html#i_icmp>`_ (integer opreands), and
2804*f4a2713aSLionel Sambuc  `FCmpInst <LangRef.html#i_fcmp>`_ (floating point operands).
2805*f4a2713aSLionel Sambuc
2806*f4a2713aSLionel Sambuc.. _TerminatorInst:
2807*f4a2713aSLionel Sambuc
2808*f4a2713aSLionel Sambuc* ``TerminatorInst``
2809*f4a2713aSLionel Sambuc
2810*f4a2713aSLionel Sambuc  This subclass is the parent of all terminator instructions (those which can
2811*f4a2713aSLionel Sambuc  terminate a block).
2812*f4a2713aSLionel Sambuc
2813*f4a2713aSLionel Sambuc.. _m_Instruction:
2814*f4a2713aSLionel Sambuc
2815*f4a2713aSLionel SambucImportant Public Members of the ``Instruction`` class
2816*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2817*f4a2713aSLionel Sambuc
2818*f4a2713aSLionel Sambuc* ``BasicBlock *getParent()``
2819*f4a2713aSLionel Sambuc
2820*f4a2713aSLionel Sambuc  Returns the BasicBlock_ that this
2821*f4a2713aSLionel Sambuc  ``Instruction`` is embedded into.
2822*f4a2713aSLionel Sambuc
2823*f4a2713aSLionel Sambuc* ``bool mayWriteToMemory()``
2824*f4a2713aSLionel Sambuc
2825*f4a2713aSLionel Sambuc  Returns true if the instruction writes to memory, i.e. it is a ``call``,
2826*f4a2713aSLionel Sambuc  ``free``, ``invoke``, or ``store``.
2827*f4a2713aSLionel Sambuc
2828*f4a2713aSLionel Sambuc* ``unsigned getOpcode()``
2829*f4a2713aSLionel Sambuc
2830*f4a2713aSLionel Sambuc  Returns the opcode for the ``Instruction``.
2831*f4a2713aSLionel Sambuc
2832*f4a2713aSLionel Sambuc* ``Instruction *clone() const``
2833*f4a2713aSLionel Sambuc
2834*f4a2713aSLionel Sambuc  Returns another instance of the specified instruction, identical in all ways
2835*f4a2713aSLionel Sambuc  to the original except that the instruction has no parent (i.e. it's not
2836*f4a2713aSLionel Sambuc  embedded into a BasicBlock_), and it has no name.
2837*f4a2713aSLionel Sambuc
2838*f4a2713aSLionel Sambuc.. _Constant:
2839*f4a2713aSLionel Sambuc
2840*f4a2713aSLionel SambucThe ``Constant`` class and subclasses
2841*f4a2713aSLionel Sambuc-------------------------------------
2842*f4a2713aSLionel Sambuc
2843*f4a2713aSLionel SambucConstant represents a base class for different types of constants.  It is
2844*f4a2713aSLionel Sambucsubclassed by ConstantInt, ConstantArray, etc. for representing the various
2845*f4a2713aSLionel Sambuctypes of Constants.  GlobalValue_ is also a subclass, which represents the
2846*f4a2713aSLionel Sambucaddress of a global variable or function.
2847*f4a2713aSLionel Sambuc
2848*f4a2713aSLionel Sambuc.. _s_Constant:
2849*f4a2713aSLionel Sambuc
2850*f4a2713aSLionel SambucImportant Subclasses of Constant
2851*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2852*f4a2713aSLionel Sambuc
2853*f4a2713aSLionel Sambuc* ConstantInt : This subclass of Constant represents an integer constant of
2854*f4a2713aSLionel Sambuc  any width.
2855*f4a2713aSLionel Sambuc
2856*f4a2713aSLionel Sambuc  * ``const APInt& getValue() const``: Returns the underlying
2857*f4a2713aSLionel Sambuc    value of this constant, an APInt value.
2858*f4a2713aSLionel Sambuc
2859*f4a2713aSLionel Sambuc  * ``int64_t getSExtValue() const``: Converts the underlying APInt value to an
2860*f4a2713aSLionel Sambuc    int64_t via sign extension.  If the value (not the bit width) of the APInt
2861*f4a2713aSLionel Sambuc    is too large to fit in an int64_t, an assertion will result.  For this
2862*f4a2713aSLionel Sambuc    reason, use of this method is discouraged.
2863*f4a2713aSLionel Sambuc
2864*f4a2713aSLionel Sambuc  * ``uint64_t getZExtValue() const``: Converts the underlying APInt value
2865*f4a2713aSLionel Sambuc    to a uint64_t via zero extension.  IF the value (not the bit width) of the
2866*f4a2713aSLionel Sambuc    APInt is too large to fit in a uint64_t, an assertion will result.  For this
2867*f4a2713aSLionel Sambuc    reason, use of this method is discouraged.
2868*f4a2713aSLionel Sambuc
2869*f4a2713aSLionel Sambuc  * ``static ConstantInt* get(const APInt& Val)``: Returns the ConstantInt
2870*f4a2713aSLionel Sambuc    object that represents the value provided by ``Val``.  The type is implied
2871*f4a2713aSLionel Sambuc    as the IntegerType that corresponds to the bit width of ``Val``.
2872*f4a2713aSLionel Sambuc
2873*f4a2713aSLionel Sambuc  * ``static ConstantInt* get(const Type *Ty, uint64_t Val)``: Returns the
2874*f4a2713aSLionel Sambuc    ConstantInt object that represents the value provided by ``Val`` for integer
2875*f4a2713aSLionel Sambuc    type ``Ty``.
2876*f4a2713aSLionel Sambuc
2877*f4a2713aSLionel Sambuc* ConstantFP : This class represents a floating point constant.
2878*f4a2713aSLionel Sambuc
2879*f4a2713aSLionel Sambuc  * ``double getValue() const``: Returns the underlying value of this constant.
2880*f4a2713aSLionel Sambuc
2881*f4a2713aSLionel Sambuc* ConstantArray : This represents a constant array.
2882*f4a2713aSLionel Sambuc
2883*f4a2713aSLionel Sambuc  * ``const std::vector<Use> &getValues() const``: Returns a vector of
2884*f4a2713aSLionel Sambuc    component constants that makeup this array.
2885*f4a2713aSLionel Sambuc
2886*f4a2713aSLionel Sambuc* ConstantStruct : This represents a constant struct.
2887*f4a2713aSLionel Sambuc
2888*f4a2713aSLionel Sambuc  * ``const std::vector<Use> &getValues() const``: Returns a vector of
2889*f4a2713aSLionel Sambuc    component constants that makeup this array.
2890*f4a2713aSLionel Sambuc
2891*f4a2713aSLionel Sambuc* GlobalValue : This represents either a global variable or a function.  In
2892*f4a2713aSLionel Sambuc  either case, the value is a constant fixed address (after linking).
2893*f4a2713aSLionel Sambuc
2894*f4a2713aSLionel Sambuc.. _GlobalValue:
2895*f4a2713aSLionel Sambuc
2896*f4a2713aSLionel SambucThe ``GlobalValue`` class
2897*f4a2713aSLionel Sambuc-------------------------
2898*f4a2713aSLionel Sambuc
2899*f4a2713aSLionel Sambuc``#include "llvm/IR/GlobalValue.h"``
2900*f4a2713aSLionel Sambuc
2901*f4a2713aSLionel Sambucheader source: `GlobalValue.h
2902*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/GlobalValue_8h-source.html>`_
2903*f4a2713aSLionel Sambuc
2904*f4a2713aSLionel Sambucdoxygen info: `GlobalValue Class
2905*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/classllvm_1_1GlobalValue.html>`_
2906*f4a2713aSLionel Sambuc
2907*f4a2713aSLionel SambucSuperclasses: Constant_, User_, Value_
2908*f4a2713aSLionel Sambuc
2909*f4a2713aSLionel SambucGlobal values ( GlobalVariable_\ s or :ref:`Function <c_Function>`\ s) are the
2910*f4a2713aSLionel Sambuconly LLVM values that are visible in the bodies of all :ref:`Function
2911*f4a2713aSLionel Sambuc<c_Function>`\ s.  Because they are visible at global scope, they are also
2912*f4a2713aSLionel Sambucsubject to linking with other globals defined in different translation units.
2913*f4a2713aSLionel SambucTo control the linking process, ``GlobalValue``\ s know their linkage rules.
2914*f4a2713aSLionel SambucSpecifically, ``GlobalValue``\ s know whether they have internal or external
2915*f4a2713aSLionel Sambuclinkage, as defined by the ``LinkageTypes`` enumeration.
2916*f4a2713aSLionel Sambuc
2917*f4a2713aSLionel SambucIf a ``GlobalValue`` has internal linkage (equivalent to being ``static`` in C),
2918*f4a2713aSLionel Sambucit is not visible to code outside the current translation unit, and does not
2919*f4a2713aSLionel Sambucparticipate in linking.  If it has external linkage, it is visible to external
2920*f4a2713aSLionel Sambuccode, and does participate in linking.  In addition to linkage information,
2921*f4a2713aSLionel Sambuc``GlobalValue``\ s keep track of which Module_ they are currently part of.
2922*f4a2713aSLionel Sambuc
2923*f4a2713aSLionel SambucBecause ``GlobalValue``\ s are memory objects, they are always referred to by
2924*f4a2713aSLionel Sambuctheir **address**.  As such, the Type_ of a global is always a pointer to its
2925*f4a2713aSLionel Sambuccontents.  It is important to remember this when using the ``GetElementPtrInst``
2926*f4a2713aSLionel Sambucinstruction because this pointer must be dereferenced first.  For example, if
2927*f4a2713aSLionel Sambucyou have a ``GlobalVariable`` (a subclass of ``GlobalValue)`` that is an array
2928*f4a2713aSLionel Sambucof 24 ints, type ``[24 x i32]``, then the ``GlobalVariable`` is a pointer to
2929*f4a2713aSLionel Sambucthat array.  Although the address of the first element of this array and the
2930*f4a2713aSLionel Sambucvalue of the ``GlobalVariable`` are the same, they have different types.  The
2931*f4a2713aSLionel Sambuc``GlobalVariable``'s type is ``[24 x i32]``.  The first element's type is
2932*f4a2713aSLionel Sambuc``i32.`` Because of this, accessing a global value requires you to dereference
2933*f4a2713aSLionel Sambucthe pointer with ``GetElementPtrInst`` first, then its elements can be accessed.
2934*f4a2713aSLionel SambucThis is explained in the `LLVM Language Reference Manual
2935*f4a2713aSLionel Sambuc<LangRef.html#globalvars>`_.
2936*f4a2713aSLionel Sambuc
2937*f4a2713aSLionel Sambuc.. _m_GlobalValue:
2938*f4a2713aSLionel Sambuc
2939*f4a2713aSLionel SambucImportant Public Members of the ``GlobalValue`` class
2940*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2941*f4a2713aSLionel Sambuc
2942*f4a2713aSLionel Sambuc* | ``bool hasInternalLinkage() const``
2943*f4a2713aSLionel Sambuc  | ``bool hasExternalLinkage() const``
2944*f4a2713aSLionel Sambuc  | ``void setInternalLinkage(bool HasInternalLinkage)``
2945*f4a2713aSLionel Sambuc
2946*f4a2713aSLionel Sambuc  These methods manipulate the linkage characteristics of the ``GlobalValue``.
2947*f4a2713aSLionel Sambuc
2948*f4a2713aSLionel Sambuc* ``Module *getParent()``
2949*f4a2713aSLionel Sambuc
2950*f4a2713aSLionel Sambuc  This returns the Module_ that the
2951*f4a2713aSLionel Sambuc  GlobalValue is currently embedded into.
2952*f4a2713aSLionel Sambuc
2953*f4a2713aSLionel Sambuc.. _c_Function:
2954*f4a2713aSLionel Sambuc
2955*f4a2713aSLionel SambucThe ``Function`` class
2956*f4a2713aSLionel Sambuc----------------------
2957*f4a2713aSLionel Sambuc
2958*f4a2713aSLionel Sambuc``#include "llvm/IR/Function.h"``
2959*f4a2713aSLionel Sambuc
2960*f4a2713aSLionel Sambucheader source: `Function.h <http://llvm.org/doxygen/Function_8h-source.html>`_
2961*f4a2713aSLionel Sambuc
2962*f4a2713aSLionel Sambucdoxygen info: `Function Class
2963*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/classllvm_1_1Function.html>`_
2964*f4a2713aSLionel Sambuc
2965*f4a2713aSLionel SambucSuperclasses: GlobalValue_, Constant_, User_, Value_
2966*f4a2713aSLionel Sambuc
2967*f4a2713aSLionel SambucThe ``Function`` class represents a single procedure in LLVM.  It is actually
2968*f4a2713aSLionel Sambucone of the more complex classes in the LLVM hierarchy because it must keep track
2969*f4a2713aSLionel Sambucof a large amount of data.  The ``Function`` class keeps track of a list of
2970*f4a2713aSLionel SambucBasicBlock_\ s, a list of formal Argument_\ s, and a SymbolTable_.
2971*f4a2713aSLionel Sambuc
2972*f4a2713aSLionel SambucThe list of BasicBlock_\ s is the most commonly used part of ``Function``
2973*f4a2713aSLionel Sambucobjects.  The list imposes an implicit ordering of the blocks in the function,
2974*f4a2713aSLionel Sambucwhich indicate how the code will be laid out by the backend.  Additionally, the
2975*f4a2713aSLionel Sambucfirst BasicBlock_ is the implicit entry node for the ``Function``.  It is not
2976*f4a2713aSLionel Sambuclegal in LLVM to explicitly branch to this initial block.  There are no implicit
2977*f4a2713aSLionel Sambucexit nodes, and in fact there may be multiple exit nodes from a single
2978*f4a2713aSLionel Sambuc``Function``.  If the BasicBlock_ list is empty, this indicates that the
2979*f4a2713aSLionel Sambuc``Function`` is actually a function declaration: the actual body of the function
2980*f4a2713aSLionel Sambuchasn't been linked in yet.
2981*f4a2713aSLionel Sambuc
2982*f4a2713aSLionel SambucIn addition to a list of BasicBlock_\ s, the ``Function`` class also keeps track
2983*f4a2713aSLionel Sambucof the list of formal Argument_\ s that the function receives.  This container
2984*f4a2713aSLionel Sambucmanages the lifetime of the Argument_ nodes, just like the BasicBlock_ list does
2985*f4a2713aSLionel Sambucfor the BasicBlock_\ s.
2986*f4a2713aSLionel Sambuc
2987*f4a2713aSLionel SambucThe SymbolTable_ is a very rarely used LLVM feature that is only used when you
2988*f4a2713aSLionel Sambuchave to look up a value by name.  Aside from that, the SymbolTable_ is used
2989*f4a2713aSLionel Sambucinternally to make sure that there are not conflicts between the names of
2990*f4a2713aSLionel SambucInstruction_\ s, BasicBlock_\ s, or Argument_\ s in the function body.
2991*f4a2713aSLionel Sambuc
2992*f4a2713aSLionel SambucNote that ``Function`` is a GlobalValue_ and therefore also a Constant_.  The
2993*f4a2713aSLionel Sambucvalue of the function is its address (after linking) which is guaranteed to be
2994*f4a2713aSLionel Sambucconstant.
2995*f4a2713aSLionel Sambuc
2996*f4a2713aSLionel Sambuc.. _m_Function:
2997*f4a2713aSLionel Sambuc
2998*f4a2713aSLionel SambucImportant Public Members of the ``Function``
2999*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3000*f4a2713aSLionel Sambuc
3001*f4a2713aSLionel Sambuc* ``Function(const FunctionType *Ty, LinkageTypes Linkage,
3002*f4a2713aSLionel Sambuc  const std::string &N = "", Module* Parent = 0)``
3003*f4a2713aSLionel Sambuc
3004*f4a2713aSLionel Sambuc  Constructor used when you need to create new ``Function``\ s to add the
3005*f4a2713aSLionel Sambuc  program.  The constructor must specify the type of the function to create and
3006*f4a2713aSLionel Sambuc  what type of linkage the function should have.  The FunctionType_ argument
3007*f4a2713aSLionel Sambuc  specifies the formal arguments and return value for the function.  The same
3008*f4a2713aSLionel Sambuc  FunctionType_ value can be used to create multiple functions.  The ``Parent``
3009*f4a2713aSLionel Sambuc  argument specifies the Module in which the function is defined.  If this
3010*f4a2713aSLionel Sambuc  argument is provided, the function will automatically be inserted into that
3011*f4a2713aSLionel Sambuc  module's list of functions.
3012*f4a2713aSLionel Sambuc
3013*f4a2713aSLionel Sambuc* ``bool isDeclaration()``
3014*f4a2713aSLionel Sambuc
3015*f4a2713aSLionel Sambuc  Return whether or not the ``Function`` has a body defined.  If the function is
3016*f4a2713aSLionel Sambuc  "external", it does not have a body, and thus must be resolved by linking with
3017*f4a2713aSLionel Sambuc  a function defined in a different translation unit.
3018*f4a2713aSLionel Sambuc
3019*f4a2713aSLionel Sambuc* | ``Function::iterator`` - Typedef for basic block list iterator
3020*f4a2713aSLionel Sambuc  | ``Function::const_iterator`` - Typedef for const_iterator.
3021*f4a2713aSLionel Sambuc  | ``begin()``, ``end()``, ``size()``, ``empty()``
3022*f4a2713aSLionel Sambuc
3023*f4a2713aSLionel Sambuc  These are forwarding methods that make it easy to access the contents of a
3024*f4a2713aSLionel Sambuc  ``Function`` object's BasicBlock_ list.
3025*f4a2713aSLionel Sambuc
3026*f4a2713aSLionel Sambuc* ``Function::BasicBlockListType &getBasicBlockList()``
3027*f4a2713aSLionel Sambuc
3028*f4a2713aSLionel Sambuc  Returns the list of BasicBlock_\ s.  This is necessary to use when you need to
3029*f4a2713aSLionel Sambuc  update the list or perform a complex action that doesn't have a forwarding
3030*f4a2713aSLionel Sambuc  method.
3031*f4a2713aSLionel Sambuc
3032*f4a2713aSLionel Sambuc* | ``Function::arg_iterator`` - Typedef for the argument list iterator
3033*f4a2713aSLionel Sambuc  | ``Function::const_arg_iterator`` - Typedef for const_iterator.
3034*f4a2713aSLionel Sambuc  | ``arg_begin()``, ``arg_end()``, ``arg_size()``, ``arg_empty()``
3035*f4a2713aSLionel Sambuc
3036*f4a2713aSLionel Sambuc  These are forwarding methods that make it easy to access the contents of a
3037*f4a2713aSLionel Sambuc  ``Function`` object's Argument_ list.
3038*f4a2713aSLionel Sambuc
3039*f4a2713aSLionel Sambuc* ``Function::ArgumentListType &getArgumentList()``
3040*f4a2713aSLionel Sambuc
3041*f4a2713aSLionel Sambuc  Returns the list of Argument_.  This is necessary to use when you need to
3042*f4a2713aSLionel Sambuc  update the list or perform a complex action that doesn't have a forwarding
3043*f4a2713aSLionel Sambuc  method.
3044*f4a2713aSLionel Sambuc
3045*f4a2713aSLionel Sambuc* ``BasicBlock &getEntryBlock()``
3046*f4a2713aSLionel Sambuc
3047*f4a2713aSLionel Sambuc  Returns the entry ``BasicBlock`` for the function.  Because the entry block
3048*f4a2713aSLionel Sambuc  for the function is always the first block, this returns the first block of
3049*f4a2713aSLionel Sambuc  the ``Function``.
3050*f4a2713aSLionel Sambuc
3051*f4a2713aSLionel Sambuc* | ``Type *getReturnType()``
3052*f4a2713aSLionel Sambuc  | ``FunctionType *getFunctionType()``
3053*f4a2713aSLionel Sambuc
3054*f4a2713aSLionel Sambuc  This traverses the Type_ of the ``Function`` and returns the return type of
3055*f4a2713aSLionel Sambuc  the function, or the FunctionType_ of the actual function.
3056*f4a2713aSLionel Sambuc
3057*f4a2713aSLionel Sambuc* ``SymbolTable *getSymbolTable()``
3058*f4a2713aSLionel Sambuc
3059*f4a2713aSLionel Sambuc  Return a pointer to the SymbolTable_ for this ``Function``.
3060*f4a2713aSLionel Sambuc
3061*f4a2713aSLionel Sambuc.. _GlobalVariable:
3062*f4a2713aSLionel Sambuc
3063*f4a2713aSLionel SambucThe ``GlobalVariable`` class
3064*f4a2713aSLionel Sambuc----------------------------
3065*f4a2713aSLionel Sambuc
3066*f4a2713aSLionel Sambuc``#include "llvm/IR/GlobalVariable.h"``
3067*f4a2713aSLionel Sambuc
3068*f4a2713aSLionel Sambucheader source: `GlobalVariable.h
3069*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/GlobalVariable_8h-source.html>`_
3070*f4a2713aSLionel Sambuc
3071*f4a2713aSLionel Sambucdoxygen info: `GlobalVariable Class
3072*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/classllvm_1_1GlobalVariable.html>`_
3073*f4a2713aSLionel Sambuc
3074*f4a2713aSLionel SambucSuperclasses: GlobalValue_, Constant_, User_, Value_
3075*f4a2713aSLionel Sambuc
3076*f4a2713aSLionel SambucGlobal variables are represented with the (surprise surprise) ``GlobalVariable``
3077*f4a2713aSLionel Sambucclass.  Like functions, ``GlobalVariable``\ s are also subclasses of
3078*f4a2713aSLionel SambucGlobalValue_, and as such are always referenced by their address (global values
3079*f4a2713aSLionel Sambucmust live in memory, so their "name" refers to their constant address).  See
3080*f4a2713aSLionel SambucGlobalValue_ for more on this.  Global variables may have an initial value
3081*f4a2713aSLionel Sambuc(which must be a Constant_), and if they have an initializer, they may be marked
3082*f4a2713aSLionel Sambucas "constant" themselves (indicating that their contents never change at
3083*f4a2713aSLionel Sambucruntime).
3084*f4a2713aSLionel Sambuc
3085*f4a2713aSLionel Sambuc.. _m_GlobalVariable:
3086*f4a2713aSLionel Sambuc
3087*f4a2713aSLionel SambucImportant Public Members of the ``GlobalVariable`` class
3088*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3089*f4a2713aSLionel Sambuc
3090*f4a2713aSLionel Sambuc* ``GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes &Linkage,
3091*f4a2713aSLionel Sambuc  Constant *Initializer = 0, const std::string &Name = "", Module* Parent = 0)``
3092*f4a2713aSLionel Sambuc
3093*f4a2713aSLionel Sambuc  Create a new global variable of the specified type.  If ``isConstant`` is true
3094*f4a2713aSLionel Sambuc  then the global variable will be marked as unchanging for the program.  The
3095*f4a2713aSLionel Sambuc  Linkage parameter specifies the type of linkage (internal, external, weak,
3096*f4a2713aSLionel Sambuc  linkonce, appending) for the variable.  If the linkage is InternalLinkage,
3097*f4a2713aSLionel Sambuc  WeakAnyLinkage, WeakODRLinkage, LinkOnceAnyLinkage or LinkOnceODRLinkage, then
3098*f4a2713aSLionel Sambuc  the resultant global variable will have internal linkage.  AppendingLinkage
3099*f4a2713aSLionel Sambuc  concatenates together all instances (in different translation units) of the
3100*f4a2713aSLionel Sambuc  variable into a single variable but is only applicable to arrays.  See the
3101*f4a2713aSLionel Sambuc  `LLVM Language Reference <LangRef.html#modulestructure>`_ for further details
3102*f4a2713aSLionel Sambuc  on linkage types.  Optionally an initializer, a name, and the module to put
3103*f4a2713aSLionel Sambuc  the variable into may be specified for the global variable as well.
3104*f4a2713aSLionel Sambuc
3105*f4a2713aSLionel Sambuc* ``bool isConstant() const``
3106*f4a2713aSLionel Sambuc
3107*f4a2713aSLionel Sambuc  Returns true if this is a global variable that is known not to be modified at
3108*f4a2713aSLionel Sambuc  runtime.
3109*f4a2713aSLionel Sambuc
3110*f4a2713aSLionel Sambuc* ``bool hasInitializer()``
3111*f4a2713aSLionel Sambuc
3112*f4a2713aSLionel Sambuc  Returns true if this ``GlobalVariable`` has an intializer.
3113*f4a2713aSLionel Sambuc
3114*f4a2713aSLionel Sambuc* ``Constant *getInitializer()``
3115*f4a2713aSLionel Sambuc
3116*f4a2713aSLionel Sambuc  Returns the initial value for a ``GlobalVariable``.  It is not legal to call
3117*f4a2713aSLionel Sambuc  this method if there is no initializer.
3118*f4a2713aSLionel Sambuc
3119*f4a2713aSLionel Sambuc.. _BasicBlock:
3120*f4a2713aSLionel Sambuc
3121*f4a2713aSLionel SambucThe ``BasicBlock`` class
3122*f4a2713aSLionel Sambuc------------------------
3123*f4a2713aSLionel Sambuc
3124*f4a2713aSLionel Sambuc``#include "llvm/IR/BasicBlock.h"``
3125*f4a2713aSLionel Sambuc
3126*f4a2713aSLionel Sambucheader source: `BasicBlock.h
3127*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/BasicBlock_8h-source.html>`_
3128*f4a2713aSLionel Sambuc
3129*f4a2713aSLionel Sambucdoxygen info: `BasicBlock Class
3130*f4a2713aSLionel Sambuc<http://llvm.org/doxygen/classllvm_1_1BasicBlock.html>`_
3131*f4a2713aSLionel Sambuc
3132*f4a2713aSLionel SambucSuperclass: Value_
3133*f4a2713aSLionel Sambuc
3134*f4a2713aSLionel SambucThis class represents a single entry single exit section of the code, commonly
3135*f4a2713aSLionel Sambucknown as a basic block by the compiler community.  The ``BasicBlock`` class
3136*f4a2713aSLionel Sambucmaintains a list of Instruction_\ s, which form the body of the block.  Matching
3137*f4a2713aSLionel Sambucthe language definition, the last element of this list of instructions is always
3138*f4a2713aSLionel Sambuca terminator instruction (a subclass of the TerminatorInst_ class).
3139*f4a2713aSLionel Sambuc
3140*f4a2713aSLionel SambucIn addition to tracking the list of instructions that make up the block, the
3141*f4a2713aSLionel Sambuc``BasicBlock`` class also keeps track of the :ref:`Function <c_Function>` that
3142*f4a2713aSLionel Sambucit is embedded into.
3143*f4a2713aSLionel Sambuc
3144*f4a2713aSLionel SambucNote that ``BasicBlock``\ s themselves are Value_\ s, because they are
3145*f4a2713aSLionel Sambucreferenced by instructions like branches and can go in the switch tables.
3146*f4a2713aSLionel Sambuc``BasicBlock``\ s have type ``label``.
3147*f4a2713aSLionel Sambuc
3148*f4a2713aSLionel Sambuc.. _m_BasicBlock:
3149*f4a2713aSLionel Sambuc
3150*f4a2713aSLionel SambucImportant Public Members of the ``BasicBlock`` class
3151*f4a2713aSLionel Sambuc^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3152*f4a2713aSLionel Sambuc
3153*f4a2713aSLionel Sambuc* ``BasicBlock(const std::string &Name = "", Function *Parent = 0)``
3154*f4a2713aSLionel Sambuc
3155*f4a2713aSLionel Sambuc  The ``BasicBlock`` constructor is used to create new basic blocks for
3156*f4a2713aSLionel Sambuc  insertion into a function.  The constructor optionally takes a name for the
3157*f4a2713aSLionel Sambuc  new block, and a :ref:`Function <c_Function>` to insert it into.  If the
3158*f4a2713aSLionel Sambuc  ``Parent`` parameter is specified, the new ``BasicBlock`` is automatically
3159*f4a2713aSLionel Sambuc  inserted at the end of the specified :ref:`Function <c_Function>`, if not
3160*f4a2713aSLionel Sambuc  specified, the BasicBlock must be manually inserted into the :ref:`Function
3161*f4a2713aSLionel Sambuc  <c_Function>`.
3162*f4a2713aSLionel Sambuc
3163*f4a2713aSLionel Sambuc* | ``BasicBlock::iterator`` - Typedef for instruction list iterator
3164*f4a2713aSLionel Sambuc  | ``BasicBlock::const_iterator`` - Typedef for const_iterator.
3165*f4a2713aSLionel Sambuc  | ``begin()``, ``end()``, ``front()``, ``back()``,
3166*f4a2713aSLionel Sambuc    ``size()``, ``empty()``
3167*f4a2713aSLionel Sambuc    STL-style functions for accessing the instruction list.
3168*f4a2713aSLionel Sambuc
3169*f4a2713aSLionel Sambuc  These methods and typedefs are forwarding functions that have the same
3170*f4a2713aSLionel Sambuc  semantics as the standard library methods of the same names.  These methods
3171*f4a2713aSLionel Sambuc  expose the underlying instruction list of a basic block in a way that is easy
3172*f4a2713aSLionel Sambuc  to manipulate.  To get the full complement of container operations (including
3173*f4a2713aSLionel Sambuc  operations to update the list), you must use the ``getInstList()`` method.
3174*f4a2713aSLionel Sambuc
3175*f4a2713aSLionel Sambuc* ``BasicBlock::InstListType &getInstList()``
3176*f4a2713aSLionel Sambuc
3177*f4a2713aSLionel Sambuc  This method is used to get access to the underlying container that actually
3178*f4a2713aSLionel Sambuc  holds the Instructions.  This method must be used when there isn't a
3179*f4a2713aSLionel Sambuc  forwarding function in the ``BasicBlock`` class for the operation that you
3180*f4a2713aSLionel Sambuc  would like to perform.  Because there are no forwarding functions for
3181*f4a2713aSLionel Sambuc  "updating" operations, you need to use this if you want to update the contents
3182*f4a2713aSLionel Sambuc  of a ``BasicBlock``.
3183*f4a2713aSLionel Sambuc
3184*f4a2713aSLionel Sambuc* ``Function *getParent()``
3185*f4a2713aSLionel Sambuc
3186*f4a2713aSLionel Sambuc  Returns a pointer to :ref:`Function <c_Function>` the block is embedded into,
3187*f4a2713aSLionel Sambuc  or a null pointer if it is homeless.
3188*f4a2713aSLionel Sambuc
3189*f4a2713aSLionel Sambuc* ``TerminatorInst *getTerminator()``
3190*f4a2713aSLionel Sambuc
3191*f4a2713aSLionel Sambuc  Returns a pointer to the terminator instruction that appears at the end of the
3192*f4a2713aSLionel Sambuc  ``BasicBlock``.  If there is no terminator instruction, or if the last
3193*f4a2713aSLionel Sambuc  instruction in the block is not a terminator, then a null pointer is returned.
3194*f4a2713aSLionel Sambuc
3195*f4a2713aSLionel Sambuc.. _Argument:
3196*f4a2713aSLionel Sambuc
3197*f4a2713aSLionel SambucThe ``Argument`` class
3198*f4a2713aSLionel Sambuc----------------------
3199*f4a2713aSLionel Sambuc
3200*f4a2713aSLionel SambucThis subclass of Value defines the interface for incoming formal arguments to a
3201*f4a2713aSLionel Sambucfunction.  A Function maintains a list of its formal arguments.  An argument has
3202*f4a2713aSLionel Sambuca pointer to the parent Function.
3203*f4a2713aSLionel Sambuc
3204*f4a2713aSLionel Sambuc
3205