1 //===- PassManager.h - LegacyContainer for Passes --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This header defines various interfaces for pass management in LLVM. There
12 /// is no "pass" interface in LLVM per se. Instead, an instance of any class
13 /// which supports a method to 'run' it over a unit of IR can be used as
14 /// a pass. A pass manager is generally a tool to collect a sequence of passes
15 /// which run over a particular IR construct, and run each of them in sequence
16 /// over each such construct in the containing IR construct. As there is no
17 /// containing IR construct for a Module, a manager for passes over modules
18 /// forms the base case which runs its managed passes in sequence over the
19 /// single module provided.
21 /// The core IR library provides managers for running passes over
22 /// modules and functions.
24 /// * FunctionPassManager can run over a Module, runs each pass over
26 /// * ModulePassManager must be directly run, runs each pass over the Module.
28 //===----------------------------------------------------------------------===//
30 #include "llvm/ADT/polymorphic_ptr.h"
31 #include "llvm/IR/Module.h"
39 /// \brief Implementation details of the pass manager interfaces.
42 /// \brief Template for the abstract base class used to dispatch
43 /// polymorphically over pass objects.
44 template <typename T> struct PassConcept {
45 // Boiler plate necessary for the container of derived classes.
46 virtual ~PassConcept() {}
47 virtual PassConcept *clone() = 0;
49 /// \brief The polymorphic API which runs the pass over a given IR entity.
50 virtual bool run(T Arg) = 0;
53 /// \brief A template wrapper used to implement the polymorphic API.
55 /// Can be instantiated for any object which provides a \c run method
56 /// accepting a \c T. It requires the pass to be a copyable
58 template <typename T, typename PassT> struct PassModel : PassConcept<T> {
59 PassModel(PassT Pass) : Pass(llvm_move(Pass)) {}
60 virtual PassModel *clone() { return new PassModel(Pass); }
61 virtual bool run(T Arg) { return Pass.run(Arg); }
67 class ModulePassManager {
69 ModulePassManager(Module *M) : M(M) {}
71 template <typename ModulePassT> void addPass(ModulePassT Pass) {
72 Passes.push_back(new ModulePassModel<ModulePassT>(llvm_move(Pass)));
76 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx)
81 // Pull in the concept type and model template specialized for modules.
82 typedef detail::PassConcept<Module *> ModulePassConcept;
83 template <typename PassT>
84 struct ModulePassModel : detail::PassModel<Module *, PassT> {
85 ModulePassModel(PassT Pass) : detail::PassModel<Module *, PassT>(Pass) {}
89 std::vector<polymorphic_ptr<ModulePassConcept> > Passes;
92 class FunctionPassManager {
94 template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
95 Passes.push_back(new FunctionPassModel<FunctionPassT>(llvm_move(Pass)));
100 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
101 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx)
102 Changed |= Passes[Idx]->run(I);
107 // Pull in the concept type and model template specialized for functions.
108 typedef detail::PassConcept<Function *> FunctionPassConcept;
109 template <typename PassT>
110 struct FunctionPassModel : detail::PassModel<Function *, PassT> {
111 FunctionPassModel(PassT Pass)
112 : detail::PassModel<Function *, PassT>(Pass) {}
115 std::vector<polymorphic_ptr<FunctionPassConcept> > Passes;