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),
00041                     ICA(nullptr) {
00042     initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
00043   }
00044 
00045   AlwaysInliner(bool InsertLifetime)
00046       : Inliner(ID, -2000000000, InsertLifetime), ICA(nullptr) {
00047     initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
00048   }
00049 
00050   static char ID; // Pass identification, replacement for typeid
00051 
00052   InlineCost getInlineCost(CallSite CS) override;
00053 
00054   void getAnalysisUsage(AnalysisUsage &AU) const override;
00055   bool runOnSCC(CallGraphSCC &SCC) override;
00056 
00057   using llvm::Pass::doFinalization;
00058   bool doFinalization(CallGraph &CG) override {
00059     return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true);
00060   }
00061 };
00062 
00063 }
00064 
00065 char AlwaysInliner::ID = 0;
00066 INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline",
00067                 "Inliner for always_inline functions", false, false)
00068 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
00069 INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
00070 INITIALIZE_PASS_END(AlwaysInliner, "always-inline",
00071                 "Inliner for always_inline functions", false, false)
00072 
00073 Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); }
00074 
00075 Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) {
00076   return new AlwaysInliner(InsertLifetime);
00077 }
00078 
00079 /// \brief Get the inline cost for the always-inliner.
00080 ///
00081 /// The always inliner *only* handles functions which are marked with the
00082 /// attribute to force inlining. As such, it is dramatically simpler and avoids
00083 /// using the powerful (but expensive) inline cost analysis. Instead it uses
00084 /// a very simple and boring direct walk of the instructions looking for
00085 /// impossible-to-inline constructs.
00086 ///
00087 /// Note, it would be possible to go to some lengths to cache the information
00088 /// computed here, but as we only expect to do this for relatively few and
00089 /// small functions which have the explicit attribute to force inlining, it is
00090 /// likely not worth it in practice.
00091 InlineCost AlwaysInliner::getInlineCost(CallSite CS) {
00092   Function *Callee = CS.getCalledFunction();
00093 
00094   // Only inline direct calls to functions with always-inline attributes
00095   // that are viable for inlining. FIXME: We shouldn't even get here for
00096   // declarations.
00097   if (Callee && !Callee->isDeclaration() &&
00098       CS.hasFnAttr(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 }