1 //===- InlineSimple.cpp - Code to perform simple function inlining --------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements bottom-up inlining of functions into callees. 11 // 12 //===----------------------------------------------------------------------===// 13 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/InlinerPass.h" 29 30 using namespace llvm; 31 32 #define DEBUG_TYPE "inline" 33 34 namespace { 35 36 /// \brief Actual inliner pass implementation. 37 /// 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 Inliner { 42 // This field is populated based on one of the following: 43 // * optimization or size-optimization levels, 44 // * the --inline-threshold flag, or 45 // * a user specified value. 46 int DefaultThreshold; 47 48 public: 49 SimpleInliner() 50 : Inliner(ID), DefaultThreshold(llvm::getDefaultInlineThreshold()) { 51 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry()); 52 } 53 54 explicit SimpleInliner(int Threshold) 55 : Inliner(ID), DefaultThreshold(Threshold) { 56 initializeSimpleInlinerPass(*PassRegistry::getPassRegistry()); 57 } 58 59 static char ID; // Pass identification, replacement for typeid 60 61 InlineCost getInlineCost(CallSite CS) override { 62 Function *Callee = CS.getCalledFunction(); 63 TargetTransformInfo &TTI = TTIWP->getTTI(*Callee); 64 std::function<AssumptionCache &(Function &)> GetAssumptionCache = [&]( 65 Function &F) -> AssumptionCache & { 66 return ACT->getAssumptionCache(F); 67 }; 68 return llvm::getInlineCost(CS, DefaultThreshold, TTI, GetAssumptionCache, 69 PSI); 70 } 71 72 bool runOnSCC(CallGraphSCC &SCC) override; 73 void getAnalysisUsage(AnalysisUsage &AU) const override; 74 75 private: 76 TargetTransformInfoWrapperPass *TTIWP; 77 }; 78 79 } // end anonymous namespace 80 81 char SimpleInliner::ID = 0; 82 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline", 83 "Function Integration/Inlining", false, false) 84 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 85 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 86 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) 87 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 88 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 89 INITIALIZE_PASS_END(SimpleInliner, "inline", 90 "Function Integration/Inlining", false, false) 91 92 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); } 93 94 Pass *llvm::createFunctionInliningPass(int Threshold) { 95 return new SimpleInliner(Threshold); 96 } 97 98 Pass *llvm::createFunctionInliningPass(unsigned OptLevel, 99 unsigned SizeOptLevel) { 100 return new SimpleInliner( 101 llvm::computeThresholdFromOptLevels(OptLevel, SizeOptLevel)); 102 } 103 104 bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) { 105 TTIWP = &getAnalysis<TargetTransformInfoWrapperPass>(); 106 return Inliner::runOnSCC(SCC); 107 } 108 109 void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const { 110 AU.addRequired<TargetTransformInfoWrapperPass>(); 111 Inliner::getAnalysisUsage(AU); 112 } 113