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 // M is unused here 37 PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(M); 38 39 // Add a PIC to verify machine functions. 40 if (VerifyMachineFunction) { 41 // No need to pop this callback later since MIR pipeline is flat which means 42 // current pipeline is the top-level pipeline. Callbacks are not used after 43 // current pipeline. 44 PI.pushBeforeNonSkippedPassCallback([&MFAM](StringRef PassID, Any IR) { 45 assert(llvm::any_cast<const MachineFunction *>(&IR)); 46 const MachineFunction *MF = llvm::any_cast<const MachineFunction *>(IR); 47 assert(MF && "Machine function should be valid for printing"); 48 std::string Banner = std::string("After ") + std::string(PassID); 49 verifyMachineFunction(&MFAM, Banner, *MF); 50 }); 51 } 52 53 for (auto &F : InitializationFuncs) { 54 if (auto Err = F(M, MFAM)) 55 return Err; 56 } 57 58 unsigned Idx = 0; 59 size_t Size = Passes.size(); 60 do { 61 // Run machine module passes 62 for (; MachineModulePasses.count(Idx) && Idx != Size; ++Idx) { 63 if (!PI.runBeforePass<Module>(*Passes[Idx], M)) 64 continue; 65 if (auto Err = MachineModulePasses.at(Idx)(M, MFAM)) 66 return Err; 67 PI.runAfterPass(*Passes[Idx], M, PreservedAnalyses::all()); 68 } 69 70 // Finish running all passes. 71 if (Idx == Size) 72 break; 73 74 // Run machine function passes 75 76 // Get index range of machine function passes. 77 unsigned Begin = Idx; 78 for (; !MachineModulePasses.count(Idx) && Idx != Size; ++Idx) 79 ; 80 81 for (Function &F : M) { 82 // Do not codegen any 'available_externally' functions at all, they have 83 // definitions outside the translation unit. 84 if (F.hasAvailableExternallyLinkage()) 85 continue; 86 87 MachineFunction &MF = MMI.getOrCreateMachineFunction(F); 88 89 for (unsigned I = Begin, E = Idx; I != E; ++I) { 90 auto *P = Passes[I].get(); 91 92 if (!PI.runBeforePass<MachineFunction>(*P, MF)) 93 continue; 94 95 // TODO: EmitSizeRemarks 96 PreservedAnalyses PassPA = P->run(MF, MFAM); 97 MFAM.invalidate(MF, PassPA); 98 PI.runAfterPass(*P, MF, PassPA); 99 } 100 } 101 } while (true); 102 103 for (auto &F : FinalizationFuncs) { 104 if (auto Err = F(M, MFAM)) 105 return Err; 106 } 107 108 return Error::success(); 109 } 110 111 } // namespace llvm 112