1 //===- InlineSimple.cpp - Code to perform simple function inlining --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements bottom-up inlining of functions into callees.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/AssumptionCache.h"
15 #include "llvm/Analysis/InlineCost.h"
16 #include "llvm/Analysis/ProfileSummaryInfo.h"
17 #include "llvm/Analysis/TargetLibraryInfo.h"
18 #include "llvm/Analysis/TargetTransformInfo.h"
19 #include "llvm/IR/CallSite.h"
20 #include "llvm/IR/CallingConv.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Type.h"
25 #include "llvm/Transforms/IPO.h"
26 #include "llvm/Transforms/IPO/Inliner.h"
30 #define DEBUG_TYPE "inline"
34 /// \brief Actual inliner pass implementation.
36 /// The common implementation of the inlining logic is shared between this
37 /// inliner pass and the always inliner pass. The two passes use different cost
38 /// analyses to determine when to inline.
39 class SimpleInliner : public LegacyInlinerBase {
44 SimpleInliner() : LegacyInlinerBase(ID), Params(llvm::getInlineParams()) {
45 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
48 explicit SimpleInliner(InlineParams Params)
49 : LegacyInlinerBase(ID), Params(std::move(Params)) {
50 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
53 static char ID; // Pass identification, replacement for typeid
55 InlineCost getInlineCost(CallSite CS) override {
56 Function *Callee = CS.getCalledFunction();
57 TargetTransformInfo &TTI = TTIWP->getTTI(*Callee);
59 bool RemarksEnabled = false;
60 const auto &BBs = CS.getCaller()->getBasicBlockList();
62 auto DI = OptimizationRemark(DEBUG_TYPE, "", DebugLoc(), &BBs.front());
64 RemarksEnabled = true;
66 OptimizationRemarkEmitter ORE(CS.getCaller());
68 std::function<AssumptionCache &(Function &)> GetAssumptionCache =
69 [&](Function &F) -> AssumptionCache & {
70 return ACT->getAssumptionCache(F);
72 return llvm::getInlineCost(CS, Params, TTI, GetAssumptionCache,
74 RemarksEnabled ? &ORE : nullptr);
77 bool runOnSCC(CallGraphSCC &SCC) override;
78 void getAnalysisUsage(AnalysisUsage &AU) const override;
81 TargetTransformInfoWrapperPass *TTIWP;
85 } // end anonymous namespace
87 char SimpleInliner::ID = 0;
88 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline", "Function Integration/Inlining",
90 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
91 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
92 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
93 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
94 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
95 INITIALIZE_PASS_END(SimpleInliner, "inline", "Function Integration/Inlining",
98 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
100 Pass *llvm::createFunctionInliningPass(int Threshold) {
101 return new SimpleInliner(llvm::getInlineParams(Threshold));
104 Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
105 unsigned SizeOptLevel,
106 bool DisableInlineHotCallSite) {
107 auto Param = llvm::getInlineParams(OptLevel, SizeOptLevel);
108 if (DisableInlineHotCallSite)
109 Param.HotCallSiteThreshold = 0;
110 return new SimpleInliner(Param);
113 Pass *llvm::createFunctionInliningPass(InlineParams &Params) {
114 return new SimpleInliner(Params);
117 bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
118 TTIWP = &getAnalysis<TargetTransformInfoWrapperPass>();
119 return LegacyInlinerBase::runOnSCC(SCC);
122 void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
123 AU.addRequired<TargetTransformInfoWrapperPass>();
124 LegacyInlinerBase::getAnalysisUsage(AU);