PassManager.h 54 KB

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