Pass.h revision a8185fe50bdc2de23c00c2ada1a632e17adcf6c5
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 16aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner// executed as effeciently 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 32a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson#include "llvm/Module.h" 3357db1734af6d7bc9bb2659272122f875d62946a4Anton Korobeynikov#include "llvm/Support/DataTypes.h" 34b5ebf15b2b2ce8989caf1a1114b05d80b0f9bd48Bill Wendling#include "llvm/Support/Streams.h" 35be67780f31958b05ad3c510ca3a973d327517e86Chris Lattner#include <cassert> 36630fcb86785f96501126e52009619b475403dc62Misha Brukman#include <iosfwd> 37630fcb86785f96501126e52009619b475403dc62Misha Brukman#include <utility> 38630fcb86785f96501126e52009619b475403dc62Misha Brukman#include <vector> 39d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 40d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 41d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 421fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerclass BasicBlock; 43e7506a366e8bd56c97d10beb68e4db953aebaecaChris Lattnerclass Function; 4405ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattnerclass Module; 45f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattnerclass AnalysisUsage; 464c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattnerclass PassInfo; 4770b4f3e05106761e99cc56919767155cad0e2d3aChris Lattnerclass ImmutablePass; 4809e6e4303f1aac10fea6860e7736c234fcbf56ccDevang Patelclass PMStack; 49cde53d3c1e9d6a2add5de847b44818fbb1d69c20Devang Patelclass AnalysisResolver; 5097149737f27457b0411e49af3e4539688e29848fDevang Patelclass PMDataManager; 51a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Andersonclass LLVMContext; 52aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner 53a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner// AnalysisID - Use the PassInfo to identify a pass... 54a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattnertypedef const PassInfo* AnalysisID; 55a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner 56be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel/// Different types of internal pass managers. External pass managers 57be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel/// (PassManager and FunctionPassManager) are not represented here. 58be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel/// Ordering of pass manager types is important here. 59be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patelenum PassManagerType { 60be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel PMT_Unknown = 0, 61be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel PMT_ModulePassManager = 1, /// MPPassManager 62be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel PMT_CallGraphPassManager, /// CGPassManager 63be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel PMT_FunctionPassManager, /// FPPassManager 64be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel PMT_LoopPassManager, /// LPPassManager 65fe613905b371ef7160968cdaabf6074cfa10f4a0Devang Patel PMT_BasicBlockPassManager, /// BBPassManager 66fe613905b371ef7160968cdaabf6074cfa10f4a0Devang Patel PMT_Last 67be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel}; 68be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel 691bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner//===----------------------------------------------------------------------===// 7026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// Pass interface - Implemented by all 'passes'. Subclass this if you are an 7126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// interprocedural optimization or you do not fit into any of the more 7226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// constrained passes described below. 7326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// 7405ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattnerclass Pass { 75cde53d3c1e9d6a2add5de847b44818fbb1d69c20Devang Patel AnalysisResolver *Resolver; // Used to resolve analysis 76eae540a037d17d218767f21b6d3b45f395df4619Devang Patel intptr_t PassID; 7776a8f7fb5a325ab635f55f811803eb3ca034f8faChris Lattner 78f9b2297380d57d35c707864a9d55c1e00da39700Chris Lattner void operator=(const Pass&); // DO NOT IMPLEMENT 79f9b2297380d57d35c707864a9d55c1e00da39700Chris Lattner Pass(const Pass &); // DO NOT IMPLEMENT 80a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson 81a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Andersonprotected: 82a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson LLVMContext* Context; 83a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson 8405ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattnerpublic: 85d24931632b480e0472e88df200c4ef80649e48f1Torok Edwin explicit Pass(intptr_t pid) : Resolver(0), PassID(pid) { 86d24931632b480e0472e88df200c4ef80649e48f1Torok Edwin assert(pid && "pid cannot be 0"); 87d24931632b480e0472e88df200c4ef80649e48f1Torok Edwin } 88d24931632b480e0472e88df200c4ef80649e48f1Torok Edwin explicit Pass(const void *pid) : Resolver(0), PassID((intptr_t)pid) { 89d24931632b480e0472e88df200c4ef80649e48f1Torok Edwin assert(pid && "pid cannot be 0"); 90d24931632b480e0472e88df200c4ef80649e48f1Torok Edwin } 916e21ff0b0a8e4f0878431afa5628bb1c2db0b8e1Devang Patel virtual ~Pass(); 9205ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner 9326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// getPassName - Return a nice clean name for a pass. This usually 9426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// implemented in terms of the name that is registered by one of the 95b973d5f9b59f75854493bc37a9eb0f3032be0864Dan Gohman /// Registration templates, but can be overloaded directly. 9626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 9796c466b06ab0c830b07329c1b16037f585ccbe40Chris Lattner virtual const char *getPassName() const; 98aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner 9926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// getPassInfo - Return the PassInfo data structure that corresponds to this 10026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// pass... If the pass has not been registered, this will return null. 10126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 1024c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner const PassInfo *getPassInfo() const; 1034c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner 10426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// print - Print out the internal state of the pass. This is called by 10526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// Analyze to print out the contents of an analysis. Otherwise it is not 1065560c9d49ccae132cabf1155f18aa0480dce3edaMisha Brukman /// necessary to implement this method. Beware that the module pointer MAY be 10726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// null. This automatically forwards to a virtual function that does not 10826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// provide the Module* in case the analysis doesn't need it it can just be 10926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// ignored. 11026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 111ce9653ce449f1409815547e1bf60abcd1332d2c9Reid Spencer virtual void print(std::ostream &O, const Module *M) const; 1125c7e326585f3a543388ba871c3425f7664cd9143Bill Wendling void print(std::ostream *O, const Module *M) const { if (O) print(*O, M); } 113a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner void dump() const; // dump - call print(std::cerr, 0); 114a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner 11522a1cf9d3a5c829d260bcf44ffe6b34ecf16076cDevang Patel /// Each pass is responsible for assigning a pass manager to itself. 11622a1cf9d3a5c829d260bcf44ffe6b34ecf16076cDevang Patel /// PMS is the stack of available pass manager. 11713d57320bd212483463d4f8992d5787b29eda5dfBill Wendling virtual void assignPassManager(PMStack &, 11813d57320bd212483463d4f8992d5787b29eda5dfBill Wendling PassManagerType = PMT_Unknown) {} 11922a1cf9d3a5c829d260bcf44ffe6b34ecf16076cDevang Patel /// Check if available pass managers are suitable for this pass or not. 12013d57320bd212483463d4f8992d5787b29eda5dfBill Wendling virtual void preparePassManager(PMStack &) {} 1218f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel 1228f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel /// Return what kind of Pass Manager can manage this pass. 1238f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel virtual PassManagerType getPotentialPassManagerType() const { 1248f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel return PMT_Unknown; 1258f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel } 12622a1cf9d3a5c829d260bcf44ffe6b34ecf16076cDevang Patel 127cde53d3c1e9d6a2add5de847b44818fbb1d69c20Devang Patel // Access AnalysisResolver 128b42295df4da77a34519d0c49840a8b997d3f0b7cDevang Patel inline void setResolver(AnalysisResolver *AR) { 129b42295df4da77a34519d0c49840a8b997d3f0b7cDevang Patel assert (!Resolver && "Resolver is already set"); 130b42295df4da77a34519d0c49840a8b997d3f0b7cDevang Patel Resolver = AR; 131b42295df4da77a34519d0c49840a8b997d3f0b7cDevang Patel } 132b42295df4da77a34519d0c49840a8b997d3f0b7cDevang Patel inline AnalysisResolver *getResolver() { 133b42295df4da77a34519d0c49840a8b997d3f0b7cDevang Patel return Resolver; 134b42295df4da77a34519d0c49840a8b997d3f0b7cDevang Patel } 135a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner 13626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// getAnalysisUsage - This function should be overriden by passes that need 13726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// analysis information to do their job. If a pass specifies that it uses a 13826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// particular analysis result to this function, it can then use the 13926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// getAnalysis<AnalysisType>() function, below. 14026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 14113d57320bd212483463d4f8992d5787b29eda5dfBill Wendling virtual void getAnalysisUsage(AnalysisUsage &) const { 142f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner // By default, no analysis results are used, all are invalidated. 14305ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner } 14405ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner 14526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// releaseMemory() - This member can be implemented by a pass if it wants to 14626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// be able to release its memory when it is no longer needed. The default 14726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// behavior of passes is to hold onto memory for the entire duration of their 14826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// lifetime (which is the entire compile time). For pipelined passes, this 14926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// is not a big deal because that memory gets recycled every time the pass is 15026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// invoked on another program unit. For IP passes, it is more important to 15126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// free memory when it is unused. 15226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 15326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// Optionally implement this function to release pass memory when it is no 15426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// longer used. 15526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 156ce885e9f9d03fa1d9dd5e2abba5e95953473288fChris Lattner virtual void releaseMemory() {} 157ce885e9f9d03fa1d9dd5e2abba5e95953473288fChris Lattner 1589750b5d5779e6efec10a93633dd1d36c5f61dbc3Devang Patel /// verifyAnalysis() - This member can be implemented by a analysis pass to 1599750b5d5779e6efec10a93633dd1d36c5f61dbc3Devang Patel /// check state of analysis information. 16058e0ef1e90c3f6dbae213612b44e56f7d6d65ea7Devang Patel virtual void verifyAnalysis() const {} 1619750b5d5779e6efec10a93633dd1d36c5f61dbc3Devang Patel 16205ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner // dumpPassStructure - Implement the -debug-passes=PassStructure option 16305ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner virtual void dumpPassStructure(unsigned Offset = 0); 16405ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner 165fb30fda1de96251017ae092f469de4b290b6b61bChris Lattner template<typename AnalysisClass> 166fb30fda1de96251017ae092f469de4b290b6b61bChris Lattner static const PassInfo *getClassPassInfo() { 167276222a5ae189ed5c6a2afb248d4c8f0335585b4Reid Spencer return lookupPassInfo(intptr_t(&AnalysisClass::ID)); 168fb30fda1de96251017ae092f469de4b290b6b61bChris Lattner } 169fb30fda1de96251017ae092f469de4b290b6b61bChris Lattner 170fb30fda1de96251017ae092f469de4b290b6b61bChris Lattner // lookupPassInfo - Return the pass info object for the specified pass class, 171fb30fda1de96251017ae092f469de4b290b6b61bChris Lattner // or null if it is not known. 172eae540a037d17d218767f21b6d3b45f395df4619Devang Patel static const PassInfo *lookupPassInfo(intptr_t TI); 173fb30fda1de96251017ae092f469de4b290b6b61bChris Lattner 1741465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to 1751465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands /// get analysis information that might be around, for example to update it. 1761465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands /// This is different than getAnalysis in that it can fail (if the analysis 1771465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands /// results haven't been computed), so should only be used if you can handle 1781465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands /// the case when the analysis is not available. This method is often used by 1791465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands /// transformation APIs to update analysis results for a pass automatically as 1801465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands /// the transform is performed. 181f9bbe214fdd7391d2798c6e2d3d281f02f6dcc43Chris Lattner /// 1821465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands template<typename AnalysisType> AnalysisType * 1831465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h 184f9bbe214fdd7391d2798c6e2d3d281f02f6dcc43Chris Lattner 185f1ab454b71efae1a5cee7f9285d91b75ead6a8a7Chris Lattner /// mustPreserveAnalysisID - This method serves the same function as 1861465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands /// getAnalysisIfAvailable, but works if you just have an AnalysisID. This 187f1ab454b71efae1a5cee7f9285d91b75ead6a8a7Chris Lattner /// obviously cannot give you a properly typed instance of the class if you 1881465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands /// don't have the class name available (use getAnalysisIfAvailable if you 1891465d61bdd36cfd6021036a527895f0dd358e97dDuncan Sands /// do), but it can tell you if you need to preserve the pass at least. 190f1ab454b71efae1a5cee7f9285d91b75ead6a8a7Chris Lattner /// 191f1ab454b71efae1a5cee7f9285d91b75ead6a8a7Chris Lattner bool mustPreserveAnalysisID(const PassInfo *AnalysisID) const; 192f1ab454b71efae1a5cee7f9285d91b75ead6a8a7Chris Lattner 19326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// getAnalysis<AnalysisType>() - This function is used by subclasses to get 19426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// to the analysis information that they claim to use by overriding the 19526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// getAnalysisUsage function. 19626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 19705ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner template<typename AnalysisType> 1983f2577decba715615597352c4b3e2a551d44b64cDevang Patel AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h 19976a8f7fb5a325ab635f55f811803eb3ca034f8faChris Lattner 20076a8f7fb5a325ab635f55f811803eb3ca034f8faChris Lattner template<typename AnalysisType> 2016b1df0e863963929634d769f39cbce68f030f051Devang Patel AnalysisType &getAnalysis(Function &F); // Defined in PassanalysisSupport.h 2026b1df0e863963929634d769f39cbce68f030f051Devang Patel 2036b1df0e863963929634d769f39cbce68f030f051Devang Patel template<typename AnalysisType> 2043f2577decba715615597352c4b3e2a551d44b64cDevang Patel AnalysisType &getAnalysisID(const PassInfo *PI) const; 2058f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel 2066b1df0e863963929634d769f39cbce68f030f051Devang Patel template<typename AnalysisType> 2076b1df0e863963929634d769f39cbce68f030f051Devang Patel AnalysisType &getAnalysisID(const PassInfo *PI, Function &F); 208aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner}; 209aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner 210a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattnerinline std::ostream &operator<<(std::ostream &OS, const Pass &P) { 211a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner P.print(OS, 0); return OS; 212a59cbb2043c08f3cfb8fb379f0d336e21e070be8Chris Lattner} 213aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner 214b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner//===----------------------------------------------------------------------===// 215b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner/// ModulePass class - This class is used to implement unstructured 216fb215b936df0034f9030e921c0c054ab11470762Misha Brukman/// interprocedural optimizations and analyses. ModulePasses may do anything 217b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner/// they want to the program. 218b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner/// 2191fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerclass ModulePass : public Pass { 2201fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerpublic: 221b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner /// runOnModule - Virtual method overriden by subclasses to process the module 222b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner /// being operated on. 223b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner virtual bool runOnModule(Module &M) = 0; 224b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner 225be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel virtual void assignPassManager(PMStack &PMS, 226bed2946a96ecb15b0b636fa74cb26ce61b1c648eAnton Korobeynikov PassManagerType T = PMT_ModulePassManager); 2278f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel 2288f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel /// Return what kind of Pass Manager can manage this pass. 2298f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel virtual PassManagerType getPotentialPassManagerType() const { 2308f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel return PMT_ModulePassManager; 2318f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel } 2328f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel 233c758209153ca0f6da6737f25ada269c573fba456Devang Patel explicit ModulePass(intptr_t pid) : Pass(pid) {} 234c758209153ca0f6da6737f25ada269c573fba456Devang Patel explicit ModulePass(const void *pid) : Pass(pid) {} 235c7d0f4be88cc7fa33d51736e0f53d3e00e69efb4Devang Patel // Force out-of-line virtual method. 236c7d0f4be88cc7fa33d51736e0f53d3e00e69efb4Devang Patel virtual ~ModulePass(); 237b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner}; 23870b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner 23970b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner 24070b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner//===----------------------------------------------------------------------===// 24170b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner/// ImmutablePass class - This class is used to provide information that does 24270b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner/// not need to be run. This is useful for things like target information and 24370b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner/// "basic" versions of AnalysisGroups. 24470b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner/// 2451fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerclass ImmutablePass : public ModulePass { 2461fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerpublic: 247d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner /// initializePass - This method may be overriden by immutable passes to allow 248d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner /// them to perform various initialization actions they require. This is 249d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner /// primarily because an ImmutablePass can "require" another ImmutablePass, 250d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner /// and if it does, the overloaded version of initializePass may get access to 251d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner /// these passes with getAnalysis<>. 252d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner /// 253d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner virtual void initializePass() {} 25470b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner 255d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner /// ImmutablePasses are never run. 256d2c0b28c33ccf7a219f0adfcd348615493fb8e91Chris Lattner /// 25713d57320bd212483463d4f8992d5787b29eda5dfBill Wendling bool runOnModule(Module &) { return false; } 25870b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner 259c758209153ca0f6da6737f25ada269c573fba456Devang Patel explicit ImmutablePass(intptr_t pid) : ModulePass(pid) {} 260c758209153ca0f6da6737f25ada269c573fba456Devang Patel explicit ImmutablePass(const void *pid) 261c758209153ca0f6da6737f25ada269c573fba456Devang Patel : ModulePass(pid) {} 262529ac2f286b89a3f39efb91914897cef7e07b0d6Chris Lattner 263c7d0f4be88cc7fa33d51736e0f53d3e00e69efb4Devang Patel // Force out-of-line virtual method. 264c7d0f4be88cc7fa33d51736e0f53d3e00e69efb4Devang Patel virtual ~ImmutablePass(); 26570b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner}; 26670b4f3e05106761e99cc56919767155cad0e2d3aChris Lattner 267aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner//===----------------------------------------------------------------------===// 26826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// FunctionPass class - This class is used to implement most global 26926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// optimizations. Optimizations should subclass this class if they meet the 27026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// following constraints: 27126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// 2721d06fbe8935f479c0438798f86237d6264ce60d3Misha Brukman/// 1. Optimizations are organized globally, i.e., a function at a time 27326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// 2. Optimizing a function does not cause the addition or removal of any 27426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// functions in the module 27526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// 276a65f5fd21b946c33876d87105974ebcce5d37d0aDevang Patelclass FunctionPass : public Pass { 2771fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerpublic: 278c758209153ca0f6da6737f25ada269c573fba456Devang Patel explicit FunctionPass(intptr_t pid) : Pass(pid) {} 279c758209153ca0f6da6737f25ada269c573fba456Devang Patel explicit FunctionPass(const void *pid) : Pass(pid) {} 280eae540a037d17d218767f21b6d3b45f395df4619Devang Patel 28126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// doInitialization - Virtual method overridden by subclasses to do 2825560c9d49ccae132cabf1155f18aa0480dce3edaMisha Brukman /// any necessary per-module initialization. 28326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 284a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson virtual bool doInitialization(Module &M) { 285a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson Context = &M.getContext(); 286a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson return false; 287a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson } 2881bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner 28926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// runOnFunction - Virtual method overriden by subclasses to do the 29026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// per-function processing of the pass. 29126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 29218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner virtual bool runOnFunction(Function &F) = 0; 2931bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner 29426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// doFinalization - Virtual method overriden by subclasses to do any post 29526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// processing needed after all passes have run. 29626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 29713d57320bd212483463d4f8992d5787b29eda5dfBill Wendling virtual bool doFinalization(Module &) { return false; } 2981bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner 299b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner /// runOnModule - On a module, we run this pass by initializing, 300b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner /// ronOnFunction'ing once for every function in the module, then by 301b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner /// finalizing. 30226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 303b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner virtual bool runOnModule(Module &M); 3041bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner 30526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// run - On a function, we simply initialize, run the function, then 30626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// finalize. 30726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 30818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner bool run(Function &F); 30905ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner 310be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel virtual void assignPassManager(PMStack &PMS, 311bed2946a96ecb15b0b636fa74cb26ce61b1c648eAnton Korobeynikov PassManagerType T = PMT_FunctionPassManager); 3128f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel 3138f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel /// Return what kind of Pass Manager can manage this pass. 3148f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel virtual PassManagerType getPotentialPassManagerType() const { 3158f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel return PMT_FunctionPassManager; 3168f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel } 317aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner}; 3181bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner 3191bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner 3201bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner 321aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner//===----------------------------------------------------------------------===// 32226199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// BasicBlockPass class - This class is used to implement most local 32326199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// optimizations. Optimizations should subclass this class if they 32426199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// meet the following constraints: 32526199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// 1. Optimizations are local, operating on either a basic block or 32626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// instruction at a time. 32726199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// 2. Optimizations do not modify the CFG of the contained function, or any 32826199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// other basic block in the function. 329fb215b936df0034f9030e921c0c054ab11470762Misha Brukman/// 3. Optimizations conform to all of the constraints of FunctionPasses. 33026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner/// 3314d447f512122a8c3784f4ab2c55b4b1be47db82eDevang Patelclass BasicBlockPass : public Pass { 33262281a132f11e742a96ff9caeaaaa17900020acdJeff Cohenpublic: 333c758209153ca0f6da6737f25ada269c573fba456Devang Patel explicit BasicBlockPass(intptr_t pid) : Pass(pid) {} 334c758209153ca0f6da6737f25ada269c573fba456Devang Patel explicit BasicBlockPass(const void *pid) : Pass(pid) {} 335eae540a037d17d218767f21b6d3b45f395df4619Devang Patel 336d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner /// doInitialization - Virtual method overridden by subclasses to do 3375560c9d49ccae132cabf1155f18aa0480dce3edaMisha Brukman /// any necessary per-module initialization. 338d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner /// 339a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson virtual bool doInitialization(Module &M) { 340a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson Context = &M.getContext(); 341a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson return false; 342a8185fe50bdc2de23c00c2ada1a632e17adcf6c5Owen Anderson } 343d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner 344d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner /// doInitialization - Virtual method overridden by BasicBlockPass subclasses 3455560c9d49ccae132cabf1155f18aa0480dce3edaMisha Brukman /// to do any necessary per-function initialization. 346d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner /// 34713d57320bd212483463d4f8992d5787b29eda5dfBill Wendling virtual bool doInitialization(Function &) { return false; } 348d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner 34926199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// runOnBasicBlock - Virtual method overriden by subclasses to do the 35026199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// per-basicblock processing of the pass. 35126199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 35218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner virtual bool runOnBasicBlock(BasicBlock &BB) = 0; 3531bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner 354d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to 355d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner /// do any post processing needed after all passes have run. 35626199059268a05739c84ebf465fcdbf7ded861dfChris Lattner /// 35713d57320bd212483463d4f8992d5787b29eda5dfBill Wendling virtual bool doFinalization(Function &) { return false; } 358d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner 359d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner /// doFinalization - Virtual method overriden by subclasses to do any post 360d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner /// processing needed after all passes have run. 361d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner /// 36213d57320bd212483463d4f8992d5787b29eda5dfBill Wendling virtual bool doFinalization(Module &) { return false; } 363d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner 364d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner 365d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner // To run this pass on a function, we simply call runOnBasicBlock once for 366d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner // each function. 367d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner // 368d0713f94af4d2dbd97ab251a43e3d290a13e860eChris Lattner bool runOnFunction(Function &F); 369aff5bcebb7fb9880e0a3518a8e7c999e738d531cChris Lattner 370be1ffc6b8d3db7b1a9d512a1d53afd075d8e58c9Devang Patel virtual void assignPassManager(PMStack &PMS, 371bed2946a96ecb15b0b636fa74cb26ce61b1c648eAnton Korobeynikov PassManagerType T = PMT_BasicBlockPassManager); 3728f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel 3738f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel /// Return what kind of Pass Manager can manage this pass. 3748f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel virtual PassManagerType getPotentialPassManagerType() const { 3758f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel return PMT_BasicBlockPassManager; 3768f93b7fc36dbeba428c6dd122c07fe0777baa664Devang Patel } 37725919cb7802f2682b159a040ba07c5bd12b54efaDevang Patel}; 37825919cb7802f2682b159a040ba07c5bd12b54efaDevang Patel 3791f079267dc8281f9cb92b23910de621b3387265aReid Spencer/// If the user specifies the -time-passes argument on an LLVM tool command line 3801f079267dc8281f9cb92b23910de621b3387265aReid Spencer/// then the value of this boolean will be true, otherwise false. 3811f079267dc8281f9cb92b23910de621b3387265aReid Spencer/// @brief This is the storage for the -time-passes option. 3821f079267dc8281f9cb92b23910de621b3387265aReid Spencerextern bool TimePassesIsEnabled; 3831f079267dc8281f9cb92b23910de621b3387265aReid Spencer 384d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 385d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 3864c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner// Include support files that contain important APIs commonly used by Passes, 3871d06fbe8935f479c0438798f86237d6264ce60d3Misha Brukman// but that we want to separate out to make it easier to read the header files. 388f57b845547302d24ecb6a9e79d7bc386f761a6c9Chris Lattner// 3894c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner#include "llvm/PassSupport.h" 3904c76fc048bb9a3c5f760a50ea114f0605b9561d9Chris Lattner#include "llvm/PassAnalysisSupport.h" 39105ad462d1b8e0486879ecd910c3ff4541bd8604cChris Lattner 3921bffea0341968b3b2b0106c745d4602b6804e62fChris Lattner#endif 393