blob: 3b735ddd192e25e8654dd3442d8d86b8783be21c [file] [log] [blame]
Devang Patel50c66cd2008-09-03 19:52:17 +00001//===- InlineAlways.cpp - Code to inline always_inline functions ----------===//
Devang Patel924d9082008-09-03 18:50:53 +00002//
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//
Devang Patel50c66cd2008-09-03 19:52:17 +000010// This file implements a custom inliner that handles only functions that
Devang Patela563d242008-09-03 20:25:40 +000011// are marked as "always inline".
Devang Patel924d9082008-09-03 18:50:53 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth67fc52f2016-08-17 02:56:20 +000015#include "llvm/Transforms/IPO/AlwaysInliner.h"
16#include "llvm/ADT/SetVector.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000017#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Analysis/InlineCost.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000019#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000020#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/CallingConv.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Module.h"
25#include "llvm/IR/Type.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000026#include "llvm/Transforms/IPO.h"
27#include "llvm/Transforms/IPO/Inliner.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000028#include "llvm/Transforms/Utils/Cloning.h"
Chandler Carruth6e9bb7e2016-12-26 23:43:27 +000029#include "llvm/Transforms/Utils/ModuleUtils.h"
Devang Patel924d9082008-09-03 18:50:53 +000030
31using namespace llvm;
32
Chandler Carruth964daaa2014-04-22 02:55:47 +000033#define DEBUG_TYPE "inline"
34
Chandler Carruth67fc52f2016-08-17 02:56:20 +000035PreservedAnalyses AlwaysInlinerPass::run(Module &M, ModuleAnalysisManager &) {
36 InlineFunctionInfo IFI;
37 SmallSetVector<CallSite, 16> Calls;
38 bool Changed = false;
Chandler Carruth4eaff122016-12-23 23:33:35 +000039 SmallVector<Function *, 16> InlinedFunctions;
Chandler Carruth67fc52f2016-08-17 02:56:20 +000040 for (Function &F : M)
41 if (!F.isDeclaration() && F.hasFnAttribute(Attribute::AlwaysInline) &&
42 isInlineViable(F)) {
43 Calls.clear();
44
45 for (User *U : F.users())
46 if (auto CS = CallSite(U))
47 if (CS.getCalledFunction() == &F)
48 Calls.insert(CS);
49
50 for (CallSite CS : Calls)
51 // FIXME: We really shouldn't be able to fail to inline at this point!
52 // We should do something to log or check the inline failures here.
Justin Bogner6f6846f2018-01-08 22:07:42 +000053 Changed |=
54 InlineFunction(CS, IFI, /*CalleeAAR=*/nullptr, InsertLifetime);
Chandler Carruth4eaff122016-12-23 23:33:35 +000055
56 // Remember to try and delete this function afterward. This both avoids
57 // re-walking the rest of the module and avoids dealing with any iterator
58 // invalidation issues while deleting functions.
59 InlinedFunctions.push_back(&F);
Chandler Carruth67fc52f2016-08-17 02:56:20 +000060 }
61
Chandler Carruth6e9bb7e2016-12-26 23:43:27 +000062 // Remove any live functions.
63 erase_if(InlinedFunctions, [&](Function *F) {
64 F->removeDeadConstantUsers();
65 return !F->isDefTriviallyDead();
66 });
67
68 // Delete the non-comdat ones from the module and also from our vector.
69 auto NonComdatBegin = partition(
70 InlinedFunctions, [&](Function *F) { return F->hasComdat(); });
71 for (Function *F : make_range(NonComdatBegin, InlinedFunctions.end()))
72 M.getFunctionList().erase(F);
73 InlinedFunctions.erase(NonComdatBegin, InlinedFunctions.end());
74
75 if (!InlinedFunctions.empty()) {
76 // Now we just have the comdat functions. Filter out the ones whose comdats
77 // are not actually dead.
78 filterDeadComdatFunctions(M, InlinedFunctions);
79 // The remaining functions are actually dead.
80 for (Function *F : InlinedFunctions)
81 M.getFunctionList().erase(F);
Chandler Carruth4eaff122016-12-23 23:33:35 +000082 }
83
Chandler Carruth67fc52f2016-08-17 02:56:20 +000084 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
85}
86
Devang Patel924d9082008-09-03 18:50:53 +000087namespace {
88
Chandler Carruth67fc52f2016-08-17 02:56:20 +000089/// Inliner pass which only handles "always inline" functions.
90///
91/// Unlike the \c AlwaysInlinerPass, this uses the more heavyweight \c Inliner
92/// base class to provide several facilities such as array alloca merging.
Chandler Carruth1d963112016-12-20 03:15:32 +000093class AlwaysInlinerLegacyPass : public LegacyInlinerBase {
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +000094
Chandler Carruth7e88e742013-01-21 11:39:16 +000095public:
Chandler Carruth1d963112016-12-20 03:15:32 +000096 AlwaysInlinerLegacyPass() : LegacyInlinerBase(ID, /*InsertLifetime*/ true) {
Chandler Carruth67fc52f2016-08-17 02:56:20 +000097 initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
Chandler Carruth7e88e742013-01-21 11:39:16 +000098 }
Matt Beaumont-Gayabfc4462012-12-04 05:41:27 +000099
Chandler Carruth1d963112016-12-20 03:15:32 +0000100 AlwaysInlinerLegacyPass(bool InsertLifetime)
101 : LegacyInlinerBase(ID, InsertLifetime) {
Chandler Carruth67fc52f2016-08-17 02:56:20 +0000102 initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
Chandler Carruth7e88e742013-01-21 11:39:16 +0000103 }
104
Andrew Kaylor9c81d0f2016-05-23 21:57:54 +0000105 /// Main run interface method. We override here to avoid calling skipSCC().
106 bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); }
107
Chandler Carruth7e88e742013-01-21 11:39:16 +0000108 static char ID; // Pass identification, replacement for typeid
109
Craig Topper3e4c6972014-03-05 09:10:37 +0000110 InlineCost getInlineCost(CallSite CS) override;
Chandler Carruth7e88e742013-01-21 11:39:16 +0000111
112 using llvm::Pass::doFinalization;
Craig Topper3e4c6972014-03-05 09:10:37 +0000113 bool doFinalization(CallGraph &CG) override {
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000114 return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true);
Chandler Carruth7e88e742013-01-21 11:39:16 +0000115 }
Chandler Carruth7e88e742013-01-21 11:39:16 +0000116};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000117}
Devang Patel924d9082008-09-03 18:50:53 +0000118
Chandler Carruth67fc52f2016-08-17 02:56:20 +0000119char AlwaysInlinerLegacyPass::ID = 0;
120INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline",
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000121 "Inliner for always_inline functions", false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000122INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth6378cf52013-11-26 04:19:30 +0000123INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Easwaran Raman71069cf2016-06-09 22:23:21 +0000124INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000125INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chandler Carruth67fc52f2016-08-17 02:56:20 +0000126INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline",
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000127 "Inliner for always_inline functions", false, false)
Devang Patel924d9082008-09-03 18:50:53 +0000128
Chandler Carruth67fc52f2016-08-17 02:56:20 +0000129Pass *llvm::createAlwaysInlinerLegacyPass(bool InsertLifetime) {
130 return new AlwaysInlinerLegacyPass(InsertLifetime);
Chad Rosier07d37bc2012-02-25 02:56:01 +0000131}
132
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000133/// Get the inline cost for the always-inliner.
Chandler Carrutha88a0fa2012-03-31 13:17:18 +0000134///
135/// The always inliner *only* handles functions which are marked with the
136/// attribute to force inlining. As such, it is dramatically simpler and avoids
137/// using the powerful (but expensive) inline cost analysis. Instead it uses
138/// a very simple and boring direct walk of the instructions looking for
139/// impossible-to-inline constructs.
140///
141/// Note, it would be possible to go to some lengths to cache the information
142/// computed here, but as we only expect to do this for relatively few and
143/// small functions which have the explicit attribute to force inlining, it is
144/// likely not worth it in practice.
Chandler Carruth67fc52f2016-08-17 02:56:20 +0000145InlineCost AlwaysInlinerLegacyPass::getInlineCost(CallSite CS) {
Chandler Carrutha88a0fa2012-03-31 13:17:18 +0000146 Function *Callee = CS.getCalledFunction();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +0000147
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000148 // Only inline direct calls to functions with always-inline attributes
149 // that are viable for inlining. FIXME: We shouldn't even get here for
150 // declarations.
151 if (Callee && !Callee->isDeclaration() &&
Easwaran Ramanb9f71202015-12-28 20:28:19 +0000152 CS.hasFnAttr(Attribute::AlwaysInline) && isInlineViable(*Callee))
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000153 return InlineCost::getAlways();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +0000154
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000155 return InlineCost::getNever();
Chandler Carrutha88a0fa2012-03-31 13:17:18 +0000156}