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