1 ========================================
2 Writing an LLVM Pass (legacy PM version)
3 ========================================
10 Introduction --- What is a pass?
11 ================================
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`.
20 The LLVM Pass Framework is an important part of the LLVM system, because LLVM
21 passes are where most of the interesting parts of the compiler exist. Passes
22 perform the transformations and optimizations that make up the compiler, they
23 build the analysis results that are used by these transformations, and they
24 are, above all, a structuring technique for compiler code.
26 All LLVM passes are subclasses of the `Pass
27 <https://llvm.org/doxygen/classllvm_1_1Pass.html>`_ class, which implement
28 functionality by overriding virtual methods inherited from ``Pass``. Depending
29 on 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
35 information about what your pass does, and how it can be combined with other
36 passes. One of the main features of the LLVM Pass Framework is that it
37 schedules passes to run in an efficient way based on the constraints that your
38 pass meets (which are indicated by which class they derive from).
40 .. _writing-an-llvm-pass-pass-classes:
42 Pass classes and requirements
43 =============================
45 One of the first things that you should do when designing a new pass is to
46 decide what class you should subclass for your pass. Here we talk about the
47 classes available, from the most general to the most specific.
49 When choosing a superclass for your ``Pass``, you should choose the **most
50 specific** class possible, while still being able to meet the requirements
51 listed. This gives the LLVM Pass Infrastructure information necessary to
52 optimize how passes are run, so that the resultant compiler isn't unnecessarily
55 The ``ImmutablePass`` class
56 ---------------------------
58 The most plain and boring type of pass is the "`ImmutablePass
59 <https://llvm.org/doxygen/classllvm_1_1ImmutablePass.html>`_" class. This pass
60 type is used for passes that do not have to be run, do not change state, and
61 never need to be updated. This is not a normal type of transformation or
62 analysis, but can provide information about the current compiler configuration.
64 Although this pass class is very infrequently used, it is important for
65 providing information about the current target machine being compiled for, and
66 other static information that can affect the various transformations.
68 ``ImmutablePass``\ es never invalidate other transformations, are never
69 invalidated, and are never "run".
71 .. _writing-an-llvm-pass-ModulePass:
73 The ``ModulePass`` class
74 ------------------------
76 The `ModulePass <https://llvm.org/doxygen/classllvm_1_1ModulePass.html>`_ class
77 is 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,
79 referring to function bodies in no predictable order, or adding and removing
80 functions. Because nothing is known about the behavior of ``ModulePass``
81 subclasses, no optimization can be done for their execution.
83 A module pass can use function level passes (e.g. dominators) using the
84 ``getAnalysis`` interface ``getAnalysis<DominatorTree>(llvm::Function *)`` to
85 provide the function to retrieve analysis result for, if the function pass does
86 not require any module or immutable passes. Note that this can only be done
87 for functions for which the analysis ran, e.g. in the case of dominators you
88 should only ask for the ``DominatorTree`` for function definitions, not
91 To write a correct ``ModulePass`` subclass, derive from ``ModulePass`` and
92 override the ``runOnModule`` method with the following signature:
94 The ``runOnModule`` method
95 ^^^^^^^^^^^^^^^^^^^^^^^^^^
99 virtual bool runOnModule(Module &M) = 0;
101 The ``runOnModule`` method performs the interesting work of the pass. It
102 should return ``true`` if the module was modified by the transformation and
105 .. _writing-an-llvm-pass-CallGraphSCCPass:
107 The ``CallGraphSCCPass`` class
108 ------------------------------
110 The `CallGraphSCCPass
111 <https://llvm.org/doxygen/classllvm_1_1CallGraphSCCPass.html>`_ is used by
112 passes that need to traverse the program bottom-up on the call graph (callees
113 before callers). Deriving from ``CallGraphSCCPass`` provides some mechanics
114 for building and traversing the ``CallGraph``, but also allows the system to
115 optimize execution of ``CallGraphSCCPass``\ es. If your pass meets the
116 requirements outlined below, and doesn't meet the requirements of a
117 :ref:`FunctionPass <writing-an-llvm-pass-FunctionPass>`, you should derive from
118 ``CallGraphSCCPass``.
120 ``TODO``: explain briefly what SCC, Tarjan's algo, and B-U mean.
122 To be explicit, CallGraphSCCPass subclasses are:
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).
134 Implementing a ``CallGraphSCCPass`` is slightly tricky in some cases because it
135 has to handle SCCs with more than one node in it. All of the virtual methods
136 described below should return ``true`` if they modified the program, or
137 ``false`` if they didn't.
139 The ``doInitialization(CallGraph &)`` method
140 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
144 virtual bool doInitialization(CallGraph &CG);
146 The ``doInitialization`` method is allowed to do most of the things that
147 ``CallGraphSCCPass``\ es are not allowed to do. They can add and remove
148 functions, get pointers to functions, etc. The ``doInitialization`` method is
149 designed to do simple initialization type of stuff that does not depend on the
150 SCCs being processed. The ``doInitialization`` method call is not scheduled to
151 overlap with any other pass executions (thus it should be very fast).
153 .. _writing-an-llvm-pass-runOnSCC:
155 The ``runOnSCC`` method
156 ^^^^^^^^^^^^^^^^^^^^^^^
160 virtual bool runOnSCC(CallGraphSCC &SCC) = 0;
162 The ``runOnSCC`` method performs the interesting work of the pass, and should
163 return ``true`` if the module was modified by the transformation, ``false``
166 The ``doFinalization(CallGraph &)`` method
167 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
171 virtual bool doFinalization(CallGraph &CG);
173 The ``doFinalization`` method is an infrequently used method that is called
174 when the pass framework has finished calling :ref:`runOnSCC
175 <writing-an-llvm-pass-runOnSCC>` for every SCC in the program being compiled.
177 .. _writing-an-llvm-pass-FunctionPass:
179 The ``FunctionPass`` class
180 --------------------------
182 In contrast to ``ModulePass`` subclasses, `FunctionPass
183 <https://llvm.org/doxygen/classllvm_1_1Pass.html>`_ subclasses do have a
184 predictable, local behavior that can be expected by the system. All
185 ``FunctionPass`` execute on each function in the program independent of all of
186 the other functions in the program. ``FunctionPass``\ es do not require that
187 they are executed in a particular order, and ``FunctionPass``\ es do not modify
190 To be explicit, ``FunctionPass`` subclasses are not allowed to:
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).
198 Implementing a ``FunctionPass`` is usually straightforward. ``FunctionPass``\
199 es may override three virtual methods to do their work. All of these methods
200 should return ``true`` if they modified the program, or ``false`` if they
203 .. _writing-an-llvm-pass-doInitialization-mod:
205 The ``doInitialization(Module &)`` method
206 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
210 virtual bool doInitialization(Module &M);
212 The ``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,
214 get pointers to functions, etc. The ``doInitialization`` method is designed to
215 do simple initialization type of stuff that does not depend on the functions
216 being processed. The ``doInitialization`` method call is not scheduled to
217 overlap with any other pass executions (thus it should be very fast).
219 A good example of how this method should be used is the `LowerAllocations
220 <https://llvm.org/doxygen/LowerAllocations_8cpp-source.html>`_ pass. This pass
221 converts ``malloc`` and ``free`` instructions into platform dependent
222 ``malloc()`` and ``free()`` function calls. It uses the ``doInitialization``
223 method to get a reference to the ``malloc`` and ``free`` functions that it
224 needs, adding prototypes to the module if necessary.
226 .. _writing-an-llvm-pass-runOnFunction:
228 The ``runOnFunction`` method
229 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
233 virtual bool runOnFunction(Function &F) = 0;
235 The ``runOnFunction`` method must be implemented by your subclass to do the
236 transformation or analysis work of your pass. As usual, a ``true`` value
237 should be returned if the function is modified.
239 .. _writing-an-llvm-pass-doFinalization-mod:
241 The ``doFinalization(Module &)`` method
242 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
246 virtual bool doFinalization(Module &M);
248 The ``doFinalization`` method is an infrequently used method that is called
249 when the pass framework has finished calling :ref:`runOnFunction
250 <writing-an-llvm-pass-runOnFunction>` for every function in the program being
253 .. _writing-an-llvm-pass-LoopPass:
255 The ``LoopPass`` class
256 ----------------------
258 All ``LoopPass`` execute on each :ref:`loop <loop-terminology>` in the function
259 independent of all of the other loops in the function. ``LoopPass`` processes
260 loops in loop nest order such that outer most loop is processed last.
262 ``LoopPass`` subclasses are allowed to update loop nest using ``LPPassManager``
263 interface. Implementing a loop pass is usually straightforward.
264 ``LoopPass``\ es may override three virtual methods to do their work. All
265 these methods should return ``true`` if they modified the program, or ``false``
268 A ``LoopPass`` subclass which is intended to run as part of the main loop pass
269 pipeline needs to preserve all of the same *function* analyses that the other
270 loop passes in its pipeline require. To make that easier,
271 a ``getLoopAnalysisUsage`` function is provided by ``LoopUtils.h``. It can be
272 called within the subclass's ``getAnalysisUsage`` override to get consistent
273 and correct behavior. Analogously, ``INITIALIZE_PASS_DEPENDENCY(LoopPass)``
274 will initialize this set of function analyses.
276 The ``doInitialization(Loop *, LPPassManager &)`` method
277 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
281 virtual bool doInitialization(Loop *, LPPassManager &LPM);
283 The ``doInitialization`` method is designed to do simple initialization type of
284 stuff that does not depend on the functions being processed. The
285 ``doInitialization`` method call is not scheduled to overlap with any other
286 pass executions (thus it should be very fast). ``LPPassManager`` interface
287 should be used to access ``Function`` or ``Module`` level analysis information.
289 .. _writing-an-llvm-pass-runOnLoop:
291 The ``runOnLoop`` method
292 ^^^^^^^^^^^^^^^^^^^^^^^^
296 virtual bool runOnLoop(Loop *, LPPassManager &LPM) = 0;
298 The ``runOnLoop`` method must be implemented by your subclass to do the
299 transformation or analysis work of your pass. As usual, a ``true`` value
300 should be returned if the function is modified. ``LPPassManager`` interface
301 should be used to update loop nest.
303 The ``doFinalization()`` method
304 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
308 virtual bool doFinalization();
310 The ``doFinalization`` method is an infrequently used method that is called
311 when the pass framework has finished calling :ref:`runOnLoop
312 <writing-an-llvm-pass-runOnLoop>` for every loop in the program being compiled.
314 .. _writing-an-llvm-pass-RegionPass:
316 The ``RegionPass`` class
317 ------------------------
319 ``RegionPass`` is similar to :ref:`LoopPass <writing-an-llvm-pass-LoopPass>`,
320 but executes on each single entry single exit region in the function.
321 ``RegionPass`` processes regions in nested order such that the outer most
322 region is processed last.
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
327 return ``true`` if they modified the program, or ``false`` if they did not.
329 The ``doInitialization(Region *, RGPassManager &)`` method
330 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
334 virtual bool doInitialization(Region *, RGPassManager &RGM);
336 The ``doInitialization`` method is designed to do simple initialization type of
337 stuff that does not depend on the functions being processed. The
338 ``doInitialization`` method call is not scheduled to overlap with any other
339 pass executions (thus it should be very fast). ``RPPassManager`` interface
340 should be used to access ``Function`` or ``Module`` level analysis information.
342 .. _writing-an-llvm-pass-runOnRegion:
344 The ``runOnRegion`` method
345 ^^^^^^^^^^^^^^^^^^^^^^^^^^
349 virtual bool runOnRegion(Region *, RGPassManager &RGM) = 0;
351 The ``runOnRegion`` method must be implemented by your subclass to do the
352 transformation or analysis work of your pass. As usual, a true value should be
353 returned if the region is modified. ``RGPassManager`` interface should be used to
356 The ``doFinalization()`` method
357 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
361 virtual bool doFinalization();
363 The ``doFinalization`` method is an infrequently used method that is called
364 when the pass framework has finished calling :ref:`runOnRegion
365 <writing-an-llvm-pass-runOnRegion>` for every region in the program being
369 The ``MachineFunctionPass`` class
370 ---------------------------------
372 A ``MachineFunctionPass`` is a part of the LLVM code generator that executes on
373 the machine-dependent representation of each LLVM function in the program.
375 Code generator passes are registered and initialized specially by
376 ``TargetMachine::addPassesToEmitFile`` and similar routines, so they cannot
377 generally be run from the :program:`opt` or :program:`bugpoint` commands.
379 A ``MachineFunctionPass`` is also a ``FunctionPass``, so all the restrictions
380 that apply to a ``FunctionPass`` also apply to it. ``MachineFunctionPass``\ es
381 also have additional restrictions. In particular, ``MachineFunctionPass``\ es
382 are not allowed to do any of the following:
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).
391 .. _writing-an-llvm-pass-runOnMachineFunction:
393 The ``runOnMachineFunction(MachineFunction &MF)`` method
394 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
398 virtual bool runOnMachineFunction(MachineFunction &MF) = 0;
400 ``runOnMachineFunction`` can be considered the main entry point of a
401 ``MachineFunctionPass``; that is, you should override this method to do the
402 work of your ``MachineFunctionPass``.
404 The ``runOnMachineFunction`` method is called on every ``MachineFunction`` in a
405 ``Module``, so that the ``MachineFunctionPass`` may perform optimizations on
406 the machine-dependent representation of the function. If you want to get at
407 the LLVM ``Function`` for the ``MachineFunction`` you're working on, use
408 ``MachineFunction``'s ``getFunction()`` accessor method --- but remember, you
409 may not modify the LLVM ``Function`` or its contents from a
410 ``MachineFunctionPass``.
412 .. _writing-an-llvm-pass-registration:
417 Passes are registered with the ``RegisterPass`` template. The template
418 parameter is the name of the pass that is to be used on the command line to
419 specify that the pass should be added to a program. The first argument is the
420 name of the pass, which is to be used for the :option:`-help` output of
421 programs, as well as for debug output generated by the `--debug-pass` option.
423 If you want your pass to be easily dumpable, you should implement the virtual
431 virtual void print(llvm::raw_ostream &O, const Module *M) const;
433 The ``print`` method must be implemented by "analyses" in order to print a
434 human readable version of the analysis results. This is useful for debugging
435 an analysis itself, as well as for other people to figure out how an analysis
436 works. Use the opt ``-analyze`` argument to invoke this method.
438 The ``llvm::raw_ostream`` parameter specifies the stream to write the results
439 on, and the ``Module`` parameter gives a pointer to the top level module of the
440 program that has been analyzed. Note however that this pointer may be ``NULL``
441 in certain circumstances (such as calling the ``Pass::dump()`` from a
442 debugger), so it should only be used to enhance debug output, it should not be
445 .. _writing-an-llvm-pass-interaction:
447 Specifying interactions between passes
448 --------------------------------------
450 One of the main responsibilities of the ``PassManager`` is to make sure that
451 passes interact with each other correctly. Because ``PassManager`` tries to
452 :ref:`optimize the execution of passes <writing-an-llvm-pass-passmanager>` it
453 must know how the passes interact with each other and what dependencies exist
454 between the various passes. To track this, each pass can declare the set of
455 passes that are required to be executed before the current pass, and the passes
456 which are invalidated by the current pass.
458 Typically this functionality is used to require that analysis results are
459 computed before your pass is run. Running arbitrary transformation passes can
460 invalidate the computed analysis results, which is what the invalidation set
461 specifies. If a pass does not implement the :ref:`getAnalysisUsage
462 <writing-an-llvm-pass-getAnalysisUsage>` method, it defaults to not having any
463 prerequisite passes, and invalidating **all** other passes.
465 .. _writing-an-llvm-pass-getAnalysisUsage:
467 The ``getAnalysisUsage`` method
468 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
472 virtual void getAnalysisUsage(AnalysisUsage &Info) const;
474 By implementing the ``getAnalysisUsage`` method, the required and invalidated
475 sets may be specified for your transformation. The implementation should fill
476 in the `AnalysisUsage
477 <https://llvm.org/doxygen/classllvm_1_1AnalysisUsage.html>`_ object with
478 information about which passes are required and not invalidated. To do this, a
479 pass may call any of the following methods on the ``AnalysisUsage`` object:
481 The ``AnalysisUsage::addRequired<>`` and ``AnalysisUsage::addRequiredTransitive<>`` methods
482 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
484 If your pass requires a previous pass to be executed (an analysis for example),
485 it can use one of these methods to arrange for it to be run before your pass.
486 LLVM has many different types of analyses and passes that can be required,
487 spanning the range from ``DominatorSet`` to ``BreakCriticalEdges``. Requiring
488 ``BreakCriticalEdges``, for example, guarantees that there will be no critical
489 edges in the CFG when your pass has been run.
491 Some 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
494 analyses chain, the ``addRequiredTransitive`` method should be used instead of
495 the ``addRequired`` method. This informs the ``PassManager`` that the
496 transitively required pass should be alive as long as the requiring pass is.
498 The ``AnalysisUsage::addPreserved<>`` method
499 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
501 One of the jobs of the ``PassManager`` is to optimize how and when analyses are
502 run. In particular, it attempts to avoid recomputing data unless it needs to.
503 For this reason, passes are allowed to declare that they preserve (i.e., they
504 don't invalidate) an existing analysis if it's available. For example, a
505 simple constant folding pass would not modify the CFG, so it can't possibly
506 affect the results of dominator analysis. By default, all passes are assumed
507 to invalidate all others.
509 The ``AnalysisUsage`` class provides several methods which are useful in
510 certain circumstances that are related to ``addPreserved``. In particular, the
511 ``setPreservesAll`` method can be called to indicate that the pass does not
512 modify the LLVM program at all (which is true for analyses), and the
513 ``setPreservesCFG`` method can be used by transformations that change
514 instructions in the program but do not modify the CFG or terminator
517 ``addPreserved`` is particularly useful for transformations like
518 ``BreakCriticalEdges``. This pass knows how to update a small set of loop and
519 dominator related analyses if they exist, so it can preserve them, despite the
520 fact that it hacks on the CFG.
522 Example implementations of ``getAnalysisUsage``
523 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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>();
533 .. _writing-an-llvm-pass-getAnalysis:
535 The ``getAnalysis<>`` and ``getAnalysisIfAvailable<>`` methods
536 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
538 The ``Pass::getAnalysis<>`` method is automatically inherited by your class,
539 providing you with access to the passes that you declared that you required
540 with the :ref:`getAnalysisUsage <writing-an-llvm-pass-getAnalysisUsage>`
541 method. It takes a single template argument that specifies which pass class
542 you want, and returns a reference to that pass. For example:
546 bool LICM::runOnFunction(Function &F) {
547 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
551 This method call returns a reference to the pass desired. You may get a
552 runtime assertion failure if you attempt to get an analysis that you did not
553 declare as required in your :ref:`getAnalysisUsage
554 <writing-an-llvm-pass-getAnalysisUsage>` implementation. This method can be
555 called by your ``run*`` method implementation, or by any other local method
556 invoked by your ``run*`` method.
558 A module level pass can use function level analysis info using this interface.
563 bool ModuleLevelPass::runOnModule(Module &M) {
565 DominatorTree &DT = getAnalysis<DominatorTree>(Func);
569 In above example, ``runOnFunction`` for ``DominatorTree`` is called by pass
570 manager before returning a reference to the desired pass.
572 If 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
575 it is active. For example:
579 if (DominatorSet *DS = getAnalysisIfAvailable<DominatorSet>()) {
580 // A DominatorSet is active. This code will update it.
586 The `Statistic <https://llvm.org/doxygen/Statistic_8h_source.html>`_ class is
587 designed to be an easy way to expose various success metrics from passes.
588 These statistics are printed at the end of a run, when the :option:`-stats`
589 command line option is enabled on the command line. See the :ref:`Statistics
590 section <Statistic>` in the Programmer's Manual for details.
592 .. _writing-an-llvm-pass-passmanager:
594 What PassManager does
595 ---------------------
597 The `PassManager <https://llvm.org/doxygen/PassManager_8h_source.html>`_ `class
598 <https://llvm.org/doxygen/classllvm_1_1PassManager.html>`_ takes a list of
599 passes, ensures their :ref:`prerequisites <writing-an-llvm-pass-interaction>`
600 are set up correctly, and then schedules passes to run efficiently. All of the
601 LLVM tools that run passes use the PassManager for execution of these passes.
603 The PassManager does two main things to try to reduce the execution time of a
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.
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.
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.
631 The effectiveness of the ``PassManager`` is influenced directly by how much
632 information it has about the behaviors of the passes it is scheduling. For
633 example, the "preserved" set is intentionally conservative in the face of an
634 unimplemented :ref:`getAnalysisUsage <writing-an-llvm-pass-getAnalysisUsage>`
635 method. Not implementing when it should be implemented will have the effect of
636 not allowing any analysis results to live across the execution of your pass.
638 The ``PassManager`` class exposes a ``--debug-pass`` command line options that
639 is useful for debugging pass execution, seeing how things work, and diagnosing
640 when you should be preserving more analyses than you currently are. (To get
641 information about all of the variants of the ``--debug-pass`` option, just type
642 "``llc -help-hidden``").
644 By using the --debug-pass=Structure option, for example, we can see inspect the
645 default optimization pipelines, e.g. (the output has been trimmed):
647 .. code-block:: console
649 $ llc -mtriple=arm64-- -O3 -debug-pass=Structure file.ll > /dev/null
652 Pre-ISel Intrinsic Lowering
655 Expand large fp convert
656 Expand Atomic instructions
657 SVE intrinsics optimizations
659 Dominator Tree Construction
662 Dominator Tree Construction
663 Natural Loop Information
664 Canonicalize natural loops
667 .. _writing-an-llvm-pass-releaseMemory:
669 The ``releaseMemory`` method
670 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
674 virtual void releaseMemory();
676 The ``PassManager`` automatically determines when to compute analysis results,
677 and how long to keep them around for. Because the lifetime of the pass object
678 itself is effectively the entire duration of the compilation process, we need
679 some way to free analysis results when they are no longer useful. The
680 ``releaseMemory`` virtual method is the way to do this.
682 If you are writing an analysis or any other pass that retains a significant
683 amount of state (for use by another pass which "requires" your pass and uses
684 the :ref:`getAnalysis <writing-an-llvm-pass-getAnalysis>` method) you should
685 implement ``releaseMemory`` to, well, release the memory allocated to maintain
686 this internal state. This method is called after the ``run*`` method for the
687 class, before the next call of ``run*`` in your pass.
689 Registering dynamically loaded passes
690 =====================================
692 *Size matters* when constructing production quality tools using LLVM, both for
693 the purposes of distribution, and for regulating the resident code size when
694 running on the target system. Therefore, it becomes desirable to selectively
695 use some passes, while omitting others and maintain the flexibility to change
696 configurations later on. You want to be able to do all this, and, provide
697 feedback to the user. This is where pass registration comes into play.
699 The fundamental mechanisms for pass registration are the
700 ``MachinePassRegistry`` class and subclasses of ``MachinePassRegistryNode``.
702 An instance of ``MachinePassRegistry`` is used to maintain a list of
703 ``MachinePassRegistryNode`` objects. This instance maintains the list and
704 communicates additions and deletions to the command line interface.
706 An instance of ``MachinePassRegistryNode`` subclass is used to maintain
707 information provided about a particular pass. This information includes the
708 command line name, the command help string and the address of the function used
709 to create an instance of the pass. A global static constructor of one of these
710 instances *registers* with a corresponding ``MachinePassRegistry``, the static
711 destructor *unregisters*. Thus a pass that is statically linked in the tool
712 will be registered at start up. A dynamically loaded pass will register on
713 load and unregister at unload.
715 Using existing registries
716 -------------------------
718 There are predefined registries to track instruction scheduling
719 (``RegisterScheduler``) and register allocation (``RegisterRegAlloc``) machine
720 passes. Here we will describe how to *register* a register allocator machine
723 Implement your register allocator machine pass. In your register allocator
724 ``.cpp`` file add the following include:
728 #include "llvm/CodeGen/RegAllocRegistry.h"
730 Also in your register allocator ``.cpp`` file, define a creator function in the
735 FunctionPass *createMyRegisterAllocator() {
736 return new MyRegisterAllocator();
739 Note that the signature of this function should match the type of
740 ``RegisterRegAlloc::FunctionPassCtor``. In the same file add the "installing"
741 declaration, in the form:
745 static RegisterRegAlloc myRegAlloc("myregalloc",
746 "my register allocator help string",
747 createMyRegisterAllocator);
749 Note the two spaces prior to the help string produces a tidy result on the
750 :option:`-help` query.
752 .. code-block:: console
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
763 And that's it. The user is now free to use ``-regalloc=myregalloc`` as an
764 option. Registering instruction schedulers is similar except use the
765 ``RegisterScheduler`` class. Note that the
766 ``RegisterScheduler::FunctionPassCtor`` is significantly different from
767 ``RegisterRegAlloc::FunctionPassCtor``.
769 To force the load/linking of your register allocator into the
770 :program:`llc`/:program:`lli` tools, add your creator function's global
771 declaration to ``Passes.h`` and add a "pseudo" call line to
772 ``llvm/Codegen/LinkAllCodegenComponents.h``.
774 Creating new registries
775 -----------------------
777 The easiest way to get started is to clone one of the existing registries; we
778 recommend ``llvm/CodeGen/RegAllocRegistry.h``. The key things to modify are
779 the class name and the ``FunctionPassCtor`` type.
781 Then you need to declare the registry. Example: if your pass registry is
782 ``RegisterMyPasses`` then define:
786 MachinePassRegistry<RegisterMyPasses::FunctionPassCtor> RegisterMyPasses::Registry;
788 And finally, declare the command line option for your passes. Example:
792 cl::opt<RegisterMyPasses::FunctionPassCtor, false,
793 RegisterPassParser<RegisterMyPasses> >
795 cl::init(&createDefaultMyPass),
796 cl::desc("my pass option help"));
798 Here the command option is "``mypass``", with ``createDefaultMyPass`` as the
801 Using GDB with dynamically loaded passes
802 ----------------------------------------
804 Unfortunately, using GDB with dynamically loaded passes is not as easy as it
805 should be. First of all, you can't set a breakpoint in a shared object that
806 has not been loaded yet, and second of all there are problems with inlined
807 functions in shared objects. Here are some suggestions to debugging your pass
810 For sake of discussion, I'm going to assume that you are debugging a
811 transformation invoked by :program:`opt`, although nothing described here
814 Setting a breakpoint in your pass
815 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
817 First thing you do is start gdb on the opt process:
819 .. code-block:: console
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"...
831 Note that :program:`opt` has a lot of debugging information in it, so it takes
832 time 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,
834 and have it stop before it invokes our pass, but after it has loaded the shared
835 object. 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:
838 .. code-block:: console
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); }
848 Once the :program:`opt` stops in the ``PassManager::run`` method you are now
849 free to set breakpoints in your pass so that you can trace through execution or
850 do other standard debugging stuff.
852 Miscellaneous Problems
853 ^^^^^^^^^^^^^^^^^^^^^^
855 Once you have the basics down, there are a couple of problems that GDB has,
856 some with solutions, some without.
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).
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``.
872 Hopefully these tips will help with common case debugging situations. If you'd
873 like to contribute some tips of your own, just contact `Chris
874 <mailto:sabre@nondot.org>`_.