1 //===- InlineAlways.cpp - Code to inline always_inline functions ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a custom inliner that handles only functions that
11 // are marked as "always inline".
13 //===----------------------------------------------------------------------===//
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 #include "llvm/Transforms/Utils/ModuleUtils.h"
36 #define DEBUG_TYPE "inline"
38 PreservedAnalyses AlwaysInlinerPass::run(Module &M, ModuleAnalysisManager &) {
39 InlineFunctionInfo IFI;
40 SmallSetVector<CallSite, 16> Calls;
42 SmallVector<Function *, 16> InlinedFunctions;
44 if (!F.isDeclaration() && F.hasFnAttribute(Attribute::AlwaysInline) &&
48 for (User *U : F.users())
49 if (auto CS = CallSite(U))
50 if (CS.getCalledFunction() == &F)
53 for (CallSite CS : Calls)
54 // FIXME: We really shouldn't be able to fail to inline at this point!
55 // We should do something to log or check the inline failures here.
56 Changed |= InlineFunction(CS, IFI);
58 // Remember to try and delete this function afterward. This both avoids
59 // re-walking the rest of the module and avoids dealing with any iterator
60 // invalidation issues while deleting functions.
61 InlinedFunctions.push_back(&F);
64 // Remove any live functions.
65 erase_if(InlinedFunctions, [&](Function *F) {
66 F->removeDeadConstantUsers();
67 return !F->isDefTriviallyDead();
70 // Delete the non-comdat ones from the module and also from our vector.
71 auto NonComdatBegin = partition(
72 InlinedFunctions, [&](Function *F) { return F->hasComdat(); });
73 for (Function *F : make_range(NonComdatBegin, InlinedFunctions.end()))
74 M.getFunctionList().erase(F);
75 InlinedFunctions.erase(NonComdatBegin, InlinedFunctions.end());
77 if (!InlinedFunctions.empty()) {
78 // Now we just have the comdat functions. Filter out the ones whose comdats
79 // are not actually dead.
80 filterDeadComdatFunctions(M, InlinedFunctions);
81 // The remaining functions are actually dead.
82 for (Function *F : InlinedFunctions)
83 M.getFunctionList().erase(F);
86 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
91 /// Inliner pass which only handles "always inline" functions.
93 /// Unlike the \c AlwaysInlinerPass, this uses the more heavyweight \c Inliner
94 /// base class to provide several facilities such as array alloca merging.
95 class AlwaysInlinerLegacyPass : public LegacyInlinerBase {
98 AlwaysInlinerLegacyPass() : LegacyInlinerBase(ID, /*InsertLifetime*/ true) {
99 initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
102 AlwaysInlinerLegacyPass(bool InsertLifetime)
103 : LegacyInlinerBase(ID, InsertLifetime) {
104 initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
107 /// Main run interface method. We override here to avoid calling skipSCC().
108 bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); }
110 static char ID; // Pass identification, replacement for typeid
112 InlineCost getInlineCost(CallSite CS) override;
114 using llvm::Pass::doFinalization;
115 bool doFinalization(CallGraph &CG) override {
116 return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true);
121 char AlwaysInlinerLegacyPass::ID = 0;
122 INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline",
123 "Inliner for always_inline functions", false, false)
124 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
125 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
126 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
127 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
128 INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline",
129 "Inliner for always_inline functions", false, false)
131 Pass *llvm::createAlwaysInlinerLegacyPass(bool InsertLifetime) {
132 return new AlwaysInlinerLegacyPass(InsertLifetime);
135 /// \brief Get the inline cost for the always-inliner.
137 /// The always inliner *only* handles functions which are marked with the
138 /// attribute to force inlining. As such, it is dramatically simpler and avoids
139 /// using the powerful (but expensive) inline cost analysis. Instead it uses
140 /// a very simple and boring direct walk of the instructions looking for
141 /// impossible-to-inline constructs.
143 /// Note, it would be possible to go to some lengths to cache the information
144 /// computed here, but as we only expect to do this for relatively few and
145 /// small functions which have the explicit attribute to force inlining, it is
146 /// likely not worth it in practice.
147 InlineCost AlwaysInlinerLegacyPass::getInlineCost(CallSite CS) {
148 Function *Callee = CS.getCalledFunction();
150 // Only inline direct calls to functions with always-inline attributes
151 // that are viable for inlining. FIXME: We shouldn't even get here for
153 if (Callee && !Callee->isDeclaration() &&
154 CS.hasFnAttr(Attribute::AlwaysInline) && isInlineViable(*Callee))
155 return InlineCost::getAlways();
157 return InlineCost::getNever();