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/MachineModuleInfo.h" 15 #include "llvm/IR/PassManagerImpl.h" 16 17 using namespace llvm; 18 19 namespace llvm { 20 template class AllAnalysesOn<MachineFunction>; 21 template class AnalysisManager<MachineFunction>; 22 template class PassManager<MachineFunction>; 23 24 Error MachineFunctionPassManager::run(Module &M, 25 MachineFunctionAnalysisManager &MFAM) { 26 // MachineModuleAnalysis is a module analysis pass that is never invalidated 27 // because we don't run any module pass in codegen pipeline. This is very 28 // important because the codegen state is stored in MMI which is the analysis 29 // result of MachineModuleAnalysis. MMI should not be recomputed. 30 auto &MMI = MFAM.getResult<MachineModuleAnalysis>(M); 31 32 (void)RequireCodeGenSCCOrder; 33 assert(!RequireCodeGenSCCOrder && "not implemented"); 34 35 if (DebugLogging) { 36 dbgs() << "Starting " << getTypeName<MachineFunction>() 37 << " pass manager run.\n"; 38 } 39 40 for (auto &F : InitializationFuncs) { 41 if (auto Err = F(M, MFAM)) 42 return Err; 43 } 44 45 unsigned Idx = 0; 46 size_t Size = Passes.size(); 47 do { 48 // Run machine module passes 49 for (; MachineModulePasses.count(Idx) && Idx != Size; ++Idx) { 50 if (DebugLogging) 51 dbgs() << "Running pass: " << Passes[Idx]->name() << " on " 52 << M.getName() << '\n'; 53 if (auto Err = MachineModulePasses.at(Idx)(M, MFAM)) 54 return Err; 55 } 56 57 // Finish running all passes. 58 if (Idx == Size) 59 break; 60 61 // Run machine function passes 62 63 // Get index range of machine function passes. 64 unsigned Begin = Idx; 65 for (; !MachineModulePasses.count(Idx) && Idx != Size; ++Idx) 66 ; 67 68 for (Function &F : M) { 69 // Do not codegen any 'available_externally' functions at all, they have 70 // definitions outside the translation unit. 71 if (F.hasAvailableExternallyLinkage()) 72 continue; 73 74 MachineFunction &MF = MMI.getOrCreateMachineFunction(F); 75 PassInstrumentation PI = MFAM.getResult<PassInstrumentationAnalysis>(MF); 76 77 for (unsigned I = Begin, E = Idx; I != E; ++I) { 78 auto *P = Passes[I].get(); 79 80 if (!PI.runBeforePass<MachineFunction>(*P, MF)) 81 continue; 82 83 // TODO: EmitSizeRemarks 84 PreservedAnalyses PassPA = P->run(MF, MFAM); 85 PI.runAfterPass(*P, MF); 86 MFAM.invalidate(MF, PassPA); 87 } 88 } 89 } while (true); 90 91 for (auto &F : FinalizationFuncs) { 92 if (auto Err = F(M, MFAM)) 93 return Err; 94 } 95 96 if (DebugLogging) { 97 dbgs() << "Finished " << getTypeName<MachineFunction>() 98 << " pass manager run.\n"; 99 } 100 101 return Error::success(); 102 } 103 104 } // namespace llvm 105