38 #ifndef LLVM_IR_PASSMANAGER_H 39 #define LLVM_IR_PASSMANAGER_H 58 #include <type_traits> 161 PA.PreservedIDs.
insert(&AllAnalysesKey);
166 template <
typename AnalysisSetT>
180 NotPreservedAnalysisIDs.erase(ID);
184 if (!areAllPreserved())
185 PreservedIDs.insert(ID);
196 if (!areAllPreserved())
197 PreservedIDs.insert(ID);
217 PreservedIDs.erase(ID);
218 NotPreservedAnalysisIDs.insert(ID);
228 if (areAllPreserved()) {
234 for (
auto ID : Arg.NotPreservedAnalysisIDs) {
235 PreservedIDs.erase(
ID);
236 NotPreservedAnalysisIDs.insert(
ID);
238 for (
auto ID : PreservedIDs)
239 if (!Arg.PreservedIDs.
count(
ID))
240 PreservedIDs.erase(
ID);
248 if (
Arg.areAllPreserved())
250 if (areAllPreserved()) {
251 *
this = std::move(
Arg);
256 for (
auto ID :
Arg.NotPreservedAnalysisIDs) {
257 PreservedIDs.erase(
ID);
258 NotPreservedAnalysisIDs.insert(
ID);
260 for (
auto ID : PreservedIDs)
261 if (!
Arg.PreservedIDs.count(
ID))
262 PreservedIDs.erase(
ID);
272 const bool IsAbandoned;
278 : PA(PA), ID(ID), IsAbandoned(PA.NotPreservedAnalysisIDs.
count(ID)) {}
285 return !IsAbandoned && (PA.PreservedIDs.
count(&AllAnalysesKey) ||
286 PA.PreservedIDs.
count(ID));
294 return !IsAbandoned && (PA.PreservedIDs.
count(&AllAnalysesKey) ||
295 PA.PreservedIDs.
count(SetID));
322 return NotPreservedAnalysisIDs.empty() &&
323 PreservedIDs.count(&AllAnalysesKey);
337 return NotPreservedAnalysisIDs.empty() &&
338 (PreservedIDs.count(&AllAnalysesKey) || PreservedIDs.count(SetID));
359 template <
typename IRUnitT,
typename... ExtraArgTs>
class AnalysisManager;
368 static_assert(std::is_base_of<PassInfoMixin, DerivedT>::value,
369 "Must pass the derived type as the template argument!");
381 template <
typename DerivedT>
399 static_assert(std::is_base_of<AnalysisInfoMixin, DerivedT>::value,
400 "Must pass the derived type as the template argument!");
417 template <
typename IRUnitT,
419 typename... ExtraArgTs>
421 PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>> {
426 explicit PassManager(
bool DebugLogging =
false) : DebugLogging(DebugLogging) {}
433 : Passes(
std::move(
Arg.Passes)),
434 DebugLogging(
std::move(
Arg.DebugLogging)) {}
437 Passes = std::move(RHS.Passes);
438 DebugLogging = std::move(RHS.DebugLogging);
445 ExtraArgTs... ExtraArgs) {
449 dbgs() <<
"Starting " << getTypeName<IRUnitT>() <<
" pass manager run.\n";
451 for (
unsigned Idx = 0,
Size = Passes.size(); Idx !=
Size; ++Idx) {
453 dbgs() <<
"Running pass: " << Passes[Idx]->name() <<
" on " 454 << IR.getName() <<
"\n";
460 AM.invalidate(IR, PassPA);
480 dbgs() <<
"Finished " << getTypeName<IRUnitT>() <<
" pass manager run.\n";
490 Passes.emplace_back(
new PassModelT(std::move(Pass)));
497 std::vector<std::unique_ptr<PassConceptT>> Passes;
518 template <
typename IRUnitT,
typename... ExtraArgTs>
class AnalysisManager {
524 using ResultConceptT =
536 using AnalysisResultListT =
537 std::list<std::pair<AnalysisKey *, std::unique_ptr<ResultConceptT>>>;
545 using AnalysisResultMapT =
547 typename AnalysisResultListT::iterator>;
576 template <
typename PassT>
580 PreservedAnalyses, Invalidator>;
582 return invalidateImpl<ResultModelT>(
PassT::ID(),
IR, PA);
592 return invalidateImpl<>(
ID,
IR, PA);
598 template <
typename ResultT = ResultConceptT>
600 const PreservedAnalyses &PA) {
603 auto IMapI = IsResultInvalidated.find(ID);
604 if (IMapI != IsResultInvalidated.end())
605 return IMapI->second;
608 auto RI =
Results.find({ID, &IR});
610 "Trying to invalidate a dependent result that isn't in the " 611 "manager's cache is always an error, likely due to a stale result " 614 auto &
Result =
static_cast<ResultT &
>(*RI->second->second);
621 std::tie(IMapI, Inserted) =
622 IsResultInvalidated.insert({
ID, Result.invalidate(IR, PA, *
this)});
624 assert(Inserted &&
"Should not have already inserted this ID, likely " 625 "indicates a dependency cycle!");
626 return IMapI->second;
630 const AnalysisResultMapT &
Results)
631 : IsResultInvalidated(IsResultInvalidated), Results(Results) {}
634 const AnalysisResultMapT &
Results;
646 assert(AnalysisResults.empty() == AnalysisResultLists.empty() &&
647 "The storage and index of analysis results disagree on how many " 649 return AnalysisResults.empty();
659 dbgs() <<
"Clearing all analysis results for: " << Name <<
"\n";
661 auto ResultsListI = AnalysisResultLists.
find(&IR);
662 if (ResultsListI == AnalysisResultLists.end())
665 for (
auto &IDAndResult : ResultsListI->second)
666 AnalysisResults.erase({IDAndResult.first, &IR});
669 AnalysisResultLists.erase(ResultsListI);
679 AnalysisResults.clear();
680 AnalysisResultLists.clear();
686 template <
typename PassT>
687 typename PassT::Result &
getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs) {
689 "This analysis pass was not registered prior to being queried");
691 getResultImpl(
PassT::ID(), IR, ExtraArgs...);
695 PreservedAnalyses, Invalidator>;
697 return static_cast<ResultModelT &
>(ResultConcept).Result;
705 template <
typename PassT>
708 "This analysis pass was not registered prior to being queried");
716 PreservedAnalyses, Invalidator>;
718 return &
static_cast<ResultModelT *
>(ResultConcept)->Result;
738 template <
typename PassBuilderT>
743 Invalidator, ExtraArgTs...>;
745 auto &PassPtr = AnalysisPasses[
PassT::ID()];
761 "This analysis pass was not registered prior to being invalidated");
775 dbgs() <<
"Invalidating all non-preserved analyses for: " << IR.getName()
781 Invalidator Inv(IsResultInvalidated, AnalysisResults);
782 AnalysisResultListT &ResultsList = AnalysisResultLists[&
IR];
783 for (
auto &AnalysisResultPair : ResultsList) {
789 auto &Result = *AnalysisResultPair.second;
791 auto IMapI = IsResultInvalidated.
find(ID);
792 if (IMapI != IsResultInvalidated.
end())
802 IsResultInvalidated.
insert({
ID, Result.invalidate(IR, PA, Inv)})
805 assert(Inserted &&
"Should never have already inserted this ID, likely " 806 "indicates a cycle!");
810 if (!IsResultInvalidated.
empty()) {
811 for (
auto I = ResultsList.begin(),
E = ResultsList.end();
I !=
E;) {
813 if (!IsResultInvalidated.
lookup(ID)) {
819 dbgs() <<
"Invalidating analysis: " << this->lookUpPass(ID).name()
820 <<
" on " << IR.getName() <<
"\n";
822 I = ResultsList.erase(
I);
823 AnalysisResults.erase({
ID, &IR});
827 if (ResultsList.empty())
828 AnalysisResultLists.erase(&IR);
835 assert(PI != AnalysisPasses.end() &&
836 "Analysis passes must be registered prior to being queried!");
843 assert(PI != AnalysisPasses.end() &&
844 "Analysis passes must be registered prior to being queried!");
850 ExtraArgTs... ExtraArgs) {
851 typename AnalysisResultMapT::iterator RI;
853 std::tie(RI, Inserted) = AnalysisResults.insert(std::make_pair(
854 std::make_pair(ID, &IR),
typename AnalysisResultListT::iterator()));
859 auto &
P = this->lookUpPass(ID);
861 dbgs() <<
"Running analysis: " <<
P.name() <<
" on " << IR.getName()
863 AnalysisResultListT &ResultList = AnalysisResultLists[&
IR];
864 ResultList.emplace_back(ID,
P.run(IR, *
this, ExtraArgs...));
868 RI = AnalysisResults.find({
ID, &IR});
869 assert(RI != AnalysisResults.end() &&
"we just inserted it!");
871 RI->second = std::prev(ResultList.end());
874 return *RI->second->second;
879 typename AnalysisResultMapT::const_iterator RI =
880 AnalysisResults.find({
ID, &IR});
881 return RI == AnalysisResults.end() ? nullptr : &*RI->second->second;
885 void invalidateImpl(
AnalysisKey *ID, IRUnitT &IR) {
886 typename AnalysisResultMapT::iterator RI =
887 AnalysisResults.find({
ID, &IR});
888 if (RI == AnalysisResults.end())
892 dbgs() <<
"Invalidating analysis: " << this->lookUpPass(ID).name()
893 <<
" on " << IR.getName() <<
"\n";
894 AnalysisResultLists[&
IR].erase(RI->second);
895 AnalysisResults.erase(RI);
913 AnalysisResultMapT AnalysisResults;
945 template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
948 InnerAnalysisManagerProxy<AnalysisManagerT, IRUnitT>> {
952 explicit Result(AnalysisManagerT &InnerAM) : InnerAM(&InnerAM) {}
958 Arg.InnerAM =
nullptr;
972 InnerAM = RHS.InnerAM;
976 RHS.InnerAM =
nullptr;
994 IRUnitT &IR,
const PreservedAnalyses &PA,
998 AnalysisManagerT *InnerAM;
1002 : InnerAM(&InnerAM) {}
1020 AnalysisManagerT *InnerAM;
1023 template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
1035 Module &M,
const PreservedAnalyses &PA,
1061 template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
1064 OuterAnalysisManagerProxy<AnalysisManagerT, IRUnitT, ExtraArgTs...>> {
1069 explicit Result(
const AnalysisManagerT &AM) : AM(&AM) {}
1075 IRUnitT &IRUnit,
const PreservedAnalyses &PA,
1080 for (
auto &KeyValuePair : OuterAnalysisInvalidationMap) {
1082 auto &InnerIDs = KeyValuePair.second;
1084 return Inv.
invalidate(InnerID, IRUnit, PA); }),
1086 if (InnerIDs.empty())
1090 for (
auto OuterID : DeadKeys)
1091 OuterAnalysisInvalidationMap.erase(OuterID);
1099 template <
typename OuterAnalysisT,
typename Inval
idatedAnalysisT>
1104 auto &InvalidatedIDList = OuterAnalysisInvalidationMap[OuterID];
1109 auto InvalidatedIt =
std::find(InvalidatedIDList.begin(),
1110 InvalidatedIDList.end(), InvalidatedID);
1111 if (InvalidatedIt == InvalidatedIDList.end())
1112 InvalidatedIDList.push_back(InvalidatedID);
1119 return OuterAnalysisInvalidationMap;
1123 const AnalysisManagerT *AM;
1128 OuterAnalysisInvalidationMap;
1147 const AnalysisManagerT *AM;
1150 template <
typename AnalysisManagerT,
typename IRUnitT,
typename... ExtraArgTs>
1183 template <
typename FunctionPassT>
1185 :
public PassInfoMixin<ModuleToFunctionPassAdaptor<FunctionPassT>> {
1188 : Pass(
std::move(Pass)) {}
1191 PreservedAnalyses
run(Module &M, ModuleAnalysisManager &AM) {
1192 FunctionAnalysisManager &FAM =
1196 for (Function &
F : M) {
1197 if (
F.isDeclaration())
1200 PreservedAnalyses PassPA =
Pass.run(
F, FAM);
1228 template <
typename FunctionPassT>
1243 template <
typename AnalysisT,
typename IRUnitT,
1245 typename... ExtraArgTs>
1247 :
PassInfoMixin<RequireAnalysisPass<AnalysisT, IRUnitT, AnalysisManagerT,
1255 PreservedAnalyses
run(IRUnitT &
Arg, AnalysisManagerT &AM,
1256 ExtraArgTs &&...
Args) {
1257 (void)AM.template getResult<AnalysisT>(Arg,
1258 std::forward<ExtraArgTs>(
Args)...);
1266 template <
typename AnalysisT>
1275 template <
typename IRUnitT,
typename AnalysisManagerT,
typename... ExtraArgTs>
1276 PreservedAnalyses
run(IRUnitT &
Arg, AnalysisManagerT &AM, ExtraArgTs &&...) {
1289 template <
typename IRUnitT,
typename AnalysisManagerT,
typename... ExtraArgTs>
1290 PreservedAnalyses
run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) {
1299 template <
typename PassT>
1304 template <
typename IRUnitT,
typename AnalysisManagerT,
typename... Ts>
1305 PreservedAnalyses
run(IRUnitT &
Arg, AnalysisManagerT &AM, Ts &&...
Args) {
1307 for (
int i = 0; i < Count; ++i)
1317 template <
typename PassT>
1324 #endif // LLVM_IR_PASSMANAGER_H Pass interface - Implemented by all 'passes'.
void abandon()
Mark an analysis as abandoned.
void push_back(const T &Elt)
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Wrapper to model the analysis pass concept.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Compute iterated dominance frontiers using a linear time algorithm.
void intersect(const PreservedAnalyses &Arg)
Intersect this set with another in place.
Trivial adaptor that maps from a module to its functions.
A Module instance is used to store all the information related to an LLVM module. ...
bool invalidate(IRUnitT &IRUnit, const PreservedAnalyses &PA, typename AnalysisManager< IRUnitT, ExtraArgTs... >::Invalidator &Inv)
When invalidation occurs, remove any registered invalidation events.
void clear(IRUnitT &IR, llvm::StringRef Name)
Clear any cached analysis results for a single unit of IR.
PassManager(bool DebugLogging=false)
Construct a pass manager.
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA, typename AnalysisManager< IRUnitT, ExtraArgTs... >::Invalidator &Inv)
Handler for invalidation of the outer IR unit, IRUnitT.
RepeatedPass< PassT > createRepeatedPass(int Count, PassT P)
RepeatedPass(int Count, PassT P)
Function Alias Analysis Results
Template for the abstract base class used to dispatch polymorphically over pass objects.
A utility pass template that simply runs another pass multiple times.
A utility pass template to force an analysis result to be available.
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
Abstract concept of an analysis pass.
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, Ts &&... Args)
OuterAnalysisManagerProxy(const AnalysisManagerT &AM)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
static AnalysisKey * ID()
Returns an opaque, unique ID for this analysis type.
bool registerPass(PassBuilderT &&PassBuilder)
Register an analysis pass with the manager.
This class provides access to building LLVM's passes.
AnalysisManagerT & getManager()
Accessor for the analysis manager.
bool empty() const
Returns true if the analysis manager has an empty results cache.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
PassManager(PassManager &&Arg)
AnalysisManager(bool DebugLogging=false)
Construct an empty analysis manager.
A CRTP mix-in to automatically provide informational APIs needed for passes.
void registerOuterAnalysisInvalidation()
Register a deferred invalidation event for when the outer analysis manager processes its invalidation...
static PreservedAnalyses allInSet()
Construct a preserved analyses object with a single preserved set.
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
iterator find(const_arg_type_t< KeyT > Val)
A checker object that makes it easy to query for whether an analysis or some set covering it is prese...
A set of analyses that are preserved following a run of a transformation pass.
bool areAllPreserved() const
Test whether all analyses are preserved (and none are abandoned).
InnerAnalysisManagerProxy(AnalysisManagerT &InnerAM)
ModuleToFunctionPassAdaptor< FunctionPassT > createModuleToFunctionPassAdaptor(FunctionPassT Pass)
A function to deduce a function pass type and wrap it in the templated adaptor.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
A CRTP mix-in that provides informational APIs needed for analysis passes.
Wrapper to model the analysis result concept.
bool preservedSet()
Returns true if the checker's analysis was not abandoned and either.
void clear()
Clear all analysis results cached by this AnalysisManager.
PreservedAnalyses run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...)
Run this pass over some unit of IR.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
static StringRef name()
Gets the name of the pass we are mixed into.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
PreservedAnalysisChecker getChecker(AnalysisKey *ID) const
Build a checker for this PreservedAnalyses and the specified analysis ID.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void intersect(PreservedAnalyses &&Arg)
Intersect this set with a temporary other set in place.
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
PassManager & operator=(PassManager &&RHS)
Result proxy object for OuterAnalysisManagerProxy.
ModuleToFunctionPassAdaptor(FunctionPassT Pass)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
print lazy value Lazy Value Info Printer Pass
static AnalysisSetKey * ID()
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
A template wrapper used to implement the polymorphic API.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Result run(IRUnitT &IR, AnalysisManager< IRUnitT, ExtraArgTs... > &AM, ExtraArgTs...)
Run the analysis pass and create our proxy result object.
void invalidate(IRUnitT &IR)
Invalidate a specific analysis pass for an IR module.
Abstract concept of an analysis result.
A utility pass that does nothing, but preserves no analyses.
Result run(IRUnitT &, AnalysisManager< IRUnitT, ExtraArgTs... > &, ExtraArgTs...)
Run the analysis pass and create our proxy result object.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Result(AnalysisManagerT &InnerAM)
static AnalysisSetKey * ID()
This header provides internal APIs and implementation details used by the pass management interfaces ...
Represents analyses that only rely on functions' control flow.
bool allAnalysesInSetPreserved(AnalysisSetKey *SetID) const
Directly test whether a set of analyses is preserved.
void abandon(AnalysisKey *ID)
Mark an analysis as abandoned using its ID.
amdgpu Simplify well known AMD library false Value Value * Arg
Result & operator=(Result &&RHS)
void preserveSet(AnalysisSetKey *ID)
Mark an analysis set as preserved using its ID.
A special type used to provide an address that identifies a set of related analyses.
Manages a sequence of passes over a particular unit of IR.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Runs the function pass across every function in the module.
void preserveSet()
Mark an analysis set as preserved.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
void preserve()
Mark an analysis as preserved.
PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&... Args)
Run this pass over some unit of IR.
A no-op pass template which simply forces a specific analysis result to be invalidated.
LLVM_NODISCARD bool empty() const
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
API to communicate dependencies between analyses during invalidation.
bool invalidate(AnalysisKey *ID, IRUnitT &IR, const PreservedAnalyses &PA)
A type-erased variant of the above invalidate method with the same core API other than passing an ana...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This templated class represents "all analyses that operate over <a particular IR unit>" (e...
void preserve(AnalysisKey *ID)
Given an analysis's ID, mark the analysis as preserved, adding it to the set.
bool preserved()
Returns true if the checker's analysis was not abandoned and either.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Result(const AnalysisManagerT &AM)
StringRef - Represent a constant reference to a string, i.e.
A container for analyses that lazily runs them and caches their results.
const SmallDenseMap< AnalysisKey *, TinyPtrVector< AnalysisKey * >, 2 > & getOuterInvalidations() const
Access the map from outer analyses to deferred invalidation requiring analyses.
PreservedAnalyses run(IRUnitT &Arg, AnalysisManagerT &AM, ExtraArgTs &&...)
Run this pass over some unit of IR.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
const AnalysisManagerT & getManager() const
A special type used by analysis passes to provide an address that identifies that particular analysis...
Statically lint checks LLVM IR
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...