Devang Patel | 50c66cd | 2008-09-03 19:52:17 +0000 | [diff] [blame] | 1 | //===- InlineAlways.cpp - Code to inline always_inline functions ----------===// |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Devang Patel | 50c66cd | 2008-09-03 19:52:17 +0000 | [diff] [blame] | 9 | // This file implements a custom inliner that handles only functions that |
Devang Patel | a563d24 | 2008-09-03 20:25:40 +0000 | [diff] [blame] | 10 | // are marked as "always inline". |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/IPO/AlwaysInliner.h" |
| 15 | #include "llvm/ADT/SetVector.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/InlineCost.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 19 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/CallingConv.h" |
| 21 | #include "llvm/IR/DataLayout.h" |
| 22 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IR/Type.h" |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/IPO.h" |
| 26 | #include "llvm/Transforms/IPO/Inliner.h" |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/Cloning.h" |
Chandler Carruth | 6e9bb7e | 2016-12-26 23:43:27 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "inline" |
| 33 | |
Leonard Chan | 09f56b5 | 2019-06-13 18:18:40 +0000 | [diff] [blame] | 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 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 40 | [&](Function &F) -> AssumptionCache & { |
| 41 | return FAM.getResult<AssumptionAnalysis>(F); |
| 42 | }; |
| 43 | InlineFunctionInfo IFI(/*cg=*/nullptr, &GetAssumptionCache); |
| 44 | |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 45 | SmallSetVector<CallSite, 16> Calls; |
| 46 | bool Changed = false; |
Chandler Carruth | 4eaff12 | 2016-12-23 23:33:35 +0000 | [diff] [blame] | 47 | SmallVector<Function *, 16> InlinedFunctions; |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 48 | for (Function &F : M) |
| 49 | if (!F.isDeclaration() && F.hasFnAttribute(Attribute::AlwaysInline) && |
| 50 | isInlineViable(F)) { |
| 51 | Calls.clear(); |
| 52 | |
| 53 | for (User *U : F.users()) |
| 54 | if (auto CS = CallSite(U)) |
| 55 | if (CS.getCalledFunction() == &F) |
| 56 | Calls.insert(CS); |
| 57 | |
| 58 | for (CallSite CS : Calls) |
| 59 | // FIXME: We really shouldn't be able to fail to inline at this point! |
| 60 | // We should do something to log or check the inline failures here. |
Justin Bogner | 6f6846f | 2018-01-08 22:07:42 +0000 | [diff] [blame] | 61 | Changed |= |
| 62 | InlineFunction(CS, IFI, /*CalleeAAR=*/nullptr, InsertLifetime); |
Chandler Carruth | 4eaff12 | 2016-12-23 23:33:35 +0000 | [diff] [blame] | 63 | |
| 64 | // Remember to try and delete this function afterward. This both avoids |
| 65 | // re-walking the rest of the module and avoids dealing with any iterator |
| 66 | // invalidation issues while deleting functions. |
| 67 | InlinedFunctions.push_back(&F); |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Chandler Carruth | 6e9bb7e | 2016-12-26 23:43:27 +0000 | [diff] [blame] | 70 | // Remove any live functions. |
| 71 | erase_if(InlinedFunctions, [&](Function *F) { |
| 72 | F->removeDeadConstantUsers(); |
| 73 | return !F->isDefTriviallyDead(); |
| 74 | }); |
| 75 | |
| 76 | // Delete the non-comdat ones from the module and also from our vector. |
| 77 | auto NonComdatBegin = partition( |
| 78 | InlinedFunctions, [&](Function *F) { return F->hasComdat(); }); |
| 79 | for (Function *F : make_range(NonComdatBegin, InlinedFunctions.end())) |
| 80 | M.getFunctionList().erase(F); |
| 81 | InlinedFunctions.erase(NonComdatBegin, InlinedFunctions.end()); |
| 82 | |
| 83 | if (!InlinedFunctions.empty()) { |
| 84 | // Now we just have the comdat functions. Filter out the ones whose comdats |
| 85 | // are not actually dead. |
| 86 | filterDeadComdatFunctions(M, InlinedFunctions); |
| 87 | // The remaining functions are actually dead. |
| 88 | for (Function *F : InlinedFunctions) |
| 89 | M.getFunctionList().erase(F); |
Chandler Carruth | 4eaff12 | 2016-12-23 23:33:35 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 92 | return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); |
| 93 | } |
| 94 | |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 95 | namespace { |
| 96 | |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 97 | /// Inliner pass which only handles "always inline" functions. |
| 98 | /// |
| 99 | /// Unlike the \c AlwaysInlinerPass, this uses the more heavyweight \c Inliner |
| 100 | /// base class to provide several facilities such as array alloca merging. |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 101 | class AlwaysInlinerLegacyPass : public LegacyInlinerBase { |
Matt Beaumont-Gay | abfc446 | 2012-12-04 05:41:27 +0000 | [diff] [blame] | 102 | |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 103 | public: |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 104 | AlwaysInlinerLegacyPass() : LegacyInlinerBase(ID, /*InsertLifetime*/ true) { |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 105 | initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 106 | } |
Matt Beaumont-Gay | abfc446 | 2012-12-04 05:41:27 +0000 | [diff] [blame] | 107 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 108 | AlwaysInlinerLegacyPass(bool InsertLifetime) |
| 109 | : LegacyInlinerBase(ID, InsertLifetime) { |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 110 | initializeAlwaysInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Andrew Kaylor | 9c81d0f | 2016-05-23 21:57:54 +0000 | [diff] [blame] | 113 | /// Main run interface method. We override here to avoid calling skipSCC(). |
| 114 | bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); } |
| 115 | |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 116 | static char ID; // Pass identification, replacement for typeid |
| 117 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 118 | InlineCost getInlineCost(CallSite CS) override; |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 119 | |
| 120 | using llvm::Pass::doFinalization; |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 121 | bool doFinalization(CallGraph &CG) override { |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 122 | return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true); |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 123 | } |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 124 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 125 | } |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 126 | |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 127 | char AlwaysInlinerLegacyPass::ID = 0; |
| 128 | INITIALIZE_PASS_BEGIN(AlwaysInlinerLegacyPass, "always-inline", |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 129 | "Inliner for always_inline functions", false, false) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 130 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 131 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Easwaran Raman | 71069cf | 2016-06-09 22:23:21 +0000 | [diff] [blame] | 132 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 133 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 134 | INITIALIZE_PASS_END(AlwaysInlinerLegacyPass, "always-inline", |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 135 | "Inliner for always_inline functions", false, false) |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 136 | |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 137 | Pass *llvm::createAlwaysInlinerLegacyPass(bool InsertLifetime) { |
| 138 | return new AlwaysInlinerLegacyPass(InsertLifetime); |
Chad Rosier | 07d37bc | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 141 | /// Get the inline cost for the always-inliner. |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 142 | /// |
| 143 | /// The always inliner *only* handles functions which are marked with the |
| 144 | /// attribute to force inlining. As such, it is dramatically simpler and avoids |
| 145 | /// using the powerful (but expensive) inline cost analysis. Instead it uses |
| 146 | /// a very simple and boring direct walk of the instructions looking for |
| 147 | /// impossible-to-inline constructs. |
| 148 | /// |
| 149 | /// Note, it would be possible to go to some lengths to cache the information |
| 150 | /// computed here, but as we only expect to do this for relatively few and |
| 151 | /// small functions which have the explicit attribute to force inlining, it is |
| 152 | /// likely not worth it in practice. |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 153 | InlineCost AlwaysInlinerLegacyPass::getInlineCost(CallSite CS) { |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 154 | Function *Callee = CS.getCalledFunction(); |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 155 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 156 | // Only inline direct calls to functions with always-inline attributes |
Yevgeny Rouban | 15b17d0 | 2019-02-01 10:44:43 +0000 | [diff] [blame] | 157 | // that are viable for inlining. |
| 158 | if (!Callee) |
| 159 | return InlineCost::getNever("indirect call"); |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 160 | |
Yevgeny Rouban | 15b17d0 | 2019-02-01 10:44:43 +0000 | [diff] [blame] | 161 | // FIXME: We shouldn't even get here for declarations. |
| 162 | if (Callee->isDeclaration()) |
| 163 | return InlineCost::getNever("no definition"); |
| 164 | |
| 165 | if (!CS.hasFnAttr(Attribute::AlwaysInline)) |
| 166 | return InlineCost::getNever("no alwaysinline attribute"); |
| 167 | |
| 168 | auto IsViable = isInlineViable(*Callee); |
| 169 | if (!IsViable) |
| 170 | return InlineCost::getNever(IsViable.message); |
| 171 | |
| 172 | return InlineCost::getAlways("always inliner"); |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 173 | } |