ProgrammersManual.rst 161 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130
  1. ========================
  2. LLVM Programmer's Manual
  3. ========================
  4. .. contents::
  5. :local:
  6. .. warning::
  7. This is always a work in progress.
  8. .. _introduction:
  9. Introduction
  10. ============
  11. This document is meant to highlight some of the important classes and interfaces
  12. available in the LLVM source-base. This manual is not intended to explain what
  13. LLVM is, how it works, and what LLVM code looks like. It assumes that you know
  14. the basics of LLVM and are interested in writing transformations or otherwise
  15. analyzing or manipulating the code.
  16. This document should get you oriented so that you can find your way in the
  17. continuously growing source code that makes up the LLVM infrastructure. Note
  18. that this manual is not intended to serve as a replacement for reading the
  19. source code, so if you think there should be a method in one of these classes to
  20. do something, but it's not listed, check the source. Links to the `doxygen
  21. <http://llvm.org/doxygen/>`__ sources are provided to make this as easy as
  22. possible.
  23. The first section of this document describes general information that is useful
  24. to know when working in the LLVM infrastructure, and the second describes the
  25. Core LLVM classes. In the future this manual will be extended with information
  26. describing how to use extension libraries, such as dominator information, CFG
  27. traversal routines, and useful utilities like the ``InstVisitor`` (`doxygen
  28. <http://llvm.org/doxygen/InstVisitor_8h_source.html>`__) template.
  29. .. _general:
  30. General Information
  31. ===================
  32. This section contains general information that is useful if you are working in
  33. the LLVM source-base, but that isn't specific to any particular API.
  34. .. _stl:
  35. The C++ Standard Template Library
  36. ---------------------------------
  37. LLVM makes heavy use of the C++ Standard Template Library (STL), perhaps much
  38. more than you are used to, or have seen before. Because of this, you might want
  39. to do a little background reading in the techniques used and capabilities of the
  40. library. There are many good pages that discuss the STL, and several books on
  41. the subject that you can get, so it will not be discussed in this document.
  42. Here are some useful links:
  43. #. `cppreference.com
  44. <http://en.cppreference.com/w/>`_ - an excellent
  45. reference for the STL and other parts of the standard C++ library.
  46. #. `C++ In a Nutshell <http://www.tempest-sw.com/cpp/>`_ - This is an O'Reilly
  47. book in the making. It has a decent Standard Library Reference that rivals
  48. Dinkumware's, and is unfortunately no longer free since the book has been
  49. published.
  50. #. `C++ Frequently Asked Questions <http://www.parashift.com/c++-faq-lite/>`_.
  51. #. `SGI's STL Programmer's Guide <http://www.sgi.com/tech/stl/>`_ - Contains a
  52. useful `Introduction to the STL
  53. <http://www.sgi.com/tech/stl/stl_introduction.html>`_.
  54. #. `Bjarne Stroustrup's C++ Page
  55. <http://www.research.att.com/%7Ebs/C++.html>`_.
  56. #. `Bruce Eckel's Thinking in C++, 2nd ed. Volume 2 Revision 4.0
  57. (even better, get the book)
  58. <http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html>`_.
  59. You are also encouraged to take a look at the :doc:`LLVM Coding Standards
  60. <CodingStandards>` guide which focuses on how to write maintainable code more
  61. than where to put your curly braces.
  62. .. _resources:
  63. Other useful references
  64. -----------------------
  65. #. `Using static and shared libraries across platforms
  66. <http://www.fortran-2000.com/ArnaudRecipes/sharedlib.html>`_
  67. .. _apis:
  68. Important and useful LLVM APIs
  69. ==============================
  70. Here we highlight some LLVM APIs that are generally useful and good to know
  71. about when writing transformations.
  72. .. _isa:
  73. The ``isa<>``, ``cast<>`` and ``dyn_cast<>`` templates
  74. ------------------------------------------------------
  75. The LLVM source-base makes extensive use of a custom form of RTTI. These
  76. templates have many similarities to the C++ ``dynamic_cast<>`` operator, but
  77. they don't have some drawbacks (primarily stemming from the fact that
  78. ``dynamic_cast<>`` only works on classes that have a v-table). Because they are
  79. used so often, you must know what they do and how they work. All of these
  80. templates are defined in the ``llvm/Support/Casting.h`` (`doxygen
  81. <http://llvm.org/doxygen/Casting_8h_source.html>`__) file (note that you very
  82. rarely have to include this file directly).
  83. ``isa<>``:
  84. The ``isa<>`` operator works exactly like the Java "``instanceof``" operator.
  85. It returns true or false depending on whether a reference or pointer points to
  86. an instance of the specified class. This can be very useful for constraint
  87. checking of various sorts (example below).
  88. ``cast<>``:
  89. The ``cast<>`` operator is a "checked cast" operation. It converts a pointer
  90. or reference from a base class to a derived class, causing an assertion
  91. failure if it is not really an instance of the right type. This should be
  92. used in cases where you have some information that makes you believe that
  93. something is of the right type. An example of the ``isa<>`` and ``cast<>``
  94. template is:
  95. .. code-block:: c++
  96. static bool isLoopInvariant(const Value *V, const Loop *L) {
  97. if (isa<Constant>(V) || isa<Argument>(V) || isa<GlobalValue>(V))
  98. return true;
  99. // Otherwise, it must be an instruction...
  100. return !L->contains(cast<Instruction>(V)->getParent());
  101. }
  102. Note that you should **not** use an ``isa<>`` test followed by a ``cast<>``,
  103. for that use the ``dyn_cast<>`` operator.
  104. ``dyn_cast<>``:
  105. The ``dyn_cast<>`` operator is a "checking cast" operation. It checks to see
  106. if the operand is of the specified type, and if so, returns a pointer to it
  107. (this operator does not work with references). If the operand is not of the
  108. correct type, a null pointer is returned. Thus, this works very much like
  109. the ``dynamic_cast<>`` operator in C++, and should be used in the same
  110. circumstances. Typically, the ``dyn_cast<>`` operator is used in an ``if``
  111. statement or some other flow control statement like this:
  112. .. code-block:: c++
  113. if (auto *AI = dyn_cast<AllocationInst>(Val)) {
  114. // ...
  115. }
  116. This form of the ``if`` statement effectively combines together a call to
  117. ``isa<>`` and a call to ``cast<>`` into one statement, which is very
  118. convenient.
  119. Note that the ``dyn_cast<>`` operator, like C++'s ``dynamic_cast<>`` or Java's
  120. ``instanceof`` operator, can be abused. In particular, you should not use big
  121. chained ``if/then/else`` blocks to check for lots of different variants of
  122. classes. If you find yourself wanting to do this, it is much cleaner and more
  123. efficient to use the ``InstVisitor`` class to dispatch over the instruction
  124. type directly.
  125. ``cast_or_null<>``:
  126. The ``cast_or_null<>`` operator works just like the ``cast<>`` operator,
  127. except that it allows for a null pointer as an argument (which it then
  128. propagates). This can sometimes be useful, allowing you to combine several
  129. null checks into one.
  130. ``dyn_cast_or_null<>``:
  131. The ``dyn_cast_or_null<>`` operator works just like the ``dyn_cast<>``
  132. operator, except that it allows for a null pointer as an argument (which it
  133. then propagates). This can sometimes be useful, allowing you to combine
  134. several null checks into one.
  135. These five templates can be used with any classes, whether they have a v-table
  136. or not. If you want to add support for these templates, see the document
  137. :doc:`How to set up LLVM-style RTTI for your class hierarchy
  138. <HowToSetUpLLVMStyleRTTI>`
  139. .. _string_apis:
  140. Passing strings (the ``StringRef`` and ``Twine`` classes)
  141. ---------------------------------------------------------
  142. Although LLVM generally does not do much string manipulation, we do have several
  143. important APIs which take strings. Two important examples are the Value class
  144. -- which has names for instructions, functions, etc. -- and the ``StringMap``
  145. class which is used extensively in LLVM and Clang.
  146. These are generic classes, and they need to be able to accept strings which may
  147. have embedded null characters. Therefore, they cannot simply take a ``const
  148. char *``, and taking a ``const std::string&`` requires clients to perform a heap
  149. allocation which is usually unnecessary. Instead, many LLVM APIs use a
  150. ``StringRef`` or a ``const Twine&`` for passing strings efficiently.
  151. .. _StringRef:
  152. The ``StringRef`` class
  153. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  154. The ``StringRef`` data type represents a reference to a constant string (a
  155. character array and a length) and supports the common operations available on
  156. ``std::string``, but does not require heap allocation.
  157. It can be implicitly constructed using a C style null-terminated string, an
  158. ``std::string``, or explicitly with a character pointer and length. For
  159. example, the ``StringRef`` find function is declared as:
  160. .. code-block:: c++
  161. iterator find(StringRef Key);
  162. and clients can call it using any one of:
  163. .. code-block:: c++
  164. Map.find("foo"); // Lookup "foo"
  165. Map.find(std::string("bar")); // Lookup "bar"
  166. Map.find(StringRef("\0baz", 4)); // Lookup "\0baz"
  167. Similarly, APIs which need to return a string may return a ``StringRef``
  168. instance, which can be used directly or converted to an ``std::string`` using
  169. the ``str`` member function. See ``llvm/ADT/StringRef.h`` (`doxygen
  170. <http://llvm.org/doxygen/StringRef_8h_source.html>`__) for more
  171. information.
  172. You should rarely use the ``StringRef`` class directly, because it contains
  173. pointers to external memory it is not generally safe to store an instance of the
  174. class (unless you know that the external storage will not be freed).
  175. ``StringRef`` is small and pervasive enough in LLVM that it should always be
  176. passed by value.
  177. The ``Twine`` class
  178. ^^^^^^^^^^^^^^^^^^^
  179. The ``Twine`` (`doxygen <http://llvm.org/doxygen/classllvm_1_1Twine.html>`__)
  180. class is an efficient way for APIs to accept concatenated strings. For example,
  181. a common LLVM paradigm is to name one instruction based on the name of another
  182. instruction with a suffix, for example:
  183. .. code-block:: c++
  184. New = CmpInst::Create(..., SO->getName() + ".cmp");
  185. The ``Twine`` class is effectively a lightweight `rope
  186. <http://en.wikipedia.org/wiki/Rope_(computer_science)>`_ which points to
  187. temporary (stack allocated) objects. Twines can be implicitly constructed as
  188. the result of the plus operator applied to strings (i.e., a C strings, an
  189. ``std::string``, or a ``StringRef``). The twine delays the actual concatenation
  190. of strings until it is actually required, at which point it can be efficiently
  191. rendered directly into a character array. This avoids unnecessary heap
  192. allocation involved in constructing the temporary results of string
  193. concatenation. See ``llvm/ADT/Twine.h`` (`doxygen
  194. <http://llvm.org/doxygen/Twine_8h_source.html>`__) and :ref:`here <dss_twine>`
  195. for more information.
  196. As with a ``StringRef``, ``Twine`` objects point to external memory and should
  197. almost never be stored or mentioned directly. They are intended solely for use
  198. when defining a function which should be able to efficiently accept concatenated
  199. strings.
  200. .. _formatting_strings:
  201. Formatting strings (the ``formatv`` function)
  202. ---------------------------------------------
  203. While LLVM doesn't necessarily do a lot of string manipulation and parsing, it
  204. does do a lot of string formatting. From diagnostic messages, to llvm tool
  205. outputs such as ``llvm-readobj`` to printing verbose disassembly listings and
  206. LLDB runtime logging, the need for string formatting is pervasive.
  207. The ``formatv`` is similar in spirit to ``printf``, but uses a different syntax
  208. which borrows heavily from Python and C#. Unlike ``printf`` it deduces the type
  209. to be formatted at compile time, so it does not need a format specifier such as
  210. ``%d``. This reduces the mental overhead of trying to construct portable format
  211. strings, especially for platform-specific types like ``size_t`` or pointer types.
  212. Unlike both ``printf`` and Python, it additionally fails to compile if LLVM does
  213. not know how to format the type. These two properties ensure that the function
  214. is both safer and simpler to use than traditional formatting methods such as
  215. the ``printf`` family of functions.
  216. Simple formatting
  217. ^^^^^^^^^^^^^^^^^
  218. A call to ``formatv`` involves a single **format string** consisting of 0 or more
  219. **replacement sequences**, followed by a variable length list of **replacement values**.
  220. A replacement sequence is a string of the form ``{N[[,align]:style]}``.
  221. ``N`` refers to the 0-based index of the argument from the list of replacement
  222. values. Note that this means it is possible to reference the same parameter
  223. multiple times, possibly with different style and/or alignment options, in any order.
  224. ``align`` is an optional string specifying the width of the field to format
  225. the value into, and the alignment of the value within the field. It is specified as
  226. an optional **alignment style** followed by a positive integral **field width**. The
  227. alignment style can be one of the characters ``-`` (left align), ``=`` (center align),
  228. or ``+`` (right align). The default is right aligned.
  229. ``style`` is an optional string consisting of a type specific that controls the
  230. formatting of the value. For example, to format a floating point value as a percentage,
  231. you can use the style option ``P``.
  232. Custom formatting
  233. ^^^^^^^^^^^^^^^^^
  234. There are two ways to customize the formatting behavior for a type.
  235. 1. Provide a template specialization of ``llvm::format_provider<T>`` for your
  236. type ``T`` with the appropriate static format method.
  237. .. code-block:: c++
  238. namespace llvm {
  239. template<>
  240. struct format_provider<MyFooBar> {
  241. static void format(const MyFooBar &V, raw_ostream &Stream, StringRef Style) {
  242. // Do whatever is necessary to format `V` into `Stream`
  243. }
  244. };
  245. void foo() {
  246. MyFooBar X;
  247. std::string S = formatv("{0}", X);
  248. }
  249. }
  250. This is a useful extensibility mechanism for adding support for formatting your own
  251. custom types with your own custom Style options. But it does not help when you want
  252. to extend the mechanism for formatting a type that the library already knows how to
  253. format. For that, we need something else.
  254. 2. Provide a **format adapter** inheriting from ``llvm::FormatAdapter<T>``.
  255. .. code-block:: c++
  256. namespace anything {
  257. struct format_int_custom : public llvm::FormatAdapter<int> {
  258. explicit format_int_custom(int N) : llvm::FormatAdapter<int>(N) {}
  259. void format(llvm::raw_ostream &Stream, StringRef Style) override {
  260. // Do whatever is necessary to format ``this->Item`` into ``Stream``
  261. }
  262. };
  263. }
  264. namespace llvm {
  265. void foo() {
  266. std::string S = formatv("{0}", anything::format_int_custom(42));
  267. }
  268. }
  269. If the type is detected to be derived from ``FormatAdapter<T>``, ``formatv``
  270. will call the
  271. ``format`` method on the argument passing in the specified style. This allows
  272. one to provide custom formatting of any type, including one which already has
  273. a builtin format provider.
  274. ``formatv`` Examples
  275. ^^^^^^^^^^^^^^^^^^^^
  276. Below is intended to provide an incomplete set of examples demonstrating
  277. the usage of ``formatv``. More information can be found by reading the
  278. doxygen documentation or by looking at the unit test suite.
  279. .. code-block:: c++
  280. std::string S;
  281. // Simple formatting of basic types and implicit string conversion.
  282. S = formatv("{0} ({1:P})", 7, 0.35); // S == "7 (35.00%)"
  283. // Out-of-order referencing and multi-referencing
  284. outs() << formatv("{0} {2} {1} {0}", 1, "test", 3); // prints "1 3 test 1"
  285. // Left, right, and center alignment
  286. S = formatv("{0,7}", 'a'); // S == " a";
  287. S = formatv("{0,-7}", 'a'); // S == "a ";
  288. S = formatv("{0,=7}", 'a'); // S == " a ";
  289. S = formatv("{0,+7}", 'a'); // S == " a";
  290. // Custom styles
  291. S = formatv("{0:N} - {0:x} - {1:E}", 12345, 123908342); // S == "12,345 - 0x3039 - 1.24E8"
  292. // Adapters
  293. S = formatv("{0}", fmt_align(42, AlignStyle::Center, 7)); // S == " 42 "
  294. S = formatv("{0}", fmt_repeat("hi", 3)); // S == "hihihi"
  295. S = formatv("{0}", fmt_pad("hi", 2, 6)); // S == " hi "
  296. // Ranges
  297. std::vector<int> V = {8, 9, 10};
  298. S = formatv("{0}", make_range(V.begin(), V.end())); // S == "8, 9, 10"
  299. S = formatv("{0:$[+]}", make_range(V.begin(), V.end())); // S == "8+9+10"
  300. S = formatv("{0:$[ + ]@[x]}", make_range(V.begin(), V.end())); // S == "0x8 + 0x9 + 0xA"
  301. .. _error_apis:
  302. Error handling
  303. --------------
  304. Proper error handling helps us identify bugs in our code, and helps end-users
  305. understand errors in their tool usage. Errors fall into two broad categories:
  306. *programmatic* and *recoverable*, with different strategies for handling and
  307. reporting.
  308. Programmatic Errors
  309. ^^^^^^^^^^^^^^^^^^^
  310. Programmatic errors are violations of program invariants or API contracts, and
  311. represent bugs within the program itself. Our aim is to document invariants, and
  312. to abort quickly at the point of failure (providing some basic diagnostic) when
  313. invariants are broken at runtime.
  314. The fundamental tools for handling programmatic errors are assertions and the
  315. llvm_unreachable function. Assertions are used to express invariant conditions,
  316. and should include a message describing the invariant:
  317. .. code-block:: c++
  318. assert(isPhysReg(R) && "All virt regs should have been allocated already.");
  319. The llvm_unreachable function can be used to document areas of control flow
  320. that should never be entered if the program invariants hold:
  321. .. code-block:: c++
  322. enum { Foo, Bar, Baz } X = foo();
  323. switch (X) {
  324. case Foo: /* Handle Foo */; break;
  325. case Bar: /* Handle Bar */; break;
  326. default:
  327. llvm_unreachable("X should be Foo or Bar here");
  328. }
  329. Recoverable Errors
  330. ^^^^^^^^^^^^^^^^^^
  331. Recoverable errors represent an error in the program's environment, for example
  332. a resource failure (a missing file, a dropped network connection, etc.), or
  333. malformed input. These errors should be detected and communicated to a level of
  334. the program where they can be handled appropriately. Handling the error may be
  335. as simple as reporting the issue to the user, or it may involve attempts at
  336. recovery.
  337. .. note::
  338. While it would be ideal to use this error handling scheme throughout
  339. LLVM, there are places where this hasn't been practical to apply. In
  340. situations where you absolutely must emit a non-programmatic error and
  341. the ``Error`` model isn't workable you can call ``report_fatal_error``,
  342. which will call installed error handlers, print a message, and exit the
  343. program.
  344. Recoverable errors are modeled using LLVM's ``Error`` scheme. This scheme
  345. represents errors using function return values, similar to classic C integer
  346. error codes, or C++'s ``std::error_code``. However, the ``Error`` class is
  347. actually a lightweight wrapper for user-defined error types, allowing arbitrary
  348. information to be attached to describe the error. This is similar to the way C++
  349. exceptions allow throwing of user-defined types.
  350. Success values are created by calling ``Error::success()``, E.g.:
  351. .. code-block:: c++
  352. Error foo() {
  353. // Do something.
  354. // Return success.
  355. return Error::success();
  356. }
  357. Success values are very cheap to construct and return - they have minimal
  358. impact on program performance.
  359. Failure values are constructed using ``make_error<T>``, where ``T`` is any class
  360. that inherits from the ErrorInfo utility, E.g.:
  361. .. code-block:: c++
  362. class BadFileFormat : public ErrorInfo<BadFileFormat> {
  363. public:
  364. static char ID;
  365. std::string Path;
  366. BadFileFormat(StringRef Path) : Path(Path.str()) {}
  367. void log(raw_ostream &OS) const override {
  368. OS << Path << " is malformed";
  369. }
  370. std::error_code convertToErrorCode() const override {
  371. return make_error_code(object_error::parse_failed);
  372. }
  373. };
  374. char BadFileFormat::ID; // This should be declared in the C++ file.
  375. Error printFormattedFile(StringRef Path) {
  376. if (<check for valid format>)
  377. return make_error<BadFileFormat>(Path);
  378. // print file contents.
  379. return Error::success();
  380. }
  381. Error values can be implicitly converted to bool: true for error, false for
  382. success, enabling the following idiom:
  383. .. code-block:: c++
  384. Error mayFail();
  385. Error foo() {
  386. if (auto Err = mayFail())
  387. return Err;
  388. // Success! We can proceed.
  389. ...
  390. For functions that can fail but need to return a value the ``Expected<T>``
  391. utility can be used. Values of this type can be constructed with either a
  392. ``T``, or an ``Error``. Expected<T> values are also implicitly convertible to
  393. boolean, but with the opposite convention to ``Error``: true for success, false
  394. for error. If success, the ``T`` value can be accessed via the dereference
  395. operator. If failure, the ``Error`` value can be extracted using the
  396. ``takeError()`` method. Idiomatic usage looks like:
  397. .. code-block:: c++
  398. Expected<FormattedFile> openFormattedFile(StringRef Path) {
  399. // If badly formatted, return an error.
  400. if (auto Err = checkFormat(Path))
  401. return std::move(Err);
  402. // Otherwise return a FormattedFile instance.
  403. return FormattedFile(Path);
  404. }
  405. Error processFormattedFile(StringRef Path) {
  406. // Try to open a formatted file
  407. if (auto FileOrErr = openFormattedFile(Path)) {
  408. // On success, grab a reference to the file and continue.
  409. auto &File = *FileOrErr;
  410. ...
  411. } else
  412. // On error, extract the Error value and return it.
  413. return FileOrErr.takeError();
  414. }
  415. If an ``Expected<T>`` value is in success mode then the ``takeError()`` method
  416. will return a success value. Using this fact, the above function can be
  417. rewritten as:
  418. .. code-block:: c++
  419. Error processFormattedFile(StringRef Path) {
  420. // Try to open a formatted file
  421. auto FileOrErr = openFormattedFile(Path);
  422. if (auto Err = FileOrErr.takeError())
  423. // On error, extract the Error value and return it.
  424. return Err;
  425. // On success, grab a reference to the file and continue.
  426. auto &File = *FileOrErr;
  427. ...
  428. }
  429. This second form is often more readable for functions that involve multiple
  430. ``Expected<T>`` values as it limits the indentation required.
  431. All ``Error`` instances, whether success or failure, must be either checked or
  432. moved from (via ``std::move`` or a return) before they are destructed.
  433. Accidentally discarding an unchecked error will cause a program abort at the
  434. point where the unchecked value's destructor is run, making it easy to identify
  435. and fix violations of this rule.
  436. Success values are considered checked once they have been tested (by invoking
  437. the boolean conversion operator):
  438. .. code-block:: c++
  439. if (auto Err = mayFail(...))
  440. return Err; // Failure value - move error to caller.
  441. // Safe to continue: Err was checked.
  442. In contrast, the following code will always cause an abort, even if ``mayFail``
  443. returns a success value:
  444. .. code-block:: c++
  445. mayFail();
  446. // Program will always abort here, even if mayFail() returns Success, since
  447. // the value is not checked.
  448. Failure values are considered checked once a handler for the error type has
  449. been activated:
  450. .. code-block:: c++
  451. handleErrors(
  452. processFormattedFile(...),
  453. [](const BadFileFormat &BFF) {
  454. report("Unable to process " + BFF.Path + ": bad format");
  455. },
  456. [](const FileNotFound &FNF) {
  457. report("File not found " + FNF.Path);
  458. });
  459. The ``handleErrors`` function takes an error as its first argument, followed by
  460. a variadic list of "handlers", each of which must be a callable type (a
  461. function, lambda, or class with a call operator) with one argument. The
  462. ``handleErrors`` function will visit each handler in the sequence and check its
  463. argument type against the dynamic type of the error, running the first handler
  464. that matches. This is the same decision process that is used decide which catch
  465. clause to run for a C++ exception.
  466. Since the list of handlers passed to ``handleErrors`` may not cover every error
  467. type that can occur, the ``handleErrors`` function also returns an Error value
  468. that must be checked or propagated. If the error value that is passed to
  469. ``handleErrors`` does not match any of the handlers it will be returned from
  470. handleErrors. Idiomatic use of ``handleErrors`` thus looks like:
  471. .. code-block:: c++
  472. if (auto Err =
  473. handleErrors(
  474. processFormattedFile(...),
  475. [](const BadFileFormat &BFF) {
  476. report("Unable to process " + BFF.Path + ": bad format");
  477. },
  478. [](const FileNotFound &FNF) {
  479. report("File not found " + FNF.Path);
  480. }))
  481. return Err;
  482. In cases where you truly know that the handler list is exhaustive the
  483. ``handleAllErrors`` function can be used instead. This is identical to
  484. ``handleErrors`` except that it will terminate the program if an unhandled
  485. error is passed in, and can therefore return void. The ``handleAllErrors``
  486. function should generally be avoided: the introduction of a new error type
  487. elsewhere in the program can easily turn a formerly exhaustive list of errors
  488. into a non-exhaustive list, risking unexpected program termination. Where
  489. possible, use handleErrors and propagate unknown errors up the stack instead.
  490. For tool code, where errors can be handled by printing an error message then
  491. exiting with an error code, the :ref:`ExitOnError <err_exitonerr>` utility
  492. may be a better choice than handleErrors, as it simplifies control flow when
  493. calling fallible functions.
  494. In situations where it is known that a particular call to a fallible function
  495. will always succeed (for example, a call to a function that can only fail on a
  496. subset of inputs with an input that is known to be safe) the
  497. :ref:`cantFail <err_cantfail>` functions can be used to remove the error type,
  498. simplifying control flow.
  499. StringError
  500. """""""""""
  501. Many kinds of errors have no recovery strategy, the only action that can be
  502. taken is to report them to the user so that the user can attempt to fix the
  503. environment. In this case representing the error as a string makes perfect
  504. sense. LLVM provides the ``StringError`` class for this purpose. It takes two
  505. arguments: A string error message, and an equivalent ``std::error_code`` for
  506. interoperability:
  507. .. code-block:: c++
  508. make_error<StringError>("Bad executable",
  509. make_error_code(errc::executable_format_error"));
  510. If you're certain that the error you're building will never need to be converted
  511. to a ``std::error_code`` you can use the ``inconvertibleErrorCode()`` function:
  512. .. code-block:: c++
  513. make_error<StringError>("Bad executable", inconvertibleErrorCode());
  514. This should be done only after careful consideration. If any attempt is made to
  515. convert this error to a ``std::error_code`` it will trigger immediate program
  516. termination. Unless you are certain that your errors will not need
  517. interoperability you should look for an existing ``std::error_code`` that you
  518. can convert to, and even (as painful as it is) consider introducing a new one as
  519. a stopgap measure.
  520. Interoperability with std::error_code and ErrorOr
  521. """""""""""""""""""""""""""""""""""""""""""""""""
  522. Many existing LLVM APIs use ``std::error_code`` and its partner ``ErrorOr<T>``
  523. (which plays the same role as ``Expected<T>``, but wraps a ``std::error_code``
  524. rather than an ``Error``). The infectious nature of error types means that an
  525. attempt to change one of these functions to return ``Error`` or ``Expected<T>``
  526. instead often results in an avalanche of changes to callers, callers of callers,
  527. and so on. (The first such attempt, returning an ``Error`` from
  528. MachOObjectFile's constructor, was abandoned after the diff reached 3000 lines,
  529. impacted half a dozen libraries, and was still growing).
  530. To solve this problem, the ``Error``/``std::error_code`` interoperability requirement was
  531. introduced. Two pairs of functions allow any ``Error`` value to be converted to a
  532. ``std::error_code``, any ``Expected<T>`` to be converted to an ``ErrorOr<T>``, and vice
  533. versa:
  534. .. code-block:: c++
  535. std::error_code errorToErrorCode(Error Err);
  536. Error errorCodeToError(std::error_code EC);
  537. template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> TOrErr);
  538. template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> TOrEC);
  539. Using these APIs it is easy to make surgical patches that update individual
  540. functions from ``std::error_code`` to ``Error``, and from ``ErrorOr<T>`` to
  541. ``Expected<T>``.
  542. Returning Errors from error handlers
  543. """"""""""""""""""""""""""""""""""""
  544. Error recovery attempts may themselves fail. For that reason, ``handleErrors``
  545. actually recognises three different forms of handler signature:
  546. .. code-block:: c++
  547. // Error must be handled, no new errors produced:
  548. void(UserDefinedError &E);
  549. // Error must be handled, new errors can be produced:
  550. Error(UserDefinedError &E);
  551. // Original error can be inspected, then re-wrapped and returned (or a new
  552. // error can be produced):
  553. Error(std::unique_ptr<UserDefinedError> E);
  554. Any error returned from a handler will be returned from the ``handleErrors``
  555. function so that it can be handled itself, or propagated up the stack.
  556. .. _err_exitonerr:
  557. Using ExitOnError to simplify tool code
  558. """""""""""""""""""""""""""""""""""""""
  559. Library code should never call ``exit`` for a recoverable error, however in tool
  560. code (especially command line tools) this can be a reasonable approach. Calling
  561. ``exit`` upon encountering an error dramatically simplifies control flow as the
  562. error no longer needs to be propagated up the stack. This allows code to be
  563. written in straight-line style, as long as each fallible call is wrapped in a
  564. check and call to exit. The ``ExitOnError`` class supports this pattern by
  565. providing call operators that inspect ``Error`` values, stripping the error away
  566. in the success case and logging to ``stderr`` then exiting in the failure case.
  567. To use this class, declare a global ``ExitOnError`` variable in your program:
  568. .. code-block:: c++
  569. ExitOnError ExitOnErr;
  570. Calls to fallible functions can then be wrapped with a call to ``ExitOnErr``,
  571. turning them into non-failing calls:
  572. .. code-block:: c++
  573. Error mayFail();
  574. Expected<int> mayFail2();
  575. void foo() {
  576. ExitOnErr(mayFail());
  577. int X = ExitOnErr(mayFail2());
  578. }
  579. On failure, the error's log message will be written to ``stderr``, optionally
  580. preceded by a string "banner" that can be set by calling the setBanner method. A
  581. mapping can also be supplied from ``Error`` values to exit codes using the
  582. ``setExitCodeMapper`` method:
  583. .. code-block:: c++
  584. int main(int argc, char *argv[]) {
  585. ExitOnErr.setBanner(std::string(argv[0]) + " error:");
  586. ExitOnErr.setExitCodeMapper(
  587. [](const Error &Err) {
  588. if (Err.isA<BadFileFormat>())
  589. return 2;
  590. return 1;
  591. });
  592. Use ``ExitOnError`` in your tool code where possible as it can greatly improve
  593. readability.
  594. .. _err_cantfail:
  595. Using cantFail to simplify safe callsites
  596. """""""""""""""""""""""""""""""""""""""""
  597. Some functions may only fail for a subset of their inputs, so calls using known
  598. safe inputs can be assumed to succeed.
  599. The cantFail functions encapsulate this by wrapping an assertion that their
  600. argument is a success value and, in the case of Expected<T>, unwrapping the
  601. T value:
  602. .. code-block:: c++
  603. Error onlyFailsForSomeXValues(int X);
  604. Expected<int> onlyFailsForSomeXValues2(int X);
  605. void foo() {
  606. cantFail(onlyFailsForSomeXValues(KnownSafeValue));
  607. int Y = cantFail(onlyFailsForSomeXValues2(KnownSafeValue));
  608. ...
  609. }
  610. Like the ExitOnError utility, cantFail simplifies control flow. Their treatment
  611. of error cases is very different however: Where ExitOnError is guaranteed to
  612. terminate the program on an error input, cantFile simply asserts that the result
  613. is success. In debug builds this will result in an assertion failure if an error
  614. is encountered. In release builds the behavior of cantFail for failure values is
  615. undefined. As such, care must be taken in the use of cantFail: clients must be
  616. certain that a cantFail wrapped call really can not fail with the given
  617. arguments.
  618. Use of the cantFail functions should be rare in library code, but they are
  619. likely to be of more use in tool and unit-test code where inputs and/or
  620. mocked-up classes or functions may be known to be safe.
  621. Fallible constructors
  622. """""""""""""""""""""
  623. Some classes require resource acquisition or other complex initialization that
  624. can fail during construction. Unfortunately constructors can't return errors,
  625. and having clients test objects after they're constructed to ensure that they're
  626. valid is error prone as it's all too easy to forget the test. To work around
  627. this, use the named constructor idiom and return an ``Expected<T>``:
  628. .. code-block:: c++
  629. class Foo {
  630. public:
  631. static Expected<Foo> Create(Resource R1, Resource R2) {
  632. Error Err;
  633. Foo F(R1, R2, Err);
  634. if (Err)
  635. return std::move(Err);
  636. return std::move(F);
  637. }
  638. private:
  639. Foo(Resource R1, Resource R2, Error &Err) {
  640. ErrorAsOutParameter EAO(&Err);
  641. if (auto Err2 = R1.acquire()) {
  642. Err = std::move(Err2);
  643. return;
  644. }
  645. Err = R2.acquire();
  646. }
  647. };
  648. Here, the named constructor passes an ``Error`` by reference into the actual
  649. constructor, which the constructor can then use to return errors. The
  650. ``ErrorAsOutParameter`` utility sets the ``Error`` value's checked flag on entry
  651. to the constructor so that the error can be assigned to, then resets it on exit
  652. to force the client (the named constructor) to check the error.
  653. By using this idiom, clients attempting to construct a Foo receive either a
  654. well-formed Foo or an Error, never an object in an invalid state.
  655. Propagating and consuming errors based on types
  656. """""""""""""""""""""""""""""""""""""""""""""""
  657. In some contexts, certain types of error are known to be benign. For example,
  658. when walking an archive, some clients may be happy to skip over badly formatted
  659. object files rather than terminating the walk immediately. Skipping badly
  660. formatted objects could be achieved using an elaborate handler method, but the
  661. Error.h header provides two utilities that make this idiom much cleaner: the
  662. type inspection method, ``isA``, and the ``consumeError`` function:
  663. .. code-block:: c++
  664. Error walkArchive(Archive A) {
  665. for (unsigned I = 0; I != A.numMembers(); ++I) {
  666. auto ChildOrErr = A.getMember(I);
  667. if (auto Err = ChildOrErr.takeError()) {
  668. if (Err.isA<BadFileFormat>())
  669. consumeError(std::move(Err))
  670. else
  671. return Err;
  672. }
  673. auto &Child = *ChildOrErr;
  674. // Use Child
  675. ...
  676. }
  677. return Error::success();
  678. }
  679. Concatenating Errors with joinErrors
  680. """"""""""""""""""""""""""""""""""""
  681. In the archive walking example above ``BadFileFormat`` errors are simply
  682. consumed and ignored. If the client had wanted report these errors after
  683. completing the walk over the archive they could use the ``joinErrors`` utility:
  684. .. code-block:: c++
  685. Error walkArchive(Archive A) {
  686. Error DeferredErrs = Error::success();
  687. for (unsigned I = 0; I != A.numMembers(); ++I) {
  688. auto ChildOrErr = A.getMember(I);
  689. if (auto Err = ChildOrErr.takeError())
  690. if (Err.isA<BadFileFormat>())
  691. DeferredErrs = joinErrors(std::move(DeferredErrs), std::move(Err));
  692. else
  693. return Err;
  694. auto &Child = *ChildOrErr;
  695. // Use Child
  696. ...
  697. }
  698. return DeferredErrs;
  699. }
  700. The ``joinErrors`` routine builds a special error type called ``ErrorList``,
  701. which holds a list of user defined errors. The ``handleErrors`` routine
  702. recognizes this type and will attempt to handle each of the contained errors in
  703. order. If all contained errors can be handled, ``handleErrors`` will return
  704. ``Error::success()``, otherwise ``handleErrors`` will concatenate the remaining
  705. errors and return the resulting ``ErrorList``.
  706. Building fallible iterators and iterator ranges
  707. """""""""""""""""""""""""""""""""""""""""""""""
  708. The archive walking examples above retrieve archive members by index, however
  709. this requires considerable boiler-plate for iteration and error checking. We can
  710. clean this up by using ``Error`` with the "fallible iterator" pattern. The usual
  711. C++ iterator patterns do not allow for failure on increment, but we can
  712. incorporate support for it by having iterators hold an Error reference through
  713. which they can report failure. In this pattern, if an increment operation fails
  714. the failure is recorded via the Error reference and the iterator value is set to
  715. the end of the range in order to terminate the loop. This ensures that the
  716. dereference operation is safe anywhere that an ordinary iterator dereference
  717. would be safe (i.e. when the iterator is not equal to end). Where this pattern
  718. is followed (as in the ``llvm::object::Archive`` class) the result is much
  719. cleaner iteration idiom:
  720. .. code-block:: c++
  721. Error Err;
  722. for (auto &Child : Ar->children(Err)) {
  723. // Use Child - we only enter the loop when it's valid
  724. ...
  725. }
  726. // Check Err after the loop to ensure it didn't break due to an error.
  727. if (Err)
  728. return Err;
  729. .. _function_apis:
  730. More information on Error and its related utilities can be found in the
  731. Error.h header file.
  732. Passing functions and other callable objects
  733. --------------------------------------------
  734. Sometimes you may want a function to be passed a callback object. In order to
  735. support lambda expressions and other function objects, you should not use the
  736. traditional C approach of taking a function pointer and an opaque cookie:
  737. .. code-block:: c++
  738. void takeCallback(bool (*Callback)(Function *, void *), void *Cookie);
  739. Instead, use one of the following approaches:
  740. Function template
  741. ^^^^^^^^^^^^^^^^^
  742. If you don't mind putting the definition of your function into a header file,
  743. make it a function template that is templated on the callable type.
  744. .. code-block:: c++
  745. template<typename Callable>
  746. void takeCallback(Callable Callback) {
  747. Callback(1, 2, 3);
  748. }
  749. The ``function_ref`` class template
  750. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  751. The ``function_ref``
  752. (`doxygen <http://llvm.org/doxygen/classllvm_1_1function__ref_3_01Ret_07Params_8_8_8_08_4.html>`__) class
  753. template represents a reference to a callable object, templated over the type
  754. of the callable. This is a good choice for passing a callback to a function,
  755. if you don't need to hold onto the callback after the function returns. In this
  756. way, ``function_ref`` is to ``std::function`` as ``StringRef`` is to
  757. ``std::string``.
  758. ``function_ref<Ret(Param1, Param2, ...)>`` can be implicitly constructed from
  759. any callable object that can be called with arguments of type ``Param1``,
  760. ``Param2``, ..., and returns a value that can be converted to type ``Ret``.
  761. For example:
  762. .. code-block:: c++
  763. void visitBasicBlocks(Function *F, function_ref<bool (BasicBlock*)> Callback) {
  764. for (BasicBlock &BB : *F)
  765. if (Callback(&BB))
  766. return;
  767. }
  768. can be called using:
  769. .. code-block:: c++
  770. visitBasicBlocks(F, [&](BasicBlock *BB) {
  771. if (process(BB))
  772. return isEmpty(BB);
  773. return false;
  774. });
  775. Note that a ``function_ref`` object contains pointers to external memory, so it
  776. is not generally safe to store an instance of the class (unless you know that
  777. the external storage will not be freed). If you need this ability, consider
  778. using ``std::function``. ``function_ref`` is small enough that it should always
  779. be passed by value.
  780. .. _DEBUG:
  781. The ``LLVM_DEBUG()`` macro and ``-debug`` option
  782. ------------------------------------------------
  783. Often when working on your pass you will put a bunch of debugging printouts and
  784. other code into your pass. After you get it working, you want to remove it, but
  785. you may need it again in the future (to work out new bugs that you run across).
  786. Naturally, because of this, you don't want to delete the debug printouts, but
  787. you don't want them to always be noisy. A standard compromise is to comment
  788. them out, allowing you to enable them if you need them in the future.
  789. The ``llvm/Support/Debug.h`` (`doxygen
  790. <http://llvm.org/doxygen/Debug_8h_source.html>`__) file provides a macro named
  791. ``LLVM_DEBUG()`` that is a much nicer solution to this problem. Basically, you can
  792. put arbitrary code into the argument of the ``LLVM_DEBUG`` macro, and it is only
  793. executed if '``opt``' (or any other tool) is run with the '``-debug``' command
  794. line argument:
  795. .. code-block:: c++
  796. LLVM_DEBUG(dbgs() << "I am here!\n");
  797. Then you can run your pass like this:
  798. .. code-block:: none
  799. $ opt < a.bc > /dev/null -mypass
  800. <no output>
  801. $ opt < a.bc > /dev/null -mypass -debug
  802. I am here!
  803. Using the ``LLVM_DEBUG()`` macro instead of a home-brewed solution allows you to not
  804. have to create "yet another" command line option for the debug output for your
  805. pass. Note that ``LLVM_DEBUG()`` macros are disabled for non-asserts builds, so they
  806. do not cause a performance impact at all (for the same reason, they should also
  807. not contain side-effects!).
  808. One additional nice thing about the ``LLVM_DEBUG()`` macro is that you can enable or
  809. disable it directly in gdb. Just use "``set DebugFlag=0``" or "``set
  810. DebugFlag=1``" from the gdb if the program is running. If the program hasn't
  811. been started yet, you can always just run it with ``-debug``.
  812. .. _DEBUG_TYPE:
  813. Fine grained debug info with ``DEBUG_TYPE`` and the ``-debug-only`` option
  814. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  815. Sometimes you may find yourself in a situation where enabling ``-debug`` just
  816. turns on **too much** information (such as when working on the code generator).
  817. If you want to enable debug information with more fine-grained control, you
  818. should define the ``DEBUG_TYPE`` macro and use the ``-debug-only`` option as
  819. follows:
  820. .. code-block:: c++
  821. #define DEBUG_TYPE "foo"
  822. LLVM_DEBUG(dbgs() << "'foo' debug type\n");
  823. #undef DEBUG_TYPE
  824. #define DEBUG_TYPE "bar"
  825. LLVM_DEBUG(dbgs() << "'bar' debug type\n");
  826. #undef DEBUG_TYPE
  827. Then you can run your pass like this:
  828. .. code-block:: none
  829. $ opt < a.bc > /dev/null -mypass
  830. <no output>
  831. $ opt < a.bc > /dev/null -mypass -debug
  832. 'foo' debug type
  833. 'bar' debug type
  834. $ opt < a.bc > /dev/null -mypass -debug-only=foo
  835. 'foo' debug type
  836. $ opt < a.bc > /dev/null -mypass -debug-only=bar
  837. 'bar' debug type
  838. $ opt < a.bc > /dev/null -mypass -debug-only=foo,bar
  839. 'foo' debug type
  840. 'bar' debug type
  841. Of course, in practice, you should only set ``DEBUG_TYPE`` at the top of a file,
  842. to specify the debug type for the entire module. Be careful that you only do
  843. this after including Debug.h and not around any #include of headers. Also, you
  844. should use names more meaningful than "foo" and "bar", because there is no
  845. system in place to ensure that names do not conflict. If two different modules
  846. use the same string, they will all be turned on when the name is specified.
  847. This allows, for example, all debug information for instruction scheduling to be
  848. enabled with ``-debug-only=InstrSched``, even if the source lives in multiple
  849. files. The name must not include a comma (,) as that is used to separate the
  850. arguments of the ``-debug-only`` option.
  851. For performance reasons, -debug-only is not available in optimized build
  852. (``--enable-optimized``) of LLVM.
  853. The ``DEBUG_WITH_TYPE`` macro is also available for situations where you would
  854. like to set ``DEBUG_TYPE``, but only for one specific ``DEBUG`` statement. It
  855. takes an additional first parameter, which is the type to use. For example, the
  856. preceding example could be written as:
  857. .. code-block:: c++
  858. DEBUG_WITH_TYPE("foo", dbgs() << "'foo' debug type\n");
  859. DEBUG_WITH_TYPE("bar", dbgs() << "'bar' debug type\n");
  860. .. _Statistic:
  861. The ``Statistic`` class & ``-stats`` option
  862. -------------------------------------------
  863. The ``llvm/ADT/Statistic.h`` (`doxygen
  864. <http://llvm.org/doxygen/Statistic_8h_source.html>`__) file provides a class
  865. named ``Statistic`` that is used as a unified way to keep track of what the LLVM
  866. compiler is doing and how effective various optimizations are. It is useful to
  867. see what optimizations are contributing to making a particular program run
  868. faster.
  869. Often you may run your pass on some big program, and you're interested to see
  870. how many times it makes a certain transformation. Although you can do this with
  871. hand inspection, or some ad-hoc method, this is a real pain and not very useful
  872. for big programs. Using the ``Statistic`` class makes it very easy to keep
  873. track of this information, and the calculated information is presented in a
  874. uniform manner with the rest of the passes being executed.
  875. There are many examples of ``Statistic`` uses, but the basics of using it are as
  876. follows:
  877. Define your statistic like this:
  878. .. code-block:: c++
  879. #define DEBUG_TYPE "mypassname" // This goes before any #includes.
  880. STATISTIC(NumXForms, "The # of times I did stuff");
  881. The ``STATISTIC`` macro defines a static variable, whose name is specified by
  882. the first argument. The pass name is taken from the ``DEBUG_TYPE`` macro, and
  883. the description is taken from the second argument. The variable defined
  884. ("NumXForms" in this case) acts like an unsigned integer.
  885. Whenever you make a transformation, bump the counter:
  886. .. code-block:: c++
  887. ++NumXForms; // I did stuff!
  888. That's all you have to do. To get '``opt``' to print out the statistics
  889. gathered, use the '``-stats``' option:
  890. .. code-block:: none
  891. $ opt -stats -mypassname < program.bc > /dev/null
  892. ... statistics output ...
  893. Note that in order to use the '``-stats``' option, LLVM must be
  894. compiled with assertions enabled.
  895. When running ``opt`` on a C file from the SPEC benchmark suite, it gives a
  896. report that looks like this:
  897. .. code-block:: none
  898. 7646 bitcodewriter - Number of normal instructions
  899. 725 bitcodewriter - Number of oversized instructions
  900. 129996 bitcodewriter - Number of bitcode bytes written
  901. 2817 raise - Number of insts DCEd or constprop'd
  902. 3213 raise - Number of cast-of-self removed
  903. 5046 raise - Number of expression trees converted
  904. 75 raise - Number of other getelementptr's formed
  905. 138 raise - Number of load/store peepholes
  906. 42 deadtypeelim - Number of unused typenames removed from symtab
  907. 392 funcresolve - Number of varargs functions resolved
  908. 27 globaldce - Number of global variables removed
  909. 2 adce - Number of basic blocks removed
  910. 134 cee - Number of branches revectored
  911. 49 cee - Number of setcc instruction eliminated
  912. 532 gcse - Number of loads removed
  913. 2919 gcse - Number of instructions removed
  914. 86 indvars - Number of canonical indvars added
  915. 87 indvars - Number of aux indvars removed
  916. 25 instcombine - Number of dead inst eliminate
  917. 434 instcombine - Number of insts combined
  918. 248 licm - Number of load insts hoisted
  919. 1298 licm - Number of insts hoisted to a loop pre-header
  920. 3 licm - Number of insts hoisted to multiple loop preds (bad, no loop pre-header)
  921. 75 mem2reg - Number of alloca's promoted
  922. 1444 cfgsimplify - Number of blocks simplified
  923. Obviously, with so many optimizations, having a unified framework for this stuff
  924. is very nice. Making your pass fit well into the framework makes it more
  925. maintainable and useful.
  926. .. _DebugCounters:
  927. Adding debug counters to aid in debugging your code
  928. ---------------------------------------------------
  929. Sometimes, when writing new passes, or trying to track down bugs, it
  930. is useful to be able to control whether certain things in your pass
  931. happen or not. For example, there are times the minimization tooling
  932. can only easily give you large testcases. You would like to narrow
  933. your bug down to a specific transformation happening or not happening,
  934. automatically, using bisection. This is where debug counters help.
  935. They provide a framework for making parts of your code only execute a
  936. certain number of times.
  937. The ``llvm/Support/DebugCounter.h`` (`doxygen
  938. <http://llvm.org/doxygen/DebugCounter_8h_source.html>`__) file
  939. provides a class named ``DebugCounter`` that can be used to create
  940. command line counter options that control execution of parts of your code.
  941. Define your DebugCounter like this:
  942. .. code-block:: c++
  943. DEBUG_COUNTER(DeleteAnInstruction, "passname-delete-instruction",
  944. "Controls which instructions get delete");
  945. The ``DEBUG_COUNTER`` macro defines a static variable, whose name
  946. is specified by the first argument. The name of the counter
  947. (which is used on the command line) is specified by the second
  948. argument, and the description used in the help is specified by the
  949. third argument.
  950. Whatever code you want that control, use ``DebugCounter::shouldExecute`` to control it.
  951. .. code-block:: c++
  952. if (DebugCounter::shouldExecute(DeleteAnInstruction))
  953. I->eraseFromParent();
  954. That's all you have to do. Now, using opt, you can control when this code triggers using
  955. the '``--debug-counter``' option. There are two counters provided, ``skip`` and ``count``.
  956. ``skip`` is the number of times to skip execution of the codepath. ``count`` is the number
  957. of times, once we are done skipping, to execute the codepath.
  958. .. code-block:: none
  959. $ opt --debug-counter=passname-delete-instruction-skip=1,passname-delete-instruction-count=2 -passname
  960. This will skip the above code the first time we hit it, then execute it twice, then skip the rest of the executions.
  961. So if executed on the following code:
  962. .. code-block:: llvm
  963. %1 = add i32 %a, %b
  964. %2 = add i32 %a, %b
  965. %3 = add i32 %a, %b
  966. %4 = add i32 %a, %b
  967. It would delete number ``%2`` and ``%3``.
  968. A utility is provided in `utils/bisect-skip-count` to binary search
  969. skip and count arguments. It can be used to automatically minimize the
  970. skip and count for a debug-counter variable.
  971. .. _ViewGraph:
  972. Viewing graphs while debugging code
  973. -----------------------------------
  974. Several of the important data structures in LLVM are graphs: for example CFGs
  975. made out of LLVM :ref:`BasicBlocks <BasicBlock>`, CFGs made out of LLVM
  976. :ref:`MachineBasicBlocks <MachineBasicBlock>`, and :ref:`Instruction Selection
  977. DAGs <SelectionDAG>`. In many cases, while debugging various parts of the
  978. compiler, it is nice to instantly visualize these graphs.
  979. LLVM provides several callbacks that are available in a debug build to do
  980. exactly that. If you call the ``Function::viewCFG()`` method, for example, the
  981. current LLVM tool will pop up a window containing the CFG for the function where
  982. each basic block is a node in the graph, and each node contains the instructions
  983. in the block. Similarly, there also exists ``Function::viewCFGOnly()`` (does
  984. not include the instructions), the ``MachineFunction::viewCFG()`` and
  985. ``MachineFunction::viewCFGOnly()``, and the ``SelectionDAG::viewGraph()``
  986. methods. Within GDB, for example, you can usually use something like ``call
  987. DAG.viewGraph()`` to pop up a window. Alternatively, you can sprinkle calls to
  988. these functions in your code in places you want to debug.
  989. Getting this to work requires a small amount of setup. On Unix systems
  990. with X11, install the `graphviz <http://www.graphviz.org>`_ toolkit, and make
  991. sure 'dot' and 'gv' are in your path. If you are running on Mac OS X, download
  992. and install the Mac OS X `Graphviz program
  993. <http://www.pixelglow.com/graphviz/>`_ and add
  994. ``/Applications/Graphviz.app/Contents/MacOS/`` (or wherever you install it) to
  995. your path. The programs need not be present when configuring, building or
  996. running LLVM and can simply be installed when needed during an active debug
  997. session.
  998. ``SelectionDAG`` has been extended to make it easier to locate *interesting*
  999. nodes in large complex graphs. From gdb, if you ``call DAG.setGraphColor(node,
  1000. "color")``, then the next ``call DAG.viewGraph()`` would highlight the node in
  1001. the specified color (choices of colors can be found at `colors
  1002. <http://www.graphviz.org/doc/info/colors.html>`_.) More complex node attributes
  1003. can be provided with ``call DAG.setGraphAttrs(node, "attributes")`` (choices can
  1004. be found at `Graph attributes <http://www.graphviz.org/doc/info/attrs.html>`_.)
  1005. If you want to restart and clear all the current graph attributes, then you can
  1006. ``call DAG.clearGraphAttrs()``.
  1007. Note that graph visualization features are compiled out of Release builds to
  1008. reduce file size. This means that you need a Debug+Asserts or Release+Asserts
  1009. build to use these features.
  1010. .. _datastructure:
  1011. Picking the Right Data Structure for a Task
  1012. ===========================================
  1013. LLVM has a plethora of data structures in the ``llvm/ADT/`` directory, and we
  1014. commonly use STL data structures. This section describes the trade-offs you
  1015. should consider when you pick one.
  1016. The first step is a choose your own adventure: do you want a sequential
  1017. container, a set-like container, or a map-like container? The most important
  1018. thing when choosing a container is the algorithmic properties of how you plan to
  1019. access the container. Based on that, you should use:
  1020. * a :ref:`map-like <ds_map>` container if you need efficient look-up of a
  1021. value based on another value. Map-like containers also support efficient
  1022. queries for containment (whether a key is in the map). Map-like containers
  1023. generally do not support efficient reverse mapping (values to keys). If you
  1024. need that, use two maps. Some map-like containers also support efficient
  1025. iteration through the keys in sorted order. Map-like containers are the most
  1026. expensive sort, only use them if you need one of these capabilities.
  1027. * a :ref:`set-like <ds_set>` container if you need to put a bunch of stuff into
  1028. a container that automatically eliminates duplicates. Some set-like
  1029. containers support efficient iteration through the elements in sorted order.
  1030. Set-like containers are more expensive than sequential containers.
  1031. * a :ref:`sequential <ds_sequential>` container provides the most efficient way
  1032. to add elements and keeps track of the order they are added to the collection.
  1033. They permit duplicates and support efficient iteration, but do not support
  1034. efficient look-up based on a key.
  1035. * a :ref:`string <ds_string>` container is a specialized sequential container or
  1036. reference structure that is used for character or byte arrays.
  1037. * a :ref:`bit <ds_bit>` container provides an efficient way to store and
  1038. perform set operations on sets of numeric id's, while automatically
  1039. eliminating duplicates. Bit containers require a maximum of 1 bit for each
  1040. identifier you want to store.
  1041. Once the proper category of container is determined, you can fine tune the
  1042. memory use, constant factors, and cache behaviors of access by intelligently
  1043. picking a member of the category. Note that constant factors and cache behavior
  1044. can be a big deal. If you have a vector that usually only contains a few
  1045. elements (but could contain many), for example, it's much better to use
  1046. :ref:`SmallVector <dss_smallvector>` than :ref:`vector <dss_vector>`. Doing so
  1047. avoids (relatively) expensive malloc/free calls, which dwarf the cost of adding
  1048. the elements to the container.
  1049. .. _ds_sequential:
  1050. Sequential Containers (std::vector, std::list, etc)
  1051. ---------------------------------------------------
  1052. There are a variety of sequential containers available for you, based on your
  1053. needs. Pick the first in this section that will do what you want.
  1054. .. _dss_arrayref:
  1055. llvm/ADT/ArrayRef.h
  1056. ^^^^^^^^^^^^^^^^^^^
  1057. The ``llvm::ArrayRef`` class is the preferred class to use in an interface that
  1058. accepts a sequential list of elements in memory and just reads from them. By
  1059. taking an ``ArrayRef``, the API can be passed a fixed size array, an
  1060. ``std::vector``, an ``llvm::SmallVector`` and anything else that is contiguous
  1061. in memory.
  1062. .. _dss_fixedarrays:
  1063. Fixed Size Arrays
  1064. ^^^^^^^^^^^^^^^^^
  1065. Fixed size arrays are very simple and very fast. They are good if you know
  1066. exactly how many elements you have, or you have a (low) upper bound on how many
  1067. you have.
  1068. .. _dss_heaparrays:
  1069. Heap Allocated Arrays
  1070. ^^^^^^^^^^^^^^^^^^^^^
  1071. Heap allocated arrays (``new[]`` + ``delete[]``) are also simple. They are good
  1072. if the number of elements is variable, if you know how many elements you will
  1073. need before the array is allocated, and if the array is usually large (if not,
  1074. consider a :ref:`SmallVector <dss_smallvector>`). The cost of a heap allocated
  1075. array is the cost of the new/delete (aka malloc/free). Also note that if you
  1076. are allocating an array of a type with a constructor, the constructor and
  1077. destructors will be run for every element in the array (re-sizable vectors only
  1078. construct those elements actually used).
  1079. .. _dss_tinyptrvector:
  1080. llvm/ADT/TinyPtrVector.h
  1081. ^^^^^^^^^^^^^^^^^^^^^^^^
  1082. ``TinyPtrVector<Type>`` is a highly specialized collection class that is
  1083. optimized to avoid allocation in the case when a vector has zero or one
  1084. elements. It has two major restrictions: 1) it can only hold values of pointer
  1085. type, and 2) it cannot hold a null pointer.
  1086. Since this container is highly specialized, it is rarely used.
  1087. .. _dss_smallvector:
  1088. llvm/ADT/SmallVector.h
  1089. ^^^^^^^^^^^^^^^^^^^^^^
  1090. ``SmallVector<Type, N>`` is a simple class that looks and smells just like
  1091. ``vector<Type>``: it supports efficient iteration, lays out elements in memory
  1092. order (so you can do pointer arithmetic between elements), supports efficient
  1093. push_back/pop_back operations, supports efficient random access to its elements,
  1094. etc.
  1095. The main advantage of SmallVector is that it allocates space for some number of
  1096. elements (N) **in the object itself**. Because of this, if the SmallVector is
  1097. dynamically smaller than N, no malloc is performed. This can be a big win in
  1098. cases where the malloc/free call is far more expensive than the code that
  1099. fiddles around with the elements.
  1100. This is good for vectors that are "usually small" (e.g. the number of
  1101. predecessors/successors of a block is usually less than 8). On the other hand,
  1102. this makes the size of the SmallVector itself large, so you don't want to
  1103. allocate lots of them (doing so will waste a lot of space). As such,
  1104. SmallVectors are most useful when on the stack.
  1105. SmallVector also provides a nice portable and efficient replacement for
  1106. ``alloca``.
  1107. SmallVector has grown a few other minor advantages over std::vector, causing
  1108. ``SmallVector<Type, 0>`` to be preferred over ``std::vector<Type>``.
  1109. #. std::vector is exception-safe, and some implementations have pessimizations
  1110. that copy elements when SmallVector would move them.
  1111. #. SmallVector understands ``isPodLike<Type>`` and uses realloc aggressively.
  1112. #. Many LLVM APIs take a SmallVectorImpl as an out parameter (see the note
  1113. below).
  1114. #. SmallVector with N equal to 0 is smaller than std::vector on 64-bit
  1115. platforms, since it uses ``unsigned`` (instead of ``void*``) for its size
  1116. and capacity.
  1117. .. note::
  1118. Prefer to use ``SmallVectorImpl<T>`` as a parameter type.
  1119. In APIs that don't care about the "small size" (most?), prefer to use
  1120. the ``SmallVectorImpl<T>`` class, which is basically just the "vector
  1121. header" (and methods) without the elements allocated after it. Note that
  1122. ``SmallVector<T, N>`` inherits from ``SmallVectorImpl<T>`` so the
  1123. conversion is implicit and costs nothing. E.g.
  1124. .. code-block:: c++
  1125. // BAD: Clients cannot pass e.g. SmallVector<Foo, 4>.
  1126. hardcodedSmallSize(SmallVector<Foo, 2> &Out);
  1127. // GOOD: Clients can pass any SmallVector<Foo, N>.
  1128. allowsAnySmallSize(SmallVectorImpl<Foo> &Out);
  1129. void someFunc() {
  1130. SmallVector<Foo, 8> Vec;
  1131. hardcodedSmallSize(Vec); // Error.
  1132. allowsAnySmallSize(Vec); // Works.
  1133. }
  1134. Even though it has "``Impl``" in the name, this is so widely used that
  1135. it really isn't "private to the implementation" anymore. A name like
  1136. ``SmallVectorHeader`` would be more appropriate.
  1137. .. _dss_vector:
  1138. <vector>
  1139. ^^^^^^^^
  1140. ``std::vector<T>`` is well loved and respected. However, ``SmallVector<T, 0>``
  1141. is often a better option due to the advantages listed above. std::vector is
  1142. still useful when you need to store more than ``UINT32_MAX`` elements or when
  1143. interfacing with code that expects vectors :).
  1144. One worthwhile note about std::vector: avoid code like this:
  1145. .. code-block:: c++
  1146. for ( ... ) {
  1147. std::vector<foo> V;
  1148. // make use of V.
  1149. }
  1150. Instead, write this as:
  1151. .. code-block:: c++
  1152. std::vector<foo> V;
  1153. for ( ... ) {
  1154. // make use of V.
  1155. V.clear();
  1156. }
  1157. Doing so will save (at least) one heap allocation and free per iteration of the
  1158. loop.
  1159. .. _dss_deque:
  1160. <deque>
  1161. ^^^^^^^
  1162. ``std::deque`` is, in some senses, a generalized version of ``std::vector``.
  1163. Like ``std::vector``, it provides constant time random access and other similar
  1164. properties, but it also provides efficient access to the front of the list. It
  1165. does not guarantee continuity of elements within memory.
  1166. In exchange for this extra flexibility, ``std::deque`` has significantly higher
  1167. constant factor costs than ``std::vector``. If possible, use ``std::vector`` or
  1168. something cheaper.
  1169. .. _dss_list:
  1170. <list>
  1171. ^^^^^^
  1172. ``std::list`` is an extremely inefficient class that is rarely useful. It
  1173. performs a heap allocation for every element inserted into it, thus having an
  1174. extremely high constant factor, particularly for small data types.
  1175. ``std::list`` also only supports bidirectional iteration, not random access
  1176. iteration.
  1177. In exchange for this high cost, std::list supports efficient access to both ends
  1178. of the list (like ``std::deque``, but unlike ``std::vector`` or
  1179. ``SmallVector``). In addition, the iterator invalidation characteristics of
  1180. std::list are stronger than that of a vector class: inserting or removing an
  1181. element into the list does not invalidate iterator or pointers to other elements
  1182. in the list.
  1183. .. _dss_ilist:
  1184. llvm/ADT/ilist.h
  1185. ^^^^^^^^^^^^^^^^
  1186. ``ilist<T>`` implements an 'intrusive' doubly-linked list. It is intrusive,
  1187. because it requires the element to store and provide access to the prev/next
  1188. pointers for the list.
  1189. ``ilist`` has the same drawbacks as ``std::list``, and additionally requires an
  1190. ``ilist_traits`` implementation for the element type, but it provides some novel
  1191. characteristics. In particular, it can efficiently store polymorphic objects,
  1192. the traits class is informed when an element is inserted or removed from the
  1193. list, and ``ilist``\ s are guaranteed to support a constant-time splice
  1194. operation.
  1195. These properties are exactly what we want for things like ``Instruction``\ s and
  1196. basic blocks, which is why these are implemented with ``ilist``\ s.
  1197. Related classes of interest are explained in the following subsections:
  1198. * :ref:`ilist_traits <dss_ilist_traits>`
  1199. * :ref:`iplist <dss_iplist>`
  1200. * :ref:`llvm/ADT/ilist_node.h <dss_ilist_node>`
  1201. * :ref:`Sentinels <dss_ilist_sentinel>`
  1202. .. _dss_packedvector:
  1203. llvm/ADT/PackedVector.h
  1204. ^^^^^^^^^^^^^^^^^^^^^^^
  1205. Useful for storing a vector of values using only a few number of bits for each
  1206. value. Apart from the standard operations of a vector-like container, it can
  1207. also perform an 'or' set operation.
  1208. For example:
  1209. .. code-block:: c++
  1210. enum State {
  1211. None = 0x0,
  1212. FirstCondition = 0x1,
  1213. SecondCondition = 0x2,
  1214. Both = 0x3
  1215. };
  1216. State get() {
  1217. PackedVector<State, 2> Vec1;
  1218. Vec1.push_back(FirstCondition);
  1219. PackedVector<State, 2> Vec2;
  1220. Vec2.push_back(SecondCondition);
  1221. Vec1 |= Vec2;
  1222. return Vec1[0]; // returns 'Both'.
  1223. }
  1224. .. _dss_ilist_traits:
  1225. ilist_traits
  1226. ^^^^^^^^^^^^
  1227. ``ilist_traits<T>`` is ``ilist<T>``'s customization mechanism. ``iplist<T>``
  1228. (and consequently ``ilist<T>``) publicly derive from this traits class.
  1229. .. _dss_iplist:
  1230. iplist
  1231. ^^^^^^
  1232. ``iplist<T>`` is ``ilist<T>``'s base and as such supports a slightly narrower
  1233. interface. Notably, inserters from ``T&`` are absent.
  1234. ``ilist_traits<T>`` is a public base of this class and can be used for a wide
  1235. variety of customizations.
  1236. .. _dss_ilist_node:
  1237. llvm/ADT/ilist_node.h
  1238. ^^^^^^^^^^^^^^^^^^^^^
  1239. ``ilist_node<T>`` implements the forward and backward links that are expected
  1240. by the ``ilist<T>`` (and analogous containers) in the default manner.
  1241. ``ilist_node<T>``\ s are meant to be embedded in the node type ``T``, usually
  1242. ``T`` publicly derives from ``ilist_node<T>``.
  1243. .. _dss_ilist_sentinel:
  1244. Sentinels
  1245. ^^^^^^^^^
  1246. ``ilist``\ s have another specialty that must be considered. To be a good
  1247. citizen in the C++ ecosystem, it needs to support the standard container
  1248. operations, such as ``begin`` and ``end`` iterators, etc. Also, the
  1249. ``operator--`` must work correctly on the ``end`` iterator in the case of
  1250. non-empty ``ilist``\ s.
  1251. The only sensible solution to this problem is to allocate a so-called *sentinel*
  1252. along with the intrusive list, which serves as the ``end`` iterator, providing
  1253. the back-link to the last element. However conforming to the C++ convention it
  1254. is illegal to ``operator++`` beyond the sentinel and it also must not be
  1255. dereferenced.
  1256. These constraints allow for some implementation freedom to the ``ilist`` how to
  1257. allocate and store the sentinel. The corresponding policy is dictated by
  1258. ``ilist_traits<T>``. By default a ``T`` gets heap-allocated whenever the need
  1259. for a sentinel arises.
  1260. While the default policy is sufficient in most cases, it may break down when
  1261. ``T`` does not provide a default constructor. Also, in the case of many
  1262. instances of ``ilist``\ s, the memory overhead of the associated sentinels is
  1263. wasted. To alleviate the situation with numerous and voluminous
  1264. ``T``-sentinels, sometimes a trick is employed, leading to *ghostly sentinels*.
  1265. Ghostly sentinels are obtained by specially-crafted ``ilist_traits<T>`` which
  1266. superpose the sentinel with the ``ilist`` instance in memory. Pointer
  1267. arithmetic is used to obtain the sentinel, which is relative to the ``ilist``'s
  1268. ``this`` pointer. The ``ilist`` is augmented by an extra pointer, which serves
  1269. as the back-link of the sentinel. This is the only field in the ghostly
  1270. sentinel which can be legally accessed.
  1271. .. _dss_other:
  1272. Other Sequential Container options
  1273. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1274. Other STL containers are available, such as ``std::string``.
  1275. There are also various STL adapter classes such as ``std::queue``,
  1276. ``std::priority_queue``, ``std::stack``, etc. These provide simplified access
  1277. to an underlying container but don't affect the cost of the container itself.
  1278. .. _ds_string:
  1279. String-like containers
  1280. ----------------------
  1281. There are a variety of ways to pass around and use strings in C and C++, and
  1282. LLVM adds a few new options to choose from. Pick the first option on this list
  1283. that will do what you need, they are ordered according to their relative cost.
  1284. Note that it is generally preferred to *not* pass strings around as ``const
  1285. char*``'s. These have a number of problems, including the fact that they
  1286. cannot represent embedded nul ("\0") characters, and do not have a length
  1287. available efficiently. The general replacement for '``const char*``' is
  1288. StringRef.
  1289. For more information on choosing string containers for APIs, please see
  1290. :ref:`Passing Strings <string_apis>`.
  1291. .. _dss_stringref:
  1292. llvm/ADT/StringRef.h
  1293. ^^^^^^^^^^^^^^^^^^^^
  1294. The StringRef class is a simple value class that contains a pointer to a
  1295. character and a length, and is quite related to the :ref:`ArrayRef
  1296. <dss_arrayref>` class (but specialized for arrays of characters). Because
  1297. StringRef carries a length with it, it safely handles strings with embedded nul
  1298. characters in it, getting the length does not require a strlen call, and it even
  1299. has very convenient APIs for slicing and dicing the character range that it
  1300. represents.
  1301. StringRef is ideal for passing simple strings around that are known to be live,
  1302. either because they are C string literals, std::string, a C array, or a
  1303. SmallVector. Each of these cases has an efficient implicit conversion to
  1304. StringRef, which doesn't result in a dynamic strlen being executed.
  1305. StringRef has a few major limitations which make more powerful string containers
  1306. useful:
  1307. #. You cannot directly convert a StringRef to a 'const char*' because there is
  1308. no way to add a trailing nul (unlike the .c_str() method on various stronger
  1309. classes).
  1310. #. StringRef doesn't own or keep alive the underlying string bytes.
  1311. As such it can easily lead to dangling pointers, and is not suitable for
  1312. embedding in datastructures in most cases (instead, use an std::string or
  1313. something like that).
  1314. #. For the same reason, StringRef cannot be used as the return value of a
  1315. method if the method "computes" the result string. Instead, use std::string.
  1316. #. StringRef's do not allow you to mutate the pointed-to string bytes and it
  1317. doesn't allow you to insert or remove bytes from the range. For editing
  1318. operations like this, it interoperates with the :ref:`Twine <dss_twine>`
  1319. class.
  1320. Because of its strengths and limitations, it is very common for a function to
  1321. take a StringRef and for a method on an object to return a StringRef that points
  1322. into some string that it owns.
  1323. .. _dss_twine:
  1324. llvm/ADT/Twine.h
  1325. ^^^^^^^^^^^^^^^^
  1326. The Twine class is used as an intermediary datatype for APIs that want to take a
  1327. string that can be constructed inline with a series of concatenations. Twine
  1328. works by forming recursive instances of the Twine datatype (a simple value
  1329. object) on the stack as temporary objects, linking them together into a tree
  1330. which is then linearized when the Twine is consumed. Twine is only safe to use
  1331. as the argument to a function, and should always be a const reference, e.g.:
  1332. .. code-block:: c++
  1333. void foo(const Twine &T);
  1334. ...
  1335. StringRef X = ...
  1336. unsigned i = ...
  1337. foo(X + "." + Twine(i));
  1338. This example forms a string like "blarg.42" by concatenating the values
  1339. together, and does not form intermediate strings containing "blarg" or "blarg.".
  1340. Because Twine is constructed with temporary objects on the stack, and because
  1341. these instances are destroyed at the end of the current statement, it is an
  1342. inherently dangerous API. For example, this simple variant contains undefined
  1343. behavior and will probably crash:
  1344. .. code-block:: c++
  1345. void foo(const Twine &T);
  1346. ...
  1347. StringRef X = ...
  1348. unsigned i = ...
  1349. const Twine &Tmp = X + "." + Twine(i);
  1350. foo(Tmp);
  1351. ... because the temporaries are destroyed before the call. That said, Twine's
  1352. are much more efficient than intermediate std::string temporaries, and they work
  1353. really well with StringRef. Just be aware of their limitations.
  1354. .. _dss_smallstring:
  1355. llvm/ADT/SmallString.h
  1356. ^^^^^^^^^^^^^^^^^^^^^^
  1357. SmallString is a subclass of :ref:`SmallVector <dss_smallvector>` that adds some
  1358. convenience APIs like += that takes StringRef's. SmallString avoids allocating
  1359. memory in the case when the preallocated space is enough to hold its data, and
  1360. it calls back to general heap allocation when required. Since it owns its data,
  1361. it is very safe to use and supports full mutation of the string.
  1362. Like SmallVector's, the big downside to SmallString is their sizeof. While they
  1363. are optimized for small strings, they themselves are not particularly small.
  1364. This means that they work great for temporary scratch buffers on the stack, but
  1365. should not generally be put into the heap: it is very rare to see a SmallString
  1366. as the member of a frequently-allocated heap data structure or returned
  1367. by-value.
  1368. .. _dss_stdstring:
  1369. std::string
  1370. ^^^^^^^^^^^
  1371. The standard C++ std::string class is a very general class that (like
  1372. SmallString) owns its underlying data. sizeof(std::string) is very reasonable
  1373. so it can be embedded into heap data structures and returned by-value. On the
  1374. other hand, std::string is highly inefficient for inline editing (e.g.
  1375. concatenating a bunch of stuff together) and because it is provided by the
  1376. standard library, its performance characteristics depend a lot of the host
  1377. standard library (e.g. libc++ and MSVC provide a highly optimized string class,
  1378. GCC contains a really slow implementation).
  1379. The major disadvantage of std::string is that almost every operation that makes
  1380. them larger can allocate memory, which is slow. As such, it is better to use
  1381. SmallVector or Twine as a scratch buffer, but then use std::string to persist
  1382. the result.
  1383. .. _ds_set:
  1384. Set-Like Containers (std::set, SmallSet, SetVector, etc)
  1385. --------------------------------------------------------
  1386. Set-like containers are useful when you need to canonicalize multiple values
  1387. into a single representation. There are several different choices for how to do
  1388. this, providing various trade-offs.
  1389. .. _dss_sortedvectorset:
  1390. A sorted 'vector'
  1391. ^^^^^^^^^^^^^^^^^
  1392. If you intend to insert a lot of elements, then do a lot of queries, a great
  1393. approach is to use an std::vector (or other sequential container) with
  1394. std::sort+std::unique to remove duplicates. This approach works really well if
  1395. your usage pattern has these two distinct phases (insert then query), and can be
  1396. coupled with a good choice of :ref:`sequential container <ds_sequential>`.
  1397. This combination provides the several nice properties: the result data is
  1398. contiguous in memory (good for cache locality), has few allocations, is easy to
  1399. address (iterators in the final vector are just indices or pointers), and can be
  1400. efficiently queried with a standard binary search (e.g.
  1401. ``std::lower_bound``; if you want the whole range of elements comparing
  1402. equal, use ``std::equal_range``).
  1403. .. _dss_smallset:
  1404. llvm/ADT/SmallSet.h
  1405. ^^^^^^^^^^^^^^^^^^^
  1406. If you have a set-like data structure that is usually small and whose elements
  1407. are reasonably small, a ``SmallSet<Type, N>`` is a good choice. This set has
  1408. space for N elements in place (thus, if the set is dynamically smaller than N,
  1409. no malloc traffic is required) and accesses them with a simple linear search.
  1410. When the set grows beyond N elements, it allocates a more expensive
  1411. representation that guarantees efficient access (for most types, it falls back
  1412. to :ref:`std::set <dss_set>`, but for pointers it uses something far better,
  1413. :ref:`SmallPtrSet <dss_smallptrset>`.
  1414. The magic of this class is that it handles small sets extremely efficiently, but
  1415. gracefully handles extremely large sets without loss of efficiency.
  1416. .. _dss_smallptrset:
  1417. llvm/ADT/SmallPtrSet.h
  1418. ^^^^^^^^^^^^^^^^^^^^^^
  1419. ``SmallPtrSet`` has all the advantages of ``SmallSet`` (and a ``SmallSet`` of
  1420. pointers is transparently implemented with a ``SmallPtrSet``). If more than N
  1421. insertions are performed, a single quadratically probed hash table is allocated
  1422. and grows as needed, providing extremely efficient access (constant time
  1423. insertion/deleting/queries with low constant factors) and is very stingy with
  1424. malloc traffic.
  1425. Note that, unlike :ref:`std::set <dss_set>`, the iterators of ``SmallPtrSet``
  1426. are invalidated whenever an insertion occurs. Also, the values visited by the
  1427. iterators are not visited in sorted order.
  1428. .. _dss_stringset:
  1429. llvm/ADT/StringSet.h
  1430. ^^^^^^^^^^^^^^^^^^^^
  1431. ``StringSet`` is a thin wrapper around :ref:`StringMap\<char\> <dss_stringmap>`,
  1432. and it allows efficient storage and retrieval of unique strings.
  1433. Functionally analogous to ``SmallSet<StringRef>``, ``StringSet`` also supports
  1434. iteration. (The iterator dereferences to a ``StringMapEntry<char>``, so you
  1435. need to call ``i->getKey()`` to access the item of the StringSet.) On the
  1436. other hand, ``StringSet`` doesn't support range-insertion and
  1437. copy-construction, which :ref:`SmallSet <dss_smallset>` and :ref:`SmallPtrSet
  1438. <dss_smallptrset>` do support.
  1439. .. _dss_denseset:
  1440. llvm/ADT/DenseSet.h
  1441. ^^^^^^^^^^^^^^^^^^^
  1442. DenseSet is a simple quadratically probed hash table. It excels at supporting
  1443. small values: it uses a single allocation to hold all of the pairs that are
  1444. currently inserted in the set. DenseSet is a great way to unique small values
  1445. that are not simple pointers (use :ref:`SmallPtrSet <dss_smallptrset>` for
  1446. pointers). Note that DenseSet has the same requirements for the value type that
  1447. :ref:`DenseMap <dss_densemap>` has.
  1448. .. _dss_sparseset:
  1449. llvm/ADT/SparseSet.h
  1450. ^^^^^^^^^^^^^^^^^^^^
  1451. SparseSet holds a small number of objects identified by unsigned keys of
  1452. moderate size. It uses a lot of memory, but provides operations that are almost
  1453. as fast as a vector. Typical keys are physical registers, virtual registers, or
  1454. numbered basic blocks.
  1455. SparseSet is useful for algorithms that need very fast clear/find/insert/erase
  1456. and fast iteration over small sets. It is not intended for building composite
  1457. data structures.
  1458. .. _dss_sparsemultiset:
  1459. llvm/ADT/SparseMultiSet.h
  1460. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1461. SparseMultiSet adds multiset behavior to SparseSet, while retaining SparseSet's
  1462. desirable attributes. Like SparseSet, it typically uses a lot of memory, but
  1463. provides operations that are almost as fast as a vector. Typical keys are
  1464. physical registers, virtual registers, or numbered basic blocks.
  1465. SparseMultiSet is useful for algorithms that need very fast
  1466. clear/find/insert/erase of the entire collection, and iteration over sets of
  1467. elements sharing a key. It is often a more efficient choice than using composite
  1468. data structures (e.g. vector-of-vectors, map-of-vectors). It is not intended for
  1469. building composite data structures.
  1470. .. _dss_FoldingSet:
  1471. llvm/ADT/FoldingSet.h
  1472. ^^^^^^^^^^^^^^^^^^^^^
  1473. FoldingSet is an aggregate class that is really good at uniquing
  1474. expensive-to-create or polymorphic objects. It is a combination of a chained
  1475. hash table with intrusive links (uniqued objects are required to inherit from
  1476. FoldingSetNode) that uses :ref:`SmallVector <dss_smallvector>` as part of its ID
  1477. process.
  1478. Consider a case where you want to implement a "getOrCreateFoo" method for a
  1479. complex object (for example, a node in the code generator). The client has a
  1480. description of **what** it wants to generate (it knows the opcode and all the
  1481. operands), but we don't want to 'new' a node, then try inserting it into a set
  1482. only to find out it already exists, at which point we would have to delete it
  1483. and return the node that already exists.
  1484. To support this style of client, FoldingSet perform a query with a
  1485. FoldingSetNodeID (which wraps SmallVector) that can be used to describe the
  1486. element that we want to query for. The query either returns the element
  1487. matching the ID or it returns an opaque ID that indicates where insertion should
  1488. take place. Construction of the ID usually does not require heap traffic.
  1489. Because FoldingSet uses intrusive links, it can support polymorphic objects in
  1490. the set (for example, you can have SDNode instances mixed with LoadSDNodes).
  1491. Because the elements are individually allocated, pointers to the elements are
  1492. stable: inserting or removing elements does not invalidate any pointers to other
  1493. elements.
  1494. .. _dss_set:
  1495. <set>
  1496. ^^^^^
  1497. ``std::set`` is a reasonable all-around set class, which is decent at many
  1498. things but great at nothing. std::set allocates memory for each element
  1499. inserted (thus it is very malloc intensive) and typically stores three pointers
  1500. per element in the set (thus adding a large amount of per-element space
  1501. overhead). It offers guaranteed log(n) performance, which is not particularly
  1502. fast from a complexity standpoint (particularly if the elements of the set are
  1503. expensive to compare, like strings), and has extremely high constant factors for
  1504. lookup, insertion and removal.
  1505. The advantages of std::set are that its iterators are stable (deleting or
  1506. inserting an element from the set does not affect iterators or pointers to other
  1507. elements) and that iteration over the set is guaranteed to be in sorted order.
  1508. If the elements in the set are large, then the relative overhead of the pointers
  1509. and malloc traffic is not a big deal, but if the elements of the set are small,
  1510. std::set is almost never a good choice.
  1511. .. _dss_setvector:
  1512. llvm/ADT/SetVector.h
  1513. ^^^^^^^^^^^^^^^^^^^^
  1514. LLVM's ``SetVector<Type>`` is an adapter class that combines your choice of a
  1515. set-like container along with a :ref:`Sequential Container <ds_sequential>` The
  1516. important property that this provides is efficient insertion with uniquing
  1517. (duplicate elements are ignored) with iteration support. It implements this by
  1518. inserting elements into both a set-like container and the sequential container,
  1519. using the set-like container for uniquing and the sequential container for
  1520. iteration.
  1521. The difference between SetVector and other sets is that the order of iteration
  1522. is guaranteed to match the order of insertion into the SetVector. This property
  1523. is really important for things like sets of pointers. Because pointer values
  1524. are non-deterministic (e.g. vary across runs of the program on different
  1525. machines), iterating over the pointers in the set will not be in a well-defined
  1526. order.
  1527. The drawback of SetVector is that it requires twice as much space as a normal
  1528. set and has the sum of constant factors from the set-like container and the
  1529. sequential container that it uses. Use it **only** if you need to iterate over
  1530. the elements in a deterministic order. SetVector is also expensive to delete
  1531. elements out of (linear time), unless you use its "pop_back" method, which is
  1532. faster.
  1533. ``SetVector`` is an adapter class that defaults to using ``std::vector`` and a
  1534. size 16 ``SmallSet`` for the underlying containers, so it is quite expensive.
  1535. However, ``"llvm/ADT/SetVector.h"`` also provides a ``SmallSetVector`` class,
  1536. which defaults to using a ``SmallVector`` and ``SmallSet`` of a specified size.
  1537. If you use this, and if your sets are dynamically smaller than ``N``, you will
  1538. save a lot of heap traffic.
  1539. .. _dss_uniquevector:
  1540. llvm/ADT/UniqueVector.h
  1541. ^^^^^^^^^^^^^^^^^^^^^^^
  1542. UniqueVector is similar to :ref:`SetVector <dss_setvector>` but it retains a
  1543. unique ID for each element inserted into the set. It internally contains a map
  1544. and a vector, and it assigns a unique ID for each value inserted into the set.
  1545. UniqueVector is very expensive: its cost is the sum of the cost of maintaining
  1546. both the map and vector, it has high complexity, high constant factors, and
  1547. produces a lot of malloc traffic. It should be avoided.
  1548. .. _dss_immutableset:
  1549. llvm/ADT/ImmutableSet.h
  1550. ^^^^^^^^^^^^^^^^^^^^^^^
  1551. ImmutableSet is an immutable (functional) set implementation based on an AVL
  1552. tree. Adding or removing elements is done through a Factory object and results
  1553. in the creation of a new ImmutableSet object. If an ImmutableSet already exists
  1554. with the given contents, then the existing one is returned; equality is compared
  1555. with a FoldingSetNodeID. The time and space complexity of add or remove
  1556. operations is logarithmic in the size of the original set.
  1557. There is no method for returning an element of the set, you can only check for
  1558. membership.
  1559. .. _dss_otherset:
  1560. Other Set-Like Container Options
  1561. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1562. The STL provides several other options, such as std::multiset and the various
  1563. "hash_set" like containers (whether from C++ TR1 or from the SGI library). We
  1564. never use hash_set and unordered_set because they are generally very expensive
  1565. (each insertion requires a malloc) and very non-portable.
  1566. std::multiset is useful if you're not interested in elimination of duplicates,
  1567. but has all the drawbacks of :ref:`std::set <dss_set>`. A sorted vector
  1568. (where you don't delete duplicate entries) or some other approach is almost
  1569. always better.
  1570. .. _ds_map:
  1571. Map-Like Containers (std::map, DenseMap, etc)
  1572. ---------------------------------------------
  1573. Map-like containers are useful when you want to associate data to a key. As
  1574. usual, there are a lot of different ways to do this. :)
  1575. .. _dss_sortedvectormap:
  1576. A sorted 'vector'
  1577. ^^^^^^^^^^^^^^^^^
  1578. If your usage pattern follows a strict insert-then-query approach, you can
  1579. trivially use the same approach as :ref:`sorted vectors for set-like containers
  1580. <dss_sortedvectorset>`. The only difference is that your query function (which
  1581. uses std::lower_bound to get efficient log(n) lookup) should only compare the
  1582. key, not both the key and value. This yields the same advantages as sorted
  1583. vectors for sets.
  1584. .. _dss_stringmap:
  1585. llvm/ADT/StringMap.h
  1586. ^^^^^^^^^^^^^^^^^^^^
  1587. Strings are commonly used as keys in maps, and they are difficult to support
  1588. efficiently: they are variable length, inefficient to hash and compare when
  1589. long, expensive to copy, etc. StringMap is a specialized container designed to
  1590. cope with these issues. It supports mapping an arbitrary range of bytes to an
  1591. arbitrary other object.
  1592. The StringMap implementation uses a quadratically-probed hash table, where the
  1593. buckets store a pointer to the heap allocated entries (and some other stuff).
  1594. The entries in the map must be heap allocated because the strings are variable
  1595. length. The string data (key) and the element object (value) are stored in the
  1596. same allocation with the string data immediately after the element object.
  1597. This container guarantees the "``(char*)(&Value+1)``" points to the key string
  1598. for a value.
  1599. The StringMap is very fast for several reasons: quadratic probing is very cache
  1600. efficient for lookups, the hash value of strings in buckets is not recomputed
  1601. when looking up an element, StringMap rarely has to touch the memory for
  1602. unrelated objects when looking up a value (even when hash collisions happen),
  1603. hash table growth does not recompute the hash values for strings already in the
  1604. table, and each pair in the map is store in a single allocation (the string data
  1605. is stored in the same allocation as the Value of a pair).
  1606. StringMap also provides query methods that take byte ranges, so it only ever
  1607. copies a string if a value is inserted into the table.
  1608. StringMap iteration order, however, is not guaranteed to be deterministic, so
  1609. any uses which require that should instead use a std::map.
  1610. .. _dss_indexmap:
  1611. llvm/ADT/IndexedMap.h
  1612. ^^^^^^^^^^^^^^^^^^^^^
  1613. IndexedMap is a specialized container for mapping small dense integers (or
  1614. values that can be mapped to small dense integers) to some other type. It is
  1615. internally implemented as a vector with a mapping function that maps the keys
  1616. to the dense integer range.
  1617. This is useful for cases like virtual registers in the LLVM code generator: they
  1618. have a dense mapping that is offset by a compile-time constant (the first
  1619. virtual register ID).
  1620. .. _dss_densemap:
  1621. llvm/ADT/DenseMap.h
  1622. ^^^^^^^^^^^^^^^^^^^
  1623. DenseMap is a simple quadratically probed hash table. It excels at supporting
  1624. small keys and values: it uses a single allocation to hold all of the pairs
  1625. that are currently inserted in the map. DenseMap is a great way to map
  1626. pointers to pointers, or map other small types to each other.
  1627. There are several aspects of DenseMap that you should be aware of, however.
  1628. The iterators in a DenseMap are invalidated whenever an insertion occurs,
  1629. unlike map. Also, because DenseMap allocates space for a large number of
  1630. key/value pairs (it starts with 64 by default), it will waste a lot of space if
  1631. your keys or values are large. Finally, you must implement a partial
  1632. specialization of DenseMapInfo for the key that you want, if it isn't already
  1633. supported. This is required to tell DenseMap about two special marker values
  1634. (which can never be inserted into the map) that it needs internally.
  1635. DenseMap's find_as() method supports lookup operations using an alternate key
  1636. type. This is useful in cases where the normal key type is expensive to
  1637. construct, but cheap to compare against. The DenseMapInfo is responsible for
  1638. defining the appropriate comparison and hashing methods for each alternate key
  1639. type used.
  1640. .. _dss_valuemap:
  1641. llvm/IR/ValueMap.h
  1642. ^^^^^^^^^^^^^^^^^^^
  1643. ValueMap is a wrapper around a :ref:`DenseMap <dss_densemap>` mapping
  1644. ``Value*``\ s (or subclasses) to another type. When a Value is deleted or
  1645. RAUW'ed, ValueMap will update itself so the new version of the key is mapped to
  1646. the same value, just as if the key were a WeakVH. You can configure exactly how
  1647. this happens, and what else happens on these two events, by passing a ``Config``
  1648. parameter to the ValueMap template.
  1649. .. _dss_intervalmap:
  1650. llvm/ADT/IntervalMap.h
  1651. ^^^^^^^^^^^^^^^^^^^^^^
  1652. IntervalMap is a compact map for small keys and values. It maps key intervals
  1653. instead of single keys, and it will automatically coalesce adjacent intervals.
  1654. When the map only contains a few intervals, they are stored in the map object
  1655. itself to avoid allocations.
  1656. The IntervalMap iterators are quite big, so they should not be passed around as
  1657. STL iterators. The heavyweight iterators allow a smaller data structure.
  1658. .. _dss_map:
  1659. <map>
  1660. ^^^^^
  1661. std::map has similar characteristics to :ref:`std::set <dss_set>`: it uses a
  1662. single allocation per pair inserted into the map, it offers log(n) lookup with
  1663. an extremely large constant factor, imposes a space penalty of 3 pointers per
  1664. pair in the map, etc.
  1665. std::map is most useful when your keys or values are very large, if you need to
  1666. iterate over the collection in sorted order, or if you need stable iterators
  1667. into the map (i.e. they don't get invalidated if an insertion or deletion of
  1668. another element takes place).
  1669. .. _dss_mapvector:
  1670. llvm/ADT/MapVector.h
  1671. ^^^^^^^^^^^^^^^^^^^^
  1672. ``MapVector<KeyT,ValueT>`` provides a subset of the DenseMap interface. The
  1673. main difference is that the iteration order is guaranteed to be the insertion
  1674. order, making it an easy (but somewhat expensive) solution for non-deterministic
  1675. iteration over maps of pointers.
  1676. It is implemented by mapping from key to an index in a vector of key,value
  1677. pairs. This provides fast lookup and iteration, but has two main drawbacks:
  1678. the key is stored twice and removing elements takes linear time. If it is
  1679. necessary to remove elements, it's best to remove them in bulk using
  1680. ``remove_if()``.
  1681. .. _dss_inteqclasses:
  1682. llvm/ADT/IntEqClasses.h
  1683. ^^^^^^^^^^^^^^^^^^^^^^^
  1684. IntEqClasses provides a compact representation of equivalence classes of small
  1685. integers. Initially, each integer in the range 0..n-1 has its own equivalence
  1686. class. Classes can be joined by passing two class representatives to the
  1687. join(a, b) method. Two integers are in the same class when findLeader() returns
  1688. the same representative.
  1689. Once all equivalence classes are formed, the map can be compressed so each
  1690. integer 0..n-1 maps to an equivalence class number in the range 0..m-1, where m
  1691. is the total number of equivalence classes. The map must be uncompressed before
  1692. it can be edited again.
  1693. .. _dss_immutablemap:
  1694. llvm/ADT/ImmutableMap.h
  1695. ^^^^^^^^^^^^^^^^^^^^^^^
  1696. ImmutableMap is an immutable (functional) map implementation based on an AVL
  1697. tree. Adding or removing elements is done through a Factory object and results
  1698. in the creation of a new ImmutableMap object. If an ImmutableMap already exists
  1699. with the given key set, then the existing one is returned; equality is compared
  1700. with a FoldingSetNodeID. The time and space complexity of add or remove
  1701. operations is logarithmic in the size of the original map.
  1702. .. _dss_othermap:
  1703. Other Map-Like Container Options
  1704. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1705. The STL provides several other options, such as std::multimap and the various
  1706. "hash_map" like containers (whether from C++ TR1 or from the SGI library). We
  1707. never use hash_set and unordered_set because they are generally very expensive
  1708. (each insertion requires a malloc) and very non-portable.
  1709. std::multimap is useful if you want to map a key to multiple values, but has all
  1710. the drawbacks of std::map. A sorted vector or some other approach is almost
  1711. always better.
  1712. .. _ds_bit:
  1713. Bit storage containers (BitVector, SparseBitVector)
  1714. ---------------------------------------------------
  1715. Unlike the other containers, there are only two bit storage containers, and
  1716. choosing when to use each is relatively straightforward.
  1717. One additional option is ``std::vector<bool>``: we discourage its use for two
  1718. reasons 1) the implementation in many common compilers (e.g. commonly
  1719. available versions of GCC) is extremely inefficient and 2) the C++ standards
  1720. committee is likely to deprecate this container and/or change it significantly
  1721. somehow. In any case, please don't use it.
  1722. .. _dss_bitvector:
  1723. BitVector
  1724. ^^^^^^^^^
  1725. The BitVector container provides a dynamic size set of bits for manipulation.
  1726. It supports individual bit setting/testing, as well as set operations. The set
  1727. operations take time O(size of bitvector), but operations are performed one word
  1728. at a time, instead of one bit at a time. This makes the BitVector very fast for
  1729. set operations compared to other containers. Use the BitVector when you expect
  1730. the number of set bits to be high (i.e. a dense set).
  1731. .. _dss_smallbitvector:
  1732. SmallBitVector
  1733. ^^^^^^^^^^^^^^
  1734. The SmallBitVector container provides the same interface as BitVector, but it is
  1735. optimized for the case where only a small number of bits, less than 25 or so,
  1736. are needed. It also transparently supports larger bit counts, but slightly less
  1737. efficiently than a plain BitVector, so SmallBitVector should only be used when
  1738. larger counts are rare.
  1739. At this time, SmallBitVector does not support set operations (and, or, xor), and
  1740. its operator[] does not provide an assignable lvalue.
  1741. .. _dss_sparsebitvector:
  1742. SparseBitVector
  1743. ^^^^^^^^^^^^^^^
  1744. The SparseBitVector container is much like BitVector, with one major difference:
  1745. Only the bits that are set, are stored. This makes the SparseBitVector much
  1746. more space efficient than BitVector when the set is sparse, as well as making
  1747. set operations O(number of set bits) instead of O(size of universe). The
  1748. downside to the SparseBitVector is that setting and testing of random bits is
  1749. O(N), and on large SparseBitVectors, this can be slower than BitVector. In our
  1750. implementation, setting or testing bits in sorted order (either forwards or
  1751. reverse) is O(1) worst case. Testing and setting bits within 128 bits (depends
  1752. on size) of the current bit is also O(1). As a general statement,
  1753. testing/setting bits in a SparseBitVector is O(distance away from last set bit).
  1754. .. _debugging:
  1755. Debugging
  1756. =========
  1757. A handful of `GDB pretty printers
  1758. <https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html>`__ are
  1759. provided for some of the core LLVM libraries. To use them, execute the
  1760. following (or add it to your ``~/.gdbinit``)::
  1761. source /path/to/llvm/src/utils/gdb-scripts/prettyprinters.py
  1762. It also might be handy to enable the `print pretty
  1763. <http://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_57.html>`__ option to
  1764. avoid data structures being printed as a big block of text.
  1765. .. _common:
  1766. Helpful Hints for Common Operations
  1767. ===================================
  1768. This section describes how to perform some very simple transformations of LLVM
  1769. code. This is meant to give examples of common idioms used, showing the
  1770. practical side of LLVM transformations.
  1771. Because this is a "how-to" section, you should also read about the main classes
  1772. that you will be working with. The :ref:`Core LLVM Class Hierarchy Reference
  1773. <coreclasses>` contains details and descriptions of the main classes that you
  1774. should know about.
  1775. .. _inspection:
  1776. Basic Inspection and Traversal Routines
  1777. ---------------------------------------
  1778. The LLVM compiler infrastructure have many different data structures that may be
  1779. traversed. Following the example of the C++ standard template library, the
  1780. techniques used to traverse these various data structures are all basically the
  1781. same. For a enumerable sequence of values, the ``XXXbegin()`` function (or
  1782. method) returns an iterator to the start of the sequence, the ``XXXend()``
  1783. function returns an iterator pointing to one past the last valid element of the
  1784. sequence, and there is some ``XXXiterator`` data type that is common between the
  1785. two operations.
  1786. Because the pattern for iteration is common across many different aspects of the
  1787. program representation, the standard template library algorithms may be used on
  1788. them, and it is easier to remember how to iterate. First we show a few common
  1789. examples of the data structures that need to be traversed. Other data
  1790. structures are traversed in very similar ways.
  1791. .. _iterate_function:
  1792. Iterating over the ``BasicBlock`` in a ``Function``
  1793. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1794. It's quite common to have a ``Function`` instance that you'd like to transform
  1795. in some way; in particular, you'd like to manipulate its ``BasicBlock``\ s. To
  1796. facilitate this, you'll need to iterate over all of the ``BasicBlock``\ s that
  1797. constitute the ``Function``. The following is an example that prints the name
  1798. of a ``BasicBlock`` and the number of ``Instruction``\ s it contains:
  1799. .. code-block:: c++
  1800. Function &Func = ...
  1801. for (BasicBlock &BB : Func)
  1802. // Print out the name of the basic block if it has one, and then the
  1803. // number of instructions that it contains
  1804. errs() << "Basic block (name=" << BB.getName() << ") has "
  1805. << BB.size() << " instructions.\n";
  1806. .. _iterate_basicblock:
  1807. Iterating over the ``Instruction`` in a ``BasicBlock``
  1808. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1809. Just like when dealing with ``BasicBlock``\ s in ``Function``\ s, it's easy to
  1810. iterate over the individual instructions that make up ``BasicBlock``\ s. Here's
  1811. a code snippet that prints out each instruction in a ``BasicBlock``:
  1812. .. code-block:: c++
  1813. BasicBlock& BB = ...
  1814. for (Instruction &I : BB)
  1815. // The next statement works since operator<<(ostream&,...)
  1816. // is overloaded for Instruction&
  1817. errs() << I << "\n";
  1818. However, this isn't really the best way to print out the contents of a
  1819. ``BasicBlock``! Since the ostream operators are overloaded for virtually
  1820. anything you'll care about, you could have just invoked the print routine on the
  1821. basic block itself: ``errs() << BB << "\n";``.
  1822. .. _iterate_insiter:
  1823. Iterating over the ``Instruction`` in a ``Function``
  1824. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1825. If you're finding that you commonly iterate over a ``Function``'s
  1826. ``BasicBlock``\ s and then that ``BasicBlock``'s ``Instruction``\ s,
  1827. ``InstIterator`` should be used instead. You'll need to include
  1828. ``llvm/IR/InstIterator.h`` (`doxygen
  1829. <http://llvm.org/doxygen/InstIterator_8h.html>`__) and then instantiate
  1830. ``InstIterator``\ s explicitly in your code. Here's a small example that shows
  1831. how to dump all instructions in a function to the standard error stream:
  1832. .. code-block:: c++
  1833. #include "llvm/IR/InstIterator.h"
  1834. // F is a pointer to a Function instance
  1835. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
  1836. errs() << *I << "\n";
  1837. Easy, isn't it? You can also use ``InstIterator``\ s to fill a work list with
  1838. its initial contents. For example, if you wanted to initialize a work list to
  1839. contain all instructions in a ``Function`` F, all you would need to do is
  1840. something like:
  1841. .. code-block:: c++
  1842. std::set<Instruction*> worklist;
  1843. // or better yet, SmallPtrSet<Instruction*, 64> worklist;
  1844. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
  1845. worklist.insert(&*I);
  1846. The STL set ``worklist`` would now contain all instructions in the ``Function``
  1847. pointed to by F.
  1848. .. _iterate_convert:
  1849. Turning an iterator into a class pointer (and vice-versa)
  1850. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1851. Sometimes, it'll be useful to grab a reference (or pointer) to a class instance
  1852. when all you've got at hand is an iterator. Well, extracting a reference or a
  1853. pointer from an iterator is very straight-forward. Assuming that ``i`` is a
  1854. ``BasicBlock::iterator`` and ``j`` is a ``BasicBlock::const_iterator``:
  1855. .. code-block:: c++
  1856. Instruction& inst = *i; // Grab reference to instruction reference
  1857. Instruction* pinst = &*i; // Grab pointer to instruction reference
  1858. const Instruction& inst = *j;
  1859. However, the iterators you'll be working with in the LLVM framework are special:
  1860. they will automatically convert to a ptr-to-instance type whenever they need to.
  1861. Instead of dereferencing the iterator and then taking the address of the result,
  1862. you can simply assign the iterator to the proper pointer type and you get the
  1863. dereference and address-of operation as a result of the assignment (behind the
  1864. scenes, this is a result of overloading casting mechanisms). Thus the second
  1865. line of the last example,
  1866. .. code-block:: c++
  1867. Instruction *pinst = &*i;
  1868. is semantically equivalent to
  1869. .. code-block:: c++
  1870. Instruction *pinst = i;
  1871. It's also possible to turn a class pointer into the corresponding iterator, and
  1872. this is a constant time operation (very efficient). The following code snippet
  1873. illustrates use of the conversion constructors provided by LLVM iterators. By
  1874. using these, you can explicitly grab the iterator of something without actually
  1875. obtaining it via iteration over some structure:
  1876. .. code-block:: c++
  1877. void printNextInstruction(Instruction* inst) {
  1878. BasicBlock::iterator it(inst);
  1879. ++it; // After this line, it refers to the instruction after *inst
  1880. if (it != inst->getParent()->end()) errs() << *it << "\n";
  1881. }
  1882. Unfortunately, these implicit conversions come at a cost; they prevent these
  1883. iterators from conforming to standard iterator conventions, and thus from being
  1884. usable with standard algorithms and containers. For example, they prevent the
  1885. following code, where ``B`` is a ``BasicBlock``, from compiling:
  1886. .. code-block:: c++
  1887. llvm::SmallVector<llvm::Instruction *, 16>(B->begin(), B->end());
  1888. Because of this, these implicit conversions may be removed some day, and
  1889. ``operator*`` changed to return a pointer instead of a reference.
  1890. .. _iterate_complex:
  1891. Finding call sites: a slightly more complex example
  1892. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1893. Say that you're writing a FunctionPass and would like to count all the locations
  1894. in the entire module (that is, across every ``Function``) where a certain
  1895. function (i.e., some ``Function *``) is already in scope. As you'll learn
  1896. later, you may want to use an ``InstVisitor`` to accomplish this in a much more
  1897. straight-forward manner, but this example will allow us to explore how you'd do
  1898. it if you didn't have ``InstVisitor`` around. In pseudo-code, this is what we
  1899. want to do:
  1900. .. code-block:: none
  1901. initialize callCounter to zero
  1902. for each Function f in the Module
  1903. for each BasicBlock b in f
  1904. for each Instruction i in b
  1905. if (i is a CallInst and calls the given function)
  1906. increment callCounter
  1907. And the actual code is (remember, because we're writing a ``FunctionPass``, our
  1908. ``FunctionPass``-derived class simply has to override the ``runOnFunction``
  1909. method):
  1910. .. code-block:: c++
  1911. Function* targetFunc = ...;
  1912. class OurFunctionPass : public FunctionPass {
  1913. public:
  1914. OurFunctionPass(): callCounter(0) { }
  1915. virtual runOnFunction(Function& F) {
  1916. for (BasicBlock &B : F) {
  1917. for (Instruction &I: B) {
  1918. if (auto *CallInst = dyn_cast<CallInst>(&I)) {
  1919. // We know we've encountered a call instruction, so we
  1920. // need to determine if it's a call to the
  1921. // function pointed to by m_func or not.
  1922. if (CallInst->getCalledFunction() == targetFunc)
  1923. ++callCounter;
  1924. }
  1925. }
  1926. }
  1927. }
  1928. private:
  1929. unsigned callCounter;
  1930. };
  1931. .. _calls_and_invokes:
  1932. Treating calls and invokes the same way
  1933. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1934. You may have noticed that the previous example was a bit oversimplified in that
  1935. it did not deal with call sites generated by 'invoke' instructions. In this,
  1936. and in other situations, you may find that you want to treat ``CallInst``\ s and
  1937. ``InvokeInst``\ s the same way, even though their most-specific common base
  1938. class is ``Instruction``, which includes lots of less closely-related things.
  1939. For these cases, LLVM provides a handy wrapper class called ``CallSite``
  1940. (`doxygen <http://llvm.org/doxygen/classllvm_1_1CallSite.html>`__) It is
  1941. essentially a wrapper around an ``Instruction`` pointer, with some methods that
  1942. provide functionality common to ``CallInst``\ s and ``InvokeInst``\ s.
  1943. This class has "value semantics": it should be passed by value, not by reference
  1944. and it should not be dynamically allocated or deallocated using ``operator new``
  1945. or ``operator delete``. It is efficiently copyable, assignable and
  1946. constructable, with costs equivalents to that of a bare pointer. If you look at
  1947. its definition, it has only a single pointer member.
  1948. .. _iterate_chains:
  1949. Iterating over def-use & use-def chains
  1950. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1951. Frequently, we might have an instance of the ``Value`` class (`doxygen
  1952. <http://llvm.org/doxygen/classllvm_1_1Value.html>`__) and we want to determine
  1953. which ``User`` s use the ``Value``. The list of all ``User``\ s of a particular
  1954. ``Value`` is called a *def-use* chain. For example, let's say we have a
  1955. ``Function*`` named ``F`` to a particular function ``foo``. Finding all of the
  1956. instructions that *use* ``foo`` is as simple as iterating over the *def-use*
  1957. chain of ``F``:
  1958. .. code-block:: c++
  1959. Function *F = ...;
  1960. for (User *U : F->users()) {
  1961. if (Instruction *Inst = dyn_cast<Instruction>(U)) {
  1962. errs() << "F is used in instruction:\n";
  1963. errs() << *Inst << "\n";
  1964. }
  1965. Alternatively, it's common to have an instance of the ``User`` Class (`doxygen
  1966. <http://llvm.org/doxygen/classllvm_1_1User.html>`__) and need to know what
  1967. ``Value``\ s are used by it. The list of all ``Value``\ s used by a ``User`` is
  1968. known as a *use-def* chain. Instances of class ``Instruction`` are common
  1969. ``User`` s, so we might want to iterate over all of the values that a particular
  1970. instruction uses (that is, the operands of the particular ``Instruction``):
  1971. .. code-block:: c++
  1972. Instruction *pi = ...;
  1973. for (Use &U : pi->operands()) {
  1974. Value *v = U.get();
  1975. // ...
  1976. }
  1977. Declaring objects as ``const`` is an important tool of enforcing mutation free
  1978. algorithms (such as analyses, etc.). For this purpose above iterators come in
  1979. constant flavors as ``Value::const_use_iterator`` and
  1980. ``Value::const_op_iterator``. They automatically arise when calling
  1981. ``use/op_begin()`` on ``const Value*``\ s or ``const User*``\ s respectively.
  1982. Upon dereferencing, they return ``const Use*``\ s. Otherwise the above patterns
  1983. remain unchanged.
  1984. .. _iterate_preds:
  1985. Iterating over predecessors & successors of blocks
  1986. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1987. Iterating over the predecessors and successors of a block is quite easy with the
  1988. routines defined in ``"llvm/IR/CFG.h"``. Just use code like this to
  1989. iterate over all predecessors of BB:
  1990. .. code-block:: c++
  1991. #include "llvm/IR/CFG.h"
  1992. BasicBlock *BB = ...;
  1993. for (BasicBlock *Pred : predecessors(BB)) {
  1994. // ...
  1995. }
  1996. Similarly, to iterate over successors use ``successors``.
  1997. .. _simplechanges:
  1998. Making simple changes
  1999. ---------------------
  2000. There are some primitive transformation operations present in the LLVM
  2001. infrastructure that are worth knowing about. When performing transformations,
  2002. it's fairly common to manipulate the contents of basic blocks. This section
  2003. describes some of the common methods for doing so and gives example code.
  2004. .. _schanges_creating:
  2005. Creating and inserting new ``Instruction``\ s
  2006. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2007. *Instantiating Instructions*
  2008. Creation of ``Instruction``\ s is straight-forward: simply call the constructor
  2009. for the kind of instruction to instantiate and provide the necessary parameters.
  2010. For example, an ``AllocaInst`` only *requires* a (const-ptr-to) ``Type``. Thus:
  2011. .. code-block:: c++
  2012. auto *ai = new AllocaInst(Type::Int32Ty);
  2013. will create an ``AllocaInst`` instance that represents the allocation of one
  2014. integer in the current stack frame, at run time. Each ``Instruction`` subclass
  2015. is likely to have varying default parameters which change the semantics of the
  2016. instruction, so refer to the `doxygen documentation for the subclass of
  2017. Instruction <http://llvm.org/doxygen/classllvm_1_1Instruction.html>`_ that
  2018. you're interested in instantiating.
  2019. *Naming values*
  2020. It is very useful to name the values of instructions when you're able to, as
  2021. this facilitates the debugging of your transformations. If you end up looking
  2022. at generated LLVM machine code, you definitely want to have logical names
  2023. associated with the results of instructions! By supplying a value for the
  2024. ``Name`` (default) parameter of the ``Instruction`` constructor, you associate a
  2025. logical name with the result of the instruction's execution at run time. For
  2026. example, say that I'm writing a transformation that dynamically allocates space
  2027. for an integer on the stack, and that integer is going to be used as some kind
  2028. of index by some other code. To accomplish this, I place an ``AllocaInst`` at
  2029. the first point in the first ``BasicBlock`` of some ``Function``, and I'm
  2030. intending to use it within the same ``Function``. I might do:
  2031. .. code-block:: c++
  2032. auto *pa = new AllocaInst(Type::Int32Ty, 0, "indexLoc");
  2033. where ``indexLoc`` is now the logical name of the instruction's execution value,
  2034. which is a pointer to an integer on the run time stack.
  2035. *Inserting instructions*
  2036. There are essentially three ways to insert an ``Instruction`` into an existing
  2037. sequence of instructions that form a ``BasicBlock``:
  2038. * Insertion into an explicit instruction list
  2039. Given a ``BasicBlock* pb``, an ``Instruction* pi`` within that ``BasicBlock``,
  2040. and a newly-created instruction we wish to insert before ``*pi``, we do the
  2041. following:
  2042. .. code-block:: c++
  2043. BasicBlock *pb = ...;
  2044. Instruction *pi = ...;
  2045. auto *newInst = new Instruction(...);
  2046. pb->getInstList().insert(pi, newInst); // Inserts newInst before pi in pb
  2047. Appending to the end of a ``BasicBlock`` is so common that the ``Instruction``
  2048. class and ``Instruction``-derived classes provide constructors which take a
  2049. pointer to a ``BasicBlock`` to be appended to. For example code that looked
  2050. like:
  2051. .. code-block:: c++
  2052. BasicBlock *pb = ...;
  2053. auto *newInst = new Instruction(...);
  2054. pb->getInstList().push_back(newInst); // Appends newInst to pb
  2055. becomes:
  2056. .. code-block:: c++
  2057. BasicBlock *pb = ...;
  2058. auto *newInst = new Instruction(..., pb);
  2059. which is much cleaner, especially if you are creating long instruction
  2060. streams.
  2061. * Insertion into an implicit instruction list
  2062. ``Instruction`` instances that are already in ``BasicBlock``\ s are implicitly
  2063. associated with an existing instruction list: the instruction list of the
  2064. enclosing basic block. Thus, we could have accomplished the same thing as the
  2065. above code without being given a ``BasicBlock`` by doing:
  2066. .. code-block:: c++
  2067. Instruction *pi = ...;
  2068. auto *newInst = new Instruction(...);
  2069. pi->getParent()->getInstList().insert(pi, newInst);
  2070. In fact, this sequence of steps occurs so frequently that the ``Instruction``
  2071. class and ``Instruction``-derived classes provide constructors which take (as
  2072. a default parameter) a pointer to an ``Instruction`` which the newly-created
  2073. ``Instruction`` should precede. That is, ``Instruction`` constructors are
  2074. capable of inserting the newly-created instance into the ``BasicBlock`` of a
  2075. provided instruction, immediately before that instruction. Using an
  2076. ``Instruction`` constructor with a ``insertBefore`` (default) parameter, the
  2077. above code becomes:
  2078. .. code-block:: c++
  2079. Instruction* pi = ...;
  2080. auto *newInst = new Instruction(..., pi);
  2081. which is much cleaner, especially if you're creating a lot of instructions and
  2082. adding them to ``BasicBlock``\ s.
  2083. * Insertion using an instance of ``IRBuilder``
  2084. Inserting several ``Instruction``\ s can be quite laborious using the previous
  2085. methods. The ``IRBuilder`` is a convenience class that can be used to add
  2086. several instructions to the end of a ``BasicBlock`` or before a particular
  2087. ``Instruction``. It also supports constant folding and renaming named
  2088. registers (see ``IRBuilder``'s template arguments).
  2089. The example below demonstrates a very simple use of the ``IRBuilder`` where
  2090. three instructions are inserted before the instruction ``pi``. The first two
  2091. instructions are Call instructions and third instruction multiplies the return
  2092. value of the two calls.
  2093. .. code-block:: c++
  2094. Instruction *pi = ...;
  2095. IRBuilder<> Builder(pi);
  2096. CallInst* callOne = Builder.CreateCall(...);
  2097. CallInst* callTwo = Builder.CreateCall(...);
  2098. Value* result = Builder.CreateMul(callOne, callTwo);
  2099. The example below is similar to the above example except that the created
  2100. ``IRBuilder`` inserts instructions at the end of the ``BasicBlock`` ``pb``.
  2101. .. code-block:: c++
  2102. BasicBlock *pb = ...;
  2103. IRBuilder<> Builder(pb);
  2104. CallInst* callOne = Builder.CreateCall(...);
  2105. CallInst* callTwo = Builder.CreateCall(...);
  2106. Value* result = Builder.CreateMul(callOne, callTwo);
  2107. See :doc:`tutorial/LangImpl03` for a practical use of the ``IRBuilder``.
  2108. .. _schanges_deleting:
  2109. Deleting Instructions
  2110. ^^^^^^^^^^^^^^^^^^^^^
  2111. Deleting an instruction from an existing sequence of instructions that form a
  2112. BasicBlock_ is very straight-forward: just call the instruction's
  2113. ``eraseFromParent()`` method. For example:
  2114. .. code-block:: c++
  2115. Instruction *I = .. ;
  2116. I->eraseFromParent();
  2117. This unlinks the instruction from its containing basic block and deletes it. If
  2118. you'd just like to unlink the instruction from its containing basic block but
  2119. not delete it, you can use the ``removeFromParent()`` method.
  2120. .. _schanges_replacing:
  2121. Replacing an Instruction with another Value
  2122. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2123. Replacing individual instructions
  2124. """""""""""""""""""""""""""""""""
  2125. Including "`llvm/Transforms/Utils/BasicBlockUtils.h
  2126. <http://llvm.org/doxygen/BasicBlockUtils_8h_source.html>`_" permits use of two
  2127. very useful replace functions: ``ReplaceInstWithValue`` and
  2128. ``ReplaceInstWithInst``.
  2129. .. _schanges_deleting_sub:
  2130. Deleting Instructions
  2131. """""""""""""""""""""
  2132. * ``ReplaceInstWithValue``
  2133. This function replaces all uses of a given instruction with a value, and then
  2134. removes the original instruction. The following example illustrates the
  2135. replacement of the result of a particular ``AllocaInst`` that allocates memory
  2136. for a single integer with a null pointer to an integer.
  2137. .. code-block:: c++
  2138. AllocaInst* instToReplace = ...;
  2139. BasicBlock::iterator ii(instToReplace);
  2140. ReplaceInstWithValue(instToReplace->getParent()->getInstList(), ii,
  2141. Constant::getNullValue(PointerType::getUnqual(Type::Int32Ty)));
  2142. * ``ReplaceInstWithInst``
  2143. This function replaces a particular instruction with another instruction,
  2144. inserting the new instruction into the basic block at the location where the
  2145. old instruction was, and replacing any uses of the old instruction with the
  2146. new instruction. The following example illustrates the replacement of one
  2147. ``AllocaInst`` with another.
  2148. .. code-block:: c++
  2149. AllocaInst* instToReplace = ...;
  2150. BasicBlock::iterator ii(instToReplace);
  2151. ReplaceInstWithInst(instToReplace->getParent()->getInstList(), ii,
  2152. new AllocaInst(Type::Int32Ty, 0, "ptrToReplacedInt"));
  2153. Replacing multiple uses of Users and Values
  2154. """""""""""""""""""""""""""""""""""""""""""
  2155. You can use ``Value::replaceAllUsesWith`` and ``User::replaceUsesOfWith`` to
  2156. change more than one use at a time. See the doxygen documentation for the
  2157. `Value Class <http://llvm.org/doxygen/classllvm_1_1Value.html>`_ and `User Class
  2158. <http://llvm.org/doxygen/classllvm_1_1User.html>`_, respectively, for more
  2159. information.
  2160. .. _schanges_deletingGV:
  2161. Deleting GlobalVariables
  2162. ^^^^^^^^^^^^^^^^^^^^^^^^
  2163. Deleting a global variable from a module is just as easy as deleting an
  2164. Instruction. First, you must have a pointer to the global variable that you
  2165. wish to delete. You use this pointer to erase it from its parent, the module.
  2166. For example:
  2167. .. code-block:: c++
  2168. GlobalVariable *GV = .. ;
  2169. GV->eraseFromParent();
  2170. .. _create_types:
  2171. How to Create Types
  2172. -------------------
  2173. In generating IR, you may need some complex types. If you know these types
  2174. statically, you can use ``TypeBuilder<...>::get()``, defined in
  2175. ``llvm/Support/TypeBuilder.h``, to retrieve them. ``TypeBuilder`` has two forms
  2176. depending on whether you're building types for cross-compilation or native
  2177. library use. ``TypeBuilder<T, true>`` requires that ``T`` be independent of the
  2178. host environment, meaning that it's built out of types from the ``llvm::types``
  2179. (`doxygen <http://llvm.org/doxygen/namespacellvm_1_1types.html>`__) namespace
  2180. and pointers, functions, arrays, etc. built of those. ``TypeBuilder<T, false>``
  2181. additionally allows native C types whose size may depend on the host compiler.
  2182. For example,
  2183. .. code-block:: c++
  2184. FunctionType *ft = TypeBuilder<types::i<8>(types::i<32>*), true>::get();
  2185. is easier to read and write than the equivalent
  2186. .. code-block:: c++
  2187. std::vector<const Type*> params;
  2188. params.push_back(PointerType::getUnqual(Type::Int32Ty));
  2189. FunctionType *ft = FunctionType::get(Type::Int8Ty, params, false);
  2190. See the `class comment
  2191. <http://llvm.org/doxygen/TypeBuilder_8h_source.html#l00001>`_ for more details.
  2192. .. _threading:
  2193. Threads and LLVM
  2194. ================
  2195. This section describes the interaction of the LLVM APIs with multithreading,
  2196. both on the part of client applications, and in the JIT, in the hosted
  2197. application.
  2198. Note that LLVM's support for multithreading is still relatively young. Up
  2199. through version 2.5, the execution of threaded hosted applications was
  2200. supported, but not threaded client access to the APIs. While this use case is
  2201. now supported, clients *must* adhere to the guidelines specified below to ensure
  2202. proper operation in multithreaded mode.
  2203. Note that, on Unix-like platforms, LLVM requires the presence of GCC's atomic
  2204. intrinsics in order to support threaded operation. If you need a
  2205. multhreading-capable LLVM on a platform without a suitably modern system
  2206. compiler, consider compiling LLVM and LLVM-GCC in single-threaded mode, and
  2207. using the resultant compiler to build a copy of LLVM with multithreading
  2208. support.
  2209. .. _shutdown:
  2210. Ending Execution with ``llvm_shutdown()``
  2211. -----------------------------------------
  2212. When you are done using the LLVM APIs, you should call ``llvm_shutdown()`` to
  2213. deallocate memory used for internal structures.
  2214. .. _managedstatic:
  2215. Lazy Initialization with ``ManagedStatic``
  2216. ------------------------------------------
  2217. ``ManagedStatic`` is a utility class in LLVM used to implement static
  2218. initialization of static resources, such as the global type tables. In a
  2219. single-threaded environment, it implements a simple lazy initialization scheme.
  2220. When LLVM is compiled with support for multi-threading, however, it uses
  2221. double-checked locking to implement thread-safe lazy initialization.
  2222. .. _llvmcontext:
  2223. Achieving Isolation with ``LLVMContext``
  2224. ----------------------------------------
  2225. ``LLVMContext`` is an opaque class in the LLVM API which clients can use to
  2226. operate multiple, isolated instances of LLVM concurrently within the same
  2227. address space. For instance, in a hypothetical compile-server, the compilation
  2228. of an individual translation unit is conceptually independent from all the
  2229. others, and it would be desirable to be able to compile incoming translation
  2230. units concurrently on independent server threads. Fortunately, ``LLVMContext``
  2231. exists to enable just this kind of scenario!
  2232. Conceptually, ``LLVMContext`` provides isolation. Every LLVM entity
  2233. (``Module``\ s, ``Value``\ s, ``Type``\ s, ``Constant``\ s, etc.) in LLVM's
  2234. in-memory IR belongs to an ``LLVMContext``. Entities in different contexts
  2235. *cannot* interact with each other: ``Module``\ s in different contexts cannot be
  2236. linked together, ``Function``\ s cannot be added to ``Module``\ s in different
  2237. contexts, etc. What this means is that is safe to compile on multiple
  2238. threads simultaneously, as long as no two threads operate on entities within the
  2239. same context.
  2240. In practice, very few places in the API require the explicit specification of a
  2241. ``LLVMContext``, other than the ``Type`` creation/lookup APIs. Because every
  2242. ``Type`` carries a reference to its owning context, most other entities can
  2243. determine what context they belong to by looking at their own ``Type``. If you
  2244. are adding new entities to LLVM IR, please try to maintain this interface
  2245. design.
  2246. .. _jitthreading:
  2247. Threads and the JIT
  2248. -------------------
  2249. LLVM's "eager" JIT compiler is safe to use in threaded programs. Multiple
  2250. threads can call ``ExecutionEngine::getPointerToFunction()`` or
  2251. ``ExecutionEngine::runFunction()`` concurrently, and multiple threads can run
  2252. code output by the JIT concurrently. The user must still ensure that only one
  2253. thread accesses IR in a given ``LLVMContext`` while another thread might be
  2254. modifying it. One way to do that is to always hold the JIT lock while accessing
  2255. IR outside the JIT (the JIT *modifies* the IR by adding ``CallbackVH``\ s).
  2256. Another way is to only call ``getPointerToFunction()`` from the
  2257. ``LLVMContext``'s thread.
  2258. When the JIT is configured to compile lazily (using
  2259. ``ExecutionEngine::DisableLazyCompilation(false)``), there is currently a `race
  2260. condition <https://bugs.llvm.org/show_bug.cgi?id=5184>`_ in updating call sites
  2261. after a function is lazily-jitted. It's still possible to use the lazy JIT in a
  2262. threaded program if you ensure that only one thread at a time can call any
  2263. particular lazy stub and that the JIT lock guards any IR access, but we suggest
  2264. using only the eager JIT in threaded programs.
  2265. .. _advanced:
  2266. Advanced Topics
  2267. ===============
  2268. This section describes some of the advanced or obscure API's that most clients
  2269. do not need to be aware of. These API's tend manage the inner workings of the
  2270. LLVM system, and only need to be accessed in unusual circumstances.
  2271. .. _SymbolTable:
  2272. The ``ValueSymbolTable`` class
  2273. ------------------------------
  2274. The ``ValueSymbolTable`` (`doxygen
  2275. <http://llvm.org/doxygen/classllvm_1_1ValueSymbolTable.html>`__) class provides
  2276. a symbol table that the :ref:`Function <c_Function>` and Module_ classes use for
  2277. naming value definitions. The symbol table can provide a name for any Value_.
  2278. Note that the ``SymbolTable`` class should not be directly accessed by most
  2279. clients. It should only be used when iteration over the symbol table names
  2280. themselves are required, which is very special purpose. Note that not all LLVM
  2281. Value_\ s have names, and those without names (i.e. they have an empty name) do
  2282. not exist in the symbol table.
  2283. Symbol tables support iteration over the values in the symbol table with
  2284. ``begin/end/iterator`` and supports querying to see if a specific name is in the
  2285. symbol table (with ``lookup``). The ``ValueSymbolTable`` class exposes no
  2286. public mutator methods, instead, simply call ``setName`` on a value, which will
  2287. autoinsert it into the appropriate symbol table.
  2288. .. _UserLayout:
  2289. The ``User`` and owned ``Use`` classes' memory layout
  2290. -----------------------------------------------------
  2291. The ``User`` (`doxygen <http://llvm.org/doxygen/classllvm_1_1User.html>`__)
  2292. class provides a basis for expressing the ownership of ``User`` towards other
  2293. `Value instance <http://llvm.org/doxygen/classllvm_1_1Value.html>`_\ s. The
  2294. ``Use`` (`doxygen <http://llvm.org/doxygen/classllvm_1_1Use.html>`__) helper
  2295. class is employed to do the bookkeeping and to facilitate *O(1)* addition and
  2296. removal.
  2297. .. _Use2User:
  2298. Interaction and relationship between ``User`` and ``Use`` objects
  2299. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2300. A subclass of ``User`` can choose between incorporating its ``Use`` objects or
  2301. refer to them out-of-line by means of a pointer. A mixed variant (some ``Use``
  2302. s inline others hung off) is impractical and breaks the invariant that the
  2303. ``Use`` objects belonging to the same ``User`` form a contiguous array.
  2304. We have 2 different layouts in the ``User`` (sub)classes:
  2305. * Layout a)
  2306. The ``Use`` object(s) are inside (resp. at fixed offset) of the ``User``
  2307. object and there are a fixed number of them.
  2308. * Layout b)
  2309. The ``Use`` object(s) are referenced by a pointer to an array from the
  2310. ``User`` object and there may be a variable number of them.
  2311. As of v2.4 each layout still possesses a direct pointer to the start of the
  2312. array of ``Use``\ s. Though not mandatory for layout a), we stick to this
  2313. redundancy for the sake of simplicity. The ``User`` object also stores the
  2314. number of ``Use`` objects it has. (Theoretically this information can also be
  2315. calculated given the scheme presented below.)
  2316. Special forms of allocation operators (``operator new``) enforce the following
  2317. memory layouts:
  2318. * Layout a) is modelled by prepending the ``User`` object by the ``Use[]``
  2319. array.
  2320. .. code-block:: none
  2321. ...---.---.---.---.-------...
  2322. | P | P | P | P | User
  2323. '''---'---'---'---'-------'''
  2324. * Layout b) is modelled by pointing at the ``Use[]`` array.
  2325. .. code-block:: none
  2326. .-------...
  2327. | User
  2328. '-------'''
  2329. |
  2330. v
  2331. .---.---.---.---...
  2332. | P | P | P | P |
  2333. '---'---'---'---'''
  2334. *(In the above figures* '``P``' *stands for the* ``Use**`` *that is stored in
  2335. each* ``Use`` *object in the member* ``Use::Prev`` *)*
  2336. .. _Waymarking:
  2337. The waymarking algorithm
  2338. ^^^^^^^^^^^^^^^^^^^^^^^^
  2339. Since the ``Use`` objects are deprived of the direct (back)pointer to their
  2340. ``User`` objects, there must be a fast and exact method to recover it. This is
  2341. accomplished by the following scheme:
  2342. A bit-encoding in the 2 LSBits (least significant bits) of the ``Use::Prev``
  2343. allows to find the start of the ``User`` object:
  2344. * ``00`` --- binary digit 0
  2345. * ``01`` --- binary digit 1
  2346. * ``10`` --- stop and calculate (``s``)
  2347. * ``11`` --- full stop (``S``)
  2348. Given a ``Use*``, all we have to do is to walk till we get a stop and we either
  2349. have a ``User`` immediately behind or we have to walk to the next stop picking
  2350. up digits and calculating the offset:
  2351. .. code-block:: none
  2352. .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.----------------
  2353. | 1 | s | 1 | 0 | 1 | 0 | s | 1 | 1 | 0 | s | 1 | 1 | s | 1 | S | User (or User*)
  2354. '---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'----------------
  2355. |+15 |+10 |+6 |+3 |+1
  2356. | | | | | __>
  2357. | | | | __________>
  2358. | | | ______________________>
  2359. | | ______________________________________>
  2360. | __________________________________________________________>
  2361. Only the significant number of bits need to be stored between the stops, so that
  2362. the *worst case is 20 memory accesses* when there are 1000 ``Use`` objects
  2363. associated with a ``User``.
  2364. .. _ReferenceImpl:
  2365. Reference implementation
  2366. ^^^^^^^^^^^^^^^^^^^^^^^^
  2367. The following literate Haskell fragment demonstrates the concept:
  2368. .. code-block:: haskell
  2369. > import Test.QuickCheck
  2370. >
  2371. > digits :: Int -> [Char] -> [Char]
  2372. > digits 0 acc = '0' : acc
  2373. > digits 1 acc = '1' : acc
  2374. > digits n acc = digits (n `div` 2) $ digits (n `mod` 2) acc
  2375. >
  2376. > dist :: Int -> [Char] -> [Char]
  2377. > dist 0 [] = ['S']
  2378. > dist 0 acc = acc
  2379. > dist 1 acc = let r = dist 0 acc in 's' : digits (length r) r
  2380. > dist n acc = dist (n - 1) $ dist 1 acc
  2381. >
  2382. > takeLast n ss = reverse $ take n $ reverse ss
  2383. >
  2384. > test = takeLast 40 $ dist 20 []
  2385. >
  2386. Printing <test> gives: ``"1s100000s11010s10100s1111s1010s110s11s1S"``
  2387. The reverse algorithm computes the length of the string just by examining a
  2388. certain prefix:
  2389. .. code-block:: haskell
  2390. > pref :: [Char] -> Int
  2391. > pref "S" = 1
  2392. > pref ('s':'1':rest) = decode 2 1 rest
  2393. > pref (_:rest) = 1 + pref rest
  2394. >
  2395. > decode walk acc ('0':rest) = decode (walk + 1) (acc * 2) rest
  2396. > decode walk acc ('1':rest) = decode (walk + 1) (acc * 2 + 1) rest
  2397. > decode walk acc _ = walk + acc
  2398. >
  2399. Now, as expected, printing <pref test> gives ``40``.
  2400. We can *quickCheck* this with following property:
  2401. .. code-block:: haskell
  2402. > testcase = dist 2000 []
  2403. > testcaseLength = length testcase
  2404. >
  2405. > identityProp n = n > 0 && n <= testcaseLength ==> length arr == pref arr
  2406. > where arr = takeLast n testcase
  2407. >
  2408. As expected <quickCheck identityProp> gives:
  2409. ::
  2410. *Main> quickCheck identityProp
  2411. OK, passed 100 tests.
  2412. Let's be a bit more exhaustive:
  2413. .. code-block:: haskell
  2414. >
  2415. > deepCheck p = check (defaultConfig { configMaxTest = 500 }) p
  2416. >
  2417. And here is the result of <deepCheck identityProp>:
  2418. ::
  2419. *Main> deepCheck identityProp
  2420. OK, passed 500 tests.
  2421. .. _Tagging:
  2422. Tagging considerations
  2423. ^^^^^^^^^^^^^^^^^^^^^^
  2424. To maintain the invariant that the 2 LSBits of each ``Use**`` in ``Use`` never
  2425. change after being set up, setters of ``Use::Prev`` must re-tag the new
  2426. ``Use**`` on every modification. Accordingly getters must strip the tag bits.
  2427. For layout b) instead of the ``User`` we find a pointer (``User*`` with LSBit
  2428. set). Following this pointer brings us to the ``User``. A portable trick
  2429. ensures that the first bytes of ``User`` (if interpreted as a pointer) never has
  2430. the LSBit set. (Portability is relying on the fact that all known compilers
  2431. place the ``vptr`` in the first word of the instances.)
  2432. .. _polymorphism:
  2433. Designing Type Hiercharies and Polymorphic Interfaces
  2434. -----------------------------------------------------
  2435. There are two different design patterns that tend to result in the use of
  2436. virtual dispatch for methods in a type hierarchy in C++ programs. The first is
  2437. a genuine type hierarchy where different types in the hierarchy model
  2438. a specific subset of the functionality and semantics, and these types nest
  2439. strictly within each other. Good examples of this can be seen in the ``Value``
  2440. or ``Type`` type hierarchies.
  2441. A second is the desire to dispatch dynamically across a collection of
  2442. polymorphic interface implementations. This latter use case can be modeled with
  2443. virtual dispatch and inheritance by defining an abstract interface base class
  2444. which all implementations derive from and override. However, this
  2445. implementation strategy forces an **"is-a"** relationship to exist that is not
  2446. actually meaningful. There is often not some nested hierarchy of useful
  2447. generalizations which code might interact with and move up and down. Instead,
  2448. there is a singular interface which is dispatched across a range of
  2449. implementations.
  2450. The preferred implementation strategy for the second use case is that of
  2451. generic programming (sometimes called "compile-time duck typing" or "static
  2452. polymorphism"). For example, a template over some type parameter ``T`` can be
  2453. instantiated across any particular implementation that conforms to the
  2454. interface or *concept*. A good example here is the highly generic properties of
  2455. any type which models a node in a directed graph. LLVM models these primarily
  2456. through templates and generic programming. Such templates include the
  2457. ``LoopInfoBase`` and ``DominatorTreeBase``. When this type of polymorphism
  2458. truly needs **dynamic** dispatch you can generalize it using a technique
  2459. called *concept-based polymorphism*. This pattern emulates the interfaces and
  2460. behaviors of templates using a very limited form of virtual dispatch for type
  2461. erasure inside its implementation. You can find examples of this technique in
  2462. the ``PassManager.h`` system, and there is a more detailed introduction to it
  2463. by Sean Parent in several of his talks and papers:
  2464. #. `Inheritance Is The Base Class of Evil
  2465. <http://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil>`_
  2466. - The GoingNative 2013 talk describing this technique, and probably the best
  2467. place to start.
  2468. #. `Value Semantics and Concepts-based Polymorphism
  2469. <http://www.youtube.com/watch?v=_BpMYeUFXv8>`_ - The C++Now! 2012 talk
  2470. describing this technique in more detail.
  2471. #. `Sean Parent's Papers and Presentations
  2472. <http://github.com/sean-parent/sean-parent.github.com/wiki/Papers-and-Presentations>`_
  2473. - A Github project full of links to slides, video, and sometimes code.
  2474. When deciding between creating a type hierarchy (with either tagged or virtual
  2475. dispatch) and using templates or concepts-based polymorphism, consider whether
  2476. there is some refinement of an abstract base class which is a semantically
  2477. meaningful type on an interface boundary. If anything more refined than the
  2478. root abstract interface is meaningless to talk about as a partial extension of
  2479. the semantic model, then your use case likely fits better with polymorphism and
  2480. you should avoid using virtual dispatch. However, there may be some exigent
  2481. circumstances that require one technique or the other to be used.
  2482. If you do need to introduce a type hierarchy, we prefer to use explicitly
  2483. closed type hierarchies with manual tagged dispatch and/or RTTI rather than the
  2484. open inheritance model and virtual dispatch that is more common in C++ code.
  2485. This is because LLVM rarely encourages library consumers to extend its core
  2486. types, and leverages the closed and tag-dispatched nature of its hierarchies to
  2487. generate significantly more efficient code. We have also found that a large
  2488. amount of our usage of type hierarchies fits better with tag-based pattern
  2489. matching rather than dynamic dispatch across a common interface. Within LLVM we
  2490. have built custom helpers to facilitate this design. See this document's
  2491. section on :ref:`isa and dyn_cast <isa>` and our :doc:`detailed document
  2492. <HowToSetUpLLVMStyleRTTI>` which describes how you can implement this
  2493. pattern for use with the LLVM helpers.
  2494. .. _abi_breaking_checks:
  2495. ABI Breaking Checks
  2496. -------------------
  2497. Checks and asserts that alter the LLVM C++ ABI are predicated on the
  2498. preprocessor symbol `LLVM_ENABLE_ABI_BREAKING_CHECKS` -- LLVM
  2499. libraries built with `LLVM_ENABLE_ABI_BREAKING_CHECKS` are not ABI
  2500. compatible LLVM libraries built without it defined. By default,
  2501. turning on assertions also turns on `LLVM_ENABLE_ABI_BREAKING_CHECKS`
  2502. so a default +Asserts build is not ABI compatible with a
  2503. default -Asserts build. Clients that want ABI compatibility
  2504. between +Asserts and -Asserts builds should use the CMake or autoconf
  2505. build systems to set `LLVM_ENABLE_ABI_BREAKING_CHECKS` independently
  2506. of `LLVM_ENABLE_ASSERTIONS`.
  2507. .. _coreclasses:
  2508. The Core LLVM Class Hierarchy Reference
  2509. =======================================
  2510. ``#include "llvm/IR/Type.h"``
  2511. header source: `Type.h <http://llvm.org/doxygen/Type_8h_source.html>`_
  2512. doxygen info: `Type Clases <http://llvm.org/doxygen/classllvm_1_1Type.html>`_
  2513. The Core LLVM classes are the primary means of representing the program being
  2514. inspected or transformed. The core LLVM classes are defined in header files in
  2515. the ``include/llvm/IR`` directory, and implemented in the ``lib/IR``
  2516. directory. It's worth noting that, for historical reasons, this library is
  2517. called ``libLLVMCore.so``, not ``libLLVMIR.so`` as you might expect.
  2518. .. _Type:
  2519. The Type class and Derived Types
  2520. --------------------------------
  2521. ``Type`` is a superclass of all type classes. Every ``Value`` has a ``Type``.
  2522. ``Type`` cannot be instantiated directly but only through its subclasses.
  2523. Certain primitive types (``VoidType``, ``LabelType``, ``FloatType`` and
  2524. ``DoubleType``) have hidden subclasses. They are hidden because they offer no
  2525. useful functionality beyond what the ``Type`` class offers except to distinguish
  2526. themselves from other subclasses of ``Type``.
  2527. All other types are subclasses of ``DerivedType``. Types can be named, but this
  2528. is not a requirement. There exists exactly one instance of a given shape at any
  2529. one time. This allows type equality to be performed with address equality of
  2530. the Type Instance. That is, given two ``Type*`` values, the types are identical
  2531. if the pointers are identical.
  2532. .. _m_Type:
  2533. Important Public Methods
  2534. ^^^^^^^^^^^^^^^^^^^^^^^^
  2535. * ``bool isIntegerTy() const``: Returns true for any integer type.
  2536. * ``bool isFloatingPointTy()``: Return true if this is one of the five
  2537. floating point types.
  2538. * ``bool isSized()``: Return true if the type has known size. Things
  2539. that don't have a size are abstract types, labels and void.
  2540. .. _derivedtypes:
  2541. Important Derived Types
  2542. ^^^^^^^^^^^^^^^^^^^^^^^
  2543. ``IntegerType``
  2544. Subclass of DerivedType that represents integer types of any bit width. Any
  2545. bit width between ``IntegerType::MIN_INT_BITS`` (1) and
  2546. ``IntegerType::MAX_INT_BITS`` (~8 million) can be represented.
  2547. * ``static const IntegerType* get(unsigned NumBits)``: get an integer
  2548. type of a specific bit width.
  2549. * ``unsigned getBitWidth() const``: Get the bit width of an integer type.
  2550. ``SequentialType``
  2551. This is subclassed by ArrayType and VectorType.
  2552. * ``const Type * getElementType() const``: Returns the type of each
  2553. of the elements in the sequential type.
  2554. * ``uint64_t getNumElements() const``: Returns the number of elements
  2555. in the sequential type.
  2556. ``ArrayType``
  2557. This is a subclass of SequentialType and defines the interface for array
  2558. types.
  2559. ``PointerType``
  2560. Subclass of Type for pointer types.
  2561. ``VectorType``
  2562. Subclass of SequentialType for vector types. A vector type is similar to an
  2563. ArrayType but is distinguished because it is a first class type whereas
  2564. ArrayType is not. Vector types are used for vector operations and are usually
  2565. small vectors of an integer or floating point type.
  2566. ``StructType``
  2567. Subclass of DerivedTypes for struct types.
  2568. .. _FunctionType:
  2569. ``FunctionType``
  2570. Subclass of DerivedTypes for function types.
  2571. * ``bool isVarArg() const``: Returns true if it's a vararg function.
  2572. * ``const Type * getReturnType() const``: Returns the return type of the
  2573. function.
  2574. * ``const Type * getParamType (unsigned i)``: Returns the type of the ith
  2575. parameter.
  2576. * ``const unsigned getNumParams() const``: Returns the number of formal
  2577. parameters.
  2578. .. _Module:
  2579. The ``Module`` class
  2580. --------------------
  2581. ``#include "llvm/IR/Module.h"``
  2582. header source: `Module.h <http://llvm.org/doxygen/Module_8h_source.html>`_
  2583. doxygen info: `Module Class <http://llvm.org/doxygen/classllvm_1_1Module.html>`_
  2584. The ``Module`` class represents the top level structure present in LLVM
  2585. programs. An LLVM module is effectively either a translation unit of the
  2586. original program or a combination of several translation units merged by the
  2587. linker. The ``Module`` class keeps track of a list of :ref:`Function
  2588. <c_Function>`\ s, a list of GlobalVariable_\ s, and a SymbolTable_.
  2589. Additionally, it contains a few helpful member functions that try to make common
  2590. operations easy.
  2591. .. _m_Module:
  2592. Important Public Members of the ``Module`` class
  2593. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2594. * ``Module::Module(std::string name = "")``
  2595. Constructing a Module_ is easy. You can optionally provide a name for it
  2596. (probably based on the name of the translation unit).
  2597. * | ``Module::iterator`` - Typedef for function list iterator
  2598. | ``Module::const_iterator`` - Typedef for const_iterator.
  2599. | ``begin()``, ``end()``, ``size()``, ``empty()``
  2600. These are forwarding methods that make it easy to access the contents of a
  2601. ``Module`` object's :ref:`Function <c_Function>` list.
  2602. * ``Module::FunctionListType &getFunctionList()``
  2603. Returns the list of :ref:`Function <c_Function>`\ s. This is necessary to use
  2604. when you need to update the list or perform a complex action that doesn't have
  2605. a forwarding method.
  2606. ----------------
  2607. * | ``Module::global_iterator`` - Typedef for global variable list iterator
  2608. | ``Module::const_global_iterator`` - Typedef for const_iterator.
  2609. | ``global_begin()``, ``global_end()``, ``global_size()``, ``global_empty()``
  2610. These are forwarding methods that make it easy to access the contents of a
  2611. ``Module`` object's GlobalVariable_ list.
  2612. * ``Module::GlobalListType &getGlobalList()``
  2613. Returns the list of GlobalVariable_\ s. This is necessary to use when you
  2614. need to update the list or perform a complex action that doesn't have a
  2615. forwarding method.
  2616. ----------------
  2617. * ``SymbolTable *getSymbolTable()``
  2618. Return a reference to the SymbolTable_ for this ``Module``.
  2619. ----------------
  2620. * ``Function *getFunction(StringRef Name) const``
  2621. Look up the specified function in the ``Module`` SymbolTable_. If it does not
  2622. exist, return ``null``.
  2623. * ``Function *getOrInsertFunction(const std::string &Name, const FunctionType
  2624. *T)``
  2625. Look up the specified function in the ``Module`` SymbolTable_. If it does not
  2626. exist, add an external declaration for the function and return it.
  2627. * ``std::string getTypeName(const Type *Ty)``
  2628. If there is at least one entry in the SymbolTable_ for the specified Type_,
  2629. return it. Otherwise return the empty string.
  2630. * ``bool addTypeName(const std::string &Name, const Type *Ty)``
  2631. Insert an entry in the SymbolTable_ mapping ``Name`` to ``Ty``. If there is
  2632. already an entry for this name, true is returned and the SymbolTable_ is not
  2633. modified.
  2634. .. _Value:
  2635. The ``Value`` class
  2636. -------------------
  2637. ``#include "llvm/IR/Value.h"``
  2638. header source: `Value.h <http://llvm.org/doxygen/Value_8h_source.html>`_
  2639. doxygen info: `Value Class <http://llvm.org/doxygen/classllvm_1_1Value.html>`_
  2640. The ``Value`` class is the most important class in the LLVM Source base. It
  2641. represents a typed value that may be used (among other things) as an operand to
  2642. an instruction. There are many different types of ``Value``\ s, such as
  2643. Constant_\ s, Argument_\ s. Even Instruction_\ s and :ref:`Function
  2644. <c_Function>`\ s are ``Value``\ s.
  2645. A particular ``Value`` may be used many times in the LLVM representation for a
  2646. program. For example, an incoming argument to a function (represented with an
  2647. instance of the Argument_ class) is "used" by every instruction in the function
  2648. that references the argument. To keep track of this relationship, the ``Value``
  2649. class keeps a list of all of the ``User``\ s that is using it (the User_ class
  2650. is a base class for all nodes in the LLVM graph that can refer to ``Value``\ s).
  2651. This use list is how LLVM represents def-use information in the program, and is
  2652. accessible through the ``use_*`` methods, shown below.
  2653. Because LLVM is a typed representation, every LLVM ``Value`` is typed, and this
  2654. Type_ is available through the ``getType()`` method. In addition, all LLVM
  2655. values can be named. The "name" of the ``Value`` is a symbolic string printed
  2656. in the LLVM code:
  2657. .. code-block:: llvm
  2658. %foo = add i32 1, 2
  2659. .. _nameWarning:
  2660. The name of this instruction is "foo". **NOTE** that the name of any value may
  2661. be missing (an empty string), so names should **ONLY** be used for debugging
  2662. (making the source code easier to read, debugging printouts), they should not be
  2663. used to keep track of values or map between them. For this purpose, use a
  2664. ``std::map`` of pointers to the ``Value`` itself instead.
  2665. One important aspect of LLVM is that there is no distinction between an SSA
  2666. variable and the operation that produces it. Because of this, any reference to
  2667. the value produced by an instruction (or the value available as an incoming
  2668. argument, for example) is represented as a direct pointer to the instance of the
  2669. class that represents this value. Although this may take some getting used to,
  2670. it simplifies the representation and makes it easier to manipulate.
  2671. .. _m_Value:
  2672. Important Public Members of the ``Value`` class
  2673. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2674. * | ``Value::use_iterator`` - Typedef for iterator over the use-list
  2675. | ``Value::const_use_iterator`` - Typedef for const_iterator over the
  2676. use-list
  2677. | ``unsigned use_size()`` - Returns the number of users of the value.
  2678. | ``bool use_empty()`` - Returns true if there are no users.
  2679. | ``use_iterator use_begin()`` - Get an iterator to the start of the
  2680. use-list.
  2681. | ``use_iterator use_end()`` - Get an iterator to the end of the use-list.
  2682. | ``User *use_back()`` - Returns the last element in the list.
  2683. These methods are the interface to access the def-use information in LLVM.
  2684. As with all other iterators in LLVM, the naming conventions follow the
  2685. conventions defined by the STL_.
  2686. * ``Type *getType() const``
  2687. This method returns the Type of the Value.
  2688. * | ``bool hasName() const``
  2689. | ``std::string getName() const``
  2690. | ``void setName(const std::string &Name)``
  2691. This family of methods is used to access and assign a name to a ``Value``, be
  2692. aware of the :ref:`precaution above <nameWarning>`.
  2693. * ``void replaceAllUsesWith(Value *V)``
  2694. This method traverses the use list of a ``Value`` changing all User_\ s of the
  2695. current value to refer to "``V``" instead. For example, if you detect that an
  2696. instruction always produces a constant value (for example through constant
  2697. folding), you can replace all uses of the instruction with the constant like
  2698. this:
  2699. .. code-block:: c++
  2700. Inst->replaceAllUsesWith(ConstVal);
  2701. .. _User:
  2702. The ``User`` class
  2703. ------------------
  2704. ``#include "llvm/IR/User.h"``
  2705. header source: `User.h <http://llvm.org/doxygen/User_8h_source.html>`_
  2706. doxygen info: `User Class <http://llvm.org/doxygen/classllvm_1_1User.html>`_
  2707. Superclass: Value_
  2708. The ``User`` class is the common base class of all LLVM nodes that may refer to
  2709. ``Value``\ s. It exposes a list of "Operands" that are all of the ``Value``\ s
  2710. that the User is referring to. The ``User`` class itself is a subclass of
  2711. ``Value``.
  2712. The operands of a ``User`` point directly to the LLVM ``Value`` that it refers
  2713. to. Because LLVM uses Static Single Assignment (SSA) form, there can only be
  2714. one definition referred to, allowing this direct connection. This connection
  2715. provides the use-def information in LLVM.
  2716. .. _m_User:
  2717. Important Public Members of the ``User`` class
  2718. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2719. The ``User`` class exposes the operand list in two ways: through an index access
  2720. interface and through an iterator based interface.
  2721. * | ``Value *getOperand(unsigned i)``
  2722. | ``unsigned getNumOperands()``
  2723. These two methods expose the operands of the ``User`` in a convenient form for
  2724. direct access.
  2725. * | ``User::op_iterator`` - Typedef for iterator over the operand list
  2726. | ``op_iterator op_begin()`` - Get an iterator to the start of the operand
  2727. list.
  2728. | ``op_iterator op_end()`` - Get an iterator to the end of the operand list.
  2729. Together, these methods make up the iterator based interface to the operands
  2730. of a ``User``.
  2731. .. _Instruction:
  2732. The ``Instruction`` class
  2733. -------------------------
  2734. ``#include "llvm/IR/Instruction.h"``
  2735. header source: `Instruction.h
  2736. <http://llvm.org/doxygen/Instruction_8h_source.html>`_
  2737. doxygen info: `Instruction Class
  2738. <http://llvm.org/doxygen/classllvm_1_1Instruction.html>`_
  2739. Superclasses: User_, Value_
  2740. The ``Instruction`` class is the common base class for all LLVM instructions.
  2741. It provides only a few methods, but is a very commonly used class. The primary
  2742. data tracked by the ``Instruction`` class itself is the opcode (instruction
  2743. type) and the parent BasicBlock_ the ``Instruction`` is embedded into. To
  2744. represent a specific type of instruction, one of many subclasses of
  2745. ``Instruction`` are used.
  2746. Because the ``Instruction`` class subclasses the User_ class, its operands can
  2747. be accessed in the same way as for other ``User``\ s (with the
  2748. ``getOperand()``/``getNumOperands()`` and ``op_begin()``/``op_end()`` methods).
  2749. An important file for the ``Instruction`` class is the ``llvm/Instruction.def``
  2750. file. This file contains some meta-data about the various different types of
  2751. instructions in LLVM. It describes the enum values that are used as opcodes
  2752. (for example ``Instruction::Add`` and ``Instruction::ICmp``), as well as the
  2753. concrete sub-classes of ``Instruction`` that implement the instruction (for
  2754. example BinaryOperator_ and CmpInst_). Unfortunately, the use of macros in this
  2755. file confuses doxygen, so these enum values don't show up correctly in the
  2756. `doxygen output <http://llvm.org/doxygen/classllvm_1_1Instruction.html>`_.
  2757. .. _s_Instruction:
  2758. Important Subclasses of the ``Instruction`` class
  2759. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2760. .. _BinaryOperator:
  2761. * ``BinaryOperator``
  2762. This subclasses represents all two operand instructions whose operands must be
  2763. the same type, except for the comparison instructions.
  2764. .. _CastInst:
  2765. * ``CastInst``
  2766. This subclass is the parent of the 12 casting instructions. It provides
  2767. common operations on cast instructions.
  2768. .. _CmpInst:
  2769. * ``CmpInst``
  2770. This subclass represents the two comparison instructions,
  2771. `ICmpInst <LangRef.html#i_icmp>`_ (integer opreands), and
  2772. `FCmpInst <LangRef.html#i_fcmp>`_ (floating point operands).
  2773. .. _m_Instruction:
  2774. Important Public Members of the ``Instruction`` class
  2775. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2776. * ``BasicBlock *getParent()``
  2777. Returns the BasicBlock_ that this
  2778. ``Instruction`` is embedded into.
  2779. * ``bool mayWriteToMemory()``
  2780. Returns true if the instruction writes to memory, i.e. it is a ``call``,
  2781. ``free``, ``invoke``, or ``store``.
  2782. * ``unsigned getOpcode()``
  2783. Returns the opcode for the ``Instruction``.
  2784. * ``Instruction *clone() const``
  2785. Returns another instance of the specified instruction, identical in all ways
  2786. to the original except that the instruction has no parent (i.e. it's not
  2787. embedded into a BasicBlock_), and it has no name.
  2788. .. _Constant:
  2789. The ``Constant`` class and subclasses
  2790. -------------------------------------
  2791. Constant represents a base class for different types of constants. It is
  2792. subclassed by ConstantInt, ConstantArray, etc. for representing the various
  2793. types of Constants. GlobalValue_ is also a subclass, which represents the
  2794. address of a global variable or function.
  2795. .. _s_Constant:
  2796. Important Subclasses of Constant
  2797. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2798. * ConstantInt : This subclass of Constant represents an integer constant of
  2799. any width.
  2800. * ``const APInt& getValue() const``: Returns the underlying
  2801. value of this constant, an APInt value.
  2802. * ``int64_t getSExtValue() const``: Converts the underlying APInt value to an
  2803. int64_t via sign extension. If the value (not the bit width) of the APInt
  2804. is too large to fit in an int64_t, an assertion will result. For this
  2805. reason, use of this method is discouraged.
  2806. * ``uint64_t getZExtValue() const``: Converts the underlying APInt value
  2807. to a uint64_t via zero extension. IF the value (not the bit width) of the
  2808. APInt is too large to fit in a uint64_t, an assertion will result. For this
  2809. reason, use of this method is discouraged.
  2810. * ``static ConstantInt* get(const APInt& Val)``: Returns the ConstantInt
  2811. object that represents the value provided by ``Val``. The type is implied
  2812. as the IntegerType that corresponds to the bit width of ``Val``.
  2813. * ``static ConstantInt* get(const Type *Ty, uint64_t Val)``: Returns the
  2814. ConstantInt object that represents the value provided by ``Val`` for integer
  2815. type ``Ty``.
  2816. * ConstantFP : This class represents a floating point constant.
  2817. * ``double getValue() const``: Returns the underlying value of this constant.
  2818. * ConstantArray : This represents a constant array.
  2819. * ``const std::vector<Use> &getValues() const``: Returns a vector of
  2820. component constants that makeup this array.
  2821. * ConstantStruct : This represents a constant struct.
  2822. * ``const std::vector<Use> &getValues() const``: Returns a vector of
  2823. component constants that makeup this array.
  2824. * GlobalValue : This represents either a global variable or a function. In
  2825. either case, the value is a constant fixed address (after linking).
  2826. .. _GlobalValue:
  2827. The ``GlobalValue`` class
  2828. -------------------------
  2829. ``#include "llvm/IR/GlobalValue.h"``
  2830. header source: `GlobalValue.h
  2831. <http://llvm.org/doxygen/GlobalValue_8h_source.html>`_
  2832. doxygen info: `GlobalValue Class
  2833. <http://llvm.org/doxygen/classllvm_1_1GlobalValue.html>`_
  2834. Superclasses: Constant_, User_, Value_
  2835. Global values ( GlobalVariable_\ s or :ref:`Function <c_Function>`\ s) are the
  2836. only LLVM values that are visible in the bodies of all :ref:`Function
  2837. <c_Function>`\ s. Because they are visible at global scope, they are also
  2838. subject to linking with other globals defined in different translation units.
  2839. To control the linking process, ``GlobalValue``\ s know their linkage rules.
  2840. Specifically, ``GlobalValue``\ s know whether they have internal or external
  2841. linkage, as defined by the ``LinkageTypes`` enumeration.
  2842. If a ``GlobalValue`` has internal linkage (equivalent to being ``static`` in C),
  2843. it is not visible to code outside the current translation unit, and does not
  2844. participate in linking. If it has external linkage, it is visible to external
  2845. code, and does participate in linking. In addition to linkage information,
  2846. ``GlobalValue``\ s keep track of which Module_ they are currently part of.
  2847. Because ``GlobalValue``\ s are memory objects, they are always referred to by
  2848. their **address**. As such, the Type_ of a global is always a pointer to its
  2849. contents. It is important to remember this when using the ``GetElementPtrInst``
  2850. instruction because this pointer must be dereferenced first. For example, if
  2851. you have a ``GlobalVariable`` (a subclass of ``GlobalValue)`` that is an array
  2852. of 24 ints, type ``[24 x i32]``, then the ``GlobalVariable`` is a pointer to
  2853. that array. Although the address of the first element of this array and the
  2854. value of the ``GlobalVariable`` are the same, they have different types. The
  2855. ``GlobalVariable``'s type is ``[24 x i32]``. The first element's type is
  2856. ``i32.`` Because of this, accessing a global value requires you to dereference
  2857. the pointer with ``GetElementPtrInst`` first, then its elements can be accessed.
  2858. This is explained in the `LLVM Language Reference Manual
  2859. <LangRef.html#globalvars>`_.
  2860. .. _m_GlobalValue:
  2861. Important Public Members of the ``GlobalValue`` class
  2862. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2863. * | ``bool hasInternalLinkage() const``
  2864. | ``bool hasExternalLinkage() const``
  2865. | ``void setInternalLinkage(bool HasInternalLinkage)``
  2866. These methods manipulate the linkage characteristics of the ``GlobalValue``.
  2867. * ``Module *getParent()``
  2868. This returns the Module_ that the
  2869. GlobalValue is currently embedded into.
  2870. .. _c_Function:
  2871. The ``Function`` class
  2872. ----------------------
  2873. ``#include "llvm/IR/Function.h"``
  2874. header source: `Function.h <http://llvm.org/doxygen/Function_8h_source.html>`_
  2875. doxygen info: `Function Class
  2876. <http://llvm.org/doxygen/classllvm_1_1Function.html>`_
  2877. Superclasses: GlobalValue_, Constant_, User_, Value_
  2878. The ``Function`` class represents a single procedure in LLVM. It is actually
  2879. one of the more complex classes in the LLVM hierarchy because it must keep track
  2880. of a large amount of data. The ``Function`` class keeps track of a list of
  2881. BasicBlock_\ s, a list of formal Argument_\ s, and a SymbolTable_.
  2882. The list of BasicBlock_\ s is the most commonly used part of ``Function``
  2883. objects. The list imposes an implicit ordering of the blocks in the function,
  2884. which indicate how the code will be laid out by the backend. Additionally, the
  2885. first BasicBlock_ is the implicit entry node for the ``Function``. It is not
  2886. legal in LLVM to explicitly branch to this initial block. There are no implicit
  2887. exit nodes, and in fact there may be multiple exit nodes from a single
  2888. ``Function``. If the BasicBlock_ list is empty, this indicates that the
  2889. ``Function`` is actually a function declaration: the actual body of the function
  2890. hasn't been linked in yet.
  2891. In addition to a list of BasicBlock_\ s, the ``Function`` class also keeps track
  2892. of the list of formal Argument_\ s that the function receives. This container
  2893. manages the lifetime of the Argument_ nodes, just like the BasicBlock_ list does
  2894. for the BasicBlock_\ s.
  2895. The SymbolTable_ is a very rarely used LLVM feature that is only used when you
  2896. have to look up a value by name. Aside from that, the SymbolTable_ is used
  2897. internally to make sure that there are not conflicts between the names of
  2898. Instruction_\ s, BasicBlock_\ s, or Argument_\ s in the function body.
  2899. Note that ``Function`` is a GlobalValue_ and therefore also a Constant_. The
  2900. value of the function is its address (after linking) which is guaranteed to be
  2901. constant.
  2902. .. _m_Function:
  2903. Important Public Members of the ``Function``
  2904. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2905. * ``Function(const FunctionType *Ty, LinkageTypes Linkage,
  2906. const std::string &N = "", Module* Parent = 0)``
  2907. Constructor used when you need to create new ``Function``\ s to add the
  2908. program. The constructor must specify the type of the function to create and
  2909. what type of linkage the function should have. The FunctionType_ argument
  2910. specifies the formal arguments and return value for the function. The same
  2911. FunctionType_ value can be used to create multiple functions. The ``Parent``
  2912. argument specifies the Module in which the function is defined. If this
  2913. argument is provided, the function will automatically be inserted into that
  2914. module's list of functions.
  2915. * ``bool isDeclaration()``
  2916. Return whether or not the ``Function`` has a body defined. If the function is
  2917. "external", it does not have a body, and thus must be resolved by linking with
  2918. a function defined in a different translation unit.
  2919. * | ``Function::iterator`` - Typedef for basic block list iterator
  2920. | ``Function::const_iterator`` - Typedef for const_iterator.
  2921. | ``begin()``, ``end()``, ``size()``, ``empty()``
  2922. These are forwarding methods that make it easy to access the contents of a
  2923. ``Function`` object's BasicBlock_ list.
  2924. * ``Function::BasicBlockListType &getBasicBlockList()``
  2925. Returns the list of BasicBlock_\ s. This is necessary to use when you need to
  2926. update the list or perform a complex action that doesn't have a forwarding
  2927. method.
  2928. * | ``Function::arg_iterator`` - Typedef for the argument list iterator
  2929. | ``Function::const_arg_iterator`` - Typedef for const_iterator.
  2930. | ``arg_begin()``, ``arg_end()``, ``arg_size()``, ``arg_empty()``
  2931. These are forwarding methods that make it easy to access the contents of a
  2932. ``Function`` object's Argument_ list.
  2933. * ``Function::ArgumentListType &getArgumentList()``
  2934. Returns the list of Argument_. This is necessary to use when you need to
  2935. update the list or perform a complex action that doesn't have a forwarding
  2936. method.
  2937. * ``BasicBlock &getEntryBlock()``
  2938. Returns the entry ``BasicBlock`` for the function. Because the entry block
  2939. for the function is always the first block, this returns the first block of
  2940. the ``Function``.
  2941. * | ``Type *getReturnType()``
  2942. | ``FunctionType *getFunctionType()``
  2943. This traverses the Type_ of the ``Function`` and returns the return type of
  2944. the function, or the FunctionType_ of the actual function.
  2945. * ``SymbolTable *getSymbolTable()``
  2946. Return a pointer to the SymbolTable_ for this ``Function``.
  2947. .. _GlobalVariable:
  2948. The ``GlobalVariable`` class
  2949. ----------------------------
  2950. ``#include "llvm/IR/GlobalVariable.h"``
  2951. header source: `GlobalVariable.h
  2952. <http://llvm.org/doxygen/GlobalVariable_8h_source.html>`_
  2953. doxygen info: `GlobalVariable Class
  2954. <http://llvm.org/doxygen/classllvm_1_1GlobalVariable.html>`_
  2955. Superclasses: GlobalValue_, Constant_, User_, Value_
  2956. Global variables are represented with the (surprise surprise) ``GlobalVariable``
  2957. class. Like functions, ``GlobalVariable``\ s are also subclasses of
  2958. GlobalValue_, and as such are always referenced by their address (global values
  2959. must live in memory, so their "name" refers to their constant address). See
  2960. GlobalValue_ for more on this. Global variables may have an initial value
  2961. (which must be a Constant_), and if they have an initializer, they may be marked
  2962. as "constant" themselves (indicating that their contents never change at
  2963. runtime).
  2964. .. _m_GlobalVariable:
  2965. Important Public Members of the ``GlobalVariable`` class
  2966. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2967. * ``GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes &Linkage,
  2968. Constant *Initializer = 0, const std::string &Name = "", Module* Parent = 0)``
  2969. Create a new global variable of the specified type. If ``isConstant`` is true
  2970. then the global variable will be marked as unchanging for the program. The
  2971. Linkage parameter specifies the type of linkage (internal, external, weak,
  2972. linkonce, appending) for the variable. If the linkage is InternalLinkage,
  2973. WeakAnyLinkage, WeakODRLinkage, LinkOnceAnyLinkage or LinkOnceODRLinkage, then
  2974. the resultant global variable will have internal linkage. AppendingLinkage
  2975. concatenates together all instances (in different translation units) of the
  2976. variable into a single variable but is only applicable to arrays. See the
  2977. `LLVM Language Reference <LangRef.html#modulestructure>`_ for further details
  2978. on linkage types. Optionally an initializer, a name, and the module to put
  2979. the variable into may be specified for the global variable as well.
  2980. * ``bool isConstant() const``
  2981. Returns true if this is a global variable that is known not to be modified at
  2982. runtime.
  2983. * ``bool hasInitializer()``
  2984. Returns true if this ``GlobalVariable`` has an intializer.
  2985. * ``Constant *getInitializer()``
  2986. Returns the initial value for a ``GlobalVariable``. It is not legal to call
  2987. this method if there is no initializer.
  2988. .. _BasicBlock:
  2989. The ``BasicBlock`` class
  2990. ------------------------
  2991. ``#include "llvm/IR/BasicBlock.h"``
  2992. header source: `BasicBlock.h
  2993. <http://llvm.org/doxygen/BasicBlock_8h_source.html>`_
  2994. doxygen info: `BasicBlock Class
  2995. <http://llvm.org/doxygen/classllvm_1_1BasicBlock.html>`_
  2996. Superclass: Value_
  2997. This class represents a single entry single exit section of the code, commonly
  2998. known as a basic block by the compiler community. The ``BasicBlock`` class
  2999. maintains a list of Instruction_\ s, which form the body of the block. Matching
  3000. the language definition, the last element of this list of instructions is always
  3001. a terminator instruction.
  3002. In addition to tracking the list of instructions that make up the block, the
  3003. ``BasicBlock`` class also keeps track of the :ref:`Function <c_Function>` that
  3004. it is embedded into.
  3005. Note that ``BasicBlock``\ s themselves are Value_\ s, because they are
  3006. referenced by instructions like branches and can go in the switch tables.
  3007. ``BasicBlock``\ s have type ``label``.
  3008. .. _m_BasicBlock:
  3009. Important Public Members of the ``BasicBlock`` class
  3010. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  3011. * ``BasicBlock(const std::string &Name = "", Function *Parent = 0)``
  3012. The ``BasicBlock`` constructor is used to create new basic blocks for
  3013. insertion into a function. The constructor optionally takes a name for the
  3014. new block, and a :ref:`Function <c_Function>` to insert it into. If the
  3015. ``Parent`` parameter is specified, the new ``BasicBlock`` is automatically
  3016. inserted at the end of the specified :ref:`Function <c_Function>`, if not
  3017. specified, the BasicBlock must be manually inserted into the :ref:`Function
  3018. <c_Function>`.
  3019. * | ``BasicBlock::iterator`` - Typedef for instruction list iterator
  3020. | ``BasicBlock::const_iterator`` - Typedef for const_iterator.
  3021. | ``begin()``, ``end()``, ``front()``, ``back()``,
  3022. ``size()``, ``empty()``
  3023. STL-style functions for accessing the instruction list.
  3024. These methods and typedefs are forwarding functions that have the same
  3025. semantics as the standard library methods of the same names. These methods
  3026. expose the underlying instruction list of a basic block in a way that is easy
  3027. to manipulate. To get the full complement of container operations (including
  3028. operations to update the list), you must use the ``getInstList()`` method.
  3029. * ``BasicBlock::InstListType &getInstList()``
  3030. This method is used to get access to the underlying container that actually
  3031. holds the Instructions. This method must be used when there isn't a
  3032. forwarding function in the ``BasicBlock`` class for the operation that you
  3033. would like to perform. Because there are no forwarding functions for
  3034. "updating" operations, you need to use this if you want to update the contents
  3035. of a ``BasicBlock``.
  3036. * ``Function *getParent()``
  3037. Returns a pointer to :ref:`Function <c_Function>` the block is embedded into,
  3038. or a null pointer if it is homeless.
  3039. * ``Instruction *getTerminator()``
  3040. Returns a pointer to the terminator instruction that appears at the end of the
  3041. ``BasicBlock``. If there is no terminator instruction, or if the last
  3042. instruction in the block is not a terminator, then a null pointer is returned.
  3043. .. _Argument:
  3044. The ``Argument`` class
  3045. ----------------------
  3046. This subclass of Value defines the interface for incoming formal arguments to a
  3047. function. A Function maintains a list of its formal arguments. An argument has
  3048. a pointer to the parent Function.