WritingAnLLVMPass.rst 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. ====================
  2. Writing an LLVM Pass
  3. ====================
  4. .. contents::
  5. :local:
  6. Introduction --- What is a pass?
  7. ================================
  8. NOTE: this document describes the instructions for LLVM, not the DirectX
  9. Compiler. There are a few additional requirements to have a pass behave
  10. properly with other compiler components.
  11. The LLVM Pass Framework is an important part of the LLVM system, because LLVM
  12. passes are where most of the interesting parts of the compiler exist. Passes
  13. perform the transformations and optimizations that make up the compiler, they
  14. build the analysis results that are used by these transformations, and they
  15. are, above all, a structuring technique for compiler code.
  16. All LLVM passes are subclasses of the `Pass
  17. <http://llvm.org/doxygen/classllvm_1_1Pass.html>`_ class, which implement
  18. functionality by overriding virtual methods inherited from ``Pass``. Depending
  19. on how your pass works, you should inherit from the :ref:`ModulePass
  20. <writing-an-llvm-pass-ModulePass>` , :ref:`CallGraphSCCPass
  21. <writing-an-llvm-pass-CallGraphSCCPass>`, :ref:`FunctionPass
  22. <writing-an-llvm-pass-FunctionPass>` , or :ref:`LoopPass
  23. <writing-an-llvm-pass-LoopPass>`, or :ref:`RegionPass
  24. <writing-an-llvm-pass-RegionPass>`, or :ref:`BasicBlockPass
  25. <writing-an-llvm-pass-BasicBlockPass>` classes, which gives the system more
  26. information about what your pass does, and how it can be combined with other
  27. passes. One of the main features of the LLVM Pass Framework is that it
  28. schedules passes to run in an efficient way based on the constraints that your
  29. pass meets (which are indicated by which class they derive from).
  30. We start by showing you how to construct a pass, everything from setting up the
  31. code, to compiling, loading, and executing it. After the basics are down, more
  32. advanced features are discussed.
  33. Quick Start --- Writing hello world
  34. ===================================
  35. Here we describe how to write the "hello world" of passes. The "Hello" pass is
  36. designed to simply print out the name of non-external functions that exist in
  37. the program being compiled. It does not modify the program at all, it just
  38. inspects it. The source code and files for this pass are available in the LLVM
  39. source tree in the ``lib/Transforms/Hello`` directory.
  40. .. _writing-an-llvm-pass-makefile:
  41. Setting up the build environment
  42. --------------------------------
  43. .. FIXME: Why does this recommend to build in-tree?
  44. First, configure and build LLVM. This needs to be done directly inside the
  45. LLVM source tree rather than in a separate objects directory. Next, you need
  46. to create a new directory somewhere in the LLVM source base. For this example,
  47. we'll assume that you made ``lib/Transforms/Hello``. Finally, you must set up
  48. a build script (``Makefile``) that will compile the source code for the new
  49. pass. To do this, copy the following into ``Makefile``:
  50. .. code-block:: make
  51. # Makefile for hello pass
  52. # Path to top level of LLVM hierarchy
  53. LEVEL = ../../..
  54. # Name of the library to build
  55. LIBRARYNAME = Hello
  56. # Make the shared library become a loadable module so the tools can
  57. # dlopen/dlsym on the resulting library.
  58. LOADABLE_MODULE = 1
  59. # Include the makefile implementation stuff
  60. include $(LEVEL)/Makefile.common
  61. This makefile specifies that all of the ``.cpp`` files in the current directory
  62. are to be compiled and linked together into a shared object
  63. ``$(LEVEL)/Debug+Asserts/lib/Hello.so`` that can be dynamically loaded by the
  64. :program:`opt` or :program:`bugpoint` tools via their :option:`-load` options.
  65. If your operating system uses a suffix other than ``.so`` (such as Windows or Mac
  66. OS X), the appropriate extension will be used.
  67. If you are used CMake to build LLVM, see :ref:`cmake-out-of-source-pass`.
  68. Now that we have the build scripts set up, we just need to write the code for
  69. the pass itself.
  70. .. _writing-an-llvm-pass-basiccode:
  71. Basic code required
  72. -------------------
  73. Now that we have a way to compile our new pass, we just have to write it.
  74. Start out with:
  75. .. code-block:: c++
  76. #include "llvm/Pass.h"
  77. #include "llvm/IR/Function.h"
  78. #include "llvm/Support/raw_ostream.h"
  79. Which are needed because we are writing a `Pass
  80. <http://llvm.org/doxygen/classllvm_1_1Pass.html>`_, we are operating on
  81. `Function <http://llvm.org/doxygen/classllvm_1_1Function.html>`_\ s, and we will
  82. be doing some printing.
  83. Next we have:
  84. .. code-block:: c++
  85. using namespace llvm;
  86. ... which is required because the functions from the include files live in the
  87. llvm namespace.
  88. Next we have:
  89. .. code-block:: c++
  90. namespace {
  91. ... which starts out an anonymous namespace. Anonymous namespaces are to C++
  92. what the "``static``" keyword is to C (at global scope). It makes the things
  93. declared inside of the anonymous namespace visible only to the current file.
  94. If you're not familiar with them, consult a decent C++ book for more
  95. information.
  96. Next, we declare our pass itself:
  97. .. code-block:: c++
  98. struct Hello : public FunctionPass {
  99. This declares a "``Hello``" class that is a subclass of :ref:`FunctionPass
  100. <writing-an-llvm-pass-FunctionPass>`. The different builtin pass subclasses
  101. are described in detail :ref:`later <writing-an-llvm-pass-pass-classes>`, but
  102. for now, know that ``FunctionPass`` operates on a function at a time.
  103. .. code-block:: c++
  104. static char ID;
  105. Hello() : FunctionPass(ID) {}
  106. This declares pass identifier used by LLVM to identify pass. This allows LLVM
  107. to avoid using expensive C++ runtime information.
  108. .. code-block:: c++
  109. bool runOnFunction(Function &F) override {
  110. errs() << "Hello: ";
  111. errs().write_escaped(F.getName()) << "\n";
  112. return false;
  113. }
  114. }; // end of struct Hello
  115. } // end of anonymous namespace
  116. We declare a :ref:`runOnFunction <writing-an-llvm-pass-runOnFunction>` method,
  117. which overrides an abstract virtual method inherited from :ref:`FunctionPass
  118. <writing-an-llvm-pass-FunctionPass>`. This is where we are supposed to do our
  119. thing, so we just print out our message with the name of each function.
  120. .. code-block:: c++
  121. char Hello::ID = 0;
  122. We initialize pass ID here. LLVM uses ID's address to identify a pass, so
  123. initialization value is not important.
  124. .. code-block:: c++
  125. static RegisterPass<Hello> X("hello", "Hello World Pass",
  126. false /* Only looks at CFG */,
  127. false /* Analysis Pass */);
  128. Lastly, we :ref:`register our class <writing-an-llvm-pass-registration>`
  129. ``Hello``, giving it a command line argument "``hello``", and a name "Hello
  130. World Pass". The last two arguments describe its behavior: if a pass walks CFG
  131. without modifying it then the third argument is set to ``true``; if a pass is
  132. an analysis pass, for example dominator tree pass, then ``true`` is supplied as
  133. the fourth argument.
  134. As a whole, the ``.cpp`` file looks like:
  135. .. code-block:: c++
  136. #include "llvm/Pass.h"
  137. #include "llvm/IR/Function.h"
  138. #include "llvm/Support/raw_ostream.h"
  139. using namespace llvm;
  140. namespace {
  141. struct Hello : public FunctionPass {
  142. static char ID;
  143. Hello() : FunctionPass(ID) {}
  144. bool runOnFunction(Function &F) override {
  145. errs() << "Hello: ";
  146. errs().write_escaped(F.getName()) << '\n';
  147. return false;
  148. }
  149. };
  150. }
  151. char Hello::ID = 0;
  152. static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);
  153. Now that it's all together, compile the file with a simple "``gmake``" command
  154. in the local directory and you should get a new file
  155. "``Debug+Asserts/lib/Hello.so``" under the top level directory of the LLVM
  156. source tree (not in the local directory). Note that everything in this file is
  157. contained in an anonymous namespace --- this reflects the fact that passes
  158. are self contained units that do not need external interfaces (although they
  159. can have them) to be useful.
  160. Running a pass with ``opt``
  161. ---------------------------
  162. NOTE: this section should be updated with instructions on how to run with
  163. dxcopt.
  164. Now that you have seen the basics of the mechanics behind passes, we can talk
  165. about some more details of how they work and how to use them.
  166. .. _writing-an-llvm-pass-pass-classes:
  167. Pass classes and requirements
  168. =============================
  169. One of the first things that you should do when designing a new pass is to
  170. decide what class you should subclass for your pass. The :ref:`Hello World
  171. <writing-an-llvm-pass-basiccode>` example uses the :ref:`FunctionPass
  172. <writing-an-llvm-pass-FunctionPass>` class for its implementation, but we did
  173. not discuss why or when this should occur. Here we talk about the classes
  174. available, from the most general to the most specific.
  175. When choosing a superclass for your ``Pass``, you should choose the **most
  176. specific** class possible, while still being able to meet the requirements
  177. listed. This gives the LLVM Pass Infrastructure information necessary to
  178. optimize how passes are run, so that the resultant compiler isn't unnecessarily
  179. slow.
  180. The ``ImmutablePass`` class
  181. ---------------------------
  182. The most plain and boring type of pass is the "`ImmutablePass
  183. <http://llvm.org/doxygen/classllvm_1_1ImmutablePass.html>`_" class. This pass
  184. type is used for passes that do not have to be run, do not change state, and
  185. never need to be updated. This is not a normal type of transformation or
  186. analysis, but can provide information about the current compiler configuration.
  187. Although this pass class is very infrequently used, it is important for
  188. providing information about the current target machine being compiled for, and
  189. other static information that can affect the various transformations.
  190. ``ImmutablePass``\ es never invalidate other transformations, are never
  191. invalidated, and are never "run".
  192. .. _writing-an-llvm-pass-ModulePass:
  193. The ``ModulePass`` class
  194. ------------------------
  195. The `ModulePass <http://llvm.org/doxygen/classllvm_1_1ModulePass.html>`_ class
  196. is the most general of all superclasses that you can use. Deriving from
  197. ``ModulePass`` indicates that your pass uses the entire program as a unit,
  198. referring to function bodies in no predictable order, or adding and removing
  199. functions. Because nothing is known about the behavior of ``ModulePass``
  200. subclasses, no optimization can be done for their execution.
  201. A module pass can use function level passes (e.g. dominators) using the
  202. ``getAnalysis`` interface ``getAnalysis<DominatorTree>(llvm::Function *)`` to
  203. provide the function to retrieve analysis result for, if the function pass does
  204. not require any module or immutable passes. Note that this can only be done
  205. for functions for which the analysis ran, e.g. in the case of dominators you
  206. should only ask for the ``DominatorTree`` for function definitions, not
  207. declarations.
  208. To write a correct ``ModulePass`` subclass, derive from ``ModulePass`` and
  209. overload the ``runOnModule`` method with the following signature:
  210. The ``runOnModule`` method
  211. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  212. .. code-block:: c++
  213. virtual bool runOnModule(Module &M) = 0;
  214. The ``runOnModule`` method performs the interesting work of the pass. It
  215. should return ``true`` if the module was modified by the transformation and
  216. ``false`` otherwise.
  217. .. _writing-an-llvm-pass-CallGraphSCCPass:
  218. The ``CallGraphSCCPass`` class
  219. ------------------------------
  220. The `CallGraphSCCPass
  221. <http://llvm.org/doxygen/classllvm_1_1CallGraphSCCPass.html>`_ is used by
  222. passes that need to traverse the program bottom-up on the call graph (callees
  223. before callers). Deriving from ``CallGraphSCCPass`` provides some mechanics
  224. for building and traversing the ``CallGraph``, but also allows the system to
  225. optimize execution of ``CallGraphSCCPass``\ es. If your pass meets the
  226. requirements outlined below, and doesn't meet the requirements of a
  227. :ref:`FunctionPass <writing-an-llvm-pass-FunctionPass>` or :ref:`BasicBlockPass
  228. <writing-an-llvm-pass-BasicBlockPass>`, you should derive from
  229. ``CallGraphSCCPass``.
  230. ``TODO``: explain briefly what SCC, Tarjan's algo, and B-U mean.
  231. To be explicit, CallGraphSCCPass subclasses are:
  232. #. ... *not allowed* to inspect or modify any ``Function``\ s other than those
  233. in the current SCC and the direct callers and direct callees of the SCC.
  234. #. ... *required* to preserve the current ``CallGraph`` object, updating it to
  235. reflect any changes made to the program.
  236. #. ... *not allowed* to add or remove SCC's from the current Module, though
  237. they may change the contents of an SCC.
  238. #. ... *allowed* to add or remove global variables from the current Module.
  239. #. ... *allowed* to maintain state across invocations of :ref:`runOnSCC
  240. <writing-an-llvm-pass-runOnSCC>` (including global data).
  241. Implementing a ``CallGraphSCCPass`` is slightly tricky in some cases because it
  242. has to handle SCCs with more than one node in it. All of the virtual methods
  243. described below should return ``true`` if they modified the program, or
  244. ``false`` if they didn't.
  245. The ``doInitialization(CallGraph &)`` method
  246. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  247. .. code-block:: c++
  248. virtual bool doInitialization(CallGraph &CG);
  249. The ``doInitialization`` method is allowed to do most of the things that
  250. ``CallGraphSCCPass``\ es are not allowed to do. They can add and remove
  251. functions, get pointers to functions, etc. The ``doInitialization`` method is
  252. designed to do simple initialization type of stuff that does not depend on the
  253. SCCs being processed. The ``doInitialization`` method call is not scheduled to
  254. overlap with any other pass executions (thus it should be very fast).
  255. .. _writing-an-llvm-pass-runOnSCC:
  256. The ``runOnSCC`` method
  257. ^^^^^^^^^^^^^^^^^^^^^^^
  258. .. code-block:: c++
  259. virtual bool runOnSCC(CallGraphSCC &SCC) = 0;
  260. The ``runOnSCC`` method performs the interesting work of the pass, and should
  261. return ``true`` if the module was modified by the transformation, ``false``
  262. otherwise.
  263. The ``doFinalization(CallGraph &)`` method
  264. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  265. .. code-block:: c++
  266. virtual bool doFinalization(CallGraph &CG);
  267. The ``doFinalization`` method is an infrequently used method that is called
  268. when the pass framework has finished calling :ref:`runOnSCC
  269. <writing-an-llvm-pass-runOnSCC>` for every SCC in the program being compiled.
  270. .. _writing-an-llvm-pass-FunctionPass:
  271. The ``FunctionPass`` class
  272. --------------------------
  273. In contrast to ``ModulePass`` subclasses, `FunctionPass
  274. <http://llvm.org/doxygen/classllvm_1_1Pass.html>`_ subclasses do have a
  275. predictable, local behavior that can be expected by the system. All
  276. ``FunctionPass`` execute on each function in the program independent of all of
  277. the other functions in the program. ``FunctionPass``\ es do not require that
  278. they are executed in a particular order, and ``FunctionPass``\ es do not modify
  279. external functions.
  280. To be explicit, ``FunctionPass`` subclasses are not allowed to:
  281. #. Inspect or modify a ``Function`` other than the one currently being processed.
  282. #. Add or remove ``Function``\ s from the current ``Module``.
  283. #. Add or remove global variables from the current ``Module``.
  284. #. Maintain state across invocations of :ref:`runOnFunction
  285. <writing-an-llvm-pass-runOnFunction>` (including global data).
  286. Implementing a ``FunctionPass`` is usually straightforward (See the :ref:`Hello
  287. World <writing-an-llvm-pass-basiccode>` pass for example).
  288. ``FunctionPass``\ es may overload three virtual methods to do their work. All
  289. of these methods should return ``true`` if they modified the program, or
  290. ``false`` if they didn't.
  291. .. _writing-an-llvm-pass-doInitialization-mod:
  292. The ``doInitialization(Module &)`` method
  293. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  294. .. code-block:: c++
  295. virtual bool doInitialization(Module &M);
  296. The ``doInitialization`` method is allowed to do most of the things that
  297. ``FunctionPass``\ es are not allowed to do. They can add and remove functions,
  298. get pointers to functions, etc. The ``doInitialization`` method is designed to
  299. do simple initialization type of stuff that does not depend on the functions
  300. being processed. The ``doInitialization`` method call is not scheduled to
  301. overlap with any other pass executions (thus it should be very fast).
  302. A good example of how this method should be used is the `LowerAllocations
  303. <http://llvm.org/doxygen/LowerAllocations_8cpp-source.html>`_ pass. This pass
  304. converts ``malloc`` and ``free`` instructions into platform dependent
  305. ``malloc()`` and ``free()`` function calls. It uses the ``doInitialization``
  306. method to get a reference to the ``malloc`` and ``free`` functions that it
  307. needs, adding prototypes to the module if necessary.
  308. .. _writing-an-llvm-pass-runOnFunction:
  309. The ``runOnFunction`` method
  310. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  311. .. code-block:: c++
  312. virtual bool runOnFunction(Function &F) = 0;
  313. The ``runOnFunction`` method must be implemented by your subclass to do the
  314. transformation or analysis work of your pass. As usual, a ``true`` value
  315. should be returned if the function is modified.
  316. .. _writing-an-llvm-pass-doFinalization-mod:
  317. The ``doFinalization(Module &)`` method
  318. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  319. .. code-block:: c++
  320. virtual bool doFinalization(Module &M);
  321. The ``doFinalization`` method is an infrequently used method that is called
  322. when the pass framework has finished calling :ref:`runOnFunction
  323. <writing-an-llvm-pass-runOnFunction>` for every function in the program being
  324. compiled.
  325. .. _writing-an-llvm-pass-LoopPass:
  326. The ``LoopPass`` class
  327. ----------------------
  328. All ``LoopPass`` execute on each loop in the function independent of all of the
  329. other loops in the function. ``LoopPass`` processes loops in loop nest order
  330. such that outer most loop is processed last.
  331. ``LoopPass`` subclasses are allowed to update loop nest using ``LPPassManager``
  332. interface. Implementing a loop pass is usually straightforward.
  333. ``LoopPass``\ es may overload three virtual methods to do their work. All
  334. these methods should return ``true`` if they modified the program, or ``false``
  335. if they didn't.
  336. The ``doInitialization(Loop *, LPPassManager &)`` method
  337. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  338. .. code-block:: c++
  339. virtual bool doInitialization(Loop *, LPPassManager &LPM);
  340. The ``doInitialization`` method is designed to do simple initialization type of
  341. stuff that does not depend on the functions being processed. The
  342. ``doInitialization`` method call is not scheduled to overlap with any other
  343. pass executions (thus it should be very fast). ``LPPassManager`` interface
  344. should be used to access ``Function`` or ``Module`` level analysis information.
  345. .. _writing-an-llvm-pass-runOnLoop:
  346. The ``runOnLoop`` method
  347. ^^^^^^^^^^^^^^^^^^^^^^^^
  348. .. code-block:: c++
  349. virtual bool runOnLoop(Loop *, LPPassManager &LPM) = 0;
  350. The ``runOnLoop`` method must be implemented by your subclass to do the
  351. transformation or analysis work of your pass. As usual, a ``true`` value
  352. should be returned if the function is modified. ``LPPassManager`` interface
  353. should be used to update loop nest.
  354. The ``doFinalization()`` method
  355. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  356. .. code-block:: c++
  357. virtual bool doFinalization();
  358. The ``doFinalization`` method is an infrequently used method that is called
  359. when the pass framework has finished calling :ref:`runOnLoop
  360. <writing-an-llvm-pass-runOnLoop>` for every loop in the program being compiled.
  361. .. _writing-an-llvm-pass-RegionPass:
  362. The ``RegionPass`` class
  363. ------------------------
  364. ``RegionPass`` is similar to :ref:`LoopPass <writing-an-llvm-pass-LoopPass>`,
  365. but executes on each single entry single exit region in the function.
  366. ``RegionPass`` processes regions in nested order such that the outer most
  367. region is processed last.
  368. ``RegionPass`` subclasses are allowed to update the region tree by using the
  369. ``RGPassManager`` interface. You may overload three virtual methods of
  370. ``RegionPass`` to implement your own region pass. All these methods should
  371. return ``true`` if they modified the program, or ``false`` if they did not.
  372. The ``doInitialization(Region *, RGPassManager &)`` method
  373. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  374. .. code-block:: c++
  375. virtual bool doInitialization(Region *, RGPassManager &RGM);
  376. The ``doInitialization`` method is designed to do simple initialization type of
  377. stuff that does not depend on the functions being processed. The
  378. ``doInitialization`` method call is not scheduled to overlap with any other
  379. pass executions (thus it should be very fast). ``RPPassManager`` interface
  380. should be used to access ``Function`` or ``Module`` level analysis information.
  381. .. _writing-an-llvm-pass-runOnRegion:
  382. The ``runOnRegion`` method
  383. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  384. .. code-block:: c++
  385. virtual bool runOnRegion(Region *, RGPassManager &RGM) = 0;
  386. The ``runOnRegion`` method must be implemented by your subclass to do the
  387. transformation or analysis work of your pass. As usual, a true value should be
  388. returned if the region is modified. ``RGPassManager`` interface should be used to
  389. update region tree.
  390. The ``doFinalization()`` method
  391. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  392. .. code-block:: c++
  393. virtual bool doFinalization();
  394. The ``doFinalization`` method is an infrequently used method that is called
  395. when the pass framework has finished calling :ref:`runOnRegion
  396. <writing-an-llvm-pass-runOnRegion>` for every region in the program being
  397. compiled.
  398. .. _writing-an-llvm-pass-BasicBlockPass:
  399. The ``BasicBlockPass`` class
  400. ----------------------------
  401. ``BasicBlockPass``\ es are just like :ref:`FunctionPass's
  402. <writing-an-llvm-pass-FunctionPass>` , except that they must limit their scope
  403. of inspection and modification to a single basic block at a time. As such,
  404. they are **not** allowed to do any of the following:
  405. #. Modify or inspect any basic blocks outside of the current one.
  406. #. Maintain state across invocations of :ref:`runOnBasicBlock
  407. <writing-an-llvm-pass-runOnBasicBlock>`.
  408. #. Modify the control flow graph (by altering terminator instructions)
  409. #. Any of the things forbidden for :ref:`FunctionPasses
  410. <writing-an-llvm-pass-FunctionPass>`.
  411. ``BasicBlockPass``\ es are useful for traditional local and "peephole"
  412. optimizations. They may override the same :ref:`doInitialization(Module &)
  413. <writing-an-llvm-pass-doInitialization-mod>` and :ref:`doFinalization(Module &)
  414. <writing-an-llvm-pass-doFinalization-mod>` methods that :ref:`FunctionPass's
  415. <writing-an-llvm-pass-FunctionPass>` have, but also have the following virtual
  416. methods that may also be implemented:
  417. The ``doInitialization(Function &)`` method
  418. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  419. .. code-block:: c++
  420. virtual bool doInitialization(Function &F);
  421. The ``doInitialization`` method is allowed to do most of the things that
  422. ``BasicBlockPass``\ es are not allowed to do, but that ``FunctionPass``\ es
  423. can. The ``doInitialization`` method is designed to do simple initialization
  424. that does not depend on the ``BasicBlock``\ s being processed. The
  425. ``doInitialization`` method call is not scheduled to overlap with any other
  426. pass executions (thus it should be very fast).
  427. .. _writing-an-llvm-pass-runOnBasicBlock:
  428. The ``runOnBasicBlock`` method
  429. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  430. .. code-block:: c++
  431. virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
  432. Override this function to do the work of the ``BasicBlockPass``. This function
  433. is not allowed to inspect or modify basic blocks other than the parameter, and
  434. are not allowed to modify the CFG. A ``true`` value must be returned if the
  435. basic block is modified.
  436. The ``doFinalization(Function &)`` method
  437. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  438. .. code-block:: c++
  439. virtual bool doFinalization(Function &F);
  440. The ``doFinalization`` method is an infrequently used method that is called
  441. when the pass framework has finished calling :ref:`runOnBasicBlock
  442. <writing-an-llvm-pass-runOnBasicBlock>` for every ``BasicBlock`` in the program
  443. being compiled. This can be used to perform per-function finalization.
  444. The ``MachineFunctionPass`` class
  445. ---------------------------------
  446. A ``MachineFunctionPass`` is a part of the LLVM code generator that executes on
  447. the machine-dependent representation of each LLVM function in the program.
  448. Code generator passes are registered and initialized specially by
  449. ``TargetMachine::addPassesToEmitFile`` and similar routines, so they cannot
  450. generally be run from the :program:`opt` or :program:`bugpoint` commands.
  451. A ``MachineFunctionPass`` is also a ``FunctionPass``, so all the restrictions
  452. that apply to a ``FunctionPass`` also apply to it. ``MachineFunctionPass``\ es
  453. also have additional restrictions. In particular, ``MachineFunctionPass``\ es
  454. are not allowed to do any of the following:
  455. #. Modify or create any LLVM IR ``Instruction``\ s, ``BasicBlock``\ s,
  456. ``Argument``\ s, ``Function``\ s, ``GlobalVariable``\ s,
  457. ``GlobalAlias``\ es, or ``Module``\ s.
  458. #. Modify a ``MachineFunction`` other than the one currently being processed.
  459. #. Maintain state across invocations of :ref:`runOnMachineFunction
  460. <writing-an-llvm-pass-runOnMachineFunction>` (including global data).
  461. .. _writing-an-llvm-pass-runOnMachineFunction:
  462. The ``runOnMachineFunction(MachineFunction &MF)`` method
  463. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  464. .. code-block:: c++
  465. virtual bool runOnMachineFunction(MachineFunction &MF) = 0;
  466. ``runOnMachineFunction`` can be considered the main entry point of a
  467. ``MachineFunctionPass``; that is, you should override this method to do the
  468. work of your ``MachineFunctionPass``.
  469. The ``runOnMachineFunction`` method is called on every ``MachineFunction`` in a
  470. ``Module``, so that the ``MachineFunctionPass`` may perform optimizations on
  471. the machine-dependent representation of the function. If you want to get at
  472. the LLVM ``Function`` for the ``MachineFunction`` you're working on, use
  473. ``MachineFunction``'s ``getFunction()`` accessor method --- but remember, you
  474. may not modify the LLVM ``Function`` or its contents from a
  475. ``MachineFunctionPass``.
  476. .. _writing-an-llvm-pass-registration:
  477. Pass registration
  478. -----------------
  479. In the :ref:`Hello World <writing-an-llvm-pass-basiccode>` example pass we
  480. illustrated how pass registration works, and discussed some of the reasons that
  481. it is used and what it does. Here we discuss how and why passes are
  482. registered.
  483. As we saw above, passes are registered with the ``RegisterPass`` template. The
  484. template parameter is the name of the pass that is to be used on the command
  485. line to specify that the pass should be added to a program (for example, with
  486. :program:`opt` or :program:`bugpoint`). The first argument is the name of the
  487. pass, which is to be used for the :option:`-help` output of programs, as well
  488. as for debug output generated by the :option:`--debug-pass` option.
  489. If you want your pass to be easily dumpable, you should implement the virtual
  490. print method:
  491. The ``print`` method
  492. ^^^^^^^^^^^^^^^^^^^^
  493. .. code-block:: c++
  494. virtual void print(llvm::raw_ostream &O, const Module *M) const;
  495. The ``print`` method must be implemented by "analyses" in order to print a
  496. human readable version of the analysis results. This is useful for debugging
  497. an analysis itself, as well as for other people to figure out how an analysis
  498. works. Use the opt ``-analyze`` argument to invoke this method.
  499. The ``llvm::raw_ostream`` parameter specifies the stream to write the results
  500. on, and the ``Module`` parameter gives a pointer to the top level module of the
  501. program that has been analyzed. Note however that this pointer may be ``NULL``
  502. in certain circumstances (such as calling the ``Pass::dump()`` from a
  503. debugger), so it should only be used to enhance debug output, it should not be
  504. depended on.
  505. .. _writing-an-llvm-pass-interaction:
  506. Specifying interactions between passes
  507. --------------------------------------
  508. One of the main responsibilities of the ``PassManager`` is to make sure that
  509. passes interact with each other correctly. Because ``PassManager`` tries to
  510. :ref:`optimize the execution of passes <writing-an-llvm-pass-passmanager>` it
  511. must know how the passes interact with each other and what dependencies exist
  512. between the various passes. To track this, each pass can declare the set of
  513. passes that are required to be executed before the current pass, and the passes
  514. which are invalidated by the current pass.
  515. Typically this functionality is used to require that analysis results are
  516. computed before your pass is run. Running arbitrary transformation passes can
  517. invalidate the computed analysis results, which is what the invalidation set
  518. specifies. If a pass does not implement the :ref:`getAnalysisUsage
  519. <writing-an-llvm-pass-getAnalysisUsage>` method, it defaults to not having any
  520. prerequisite passes, and invalidating **all** other passes.
  521. .. _writing-an-llvm-pass-getAnalysisUsage:
  522. The ``getAnalysisUsage`` method
  523. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  524. .. code-block:: c++
  525. virtual void getAnalysisUsage(AnalysisUsage &Info) const;
  526. By implementing the ``getAnalysisUsage`` method, the required and invalidated
  527. sets may be specified for your transformation. The implementation should fill
  528. in the `AnalysisUsage
  529. <http://llvm.org/doxygen/classllvm_1_1AnalysisUsage.html>`_ object with
  530. information about which passes are required and not invalidated. To do this, a
  531. pass may call any of the following methods on the ``AnalysisUsage`` object:
  532. The ``AnalysisUsage::addRequired<>`` and ``AnalysisUsage::addRequiredTransitive<>`` methods
  533. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  534. If your pass requires a previous pass to be executed (an analysis for example),
  535. it can use one of these methods to arrange for it to be run before your pass.
  536. LLVM has many different types of analyses and passes that can be required,
  537. spanning the range from ``DominatorSet`` to ``BreakCriticalEdges``. Requiring
  538. ``BreakCriticalEdges``, for example, guarantees that there will be no critical
  539. edges in the CFG when your pass has been run.
  540. Some analyses chain to other analyses to do their job. For example, an
  541. `AliasAnalysis <AliasAnalysis>` implementation is required to :ref:`chain
  542. <aliasanalysis-chaining>` to other alias analysis passes. In cases where
  543. analyses chain, the ``addRequiredTransitive`` method should be used instead of
  544. the ``addRequired`` method. This informs the ``PassManager`` that the
  545. transitively required pass should be alive as long as the requiring pass is.
  546. The ``AnalysisUsage::addPreserved<>`` method
  547. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  548. One of the jobs of the ``PassManager`` is to optimize how and when analyses are
  549. run. In particular, it attempts to avoid recomputing data unless it needs to.
  550. For this reason, passes are allowed to declare that they preserve (i.e., they
  551. don't invalidate) an existing analysis if it's available. For example, a
  552. simple constant folding pass would not modify the CFG, so it can't possibly
  553. affect the results of dominator analysis. By default, all passes are assumed
  554. to invalidate all others.
  555. The ``AnalysisUsage`` class provides several methods which are useful in
  556. certain circumstances that are related to ``addPreserved``. In particular, the
  557. ``setPreservesAll`` method can be called to indicate that the pass does not
  558. modify the LLVM program at all (which is true for analyses), and the
  559. ``setPreservesCFG`` method can be used by transformations that change
  560. instructions in the program but do not modify the CFG or terminator
  561. instructions (note that this property is implicitly set for
  562. :ref:`BasicBlockPass <writing-an-llvm-pass-BasicBlockPass>`\ es).
  563. ``addPreserved`` is particularly useful for transformations like
  564. ``BreakCriticalEdges``. This pass knows how to update a small set of loop and
  565. dominator related analyses if they exist, so it can preserve them, despite the
  566. fact that it hacks on the CFG.
  567. Example implementations of ``getAnalysisUsage``
  568. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  569. .. code-block:: c++
  570. // This example modifies the program, but does not modify the CFG
  571. void LICM::getAnalysisUsage(AnalysisUsage &AU) const {
  572. AU.setPreservesCFG();
  573. AU.addRequired<LoopInfoWrapperPass>();
  574. }
  575. .. _writing-an-llvm-pass-getAnalysis:
  576. The ``getAnalysis<>`` and ``getAnalysisIfAvailable<>`` methods
  577. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  578. The ``Pass::getAnalysis<>`` method is automatically inherited by your class,
  579. providing you with access to the passes that you declared that you required
  580. with the :ref:`getAnalysisUsage <writing-an-llvm-pass-getAnalysisUsage>`
  581. method. It takes a single template argument that specifies which pass class
  582. you want, and returns a reference to that pass. For example:
  583. .. code-block:: c++
  584. bool LICM::runOnFunction(Function &F) {
  585. LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  586. //...
  587. }
  588. This method call returns a reference to the pass desired. You may get a
  589. runtime assertion failure if you attempt to get an analysis that you did not
  590. declare as required in your :ref:`getAnalysisUsage
  591. <writing-an-llvm-pass-getAnalysisUsage>` implementation. This method can be
  592. called by your ``run*`` method implementation, or by any other local method
  593. invoked by your ``run*`` method.
  594. A module level pass can use function level analysis info using this interface.
  595. For example:
  596. .. code-block:: c++
  597. bool ModuleLevelPass::runOnModule(Module &M) {
  598. //...
  599. DominatorTree &DT = getAnalysis<DominatorTree>(Func);
  600. //...
  601. }
  602. In above example, ``runOnFunction`` for ``DominatorTree`` is called by pass
  603. manager before returning a reference to the desired pass.
  604. If your pass is capable of updating analyses if they exist (e.g.,
  605. ``BreakCriticalEdges``, as described above), you can use the
  606. ``getAnalysisIfAvailable`` method, which returns a pointer to the analysis if
  607. it is active. For example:
  608. .. code-block:: c++
  609. if (DominatorSet *DS = getAnalysisIfAvailable<DominatorSet>()) {
  610. // A DominatorSet is active. This code will update it.
  611. }
  612. Implementing Analysis Groups
  613. ----------------------------
  614. Now that we understand the basics of how passes are defined, how they are used,
  615. and how they are required from other passes, it's time to get a little bit
  616. fancier. All of the pass relationships that we have seen so far are very
  617. simple: one pass depends on one other specific pass to be run before it can
  618. run. For many applications, this is great, for others, more flexibility is
  619. required.
  620. In particular, some analyses are defined such that there is a single simple
  621. interface to the analysis results, but multiple ways of calculating them.
  622. Consider alias analysis for example. The most trivial alias analysis returns
  623. "may alias" for any alias query. The most sophisticated analysis a
  624. flow-sensitive, context-sensitive interprocedural analysis that can take a
  625. significant amount of time to execute (and obviously, there is a lot of room
  626. between these two extremes for other implementations). To cleanly support
  627. situations like this, the LLVM Pass Infrastructure supports the notion of
  628. Analysis Groups.
  629. Analysis Group Concepts
  630. ^^^^^^^^^^^^^^^^^^^^^^^
  631. An Analysis Group is a single simple interface that may be implemented by
  632. multiple different passes. Analysis Groups can be given human readable names
  633. just like passes, but unlike passes, they need not derive from the ``Pass``
  634. class. An analysis group may have one or more implementations, one of which is
  635. the "default" implementation.
  636. Analysis groups are used by client passes just like other passes are: the
  637. ``AnalysisUsage::addRequired()`` and ``Pass::getAnalysis()`` methods. In order
  638. to resolve this requirement, the :ref:`PassManager
  639. <writing-an-llvm-pass-passmanager>` scans the available passes to see if any
  640. implementations of the analysis group are available. If none is available, the
  641. default implementation is created for the pass to use. All standard rules for
  642. :ref:`interaction between passes <writing-an-llvm-pass-interaction>` still
  643. apply.
  644. Although :ref:`Pass Registration <writing-an-llvm-pass-registration>` is
  645. optional for normal passes, all analysis group implementations must be
  646. registered, and must use the :ref:`INITIALIZE_AG_PASS
  647. <writing-an-llvm-pass-RegisterAnalysisGroup>` template to join the
  648. implementation pool. Also, a default implementation of the interface **must**
  649. be registered with :ref:`RegisterAnalysisGroup
  650. <writing-an-llvm-pass-RegisterAnalysisGroup>`.
  651. As a concrete example of an Analysis Group in action, consider the
  652. `AliasAnalysis <http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html>`_
  653. analysis group. The default implementation of the alias analysis interface
  654. (the `basicaa <http://llvm.org/doxygen/structBasicAliasAnalysis.html>`_ pass)
  655. just does a few simple checks that don't require significant analysis to
  656. compute (such as: two different globals can never alias each other, etc).
  657. Passes that use the `AliasAnalysis
  658. <http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html>`_ interface (for
  659. example the `gcse <http://llvm.org/doxygen/structGCSE.html>`_ pass), do not
  660. care which implementation of alias analysis is actually provided, they just use
  661. the designated interface.
  662. From the user's perspective, commands work just like normal. Issuing the
  663. command ``opt -gcse ...`` will cause the ``basicaa`` class to be instantiated
  664. and added to the pass sequence. Issuing the command ``opt -somefancyaa -gcse
  665. ...`` will cause the ``gcse`` pass to use the ``somefancyaa`` alias analysis
  666. (which doesn't actually exist, it's just a hypothetical example) instead.
  667. .. _writing-an-llvm-pass-RegisterAnalysisGroup:
  668. Using ``RegisterAnalysisGroup``
  669. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  670. The ``RegisterAnalysisGroup`` template is used to register the analysis group
  671. itself, while the ``INITIALIZE_AG_PASS`` is used to add pass implementations to
  672. the analysis group. First, an analysis group should be registered, with a
  673. human readable name provided for it. Unlike registration of passes, there is
  674. no command line argument to be specified for the Analysis Group Interface
  675. itself, because it is "abstract":
  676. .. code-block:: c++
  677. static RegisterAnalysisGroup<AliasAnalysis> A("Alias Analysis");
  678. Once the analysis is registered, passes can declare that they are valid
  679. implementations of the interface by using the following code:
  680. .. code-block:: c++
  681. namespace {
  682. // Declare that we implement the AliasAnalysis interface
  683. INITIALIZE_AG_PASS(FancyAA, AliasAnalysis , "somefancyaa",
  684. "A more complex alias analysis implementation",
  685. false, // Is CFG Only?
  686. true, // Is Analysis?
  687. false); // Is default Analysis Group implementation?
  688. }
  689. This just shows a class ``FancyAA`` that uses the ``INITIALIZE_AG_PASS`` macro
  690. both to register and to "join" the `AliasAnalysis
  691. <http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html>`_ analysis group.
  692. Every implementation of an analysis group should join using this macro.
  693. .. code-block:: c++
  694. namespace {
  695. // Declare that we implement the AliasAnalysis interface
  696. INITIALIZE_AG_PASS(BasicAA, AliasAnalysis, "basicaa",
  697. "Basic Alias Analysis (default AA impl)",
  698. false, // Is CFG Only?
  699. true, // Is Analysis?
  700. true); // Is default Analysis Group implementation?
  701. }
  702. Here we show how the default implementation is specified (using the final
  703. argument to the ``INITIALIZE_AG_PASS`` template). There must be exactly one
  704. default implementation available at all times for an Analysis Group to be used.
  705. Only default implementation can derive from ``ImmutablePass``. Here we declare
  706. that the `BasicAliasAnalysis
  707. <http://llvm.org/doxygen/structBasicAliasAnalysis.html>`_ pass is the default
  708. implementation for the interface.
  709. Pass Statistics
  710. ===============
  711. The `Statistic <http://llvm.org/doxygen/Statistic_8h-source.html>`_ class is
  712. designed to be an easy way to expose various success metrics from passes.
  713. These statistics are printed at the end of a run, when the :option:`-stats`
  714. command line option is enabled on the command line. See the :ref:`Statistics
  715. section <Statistic>` in the Programmer's Manual for details.
  716. .. _writing-an-llvm-pass-passmanager:
  717. What PassManager does
  718. ---------------------
  719. The `PassManager <http://llvm.org/doxygen/PassManager_8h-source.html>`_ `class
  720. <http://llvm.org/doxygen/classllvm_1_1PassManager.html>`_ takes a list of
  721. passes, ensures their :ref:`prerequisites <writing-an-llvm-pass-interaction>`
  722. are set up correctly, and then schedules passes to run efficiently. All of the
  723. LLVM tools that run passes use the PassManager for execution of these passes.
  724. The PassManager does two main things to try to reduce the execution time of a
  725. series of passes:
  726. #. **Share analysis results.** The ``PassManager`` attempts to avoid
  727. recomputing analysis results as much as possible. This means keeping track
  728. of which analyses are available already, which analyses get invalidated, and
  729. which analyses are needed to be run for a pass. An important part of work
  730. is that the ``PassManager`` tracks the exact lifetime of all analysis
  731. results, allowing it to :ref:`free memory
  732. <writing-an-llvm-pass-releaseMemory>` allocated to holding analysis results
  733. as soon as they are no longer needed.
  734. #. **Pipeline the execution of passes on the program.** The ``PassManager``
  735. attempts to get better cache and memory usage behavior out of a series of
  736. passes by pipelining the passes together. This means that, given a series
  737. of consecutive :ref:`FunctionPass <writing-an-llvm-pass-FunctionPass>`, it
  738. will execute all of the :ref:`FunctionPass
  739. <writing-an-llvm-pass-FunctionPass>` on the first function, then all of the
  740. :ref:`FunctionPasses <writing-an-llvm-pass-FunctionPass>` on the second
  741. function, etc... until the entire program has been run through the passes.
  742. This improves the cache behavior of the compiler, because it is only
  743. touching the LLVM program representation for a single function at a time,
  744. instead of traversing the entire program. It reduces the memory consumption
  745. of compiler, because, for example, only one `DominatorSet
  746. <http://llvm.org/doxygen/classllvm_1_1DominatorSet.html>`_ needs to be
  747. calculated at a time. This also makes it possible to implement some
  748. :ref:`interesting enhancements <writing-an-llvm-pass-SMP>` in the future.
  749. The effectiveness of the ``PassManager`` is influenced directly by how much
  750. information it has about the behaviors of the passes it is scheduling. For
  751. example, the "preserved" set is intentionally conservative in the face of an
  752. unimplemented :ref:`getAnalysisUsage <writing-an-llvm-pass-getAnalysisUsage>`
  753. method. Not implementing when it should be implemented will have the effect of
  754. not allowing any analysis results to live across the execution of your pass.
  755. The ``PassManager`` class exposes a ``--debug-pass`` command line options that
  756. is useful for debugging pass execution, seeing how things work, and diagnosing
  757. when you should be preserving more analyses than you currently are. (To get
  758. information about all of the variants of the ``--debug-pass`` option, just type
  759. "``opt -help-hidden``").
  760. By using the --debug-pass=Structure option, for example, we can see how our
  761. :ref:`Hello World <writing-an-llvm-pass-basiccode>` pass interacts with other
  762. passes. Lets try it out with the gcse and licm passes:
  763. .. code-block:: console
  764. $ opt -load ../../../Debug+Asserts/lib/Hello.so -gcse -licm --debug-pass=Structure < hello.bc > /dev/null
  765. Module Pass Manager
  766. Function Pass Manager
  767. Dominator Set Construction
  768. Immediate Dominators Construction
  769. Global Common Subexpression Elimination
  770. -- Immediate Dominators Construction
  771. -- Global Common Subexpression Elimination
  772. Natural Loop Construction
  773. Loop Invariant Code Motion
  774. -- Natural Loop Construction
  775. -- Loop Invariant Code Motion
  776. Module Verifier
  777. -- Dominator Set Construction
  778. -- Module Verifier
  779. Bitcode Writer
  780. --Bitcode Writer
  781. This output shows us when passes are constructed and when the analysis results
  782. are known to be dead (prefixed with "``--``"). Here we see that GCSE uses
  783. dominator and immediate dominator information to do its job. The LICM pass
  784. uses natural loop information, which uses dominator sets, but not immediate
  785. dominators. Because immediate dominators are no longer useful after the GCSE
  786. pass, it is immediately destroyed. The dominator sets are then reused to
  787. compute natural loop information, which is then used by the LICM pass.
  788. After the LICM pass, the module verifier runs (which is automatically added by
  789. the :program:`opt` tool), which uses the dominator set to check that the
  790. resultant LLVM code is well formed. After it finishes, the dominator set
  791. information is destroyed, after being computed once, and shared by three
  792. passes.
  793. Lets see how this changes when we run the :ref:`Hello World
  794. <writing-an-llvm-pass-basiccode>` pass in between the two passes:
  795. .. code-block:: console
  796. $ opt -load ../../../Debug+Asserts/lib/Hello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
  797. Module Pass Manager
  798. Function Pass Manager
  799. Dominator Set Construction
  800. Immediate Dominators Construction
  801. Global Common Subexpression Elimination
  802. -- Dominator Set Construction
  803. -- Immediate Dominators Construction
  804. -- Global Common Subexpression Elimination
  805. Hello World Pass
  806. -- Hello World Pass
  807. Dominator Set Construction
  808. Natural Loop Construction
  809. Loop Invariant Code Motion
  810. -- Natural Loop Construction
  811. -- Loop Invariant Code Motion
  812. Module Verifier
  813. -- Dominator Set Construction
  814. -- Module Verifier
  815. Bitcode Writer
  816. --Bitcode Writer
  817. Hello: __main
  818. Hello: puts
  819. Hello: main
  820. Here we see that the :ref:`Hello World <writing-an-llvm-pass-basiccode>` pass
  821. has killed the Dominator Set pass, even though it doesn't modify the code at
  822. all! To fix this, we need to add the following :ref:`getAnalysisUsage
  823. <writing-an-llvm-pass-getAnalysisUsage>` method to our pass:
  824. .. code-block:: c++
  825. // We don't modify the program, so we preserve all analyses
  826. void getAnalysisUsage(AnalysisUsage &AU) const override {
  827. AU.setPreservesAll();
  828. }
  829. Now when we run our pass, we get this output:
  830. .. code-block:: console
  831. $ opt -load ../../../Debug+Asserts/lib/Hello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
  832. Pass Arguments: -gcse -hello -licm
  833. Module Pass Manager
  834. Function Pass Manager
  835. Dominator Set Construction
  836. Immediate Dominators Construction
  837. Global Common Subexpression Elimination
  838. -- Immediate Dominators Construction
  839. -- Global Common Subexpression Elimination
  840. Hello World Pass
  841. -- Hello World Pass
  842. Natural Loop Construction
  843. Loop Invariant Code Motion
  844. -- Loop Invariant Code Motion
  845. -- Natural Loop Construction
  846. Module Verifier
  847. -- Dominator Set Construction
  848. -- Module Verifier
  849. Bitcode Writer
  850. --Bitcode Writer
  851. Hello: __main
  852. Hello: puts
  853. Hello: main
  854. Which shows that we don't accidentally invalidate dominator information
  855. anymore, and therefore do not have to compute it twice.
  856. .. _writing-an-llvm-pass-releaseMemory:
  857. The ``releaseMemory`` method
  858. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  859. .. code-block:: c++
  860. virtual void releaseMemory();
  861. The ``PassManager`` automatically determines when to compute analysis results,
  862. and how long to keep them around for. Because the lifetime of the pass object
  863. itself is effectively the entire duration of the compilation process, we need
  864. some way to free analysis results when they are no longer useful. The
  865. ``releaseMemory`` virtual method is the way to do this.
  866. If you are writing an analysis or any other pass that retains a significant
  867. amount of state (for use by another pass which "requires" your pass and uses
  868. the :ref:`getAnalysis <writing-an-llvm-pass-getAnalysis>` method) you should
  869. implement ``releaseMemory`` to, well, release the memory allocated to maintain
  870. this internal state. This method is called after the ``run*`` method for the
  871. class, before the next call of ``run*`` in your pass.
  872. Registering dynamically loaded passes
  873. =====================================
  874. *Size matters* when constructing production quality tools using LLVM, both for
  875. the purposes of distribution, and for regulating the resident code size when
  876. running on the target system. Therefore, it becomes desirable to selectively
  877. use some passes, while omitting others and maintain the flexibility to change
  878. configurations later on. You want to be able to do all this, and, provide
  879. feedback to the user. This is where pass registration comes into play.
  880. The fundamental mechanisms for pass registration are the
  881. ``MachinePassRegistry`` class and subclasses of ``MachinePassRegistryNode``.
  882. An instance of ``MachinePassRegistry`` is used to maintain a list of
  883. ``MachinePassRegistryNode`` objects. This instance maintains the list and
  884. communicates additions and deletions to the command line interface.
  885. An instance of ``MachinePassRegistryNode`` subclass is used to maintain
  886. information provided about a particular pass. This information includes the
  887. command line name, the command help string and the address of the function used
  888. to create an instance of the pass. A global static constructor of one of these
  889. instances *registers* with a corresponding ``MachinePassRegistry``, the static
  890. destructor *unregisters*. Thus a pass that is statically linked in the tool
  891. will be registered at start up. A dynamically loaded pass will register on
  892. load and unregister at unload.
  893. Using existing registries
  894. -------------------------
  895. There are predefined registries to track instruction scheduling
  896. (``RegisterScheduler``) and register allocation (``RegisterRegAlloc``) machine
  897. passes. Here we will describe how to *register* a register allocator machine
  898. pass.
  899. Implement your register allocator machine pass. In your register allocator
  900. ``.cpp`` file add the following include:
  901. .. code-block:: c++
  902. #include "llvm/CodeGen/RegAllocRegistry.h"
  903. Also in your register allocator ``.cpp`` file, define a creator function in the
  904. form:
  905. .. code-block:: c++
  906. FunctionPass *createMyRegisterAllocator() {
  907. return new MyRegisterAllocator();
  908. }
  909. Note that the signature of this function should match the type of
  910. ``RegisterRegAlloc::FunctionPassCtor``. In the same file add the "installing"
  911. declaration, in the form:
  912. .. code-block:: c++
  913. static RegisterRegAlloc myRegAlloc("myregalloc",
  914. "my register allocator help string",
  915. createMyRegisterAllocator);
  916. Note the two spaces prior to the help string produces a tidy result on the
  917. :option:`-help` query.
  918. .. code-block:: console
  919. $ llc -help
  920. ...
  921. -regalloc - Register allocator to use (default=linearscan)
  922. =linearscan - linear scan register allocator
  923. =local - local register allocator
  924. =simple - simple register allocator
  925. =myregalloc - my register allocator help string
  926. ...
  927. And that's it. The user is now free to use ``-regalloc=myregalloc`` as an
  928. option. Registering instruction schedulers is similar except use the
  929. ``RegisterScheduler`` class. Note that the
  930. ``RegisterScheduler::FunctionPassCtor`` is significantly different from
  931. ``RegisterRegAlloc::FunctionPassCtor``.
  932. To force the load/linking of your register allocator into the
  933. :program:`llc`/:program:`lli` tools, add your creator function's global
  934. declaration to ``Passes.h`` and add a "pseudo" call line to
  935. ``llvm/Codegen/LinkAllCodegenComponents.h``.
  936. Creating new registries
  937. -----------------------
  938. The easiest way to get started is to clone one of the existing registries; we
  939. recommend ``llvm/CodeGen/RegAllocRegistry.h``. The key things to modify are
  940. the class name and the ``FunctionPassCtor`` type.
  941. Then you need to declare the registry. Example: if your pass registry is
  942. ``RegisterMyPasses`` then define:
  943. .. code-block:: c++
  944. MachinePassRegistry RegisterMyPasses::Registry;
  945. And finally, declare the command line option for your passes. Example:
  946. .. code-block:: c++
  947. cl::opt<RegisterMyPasses::FunctionPassCtor, false,
  948. RegisterPassParser<RegisterMyPasses> >
  949. MyPassOpt("mypass",
  950. cl::init(&createDefaultMyPass),
  951. cl::desc("my pass option help"));
  952. Here the command option is "``mypass``", with ``createDefaultMyPass`` as the
  953. default creator.
  954. Using GDB with dynamically loaded passes
  955. ----------------------------------------
  956. Unfortunately, using GDB with dynamically loaded passes is not as easy as it
  957. should be. First of all, you can't set a breakpoint in a shared object that
  958. has not been loaded yet, and second of all there are problems with inlined
  959. functions in shared objects. Here are some suggestions to debugging your pass
  960. with GDB.
  961. For sake of discussion, I'm going to assume that you are debugging a
  962. transformation invoked by :program:`opt`, although nothing described here
  963. depends on that.
  964. Setting a breakpoint in your pass
  965. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  966. First thing you do is start gdb on the opt process:
  967. .. code-block:: console
  968. $ gdb opt
  969. GNU gdb 5.0
  970. Copyright 2000 Free Software Foundation, Inc.
  971. GDB is free software, covered by the GNU General Public License, and you are
  972. welcome to change it and/or distribute copies of it under certain conditions.
  973. Type "show copying" to see the conditions.
  974. There is absolutely no warranty for GDB. Type "show warranty" for details.
  975. This GDB was configured as "sparc-sun-solaris2.6"...
  976. (gdb)
  977. Note that :program:`opt` has a lot of debugging information in it, so it takes
  978. time to load. Be patient. Since we cannot set a breakpoint in our pass yet
  979. (the shared object isn't loaded until runtime), we must execute the process,
  980. and have it stop before it invokes our pass, but after it has loaded the shared
  981. object. The most foolproof way of doing this is to set a breakpoint in
  982. ``PassManager::run`` and then run the process with the arguments you want:
  983. .. code-block:: console
  984. $ (gdb) break llvm::PassManager::run
  985. Breakpoint 1 at 0x2413bc: file Pass.cpp, line 70.
  986. (gdb) run test.bc -load $(LLVMTOP)/llvm/Debug+Asserts/lib/[libname].so -[passoption]
  987. Starting program: opt test.bc -load $(LLVMTOP)/llvm/Debug+Asserts/lib/[libname].so -[passoption]
  988. Breakpoint 1, PassManager::run (this=0xffbef174, M=@0x70b298) at Pass.cpp:70
  989. 70 bool PassManager::run(Module &M) { return PM->run(M); }
  990. (gdb)
  991. Once the :program:`opt` stops in the ``PassManager::run`` method you are now
  992. free to set breakpoints in your pass so that you can trace through execution or
  993. do other standard debugging stuff.
  994. Miscellaneous Problems
  995. ^^^^^^^^^^^^^^^^^^^^^^
  996. Once you have the basics down, there are a couple of problems that GDB has,
  997. some with solutions, some without.
  998. * Inline functions have bogus stack information. In general, GDB does a pretty
  999. good job getting stack traces and stepping through inline functions. When a
  1000. pass is dynamically loaded however, it somehow completely loses this
  1001. capability. The only solution I know of is to de-inline a function (move it
  1002. from the body of a class to a ``.cpp`` file).
  1003. * Restarting the program breaks breakpoints. After following the information
  1004. above, you have succeeded in getting some breakpoints planted in your pass.
  1005. Nex thing you know, you restart the program (i.e., you type "``run``" again),
  1006. and you start getting errors about breakpoints being unsettable. The only
  1007. way I have found to "fix" this problem is to delete the breakpoints that are
  1008. already set in your pass, run the program, and re-set the breakpoints once
  1009. execution stops in ``PassManager::run``.
  1010. Hopefully these tips will help with common case debugging situations. If you'd
  1011. like to contribute some tips of your own, just contact `Chris
  1012. <mailto:[email protected]>`_.
  1013. Future extensions planned
  1014. -------------------------
  1015. Although the LLVM Pass Infrastructure is very capable as it stands, and does
  1016. some nifty stuff, there are things we'd like to add in the future. Here is
  1017. where we are going:
  1018. .. _writing-an-llvm-pass-SMP:
  1019. Multithreaded LLVM
  1020. ^^^^^^^^^^^^^^^^^^
  1021. Multiple CPU machines are becoming more common and compilation can never be
  1022. fast enough: obviously we should allow for a multithreaded compiler. Because
  1023. of the semantics defined for passes above (specifically they cannot maintain
  1024. state across invocations of their ``run*`` methods), a nice clean way to
  1025. implement a multithreaded compiler would be for the ``PassManager`` class to
  1026. create multiple instances of each pass object, and allow the separate instances
  1027. to be hacking on different parts of the program at the same time.
  1028. This implementation would prevent each of the passes from having to implement
  1029. multithreaded constructs, requiring only the LLVM core to have locking in a few
  1030. places (for global resources). Although this is a simple extension, we simply
  1031. haven't had time (or multiprocessor machines, thus a reason) to implement this.
  1032. Despite that, we have kept the LLVM passes SMP ready, and you should too.