LLVM  mainline
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/AssumptionCache.h"
00018 #include "llvm/Analysis/CallGraph.h"
00019 #include "llvm/Analysis/InlineCost.h"
00020 #include "llvm/Analysis/TargetLibraryInfo.h"
00021 #include "llvm/IR/CallSite.h"
00022 #include "llvm/IR/CallingConv.h"
00023 #include "llvm/IR/DataLayout.h"
00024 #include "llvm/IR/Instructions.h"
00025 #include "llvm/IR/IntrinsicInst.h"
00026 #include "llvm/IR/Module.h"
00027 #include "llvm/IR/Type.h"
00028 #include "llvm/Transforms/IPO/InlinerPass.h"
00029 
00030 using namespace llvm;
00031 
00032 #define DEBUG_TYPE "inline"
00033 
00034 namespace {
00035 
00036 /// \brief Inliner pass which only handles "always inline" functions.
00037 class AlwaysInliner : public Inliner {
00038 
00039 public:
00040   AlwaysInliner() : Inliner(ID, /*InsertLifetime*/ true) {
00041     initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
00042   }
00043 
00044   AlwaysInliner(bool InsertLifetime) : Inliner(ID, InsertLifetime) {
00045     initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
00046   }
00047 
00048   static char ID; // Pass identification, replacement for typeid
00049 
00050   InlineCost getInlineCost(CallSite CS) override;
00051 
00052   using llvm::Pass::doFinalization;
00053   bool doFinalization(CallGraph &CG) override {
00054     return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true);
00055   }
00056 };
00057 
00058 }
00059 
00060 char AlwaysInliner::ID = 0;
00061 INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline",
00062                 "Inliner for always_inline functions", false, false)
00063 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
00064 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
00065 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
00066 INITIALIZE_PASS_END(AlwaysInliner, "always-inline",
00067                 "Inliner for always_inline functions", false, false)
00068 
00069 Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); }
00070 
00071 Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) {
00072   return new AlwaysInliner(InsertLifetime);
00073 }
00074 
00075 /// \brief Get the inline cost for the always-inliner.
00076 ///
00077 /// The always inliner *only* handles functions which are marked with the
00078 /// attribute to force inlining. As such, it is dramatically simpler and avoids
00079 /// using the powerful (but expensive) inline cost analysis. Instead it uses
00080 /// a very simple and boring direct walk of the instructions looking for
00081 /// impossible-to-inline constructs.
00082 ///
00083 /// Note, it would be possible to go to some lengths to cache the information
00084 /// computed here, but as we only expect to do this for relatively few and
00085 /// small functions which have the explicit attribute to force inlining, it is
00086 /// likely not worth it in practice.
00087 InlineCost AlwaysInliner::getInlineCost(CallSite CS) {
00088   Function *Callee = CS.getCalledFunction();
00089 
00090   // Only inline direct calls to functions with always-inline attributes
00091   // that are viable for inlining. FIXME: We shouldn't even get here for
00092   // declarations.
00093   if (Callee && !Callee->isDeclaration() &&
00094       CS.hasFnAttr(Attribute::AlwaysInline) && isInlineViable(*Callee))
00095     return InlineCost::getAlways();
00096 
00097   return InlineCost::getNever();
00098 }