LLVM  mainline
InlineSimple.cpp
Go to the documentation of this file.
00001 //===- InlineSimple.cpp - Code to perform simple function inlining --------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements bottom-up inlining of functions into callees.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Analysis/AssumptionCache.h"
00015 #include "llvm/Analysis/CallGraph.h"
00016 #include "llvm/Analysis/InlineCost.h"
00017 #include "llvm/Analysis/TargetLibraryInfo.h"
00018 #include "llvm/Analysis/TargetTransformInfo.h"
00019 #include "llvm/IR/CallSite.h"
00020 #include "llvm/IR/CallingConv.h"
00021 #include "llvm/IR/DataLayout.h"
00022 #include "llvm/IR/Instructions.h"
00023 #include "llvm/IR/IntrinsicInst.h"
00024 #include "llvm/IR/Module.h"
00025 #include "llvm/IR/Type.h"
00026 #include "llvm/Transforms/IPO.h"
00027 #include "llvm/Transforms/IPO/InlinerPass.h"
00028 
00029 using namespace llvm;
00030 
00031 #define DEBUG_TYPE "inline"
00032 
00033 namespace {
00034 
00035 /// \brief Actual inliner pass implementation.
00036 ///
00037 /// The common implementation of the inlining logic is shared between this
00038 /// inliner pass and the always inliner pass. The two passes use different cost
00039 /// analyses to determine when to inline.
00040 class SimpleInliner : public Inliner {
00041   // This field is populated based on one of the following:
00042   //  optimization or size optimization levels,
00043   //  --inline-threshold flag,
00044   //  user specified value.
00045   int DefaultThreshold;
00046 
00047 public:
00048   SimpleInliner()
00049       : Inliner(ID), DefaultThreshold(llvm::getDefaultInlineThreshold()) {
00050     initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
00051   }
00052 
00053   SimpleInliner(int Threshold) : Inliner(ID), DefaultThreshold(Threshold) {
00054     initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
00055   }
00056 
00057   static char ID; // Pass identification, replacement for typeid
00058 
00059   InlineCost getInlineCost(CallSite CS) override {
00060     Function *Callee = CS.getCalledFunction();
00061     TargetTransformInfo &TTI = TTIWP->getTTI(*Callee);
00062     return llvm::getInlineCost(CS, DefaultThreshold, TTI, ACT);
00063   }
00064 
00065   bool runOnSCC(CallGraphSCC &SCC) override;
00066   void getAnalysisUsage(AnalysisUsage &AU) const override;
00067 
00068 private:
00069   TargetTransformInfoWrapperPass *TTIWP;
00070 };
00071 
00072 } // end anonymous namespace
00073 
00074 char SimpleInliner::ID = 0;
00075 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
00076                 "Function Integration/Inlining", false, false)
00077 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
00078 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
00079 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
00080 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
00081 INITIALIZE_PASS_END(SimpleInliner, "inline",
00082                 "Function Integration/Inlining", false, false)
00083 
00084 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
00085 
00086 Pass *llvm::createFunctionInliningPass(int Threshold) {
00087   return new SimpleInliner(Threshold);
00088 }
00089 
00090 Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
00091                                        unsigned SizeOptLevel) {
00092   return new SimpleInliner(
00093       llvm::computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
00094 }
00095 
00096 bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
00097   TTIWP = &getAnalysis<TargetTransformInfoWrapperPass>();
00098   return Inliner::runOnSCC(SCC);
00099 }
00100 
00101 void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
00102   AU.addRequired<TargetTransformInfoWrapperPass>();
00103   Inliner::getAnalysisUsage(AU);
00104 }