148486893f46d2e12e926682a3ecb908716bc66c4Chris Lattner//===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
29769ab22265b313171d201b5928688524a01bd87Misha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
79769ab22265b313171d201b5928688524a01bd87Misha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
91bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner//
10986fced5f9b3ef06f514cba66db499acffdc9711Chris Lattner// This file defines a base class that indicates that a specified class is a
111bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner// transformation pass implementation.
121bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner//
13fb215b936df0034f9030e921c0c054ab11470762Misha Brukman// Passes are designed this way so that it is possible to run passes in a cache
14aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner// and organizationally optimal order without having to specify it at the front
15aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner// end.  This allows arbitrary passes to be strung together and have them
167a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner// executed as efficiently as possible.
171bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner//
18aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner// Passes should extend one of the classes below, depending on the guarantees
19aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner// that it can make about what will be modified as it is run.  For example, most
20f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner// global optimizations should derive from FunctionPass, because they do not add
21d30efaf56ed1e374240b4c2fe2ea7054cbd7cb52Chris Lattner// or delete functions, they operate on the internals of the function.
221bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner//
234c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner// Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the
244c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner// bottom), so the APIs exposed by these files are also automatically available
254c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner// to all users of this file.
264c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner//
271bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner//===----------------------------------------------------------------------===//
281bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner
29986fced5f9b3ef06f514cba66db499acffdc9711Chris Lattner#ifndef LLVM_PASS_H
30986fced5f9b3ef06f514cba66db499acffdc9711Chris Lattner#define LLVM_PASS_H
311bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner
325c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene#include <string>
33d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
34d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
35d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
361fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerclass BasicBlock;
37e7506a366e8bd56c97d10beb68e4db953aebaecaChris Lattnerclass Function;
3805ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattnerclass Module;
39f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattnerclass AnalysisUsage;
404c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattnerclass PassInfo;
4170b4f3e05106761e99cc56919767155cad0e2d3aChris Lattnerclass ImmutablePass;
4209e6e4303f1aac10fea6860e7736c234fcbf56ccDevang Patelclass PMStack;
43cde53d3c1e9d6a2add5de847b44818fbb1d69c20Devang Patelclass AnalysisResolver;
4497149737f27457b0411e49af3e4539688e29848fDevang Patelclass PMDataManager;
4545cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattnerclass raw_ostream;
468a261e44f71a433b7d9af373c3e3dfa6fea67146Dan Gohmanclass StringRef;
47aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner
48a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner// AnalysisID - Use the PassInfo to identify a pass...
4990c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Andersontypedef const void* AnalysisID;
50a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner
51be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel/// Different types of internal pass managers. External pass managers
52be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel/// (PassManager and FunctionPassManager) are not represented here.
53be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel/// Ordering of pass manager types is important here.
54be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patelenum PassManagerType {
55be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel  PMT_Unknown = 0,
568247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick  PMT_ModulePassManager = 1, ///< MPPassManager
5742cefa1ea34bfe01b3073d27a1e78f1c1d75cedcChris Lattner  PMT_CallGraphPassManager,  ///< CGPassManager
5842cefa1ea34bfe01b3073d27a1e78f1c1d75cedcChris Lattner  PMT_FunctionPassManager,   ///< FPPassManager
5942cefa1ea34bfe01b3073d27a1e78f1c1d75cedcChris Lattner  PMT_LoopPassManager,       ///< LPPassManager
6065513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser  PMT_RegionPassManager,     ///< RGPassManager
6142cefa1ea34bfe01b3073d27a1e78f1c1d75cedcChris Lattner  PMT_BasicBlockPassManager, ///< BBPassManager
62fe613905b371ef7160968cdaabf6074cfa10f4a0Devang Patel  PMT_Last
63be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel};
64be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel
65476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattner// Different types of passes.
66476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattnerenum PassKind {
67476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattner  PT_BasicBlock,
6865513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser  PT_Region,
69476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattner  PT_Loop,
70476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattner  PT_Function,
71476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattner  PT_CallGraphSCC,
72476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattner  PT_Module,
73476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattner  PT_PassManager
74476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattner};
7565513605353c7e3ee8be6fc92892f257ad399d92Tobias Grosser
761bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner//===----------------------------------------------------------------------===//
7726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// Pass interface - Implemented by all 'passes'.  Subclass this if you are an
7826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// interprocedural optimization or you do not fit into any of the more
7926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// constrained passes described below.
8026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///
8105ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattnerclass Pass {
82cde53d3c1e9d6a2add5de847b44818fbb1d69c20Devang Patel  AnalysisResolver *Resolver;  // Used to resolve analysis
8390c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  const void *PassID;
84476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattner  PassKind Kind;
85f9b2297380d57d35c707864a9d55c1e00da39700Chris Lattner  void operator=(const Pass&);  // DO NOT IMPLEMENT
86f9b2297380d57d35c707864a9d55c1e00da39700Chris Lattner  Pass(const Pass &);           // DO NOT IMPLEMENT
878247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick
8805ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattnerpublic:
898a42633273e01ed867c69edd3039a3b122ee79b2Benjamin Kramer  explicit Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { }
906e21ff0b0a8e4f0878431afa5628bb1c2db0b8e1Devang Patel  virtual ~Pass();
9105ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner
928247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick
93476e9bd1146624fa17243ae55fdb156f905ba3d4Chris Lattner  PassKind getPassKind() const { return Kind; }
948247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick
9526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// getPassName - Return a nice clean name for a pass.  This usually
9626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// implemented in terms of the name that is registered by one of the
97b973d5f9b59f75854493bc37a9eb0f3032be0864Dan Gohman  /// Registration templates, but can be overloaded directly.
9826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
9996c466b06ab0c830b07329c1b16037f585ccbe40Chris Lattner  virtual const char *getPassName() const;
100aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner
10190c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  /// getPassID - Return the PassID number that corresponds to this pass.
1028a42633273e01ed867c69edd3039a3b122ee79b2Benjamin Kramer  AnalysisID getPassID() const {
10390c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson    return PassID;
10490c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  }
1054c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner
10626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// print - Print out the internal state of the pass.  This is called by
10726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// Analyze to print out the contents of an analysis.  Otherwise it is not
1085560c9d49ccae132cabf1155f18aa0480dce3edaMisha Brukman  /// necessary to implement this method.  Beware that the module pointer MAY be
10926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// null.  This automatically forwards to a virtual function that does not
11026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// provide the Module* in case the analysis doesn't need it it can just be
11126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// ignored.
11226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
11345cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattner  virtual void print(raw_ostream &O, const Module *M) const;
114275872e79950dafc6699f6502cee52f74b84a22aDaniel Dunbar  void dump() const; // dump - Print to stderr.
115a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner
1165c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene  /// createPrinterPass - Get a Pass appropriate to print the IR this
117eb69732d56f7e581bf648df9bf1352d513913df6Jay Foad  /// pass operates on (Module, Function or MachineFunction).
1185c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene  virtual Pass *createPrinterPass(raw_ostream &O,
1195c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene                                  const std::string &Banner) const = 0;
1205c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene
12122a1cf9d3a5c829d260bcf44ffe6b34ecf16076cDevang Patel  /// Each pass is responsible for assigning a pass manager to itself.
1228247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick  /// PMS is the stack of available pass manager.
1238247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick  virtual void assignPassManager(PMStack &,
1248d570bf8e9eb41e2f1c283bd67cc58514b0c429aDan Gohman                                 PassManagerType) {}
12522a1cf9d3a5c829d260bcf44ffe6b34ecf16076cDevang Patel  /// Check if available pass managers are suitable for this pass or not.
126eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual void preparePassManager(PMStack &);
1278247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick
1288f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel  ///  Return what kind of Pass Manager can manage this pass.
129eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual PassManagerType getPotentialPassManagerType() const;
13022a1cf9d3a5c829d260bcf44ffe6b34ecf16076cDevang Patel
131cde53d3c1e9d6a2add5de847b44818fbb1d69c20Devang Patel  // Access AnalysisResolver
132e407c1d1583cfc788fa7d00fee5f612c3f224983Dan Gohman  void setResolver(AnalysisResolver *AR);
133e407c1d1583cfc788fa7d00fee5f612c3f224983Dan Gohman  AnalysisResolver *getResolver() const { return Resolver; }
134a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner
13526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// getAnalysisUsage - This function should be overriden by passes that need
13626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// analysis information to do their job.  If a pass specifies that it uses a
13726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// particular analysis result to this function, it can then use the
13826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// getAnalysis<AnalysisType>() function, below.
13926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
140eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual void getAnalysisUsage(AnalysisUsage &) const;
14105ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner
14226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// releaseMemory() - This member can be implemented by a pass if it wants to
14326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// be able to release its memory when it is no longer needed.  The default
14426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// behavior of passes is to hold onto memory for the entire duration of their
14526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// lifetime (which is the entire compile time).  For pipelined passes, this
14626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// is not a big deal because that memory gets recycled every time the pass is
14726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// invoked on another program unit.  For IP passes, it is more important to
14826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// free memory when it is unused.
14926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
15026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// Optionally implement this function to release pass memory when it is no
15126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// longer used.
15226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
153eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual void releaseMemory();
154ce885e9f9d03fa1d9dd5e2abba5e95953473288fChris Lattner
1552033097b1f5b1889a5d801febbc7848c7b8ca439Chris Lattner  /// getAdjustedAnalysisPointer - This method is used when a pass implements
1562033097b1f5b1889a5d801febbc7848c7b8ca439Chris Lattner  /// an analysis interface through multiple inheritance.  If needed, it should
1572033097b1f5b1889a5d801febbc7848c7b8ca439Chris Lattner  /// override this to adjust the this pointer as needed for the specified pass
1582033097b1f5b1889a5d801febbc7848c7b8ca439Chris Lattner  /// info.
15990c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
160e407c1d1583cfc788fa7d00fee5f612c3f224983Dan Gohman  virtual ImmutablePass *getAsImmutablePass();
161e407c1d1583cfc788fa7d00fee5f612c3f224983Dan Gohman  virtual PMDataManager *getAsPMDataManager();
1628247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick
1639750b5d5779e6efec10a93633dd1d36c5f61dbc3Devang Patel  /// verifyAnalysis() - This member can be implemented by a analysis pass to
1648247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick  /// check state of analysis information.
165eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual void verifyAnalysis() const;
1669750b5d5779e6efec10a93633dd1d36c5f61dbc3Devang Patel
16705ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner  // dumpPassStructure - Implement the -debug-passes=PassStructure option
1688a757aeac436ecd27e28a39b10032fd6fda33780Dan Gohman  virtual void dumpPassStructure(unsigned Offset = 0);
16905ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner
170fb30fda1de96251017ae092f469de4b290b6b61bChris Lattner  // lookupPassInfo - Return the pass info object for the specified pass class,
171fb30fda1de96251017ae092f469de4b290b6b61bChris Lattner  // or null if it is not known.
17290c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  static const PassInfo *lookupPassInfo(const void *TI);
173fb30fda1de96251017ae092f469de4b290b6b61bChris Lattner
1748a261e44f71a433b7d9af373c3e3dfa6fea67146Dan Gohman  // lookupPassInfo - Return the pass info object for the pass with the given
1758a261e44f71a433b7d9af373c3e3dfa6fea67146Dan Gohman  // argument string, or null if it is not known.
1768be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  static const PassInfo *lookupPassInfo(StringRef Arg);
1778a261e44f71a433b7d9af373c3e3dfa6fea67146Dan Gohman
178ebe18ef5c286bb7c33f6c43f1963a7d22cd73f40Andrew Trick  // createPass - Create a object for the specified pass class,
179ebe18ef5c286bb7c33f6c43f1963a7d22cd73f40Andrew Trick  // or null if it is not known.
1805e108eeeef34dd2afa00d1da77bca47188de4244Andrew Trick  static Pass *createPass(AnalysisID ID);
181ebe18ef5c286bb7c33f6c43f1963a7d22cd73f40Andrew Trick
1821465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
1831465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  /// get analysis information that might be around, for example to update it.
1841465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  /// This is different than getAnalysis in that it can fail (if the analysis
1851465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  /// results haven't been computed), so should only be used if you can handle
1861465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  /// the case when the analysis is not available.  This method is often used by
1871465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  /// transformation APIs to update analysis results for a pass automatically as
1881465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  /// the transform is performed.
189f9bbe214fdd7391d2798c6e2d3d281f02f6dcc43Chris Lattner  ///
1901465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  template<typename AnalysisType> AnalysisType *
1911465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands    getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
192f9bbe214fdd7391d2798c6e2d3d281f02f6dcc43Chris Lattner
193f1ab454b71efae1a5cee7f9285d91b75ead6a8a7Chris Lattner  /// mustPreserveAnalysisID - This method serves the same function as
1941465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  /// getAnalysisIfAvailable, but works if you just have an AnalysisID.  This
195f1ab454b71efae1a5cee7f9285d91b75ead6a8a7Chris Lattner  /// obviously cannot give you a properly typed instance of the class if you
1961465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  /// don't have the class name available (use getAnalysisIfAvailable if you
1971465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands  /// do), but it can tell you if you need to preserve the pass at least.
198f1ab454b71efae1a5cee7f9285d91b75ead6a8a7Chris Lattner  ///
19990c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  bool mustPreserveAnalysisID(char &AID) const;
200f1ab454b71efae1a5cee7f9285d91b75ead6a8a7Chris Lattner
20126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
20226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// to the analysis information that they claim to use by overriding the
20326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// getAnalysisUsage function.
20426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
20505ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner  template<typename AnalysisType>
2063f2577decba715615597352c4b3e2a551d44b64cDevang Patel  AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
20776a8f7fb5a325ab635f55f811803eb3ca034f8faChris Lattner
20876a8f7fb5a325ab635f55f811803eb3ca034f8faChris Lattner  template<typename AnalysisType>
2097ab42aec4266ef1b474ae03f3cb94ce509aa94a7Dan Gohman  AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
2106b1df0e863963929634d769f39cbce68f030f051Devang Patel
2116b1df0e863963929634d769f39cbce68f030f051Devang Patel  template<typename AnalysisType>
21290c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  AnalysisType &getAnalysisID(AnalysisID PI) const;
2138f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel
2146b1df0e863963929634d769f39cbce68f030f051Devang Patel  template<typename AnalysisType>
21590c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
216aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner};
217aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner
218aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner
219b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner//===----------------------------------------------------------------------===//
220b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner/// ModulePass class - This class is used to implement unstructured
221fb215b936df0034f9030e921c0c054ab11470762Misha Brukman/// interprocedural optimizations and analyses.  ModulePasses may do anything
222b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner/// they want to the program.
223b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner///
2241fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerclass ModulePass : public Pass {
2251fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerpublic:
2265c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene  /// createPrinterPass - Get a module printer pass.
2275c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
2285c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene
229b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner  /// runOnModule - Virtual method overriden by subclasses to process the module
230b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner  /// being operated on.
231b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner  virtual bool runOnModule(Module &M) = 0;
232b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner
2338247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick  virtual void assignPassManager(PMStack &PMS,
2348d570bf8e9eb41e2f1c283bd67cc58514b0c429aDan Gohman                                 PassManagerType T);
2358f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel
2368f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel  ///  Return what kind of Pass Manager can manage this pass.
237eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual PassManagerType getPotentialPassManagerType() const;
2388f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel
23990c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
240c7d0f4be88cc7fa33d51736e0f53d3e00e69efb4Devang Patel  // Force out-of-line virtual method.
241c7d0f4be88cc7fa33d51736e0f53d3e00e69efb4Devang Patel  virtual ~ModulePass();
242b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner};
24370b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner
24470b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner
24570b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner//===----------------------------------------------------------------------===//
24670b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner/// ImmutablePass class - This class is used to provide information that does
24770b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner/// not need to be run.  This is useful for things like target information and
24870b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner/// "basic" versions of AnalysisGroups.
24970b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner///
2501fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerclass ImmutablePass : public ModulePass {
2511fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerpublic:
252d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner  /// initializePass - This method may be overriden by immutable passes to allow
253d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner  /// them to perform various initialization actions they require.  This is
254d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner  /// primarily because an ImmutablePass can "require" another ImmutablePass,
255d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner  /// and if it does, the overloaded version of initializePass may get access to
256d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner  /// these passes with getAnalysis<>.
257d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner  ///
258eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual void initializePass();
25970b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner
2605e664b8f7c5ba877058a3a1cdfcce8e6b7388a6aChris Lattner  virtual ImmutablePass *getAsImmutablePass() { return this; }
2615e664b8f7c5ba877058a3a1cdfcce8e6b7388a6aChris Lattner
262d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner  /// ImmutablePasses are never run.
263d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner  ///
26413d57320bd212483463d4f8992d5787b29eda5dfBill Wendling  bool runOnModule(Module &) { return false; }
26570b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner
2668247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick  explicit ImmutablePass(char &pid)
267c758209153ca0f6da6737f25ada269c573fba456Devang Patel  : ModulePass(pid) {}
2688247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick
269c7d0f4be88cc7fa33d51736e0f53d3e00e69efb4Devang Patel  // Force out-of-line virtual method.
270c7d0f4be88cc7fa33d51736e0f53d3e00e69efb4Devang Patel  virtual ~ImmutablePass();
27170b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner};
27270b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner
273aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner//===----------------------------------------------------------------------===//
27426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// FunctionPass class - This class is used to implement most global
27526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// optimizations.  Optimizations should subclass this class if they meet the
27626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// following constraints:
27726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///
2781d06fbe8935f479c0438798f86237d6264ce60d3Misha Brukman///  1. Optimizations are organized globally, i.e., a function at a time
27926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///  2. Optimizing a function does not cause the addition or removal of any
28026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///     functions in the module
28126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///
282a65f5fd21b946c33876d87105974ebcce5d37d0aDevang Patelclass FunctionPass : public Pass {
2831fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerpublic:
28490c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
285eae540a037d17d218767f21b6d3b45f395df4619Devang Patel
2865c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene  /// createPrinterPass - Get a function printer pass.
2875c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
2885c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene
28926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// doInitialization - Virtual method overridden by subclasses to do
2905560c9d49ccae132cabf1155f18aa0480dce3edaMisha Brukman  /// any necessary per-module initialization.
29126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
292eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual bool doInitialization(Module &);
2938247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick
29426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// runOnFunction - Virtual method overriden by subclasses to do the
29526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// per-function processing of the pass.
29626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
29718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  virtual bool runOnFunction(Function &F) = 0;
2981bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner
29926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// doFinalization - Virtual method overriden by subclasses to do any post
30026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// processing needed after all passes have run.
30126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
302eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual bool doFinalization(Module &);
3031bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner
3048247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick  virtual void assignPassManager(PMStack &PMS,
3058d570bf8e9eb41e2f1c283bd67cc58514b0c429aDan Gohman                                 PassManagerType T);
3068f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel
3078f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel  ///  Return what kind of Pass Manager can manage this pass.
308eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual PassManagerType getPotentialPassManagerType() const;
309aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner};
3101bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner
3111bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner
3121bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner
313aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner//===----------------------------------------------------------------------===//
31426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// BasicBlockPass class - This class is used to implement most local
31526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// optimizations.  Optimizations should subclass this class if they
31626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// meet the following constraints:
31726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///   1. Optimizations are local, operating on either a basic block or
31826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///      instruction at a time.
31926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///   2. Optimizations do not modify the CFG of the contained function, or any
32026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///      other basic block in the function.
321fb215b936df0034f9030e921c0c054ab11470762Misha Brukman///   3. Optimizations conform to all of the constraints of FunctionPasses.
32226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner///
3234d447f512122a8c3784f4ab2c55b4b1be47db82eDevang Patelclass BasicBlockPass : public Pass {
32462281a132f11e742a96ff9caeaaaa17900020acdJeff Cohenpublic:
32590c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson  explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
326eae540a037d17d218767f21b6d3b45f395df4619Devang Patel
327eb69732d56f7e581bf648df9bf1352d513913df6Jay Foad  /// createPrinterPass - Get a basic block printer pass.
3285c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene  Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
3295c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene
330d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner  /// doInitialization - Virtual method overridden by subclasses to do
3315560c9d49ccae132cabf1155f18aa0480dce3edaMisha Brukman  /// any necessary per-module initialization.
332d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner  ///
333eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual bool doInitialization(Module &);
334d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner
335d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner  /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
3365560c9d49ccae132cabf1155f18aa0480dce3edaMisha Brukman  /// to do any necessary per-function initialization.
337d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner  ///
338eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual bool doInitialization(Function &);
339d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner
34026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// runOnBasicBlock - Virtual method overriden by subclasses to do the
34126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  /// per-basicblock processing of the pass.
34226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
34318961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
3441bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner
345d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner  /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
346d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner  /// do any post processing needed after all passes have run.
34726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner  ///
348eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual bool doFinalization(Function &);
349d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner
350d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner  /// doFinalization - Virtual method overriden by subclasses to do any post
351d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner  /// processing needed after all passes have run.
352d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner  ///
353eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual bool doFinalization(Module &);
354d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner
3558247e0dca6759d9a22ac4c5cf305fac052b285acAndrew Trick  virtual void assignPassManager(PMStack &PMS,
3568d570bf8e9eb41e2f1c283bd67cc58514b0c429aDan Gohman                                 PassManagerType T);
3578f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel
3588f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel  ///  Return what kind of Pass Manager can manage this pass.
359eda23faedb436777ea8b1183f743d8add6b79170Dan Gohman  virtual PassManagerType getPotentialPassManagerType() const;
36025919cb7802f2682b159a040ba07c5bd12b54efaDevang Patel};
36125919cb7802f2682b159a040ba07c5bd12b54efaDevang Patel
3621f079267dc8281f9cb92b23910de621b3387265aReid Spencer/// If the user specifies the -time-passes argument on an LLVM tool command line
3631f079267dc8281f9cb92b23910de621b3387265aReid Spencer/// then the value of this boolean will be true, otherwise false.
3641f079267dc8281f9cb92b23910de621b3387265aReid Spencer/// @brief This is the storage for the -time-passes option.
3651f079267dc8281f9cb92b23910de621b3387265aReid Spencerextern bool TimePassesIsEnabled;
3661f079267dc8281f9cb92b23910de621b3387265aReid Spencer
367d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
368d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
3694c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner// Include support files that contain important APIs commonly used by Passes,
3701d06fbe8935f479c0438798f86237d6264ce60d3Misha Brukman// but that we want to separate out to make it easier to read the header files.
371f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner//
3724c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner#include "llvm/PassSupport.h"
3734c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner#include "llvm/PassAnalysisSupport.h"
37405ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner
3751bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner#endif
376