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 #include <any> 19 20 using namespace llvm; 21 22 namespace llvm { 23 template class AllAnalysesOn<MachineFunction>; 24 template class AnalysisManager<MachineFunction>; 25 template class PassManager<MachineFunction>; 26 27 Error MachineFunctionPassManager::run(Module &M, 28 MachineFunctionAnalysisManager &MFAM) { 29 // MachineModuleAnalysis is a module analysis pass that is never invalidated 30 // because we don't run any module pass in codegen pipeline. This is very 31 // important because the codegen state is stored in MMI which is the analysis 32 // result of MachineModuleAnalysis. MMI should not be recomputed. 33 auto &MMI = MFAM.getResult<MachineModuleAnalysis>(M); 34 35 (void)RequireCodeGenSCCOrder; 36 assert(!RequireCodeGenSCCOrder && "not implemented"); 37 38 // Add a PIC to verify machine functions. 39 if (VerifyMachineFunction) { 40 PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(M); 41 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([&MFAM](StringRef PassID, std::any IR) { 46 assert(std::any_cast<const MachineFunction *>(&IR)); 47 const MachineFunction **MF = std::any_cast<const MachineFunction *>(&IR); 48 assert(MF && "Machine function should be valid for printing"); 49 std::string Banner = std::string("After ") + std::string(PassID); 50 verifyMachineFunction(&MFAM, Banner, **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(Idx) && Idx != Size; ++Idx) { 64 if (auto Err = MachineModulePasses.at(Idx)(M, MFAM)) 65 return Err; 66 } 67 68 // Finish running all passes. 69 if (Idx == Size) 70 break; 71 72 // Run machine function passes 73 74 // Get index range of machine function passes. 75 unsigned Begin = Idx; 76 for (; !MachineModulePasses.count(Idx) && Idx != Size; ++Idx) 77 ; 78 79 for (Function &F : M) { 80 // Do not codegen any 'available_externally' functions at all, they have 81 // definitions outside the translation unit. 82 if (F.hasAvailableExternallyLinkage()) 83 continue; 84 85 MachineFunction &MF = MMI.getOrCreateMachineFunction(F); 86 PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(MF); 87 88 for (unsigned I = Begin, E = Idx; I != E; ++I) { 89 auto *P = Passes[I].get(); 90 91 if (!PI.runBeforePass<MachineFunction>(*P, MF)) 92 continue; 93 94 // TODO: EmitSizeRemarks 95 PreservedAnalyses PassPA = P->run(MF, MFAM); 96 PI.runAfterPass(*P, MF, PassPA); 97 MFAM.invalidate(MF, PassPA); 98 } 99 } 100 } while (true); 101 102 for (auto &F : FinalizationFuncs) { 103 if (auto Err = F(M, MFAM)) 104 return Err; 105 } 106 107 return Error::success(); 108 } 109 110 } // namespace llvm 111