ModuleInliner.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //===- ModuleInliner.cpp - Code related to module inliner -----------------===//
  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 implements the mechanics required to implement inlining without
  10. // missing any calls in the module level. It doesn't need any infromation about
  11. // SCC or call graph, which is different from the SCC inliner. The decisions of
  12. // which calls are profitable to inline are implemented elsewhere.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Transforms/IPO/ModuleInliner.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/ScopeExit.h"
  18. #include "llvm/ADT/SetVector.h"
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/Analysis/AssumptionCache.h"
  23. #include "llvm/Analysis/BlockFrequencyInfo.h"
  24. #include "llvm/Analysis/GlobalsModRef.h"
  25. #include "llvm/Analysis/InlineAdvisor.h"
  26. #include "llvm/Analysis/InlineCost.h"
  27. #include "llvm/Analysis/InlineOrder.h"
  28. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  29. #include "llvm/Analysis/ProfileSummaryInfo.h"
  30. #include "llvm/Analysis/TargetLibraryInfo.h"
  31. #include "llvm/Analysis/TargetTransformInfo.h"
  32. #include "llvm/IR/DebugLoc.h"
  33. #include "llvm/IR/DiagnosticInfo.h"
  34. #include "llvm/IR/Function.h"
  35. #include "llvm/IR/InstIterator.h"
  36. #include "llvm/IR/Instruction.h"
  37. #include "llvm/IR/Instructions.h"
  38. #include "llvm/IR/IntrinsicInst.h"
  39. #include "llvm/IR/Metadata.h"
  40. #include "llvm/IR/Module.h"
  41. #include "llvm/IR/PassManager.h"
  42. #include "llvm/IR/User.h"
  43. #include "llvm/IR/Value.h"
  44. #include "llvm/Support/CommandLine.h"
  45. #include "llvm/Support/Debug.h"
  46. #include "llvm/Support/raw_ostream.h"
  47. #include "llvm/Transforms/Utils/CallPromotionUtils.h"
  48. #include "llvm/Transforms/Utils/Cloning.h"
  49. #include "llvm/Transforms/Utils/Local.h"
  50. #include "llvm/Transforms/Utils/ModuleUtils.h"
  51. #include <cassert>
  52. #include <functional>
  53. using namespace llvm;
  54. #define DEBUG_TYPE "module-inline"
  55. STATISTIC(NumInlined, "Number of functions inlined");
  56. STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
  57. static cl::opt<bool> InlineEnablePriorityOrder(
  58. "module-inline-enable-priority-order", cl::Hidden, cl::init(true),
  59. cl::desc("Enable the priority inline order for the module inliner"));
  60. /// Return true if the specified inline history ID
  61. /// indicates an inline history that includes the specified function.
  62. static bool inlineHistoryIncludes(
  63. Function *F, int InlineHistoryID,
  64. const SmallVectorImpl<std::pair<Function *, int>> &InlineHistory) {
  65. while (InlineHistoryID != -1) {
  66. assert(unsigned(InlineHistoryID) < InlineHistory.size() &&
  67. "Invalid inline history ID");
  68. if (InlineHistory[InlineHistoryID].first == F)
  69. return true;
  70. InlineHistoryID = InlineHistory[InlineHistoryID].second;
  71. }
  72. return false;
  73. }
  74. InlineAdvisor &ModuleInlinerPass::getAdvisor(const ModuleAnalysisManager &MAM,
  75. FunctionAnalysisManager &FAM,
  76. Module &M) {
  77. if (OwnedAdvisor)
  78. return *OwnedAdvisor;
  79. auto *IAA = MAM.getCachedResult<InlineAdvisorAnalysis>(M);
  80. if (!IAA) {
  81. // It should still be possible to run the inliner as a stand-alone module
  82. // pass, for test scenarios. In that case, we default to the
  83. // DefaultInlineAdvisor, which doesn't need to keep state between module
  84. // pass runs. It also uses just the default InlineParams. In this case, we
  85. // need to use the provided FAM, which is valid for the duration of the
  86. // inliner pass, and thus the lifetime of the owned advisor. The one we
  87. // would get from the MAM can be invalidated as a result of the inliner's
  88. // activity.
  89. OwnedAdvisor = std::make_unique<DefaultInlineAdvisor>(M, FAM, Params);
  90. return *OwnedAdvisor;
  91. }
  92. assert(IAA->getAdvisor() &&
  93. "Expected a present InlineAdvisorAnalysis also have an "
  94. "InlineAdvisor initialized");
  95. return *IAA->getAdvisor();
  96. }
  97. static bool isKnownLibFunction(Function &F, TargetLibraryInfo &TLI) {
  98. LibFunc LF;
  99. // Either this is a normal library function or a "vectorizable"
  100. // function. Not using the VFDatabase here because this query
  101. // is related only to libraries handled via the TLI.
  102. return TLI.getLibFunc(F, LF) ||
  103. TLI.isKnownVectorFunctionInLibrary(F.getName());
  104. }
  105. PreservedAnalyses ModuleInlinerPass::run(Module &M,
  106. ModuleAnalysisManager &MAM) {
  107. LLVM_DEBUG(dbgs() << "---- Module Inliner is Running ---- \n");
  108. auto &IAA = MAM.getResult<InlineAdvisorAnalysis>(M);
  109. if (!IAA.tryCreate(Params, Mode, {})) {
  110. M.getContext().emitError(
  111. "Could not setup Inlining Advisor for the requested "
  112. "mode and/or options");
  113. return PreservedAnalyses::all();
  114. }
  115. bool Changed = false;
  116. ProfileSummaryInfo *PSI = MAM.getCachedResult<ProfileSummaryAnalysis>(M);
  117. FunctionAnalysisManager &FAM =
  118. MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  119. auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
  120. return FAM.getResult<TargetLibraryAnalysis>(F);
  121. };
  122. InlineAdvisor &Advisor = getAdvisor(MAM, FAM, M);
  123. Advisor.onPassEntry();
  124. auto AdvisorOnExit = make_scope_exit([&] { Advisor.onPassExit(); });
  125. // In the module inliner, a priority-based worklist is used for calls across
  126. // the entire Module. With this module inliner, the inline order is not
  127. // limited to bottom-up order. More globally scope inline order is enabled.
  128. // Also, the inline deferral logic become unnecessary in this module inliner.
  129. // It is possible to use other priority heuristics, e.g. profile-based
  130. // heuristic.
  131. //
  132. // TODO: Here is a huge amount duplicate code between the module inliner and
  133. // the SCC inliner, which need some refactoring.
  134. std::unique_ptr<InlineOrder<std::pair<CallBase *, int>>> Calls;
  135. if (InlineEnablePriorityOrder)
  136. Calls = std::make_unique<PriorityInlineOrder<InlineSizePriority>>();
  137. else
  138. Calls = std::make_unique<DefaultInlineOrder<std::pair<CallBase *, int>>>();
  139. assert(Calls != nullptr && "Expected an initialized InlineOrder");
  140. // Populate the initial list of calls in this module.
  141. for (Function &F : M) {
  142. auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
  143. // We want to generally process call sites top-down in order for
  144. // simplifications stemming from replacing the call with the returned value
  145. // after inlining to be visible to subsequent inlining decisions.
  146. // FIXME: Using instructions sequence is a really bad way to do this.
  147. // Instead we should do an actual RPO walk of the function body.
  148. for (Instruction &I : instructions(F))
  149. if (auto *CB = dyn_cast<CallBase>(&I))
  150. if (Function *Callee = CB->getCalledFunction()) {
  151. if (!Callee->isDeclaration())
  152. Calls->push({CB, -1});
  153. else if (!isa<IntrinsicInst>(I)) {
  154. using namespace ore;
  155. setInlineRemark(*CB, "unavailable definition");
  156. ORE.emit([&]() {
  157. return OptimizationRemarkMissed(DEBUG_TYPE, "NoDefinition", &I)
  158. << NV("Callee", Callee) << " will not be inlined into "
  159. << NV("Caller", CB->getCaller())
  160. << " because its definition is unavailable"
  161. << setIsVerbose();
  162. });
  163. }
  164. }
  165. }
  166. if (Calls->empty())
  167. return PreservedAnalyses::all();
  168. // When inlining a callee produces new call sites, we want to keep track of
  169. // the fact that they were inlined from the callee. This allows us to avoid
  170. // infinite inlining in some obscure cases. To represent this, we use an
  171. // index into the InlineHistory vector.
  172. SmallVector<std::pair<Function *, int>, 16> InlineHistory;
  173. // Track a set vector of inlined callees so that we can augment the caller
  174. // with all of their edges in the call graph before pruning out the ones that
  175. // got simplified away.
  176. SmallSetVector<Function *, 4> InlinedCallees;
  177. // Track the dead functions to delete once finished with inlining calls. We
  178. // defer deleting these to make it easier to handle the call graph updates.
  179. SmallVector<Function *, 4> DeadFunctions;
  180. // Loop forward over all of the calls.
  181. while (!Calls->empty()) {
  182. // We expect the calls to typically be batched with sequences of calls that
  183. // have the same caller, so we first set up some shared infrastructure for
  184. // this caller. We also do any pruning we can at this layer on the caller
  185. // alone.
  186. Function &F = *Calls->front().first->getCaller();
  187. LLVM_DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n"
  188. << " Function size: " << F.getInstructionCount()
  189. << "\n");
  190. auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
  191. return FAM.getResult<AssumptionAnalysis>(F);
  192. };
  193. // Now process as many calls as we have within this caller in the sequence.
  194. // We bail out as soon as the caller has to change so we can
  195. // prepare the context of that new caller.
  196. bool DidInline = false;
  197. while (!Calls->empty() && Calls->front().first->getCaller() == &F) {
  198. auto P = Calls->pop();
  199. CallBase *CB = P.first;
  200. const int InlineHistoryID = P.second;
  201. Function &Callee = *CB->getCalledFunction();
  202. if (InlineHistoryID != -1 &&
  203. inlineHistoryIncludes(&Callee, InlineHistoryID, InlineHistory)) {
  204. setInlineRemark(*CB, "recursive");
  205. continue;
  206. }
  207. auto Advice = Advisor.getAdvice(*CB, /*OnlyMandatory*/ false);
  208. // Check whether we want to inline this callsite.
  209. if (!Advice->isInliningRecommended()) {
  210. Advice->recordUnattemptedInlining();
  211. continue;
  212. }
  213. // Setup the data structure used to plumb customization into the
  214. // `InlineFunction` routine.
  215. InlineFunctionInfo IFI(
  216. /*cg=*/nullptr, GetAssumptionCache, PSI,
  217. &FAM.getResult<BlockFrequencyAnalysis>(*(CB->getCaller())),
  218. &FAM.getResult<BlockFrequencyAnalysis>(Callee));
  219. InlineResult IR =
  220. InlineFunction(*CB, IFI, &FAM.getResult<AAManager>(*CB->getCaller()));
  221. if (!IR.isSuccess()) {
  222. Advice->recordUnsuccessfulInlining(IR);
  223. continue;
  224. }
  225. DidInline = true;
  226. InlinedCallees.insert(&Callee);
  227. ++NumInlined;
  228. LLVM_DEBUG(dbgs() << " Size after inlining: "
  229. << F.getInstructionCount() << "\n");
  230. // Add any new callsites to defined functions to the worklist.
  231. if (!IFI.InlinedCallSites.empty()) {
  232. int NewHistoryID = InlineHistory.size();
  233. InlineHistory.push_back({&Callee, InlineHistoryID});
  234. for (CallBase *ICB : reverse(IFI.InlinedCallSites)) {
  235. Function *NewCallee = ICB->getCalledFunction();
  236. if (!NewCallee) {
  237. // Try to promote an indirect (virtual) call without waiting for
  238. // the post-inline cleanup and the next DevirtSCCRepeatedPass
  239. // iteration because the next iteration may not happen and we may
  240. // miss inlining it.
  241. if (tryPromoteCall(*ICB))
  242. NewCallee = ICB->getCalledFunction();
  243. }
  244. if (NewCallee)
  245. if (!NewCallee->isDeclaration())
  246. Calls->push({ICB, NewHistoryID});
  247. }
  248. }
  249. // Merge the attributes based on the inlining.
  250. AttributeFuncs::mergeAttributesForInlining(F, Callee);
  251. // For local functions, check whether this makes the callee trivially
  252. // dead. In that case, we can drop the body of the function eagerly
  253. // which may reduce the number of callers of other functions to one,
  254. // changing inline cost thresholds.
  255. bool CalleeWasDeleted = false;
  256. if (Callee.hasLocalLinkage()) {
  257. // To check this we also need to nuke any dead constant uses (perhaps
  258. // made dead by this operation on other functions).
  259. Callee.removeDeadConstantUsers();
  260. // if (Callee.use_empty() && !CG.isLibFunction(Callee)) {
  261. if (Callee.use_empty() && !isKnownLibFunction(Callee, GetTLI(Callee))) {
  262. Calls->erase_if([&](const std::pair<CallBase *, int> &Call) {
  263. return Call.first->getCaller() == &Callee;
  264. });
  265. // Clear the body and queue the function itself for deletion when we
  266. // finish inlining.
  267. // Note that after this point, it is an error to do anything other
  268. // than use the callee's address or delete it.
  269. Callee.dropAllReferences();
  270. assert(!is_contained(DeadFunctions, &Callee) &&
  271. "Cannot put cause a function to become dead twice!");
  272. DeadFunctions.push_back(&Callee);
  273. CalleeWasDeleted = true;
  274. }
  275. }
  276. if (CalleeWasDeleted)
  277. Advice->recordInliningWithCalleeDeleted();
  278. else
  279. Advice->recordInlining();
  280. }
  281. if (!DidInline)
  282. continue;
  283. Changed = true;
  284. InlinedCallees.clear();
  285. }
  286. // Now that we've finished inlining all of the calls across this module,
  287. // delete all of the trivially dead functions.
  288. //
  289. // Note that this walks a pointer set which has non-deterministic order but
  290. // that is OK as all we do is delete things and add pointers to unordered
  291. // sets.
  292. for (Function *DeadF : DeadFunctions) {
  293. // Clear out any cached analyses.
  294. FAM.clear(*DeadF, DeadF->getName());
  295. // And delete the actual function from the module.
  296. M.getFunctionList().erase(DeadF);
  297. ++NumDeleted;
  298. }
  299. if (!Changed)
  300. return PreservedAnalyses::all();
  301. return PreservedAnalyses::none();
  302. }