xref: /llvm-project/llvm/docs/WritingAnLLVMPass.rst (revision 974d8f6c0c49d279d8a5880ec3dd1fd11589cca0)
1========================================
2Writing an LLVM Pass (legacy PM version)
3========================================
4
5.. program:: opt
6
7.. contents::
8    :local:
9
10Introduction --- What is a pass?
11================================
12
13.. warning::
14  This document deals with the legacy pass manager. LLVM uses the new pass
15  manager for the optimization pipeline (the codegen pipeline
16  still uses the legacy pass manager), which has its own way of defining
17  passes. For more details, see :doc:`WritingAnLLVMNewPMPass` and
18  :doc:`NewPassManager`.
19
20The LLVM Pass Framework is an important part of the LLVM system, because LLVM
21passes are where most of the interesting parts of the compiler exist.  Passes
22perform the transformations and optimizations that make up the compiler, they
23build the analysis results that are used by these transformations, and they
24are, above all, a structuring technique for compiler code.
25
26All LLVM passes are subclasses of the `Pass
27<https://llvm.org/doxygen/classllvm_1_1Pass.html>`_ class, which implement
28functionality by overriding virtual methods inherited from ``Pass``.  Depending
29on how your pass works, you should inherit from the :ref:`ModulePass
30<writing-an-llvm-pass-ModulePass>` , :ref:`CallGraphSCCPass
31<writing-an-llvm-pass-CallGraphSCCPass>`, :ref:`FunctionPass
32<writing-an-llvm-pass-FunctionPass>` , or :ref:`LoopPass
33<writing-an-llvm-pass-LoopPass>`, or :ref:`RegionPass
34<writing-an-llvm-pass-RegionPass>` classes, which gives the system more
35information about what your pass does, and how it can be combined with other
36passes.  One of the main features of the LLVM Pass Framework is that it
37schedules passes to run in an efficient way based on the constraints that your
38pass meets (which are indicated by which class they derive from).
39
40.. _writing-an-llvm-pass-pass-classes:
41
42Pass classes and requirements
43=============================
44
45One of the first things that you should do when designing a new pass is to
46decide what class you should subclass for your pass. Here we talk about the
47classes available, from the most general to the most specific.
48
49When choosing a superclass for your ``Pass``, you should choose the **most
50specific** class possible, while still being able to meet the requirements
51listed.  This gives the LLVM Pass Infrastructure information necessary to
52optimize how passes are run, so that the resultant compiler isn't unnecessarily
53slow.
54
55The ``ImmutablePass`` class
56---------------------------
57
58The most plain and boring type of pass is the "`ImmutablePass
59<https://llvm.org/doxygen/classllvm_1_1ImmutablePass.html>`_" class.  This pass
60type is used for passes that do not have to be run, do not change state, and
61never need to be updated.  This is not a normal type of transformation or
62analysis, but can provide information about the current compiler configuration.
63
64Although this pass class is very infrequently used, it is important for
65providing information about the current target machine being compiled for, and
66other static information that can affect the various transformations.
67
68``ImmutablePass``\ es never invalidate other transformations, are never
69invalidated, and are never "run".
70
71.. _writing-an-llvm-pass-ModulePass:
72
73The ``ModulePass`` class
74------------------------
75
76The `ModulePass <https://llvm.org/doxygen/classllvm_1_1ModulePass.html>`_ class
77is the most general of all superclasses that you can use.  Deriving from
78``ModulePass`` indicates that your pass uses the entire program as a unit,
79referring to function bodies in no predictable order, or adding and removing
80functions.  Because nothing is known about the behavior of ``ModulePass``
81subclasses, no optimization can be done for their execution.
82
83A module pass can use function level passes (e.g. dominators) using the
84``getAnalysis`` interface ``getAnalysis<DominatorTree>(llvm::Function *)`` to
85provide the function to retrieve analysis result for, if the function pass does
86not require any module or immutable passes.  Note that this can only be done
87for functions for which the analysis ran, e.g. in the case of dominators you
88should only ask for the ``DominatorTree`` for function definitions, not
89declarations.
90
91To write a correct ``ModulePass`` subclass, derive from ``ModulePass`` and
92override the ``runOnModule`` method with the following signature:
93
94The ``runOnModule`` method
95^^^^^^^^^^^^^^^^^^^^^^^^^^
96
97.. code-block:: c++
98
99  virtual bool runOnModule(Module &M) = 0;
100
101The ``runOnModule`` method performs the interesting work of the pass.  It
102should return ``true`` if the module was modified by the transformation and
103``false`` otherwise.
104
105.. _writing-an-llvm-pass-CallGraphSCCPass:
106
107The ``CallGraphSCCPass`` class
108------------------------------
109
110The `CallGraphSCCPass
111<https://llvm.org/doxygen/classllvm_1_1CallGraphSCCPass.html>`_ is used by
112passes that need to traverse the program bottom-up on the call graph (callees
113before callers).  Deriving from ``CallGraphSCCPass`` provides some mechanics
114for building and traversing the ``CallGraph``, but also allows the system to
115optimize execution of ``CallGraphSCCPass``\ es.  If your pass meets the
116requirements outlined below, and doesn't meet the requirements of a
117:ref:`FunctionPass <writing-an-llvm-pass-FunctionPass>`, you should derive from
118``CallGraphSCCPass``.
119
120``TODO``: explain briefly what SCC, Tarjan's algo, and B-U mean.
121
122To be explicit, CallGraphSCCPass subclasses are:
123
124#. ... *not allowed* to inspect or modify any ``Function``\ s other than those
125   in the current SCC and the direct callers and direct callees of the SCC.
126#. ... *required* to preserve the current ``CallGraph`` object, updating it to
127   reflect any changes made to the program.
128#. ... *not allowed* to add or remove SCC's from the current Module, though
129   they may change the contents of an SCC.
130#. ... *allowed* to add or remove global variables from the current Module.
131#. ... *allowed* to maintain state across invocations of :ref:`runOnSCC
132   <writing-an-llvm-pass-runOnSCC>` (including global data).
133
134Implementing a ``CallGraphSCCPass`` is slightly tricky in some cases because it
135has to handle SCCs with more than one node in it.  All of the virtual methods
136described below should return ``true`` if they modified the program, or
137``false`` if they didn't.
138
139The ``doInitialization(CallGraph &)`` method
140^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
141
142.. code-block:: c++
143
144  virtual bool doInitialization(CallGraph &CG);
145
146The ``doInitialization`` method is allowed to do most of the things that
147``CallGraphSCCPass``\ es are not allowed to do.  They can add and remove
148functions, get pointers to functions, etc.  The ``doInitialization`` method is
149designed to do simple initialization type of stuff that does not depend on the
150SCCs being processed.  The ``doInitialization`` method call is not scheduled to
151overlap with any other pass executions (thus it should be very fast).
152
153.. _writing-an-llvm-pass-runOnSCC:
154
155The ``runOnSCC`` method
156^^^^^^^^^^^^^^^^^^^^^^^
157
158.. code-block:: c++
159
160  virtual bool runOnSCC(CallGraphSCC &SCC) = 0;
161
162The ``runOnSCC`` method performs the interesting work of the pass, and should
163return ``true`` if the module was modified by the transformation, ``false``
164otherwise.
165
166The ``doFinalization(CallGraph &)`` method
167^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
168
169.. code-block:: c++
170
171  virtual bool doFinalization(CallGraph &CG);
172
173The ``doFinalization`` method is an infrequently used method that is called
174when the pass framework has finished calling :ref:`runOnSCC
175<writing-an-llvm-pass-runOnSCC>` for every SCC in the program being compiled.
176
177.. _writing-an-llvm-pass-FunctionPass:
178
179The ``FunctionPass`` class
180--------------------------
181
182In contrast to ``ModulePass`` subclasses, `FunctionPass
183<https://llvm.org/doxygen/classllvm_1_1Pass.html>`_ subclasses do have a
184predictable, local behavior that can be expected by the system.  All
185``FunctionPass`` execute on each function in the program independent of all of
186the other functions in the program.  ``FunctionPass``\ es do not require that
187they are executed in a particular order, and ``FunctionPass``\ es do not modify
188external functions.
189
190To be explicit, ``FunctionPass`` subclasses are not allowed to:
191
192#. Inspect or modify a ``Function`` other than the one currently being processed.
193#. Add or remove ``Function``\ s from the current ``Module``.
194#. Add or remove global variables from the current ``Module``.
195#. Maintain state across invocations of :ref:`runOnFunction
196   <writing-an-llvm-pass-runOnFunction>` (including global data).
197
198Implementing a ``FunctionPass`` is usually straightforward. ``FunctionPass``\
199es may override three virtual methods to do their work.  All of these methods
200should return ``true`` if they modified the program, or ``false`` if they
201didn't.
202
203.. _writing-an-llvm-pass-doInitialization-mod:
204
205The ``doInitialization(Module &)`` method
206^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
207
208.. code-block:: c++
209
210  virtual bool doInitialization(Module &M);
211
212The ``doInitialization`` method is allowed to do most of the things that
213``FunctionPass``\ es are not allowed to do.  They can add and remove functions,
214get pointers to functions, etc.  The ``doInitialization`` method is designed to
215do simple initialization type of stuff that does not depend on the functions
216being processed.  The ``doInitialization`` method call is not scheduled to
217overlap with any other pass executions (thus it should be very fast).
218
219A good example of how this method should be used is the `LowerAllocations
220<https://llvm.org/doxygen/LowerAllocations_8cpp-source.html>`_ pass.  This pass
221converts ``malloc`` and ``free`` instructions into platform dependent
222``malloc()`` and ``free()`` function calls.  It uses the ``doInitialization``
223method to get a reference to the ``malloc`` and ``free`` functions that it
224needs, adding prototypes to the module if necessary.
225
226.. _writing-an-llvm-pass-runOnFunction:
227
228The ``runOnFunction`` method
229^^^^^^^^^^^^^^^^^^^^^^^^^^^^
230
231.. code-block:: c++
232
233  virtual bool runOnFunction(Function &F) = 0;
234
235The ``runOnFunction`` method must be implemented by your subclass to do the
236transformation or analysis work of your pass.  As usual, a ``true`` value
237should be returned if the function is modified.
238
239.. _writing-an-llvm-pass-doFinalization-mod:
240
241The ``doFinalization(Module &)`` method
242^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
243
244.. code-block:: c++
245
246  virtual bool doFinalization(Module &M);
247
248The ``doFinalization`` method is an infrequently used method that is called
249when the pass framework has finished calling :ref:`runOnFunction
250<writing-an-llvm-pass-runOnFunction>` for every function in the program being
251compiled.
252
253.. _writing-an-llvm-pass-LoopPass:
254
255The ``LoopPass`` class
256----------------------
257
258All ``LoopPass`` execute on each :ref:`loop <loop-terminology>` in the function
259independent of all of the other loops in the function.  ``LoopPass`` processes
260loops in loop nest order such that outer most loop is processed last.
261
262``LoopPass`` subclasses are allowed to update loop nest using ``LPPassManager``
263interface.  Implementing a loop pass is usually straightforward.
264``LoopPass``\ es may override three virtual methods to do their work.  All
265these methods should return ``true`` if they modified the program, or ``false``
266if they didn't.
267
268A ``LoopPass`` subclass which is intended to run as part of the main loop pass
269pipeline needs to preserve all of the same *function* analyses that the other
270loop passes in its pipeline require. To make that easier,
271a ``getLoopAnalysisUsage`` function is provided by ``LoopUtils.h``. It can be
272called within the subclass's ``getAnalysisUsage`` override to get consistent
273and correct behavior. Analogously, ``INITIALIZE_PASS_DEPENDENCY(LoopPass)``
274will initialize this set of function analyses.
275
276The ``doInitialization(Loop *, LPPassManager &)`` method
277^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
278
279.. code-block:: c++
280
281  virtual bool doInitialization(Loop *, LPPassManager &LPM);
282
283The ``doInitialization`` method is designed to do simple initialization type of
284stuff that does not depend on the functions being processed.  The
285``doInitialization`` method call is not scheduled to overlap with any other
286pass executions (thus it should be very fast).  ``LPPassManager`` interface
287should be used to access ``Function`` or ``Module`` level analysis information.
288
289.. _writing-an-llvm-pass-runOnLoop:
290
291The ``runOnLoop`` method
292^^^^^^^^^^^^^^^^^^^^^^^^
293
294.. code-block:: c++
295
296  virtual bool runOnLoop(Loop *, LPPassManager &LPM) = 0;
297
298The ``runOnLoop`` method must be implemented by your subclass to do the
299transformation or analysis work of your pass.  As usual, a ``true`` value
300should be returned if the function is modified.  ``LPPassManager`` interface
301should be used to update loop nest.
302
303The ``doFinalization()`` method
304^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
305
306.. code-block:: c++
307
308  virtual bool doFinalization();
309
310The ``doFinalization`` method is an infrequently used method that is called
311when the pass framework has finished calling :ref:`runOnLoop
312<writing-an-llvm-pass-runOnLoop>` for every loop in the program being compiled.
313
314.. _writing-an-llvm-pass-RegionPass:
315
316The ``RegionPass`` class
317------------------------
318
319``RegionPass`` is similar to :ref:`LoopPass <writing-an-llvm-pass-LoopPass>`,
320but executes on each single entry single exit region in the function.
321``RegionPass`` processes regions in nested order such that the outer most
322region is processed last.
323
324``RegionPass`` subclasses are allowed to update the region tree by using the
325``RGPassManager`` interface.  You may override three virtual methods of
326``RegionPass`` to implement your own region pass.  All these methods should
327return ``true`` if they modified the program, or ``false`` if they did not.
328
329The ``doInitialization(Region *, RGPassManager &)`` method
330^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
331
332.. code-block:: c++
333
334  virtual bool doInitialization(Region *, RGPassManager &RGM);
335
336The ``doInitialization`` method is designed to do simple initialization type of
337stuff that does not depend on the functions being processed.  The
338``doInitialization`` method call is not scheduled to overlap with any other
339pass executions (thus it should be very fast).  ``RPPassManager`` interface
340should be used to access ``Function`` or ``Module`` level analysis information.
341
342.. _writing-an-llvm-pass-runOnRegion:
343
344The ``runOnRegion`` method
345^^^^^^^^^^^^^^^^^^^^^^^^^^
346
347.. code-block:: c++
348
349  virtual bool runOnRegion(Region *, RGPassManager &RGM) = 0;
350
351The ``runOnRegion`` method must be implemented by your subclass to do the
352transformation or analysis work of your pass.  As usual, a true value should be
353returned if the region is modified.  ``RGPassManager`` interface should be used to
354update region tree.
355
356The ``doFinalization()`` method
357^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
358
359.. code-block:: c++
360
361  virtual bool doFinalization();
362
363The ``doFinalization`` method is an infrequently used method that is called
364when the pass framework has finished calling :ref:`runOnRegion
365<writing-an-llvm-pass-runOnRegion>` for every region in the program being
366compiled.
367
368
369The ``MachineFunctionPass`` class
370---------------------------------
371
372A ``MachineFunctionPass`` is a part of the LLVM code generator that executes on
373the machine-dependent representation of each LLVM function in the program.
374
375Code generator passes are registered and initialized specially by
376``TargetMachine::addPassesToEmitFile`` and similar routines, so they cannot
377generally be run from the :program:`opt` or :program:`bugpoint` commands.
378
379A ``MachineFunctionPass`` is also a ``FunctionPass``, so all the restrictions
380that apply to a ``FunctionPass`` also apply to it.  ``MachineFunctionPass``\ es
381also have additional restrictions.  In particular, ``MachineFunctionPass``\ es
382are not allowed to do any of the following:
383
384#. Modify or create any LLVM IR ``Instruction``\ s, ``BasicBlock``\ s,
385   ``Argument``\ s, ``Function``\ s, ``GlobalVariable``\ s,
386   ``GlobalAlias``\ es, or ``Module``\ s.
387#. Modify a ``MachineFunction`` other than the one currently being processed.
388#. Maintain state across invocations of :ref:`runOnMachineFunction
389   <writing-an-llvm-pass-runOnMachineFunction>` (including global data).
390
391.. _writing-an-llvm-pass-runOnMachineFunction:
392
393The ``runOnMachineFunction(MachineFunction &MF)`` method
394^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
395
396.. code-block:: c++
397
398  virtual bool runOnMachineFunction(MachineFunction &MF) = 0;
399
400``runOnMachineFunction`` can be considered the main entry point of a
401``MachineFunctionPass``; that is, you should override this method to do the
402work of your ``MachineFunctionPass``.
403
404The ``runOnMachineFunction`` method is called on every ``MachineFunction`` in a
405``Module``, so that the ``MachineFunctionPass`` may perform optimizations on
406the machine-dependent representation of the function.  If you want to get at
407the LLVM ``Function`` for the ``MachineFunction`` you're working on, use
408``MachineFunction``'s ``getFunction()`` accessor method --- but remember, you
409may not modify the LLVM ``Function`` or its contents from a
410``MachineFunctionPass``.
411
412.. _writing-an-llvm-pass-registration:
413
414Pass registration
415-----------------
416
417Passes are registered with the ``RegisterPass`` template.  The template
418parameter is the name of the pass that is to be used on the command line to
419specify that the pass should be added to a program. The first argument is the
420name of the pass, which is to be used for the :option:`-help` output of
421programs, as well as for debug output generated by the `--debug-pass` option.
422
423If you want your pass to be easily dumpable, you should implement the virtual
424print method:
425
426The ``print`` method
427^^^^^^^^^^^^^^^^^^^^
428
429.. code-block:: c++
430
431  virtual void print(llvm::raw_ostream &O, const Module *M) const;
432
433The ``print`` method must be implemented by "analyses" in order to print a
434human readable version of the analysis results.  This is useful for debugging
435an analysis itself, as well as for other people to figure out how an analysis
436works.  Use the opt ``-analyze`` argument to invoke this method.
437
438The ``llvm::raw_ostream`` parameter specifies the stream to write the results
439on, and the ``Module`` parameter gives a pointer to the top level module of the
440program that has been analyzed.  Note however that this pointer may be ``NULL``
441in certain circumstances (such as calling the ``Pass::dump()`` from a
442debugger), so it should only be used to enhance debug output, it should not be
443depended on.
444
445.. _writing-an-llvm-pass-interaction:
446
447Specifying interactions between passes
448--------------------------------------
449
450One of the main responsibilities of the ``PassManager`` is to make sure that
451passes interact with each other correctly.  Because ``PassManager`` tries to
452:ref:`optimize the execution of passes <writing-an-llvm-pass-passmanager>` it
453must know how the passes interact with each other and what dependencies exist
454between the various passes.  To track this, each pass can declare the set of
455passes that are required to be executed before the current pass, and the passes
456which are invalidated by the current pass.
457
458Typically this functionality is used to require that analysis results are
459computed before your pass is run.  Running arbitrary transformation passes can
460invalidate the computed analysis results, which is what the invalidation set
461specifies.  If a pass does not implement the :ref:`getAnalysisUsage
462<writing-an-llvm-pass-getAnalysisUsage>` method, it defaults to not having any
463prerequisite passes, and invalidating **all** other passes.
464
465.. _writing-an-llvm-pass-getAnalysisUsage:
466
467The ``getAnalysisUsage`` method
468^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
469
470.. code-block:: c++
471
472  virtual void getAnalysisUsage(AnalysisUsage &Info) const;
473
474By implementing the ``getAnalysisUsage`` method, the required and invalidated
475sets may be specified for your transformation.  The implementation should fill
476in the `AnalysisUsage
477<https://llvm.org/doxygen/classllvm_1_1AnalysisUsage.html>`_ object with
478information about which passes are required and not invalidated.  To do this, a
479pass may call any of the following methods on the ``AnalysisUsage`` object:
480
481The ``AnalysisUsage::addRequired<>`` and ``AnalysisUsage::addRequiredTransitive<>`` methods
482^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
483
484If your pass requires a previous pass to be executed (an analysis for example),
485it can use one of these methods to arrange for it to be run before your pass.
486LLVM has many different types of analyses and passes that can be required,
487spanning the range from ``DominatorSet`` to ``BreakCriticalEdges``.  Requiring
488``BreakCriticalEdges``, for example, guarantees that there will be no critical
489edges in the CFG when your pass has been run.
490
491Some analyses chain to other analyses to do their job.  For example, an
492`AliasAnalysis <AliasAnalysis.html>`_ implementation is required to :ref:`chain
493<aliasanalysis-chaining>` to other alias analysis passes.  In cases where
494analyses chain, the ``addRequiredTransitive`` method should be used instead of
495the ``addRequired`` method.  This informs the ``PassManager`` that the
496transitively required pass should be alive as long as the requiring pass is.
497
498The ``AnalysisUsage::addPreserved<>`` method
499^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
500
501One of the jobs of the ``PassManager`` is to optimize how and when analyses are
502run.  In particular, it attempts to avoid recomputing data unless it needs to.
503For this reason, passes are allowed to declare that they preserve (i.e., they
504don't invalidate) an existing analysis if it's available.  For example, a
505simple constant folding pass would not modify the CFG, so it can't possibly
506affect the results of dominator analysis.  By default, all passes are assumed
507to invalidate all others.
508
509The ``AnalysisUsage`` class provides several methods which are useful in
510certain circumstances that are related to ``addPreserved``.  In particular, the
511``setPreservesAll`` method can be called to indicate that the pass does not
512modify the LLVM program at all (which is true for analyses), and the
513``setPreservesCFG`` method can be used by transformations that change
514instructions in the program but do not modify the CFG or terminator
515instructions.
516
517``addPreserved`` is particularly useful for transformations like
518``BreakCriticalEdges``.  This pass knows how to update a small set of loop and
519dominator related analyses if they exist, so it can preserve them, despite the
520fact that it hacks on the CFG.
521
522Example implementations of ``getAnalysisUsage``
523^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
524
525.. code-block:: c++
526
527  // This example modifies the program, but does not modify the CFG
528  void LICM::getAnalysisUsage(AnalysisUsage &AU) const {
529    AU.setPreservesCFG();
530    AU.addRequired<LoopInfoWrapperPass>();
531  }
532
533.. _writing-an-llvm-pass-getAnalysis:
534
535The ``getAnalysis<>`` and ``getAnalysisIfAvailable<>`` methods
536^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
537
538The ``Pass::getAnalysis<>`` method is automatically inherited by your class,
539providing you with access to the passes that you declared that you required
540with the :ref:`getAnalysisUsage <writing-an-llvm-pass-getAnalysisUsage>`
541method.  It takes a single template argument that specifies which pass class
542you want, and returns a reference to that pass.  For example:
543
544.. code-block:: c++
545
546  bool LICM::runOnFunction(Function &F) {
547    LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
548    //...
549  }
550
551This method call returns a reference to the pass desired.  You may get a
552runtime assertion failure if you attempt to get an analysis that you did not
553declare as required in your :ref:`getAnalysisUsage
554<writing-an-llvm-pass-getAnalysisUsage>` implementation.  This method can be
555called by your ``run*`` method implementation, or by any other local method
556invoked by your ``run*`` method.
557
558A module level pass can use function level analysis info using this interface.
559For example:
560
561.. code-block:: c++
562
563  bool ModuleLevelPass::runOnModule(Module &M) {
564    //...
565    DominatorTree &DT = getAnalysis<DominatorTree>(Func);
566    //...
567  }
568
569In above example, ``runOnFunction`` for ``DominatorTree`` is called by pass
570manager before returning a reference to the desired pass.
571
572If your pass is capable of updating analyses if they exist (e.g.,
573``BreakCriticalEdges``, as described above), you can use the
574``getAnalysisIfAvailable`` method, which returns a pointer to the analysis if
575it is active.  For example:
576
577.. code-block:: c++
578
579  if (DominatorSet *DS = getAnalysisIfAvailable<DominatorSet>()) {
580    // A DominatorSet is active.  This code will update it.
581  }
582
583Pass Statistics
584===============
585
586The `Statistic <https://llvm.org/doxygen/Statistic_8h_source.html>`_ class is
587designed to be an easy way to expose various success metrics from passes.
588These statistics are printed at the end of a run, when the :option:`-stats`
589command line option is enabled on the command line.  See the :ref:`Statistics
590section <Statistic>` in the Programmer's Manual for details.
591
592.. _writing-an-llvm-pass-passmanager:
593
594What PassManager does
595---------------------
596
597The `PassManager <https://llvm.org/doxygen/PassManager_8h_source.html>`_ `class
598<https://llvm.org/doxygen/classllvm_1_1PassManager.html>`_ takes a list of
599passes, ensures their :ref:`prerequisites <writing-an-llvm-pass-interaction>`
600are set up correctly, and then schedules passes to run efficiently.  All of the
601LLVM tools that run passes use the PassManager for execution of these passes.
602
603The PassManager does two main things to try to reduce the execution time of a
604series of passes:
605
606#. **Share analysis results.**  The ``PassManager`` attempts to avoid
607   recomputing analysis results as much as possible.  This means keeping track
608   of which analyses are available already, which analyses get invalidated, and
609   which analyses are needed to be run for a pass.  An important part of work
610   is that the ``PassManager`` tracks the exact lifetime of all analysis
611   results, allowing it to :ref:`free memory
612   <writing-an-llvm-pass-releaseMemory>` allocated to holding analysis results
613   as soon as they are no longer needed.
614
615#. **Pipeline the execution of passes on the program.**  The ``PassManager``
616   attempts to get better cache and memory usage behavior out of a series of
617   passes by pipelining the passes together.  This means that, given a series
618   of consecutive :ref:`FunctionPass <writing-an-llvm-pass-FunctionPass>`, it
619   will execute all of the :ref:`FunctionPass
620   <writing-an-llvm-pass-FunctionPass>` on the first function, then all of the
621   :ref:`FunctionPasses <writing-an-llvm-pass-FunctionPass>` on the second
622   function, etc... until the entire program has been run through the passes.
623
624   This improves the cache behavior of the compiler, because it is only
625   touching the LLVM program representation for a single function at a time,
626   instead of traversing the entire program.  It reduces the memory consumption
627   of compiler, because, for example, only one `DominatorSet
628   <https://llvm.org/doxygen/classllvm_1_1DominatorSet.html>`_ needs to be
629   calculated at a time.
630
631The effectiveness of the ``PassManager`` is influenced directly by how much
632information it has about the behaviors of the passes it is scheduling.  For
633example, the "preserved" set is intentionally conservative in the face of an
634unimplemented :ref:`getAnalysisUsage <writing-an-llvm-pass-getAnalysisUsage>`
635method.  Not implementing when it should be implemented will have the effect of
636not allowing any analysis results to live across the execution of your pass.
637
638The ``PassManager`` class exposes a ``--debug-pass`` command line options that
639is useful for debugging pass execution, seeing how things work, and diagnosing
640when you should be preserving more analyses than you currently are.  (To get
641information about all of the variants of the ``--debug-pass`` option, just type
642"``llc -help-hidden``").
643
644By using the --debug-pass=Structure option, for example, we can see inspect the
645default optimization pipelines, e.g. (the output has been trimmed):
646
647.. code-block:: console
648
649  $ llc -mtriple=arm64-- -O3 -debug-pass=Structure file.ll > /dev/null
650  (...)
651  ModulePass Manager
652  Pre-ISel Intrinsic Lowering
653  FunctionPass Manager
654    Expand large div/rem
655    Expand large fp convert
656    Expand Atomic instructions
657  SVE intrinsics optimizations
658    FunctionPass Manager
659      Dominator Tree Construction
660  FunctionPass Manager
661    Simplify the CFG
662    Dominator Tree Construction
663    Natural Loop Information
664    Canonicalize natural loops
665  (...)
666
667.. _writing-an-llvm-pass-releaseMemory:
668
669The ``releaseMemory`` method
670^^^^^^^^^^^^^^^^^^^^^^^^^^^^
671
672.. code-block:: c++
673
674  virtual void releaseMemory();
675
676The ``PassManager`` automatically determines when to compute analysis results,
677and how long to keep them around for.  Because the lifetime of the pass object
678itself is effectively the entire duration of the compilation process, we need
679some way to free analysis results when they are no longer useful.  The
680``releaseMemory`` virtual method is the way to do this.
681
682If you are writing an analysis or any other pass that retains a significant
683amount of state (for use by another pass which "requires" your pass and uses
684the :ref:`getAnalysis <writing-an-llvm-pass-getAnalysis>` method) you should
685implement ``releaseMemory`` to, well, release the memory allocated to maintain
686this internal state.  This method is called after the ``run*`` method for the
687class, before the next call of ``run*`` in your pass.
688
689Registering dynamically loaded passes
690=====================================
691
692*Size matters* when constructing production quality tools using LLVM, both for
693the purposes of distribution, and for regulating the resident code size when
694running on the target system.  Therefore, it becomes desirable to selectively
695use some passes, while omitting others and maintain the flexibility to change
696configurations later on.  You want to be able to do all this, and, provide
697feedback to the user.  This is where pass registration comes into play.
698
699The fundamental mechanisms for pass registration are the
700``MachinePassRegistry`` class and subclasses of ``MachinePassRegistryNode``.
701
702An instance of ``MachinePassRegistry`` is used to maintain a list of
703``MachinePassRegistryNode`` objects.  This instance maintains the list and
704communicates additions and deletions to the command line interface.
705
706An instance of ``MachinePassRegistryNode`` subclass is used to maintain
707information provided about a particular pass.  This information includes the
708command line name, the command help string and the address of the function used
709to create an instance of the pass.  A global static constructor of one of these
710instances *registers* with a corresponding ``MachinePassRegistry``, the static
711destructor *unregisters*.  Thus a pass that is statically linked in the tool
712will be registered at start up.  A dynamically loaded pass will register on
713load and unregister at unload.
714
715Using existing registries
716-------------------------
717
718There are predefined registries to track instruction scheduling
719(``RegisterScheduler``) and register allocation (``RegisterRegAlloc``) machine
720passes.  Here we will describe how to *register* a register allocator machine
721pass.
722
723Implement your register allocator machine pass.  In your register allocator
724``.cpp`` file add the following include:
725
726.. code-block:: c++
727
728  #include "llvm/CodeGen/RegAllocRegistry.h"
729
730Also in your register allocator ``.cpp`` file, define a creator function in the
731form:
732
733.. code-block:: c++
734
735  FunctionPass *createMyRegisterAllocator() {
736    return new MyRegisterAllocator();
737  }
738
739Note that the signature of this function should match the type of
740``RegisterRegAlloc::FunctionPassCtor``.  In the same file add the "installing"
741declaration, in the form:
742
743.. code-block:: c++
744
745  static RegisterRegAlloc myRegAlloc("myregalloc",
746                                     "my register allocator help string",
747                                     createMyRegisterAllocator);
748
749Note the two spaces prior to the help string produces a tidy result on the
750:option:`-help` query.
751
752.. code-block:: console
753
754  $ llc -help
755    ...
756    -regalloc                    - Register allocator to use (default=linearscan)
757      =linearscan                -   linear scan register allocator
758      =local                     -   local register allocator
759      =simple                    -   simple register allocator
760      =myregalloc                -   my register allocator help string
761    ...
762
763And that's it.  The user is now free to use ``-regalloc=myregalloc`` as an
764option.  Registering instruction schedulers is similar except use the
765``RegisterScheduler`` class.  Note that the
766``RegisterScheduler::FunctionPassCtor`` is significantly different from
767``RegisterRegAlloc::FunctionPassCtor``.
768
769To force the load/linking of your register allocator into the
770:program:`llc`/:program:`lli` tools, add your creator function's global
771declaration to ``Passes.h`` and add a "pseudo" call line to
772``llvm/Codegen/LinkAllCodegenComponents.h``.
773
774Creating new registries
775-----------------------
776
777The easiest way to get started is to clone one of the existing registries; we
778recommend ``llvm/CodeGen/RegAllocRegistry.h``.  The key things to modify are
779the class name and the ``FunctionPassCtor`` type.
780
781Then you need to declare the registry.  Example: if your pass registry is
782``RegisterMyPasses`` then define:
783
784.. code-block:: c++
785
786  MachinePassRegistry<RegisterMyPasses::FunctionPassCtor> RegisterMyPasses::Registry;
787
788And finally, declare the command line option for your passes.  Example:
789
790.. code-block:: c++
791
792  cl::opt<RegisterMyPasses::FunctionPassCtor, false,
793          RegisterPassParser<RegisterMyPasses> >
794  MyPassOpt("mypass",
795            cl::init(&createDefaultMyPass),
796            cl::desc("my pass option help"));
797
798Here the command option is "``mypass``", with ``createDefaultMyPass`` as the
799default creator.
800
801Using GDB with dynamically loaded passes
802----------------------------------------
803
804Unfortunately, using GDB with dynamically loaded passes is not as easy as it
805should be.  First of all, you can't set a breakpoint in a shared object that
806has not been loaded yet, and second of all there are problems with inlined
807functions in shared objects.  Here are some suggestions to debugging your pass
808with GDB.
809
810For sake of discussion, I'm going to assume that you are debugging a
811transformation invoked by :program:`opt`, although nothing described here
812depends on that.
813
814Setting a breakpoint in your pass
815^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
816
817First thing you do is start gdb on the opt process:
818
819.. code-block:: console
820
821  $ gdb opt
822  GNU gdb 5.0
823  Copyright 2000 Free Software Foundation, Inc.
824  GDB is free software, covered by the GNU General Public License, and you are
825  welcome to change it and/or distribute copies of it under certain conditions.
826  Type "show copying" to see the conditions.
827  There is absolutely no warranty for GDB.  Type "show warranty" for details.
828  This GDB was configured as "sparc-sun-solaris2.6"...
829  (gdb)
830
831Note that :program:`opt` has a lot of debugging information in it, so it takes
832time to load.  Be patient.  Since we cannot set a breakpoint in our pass yet
833(the shared object isn't loaded until runtime), we must execute the process,
834and have it stop before it invokes our pass, but after it has loaded the shared
835object.  The most foolproof way of doing this is to set a breakpoint in
836``PassManager::run`` and then run the process with the arguments you want:
837
838.. code-block:: console
839
840  $ (gdb) break llvm::PassManager::run
841  Breakpoint 1 at 0x2413bc: file Pass.cpp, line 70.
842  (gdb) run test.bc -load $(LLVMTOP)/llvm/Debug+Asserts/lib/[libname].so -[passoption]
843  Starting program: opt test.bc -load $(LLVMTOP)/llvm/Debug+Asserts/lib/[libname].so -[passoption]
844  Breakpoint 1, PassManager::run (this=0xffbef174, M=@0x70b298) at Pass.cpp:70
845  70      bool PassManager::run(Module &M) { return PM->run(M); }
846  (gdb)
847
848Once the :program:`opt` stops in the ``PassManager::run`` method you are now
849free to set breakpoints in your pass so that you can trace through execution or
850do other standard debugging stuff.
851
852Miscellaneous Problems
853^^^^^^^^^^^^^^^^^^^^^^
854
855Once you have the basics down, there are a couple of problems that GDB has,
856some with solutions, some without.
857
858* Inline functions have bogus stack information.  In general, GDB does a pretty
859  good job getting stack traces and stepping through inline functions.  When a
860  pass is dynamically loaded however, it somehow completely loses this
861  capability.  The only solution I know of is to de-inline a function (move it
862  from the body of a class to a ``.cpp`` file).
863
864* Restarting the program breaks breakpoints.  After following the information
865  above, you have succeeded in getting some breakpoints planted in your pass.
866  Next thing you know, you restart the program (i.e., you type "``run``" again),
867  and you start getting errors about breakpoints being unsettable.  The only
868  way I have found to "fix" this problem is to delete the breakpoints that are
869  already set in your pass, run the program, and re-set the breakpoints once
870  execution stops in ``PassManager::run``.
871
872Hopefully these tips will help with common case debugging situations.  If you'd
873like to contribute some tips of your own, just contact `Chris
874<mailto:sabre@nondot.org>`_.
875