xref: /llvm-project/llvm/lib/CodeGen/MachinePassManager.cpp (revision 911565d1085d9447363fe8ad041817436c4998fe)
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 
13 #include "llvm/CodeGen/MachinePassManager.h"
14 #include "llvm/CodeGen/MachineModuleInfo.h"
15 #include "llvm/IR/PassManagerImpl.h"
16 
17 using namespace llvm;
18 
19 namespace llvm {
20 template class AllAnalysesOn<MachineFunction>;
21 template class AnalysisManager<MachineFunction>;
22 template class PassManager<MachineFunction>;
23 
24 Error MachineFunctionPassManager::run(Module &M,
25                                       MachineFunctionAnalysisManager &MFAM) {
26   // MachineModuleAnalysis is a module analysis pass that is never invalidated
27   // because we don't run any module pass in codegen pipeline. This is very
28   // important because the codegen state is stored in MMI which is the analysis
29   // result of MachineModuleAnalysis. MMI should not be recomputed.
30   auto &MMI = MFAM.getResult<MachineModuleAnalysis>(M);
31 
32   assert(!RequireCodeGenSCCOrder && "not implemented");
33 
34   if (DebugLogging) {
35     dbgs() << "Starting " << getTypeName<MachineFunction>()
36            << " pass manager run.\n";
37   }
38 
39   for (auto &F : InitializationFuncs) {
40     if (auto Err = F(M, MFAM))
41       return Err;
42   }
43 
44   unsigned Idx = 0;
45   size_t Size = Passes.size();
46   do {
47     // Run machine module passes
48     for (; MachineModulePasses.count(Idx) && Idx != Size; ++Idx) {
49       if (DebugLogging)
50         dbgs() << "Running pass: " << Passes[Idx]->name() << " on "
51                << M.getName() << '\n';
52       if (auto Err = MachineModulePasses.at(Idx)(M, MFAM))
53         return Err;
54     }
55 
56     // Finish running all passes.
57     if (Idx == Size)
58       break;
59 
60     // Run machine function passes
61 
62     // Get index range of machine function passes.
63     unsigned Begin = Idx;
64     for (; !MachineModulePasses.count(Idx) && Idx != Size; ++Idx)
65       ;
66 
67     for (Function &F : M) {
68       // Do not codegen any 'available_externally' functions at all, they have
69       // definitions outside the translation unit.
70       if (F.hasAvailableExternallyLinkage())
71         continue;
72 
73       MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
74       PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(MF);
75 
76       for (unsigned I = Begin, E = Idx; I != E; ++I) {
77         auto *P = Passes[I].get();
78 
79         if (!PI.runBeforePass<MachineFunction>(*P, MF))
80           continue;
81 
82         // TODO: EmitSizeRemarks
83         PreservedAnalyses PassPA = P->run(MF, MFAM);
84         PI.runAfterPass(*P, MF);
85         MFAM.invalidate(MF, PassPA);
86       }
87     }
88   } while (true);
89 
90   for (auto &F : FinalizationFuncs) {
91     if (auto Err = F(M, MFAM))
92       return Err;
93   }
94 
95   if (DebugLogging) {
96     dbgs() << "Finished " << getTypeName<MachineFunction>()
97            << " pass manager run.\n";
98   }
99 
100   return Error::success();
101 }
102 
103 } // namespace llvm
104