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/CallGraph.h"
16 #include "llvm/Analysis/InlineCost.h"
17 #include "llvm/Analysis/ProfileSummaryInfo.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/CallingConv.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/Transforms/IPO.h"
28 #include "llvm/Transforms/IPO/Inliner.h"
32 #define DEBUG_TYPE "inline"
36 /// \brief Actual inliner pass implementation.
38 /// The common implementation of the inlining logic is shared between this
39 /// inliner pass and the always inliner pass. The two passes use different cost
40 /// analyses to determine when to inline.
41 class SimpleInliner : public LegacyInlinerBase {
46 SimpleInliner() : LegacyInlinerBase(ID), Params(llvm::getInlineParams()) {
47 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
50 explicit SimpleInliner(InlineParams Params)
51 : LegacyInlinerBase(ID), Params(Params) {
52 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
55 static char ID; // Pass identification, replacement for typeid
57 InlineCost getInlineCost(CallSite CS) override {
58 Function *Callee = CS.getCalledFunction();
59 TargetTransformInfo &TTI = TTIWP->getTTI(*Callee);
60 std::function<AssumptionCache &(Function &)> GetAssumptionCache =
61 [&](Function &F) -> AssumptionCache & {
62 return ACT->getAssumptionCache(F);
64 return llvm::getInlineCost(CS, Params, TTI, GetAssumptionCache, PSI);
67 bool runOnSCC(CallGraphSCC &SCC) override;
68 void getAnalysisUsage(AnalysisUsage &AU) const override;
71 TargetTransformInfoWrapperPass *TTIWP;
75 } // end anonymous namespace
77 char SimpleInliner::ID = 0;
78 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline", "Function Integration/Inlining",
80 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
81 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
82 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
83 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
84 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
85 INITIALIZE_PASS_END(SimpleInliner, "inline", "Function Integration/Inlining",
88 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
90 Pass *llvm::createFunctionInliningPass(int Threshold) {
91 return new SimpleInliner(llvm::getInlineParams(Threshold));
94 Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
95 unsigned SizeOptLevel) {
96 return new SimpleInliner(llvm::getInlineParams(OptLevel, SizeOptLevel));
99 Pass *llvm::createFunctionInliningPass(InlineParams &Params) {
100 return new SimpleInliner(Params);
103 bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
104 TTIWP = &getAnalysis<TargetTransformInfoWrapperPass>();
105 return LegacyInlinerBase::runOnSCC(SCC);
108 void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
109 AU.addRequired<TargetTransformInfoWrapperPass>();
110 LegacyInlinerBase::getAnalysisUsage(AU);