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/CallingConv.h" 20 #include "llvm/IR/DataLayout.h" 21 #include "llvm/IR/Instructions.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/IR/Type.h" 24 #include "llvm/InitializePasses.h" 25 #include "llvm/Transforms/Coroutines.h" 26 #include "llvm/Transforms/IPO.h" 27 #include "llvm/Transforms/IPO/Inliner.h" 28 #include "llvm/Transforms/Utils/Cloning.h" 29 #include "llvm/Transforms/Utils/ModuleUtils.h" 30 31 using namespace llvm; 32 33 #define DEBUG_TYPE "inline" 34 35 PreservedAnalyses AlwaysInlinerPass::run(Module &M, 36 ModuleAnalysisManager &MAM) { 37 // Add inline assumptions during code generation. 38 FunctionAnalysisManager &FAM = 39 MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); 40 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & { 41 return FAM.getResult<AssumptionAnalysis>(F); 42 }; 43 InlineFunctionInfo IFI(/*cg=*/nullptr, GetAssumptionCache); 44 45 SmallSetVector<CallBase *, 16> Calls; 46 bool Changed = false; 47 SmallVector<Function *, 16> InlinedFunctions; 48 for (Function &F : M) { 49 // When callee coroutine function is inlined into caller coroutine function 50 // before coro-split pass, 51 // coro-early pass can not handle this quiet well. 52 // So we won't inline the coroutine function if it have not been unsplited 53 if (F.hasFnAttribute(CORO_PRESPLIT_ATTR)) 54 continue; 55 56 if (!F.isDeclaration() && F.hasFnAttribute(Attribute::AlwaysInline) && 57 isInlineViable(F).isSuccess()) { 58 Calls.clear(); 59 60 for (User *U : F.users()) 61 if (auto *CB = dyn_cast<CallBase>(U)) 62 if (CB->getCalledFunction() == &F) 63 Calls.insert(CB); 64 65 for (CallBase *CB : Calls) 66 // FIXME: We really shouldn't be able to fail to inline at this point! 67 // We should do something to log or check the inline failures here. 68 Changed |= 69 InlineFunction(*CB, IFI, /*CalleeAAR=*/nullptr, InsertLifetime) 70 .isSuccess(); 71 72 // Remember to try and delete this function afterward. This both avoids 73 // re-walking the rest of the module and avoids dealing with any iterator 74 // invalidation issues while deleting functions. 75 InlinedFunctions.push_back(&F); 76 } 77 } 78 79 // Remove any live functions. 80 erase_if(InlinedFunctions, [&](Function *F) { 81 F->removeDeadConstantUsers(); 82 return !F->isDefTriviallyDead(); 83 }); 84 85 // Delete the non-comdat ones from the module and also from our vector. 86 auto NonComdatBegin = partition( 87 InlinedFunctions, [&](Function *F) { return F->hasComdat(); }); 88 for (Function *F : make_range(NonComdatBegin, InlinedFunctions.end())) 89 M.getFunctionList().erase(F); 90 InlinedFunctions.erase(NonComdatBegin, InlinedFunctions.end()); 91 92 if (!InlinedFunctions.empty()) { 93 // Now we just have the comdat functions. Filter out the ones whose comdats 94 // are not actually dead. 95 filterDeadComdatFunctions(M, InlinedFunctions); 96 // The remaining functions are actually dead. 97 for (Function *F : InlinedFunctions) 98 M.getFunctionList().erase(F); 99 } 100 101 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); 102 } 103 104 namespace { 105 106 /// Inliner pass which only handles "always inline" functions. 107 /// 108 /// Unlike the \c AlwaysInlinerPass, this uses the more heavyweight \c Inliner 109 /// base class to provide several facilities such as array alloca merging. 110 class AlwaysInlinerLegacyPass : public LegacyInlinerBase { 111 112 public: 113 AlwaysInlinerLegacyPass() : LegacyInlinerBase(ID, /*InsertLifetime*/ true) { 114 initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); 115 } 116 117 AlwaysInlinerLegacyPass(bool InsertLifetime) 118 : LegacyInlinerBase(ID, InsertLifetime) { 119 initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); 120 } 121 122 /// Main run interface method. We override here to avoid calling skipSCC(). 123 bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); } 124 125 static char ID; // Pass identification, replacement for typeid 126 127 InlineCost getInlineCost(CallBase &CB) override; 128 129 using llvm::Pass::doFinalization; 130 bool doFinalization(CallGraph &CG) override { 131 return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true); 132 } 133 }; 134 } 135 136 char AlwaysInlinerLegacyPass::ID = 0; 137 INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline", 138 "Inliner for always_inline functions", false, false) 139 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 140 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 141 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) 142 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 143 INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline", 144 "Inliner for always_inline functions", false, false) 145 146 Pass *llvm::createAlwaysInlinerLegacyPass(bool InsertLifetime) { 147 return new AlwaysInlinerLegacyPass(InsertLifetime); 148 } 149 150 /// Get the inline cost for the always-inliner. 151 /// 152 /// The always inliner *only* handles functions which are marked with the 153 /// attribute to force inlining. As such, it is dramatically simpler and avoids 154 /// using the powerful (but expensive) inline cost analysis. Instead it uses 155 /// a very simple and boring direct walk of the instructions looking for 156 /// impossible-to-inline constructs. 157 /// 158 /// Note, it would be possible to go to some lengths to cache the information 159 /// computed here, but as we only expect to do this for relatively few and 160 /// small functions which have the explicit attribute to force inlining, it is 161 /// likely not worth it in practice. 162 InlineCost AlwaysInlinerLegacyPass::getInlineCost(CallBase &CB) { 163 Function *Callee = CB.getCalledFunction(); 164 165 // Only inline direct calls to functions with always-inline attributes 166 // that are viable for inlining. 167 if (!Callee) 168 return InlineCost::getNever("indirect call"); 169 170 // When callee coroutine function is inlined into caller coroutine function 171 // before coro-split pass, 172 // coro-early pass can not handle this quiet well. 173 // So we won't inline the coroutine function if it have not been unsplited 174 if (Callee->hasFnAttribute(CORO_PRESPLIT_ATTR)) 175 return InlineCost::getNever("unsplited coroutine call"); 176 177 // FIXME: We shouldn't even get here for declarations. 178 if (Callee->isDeclaration()) 179 return InlineCost::getNever("no definition"); 180 181 if (!CB.hasFnAttr(Attribute::AlwaysInline)) 182 return InlineCost::getNever("no alwaysinline attribute"); 183 184 auto IsViable = isInlineViable(*Callee); 185 if (!IsViable.isSuccess()) 186 return InlineCost::getNever(IsViable.getFailureReason()); 187 188 return InlineCost::getAlways("always inliner"); 189 } 190