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

source code of llvm/lib/CodeGen/MachinePassManager.cpp