|
21 | 21 | #ifndef LLVM_CODEGEN_MACHINEPASSMANAGER_H
|
22 | 22 | #define LLVM_CODEGEN_MACHINEPASSMANAGER_H
|
23 | 23 |
|
| 24 | +#include "llvm/ADT/DenseMap.h" |
24 | 25 | #include "llvm/ADT/FunctionExtras.h"
|
| 26 | +#include "llvm/ADT/STLExtras.h" |
25 | 27 | #include "llvm/ADT/SmallVector.h"
|
26 | 28 | #include "llvm/CodeGen/MachineFunction.h"
|
| 29 | +#include "llvm/IR/Function.h" |
27 | 30 | #include "llvm/IR/PassManager.h"
|
28 | 31 | #include "llvm/IR/PassManagerInternal.h"
|
| 32 | +#include "llvm/Pass.h" |
29 | 33 | #include "llvm/Support/Error.h"
|
| 34 | +#include <memory> |
| 35 | +#include <type_traits> |
| 36 | +#include <utility> |
30 | 37 |
|
31 | 38 | namespace llvm {
|
32 | 39 | class Module;
|
@@ -236,6 +243,79 @@ using MachineFunctionPassManager = PassManager<MachineFunction>;
|
236 | 243 | /// preserve.
|
237 | 244 | PreservedAnalyses getMachineFunctionPassPreservedAnalyses();
|
238 | 245 |
|
| 246 | +/// For migrating to new pass manager |
| 247 | +/// Provides a common interface to fetch analyses instead of doing it twice in |
| 248 | +/// the *LegacyPass::runOnMachineFunction and NPM Pass::run NPM analyses must |
| 249 | +/// have the LegacyWrapper type to indicate which legacy analysis to run. |
| 250 | +/// |
| 251 | +/// Outer analyses passes(Module or Function) can also be requested through |
| 252 | +/// `getAnalysis` or `getCachedAnalysis`. |
| 253 | +class MFAnalysisGetter { |
| 254 | +private: |
| 255 | + Pass *LegacyPass; |
| 256 | + MachineFunctionAnalysisManager *MFAM; |
| 257 | + |
| 258 | + template <typename T> |
| 259 | + using type_of_run = |
| 260 | + typename function_traits<decltype(&T::run)>::template arg_t<0>; |
| 261 | + |
| 262 | + template <typename T> |
| 263 | + static constexpr bool IsFunctionAnalysis = |
| 264 | + std::is_same_v<Function, type_of_run<T>>; |
| 265 | + |
| 266 | + template <typename T> |
| 267 | + static constexpr bool IsModuleAnalysis = |
| 268 | + std::is_same_v<Module, type_of_run<T>>; |
| 269 | + |
| 270 | +public: |
| 271 | + MFAnalysisGetter(Pass *LegacyPass) : LegacyPass(LegacyPass) {} |
| 272 | + MFAnalysisGetter(MachineFunctionAnalysisManager *MFAM) : MFAM(MFAM) {} |
| 273 | + |
| 274 | + /// Outer analyses requested from NPM will be cached results and can be null |
| 275 | + template <typename AnalysisT> |
| 276 | + typename AnalysisT::Result *getAnalysis(MachineFunction &MF) { |
| 277 | + if (MFAM) { |
| 278 | + // need a proxy to get the result for outer analyses |
| 279 | + // this can return null |
| 280 | + if constexpr (IsModuleAnalysis<AnalysisT>) |
| 281 | + return MFAM->getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF) |
| 282 | + .getCachedResult<AnalysisT>(*MF.getFunction().getParent()); |
| 283 | + else if constexpr (IsFunctionAnalysis<AnalysisT>) { |
| 284 | + return &MFAM->getResult<FunctionAnalysisManagerMachineFunctionProxy>(MF) |
| 285 | + .getManager() |
| 286 | + .getResult<AnalysisT>(MF.getFunction()); |
| 287 | + } |
| 288 | + return &MFAM->getResult<AnalysisT>(MF); |
| 289 | + } |
| 290 | + return &LegacyPass->getAnalysis<typename AnalysisT::LegacyWrapper>() |
| 291 | + .getResult(); |
| 292 | + } |
| 293 | + |
| 294 | + template <typename AnalysisT> |
| 295 | + typename AnalysisT::Result *getCachedAnalysis(MachineFunction &MF) { |
| 296 | + if (MFAM) { |
| 297 | + if constexpr (IsFunctionAnalysis<AnalysisT>) { |
| 298 | + return MFAM->getResult<FunctionAnalysisManagerMachineFunctionProxy>(MF) |
| 299 | + .getManager() |
| 300 | + .getCachedResult<AnalysisT>(MF.getFunction()); |
| 301 | + } else if constexpr (IsModuleAnalysis<AnalysisT>) |
| 302 | + return MFAM->getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF) |
| 303 | + .getCachedResult<AnalysisT>(*MF.getFunction().getParent()); |
| 304 | + |
| 305 | + return &MFAM->getCachedResult<AnalysisT>(MF); |
| 306 | + } |
| 307 | + |
| 308 | + if (auto *P = |
| 309 | + LegacyPass->getAnalysisIfAvailable<AnalysisT::LegacyWrapper>()) |
| 310 | + return &P->getResult(); |
| 311 | + return nullptr; |
| 312 | + } |
| 313 | + |
| 314 | + /// This is not intended to be used to invoke getAnalysis() |
| 315 | + Pass *getLegacyPass() const { return LegacyPass; } |
| 316 | + MachineFunctionAnalysisManager *getMFAM() const { return MFAM; } |
| 317 | +}; |
| 318 | + |
239 | 319 | } // end namespace llvm
|
240 | 320 |
|
241 | 321 | #endif // LLVM_CODEGEN_MACHINEPASSMANAGER_H
|
0 commit comments