PassManager.h 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- PassManager.h - Pass management infrastructure -----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. ///
  15. /// This header defines various interfaces for pass management in LLVM. There
  16. /// is no "pass" interface in LLVM per se. Instead, an instance of any class
  17. /// which supports a method to 'run' it over a unit of IR can be used as
  18. /// a pass. A pass manager is generally a tool to collect a sequence of passes
  19. /// which run over a particular IR construct, and run each of them in sequence
  20. /// over each such construct in the containing IR construct. As there is no
  21. /// containing IR construct for a Module, a manager for passes over modules
  22. /// forms the base case which runs its managed passes in sequence over the
  23. /// single module provided.
  24. ///
  25. /// The core IR library provides managers for running passes over
  26. /// modules and functions.
  27. ///
  28. /// * FunctionPassManager can run over a Module, runs each pass over
  29. /// a Function.
  30. /// * ModulePassManager must be directly run, runs each pass over the Module.
  31. ///
  32. /// Note that the implementations of the pass managers use concept-based
  33. /// polymorphism as outlined in the "Value Semantics and Concept-based
  34. /// Polymorphism" talk (or its abbreviated sibling "Inheritance Is The Base
  35. /// Class of Evil") by Sean Parent:
  36. /// * http://github.com/sean-parent/sean-parent.github.com/wiki/Papers-and-Presentations
  37. /// * http://www.youtube.com/watch?v=_BpMYeUFXv8
  38. /// * http://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil
  39. ///
  40. //===----------------------------------------------------------------------===//
  41. #ifndef LLVM_IR_PASSMANAGER_H
  42. #define LLVM_IR_PASSMANAGER_H
  43. #include "llvm/ADT/DenseMap.h"
  44. #include "llvm/ADT/STLExtras.h"
  45. #include "llvm/ADT/SmallPtrSet.h"
  46. #include "llvm/ADT/StringRef.h"
  47. #include "llvm/ADT/TinyPtrVector.h"
  48. #include "llvm/IR/Function.h"
  49. #include "llvm/IR/Module.h"
  50. #include "llvm/IR/PassInstrumentation.h"
  51. #include "llvm/IR/PassManagerInternal.h"
  52. #include "llvm/Pass.h"
  53. #include "llvm/Support/Debug.h"
  54. #include "llvm/Support/TimeProfiler.h"
  55. #include "llvm/Support/TypeName.h"
  56. #include <algorithm>
  57. #include <cassert>
  58. #include <cstring>
  59. #include <iterator>
  60. #include <list>
  61. #include <memory>
  62. #include <tuple>
  63. #include <type_traits>
  64. #include <utility>
  65. #include <vector>
  66. namespace llvm {
  67. /// A special type used by analysis passes to provide an address that
  68. /// identifies that particular analysis pass type.
  69. ///
  70. /// Analysis passes should have a static data member of this type and derive
  71. /// from the \c AnalysisInfoMixin to get a static ID method used to identify
  72. /// the analysis in the pass management infrastructure.
  73. struct alignas(8) AnalysisKey {};
  74. /// A special type used to provide an address that identifies a set of related
  75. /// analyses. These sets are primarily used below to mark sets of analyses as
  76. /// preserved.
  77. ///
  78. /// For example, a transformation can indicate that it preserves the CFG of a
  79. /// function by preserving the appropriate AnalysisSetKey. An analysis that
  80. /// depends only on the CFG can then check if that AnalysisSetKey is preserved;
  81. /// if it is, the analysis knows that it itself is preserved.
  82. struct alignas(8) AnalysisSetKey {};
  83. /// This templated class represents "all analyses that operate over \<a
  84. /// particular IR unit\>" (e.g. a Function or a Module) in instances of
  85. /// PreservedAnalysis.
  86. ///
  87. /// This lets a transformation say e.g. "I preserved all function analyses".
  88. ///
  89. /// Note that you must provide an explicit instantiation declaration and
  90. /// definition for this template in order to get the correct behavior on
  91. /// Windows. Otherwise, the address of SetKey will not be stable.
  92. template <typename IRUnitT> class AllAnalysesOn {
  93. public:
  94. static AnalysisSetKey *ID() { return &SetKey; }
  95. private:
  96. static AnalysisSetKey SetKey;
  97. };
  98. template <typename IRUnitT> AnalysisSetKey AllAnalysesOn<IRUnitT>::SetKey;
  99. extern template class AllAnalysesOn<Module>;
  100. extern template class AllAnalysesOn<Function>;
  101. /// Represents analyses that only rely on functions' control flow.
  102. ///
  103. /// This can be used with \c PreservedAnalyses to mark the CFG as preserved and
  104. /// to query whether it has been preserved.
  105. ///
  106. /// The CFG of a function is defined as the set of basic blocks and the edges
  107. /// between them. Changing the set of basic blocks in a function is enough to
  108. /// mutate the CFG. Mutating the condition of a branch or argument of an
  109. /// invoked function does not mutate the CFG, but changing the successor labels
  110. /// of those instructions does.
  111. class CFGAnalyses {
  112. public:
  113. static AnalysisSetKey *ID() { return &SetKey; }
  114. private:
  115. static AnalysisSetKey SetKey;
  116. };
  117. /// A set of analyses that are preserved following a run of a transformation
  118. /// pass.
  119. ///
  120. /// Transformation passes build and return these objects to communicate which
  121. /// analyses are still valid after the transformation. For most passes this is
  122. /// fairly simple: if they don't change anything all analyses are preserved,
  123. /// otherwise only a short list of analyses that have been explicitly updated
  124. /// are preserved.
  125. ///
  126. /// This class also lets transformation passes mark abstract *sets* of analyses
  127. /// as preserved. A transformation that (say) does not alter the CFG can
  128. /// indicate such by marking a particular AnalysisSetKey as preserved, and
  129. /// then analyses can query whether that AnalysisSetKey is preserved.
  130. ///
  131. /// Finally, this class can represent an "abandoned" analysis, which is
  132. /// not preserved even if it would be covered by some abstract set of analyses.
  133. ///
  134. /// Given a `PreservedAnalyses` object, an analysis will typically want to
  135. /// figure out whether it is preserved. In the example below, MyAnalysisType is
  136. /// preserved if it's not abandoned, and (a) it's explicitly marked as
  137. /// preserved, (b), the set AllAnalysesOn<MyIRUnit> is preserved, or (c) both
  138. /// AnalysisSetA and AnalysisSetB are preserved.
  139. ///
  140. /// ```
  141. /// auto PAC = PA.getChecker<MyAnalysisType>();
  142. /// if (PAC.preserved() || PAC.preservedSet<AllAnalysesOn<MyIRUnit>>() ||
  143. /// (PAC.preservedSet<AnalysisSetA>() &&
  144. /// PAC.preservedSet<AnalysisSetB>())) {
  145. /// // The analysis has been successfully preserved ...
  146. /// }
  147. /// ```
  148. class PreservedAnalyses {
  149. public:
  150. /// Convenience factory function for the empty preserved set.
  151. static PreservedAnalyses none() { return PreservedAnalyses(); }
  152. /// Construct a special preserved set that preserves all passes.
  153. static PreservedAnalyses all() {
  154. PreservedAnalyses PA;
  155. PA.PreservedIDs.insert(&AllAnalysesKey);
  156. return PA;
  157. }
  158. /// Construct a preserved analyses object with a single preserved set.
  159. template <typename AnalysisSetT>
  160. static PreservedAnalyses allInSet() {
  161. PreservedAnalyses PA;
  162. PA.preserveSet<AnalysisSetT>();
  163. return PA;
  164. }
  165. /// Mark an analysis as preserved.
  166. template <typename AnalysisT> void preserve() { preserve(AnalysisT::ID()); }
  167. /// Given an analysis's ID, mark the analysis as preserved, adding it
  168. /// to the set.
  169. void preserve(AnalysisKey *ID) {
  170. // Clear this ID from the explicit not-preserved set if present.
  171. NotPreservedAnalysisIDs.erase(ID);
  172. // If we're not already preserving all analyses (other than those in
  173. // NotPreservedAnalysisIDs).
  174. if (!areAllPreserved())
  175. PreservedIDs.insert(ID);
  176. }
  177. /// Mark an analysis set as preserved.
  178. template <typename AnalysisSetT> void preserveSet() {
  179. preserveSet(AnalysisSetT::ID());
  180. }
  181. /// Mark an analysis set as preserved using its ID.
  182. void preserveSet(AnalysisSetKey *ID) {
  183. // If we're not already in the saturated 'all' state, add this set.
  184. if (!areAllPreserved())
  185. PreservedIDs.insert(ID);
  186. }
  187. /// Mark an analysis as abandoned.
  188. ///
  189. /// An abandoned analysis is not preserved, even if it is nominally covered
  190. /// by some other set or was previously explicitly marked as preserved.
  191. ///
  192. /// Note that you can only abandon a specific analysis, not a *set* of
  193. /// analyses.
  194. template <typename AnalysisT> void abandon() { abandon(AnalysisT::ID()); }
  195. /// Mark an analysis as abandoned using its ID.
  196. ///
  197. /// An abandoned analysis is not preserved, even if it is nominally covered
  198. /// by some other set or was previously explicitly marked as preserved.
  199. ///
  200. /// Note that you can only abandon a specific analysis, not a *set* of
  201. /// analyses.
  202. void abandon(AnalysisKey *ID) {
  203. PreservedIDs.erase(ID);
  204. NotPreservedAnalysisIDs.insert(ID);
  205. }
  206. /// Intersect this set with another in place.
  207. ///
  208. /// This is a mutating operation on this preserved set, removing all
  209. /// preserved passes which are not also preserved in the argument.
  210. void intersect(const PreservedAnalyses &Arg) {
  211. if (Arg.areAllPreserved())
  212. return;
  213. if (areAllPreserved()) {
  214. *this = Arg;
  215. return;
  216. }
  217. // The intersection requires the *union* of the explicitly not-preserved
  218. // IDs and the *intersection* of the preserved IDs.
  219. for (auto ID : Arg.NotPreservedAnalysisIDs) {
  220. PreservedIDs.erase(ID);
  221. NotPreservedAnalysisIDs.insert(ID);
  222. }
  223. for (auto ID : PreservedIDs)
  224. if (!Arg.PreservedIDs.count(ID))
  225. PreservedIDs.erase(ID);
  226. }
  227. /// Intersect this set with a temporary other set in place.
  228. ///
  229. /// This is a mutating operation on this preserved set, removing all
  230. /// preserved passes which are not also preserved in the argument.
  231. void intersect(PreservedAnalyses &&Arg) {
  232. if (Arg.areAllPreserved())
  233. return;
  234. if (areAllPreserved()) {
  235. *this = std::move(Arg);
  236. return;
  237. }
  238. // The intersection requires the *union* of the explicitly not-preserved
  239. // IDs and the *intersection* of the preserved IDs.
  240. for (auto ID : Arg.NotPreservedAnalysisIDs) {
  241. PreservedIDs.erase(ID);
  242. NotPreservedAnalysisIDs.insert(ID);
  243. }
  244. for (auto ID : PreservedIDs)
  245. if (!Arg.PreservedIDs.count(ID))
  246. PreservedIDs.erase(ID);
  247. }
  248. /// A checker object that makes it easy to query for whether an analysis or
  249. /// some set covering it is preserved.
  250. class PreservedAnalysisChecker {
  251. friend class PreservedAnalyses;
  252. const PreservedAnalyses &PA;
  253. AnalysisKey *const ID;
  254. const bool IsAbandoned;
  255. /// A PreservedAnalysisChecker is tied to a particular Analysis because
  256. /// `preserved()` and `preservedSet()` both return false if the Analysis
  257. /// was abandoned.
  258. PreservedAnalysisChecker(const PreservedAnalyses &PA, AnalysisKey *ID)
  259. : PA(PA), ID(ID), IsAbandoned(PA.NotPreservedAnalysisIDs.count(ID)) {}
  260. public:
  261. /// Returns true if the checker's analysis was not abandoned and either
  262. /// - the analysis is explicitly preserved or
  263. /// - all analyses are preserved.
  264. bool preserved() {
  265. return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
  266. PA.PreservedIDs.count(ID));
  267. }
  268. /// Return true if the checker's analysis was not abandoned, i.e. it was not
  269. /// explicitly invalidated. Even if the analysis is not explicitly
  270. /// preserved, if the analysis is known stateless, then it is preserved.
  271. bool preservedWhenStateless() {
  272. return !IsAbandoned;
  273. }
  274. /// Returns true if the checker's analysis was not abandoned and either
  275. /// - \p AnalysisSetT is explicitly preserved or
  276. /// - all analyses are preserved.
  277. template <typename AnalysisSetT> bool preservedSet() {
  278. AnalysisSetKey *SetID = AnalysisSetT::ID();
  279. return !IsAbandoned && (PA.PreservedIDs.count(&AllAnalysesKey) ||
  280. PA.PreservedIDs.count(SetID));
  281. }
  282. };
  283. /// Build a checker for this `PreservedAnalyses` and the specified analysis
  284. /// type.
  285. ///
  286. /// You can use the returned object to query whether an analysis was
  287. /// preserved. See the example in the comment on `PreservedAnalysis`.
  288. template <typename AnalysisT> PreservedAnalysisChecker getChecker() const {
  289. return PreservedAnalysisChecker(*this, AnalysisT::ID());
  290. }
  291. /// Build a checker for this `PreservedAnalyses` and the specified analysis
  292. /// ID.
  293. ///
  294. /// You can use the returned object to query whether an analysis was
  295. /// preserved. See the example in the comment on `PreservedAnalysis`.
  296. PreservedAnalysisChecker getChecker(AnalysisKey *ID) const {
  297. return PreservedAnalysisChecker(*this, ID);
  298. }
  299. /// Test whether all analyses are preserved (and none are abandoned).
  300. ///
  301. /// This is used primarily to optimize for the common case of a transformation
  302. /// which makes no changes to the IR.
  303. bool areAllPreserved() const {
  304. return NotPreservedAnalysisIDs.empty() &&
  305. PreservedIDs.count(&AllAnalysesKey);
  306. }
  307. /// Directly test whether a set of analyses is preserved.
  308. ///
  309. /// This is only true when no analyses have been explicitly abandoned.
  310. template <typename AnalysisSetT> bool allAnalysesInSetPreserved() const {
  311. return allAnalysesInSetPreserved(AnalysisSetT::ID());
  312. }
  313. /// Directly test whether a set of analyses is preserved.
  314. ///
  315. /// This is only true when no analyses have been explicitly abandoned.
  316. bool allAnalysesInSetPreserved(AnalysisSetKey *SetID) const {
  317. return NotPreservedAnalysisIDs.empty() &&
  318. (PreservedIDs.count(&AllAnalysesKey) || PreservedIDs.count(SetID));
  319. }
  320. private:
  321. /// A special key used to indicate all analyses.
  322. static AnalysisSetKey AllAnalysesKey;
  323. /// The IDs of analyses and analysis sets that are preserved.
  324. SmallPtrSet<void *, 2> PreservedIDs;
  325. /// The IDs of explicitly not-preserved analyses.
  326. ///
  327. /// If an analysis in this set is covered by a set in `PreservedIDs`, we
  328. /// consider it not-preserved. That is, `NotPreservedAnalysisIDs` always
  329. /// "wins" over analysis sets in `PreservedIDs`.
  330. ///
  331. /// Also, a given ID should never occur both here and in `PreservedIDs`.
  332. SmallPtrSet<AnalysisKey *, 2> NotPreservedAnalysisIDs;
  333. };
  334. // Forward declare the analysis manager template.
  335. template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
  336. /// A CRTP mix-in to automatically provide informational APIs needed for
  337. /// passes.
  338. ///
  339. /// This provides some boilerplate for types that are passes.
  340. template <typename DerivedT> struct PassInfoMixin {
  341. /// Gets the name of the pass we are mixed into.
  342. static StringRef name() {
  343. static_assert(std::is_base_of<PassInfoMixin, DerivedT>::value,
  344. "Must pass the derived type as the template argument!");
  345. StringRef Name = getTypeName<DerivedT>();
  346. if (Name.startswith("llvm::"))
  347. Name = Name.drop_front(strlen("llvm::"));
  348. return Name;
  349. }
  350. };
  351. /// A CRTP mix-in that provides informational APIs needed for analysis passes.
  352. ///
  353. /// This provides some boilerplate for types that are analysis passes. It
  354. /// automatically mixes in \c PassInfoMixin.
  355. template <typename DerivedT>
  356. struct AnalysisInfoMixin : PassInfoMixin<DerivedT> {
  357. /// Returns an opaque, unique ID for this analysis type.
  358. ///
  359. /// This ID is a pointer type that is guaranteed to be 8-byte aligned and thus
  360. /// suitable for use in sets, maps, and other data structures that use the low
  361. /// bits of pointers.
  362. ///
  363. /// Note that this requires the derived type provide a static \c AnalysisKey
  364. /// member called \c Key.
  365. ///
  366. /// FIXME: The only reason the mixin type itself can't declare the Key value
  367. /// is that some compilers cannot correctly unique a templated static variable
  368. /// so it has the same addresses in each instantiation. The only currently
  369. /// known platform with this limitation is Windows DLL builds, specifically
  370. /// building each part of LLVM as a DLL. If we ever remove that build
  371. /// configuration, this mixin can provide the static key as well.
  372. static AnalysisKey *ID() {
  373. static_assert(std::is_base_of<AnalysisInfoMixin, DerivedT>::value,
  374. "Must pass the derived type as the template argument!");
  375. return &DerivedT::Key;
  376. }
  377. };
  378. namespace detail {
  379. /// Actual unpacker of extra arguments in getAnalysisResult,
  380. /// passes only those tuple arguments that are mentioned in index_sequence.
  381. template <typename PassT, typename IRUnitT, typename AnalysisManagerT,
  382. typename... ArgTs, size_t... Ns>
  383. typename PassT::Result
  384. getAnalysisResultUnpackTuple(AnalysisManagerT &AM, IRUnitT &IR,
  385. std::tuple<ArgTs...> Args,
  386. std::index_sequence<Ns...>) {
  387. (void)Args;
  388. return AM.template getResult<PassT>(IR, std::get<Ns>(Args)...);
  389. }
  390. /// Helper for *partial* unpacking of extra arguments in getAnalysisResult.
  391. ///
  392. /// Arguments passed in tuple come from PassManager, so they might have extra
  393. /// arguments after those AnalysisManager's ExtraArgTs ones that we need to
  394. /// pass to getResult.
  395. template <typename PassT, typename IRUnitT, typename... AnalysisArgTs,
  396. typename... MainArgTs>
  397. typename PassT::Result
  398. getAnalysisResult(AnalysisManager<IRUnitT, AnalysisArgTs...> &AM, IRUnitT &IR,
  399. std::tuple<MainArgTs...> Args) {
  400. return (getAnalysisResultUnpackTuple<
  401. PassT, IRUnitT>)(AM, IR, Args,
  402. std::index_sequence_for<AnalysisArgTs...>{});
  403. }
  404. } // namespace detail
  405. // Forward declare the pass instrumentation analysis explicitly queried in
  406. // generic PassManager code.
  407. // FIXME: figure out a way to move PassInstrumentationAnalysis into its own
  408. // header.
  409. class PassInstrumentationAnalysis;
  410. /// Manages a sequence of passes over a particular unit of IR.
  411. ///
  412. /// A pass manager contains a sequence of passes to run over a particular unit
  413. /// of IR (e.g. Functions, Modules). It is itself a valid pass over that unit of
  414. /// IR, and when run over some given IR will run each of its contained passes in
  415. /// sequence. Pass managers are the primary and most basic building block of a
  416. /// pass pipeline.
  417. ///
  418. /// When you run a pass manager, you provide an \c AnalysisManager<IRUnitT>
  419. /// argument. The pass manager will propagate that analysis manager to each
  420. /// pass it runs, and will call the analysis manager's invalidation routine with
  421. /// the PreservedAnalyses of each pass it runs.
  422. template <typename IRUnitT,
  423. typename AnalysisManagerT = AnalysisManager<IRUnitT>,
  424. typename... ExtraArgTs>
  425. class PassManager : public PassInfoMixin<
  426. PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
  427. public:
  428. /// Construct a pass manager.
  429. ///
  430. /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
  431. explicit PassManager(bool DebugLogging = false) : DebugLogging(DebugLogging) {}
  432. // FIXME: These are equivalent to the default move constructor/move
  433. // assignment. However, using = default triggers linker errors due to the
  434. // explicit instantiations below. Find away to use the default and remove the
  435. // duplicated code here.
  436. PassManager(PassManager &&Arg)
  437. : Passes(std::move(Arg.Passes)),
  438. DebugLogging(std::move(Arg.DebugLogging)) {}
  439. PassManager &operator=(PassManager &&RHS) {
  440. Passes = std::move(RHS.Passes);
  441. DebugLogging = std::move(RHS.DebugLogging);
  442. return *this;
  443. }
  444. /// Run all of the passes in this manager over the given unit of IR.
  445. /// ExtraArgs are passed to each pass.
  446. PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
  447. ExtraArgTs... ExtraArgs) {
  448. PreservedAnalyses PA = PreservedAnalyses::all();
  449. // Request PassInstrumentation from analysis manager, will use it to run
  450. // instrumenting callbacks for the passes later.
  451. // Here we use std::tuple wrapper over getResult which helps to extract
  452. // AnalysisManager's arguments out of the whole ExtraArgs set.
  453. PassInstrumentation PI =
  454. detail::getAnalysisResult<PassInstrumentationAnalysis>(
  455. AM, IR, std::tuple<ExtraArgTs...>(ExtraArgs...));
  456. if (DebugLogging)
  457. dbgs() << "Starting " << getTypeName<IRUnitT>() << " pass manager run.\n";
  458. for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
  459. auto *P = Passes[Idx].get();
  460. // Check the PassInstrumentation's BeforePass callbacks before running the
  461. // pass, skip its execution completely if asked to (callback returns
  462. // false).
  463. if (!PI.runBeforePass<IRUnitT>(*P, IR))
  464. continue;
  465. PreservedAnalyses PassPA;
  466. {
  467. TimeTraceScope TimeScope(P->name(), IR.getName());
  468. PassPA = P->run(IR, AM, ExtraArgs...);
  469. }
  470. // Call onto PassInstrumentation's AfterPass callbacks immediately after
  471. // running the pass.
  472. PI.runAfterPass<IRUnitT>(*P, IR, PassPA);
  473. // Update the analysis manager as each pass runs and potentially
  474. // invalidates analyses.
  475. AM.invalidate(IR, PassPA);
  476. // Finally, intersect the preserved analyses to compute the aggregate
  477. // preserved set for this pass manager.
  478. PA.intersect(std::move(PassPA));
  479. // FIXME: Historically, the pass managers all called the LLVM context's
  480. // yield function here. We don't have a generic way to acquire the
  481. // context and it isn't yet clear what the right pattern is for yielding
  482. // in the new pass manager so it is currently omitted.
  483. //IR.getContext().yield();
  484. }
  485. // Invalidation was handled after each pass in the above loop for the
  486. // current unit of IR. Therefore, the remaining analysis results in the
  487. // AnalysisManager are preserved. We mark this with a set so that we don't
  488. // need to inspect each one individually.
  489. PA.preserveSet<AllAnalysesOn<IRUnitT>>();
  490. if (DebugLogging)
  491. dbgs() << "Finished " << getTypeName<IRUnitT>() << " pass manager run.\n";
  492. return PA;
  493. }
  494. template <typename PassT>
  495. std::enable_if_t<!std::is_same<PassT, PassManager>::value>
  496. addPass(PassT Pass) {
  497. using PassModelT =
  498. detail::PassModel<IRUnitT, PassT, PreservedAnalyses, AnalysisManagerT,
  499. ExtraArgTs...>;
  500. Passes.emplace_back(new PassModelT(std::move(Pass)));
  501. }
  502. /// When adding a pass manager pass that has the same type as this pass
  503. /// manager, simply move the passes over. This is because we don't have use
  504. /// cases rely on executing nested pass managers. Doing this could reduce
  505. /// implementation complexity and avoid potential invalidation issues that may
  506. /// happen with nested pass managers of the same type.
  507. template <typename PassT>
  508. std::enable_if_t<std::is_same<PassT, PassManager>::value>
  509. addPass(PassT &&Pass) {
  510. for (auto &P : Pass.Passes)
  511. Passes.emplace_back(std::move(P));
  512. }
  513. /// Returns if the pass manager contains any passes.
  514. bool isEmpty() const { return Passes.empty(); }
  515. static bool isRequired() { return true; }
  516. protected:
  517. using PassConceptT =
  518. detail::PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>;
  519. std::vector<std::unique_ptr<PassConceptT>> Passes;
  520. /// Flag indicating whether we should do debug logging.
  521. bool DebugLogging;
  522. };
  523. extern template class PassManager<Module>;
  524. /// Convenience typedef for a pass manager over modules.
  525. using ModulePassManager = PassManager<Module>;
  526. extern template class PassManager<Function>;
  527. /// Convenience typedef for a pass manager over functions.
  528. using FunctionPassManager = PassManager<Function>;
  529. /// Pseudo-analysis pass that exposes the \c PassInstrumentation to pass
  530. /// managers. Goes before AnalysisManager definition to provide its
  531. /// internals (e.g PassInstrumentationAnalysis::ID) for use there if needed.
  532. /// FIXME: figure out a way to move PassInstrumentationAnalysis into its own
  533. /// header.
  534. class PassInstrumentationAnalysis
  535. : public AnalysisInfoMixin<PassInstrumentationAnalysis> {
  536. friend AnalysisInfoMixin<PassInstrumentationAnalysis>;
  537. static AnalysisKey Key;
  538. PassInstrumentationCallbacks *Callbacks;
  539. public:
  540. /// PassInstrumentationCallbacks object is shared, owned by something else,
  541. /// not this analysis.
  542. PassInstrumentationAnalysis(PassInstrumentationCallbacks *Callbacks = nullptr)
  543. : Callbacks(Callbacks) {}
  544. using Result = PassInstrumentation;
  545. template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
  546. Result run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
  547. return PassInstrumentation(Callbacks);
  548. }
  549. };
  550. /// A container for analyses that lazily runs them and caches their
  551. /// results.
  552. ///
  553. /// This class can manage analyses for any IR unit where the address of the IR
  554. /// unit sufficies as its identity.
  555. template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager {
  556. public:
  557. class Invalidator;
  558. private:
  559. // Now that we've defined our invalidator, we can define the concept types.
  560. using ResultConceptT =
  561. detail::AnalysisResultConcept<IRUnitT, PreservedAnalyses, Invalidator>;
  562. using PassConceptT =
  563. detail::AnalysisPassConcept<IRUnitT, PreservedAnalyses, Invalidator,
  564. ExtraArgTs...>;
  565. /// List of analysis pass IDs and associated concept pointers.
  566. ///
  567. /// Requires iterators to be valid across appending new entries and arbitrary
  568. /// erases. Provides the analysis ID to enable finding iterators to a given
  569. /// entry in maps below, and provides the storage for the actual result
  570. /// concept.
  571. using AnalysisResultListT =
  572. std::list<std::pair<AnalysisKey *, std::unique_ptr<ResultConceptT>>>;
  573. /// Map type from IRUnitT pointer to our custom list type.
  574. using AnalysisResultListMapT = DenseMap<IRUnitT *, AnalysisResultListT>;
  575. /// Map type from a pair of analysis ID and IRUnitT pointer to an
  576. /// iterator into a particular result list (which is where the actual analysis
  577. /// result is stored).
  578. using AnalysisResultMapT =
  579. DenseMap<std::pair<AnalysisKey *, IRUnitT *>,
  580. typename AnalysisResultListT::iterator>;
  581. public:
  582. /// API to communicate dependencies between analyses during invalidation.
  583. ///
  584. /// When an analysis result embeds handles to other analysis results, it
  585. /// needs to be invalidated both when its own information isn't preserved and
  586. /// when any of its embedded analysis results end up invalidated. We pass an
  587. /// \c Invalidator object as an argument to \c invalidate() in order to let
  588. /// the analysis results themselves define the dependency graph on the fly.
  589. /// This lets us avoid building an explicit representation of the
  590. /// dependencies between analysis results.
  591. class Invalidator {
  592. public:
  593. /// Trigger the invalidation of some other analysis pass if not already
  594. /// handled and return whether it was in fact invalidated.
  595. ///
  596. /// This is expected to be called from within a given analysis result's \c
  597. /// invalidate method to trigger a depth-first walk of all inter-analysis
  598. /// dependencies. The same \p IR unit and \p PA passed to that result's \c
  599. /// invalidate method should in turn be provided to this routine.
  600. ///
  601. /// The first time this is called for a given analysis pass, it will call
  602. /// the corresponding result's \c invalidate method. Subsequent calls will
  603. /// use a cache of the results of that initial call. It is an error to form
  604. /// cyclic dependencies between analysis results.
  605. ///
  606. /// This returns true if the given analysis's result is invalid. Any
  607. /// dependecies on it will become invalid as a result.
  608. template <typename PassT>
  609. bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA) {
  610. using ResultModelT =
  611. detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
  612. PreservedAnalyses, Invalidator>;
  613. return invalidateImpl<ResultModelT>(PassT::ID(), IR, PA);
  614. }
  615. /// A type-erased variant of the above invalidate method with the same core
  616. /// API other than passing an analysis ID rather than an analysis type
  617. /// parameter.
  618. ///
  619. /// This is sadly less efficient than the above routine, which leverages
  620. /// the type parameter to avoid the type erasure overhead.
  621. bool invalidate(AnalysisKey *ID, IRUnitT &IR, const PreservedAnalyses &PA) {
  622. return invalidateImpl<>(ID, IR, PA);
  623. }
  624. private:
  625. friend class AnalysisManager;
  626. template <typename ResultT = ResultConceptT>
  627. bool invalidateImpl(AnalysisKey *ID, IRUnitT &IR,
  628. const PreservedAnalyses &PA) {
  629. // If we've already visited this pass, return true if it was invalidated
  630. // and false otherwise.
  631. auto IMapI = IsResultInvalidated.find(ID);
  632. if (IMapI != IsResultInvalidated.end())
  633. return IMapI->second;
  634. // Otherwise look up the result object.
  635. auto RI = Results.find({ID, &IR});
  636. assert(RI != Results.end() &&
  637. "Trying to invalidate a dependent result that isn't in the "
  638. "manager's cache is always an error, likely due to a stale result "
  639. "handle!");
  640. auto &Result = static_cast<ResultT &>(*RI->second->second);
  641. // Insert into the map whether the result should be invalidated and return
  642. // that. Note that we cannot reuse IMapI and must do a fresh insert here,
  643. // as calling invalidate could (recursively) insert things into the map,
  644. // making any iterator or reference invalid.
  645. bool Inserted;
  646. std::tie(IMapI, Inserted) =
  647. IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, *this)});
  648. (void)Inserted;
  649. assert(Inserted && "Should not have already inserted this ID, likely "
  650. "indicates a dependency cycle!");
  651. return IMapI->second;
  652. }
  653. Invalidator(SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated,
  654. const AnalysisResultMapT &Results)
  655. : IsResultInvalidated(IsResultInvalidated), Results(Results) {}
  656. SmallDenseMap<AnalysisKey *, bool, 8> &IsResultInvalidated;
  657. const AnalysisResultMapT &Results;
  658. };
  659. /// Construct an empty analysis manager.
  660. ///
  661. /// If \p DebugLogging is true, we'll log our progress to llvm::dbgs().
  662. AnalysisManager(bool DebugLogging = false);
  663. AnalysisManager(AnalysisManager &&);
  664. AnalysisManager &operator=(AnalysisManager &&);
  665. /// Returns true if the analysis manager has an empty results cache.
  666. bool empty() const {
  667. assert(AnalysisResults.empty() == AnalysisResultLists.empty() &&
  668. "The storage and index of analysis results disagree on how many "
  669. "there are!");
  670. return AnalysisResults.empty();
  671. }
  672. /// Clear any cached analysis results for a single unit of IR.
  673. ///
  674. /// This doesn't invalidate, but instead simply deletes, the relevant results.
  675. /// It is useful when the IR is being removed and we want to clear out all the
  676. /// memory pinned for it.
  677. void clear(IRUnitT &IR, llvm::StringRef Name);
  678. /// Clear all analysis results cached by this AnalysisManager.
  679. ///
  680. /// Like \c clear(IRUnitT&), this doesn't invalidate the results; it simply
  681. /// deletes them. This lets you clean up the AnalysisManager when the set of
  682. /// IR units itself has potentially changed, and thus we can't even look up a
  683. /// a result and invalidate/clear it directly.
  684. void clear() {
  685. AnalysisResults.clear();
  686. AnalysisResultLists.clear();
  687. }
  688. /// Get the result of an analysis pass for a given IR unit.
  689. ///
  690. /// Runs the analysis if a cached result is not available.
  691. template <typename PassT>
  692. typename PassT::Result &getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs) {
  693. assert(AnalysisPasses.count(PassT::ID()) &&
  694. "This analysis pass was not registered prior to being queried");
  695. ResultConceptT &ResultConcept =
  696. getResultImpl(PassT::ID(), IR, ExtraArgs...);
  697. using ResultModelT =
  698. detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
  699. PreservedAnalyses, Invalidator>;
  700. return static_cast<ResultModelT &>(ResultConcept).Result;
  701. }
  702. /// Get the cached result of an analysis pass for a given IR unit.
  703. ///
  704. /// This method never runs the analysis.
  705. ///
  706. /// \returns null if there is no cached result.
  707. template <typename PassT>
  708. typename PassT::Result *getCachedResult(IRUnitT &IR) const {
  709. assert(AnalysisPasses.count(PassT::ID()) &&
  710. "This analysis pass was not registered prior to being queried");
  711. ResultConceptT *ResultConcept = getCachedResultImpl(PassT::ID(), IR);
  712. if (!ResultConcept)
  713. return nullptr;
  714. using ResultModelT =
  715. detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
  716. PreservedAnalyses, Invalidator>;
  717. return &static_cast<ResultModelT *>(ResultConcept)->Result;
  718. }
  719. /// Verify that the given Result cannot be invalidated, assert otherwise.
  720. template <typename PassT>
  721. void verifyNotInvalidated(IRUnitT &IR, typename PassT::Result *Result) const {
  722. PreservedAnalyses PA = PreservedAnalyses::none();
  723. SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated;
  724. Invalidator Inv(IsResultInvalidated, AnalysisResults);
  725. assert(!Result->invalidate(IR, PA, Inv) &&
  726. "Cached result cannot be invalidated");
  727. }
  728. /// Register an analysis pass with the manager.
  729. ///
  730. /// The parameter is a callable whose result is an analysis pass. This allows
  731. /// passing in a lambda to construct the analysis.
  732. ///
  733. /// The analysis type to register is the type returned by calling the \c
  734. /// PassBuilder argument. If that type has already been registered, then the
  735. /// argument will not be called and this function will return false.
  736. /// Otherwise, we register the analysis returned by calling \c PassBuilder(),
  737. /// and this function returns true.
  738. ///
  739. /// (Note: Although the return value of this function indicates whether or not
  740. /// an analysis was previously registered, there intentionally isn't a way to
  741. /// query this directly. Instead, you should just register all the analyses
  742. /// you might want and let this class run them lazily. This idiom lets us
  743. /// minimize the number of times we have to look up analyses in our
  744. /// hashtable.)
  745. template <typename PassBuilderT>
  746. bool registerPass(PassBuilderT &&PassBuilder) {
  747. using PassT = decltype(PassBuilder());
  748. using PassModelT =
  749. detail::AnalysisPassModel<IRUnitT, PassT, PreservedAnalyses,
  750. Invalidator, ExtraArgTs...>;
  751. auto &PassPtr = AnalysisPasses[PassT::ID()];
  752. if (PassPtr)
  753. // Already registered this pass type!
  754. return false;
  755. // Construct a new model around the instance returned by the builder.
  756. PassPtr.reset(new PassModelT(PassBuilder()));
  757. return true;
  758. }
  759. /// Invalidate a specific analysis pass for an IR unit.
  760. ///
  761. /// Note that the analysis result can disregard invalidation, if it determines
  762. /// it is in fact still valid.
  763. template <typename PassT> void invalidate(IRUnitT &IR) {
  764. assert(AnalysisPasses.count(PassT::ID()) &&
  765. "This analysis pass was not registered prior to being invalidated");
  766. invalidateImpl(PassT::ID(), IR);
  767. }
  768. /// Invalidate cached analyses for an IR unit.
  769. ///
  770. /// Walk through all of the analyses pertaining to this unit of IR and
  771. /// invalidate them, unless they are preserved by the PreservedAnalyses set.
  772. void invalidate(IRUnitT &IR, const PreservedAnalyses &PA);
  773. private:
  774. /// Look up a registered analysis pass.
  775. PassConceptT &lookUpPass(AnalysisKey *ID) {
  776. typename AnalysisPassMapT::iterator PI = AnalysisPasses.find(ID);
  777. assert(PI != AnalysisPasses.end() &&
  778. "Analysis passes must be registered prior to being queried!");
  779. return *PI->second;
  780. }
  781. /// Look up a registered analysis pass.
  782. const PassConceptT &lookUpPass(AnalysisKey *ID) const {
  783. typename AnalysisPassMapT::const_iterator PI = AnalysisPasses.find(ID);
  784. assert(PI != AnalysisPasses.end() &&
  785. "Analysis passes must be registered prior to being queried!");
  786. return *PI->second;
  787. }
  788. /// Get an analysis result, running the pass if necessary.
  789. ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &IR,
  790. ExtraArgTs... ExtraArgs);
  791. /// Get a cached analysis result or return null.
  792. ResultConceptT *getCachedResultImpl(AnalysisKey *ID, IRUnitT &IR) const {
  793. typename AnalysisResultMapT::const_iterator RI =
  794. AnalysisResults.find({ID, &IR});
  795. return RI == AnalysisResults.end() ? nullptr : &*RI->second->second;
  796. }
  797. /// Invalidate a pass result for a IR unit.
  798. void invalidateImpl(AnalysisKey *ID, IRUnitT &IR) {
  799. typename AnalysisResultMapT::iterator RI =
  800. AnalysisResults.find({ID, &IR});
  801. if (RI == AnalysisResults.end())
  802. return;
  803. if (DebugLogging)
  804. dbgs() << "Invalidating analysis: " << this->lookUpPass(ID).name()
  805. << " on " << IR.getName() << "\n";
  806. AnalysisResultLists[&IR].erase(RI->second);
  807. AnalysisResults.erase(RI);
  808. }
  809. /// Map type from analysis pass ID to pass concept pointer.
  810. using AnalysisPassMapT =
  811. DenseMap<AnalysisKey *, std::unique_ptr<PassConceptT>>;
  812. /// Collection of analysis passes, indexed by ID.
  813. AnalysisPassMapT AnalysisPasses;
  814. /// Map from IR unit to a list of analysis results.
  815. ///
  816. /// Provides linear time removal of all analysis results for a IR unit and
  817. /// the ultimate storage for a particular cached analysis result.
  818. AnalysisResultListMapT AnalysisResultLists;
  819. /// Map from an analysis ID and IR unit to a particular cached
  820. /// analysis result.
  821. AnalysisResultMapT AnalysisResults;
  822. /// Indicates whether we log to \c llvm::dbgs().
  823. bool DebugLogging;
  824. };
  825. extern template class AnalysisManager<Module>;
  826. /// Convenience typedef for the Module analysis manager.
  827. using ModuleAnalysisManager = AnalysisManager<Module>;
  828. extern template class AnalysisManager<Function>;
  829. /// Convenience typedef for the Function analysis manager.
  830. using FunctionAnalysisManager = AnalysisManager<Function>;
  831. /// An analysis over an "outer" IR unit that provides access to an
  832. /// analysis manager over an "inner" IR unit. The inner unit must be contained
  833. /// in the outer unit.
  834. ///
  835. /// For example, InnerAnalysisManagerProxy<FunctionAnalysisManager, Module> is
  836. /// an analysis over Modules (the "outer" unit) that provides access to a
  837. /// Function analysis manager. The FunctionAnalysisManager is the "inner"
  838. /// manager being proxied, and Functions are the "inner" unit. The inner/outer
  839. /// relationship is valid because each Function is contained in one Module.
  840. ///
  841. /// If you're (transitively) within a pass manager for an IR unit U that
  842. /// contains IR unit V, you should never use an analysis manager over V, except
  843. /// via one of these proxies.
  844. ///
  845. /// Note that the proxy's result is a move-only RAII object. The validity of
  846. /// the analyses in the inner analysis manager is tied to its lifetime.
  847. template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
  848. class InnerAnalysisManagerProxy
  849. : public AnalysisInfoMixin<
  850. InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> {
  851. public:
  852. class Result {
  853. public:
  854. explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {}
  855. Result(Result &&Arg) : InnerAM(std::move(Arg.InnerAM)) {
  856. // We have to null out the analysis manager in the moved-from state
  857. // because we are taking ownership of the responsibilty to clear the
  858. // analysis state.
  859. Arg.InnerAM = nullptr;
  860. }
  861. ~Result() {
  862. // InnerAM is cleared in a moved from state where there is nothing to do.
  863. if (!InnerAM)
  864. return;
  865. // Clear out the analysis manager if we're being destroyed -- it means we
  866. // didn't even see an invalidate call when we got invalidated.
  867. InnerAM->clear();
  868. }
  869. Result &operator=(Result &&RHS) {
  870. InnerAM = RHS.InnerAM;
  871. // We have to null out the analysis manager in the moved-from state
  872. // because we are taking ownership of the responsibilty to clear the
  873. // analysis state.
  874. RHS.InnerAM = nullptr;
  875. return *this;
  876. }
  877. /// Accessor for the analysis manager.
  878. AnalysisManagerT &getManager() { return *InnerAM; }
  879. /// Handler for invalidation of the outer IR unit, \c IRUnitT.
  880. ///
  881. /// If the proxy analysis itself is not preserved, we assume that the set of
  882. /// inner IR objects contained in IRUnit may have changed. In this case,
  883. /// we have to call \c clear() on the inner analysis manager, as it may now
  884. /// have stale pointers to its inner IR objects.
  885. ///
  886. /// Regardless of whether the proxy analysis is marked as preserved, all of
  887. /// the analyses in the inner analysis manager are potentially invalidated
  888. /// based on the set of preserved analyses.
  889. bool invalidate(
  890. IRUnitT &IR, const PreservedAnalyses &PA,
  891. typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv);
  892. private:
  893. AnalysisManagerT *InnerAM;
  894. };
  895. explicit InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
  896. : InnerAM(&InnerAM) {}
  897. /// Run the analysis pass and create our proxy result object.
  898. ///
  899. /// This doesn't do any interesting work; it is primarily used to insert our
  900. /// proxy result object into the outer analysis cache so that we can proxy
  901. /// invalidation to the inner analysis manager.
  902. Result run(IRUnitT &IR, AnalysisManager<IRUnitT, ExtraArgTs...> &AM,
  903. ExtraArgTs...) {
  904. return Result(*InnerAM);
  905. }
  906. private:
  907. friend AnalysisInfoMixin<
  908. InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>>;
  909. static AnalysisKey Key;
  910. AnalysisManagerT *InnerAM;
  911. };
  912. template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
  913. AnalysisKey
  914. InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
  915. /// Provide the \c FunctionAnalysisManager to \c Module proxy.
  916. using FunctionAnalysisManagerModuleProxy =
  917. InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;
  918. /// Specialization of the invalidate method for the \c
  919. /// FunctionAnalysisManagerModuleProxy's result.
  920. template <>
  921. bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
  922. Module &M, const PreservedAnalyses &PA,
  923. ModuleAnalysisManager::Invalidator &Inv);
  924. // Ensure the \c FunctionAnalysisManagerModuleProxy is provided as an extern
  925. // template.
  926. extern template class InnerAnalysisManagerProxy<FunctionAnalysisManager,
  927. Module>;
  928. /// An analysis over an "inner" IR unit that provides access to an
  929. /// analysis manager over a "outer" IR unit. The inner unit must be contained
  930. /// in the outer unit.
  931. ///
  932. /// For example OuterAnalysisManagerProxy<ModuleAnalysisManager, Function> is an
  933. /// analysis over Functions (the "inner" unit) which provides access to a Module
  934. /// analysis manager. The ModuleAnalysisManager is the "outer" manager being
  935. /// proxied, and Modules are the "outer" IR unit. The inner/outer relationship
  936. /// is valid because each Function is contained in one Module.
  937. ///
  938. /// This proxy only exposes the const interface of the outer analysis manager,
  939. /// to indicate that you cannot cause an outer analysis to run from within an
  940. /// inner pass. Instead, you must rely on the \c getCachedResult API. This is
  941. /// due to keeping potential future concurrency in mind. To give an example,
  942. /// running a module analysis before any function passes may give a different
  943. /// result than running it in a function pass. Both may be valid, but it would
  944. /// produce non-deterministic results. GlobalsAA is a good analysis example,
  945. /// because the cached information has the mod/ref info for all memory for each
  946. /// function at the time the analysis was computed. The information is still
  947. /// valid after a function transformation, but it may be *different* if
  948. /// recomputed after that transform. GlobalsAA is never invalidated.
  949. ///
  950. /// This proxy doesn't manage invalidation in any way -- that is handled by the
  951. /// recursive return path of each layer of the pass manager. A consequence of
  952. /// this is the outer analyses may be stale. We invalidate the outer analyses
  953. /// only when we're done running passes over the inner IR units.
  954. template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
  955. class OuterAnalysisManagerProxy
  956. : public AnalysisInfoMixin<
  957. OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> {
  958. public:
  959. /// Result proxy object for \c OuterAnalysisManagerProxy.
  960. class Result {
  961. public:
  962. explicit Result(const AnalysisManagerT &OuterAM) : OuterAM(&OuterAM) {}
  963. /// Get a cached analysis. If the analysis can be invalidated, this will
  964. /// assert.
  965. template <typename PassT, typename IRUnitTParam>
  966. typename PassT::Result *getCachedResult(IRUnitTParam &IR) const {
  967. typename PassT::Result *Res =
  968. OuterAM->template getCachedResult<PassT>(IR);
  969. if (Res)
  970. OuterAM->template verifyNotInvalidated<PassT>(IR, Res);
  971. return Res;
  972. }
  973. /// Method provided for unit testing, not intended for general use.
  974. template <typename PassT, typename IRUnitTParam>
  975. bool cachedResultExists(IRUnitTParam &IR) const {
  976. typename PassT::Result *Res =
  977. OuterAM->template getCachedResult<PassT>(IR);
  978. return Res != nullptr;
  979. }
  980. /// When invalidation occurs, remove any registered invalidation events.
  981. bool invalidate(
  982. IRUnitT &IRUnit, const PreservedAnalyses &PA,
  983. typename AnalysisManager<IRUnitT, ExtraArgTs...>::Invalidator &Inv) {
  984. // Loop over the set of registered outer invalidation mappings and if any
  985. // of them map to an analysis that is now invalid, clear it out.
  986. SmallVector<AnalysisKey *, 4> DeadKeys;
  987. for (auto &KeyValuePair : OuterAnalysisInvalidationMap) {
  988. AnalysisKey *OuterID = KeyValuePair.first;
  989. auto &InnerIDs = KeyValuePair.second;
  990. llvm::erase_if(InnerIDs, [&](AnalysisKey *InnerID) {
  991. return Inv.invalidate(InnerID, IRUnit, PA);
  992. });
  993. if (InnerIDs.empty())
  994. DeadKeys.push_back(OuterID);
  995. }
  996. for (auto OuterID : DeadKeys)
  997. OuterAnalysisInvalidationMap.erase(OuterID);
  998. // The proxy itself remains valid regardless of anything else.
  999. return false;
  1000. }
  1001. /// Register a deferred invalidation event for when the outer analysis
  1002. /// manager processes its invalidations.
  1003. template <typename OuterAnalysisT, typename InvalidatedAnalysisT>
  1004. void registerOuterAnalysisInvalidation() {
  1005. AnalysisKey *OuterID = OuterAnalysisT::ID();
  1006. AnalysisKey *InvalidatedID = InvalidatedAnalysisT::ID();
  1007. auto &InvalidatedIDList = OuterAnalysisInvalidationMap[OuterID];
  1008. // Note, this is a linear scan. If we end up with large numbers of
  1009. // analyses that all trigger invalidation on the same outer analysis,
  1010. // this entire system should be changed to some other deterministic
  1011. // data structure such as a `SetVector` of a pair of pointers.
  1012. if (!llvm::is_contained(InvalidatedIDList, InvalidatedID))
  1013. InvalidatedIDList.push_back(InvalidatedID);
  1014. }
  1015. /// Access the map from outer analyses to deferred invalidation requiring
  1016. /// analyses.
  1017. const SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2> &
  1018. getOuterInvalidations() const {
  1019. return OuterAnalysisInvalidationMap;
  1020. }
  1021. private:
  1022. const AnalysisManagerT *OuterAM;
  1023. /// A map from an outer analysis ID to the set of this IR-unit's analyses
  1024. /// which need to be invalidated.
  1025. SmallDenseMap<AnalysisKey *, TinyPtrVector<AnalysisKey *>, 2>
  1026. OuterAnalysisInvalidationMap;
  1027. };
  1028. OuterAnalysisManagerProxy(const AnalysisManagerT &OuterAM)
  1029. : OuterAM(&OuterAM) {}
  1030. /// Run the analysis pass and create our proxy result object.
  1031. /// Nothing to see here, it just forwards the \c OuterAM reference into the
  1032. /// result.
  1033. Result run(IRUnitT &, AnalysisManager<IRUnitT, ExtraArgTs...> &,
  1034. ExtraArgTs...) {
  1035. return Result(*OuterAM);
  1036. }
  1037. private:
  1038. friend AnalysisInfoMixin<
  1039. OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>>;
  1040. static AnalysisKey Key;
  1041. const AnalysisManagerT *OuterAM;
  1042. };
  1043. template <typename AnalysisManagerT, typename IRUnitT, typename... ExtraArgTs>
  1044. AnalysisKey
  1045. OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>::Key;
  1046. extern template class OuterAnalysisManagerProxy<ModuleAnalysisManager,
  1047. Function>;
  1048. /// Provide the \c ModuleAnalysisManager to \c Function proxy.
  1049. using ModuleAnalysisManagerFunctionProxy =
  1050. OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;
  1051. /// Trivial adaptor that maps from a module to its functions.
  1052. ///
  1053. /// Designed to allow composition of a FunctionPass(Manager) and
  1054. /// a ModulePassManager, by running the FunctionPass(Manager) over every
  1055. /// function in the module.
  1056. ///
  1057. /// Function passes run within this adaptor can rely on having exclusive access
  1058. /// to the function they are run over. They should not read or modify any other
  1059. /// functions! Other threads or systems may be manipulating other functions in
  1060. /// the module, and so their state should never be relied on.
  1061. /// FIXME: Make the above true for all of LLVM's actual passes, some still
  1062. /// violate this principle.
  1063. ///
  1064. /// Function passes can also read the module containing the function, but they
  1065. /// should not modify that module outside of the use lists of various globals.
  1066. /// For example, a function pass is not permitted to add functions to the
  1067. /// module.
  1068. /// FIXME: Make the above true for all of LLVM's actual passes, some still
  1069. /// violate this principle.
  1070. ///
  1071. /// Note that although function passes can access module analyses, module
  1072. /// analyses are not invalidated while the function passes are running, so they
  1073. /// may be stale. Function analyses will not be stale.
  1074. class ModuleToFunctionPassAdaptor
  1075. : public PassInfoMixin<ModuleToFunctionPassAdaptor> {
  1076. public:
  1077. using PassConceptT = detail::PassConcept<Function, FunctionAnalysisManager>;
  1078. explicit ModuleToFunctionPassAdaptor(std::unique_ptr<PassConceptT> Pass)
  1079. : Pass(std::move(Pass)) {}
  1080. /// Runs the function pass across every function in the module.
  1081. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  1082. static bool isRequired() { return true; }
  1083. private:
  1084. std::unique_ptr<PassConceptT> Pass;
  1085. };
  1086. /// A function to deduce a function pass type and wrap it in the
  1087. /// templated adaptor.
  1088. template <typename FunctionPassT>
  1089. ModuleToFunctionPassAdaptor
  1090. createModuleToFunctionPassAdaptor(FunctionPassT Pass) {
  1091. using PassModelT =
  1092. detail::PassModel<Function, FunctionPassT, PreservedAnalyses,
  1093. FunctionAnalysisManager>;
  1094. return ModuleToFunctionPassAdaptor(
  1095. std::make_unique<PassModelT>(std::move(Pass)));
  1096. }
  1097. /// A utility pass template to force an analysis result to be available.
  1098. ///
  1099. /// If there are extra arguments at the pass's run level there may also be
  1100. /// extra arguments to the analysis manager's \c getResult routine. We can't
  1101. /// guess how to effectively map the arguments from one to the other, and so
  1102. /// this specialization just ignores them.
  1103. ///
  1104. /// Specific patterns of run-method extra arguments and analysis manager extra
  1105. /// arguments will have to be defined as appropriate specializations.
  1106. template <typename AnalysisT, typename IRUnitT,
  1107. typename AnalysisManagerT = AnalysisManager<IRUnitT>,
  1108. typename... ExtraArgTs>
  1109. struct RequireAnalysisPass
  1110. : PassInfoMixin<RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
  1111. ExtraArgTs...>> {
  1112. /// Run this pass over some unit of IR.
  1113. ///
  1114. /// This pass can be run over any unit of IR and use any analysis manager
  1115. /// provided they satisfy the basic API requirements. When this pass is
  1116. /// created, these methods can be instantiated to satisfy whatever the
  1117. /// context requires.
  1118. PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM,
  1119. ExtraArgTs &&... Args) {
  1120. (void)AM.template getResult<AnalysisT>(Arg,
  1121. std::forward<ExtraArgTs>(Args)...);
  1122. return PreservedAnalyses::all();
  1123. }
  1124. static bool isRequired() { return true; }
  1125. };
  1126. /// A no-op pass template which simply forces a specific analysis result
  1127. /// to be invalidated.
  1128. template <typename AnalysisT>
  1129. struct InvalidateAnalysisPass
  1130. : PassInfoMixin<InvalidateAnalysisPass<AnalysisT>> {
  1131. /// Run this pass over some unit of IR.
  1132. ///
  1133. /// This pass can be run over any unit of IR and use any analysis manager,
  1134. /// provided they satisfy the basic API requirements. When this pass is
  1135. /// created, these methods can be instantiated to satisfy whatever the
  1136. /// context requires.
  1137. template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
  1138. PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...) {
  1139. auto PA = PreservedAnalyses::all();
  1140. PA.abandon<AnalysisT>();
  1141. return PA;
  1142. }
  1143. };
  1144. /// A utility pass that does nothing, but preserves no analyses.
  1145. ///
  1146. /// Because this preserves no analyses, any analysis passes queried after this
  1147. /// pass runs will recompute fresh results.
  1148. struct InvalidateAllAnalysesPass : PassInfoMixin<InvalidateAllAnalysesPass> {
  1149. /// Run this pass over some unit of IR.
  1150. template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
  1151. PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
  1152. return PreservedAnalyses::none();
  1153. }
  1154. };
  1155. /// A utility pass template that simply runs another pass multiple times.
  1156. ///
  1157. /// This can be useful when debugging or testing passes. It also serves as an
  1158. /// example of how to extend the pass manager in ways beyond composition.
  1159. template <typename PassT>
  1160. class RepeatedPass : public PassInfoMixin<RepeatedPass<PassT>> {
  1161. public:
  1162. RepeatedPass(int Count, PassT P) : Count(Count), P(std::move(P)) {}
  1163. template <typename IRUnitT, typename AnalysisManagerT, typename... Ts>
  1164. PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, Ts &&... Args) {
  1165. // Request PassInstrumentation from analysis manager, will use it to run
  1166. // instrumenting callbacks for the passes later.
  1167. // Here we use std::tuple wrapper over getResult which helps to extract
  1168. // AnalysisManager's arguments out of the whole Args set.
  1169. PassInstrumentation PI =
  1170. detail::getAnalysisResult<PassInstrumentationAnalysis>(
  1171. AM, IR, std::tuple<Ts...>(Args...));
  1172. auto PA = PreservedAnalyses::all();
  1173. for (int i = 0; i < Count; ++i) {
  1174. // Check the PassInstrumentation's BeforePass callbacks before running the
  1175. // pass, skip its execution completely if asked to (callback returns
  1176. // false).
  1177. if (!PI.runBeforePass<IRUnitT>(P, IR))
  1178. continue;
  1179. PreservedAnalyses IterPA = P.run(IR, AM, std::forward<Ts>(Args)...);
  1180. PA.intersect(IterPA);
  1181. PI.runAfterPass(P, IR, IterPA);
  1182. }
  1183. return PA;
  1184. }
  1185. private:
  1186. int Count;
  1187. PassT P;
  1188. };
  1189. template <typename PassT>
  1190. RepeatedPass<PassT> createRepeatedPass(int Count, PassT P) {
  1191. return RepeatedPass<PassT>(Count, std::move(P));
  1192. }
  1193. } // end namespace llvm
  1194. #endif // LLVM_IR_PASSMANAGER_H
  1195. #ifdef __GNUC__
  1196. #pragma GCC diagnostic pop
  1197. #endif