1 //===- InlineAlways.cpp - Code to inline always_inline functions ----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements a custom inliner that handles only functions that 11 // are marked as "always inline". 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Transforms/IPO/AlwaysInliner.h" 16 #include "llvm/ADT/SetVector.h" 17 #include "llvm/Analysis/AssumptionCache.h" 18 #include "llvm/Analysis/CallGraph.h" 19 #include "llvm/Analysis/InlineCost.h" 20 #include "llvm/Analysis/ProfileSummaryInfo.h" 21 #include "llvm/Analysis/TargetLibraryInfo.h" 22 #include "llvm/IR/CallSite.h" 23 #include "llvm/IR/CallingConv.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/Instructions.h" 26 #include "llvm/IR/IntrinsicInst.h" 27 #include "llvm/IR/Module.h" 28 #include "llvm/IR/Type.h" 29 #include "llvm/Transforms/IPO.h" 30 #include "llvm/Transforms/IPO/Inliner.h" 31 #include "llvm/Transforms/Utils/Cloning.h" 32 33 using namespace llvm; 34 35 #define DEBUG_TYPE "inline" 36 37 PreservedAnalyses AlwaysInlinerPass::run(Module &M, ModuleAnalysisManager &) { 38 InlineFunctionInfo IFI; 39 SmallSetVector<CallSite, 16> Calls; 40 bool Changed = false; 41 for (Function &F : M) 42 if (!F.isDeclaration() && F.hasFnAttribute(Attribute::AlwaysInline) && 43 isInlineViable(F)) { 44 Calls.clear(); 45 46 for (User *U : F.users()) 47 if (auto CS = CallSite(U)) 48 if (CS.getCalledFunction() == &F) 49 Calls.insert(CS); 50 51 for (CallSite CS : Calls) 52 // FIXME: We really shouldn't be able to fail to inline at this point! 53 // We should do something to log or check the inline failures here. 54 Changed |= InlineFunction(CS, IFI); 55 } 56 57 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); 58 } 59 60 namespace { 61 62 /// Inliner pass which only handles "always inline" functions. 63 /// 64 /// Unlike the \c AlwaysInlinerPass, this uses the more heavyweight \c Inliner 65 /// base class to provide several facilities such as array alloca merging. 66 class AlwaysInlinerLegacyPass : public LegacyInlinerBase { 67 68 public: 69 AlwaysInlinerLegacyPass() : LegacyInlinerBase(ID, /*InsertLifetime*/ true) { 70 initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); 71 } 72 73 AlwaysInlinerLegacyPass(bool InsertLifetime) 74 : LegacyInlinerBase(ID, InsertLifetime) { 75 initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); 76 } 77 78 /// Main run interface method. We override here to avoid calling skipSCC(). 79 bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); } 80 81 static char ID; // Pass identification, replacement for typeid 82 83 InlineCost getInlineCost(CallSite CS) override; 84 85 using llvm::Pass::doFinalization; 86 bool doFinalization(CallGraph &CG) override { 87 return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true); 88 } 89 }; 90 } 91 92 char AlwaysInlinerLegacyPass::ID = 0; 93 INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline", 94 "Inliner for always_inline functions", false, false) 95 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 96 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 97 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) 98 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 99 INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline", 100 "Inliner for always_inline functions", false, false) 101 102 Pass *llvm::createAlwaysInlinerLegacyPass(bool InsertLifetime) { 103 return new AlwaysInlinerLegacyPass(InsertLifetime); 104 } 105 106 /// \brief Get the inline cost for the always-inliner. 107 /// 108 /// The always inliner *only* handles functions which are marked with the 109 /// attribute to force inlining. As such, it is dramatically simpler and avoids 110 /// using the powerful (but expensive) inline cost analysis. Instead it uses 111 /// a very simple and boring direct walk of the instructions looking for 112 /// impossible-to-inline constructs. 113 /// 114 /// Note, it would be possible to go to some lengths to cache the information 115 /// computed here, but as we only expect to do this for relatively few and 116 /// small functions which have the explicit attribute to force inlining, it is 117 /// likely not worth it in practice. 118 InlineCost AlwaysInlinerLegacyPass::getInlineCost(CallSite CS) { 119 Function *Callee = CS.getCalledFunction(); 120 121 // Only inline direct calls to functions with always-inline attributes 122 // that are viable for inlining. FIXME: We shouldn't even get here for 123 // declarations. 124 if (Callee && !Callee->isDeclaration() && 125 CS.hasFnAttr(Attribute::AlwaysInline) && isInlineViable(*Callee)) 126 return InlineCost::getAlways(); 127 128 return InlineCost::getNever(); 129 } 130