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