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