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