LLVM  16.0.0git
PassManager.cpp
Go to the documentation of this file.
1 //===- PassManager.cpp - Infrastructure for managing & running IR passes --===//
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 #include "llvm/IR/PassManager.h"
10 #include "llvm/ADT/DenseMapInfo.h"
11 #include "llvm/ADT/Optional.h"
13 #include <optional>
14 
15 using namespace llvm;
16 
17 namespace llvm {
18 // Explicit template instantiations and specialization defininitions for core
19 // template typedefs.
20 template class AllAnalysesOn<Module>;
21 template class AllAnalysesOn<Function>;
22 template class PassManager<Module>;
23 template class PassManager<Function>;
24 template class AnalysisManager<Module>;
25 template class AnalysisManager<Function>;
28 
29 template <>
31  Module &M, const PreservedAnalyses &PA,
33  // If literally everything is preserved, we're done.
34  if (PA.areAllPreserved())
35  return false; // This is still a valid proxy.
36 
37  // If this proxy isn't marked as preserved, then even if the result remains
38  // valid, the key itself may no longer be valid, so we clear everything.
39  //
40  // Note that in order to preserve this proxy, a module pass must ensure that
41  // the FAM has been completely updated to handle the deletion of functions.
42  // Specifically, any FAM-cached results for those functions need to have been
43  // forcibly cleared. When preserved, this proxy will only invalidate results
44  // cached on functions *still in the module* at the end of the module pass.
46  if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
47  InnerAM->clear();
48  return true;
49  }
50 
51  // Directly check if the relevant set is preserved.
52  bool AreFunctionAnalysesPreserved =
54 
55  // Now walk all the functions to see if any inner analysis invalidation is
56  // necessary.
57  for (Function &F : M) {
58  std::optional<PreservedAnalyses> FunctionPA;
59 
60  // Check to see whether the preserved set needs to be pruned based on
61  // module-level analysis invalidation that triggers deferred invalidation
62  // registered with the outer analysis manager proxy for this function.
63  if (auto *OuterProxy =
64  InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))
65  for (const auto &OuterInvalidationPair :
66  OuterProxy->getOuterInvalidations()) {
67  AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
68  const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
69  if (Inv.invalidate(OuterAnalysisID, M, PA)) {
70  if (!FunctionPA)
71  FunctionPA = PA;
72  for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
73  FunctionPA->abandon(InnerAnalysisID);
74  }
75  }
76 
77  // Check if we needed a custom PA set, and if so we'll need to run the
78  // inner invalidation.
79  if (FunctionPA) {
80  InnerAM->invalidate(F, *FunctionPA);
81  continue;
82  }
83 
84  // Otherwise we only need to do invalidation if the original PA set didn't
85  // preserve all function analyses.
86  if (!AreFunctionAnalysesPreserved)
87  InnerAM->invalidate(F, PA);
88  }
89 
90  // Return false to indicate that this result is still a valid proxy.
91  return false;
92 }
93 } // namespace llvm
94 
96  raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
97  OS << "function";
98  if (EagerlyInvalidate)
99  OS << "<eager-inv>";
100  OS << "(";
101  Pass->printPipeline(OS, MapClassName2PassName);
102  OS << ")";
103 }
104 
106  ModuleAnalysisManager &AM) {
109 
110  // Request PassInstrumentation from analysis manager, will use it to run
111  // instrumenting callbacks for the passes later.
113 
115  for (Function &F : M) {
116  if (F.isDeclaration())
117  continue;
118 
119  // Check the PassInstrumentation's BeforePass callbacks before running the
120  // pass, skip its execution completely if asked to (callback returns
121  // false).
122  if (!PI.runBeforePass<Function>(*Pass, F))
123  continue;
124 
125  PreservedAnalyses PassPA = Pass->run(F, FAM);
126  PI.runAfterPass(*Pass, F, PassPA);
127 
128  // We know that the function pass couldn't have invalidated any other
129  // function's analyses (that's the contract of a function pass), so
130  // directly handle the function analysis manager's invalidation here.
131  FAM.invalidate(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);
132 
133  // Then intersect the preserved set so that invalidation of module
134  // analyses will eventually occur when the module pass completes.
135  PA.intersect(std::move(PassPA));
136  }
137 
138  // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)
139  // the function passes we ran didn't add or remove any functions.
140  //
141  // We also preserve all analyses on Functions, because we did all the
142  // invalidation we needed to do above.
143  PA.preserveSet<AllAnalysesOn<Function>>();
144  PA.preserve<FunctionAnalysisManagerModuleProxy>();
145  return PA;
146 }
147 
148 AnalysisSetKey CFGAnalyses::SetKey;
149 
150 AnalysisSetKey PreservedAnalyses::AllAnalysesKey;
llvm::ModuleToFunctionPassAdaptor::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Runs the function pass across every function in the module.
Definition: PassManager.cpp:105
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm::OuterAnalysisManagerProxy
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:1056
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
Optional.h
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
llvm::Function
Definition: Function.h:60
llvm::AnalysisManager::Invalidator::invalidate
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition: PassManager.h:679
llvm::AnalysisManager::invalidate
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
Definition: PassManagerImpl.h:89
llvm::AllAnalysesOn
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: PassManager.h:90
llvm::PreservedAnalyses::abandon
void abandon()
Mark an analysis as abandoned.
Definition: PassManager.h:206
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
F
#define F(x, y, z)
Definition: MD5.cpp:55
PassManagerImpl.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:661
llvm::AnalysisSetKey
A special type used to provide an address that identifies a set of related analyses.
Definition: PassManager.h:79
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::PassInstrumentation::runAfterPass
void runAfterPass(const PassT &Pass, const IRUnitT &IR, const PreservedAnalyses &PA) const
AfterPass instrumentation point - takes Pass instance that has just been executed and constant refere...
Definition: PassInstrumentation.h:254
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:69
llvm::InnerAnalysisManagerProxy::Result::invalidate
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA, typename AnalysisManager< IRUnitT, ExtraArgTs... >::Invalidator &Inv)
Handler for invalidation of the outer IR unit, IRUnitT.
llvm::PassInstrumentation
This class provides instrumentation entry points for the Pass Manager, doing calls to callbacks regis...
Definition: PassInstrumentation.h:192
llvm::PreservedAnalyses::allAnalysesInSetPreserved
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Definition: PassManager.h:335
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:66
llvm::ModuleToFunctionPassAdaptor::printPipeline
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition: PassManager.cpp:95
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::PreservedAnalyses::areAllPreserved
bool areAllPreserved() const
Test whether all analyses are preserved (and none are abandoned).
Definition: PassManager.h:327
llvm::PassManager< Module >
llvm::PassInstrumentation::runBeforePass
bool runBeforePass(const PassT &Pass, const IRUnitT &IR) const
BeforePass instrumentation point - takes Pass instance to be executed and constant reference to IR it...
Definition: PassInstrumentation.h:229
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
PassManager.h
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
DenseMapInfo.h
llvm::PreservedAnalyses::getChecker
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: PassManager.h:310
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::PassInstrumentationAnalysis
Pseudo-analysis pass that exposes the PassInstrumentation to pass managers.
Definition: PassManager.h:594
llvm::InnerAnalysisManagerProxy
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:931