LLVM  13.0.0git
PassManagerImpl.h
Go to the documentation of this file.
1 //===- PassManagerImpl.h - Pass management infrastructure -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// Provides implementations for PassManager and AnalysisManager template
10 /// methods. These classes should be explicitly instantiated for any IR unit,
11 /// and files doing the explicit instantiation should include this header.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_PASSMANAGERIMPL_H
16 #define LLVM_IR_PASSMANAGERIMPL_H
17 
18 #include "llvm/IR/PassManager.h"
19 
20 namespace llvm {
21 
22 template <typename IRUnitT, typename... ExtraArgTs>
24  bool DebugLogging)
25  : DebugLogging(DebugLogging) {}
26 
27 template <typename IRUnitT, typename... ExtraArgTs>
29  AnalysisManager &&) = default;
30 
31 template <typename IRUnitT, typename... ExtraArgTs>
32 inline AnalysisManager<IRUnitT, ExtraArgTs...> &
34  default;
35 
36 template <typename IRUnitT, typename... ExtraArgTs>
37 inline void
40  if (DebugLogging)
41  dbgs() << "Clearing all analysis results for: " << Name << "\n";
42 
43  auto ResultsListI = AnalysisResultLists.find(&IR);
44  if (ResultsListI == AnalysisResultLists.end())
45  return;
46  // Delete the map entries that point into the results list.
47  for (auto &IDAndResult : ResultsListI->second)
48  AnalysisResults.erase({IDAndResult.first, &IR});
49 
50  // And actually destroy and erase the results associated with this IR.
51  AnalysisResultLists.erase(ResultsListI);
52 }
53 
54 template <typename IRUnitT, typename... ExtraArgTs>
55 inline typename AnalysisManager<IRUnitT, ExtraArgTs...>::ResultConceptT &
57  AnalysisKey *ID, IRUnitT &IR, ExtraArgTs... ExtraArgs) {
58  typename AnalysisResultMapT::iterator RI;
59  bool Inserted;
60  std::tie(RI, Inserted) = AnalysisResults.insert(std::make_pair(
61  std::make_pair(ID, &IR), typename AnalysisResultListT::iterator()));
62 
63  // If we don't have a cached result for this function, look up the pass and
64  // run it to produce a result, which we then add to the cache.
65  if (Inserted) {
66  auto &P = this->lookUpPass(ID);
67 
70  PI = getResult<PassInstrumentationAnalysis>(IR, ExtraArgs...);
71  PI.runBeforeAnalysis(P, IR);
72  }
73 
74  AnalysisResultListT &ResultList = AnalysisResultLists[&IR];
75  ResultList.emplace_back(ID, P.run(IR, *this, ExtraArgs...));
76 
77  PI.runAfterAnalysis(P, IR);
78 
79  // P.run may have inserted elements into AnalysisResults and invalidated
80  // RI.
81  RI = AnalysisResults.find({ID, &IR});
82  assert(RI != AnalysisResults.end() && "we just inserted it!");
83 
84  RI->second = std::prev(ResultList.end());
85  }
86 
87  return *RI->second->second;
88 }
89 
90 template <typename IRUnitT, typename... ExtraArgTs>
92  IRUnitT &IR, const PreservedAnalyses &PA) {
93  // We're done if all analyses on this IR unit are preserved.
95  return;
96 
97  // Track whether each analysis's result is invalidated in
98  // IsResultInvalidated.
99  SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated;
100  Invalidator Inv(IsResultInvalidated, AnalysisResults);
101  AnalysisResultListT &ResultsList = AnalysisResultLists[&IR];
102  for (auto &AnalysisResultPair : ResultsList) {
103  // This is basically the same thing as Invalidator::invalidate, but we
104  // can't call it here because we're operating on the type-erased result.
105  // Moreover if we instead called invalidate() directly, it would do an
106  // unnecessary look up in ResultsList.
107  AnalysisKey *ID = AnalysisResultPair.first;
108  auto &Result = *AnalysisResultPair.second;
109 
110  auto IMapI = IsResultInvalidated.find(ID);
111  if (IMapI != IsResultInvalidated.end())
112  // This result was already handled via the Invalidator.
113  continue;
114 
115  // Try to invalidate the result, giving it the Invalidator so it can
116  // recursively query for any dependencies it has and record the result.
117  // Note that we cannot reuse 'IMapI' here or pre-insert the ID, as
118  // Result.invalidate may insert things into the map, invalidating our
119  // iterator.
120  bool Inserted =
121  IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, Inv)}).second;
122  (void)Inserted;
123  assert(Inserted && "Should never have already inserted this ID, likely "
124  "indicates a cycle!");
125  }
126 
127  // Now erase the results that were marked above as invalidated.
128  if (!IsResultInvalidated.empty()) {
129  for (auto I = ResultsList.begin(), E = ResultsList.end(); I != E;) {
130  AnalysisKey *ID = I->first;
131  if (!IsResultInvalidated.lookup(ID)) {
132  ++I;
133  continue;
134  }
135 
136  if (DebugLogging)
137  dbgs() << "Invalidating analysis: " << this->lookUpPass(ID).name()
138  << " on " << IR.getName() << "\n";
139 
140  I = ResultsList.erase(I);
141  AnalysisResults.erase({ID, &IR});
142  }
143  }
144 
145  if (ResultsList.empty())
146  AnalysisResultLists.erase(&IR);
147 }
148 } // end namespace llvm
149 
150 #endif // LLVM_IR_PASSMANAGERIMPL_H
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
This class represents lattice values for constants.
Definition: AllocatorList.h:23
llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, 4, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::lookup
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...
Definition: DenseMap.h:197
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::PassInstrumentation::runBeforeAnalysis
void runBeforeAnalysis(const PassT &Analysis, const IRUnitT &IR) const
BeforeAnalysis instrumentation point - takes Analysis instance to be executed and constant reference ...
Definition: PassInstrumentation.h:244
llvm::AllAnalysesOn
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: PassManager.h:93
llvm::SmallDenseMap
Definition: DenseMap.h:880
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
clear
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:233
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
IR
Statically lint checks LLVM IR
Definition: Lint.cpp:742
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, 4, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::PassInstrumentation
This class provides instrumentation entry points for the Pass Manager, doing calls to callbacks regis...
Definition: PassInstrumentation.h:161
llvm::PreservedAnalyses::allAnalysesInSetPreserved
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Definition: PassManager.h:338
llvm::PassInstrumentation::runAfterAnalysis
void runAfterAnalysis(const PassT &Analysis, const IRUnitT &IR) const
AfterAnalysis instrumentation point - takes Analysis instance that has just been executed and constan...
Definition: PassInstrumentation.h:253
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::AnalysisManager::AnalysisManager
AnalysisManager(bool DebugLogging=false)
Construct an empty analysis manager.
Definition: PassManagerImpl.h:23
llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, 4, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:207
llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, 4, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::empty
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:97
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, 4, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end
iterator end()
Definition: DenseMap.h:83
PassManager.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:43
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37