[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / lib / CodeGen / MachinePassManager.cpp
blob476dc059d2b5d6d853106a384d90aa0d7c480f4a
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/MachinePassManager.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineModuleInfo.h"
16 #include "llvm/IR/PassManagerImpl.h"
18 using namespace llvm;
20 namespace llvm {
21 template class AllAnalysesOn<MachineFunction>;
22 template class AnalysisManager<MachineFunction>;
23 template class PassManager<MachineFunction>;
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);
33 (void)RequireCodeGenSCCOrder;
34 assert(!RequireCodeGenSCCOrder && "not implemented");
36 // Add a PIC to verify machine functions.
37 if (VerifyMachineFunction) {
38 PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(M);
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 });
52 for (auto &F : InitializationFuncs) {
53 if (auto Err = F(M, MFAM))
54 return Err;
57 unsigned Idx = 0;
58 size_t Size = Passes.size();
59 do {
60 // Run machine module passes
61 for (; MachineModulePasses.count(Idx) && Idx != Size; ++Idx) {
62 if (auto Err = MachineModulePasses.at(Idx)(M, MFAM))
63 return Err;
66 // Finish running all passes.
67 if (Idx == Size)
68 break;
70 // Run machine function passes
72 // Get index range of machine function passes.
73 unsigned Begin = Idx;
74 for (; !MachineModulePasses.count(Idx) && Idx != Size; ++Idx)
77 for (Function &F : M) {
78 // Do not codegen any 'available_externally' functions at all, they have
79 // definitions outside the translation unit.
80 if (F.hasAvailableExternallyLinkage())
81 continue;
83 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
84 PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(MF);
86 for (unsigned I = Begin, E = Idx; I != E; ++I) {
87 auto *P = Passes[I].get();
89 if (!PI.runBeforePass<MachineFunction>(*P, MF))
90 continue;
92 // TODO: EmitSizeRemarks
93 PreservedAnalyses PassPA = P->run(MF, MFAM);
94 PI.runAfterPass(*P, MF, PassPA);
95 MFAM.invalidate(MF, PassPA);
98 } while (true);
100 for (auto &F : FinalizationFuncs) {
101 if (auto Err = F(M, MFAM))
102 return Err;
105 return Error::success();
108 } // namespace llvm