1 //===- Inliner.h - Inliner pass and infrastructure --------------*- C++ -*-===// 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 #ifndef LLVM_TRANSFORMS_IPO_INLINER_H 10 #define LLVM_TRANSFORMS_IPO_INLINER_H 11 12 #include "llvm/Analysis/CGSCCPassManager.h" 13 #include "llvm/Analysis/InlineAdvisor.h" 14 #include "llvm/Analysis/InlineCost.h" 15 #include "llvm/Analysis/LazyCallGraph.h" 16 #include "llvm/Analysis/Utils/ImportedFunctionsInliningStatistics.h" 17 #include "llvm/IR/PassManager.h" 18 #include "llvm/Support/Compiler.h" 19 20 namespace llvm { 21 22 /// The inliner pass for the new pass manager. 23 /// 24 /// This pass wires together the inlining utilities and the inline cost 25 /// analysis into a CGSCC pass. It considers every call in every function in 26 /// the SCC and tries to inline if profitable. It can be tuned with a number of 27 /// parameters to control what cost model is used and what tradeoffs are made 28 /// when making the decision. 29 /// 30 /// It should be noted that the legacy inliners do considerably more than this 31 /// inliner pass does. They provide logic for manually merging allocas, and 32 /// doing considerable DCE including the DCE of dead functions. This pass makes 33 /// every attempt to be simpler. DCE of functions requires complex reasoning 34 /// about comdat groups, etc. Instead, it is expected that other more focused 35 /// passes be composed to achieve the same end result. 36 class InlinerPass : public PassInfoMixin<InlinerPass> { 37 public: 38 InlinerPass(bool OnlyMandatory = false, 39 ThinOrFullLTOPhase LTOPhase = ThinOrFullLTOPhase::None) OnlyMandatory(OnlyMandatory)40 : OnlyMandatory(OnlyMandatory), LTOPhase(LTOPhase) {} 41 InlinerPass(InlinerPass &&Arg) = default; 42 43 LLVM_ABI PreservedAnalyses run(LazyCallGraph::SCC &C, 44 CGSCCAnalysisManager &AM, LazyCallGraph &CG, 45 CGSCCUpdateResult &UR); 46 47 LLVM_ABI void 48 printPipeline(raw_ostream &OS, 49 function_ref<StringRef(StringRef)> MapClassName2PassName); 50 51 private: 52 InlineAdvisor &getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM, 53 FunctionAnalysisManager &FAM, Module &M); 54 std::unique_ptr<InlineAdvisor> OwnedAdvisor; 55 const bool OnlyMandatory; 56 const ThinOrFullLTOPhase LTOPhase; 57 }; 58 59 /// Module pass, wrapping the inliner pass. This works in conjunction with the 60 /// InlineAdvisorAnalysis to facilitate inlining decisions taking into account 61 /// module-wide state, that need to keep track of inter-inliner pass runs, for 62 /// a given module. An InlineAdvisor is configured and kept alive for the 63 /// duration of the ModuleInlinerWrapperPass::run. 64 class ModuleInlinerWrapperPass 65 : public PassInfoMixin<ModuleInlinerWrapperPass> { 66 public: 67 LLVM_ABI ModuleInlinerWrapperPass( 68 InlineParams Params = getInlineParams(), bool MandatoryFirst = true, 69 InlineContext IC = {}, 70 InliningAdvisorMode Mode = InliningAdvisorMode::Default, 71 unsigned MaxDevirtIterations = 0); 72 ModuleInlinerWrapperPass(ModuleInlinerWrapperPass &&Arg) = default; 73 74 LLVM_ABI PreservedAnalyses run(Module &, ModuleAnalysisManager &); 75 76 /// Allow adding more CGSCC passes, besides inlining. This should be called 77 /// before run is called, as part of pass pipeline building. getPM()78 CGSCCPassManager &getPM() { return PM; } 79 80 /// Add a module pass that runs before the CGSCC passes. addModulePass(T Pass)81 template <class T> void addModulePass(T Pass) { 82 MPM.addPass(std::move(Pass)); 83 } 84 85 /// Add a module pass that runs after the CGSCC passes. addLateModulePass(T Pass)86 template <class T> void addLateModulePass(T Pass) { 87 AfterCGMPM.addPass(std::move(Pass)); 88 } 89 90 LLVM_ABI void 91 printPipeline(raw_ostream &OS, 92 function_ref<StringRef(StringRef)> MapClassName2PassName); 93 94 private: 95 const InlineParams Params; 96 const InlineContext IC; 97 const InliningAdvisorMode Mode; 98 const unsigned MaxDevirtIterations; 99 // TODO: Clean this up so we only have one ModulePassManager. 100 CGSCCPassManager PM; 101 ModulePassManager MPM; 102 ModulePassManager AfterCGMPM; 103 }; 104 } // end namespace llvm 105 106 #endif // LLVM_TRANSFORMS_IPO_INLINER_H 107