LLVM API Documentation

InlineAlways.cpp
Go to the documentation of this file.
00001 //===- InlineAlways.cpp - Code to inline always_inline functions ----------===//
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 a custom inliner that handles only functions that
00011 // are marked as "always inline".
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Transforms/IPO.h"
00016 #include "llvm/ADT/SmallPtrSet.h"
00017 #include "llvm/Analysis/CallGraph.h"
00018 #include "llvm/Analysis/InlineCost.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/InlinerPass.h"
00027 
00028 using namespace llvm;
00029 
00030 #define DEBUG_TYPE "inline"
00031 
00032 namespace {
00033 
00034 /// \brief Inliner pass which only handles "always inline" functions.
00035 class AlwaysInliner : public Inliner {
00036   InlineCostAnalysis *ICA;
00037 
00038 public:
00039   // Use extremely low threshold.
00040   AlwaysInliner() : Inliner(ID, -2000000000, /*InsertLifetime*/ true), ICA(0) {
00041     initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
00042   }
00043 
00044   AlwaysInliner(bool InsertLifetime)
00045       : Inliner(ID, -2000000000, InsertLifetime), ICA(0) {
00046     initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
00047   }
00048 
00049   static char ID; // Pass identification, replacement for typeid
00050 
00051   InlineCost getInlineCost(CallSite CS) override;
00052 
00053   void getAnalysisUsage(AnalysisUsage &AU) const override;
00054   bool runOnSCC(CallGraphSCC &SCC) override;
00055 
00056   using llvm::Pass::doFinalization;
00057   bool doFinalization(CallGraph &CG) override {
00058     return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true);
00059   }
00060 };
00061 
00062 }
00063 
00064 char AlwaysInliner::ID = 0;
00065 INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline",
00066                 "Inliner for always_inline functions", false, false)
00067 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
00068 INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
00069 INITIALIZE_PASS_END(AlwaysInliner, "always-inline",
00070                 "Inliner for always_inline functions", false, false)
00071 
00072 Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); }
00073 
00074 Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) {
00075   return new AlwaysInliner(InsertLifetime);
00076 }
00077 
00078 /// \brief Get the inline cost for the always-inliner.
00079 ///
00080 /// The always inliner *only* handles functions which are marked with the
00081 /// attribute to force inlining. As such, it is dramatically simpler and avoids
00082 /// using the powerful (but expensive) inline cost analysis. Instead it uses
00083 /// a very simple and boring direct walk of the instructions looking for
00084 /// impossible-to-inline constructs.
00085 ///
00086 /// Note, it would be possible to go to some lengths to cache the information
00087 /// computed here, but as we only expect to do this for relatively few and
00088 /// small functions which have the explicit attribute to force inlining, it is
00089 /// likely not worth it in practice.
00090 InlineCost AlwaysInliner::getInlineCost(CallSite CS) {
00091   Function *Callee = CS.getCalledFunction();
00092 
00093   // Only inline direct calls to functions with always-inline attributes
00094   // that are viable for inlining. FIXME: We shouldn't even get here for
00095   // declarations.
00096   if (Callee && !Callee->isDeclaration() &&
00097       Callee->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
00098                                            Attribute::AlwaysInline) &&
00099       ICA->isInlineViable(*Callee))
00100     return InlineCost::getAlways();
00101 
00102   return InlineCost::getNever();
00103 }
00104 
00105 bool AlwaysInliner::runOnSCC(CallGraphSCC &SCC) {
00106   ICA = &getAnalysis<InlineCostAnalysis>();
00107   return Inliner::runOnSCC(SCC);
00108 }
00109 
00110 void AlwaysInliner::getAnalysisUsage(AnalysisUsage &AU) const {
00111   AU.addRequired<InlineCostAnalysis>();
00112   Inliner::getAnalysisUsage(AU);
00113 }