Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

470 lines
15 KiB

  1. //===- llvm/PassManagers.h - Pass Infrastructure classes -------*- 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. //
  10. // This file declares the LLVM Pass Manager infrastructure.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_PASSMANAGERS_H
  14. #define LLVM_PASSMANAGERS_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/SmallPtrSet.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/Pass.h"
  20. #include <map>
  21. #include <vector>
  22. //===----------------------------------------------------------------------===//
  23. // Overview:
  24. // The Pass Manager Infrastructure manages passes. It's responsibilities are:
  25. //
  26. // o Manage optimization pass execution order
  27. // o Make required Analysis information available before pass P is run
  28. // o Release memory occupied by dead passes
  29. // o If Analysis information is dirtied by a pass then regenerate Analysis
  30. // information before it is consumed by another pass.
  31. //
  32. // Pass Manager Infrastructure uses multiple pass managers. They are
  33. // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
  34. // This class hierarchy uses multiple inheritance but pass managers do not
  35. // derive from another pass manager.
  36. //
  37. // PassManager and FunctionPassManager are two top-level pass manager that
  38. // represents the external interface of this entire pass manager infrastucture.
  39. //
  40. // Important classes :
  41. //
  42. // [o] class PMTopLevelManager;
  43. //
  44. // Two top level managers, PassManager and FunctionPassManager, derive from
  45. // PMTopLevelManager. PMTopLevelManager manages information used by top level
  46. // managers such as last user info.
  47. //
  48. // [o] class PMDataManager;
  49. //
  50. // PMDataManager manages information, e.g. list of available analysis info,
  51. // used by a pass manager to manage execution order of passes. It also provides
  52. // a place to implement common pass manager APIs. All pass managers derive from
  53. // PMDataManager.
  54. //
  55. // [o] class BBPassManager : public FunctionPass, public PMDataManager;
  56. //
  57. // BBPassManager manages BasicBlockPasses.
  58. //
  59. // [o] class FunctionPassManager;
  60. //
  61. // This is a external interface used by JIT to manage FunctionPasses. This
  62. // interface relies on FunctionPassManagerImpl to do all the tasks.
  63. //
  64. // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
  65. // public PMTopLevelManager;
  66. //
  67. // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
  68. //
  69. // [o] class FPPassManager : public ModulePass, public PMDataManager;
  70. //
  71. // FPPassManager manages FunctionPasses and BBPassManagers
  72. //
  73. // [o] class MPPassManager : public Pass, public PMDataManager;
  74. //
  75. // MPPassManager manages ModulePasses and FPPassManagers
  76. //
  77. // [o] class PassManager;
  78. //
  79. // This is a external interface used by various tools to manages passes. It
  80. // relies on PassManagerImpl to do all the tasks.
  81. //
  82. // [o] class PassManagerImpl : public Pass, public PMDataManager,
  83. // public PMTopLevelManager
  84. //
  85. // PassManagerImpl is a top level pass manager responsible for managing
  86. // MPPassManagers.
  87. //===----------------------------------------------------------------------===//
  88. #include "llvm/Support/PrettyStackTrace.h"
  89. namespace llvm {
  90. class Module;
  91. class Pass;
  92. class StringRef;
  93. class Value;
  94. class Timer;
  95. class PMDataManager;
  96. // enums for debugging strings
  97. enum PassDebuggingString {
  98. EXECUTION_MSG, // "Executing Pass '"
  99. MODIFICATION_MSG, // "' Made Modification '"
  100. FREEING_MSG, // " Freeing Pass '"
  101. ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n"
  102. ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
  103. ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
  104. ON_REGION_MSG, // " 'on Region ...\n'"
  105. ON_LOOP_MSG, // " 'on Loop ...\n'"
  106. ON_CG_MSG // "' on Call Graph ...\n'"
  107. };
  108. /// PassManagerPrettyStackEntry - This is used to print informative information
  109. /// about what pass is running when/if a stack trace is generated.
  110. class PassManagerPrettyStackEntry : public PrettyStackTraceEntry {
  111. Pass *P;
  112. Value *V;
  113. Module *M;
  114. public:
  115. explicit PassManagerPrettyStackEntry(Pass *p)
  116. : P(p), V(0), M(0) {} // When P is releaseMemory'd.
  117. PassManagerPrettyStackEntry(Pass *p, Value &v)
  118. : P(p), V(&v), M(0) {} // When P is run on V
  119. PassManagerPrettyStackEntry(Pass *p, Module &m)
  120. : P(p), V(0), M(&m) {} // When P is run on M
  121. /// print - Emit information about this stack frame to OS.
  122. virtual void print(raw_ostream &OS) const;
  123. };
  124. //===----------------------------------------------------------------------===//
  125. // PMStack
  126. //
  127. /// PMStack - This class implements a stack data structure of PMDataManager
  128. /// pointers.
  129. ///
  130. /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers
  131. /// using PMStack. Each Pass implements assignPassManager() to connect itself
  132. /// with appropriate manager. assignPassManager() walks PMStack to find
  133. /// suitable manager.
  134. class PMStack {
  135. public:
  136. typedef std::vector<PMDataManager *>::const_reverse_iterator iterator;
  137. iterator begin() const { return S.rbegin(); }
  138. iterator end() const { return S.rend(); }
  139. void pop();
  140. PMDataManager *top() const { return S.back(); }
  141. void push(PMDataManager *PM);
  142. bool empty() const { return S.empty(); }
  143. void dump() const;
  144. private:
  145. std::vector<PMDataManager *> S;
  146. };
  147. //===----------------------------------------------------------------------===//
  148. // PMTopLevelManager
  149. //
  150. /// PMTopLevelManager manages LastUser info and collects common APIs used by
  151. /// top level pass managers.
  152. class PMTopLevelManager {
  153. protected:
  154. explicit PMTopLevelManager(PMDataManager *PMDM);
  155. unsigned getNumContainedManagers() const {
  156. return (unsigned)PassManagers.size();
  157. }
  158. void initializeAllAnalysisInfo();
  159. private:
  160. virtual PMDataManager *getAsPMDataManager() = 0;
  161. virtual PassManagerType getTopLevelPassManagerType() = 0;
  162. public:
  163. /// Schedule pass P for execution. Make sure that passes required by
  164. /// P are run before P is run. Update analysis info maintained by
  165. /// the manager. Remove dead passes. This is a recursive function.
  166. void schedulePass(Pass *P);
  167. /// Set pass P as the last user of the given analysis passes.
  168. void setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P);
  169. /// Collect passes whose last user is P
  170. void collectLastUses(SmallVectorImpl<Pass *> &LastUses, Pass *P);
  171. /// Find the pass that implements Analysis AID. Search immutable
  172. /// passes and all pass managers. If desired pass is not found
  173. /// then return NULL.
  174. Pass *findAnalysisPass(AnalysisID AID);
  175. /// Find analysis usage information for the pass P.
  176. AnalysisUsage *findAnalysisUsage(Pass *P);
  177. virtual ~PMTopLevelManager();
  178. /// Add immutable pass and initialize it.
  179. inline void addImmutablePass(ImmutablePass *P) {
  180. P->initializePass();
  181. ImmutablePasses.push_back(P);
  182. }
  183. inline SmallVectorImpl<ImmutablePass *>& getImmutablePasses() {
  184. return ImmutablePasses;
  185. }
  186. void addPassManager(PMDataManager *Manager) {
  187. PassManagers.push_back(Manager);
  188. }
  189. // Add Manager into the list of managers that are not directly
  190. // maintained by this top level pass manager
  191. inline void addIndirectPassManager(PMDataManager *Manager) {
  192. IndirectPassManagers.push_back(Manager);
  193. }
  194. // Print passes managed by this top level manager.
  195. void dumpPasses() const;
  196. void dumpArguments() const;
  197. // Active Pass Managers
  198. PMStack activeStack;
  199. protected:
  200. /// Collection of pass managers
  201. SmallVector<PMDataManager *, 8> PassManagers;
  202. private:
  203. /// Collection of pass managers that are not directly maintained
  204. /// by this pass manager
  205. SmallVector<PMDataManager *, 8> IndirectPassManagers;
  206. // Map to keep track of last user of the analysis pass.
  207. // LastUser->second is the last user of Lastuser->first.
  208. DenseMap<Pass *, Pass *> LastUser;
  209. // Map to keep track of passes that are last used by a pass.
  210. // This inverse map is initialized at PM->run() based on
  211. // LastUser map.
  212. DenseMap<Pass *, SmallPtrSet<Pass *, 8> > InversedLastUser;
  213. /// Immutable passes are managed by top level manager.
  214. SmallVector<ImmutablePass *, 8> ImmutablePasses;
  215. DenseMap<Pass *, AnalysisUsage *> AnUsageMap;
  216. };
  217. //===----------------------------------------------------------------------===//
  218. // PMDataManager
  219. /// PMDataManager provides the common place to manage the analysis data
  220. /// used by pass managers.
  221. class PMDataManager {
  222. public:
  223. explicit PMDataManager() : TPM(NULL), Depth(0) {
  224. initializeAnalysisInfo();
  225. }
  226. virtual ~PMDataManager();
  227. virtual Pass *getAsPass() = 0;
  228. /// Augment AvailableAnalysis by adding analysis made available by pass P.
  229. void recordAvailableAnalysis(Pass *P);
  230. /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
  231. void verifyPreservedAnalysis(Pass *P);
  232. /// Remove Analysis that is not preserved by the pass
  233. void removeNotPreservedAnalysis(Pass *P);
  234. /// Remove dead passes used by P.
  235. void removeDeadPasses(Pass *P, StringRef Msg,
  236. enum PassDebuggingString);
  237. /// Remove P.
  238. void freePass(Pass *P, StringRef Msg,
  239. enum PassDebuggingString);
  240. /// Add pass P into the PassVector. Update
  241. /// AvailableAnalysis appropriately if ProcessAnalysis is true.
  242. void add(Pass *P, bool ProcessAnalysis = true);
  243. /// Add RequiredPass into list of lower level passes required by pass P.
  244. /// RequiredPass is run on the fly by Pass Manager when P requests it
  245. /// through getAnalysis interface.
  246. virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
  247. virtual Pass *getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F);
  248. /// Initialize available analysis information.
  249. void initializeAnalysisInfo() {
  250. AvailableAnalysis.clear();
  251. for (unsigned i = 0; i < PMT_Last; ++i)
  252. InheritedAnalysis[i] = NULL;
  253. }
  254. // Return true if P preserves high level analysis used by other
  255. // passes that are managed by this manager.
  256. bool preserveHigherLevelAnalysis(Pass *P);
  257. /// Populate RequiredPasses with analysis pass that are required by
  258. /// pass P and are available. Populate ReqPassNotAvailable with analysis
  259. /// pass that are required by pass P but are not available.
  260. void collectRequiredAnalysis(SmallVectorImpl<Pass *> &RequiredPasses,
  261. SmallVectorImpl<AnalysisID> &ReqPassNotAvailable,
  262. Pass *P);
  263. /// All Required analyses should be available to the pass as it runs! Here
  264. /// we fill in the AnalysisImpls member of the pass so that it can
  265. /// successfully use the getAnalysis() method to retrieve the
  266. /// implementations it needs.
  267. void initializeAnalysisImpl(Pass *P);
  268. /// Find the pass that implements Analysis AID. If desired pass is not found
  269. /// then return NULL.
  270. Pass *findAnalysisPass(AnalysisID AID, bool Direction);
  271. // Access toplevel manager
  272. PMTopLevelManager *getTopLevelManager() { return TPM; }
  273. void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
  274. unsigned getDepth() const { return Depth; }
  275. void setDepth(unsigned newDepth) { Depth = newDepth; }
  276. // Print routines used by debug-pass
  277. void dumpLastUses(Pass *P, unsigned Offset) const;
  278. void dumpPassArguments() const;
  279. void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
  280. enum PassDebuggingString S2, StringRef Msg);
  281. void dumpRequiredSet(const Pass *P) const;
  282. void dumpPreservedSet(const Pass *P) const;
  283. unsigned getNumContainedPasses() const {
  284. return (unsigned)PassVector.size();
  285. }
  286. virtual PassManagerType getPassManagerType() const {
  287. assert ( 0 && "Invalid use of getPassManagerType");
  288. return PMT_Unknown;
  289. }
  290. DenseMap<AnalysisID, Pass*> *getAvailableAnalysis() {
  291. return &AvailableAnalysis;
  292. }
  293. // Collect AvailableAnalysis from all the active Pass Managers.
  294. void populateInheritedAnalysis(PMStack &PMS) {
  295. unsigned Index = 0;
  296. for (PMStack::iterator I = PMS.begin(), E = PMS.end();
  297. I != E; ++I)
  298. InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
  299. }
  300. protected:
  301. // Top level manager.
  302. PMTopLevelManager *TPM;
  303. // Collection of pass that are managed by this manager
  304. SmallVector<Pass *, 16> PassVector;
  305. // Collection of Analysis provided by Parent pass manager and
  306. // used by current pass manager. At at time there can not be more
  307. // then PMT_Last active pass mangers.
  308. DenseMap<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last];
  309. /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
  310. /// or higher is specified.
  311. bool isPassDebuggingExecutionsOrMore() const;
  312. private:
  313. void dumpAnalysisUsage(StringRef Msg, const Pass *P,
  314. const AnalysisUsage::VectorType &Set) const;
  315. // Set of available Analysis. This information is used while scheduling
  316. // pass. If a pass requires an analysis which is not available then
  317. // the required analysis pass is scheduled to run before the pass itself is
  318. // scheduled to run.
  319. DenseMap<AnalysisID, Pass*> AvailableAnalysis;
  320. // Collection of higher level analysis used by the pass managed by
  321. // this manager.
  322. SmallVector<Pass *, 8> HigherLevelAnalysis;
  323. unsigned Depth;
  324. };
  325. //===----------------------------------------------------------------------===//
  326. // FPPassManager
  327. //
  328. /// FPPassManager manages BBPassManagers and FunctionPasses.
  329. /// It batches all function passes and basic block pass managers together and
  330. /// sequence them to process one function at a time before processing next
  331. /// function.
  332. class FPPassManager : public ModulePass, public PMDataManager {
  333. public:
  334. static char ID;
  335. explicit FPPassManager()
  336. : ModulePass(ID), PMDataManager() { }
  337. /// run - Execute all of the passes scheduled for execution. Keep track of
  338. /// whether any of the passes modifies the module, and if so, return true.
  339. bool runOnFunction(Function &F);
  340. bool runOnModule(Module &M);
  341. /// cleanup - After running all passes, clean up pass manager cache.
  342. void cleanup();
  343. /// doInitialization - Overrides ModulePass doInitialization for global
  344. /// initialization tasks
  345. ///
  346. using ModulePass::doInitialization;
  347. /// doInitialization - Run all of the initializers for the function passes.
  348. ///
  349. bool doInitialization(Module &M);
  350. /// doFinalization - Overrides ModulePass doFinalization for global
  351. /// finalization tasks
  352. ///
  353. using ModulePass::doFinalization;
  354. /// doFinalization - Run all of the finalizers for the function passes.
  355. ///
  356. bool doFinalization(Module &M);
  357. virtual PMDataManager *getAsPMDataManager() { return this; }
  358. virtual Pass *getAsPass() { return this; }
  359. /// Pass Manager itself does not invalidate any analysis info.
  360. void getAnalysisUsage(AnalysisUsage &Info) const {
  361. Info.setPreservesAll();
  362. }
  363. // Print passes managed by this manager
  364. void dumpPassStructure(unsigned Offset);
  365. virtual const char *getPassName() const {
  366. return "Function Pass Manager";
  367. }
  368. FunctionPass *getContainedPass(unsigned N) {
  369. assert ( N < PassVector.size() && "Pass number out of range!");
  370. FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
  371. return FP;
  372. }
  373. virtual PassManagerType getPassManagerType() const {
  374. return PMT_FunctionPassManager;
  375. }
  376. };
  377. Timer *getPassTimer(Pass *);
  378. }
  379. #endif