1 //===- InlineAlways.cpp - Code to inline always_inline functions ----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements a custom inliner that handles only functions that 10 // are marked as "always inline". 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Transforms/IPO/AlwaysInliner.h" 15 #include "llvm/ADT/SetVector.h" 16 #include "llvm/Analysis/AssumptionCache.h" 17 #include "llvm/Analysis/InlineCost.h" 18 #include "llvm/Analysis/TargetLibraryInfo.h" 19 #include "llvm/IR/CallSite.h" 20 #include "llvm/IR/CallingConv.h" 21 #include "llvm/IR/DataLayout.h" 22 #include "llvm/IR/Instructions.h" 23 #include "llvm/IR/Module.h" 24 #include "llvm/IR/Type.h" 25 #include "llvm/Transforms/IPO.h" 26 #include "llvm/Transforms/IPO/Inliner.h" 27 #include "llvm/Transforms/Utils/Cloning.h" 28 #include "llvm/Transforms/Utils/ModuleUtils.h" 29 30 using namespace llvm; 31 32 #define DEBUG_TYPE "inline" 33 34 PreservedAnalyses AlwaysInlinerPass::run(Module &M, ModuleAnalysisManager &) { 35 InlineFunctionInfo IFI; 36 SmallSetVector<CallSite, 16> Calls; 37 bool Changed = false; 38 SmallVector<Function *, 16> InlinedFunctions; 39 for (Function &F : M) 40 if (!F.isDeclaration() && F.hasFnAttribute(Attribute::AlwaysInline) && 41 isInlineViable(F)) { 42 Calls.clear(); 43 44 for (User *U : F.users()) 45 if (auto CS = CallSite(U)) 46 if (CS.getCalledFunction() == &F) 47 Calls.insert(CS); 48 49 for (CallSite CS : Calls) 50 // FIXME: We really shouldn't be able to fail to inline at this point! 51 // We should do something to log or check the inline failures here. 52 Changed |= 53 InlineFunction(CS, IFI, /*CalleeAAR=*/nullptr, InsertLifetime); 54 55 // Remember to try and delete this function afterward. This both avoids 56 // re-walking the rest of the module and avoids dealing with any iterator 57 // invalidation issues while deleting functions. 58 InlinedFunctions.push_back(&F); 59 } 60 61 // Remove any live functions. 62 erase_if(InlinedFunctions, [&](Function *F) { 63 F->removeDeadConstantUsers(); 64 return !F->isDefTriviallyDead(); 65 }); 66 67 // Delete the non-comdat ones from the module and also from our vector. 68 auto NonComdatBegin = partition( 69 InlinedFunctions, [&](Function *F) { return F->hasComdat(); }); 70 for (Function *F : make_range(NonComdatBegin, InlinedFunctions.end())) 71 M.getFunctionList().erase(F); 72 InlinedFunctions.erase(NonComdatBegin, InlinedFunctions.end()); 73 74 if (!InlinedFunctions.empty()) { 75 // Now we just have the comdat functions. Filter out the ones whose comdats 76 // are not actually dead. 77 filterDeadComdatFunctions(M, InlinedFunctions); 78 // The remaining functions are actually dead. 79 for (Function *F : InlinedFunctions) 80 M.getFunctionList().erase(F); 81 } 82 83 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); 84 } 85 86 namespace { 87 88 /// Inliner pass which only handles "always inline" functions. 89 /// 90 /// Unlike the \c AlwaysInlinerPass, this uses the more heavyweight \c Inliner 91 /// base class to provide several facilities such as array alloca merging. 92 class AlwaysInlinerLegacyPass : public LegacyInlinerBase { 93 94 public: 95 AlwaysInlinerLegacyPass() : LegacyInlinerBase(ID, /*InsertLifetime*/ true) { 96 initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); 97 } 98 99 AlwaysInlinerLegacyPass(bool InsertLifetime) 100 : LegacyInlinerBase(ID, InsertLifetime) { 101 initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); 102 } 103 104 /// Main run interface method. We override here to avoid calling skipSCC(). 105 bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); } 106 107 static char ID; // Pass identification, replacement for typeid 108 109 InlineCost getInlineCost(CallSite CS) override; 110 111 using llvm::Pass::doFinalization; 112 bool doFinalization(CallGraph &CG) override { 113 return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true); 114 } 115 }; 116 } 117 118 char AlwaysInlinerLegacyPass::ID = 0; 119 INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline", 120 "Inliner for always_inline functions", false, false) 121 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 122 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 123 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) 124 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 125 INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline", 126 "Inliner for always_inline functions", false, false) 127 128 Pass *llvm::createAlwaysInlinerLegacyPass(bool InsertLifetime) { 129 return new AlwaysInlinerLegacyPass(InsertLifetime); 130 } 131 132 /// Get the inline cost for the always-inliner. 133 /// 134 /// The always inliner *only* handles functions which are marked with the 135 /// attribute to force inlining. As such, it is dramatically simpler and avoids 136 /// using the powerful (but expensive) inline cost analysis. Instead it uses 137 /// a very simple and boring direct walk of the instructions looking for 138 /// impossible-to-inline constructs. 139 /// 140 /// Note, it would be possible to go to some lengths to cache the information 141 /// computed here, but as we only expect to do this for relatively few and 142 /// small functions which have the explicit attribute to force inlining, it is 143 /// likely not worth it in practice. 144 InlineCost AlwaysInlinerLegacyPass::getInlineCost(CallSite CS) { 145 Function *Callee = CS.getCalledFunction(); 146 147 // Only inline direct calls to functions with always-inline attributes 148 // that are viable for inlining. 149 if (!Callee) 150 return InlineCost::getNever("indirect call"); 151 152 // FIXME: We shouldn't even get here for declarations. 153 if (Callee->isDeclaration()) 154 return InlineCost::getNever("no definition"); 155 156 if (!CS.hasFnAttr(Attribute::AlwaysInline)) 157 return InlineCost::getNever("no alwaysinline attribute"); 158 159 auto IsViable = isInlineViable(*Callee); 160 if (!IsViable) 161 return InlineCost::getNever(IsViable.message); 162 163 return InlineCost::getAlways("always inliner"); 164 } 165