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