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