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" |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +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" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetLibraryInfo.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 { |
Matt Beaumont-Gay | abfc446 | 2012-12-04 05:41:27 +0000 | [diff] [blame] | 38 | |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 39 | public: |
Easwaran Raman | f4bb2f0 | 2016-01-14 23:16:29 +0000 | [diff] [blame] | 40 | AlwaysInliner() : Inliner(ID, /*InsertLifetime*/ true) { |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 41 | initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry()); |
| 42 | } |
Matt Beaumont-Gay | abfc446 | 2012-12-04 05:41:27 +0000 | [diff] [blame] | 43 | |
Easwaran Raman | f4bb2f0 | 2016-01-14 23:16:29 +0000 | [diff] [blame] | 44 | AlwaysInliner(bool InsertLifetime) : Inliner(ID, InsertLifetime) { |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 45 | initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry()); |
| 46 | } |
| 47 | |
Andrew Kaylor | 9c81d0f | 2016-05-23 21:57:54 +0000 | [diff] [blame^] | 48 | /// Main run interface method. We override here to avoid calling skipSCC(). |
| 49 | bool runOnSCC(CallGraphSCC &SCC) override { return inlineCalls(SCC); } |
| 50 | |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 51 | static char ID; // Pass identification, replacement for typeid |
| 52 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 53 | InlineCost getInlineCost(CallSite CS) override; |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 54 | |
| 55 | using llvm::Pass::doFinalization; |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 56 | bool doFinalization(CallGraph &CG) override { |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 57 | return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/ true); |
| 58 | } |
Chandler Carruth | 7e88e74 | 2013-01-21 11:39:16 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 61 | } |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 62 | |
| 63 | char AlwaysInliner::ID = 0; |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 64 | INITIALIZE_PASS_BEGIN(AlwaysInliner, "always-inline", |
| 65 | "Inliner for always_inline functions", false, false) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 66 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 67 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 68 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 69 | INITIALIZE_PASS_END(AlwaysInliner, "always-inline", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 70 | "Inliner for always_inline functions", false, false) |
Devang Patel | 924d908 | 2008-09-03 18:50:53 +0000 | [diff] [blame] | 71 | |
| 72 | Pass *llvm::createAlwaysInlinerPass() { return new AlwaysInliner(); } |
| 73 | |
Chad Rosier | 07d37bc | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 74 | Pass *llvm::createAlwaysInlinerPass(bool InsertLifetime) { |
| 75 | return new AlwaysInliner(InsertLifetime); |
| 76 | } |
| 77 | |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 78 | /// \brief Get the inline cost for the always-inliner. |
| 79 | /// |
| 80 | /// The always inliner *only* handles functions which are marked with the |
| 81 | /// attribute to force inlining. As such, it is dramatically simpler and avoids |
| 82 | /// using the powerful (but expensive) inline cost analysis. Instead it uses |
| 83 | /// a very simple and boring direct walk of the instructions looking for |
| 84 | /// impossible-to-inline constructs. |
| 85 | /// |
| 86 | /// Note, it would be possible to go to some lengths to cache the information |
| 87 | /// computed here, but as we only expect to do this for relatively few and |
| 88 | /// small functions which have the explicit attribute to force inlining, it is |
| 89 | /// likely not worth it in practice. |
| 90 | InlineCost AlwaysInliner::getInlineCost(CallSite CS) { |
| 91 | Function *Callee = CS.getCalledFunction(); |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 92 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 93 | // Only inline direct calls to functions with always-inline attributes |
| 94 | // that are viable for inlining. FIXME: We shouldn't even get here for |
| 95 | // declarations. |
| 96 | if (Callee && !Callee->isDeclaration() && |
Easwaran Raman | b9f7120 | 2015-12-28 20:28:19 +0000 | [diff] [blame] | 97 | CS.hasFnAttr(Attribute::AlwaysInline) && isInlineViable(*Callee)) |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 98 | return InlineCost::getAlways(); |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 99 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 100 | return InlineCost::getNever(); |
Chandler Carruth | a88a0fa | 2012-03-31 13:17:18 +0000 | [diff] [blame] | 101 | } |