LLVM API Documentation

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 #define DEBUG_TYPE "inline"
00015 #include "llvm/Transforms/IPO.h"
00016 #include "llvm/Analysis/CallGraph.h"
00017 #include "llvm/Analysis/InlineCost.h"
00018 #include "llvm/IR/CallSite.h"
00019 #include "llvm/IR/CallingConv.h"
00020 #include "llvm/IR/DataLayout.h"
00021 #include "llvm/IR/Instructions.h"
00022 #include "llvm/IR/IntrinsicInst.h"
00023 #include "llvm/IR/Module.h"
00024 #include "llvm/IR/Type.h"
00025 #include "llvm/Transforms/IPO/InlinerPass.h"
00026 
00027 using namespace llvm;
00028 
00029 namespace {
00030 
00031 /// \brief Actual inliner pass implementation.
00032 ///
00033 /// The common implementation of the inlining logic is shared between this
00034 /// inliner pass and the always inliner pass. The two passes use different cost
00035 /// analyses to determine when to inline.
00036 class SimpleInliner : public Inliner {
00037   InlineCostAnalysis *ICA;
00038 
00039 public:
00040   SimpleInliner() : Inliner(ID), ICA(0) {
00041     initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
00042   }
00043 
00044   SimpleInliner(int Threshold)
00045       : Inliner(ID, Threshold, /*InsertLifetime*/ true), ICA(0) {
00046     initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
00047   }
00048 
00049   static char ID; // Pass identification, replacement for typeid
00050 
00051   InlineCost getInlineCost(CallSite CS) override {
00052     return ICA->getInlineCost(CS, getInlineThreshold(CS));
00053   }
00054 
00055   bool runOnSCC(CallGraphSCC &SCC) override;
00056   void getAnalysisUsage(AnalysisUsage &AU) const override;
00057 };
00058 
00059 static int computeThresholdFromOptLevels(unsigned OptLevel,
00060                                          unsigned SizeOptLevel) {
00061   if (OptLevel > 2)
00062     return 275;
00063   if (SizeOptLevel == 1) // -Os
00064     return 75;
00065   if (SizeOptLevel == 2) // -Oz
00066     return 25;
00067   return 225;
00068 }
00069 
00070 } // end anonymous namespace
00071 
00072 char SimpleInliner::ID = 0;
00073 INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
00074                 "Function Integration/Inlining", false, false)
00075 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
00076 INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
00077 INITIALIZE_PASS_END(SimpleInliner, "inline",
00078                 "Function Integration/Inlining", false, false)
00079 
00080 Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
00081 
00082 Pass *llvm::createFunctionInliningPass(int Threshold) {
00083   return new SimpleInliner(Threshold);
00084 }
00085 
00086 Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
00087                                        unsigned SizeOptLevel) {
00088   return new SimpleInliner(
00089       computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
00090 }
00091 
00092 bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
00093   ICA = &getAnalysis<InlineCostAnalysis>();
00094   return Inliner::runOnSCC(SCC);
00095 }
00096 
00097 void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
00098   AU.addRequired<InlineCostAnalysis>();
00099   Inliner::getAnalysisUsage(AU);
00100 }