MachinePassManager.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===---------- MachinePassManager.cpp ------------------------------------===//
  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. // This file contains the pass management machinery for machine functions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/MachinePassManager.h"
  13. #include "llvm/CodeGen/MachineFunction.h"
  14. #include "llvm/CodeGen/MachineModuleInfo.h"
  15. #include "llvm/IR/PassManagerImpl.h"
  16. using namespace llvm;
  17. namespace llvm {
  18. template class AllAnalysesOn<MachineFunction>;
  19. template class AnalysisManager<MachineFunction>;
  20. template class PassManager<MachineFunction>;
  21. Error MachineFunctionPassManager::run(Module &M,
  22. MachineFunctionAnalysisManager &MFAM) {
  23. // MachineModuleAnalysis is a module analysis pass that is never invalidated
  24. // because we don't run any module pass in codegen pipeline. This is very
  25. // important because the codegen state is stored in MMI which is the analysis
  26. // result of MachineModuleAnalysis. MMI should not be recomputed.
  27. auto &MMI = MFAM.getResult<MachineModuleAnalysis>(M);
  28. (void)RequireCodeGenSCCOrder;
  29. assert(!RequireCodeGenSCCOrder && "not implemented");
  30. // Add a PIC to verify machine functions.
  31. if (VerifyMachineFunction) {
  32. PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(M);
  33. // No need to pop this callback later since MIR pipeline is flat which means
  34. // current pipeline is the top-level pipeline. Callbacks are not used after
  35. // current pipeline.
  36. PI.pushBeforeNonSkippedPassCallback([&MFAM](StringRef PassID, Any IR) {
  37. assert(any_isa<const MachineFunction *>(IR));
  38. const MachineFunction *MF = any_cast<const MachineFunction *>(IR);
  39. assert(MF && "Machine function should be valid for printing");
  40. std::string Banner = std::string("After ") + std::string(PassID);
  41. verifyMachineFunction(&MFAM, Banner, *MF);
  42. });
  43. }
  44. for (auto &F : InitializationFuncs) {
  45. if (auto Err = F(M, MFAM))
  46. return Err;
  47. }
  48. unsigned Idx = 0;
  49. size_t Size = Passes.size();
  50. do {
  51. // Run machine module passes
  52. for (; MachineModulePasses.count(Idx) && Idx != Size; ++Idx) {
  53. if (auto Err = MachineModulePasses.at(Idx)(M, MFAM))
  54. return Err;
  55. }
  56. // Finish running all passes.
  57. if (Idx == Size)
  58. break;
  59. // Run machine function passes
  60. // Get index range of machine function passes.
  61. unsigned Begin = Idx;
  62. for (; !MachineModulePasses.count(Idx) && Idx != Size; ++Idx)
  63. ;
  64. for (Function &F : M) {
  65. // Do not codegen any 'available_externally' functions at all, they have
  66. // definitions outside the translation unit.
  67. if (F.hasAvailableExternallyLinkage())
  68. continue;
  69. MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
  70. PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(MF);
  71. for (unsigned I = Begin, E = Idx; I != E; ++I) {
  72. auto *P = Passes[I].get();
  73. if (!PI.runBeforePass<MachineFunction>(*P, MF))
  74. continue;
  75. // TODO: EmitSizeRemarks
  76. PreservedAnalyses PassPA = P->run(MF, MFAM);
  77. PI.runAfterPass(*P, MF, PassPA);
  78. MFAM.invalidate(MF, PassPA);
  79. }
  80. }
  81. } while (true);
  82. for (auto &F : FinalizationFuncs) {
  83. if (auto Err = F(M, MFAM))
  84. return Err;
  85. }
  86. return Error::success();
  87. }
  88. } // namespace llvm