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