[PM] Start sketching out the new module and function pass manager.
[oota-llvm.git] / include / llvm / IR / PassManager.h
1 //===- PassManager.h - LegacyContainer for Passes --------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 ///
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.
20 ///
21 /// The core IR library provides managers for running passes over
22 /// modules and functions.
23 ///
24 /// * FunctionPassManager can run over a Module, runs each pass over
25 ///   a Function.
26 /// * ModulePassManager must be directly run, runs each pass over the Module.
27 ///
28 //===----------------------------------------------------------------------===//
29
30 #include "llvm/ADT/polymorphic_ptr.h"
31 #include "llvm/IR/Module.h"
32 #include <vector>
33
34 namespace llvm {
35
36 class Module;
37 class Function;
38
39 /// \brief Implementation details of the pass manager interfaces.
40 namespace detail {
41
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;
48
49   /// \brief The polymorphic API which runs the pass over a given IR entity.
50   virtual bool run(T Arg) = 0;
51 };
52
53 /// \brief A template wrapper used to implement the polymorphic API.
54 ///
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
57 /// object.
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); }
62   PassT Pass;
63 };
64
65 }
66
67 class ModulePassManager {
68 public:
69   ModulePassManager(Module *M) : M(M) {}
70
71   template <typename ModulePassT> void addPass(ModulePassT Pass) {
72     Passes.push_back(new ModulePassModel<ModulePassT>(llvm_move(Pass)));
73   }
74
75   void run() {
76     for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx)
77       Passes[Idx]->run(M);
78   }
79
80 private:
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) {}
86   };
87
88   Module *M;
89   std::vector<polymorphic_ptr<ModulePassConcept> > Passes;
90 };
91
92 class FunctionPassManager {
93 public:
94   template <typename FunctionPassT> void addPass(FunctionPassT Pass) {
95     Passes.push_back(new FunctionPassModel<FunctionPassT>(llvm_move(Pass)));
96   }
97
98   bool run(Module *M) {
99     bool Changed = false;
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);
103     return Changed;
104   }
105
106 private:
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) {}
113   };
114
115   std::vector<polymorphic_ptr<FunctionPassConcept> > Passes;
116 };
117
118 }