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 | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/IPO.h" |
| 16 | #include "llvm/ADT/SmallPtrSet.h" |
Hal Finkel | 0c08302 | 2014-09-01 09:01:39 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CallGraph.h" |
| 20 | #include "llvm/Analysis/InlineCost.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CallingConv.h" |
| 23 | #include "llvm/IR/DataLayout.h" |
| 24 | #include "llvm/IR/Instructions.h" |
| 25 | #include "llvm/IR/IntrinsicInst.h" |
| 26 | #include "llvm/IR/Module.h" |
| 27 | #include "llvm/IR/Type.h" |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/IPO/InlinerPass.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 | |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 36 | /// \brief Inliner pass which only handles "always inline" functions. |
| 37 | class AlwaysInliner : public Inliner { |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 38 | InlineCostAnalysis *ICA; |
Matt Beaumont-Gay | abfc446 | 2012-12-04 05:41:27 +0000 | [diff] [blame] | 39 | |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 40 | public: |
| 41 | // Use extremely low threshold. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 42 | AlwaysInliner() : Inliner(ID, -2000000000, /*InsertLifetime*/ true), |
| 43 | ICA(nullptr) { |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 44 | initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry()); |
| 45 | } |
Matt Beaumont-Gay | abfc446 | 2012-12-04 05:41:27 +0000 | [diff] [blame] | 46 | |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 47 | AlwaysInliner(bool InsertLifetime) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 48 | : Inliner(ID, -2000000000, InsertLifetime), ICA(nullptr) { |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 49 | initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry()); |
| 50 | } |
| 51 | |
| 52 | static char ID; // Pass identification, replacement for typeid |
| 53 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 54 | InlineCost getInlineCost(CallSite CS) override; |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 55 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 56 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 57 | bool runOnSCC(CallGraphSCC &SCC) override; |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 58 | |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 59 | using llvm::Pass::doFinalization; |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 60 | bool doFinalization(CallGraph &CG) override { |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 61 | return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true); |
| 62 | } |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame^] | 65 | } |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 66 | |
| 67 | char AlwaysInliner::ID = 0; |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 68 | INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline", |
| 69 | "Inliner for always_inline functions", false, false) |
Hal Finkel | 0c08302 | 2014-09-01 09:01:39 +0000 | [diff] [blame] | 70 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 71 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 72 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 73 | INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis) |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 74 | INITIALIZE_PASS_END(AlwaysInliner, "always-inline", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 75 | "Inliner for always_inline functions", false, false) |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 76 | |
| 77 | Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); } |
| 78 | |
Chad Rosier | 07d37bc | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 79 | Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) { |
| 80 | return new AlwaysInliner(InsertLifetime); |
| 81 | } |
| 82 | |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 83 | /// \brief Get the inline cost for the always-inliner. |
| 84 | /// |
| 85 | /// The always inliner *only* handles functions which are marked with the |
| 86 | /// attribute to force inlining. As such, it is dramatically simpler and avoids |
| 87 | /// using the powerful (but expensive) inline cost analysis. Instead it uses |
| 88 | /// a very simple and boring direct walk of the instructions looking for |
| 89 | /// impossible-to-inline constructs. |
| 90 | /// |
| 91 | /// Note, it would be possible to go to some lengths to cache the information |
| 92 | /// computed here, but as we only expect to do this for relatively few and |
| 93 | /// small functions which have the explicit attribute to force inlining, it is |
| 94 | /// likely not worth it in practice. |
| 95 | InlineCost AlwaysInliner::getInlineCost(CallSite CS) { |
| 96 | Function *Callee = CS.getCalledFunction(); |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 97 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 98 | // Only inline direct calls to functions with always-inline attributes |
| 99 | // that are viable for inlining. FIXME: We shouldn't even get here for |
| 100 | // declarations. |
| 101 | if (Callee && !Callee->isDeclaration() && |
Peter Collingbourne | 68a8897 | 2014-05-19 18:25:54 +0000 | [diff] [blame] | 102 | CS.hasFnAttr(Attribute::AlwaysInline) && |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 103 | ICA->isInlineViable(*Callee)) |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 104 | return InlineCost::getAlways(); |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 105 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 106 | return InlineCost::getNever(); |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 109 | bool AlwaysInliner::runOnSCC(CallGraphSCC &SCC) { |
| 110 | ICA = &getAnalysis<InlineCostAnalysis>(); |
| 111 | return Inliner::runOnSCC(SCC); |
| 112 | } |
| 113 | |
| 114 | void AlwaysInliner::getAnalysisUsage(AnalysisUsage &AU) const { |
| 115 | AU.addRequired<InlineCostAnalysis>(); |
| 116 | Inliner::getAnalysisUsage(AU); |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 117 | } |