Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 1 | //===- InlineSimple.cpp - Code to perform simple function inlining --------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | ca398dc | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 10 | // This file implements bottom-up inlining of functions into callees. |
Chris Lattner | 0154505 | 2002-04-18 18:52:03 +0000 | [diff] [blame] | 11 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/IPO.h" |
| 15 | #include "llvm/Analysis/CallGraph.h" |
| 16 | #include "llvm/Analysis/InlineCost.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 17 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/CallingConv.h" |
| 19 | #include "llvm/IR/DataLayout.h" |
| 20 | #include "llvm/IR/Instructions.h" |
| 21 | #include "llvm/IR/IntrinsicInst.h" |
| 22 | #include "llvm/IR/Module.h" |
| 23 | #include "llvm/IR/Type.h" |
Tanya Lattner | 6f7426e | 2007-06-19 22:29:50 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/IPO/InlinerPass.h" |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 28 | #define DEBUG_TYPE "inline" |
| 29 | |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 30 | namespace { |
Chris Lattner | 884d6c4 | 2003-10-06 15:52:43 +0000 | [diff] [blame] | 31 | |
David Majnemer | 4286407 | 2013-11-03 11:09:39 +0000 | [diff] [blame] | 32 | /// \brief Actual inliner pass implementation. |
Chandler Carruth | 5a47127 | 2013-01-21 11:39:14 +0000 | [diff] [blame] | 33 | /// |
| 34 | /// The common implementation of the inlining logic is shared between this |
| 35 | /// inliner pass and the always inliner pass. The two passes use different cost |
| 36 | /// analyses to determine when to inline. |
| 37 | class SimpleInliner : public Inliner { |
Chandler Carruth | 86953b5 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 38 | InlineCostAnalysis *ICA; |
Chandler Carruth | 5a47127 | 2013-01-21 11:39:14 +0000 | [diff] [blame] | 39 | |
| 40 | public: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 41 | SimpleInliner() : Inliner(ID), ICA(nullptr) { |
Chandler Carruth | 5a47127 | 2013-01-21 11:39:14 +0000 | [diff] [blame] | 42 | initializeSimpleInlinerPass(*PassRegistry::getPassRegistry()); |
| 43 | } |
| 44 | |
| 45 | SimpleInliner(int Threshold) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 46 | : Inliner(ID, Threshold, /*InsertLifetime*/ true), ICA(nullptr) { |
Chandler Carruth | 5a47127 | 2013-01-21 11:39:14 +0000 | [diff] [blame] | 47 | initializeSimpleInlinerPass(*PassRegistry::getPassRegistry()); |
| 48 | } |
| 49 | |
| 50 | static char ID; // Pass identification, replacement for typeid |
| 51 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 52 | InlineCost getInlineCost(CallSite CS) override { |
Chandler Carruth | 86953b5 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 53 | return ICA->getInlineCost(CS, getInlineThreshold(CS)); |
Chandler Carruth | 5a47127 | 2013-01-21 11:39:14 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 56 | bool runOnSCC(CallGraphSCC &SCC) override; |
| 57 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Chandler Carruth | 5a47127 | 2013-01-21 11:39:14 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 60 | static int computeThresholdFromOptLevels(unsigned OptLevel, |
| 61 | unsigned SizeOptLevel) { |
| 62 | if (OptLevel > 2) |
| 63 | return 275; |
| 64 | if (SizeOptLevel == 1) // -Os |
| 65 | return 75; |
| 66 | if (SizeOptLevel == 2) // -Oz |
| 67 | return 25; |
| 68 | return 225; |
| 69 | } |
| 70 | |
Chandler Carruth | 5a47127 | 2013-01-21 11:39:14 +0000 | [diff] [blame] | 71 | } // end anonymous namespace |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 72 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 73 | char SimpleInliner::ID = 0; |
Owen Anderson | ae0a7bc | 2010-10-13 22:00:45 +0000 | [diff] [blame] | 74 | INITIALIZE_PASS_BEGIN(SimpleInliner, "inline", |
| 75 | "Function Integration/Inlining", false, false) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 76 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Chandler Carruth | 86953b5 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 77 | INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis) |
Owen Anderson | ae0a7bc | 2010-10-13 22:00:45 +0000 | [diff] [blame] | 78 | INITIALIZE_PASS_END(SimpleInliner, "inline", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 79 | "Function Integration/Inlining", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 80 | |
Devang Patel | c71ca3c | 2007-01-26 00:47:38 +0000 | [diff] [blame] | 81 | Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); } |
Chris Lattner | 869adc2 | 2003-11-21 21:46:09 +0000 | [diff] [blame] | 82 | |
Andrew Trick | 5c65541 | 2011-10-01 01:27:56 +0000 | [diff] [blame] | 83 | Pass *llvm::createFunctionInliningPass(int Threshold) { |
Chris Lattner | 120d053 | 2008-01-12 06:49:13 +0000 | [diff] [blame] | 84 | return new SimpleInliner(Threshold); |
| 85 | } |
| 86 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 87 | Pass *llvm::createFunctionInliningPass(unsigned OptLevel, |
| 88 | unsigned SizeOptLevel) { |
| 89 | return new SimpleInliner( |
| 90 | computeThresholdFromOptLevels(OptLevel, SizeOptLevel)); |
| 91 | } |
| 92 | |
Chandler Carruth | 86953b5 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 93 | bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) { |
| 94 | ICA = &getAnalysis<InlineCostAnalysis>(); |
| 95 | return Inliner::runOnSCC(SCC); |
Tanya Lattner | 682f683 | 2007-06-06 21:59:26 +0000 | [diff] [blame] | 96 | } |
Devang Patel | 6899b31 | 2007-07-25 18:00:25 +0000 | [diff] [blame] | 97 | |
Chandler Carruth | 86953b5 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 98 | void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const { |
| 99 | AU.addRequired<InlineCostAnalysis>(); |
| 100 | Inliner::getAnalysisUsage(AU); |
| 101 | } |