Chris Lattner | 7d30a6c | 2004-06-20 04:11:48 +0000 | [diff] [blame] | 1 | //===- Inliner.cpp - Code common to all inliners --------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 6754b82 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 10 | // This file implements the mechanics required to implement inlining without |
| 11 | // missing any calls and updating the call graph. The decisions of which calls |
| 12 | // are profitable to inline are implemented elsewhere. |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/IPO/Inliner.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallPtrSet.h" |
| 18 | #include "llvm/ADT/Statistic.h" |
Hal Finkel | 0c08302 | 2014-09-01 09:01:39 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Easwaran Raman | 12585b0 | 2017-01-20 22:44:04 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/CallGraph.h" |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/InlineCost.h" |
Adam Nemet | 896c09b | 2016-08-10 00:44:44 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
Easwaran Raman | 71069cf | 2016-06-09 22:23:21 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 28 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DataLayout.h" |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 31 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Instructions.h" |
| 33 | #include "llvm/IR/IntrinsicInst.h" |
| 34 | #include "llvm/IR/Module.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Utils/Cloning.h" |
| 38 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | 6e9bb7e | 2016-12-26 23:43:27 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Chris Lattner | a82f131 | 2003-11-21 21:45:31 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 41 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "inline" |
| 43 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 44 | STATISTIC(NumInlined, "Number of functions inlined"); |
Chris Lattner | b49a622 | 2010-05-01 17:19:38 +0000 | [diff] [blame] | 45 | STATISTIC(NumCallsDeleted, "Number of call sites deleted, not inlined"); |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 46 | STATISTIC(NumDeleted, "Number of functions deleted because all callers found"); |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 47 | STATISTIC(NumMergedAllocas, "Number of allocas merged together"); |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 48 | |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 49 | // This weirdly named statistic tracks the number of times that, when attempting |
Chandler Carruth | 7ae90d4 | 2012-04-11 10:15:10 +0000 | [diff] [blame] | 50 | // to inline a function A into B, we analyze the callers of B in order to see |
| 51 | // if those would be more profitable and blocked inline steps. |
| 52 | STATISTIC(NumCallerCallersAnalyzed, "Number of caller-callers analyzed"); |
| 53 | |
Chandler Carruth | f702d8e | 2016-08-17 02:40:23 +0000 | [diff] [blame] | 54 | /// Flag to disable manual alloca merging. |
| 55 | /// |
| 56 | /// Merging of allocas was originally done as a stack-size saving technique |
| 57 | /// prior to LLVM's code generator having support for stack coloring based on |
| 58 | /// lifetime markers. It is now in the process of being removed. To experiment |
| 59 | /// with disabling it and relying fully on lifetime marker based stack |
| 60 | /// coloring, you can pass this flag to LLVM. |
| 61 | static cl::opt<bool> |
| 62 | DisableInlinedAllocaMerging("disable-inlined-alloca-merging", |
| 63 | cl::init(false), cl::Hidden); |
| 64 | |
Piotr Padlewski | 84abc74 | 2016-07-29 00:27:16 +0000 | [diff] [blame] | 65 | namespace { |
| 66 | enum class InlinerFunctionImportStatsOpts { |
| 67 | No = 0, |
| 68 | Basic = 1, |
| 69 | Verbose = 2, |
| 70 | }; |
| 71 | |
| 72 | cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats( |
| 73 | "inliner-function-import-stats", |
| 74 | cl::init(InlinerFunctionImportStatsOpts::No), |
| 75 | cl::values(clEnumValN(InlinerFunctionImportStatsOpts::Basic, "basic", |
| 76 | "basic statistics"), |
| 77 | clEnumValN(InlinerFunctionImportStatsOpts::Verbose, "verbose", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 78 | "printing of statistics for each inlined function")), |
Piotr Padlewski | 84abc74 | 2016-07-29 00:27:16 +0000 | [diff] [blame] | 79 | cl::Hidden, cl::desc("Enable inliner stats for imported functions")); |
| 80 | } // namespace |
| 81 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 82 | LegacyInlinerBase::LegacyInlinerBase(char &ID) |
| 83 | : CallGraphSCCPass(ID), InsertLifetime(true) {} |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 84 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 85 | LegacyInlinerBase::LegacyInlinerBase(char &ID, bool InsertLifetime) |
Easwaran Raman | b1bd398 | 2016-03-08 00:36:35 +0000 | [diff] [blame] | 86 | : CallGraphSCCPass(ID), InsertLifetime(InsertLifetime) {} |
Chris Lattner | 22ad7ab | 2008-01-12 06:49:13 +0000 | [diff] [blame] | 87 | |
Sanjay Patel | f1b0db1 | 2015-03-10 16:42:24 +0000 | [diff] [blame] | 88 | /// For this class, we declare that we require and preserve the call graph. |
| 89 | /// If the derived class implements this method, it should |
Chris Lattner | f94bed3 | 2007-01-30 23:28:39 +0000 | [diff] [blame] | 90 | /// always explicitly call the implementation here. |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 91 | void LegacyInlinerBase::getAnalysisUsage(AnalysisUsage &AU) const { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 92 | AU.addRequired<AssumptionCacheTracker>(); |
Easwaran Raman | 71069cf | 2016-06-09 22:23:21 +0000 | [diff] [blame] | 93 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 94 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 95 | getAAResultsAnalysisUsage(AU); |
Chandler Carruth | e40e60e | 2012-12-27 11:17:15 +0000 | [diff] [blame] | 96 | CallGraphSCCPass::getAnalysisUsage(AU); |
Chris Lattner | f94bed3 | 2007-01-30 23:28:39 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 99 | typedef DenseMap<ArrayType *, std::vector<AllocaInst *>> InlinedArrayAllocasTy; |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 100 | |
Chandler Carruth | f702d8e | 2016-08-17 02:40:23 +0000 | [diff] [blame] | 101 | /// Look at all of the allocas that we inlined through this call site. If we |
| 102 | /// have already inlined other allocas through other calls into this function, |
| 103 | /// then we know that they have disjoint lifetimes and that we can merge them. |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 104 | /// |
Chandler Carruth | f702d8e | 2016-08-17 02:40:23 +0000 | [diff] [blame] | 105 | /// There are many heuristics possible for merging these allocas, and the |
| 106 | /// different options have different tradeoffs. One thing that we *really* |
| 107 | /// don't want to hurt is SRoA: once inlining happens, often allocas are no |
| 108 | /// longer address taken and so they can be promoted. |
| 109 | /// |
| 110 | /// Our "solution" for that is to only merge allocas whose outermost type is an |
| 111 | /// array type. These are usually not promoted because someone is using a |
| 112 | /// variable index into them. These are also often the most important ones to |
| 113 | /// merge. |
| 114 | /// |
| 115 | /// A better solution would be to have real memory lifetime markers in the IR |
| 116 | /// and not have the inliner do any merging of allocas at all. This would |
| 117 | /// allow the backend to do proper stack slot coloring of all allocas that |
| 118 | /// *actually make it to the backend*, which is really what we want. |
| 119 | /// |
| 120 | /// Because we don't have this information, we do this simple and useful hack. |
| 121 | static void mergeInlinedArrayAllocas( |
| 122 | Function *Caller, InlineFunctionInfo &IFI, |
| 123 | InlinedArrayAllocasTy &InlinedArrayAllocas, int InlineHistory) { |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 124 | SmallPtrSet<AllocaInst *, 16> UsedAllocas; |
| 125 | |
Chris Lattner | fb212de | 2010-12-06 07:52:42 +0000 | [diff] [blame] | 126 | // When processing our SCC, check to see if CS was inlined from some other |
| 127 | // call site. For example, if we're processing "A" in this code: |
| 128 | // A() { B() } |
| 129 | // B() { x = alloca ... C() } |
| 130 | // C() { y = alloca ... } |
| 131 | // Assume that C was not inlined into B initially, and so we're processing A |
| 132 | // and decide to inline B into A. Doing this makes an alloca available for |
| 133 | // reuse and makes a callsite (C) available for inlining. When we process |
| 134 | // the C call site we don't want to do any alloca merging between X and Y |
| 135 | // because their scopes are not disjoint. We could make this smarter by |
| 136 | // keeping track of the inline history for each alloca in the |
| 137 | // InlinedArrayAllocas but this isn't likely to be a significant win. |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 138 | if (InlineHistory != -1) // Only do merging for top-level call sites in SCC. |
Chandler Carruth | f702d8e | 2016-08-17 02:40:23 +0000 | [diff] [blame] | 139 | return; |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 140 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 141 | // Loop over all the allocas we have so far and see if they can be merged with |
| 142 | // a previously inlined alloca. If not, remember that we had it. |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 143 | for (unsigned AllocaNo = 0, e = IFI.StaticAllocas.size(); AllocaNo != e; |
| 144 | ++AllocaNo) { |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 145 | AllocaInst *AI = IFI.StaticAllocas[AllocaNo]; |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 146 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 147 | // Don't bother trying to merge array allocations (they will usually be |
| 148 | // canonicalized to be an allocation *of* an array), or allocations whose |
| 149 | // type is not itself an array (because we're afraid of pessimizing SRoA). |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 150 | ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 151 | if (!ATy || AI->isArrayAllocation()) |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 152 | continue; |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 153 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 154 | // Get the list of all available allocas for this array type. |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 155 | std::vector<AllocaInst *> &AllocasForType = InlinedArrayAllocas[ATy]; |
| 156 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 157 | // Loop over the allocas in AllocasForType to see if we can reuse one. Note |
| 158 | // that we have to be careful not to reuse the same "available" alloca for |
| 159 | // multiple different allocas that we just inlined, we use the 'UsedAllocas' |
| 160 | // set to keep track of which "available" allocas are being used by this |
| 161 | // function. Also, AllocasForType can be empty of course! |
| 162 | bool MergedAwayAlloca = false; |
Yaron Keren | 62064d6 | 2015-06-25 19:28:24 +0000 | [diff] [blame] | 163 | for (AllocaInst *AvailableAlloca : AllocasForType) { |
Hal Finkel | 9caa8f7 | 2013-07-16 17:10:55 +0000 | [diff] [blame] | 164 | |
| 165 | unsigned Align1 = AI->getAlignment(), |
| 166 | Align2 = AvailableAlloca->getAlignment(); |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 167 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 168 | // The available alloca has to be in the right function, not in some other |
| 169 | // function in this SCC. |
| 170 | if (AvailableAlloca->getParent() != AI->getParent()) |
| 171 | continue; |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 172 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 173 | // If the inlined function already uses this alloca then we can't reuse |
| 174 | // it. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 175 | if (!UsedAllocas.insert(AvailableAlloca).second) |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 176 | continue; |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 177 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 178 | // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare |
| 179 | // success! |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 180 | DEBUG(dbgs() << " ***MERGED ALLOCA: " << *AI |
| 181 | << "\n\t\tINTO: " << *AvailableAlloca << '\n'); |
| 182 | |
Evgeniy Stepanov | d8b86f7 | 2015-09-29 00:30:19 +0000 | [diff] [blame] | 183 | // Move affected dbg.declare calls immediately after the new alloca to |
Simon Pilgrim | 7d18a70 | 2016-11-20 13:19:49 +0000 | [diff] [blame] | 184 | // avoid the situation when a dbg.declare precedes its alloca. |
Evgeniy Stepanov | d8b86f7 | 2015-09-29 00:30:19 +0000 | [diff] [blame] | 185 | if (auto *L = LocalAsMetadata::getIfExists(AI)) |
| 186 | if (auto *MDV = MetadataAsValue::getIfExists(AI->getContext(), L)) |
| 187 | for (User *U : MDV->users()) |
| 188 | if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U)) |
| 189 | DDI->moveBefore(AvailableAlloca->getNextNode()); |
| 190 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 191 | AI->replaceAllUsesWith(AvailableAlloca); |
Hal Finkel | 9caa8f7 | 2013-07-16 17:10:55 +0000 | [diff] [blame] | 192 | |
Hal Finkel | ec7cd26 | 2013-07-17 14:32:41 +0000 | [diff] [blame] | 193 | if (Align1 != Align2) { |
| 194 | if (!Align1 || !Align2) { |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 195 | const DataLayout &DL = Caller->getParent()->getDataLayout(); |
| 196 | unsigned TypeAlign = DL.getABITypeAlignment(AI->getAllocatedType()); |
Hal Finkel | ec7cd26 | 2013-07-17 14:32:41 +0000 | [diff] [blame] | 197 | |
| 198 | Align1 = Align1 ? Align1 : TypeAlign; |
| 199 | Align2 = Align2 ? Align2 : TypeAlign; |
| 200 | } |
| 201 | |
| 202 | if (Align1 > Align2) |
| 203 | AvailableAlloca->setAlignment(AI->getAlignment()); |
| 204 | } |
Hal Finkel | 9caa8f7 | 2013-07-16 17:10:55 +0000 | [diff] [blame] | 205 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 206 | AI->eraseFromParent(); |
| 207 | MergedAwayAlloca = true; |
| 208 | ++NumMergedAllocas; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 209 | IFI.StaticAllocas[AllocaNo] = nullptr; |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 210 | break; |
| 211 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 212 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 213 | // If we already nuked the alloca, we're done with it. |
| 214 | if (MergedAwayAlloca) |
| 215 | continue; |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 216 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 217 | // If we were unable to merge away the alloca either because there are no |
| 218 | // allocas of the right type available or because we reused them all |
| 219 | // already, remember that this alloca came from an inlined function and mark |
| 220 | // it used so we don't reuse it for other allocas from this inline |
| 221 | // operation. |
| 222 | AllocasForType.push_back(AI); |
| 223 | UsedAllocas.insert(AI); |
Chris Lattner | 6754b82 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 224 | } |
Chandler Carruth | f702d8e | 2016-08-17 02:40:23 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /// If it is possible to inline the specified call site, |
| 228 | /// do so and update the CallGraph for this operation. |
| 229 | /// |
| 230 | /// This function also does some basic book-keeping to update the IR. The |
| 231 | /// InlinedArrayAllocas map keeps track of any allocas that are already |
| 232 | /// available from other functions inlined into the caller. If we are able to |
| 233 | /// inline this call site we attempt to reuse already available allocas or add |
| 234 | /// any new allocas to the set if not possible. |
| 235 | static bool InlineCallIfPossible( |
| 236 | CallSite CS, InlineFunctionInfo &IFI, |
| 237 | InlinedArrayAllocasTy &InlinedArrayAllocas, int InlineHistory, |
| 238 | bool InsertLifetime, function_ref<AAResults &(Function &)> &AARGetter, |
| 239 | ImportedFunctionsInliningStatistics &ImportedFunctionsStats) { |
| 240 | Function *Callee = CS.getCalledFunction(); |
| 241 | Function *Caller = CS.getCaller(); |
| 242 | |
| 243 | AAResults &AAR = AARGetter(*Callee); |
| 244 | |
| 245 | // Try to inline the function. Get the list of static allocas that were |
| 246 | // inlined. |
| 247 | if (!InlineFunction(CS, IFI, &AAR, InsertLifetime)) |
| 248 | return false; |
| 249 | |
| 250 | if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No) |
| 251 | ImportedFunctionsStats.recordInline(*Caller, *Callee); |
| 252 | |
| 253 | AttributeFuncs::mergeAttributesForInlining(*Caller, *Callee); |
| 254 | |
| 255 | if (!DisableInlinedAllocaMerging) |
| 256 | mergeInlinedArrayAllocas(Caller, IFI, InlinedArrayAllocas, InlineHistory); |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 6754b82 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 258 | return true; |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 259 | } |
Jakob Stoklund Olesen | 8a19d3c | 2010-01-20 17:51:28 +0000 | [diff] [blame] | 260 | |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 261 | /// Return true if inlining of CS can block the caller from being |
| 262 | /// inlined which is proved to be more beneficial. \p IC is the |
| 263 | /// estimated inline cost associated with callsite \p CS. |
Haicheng Wu | f8dc2d8 | 2017-01-30 16:15:14 +0000 | [diff] [blame] | 264 | /// \p TotalSecondaryCost will be set to the estimated cost of inlining the |
| 265 | /// caller if \p CS is suppressed for inlining. |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 266 | static bool |
| 267 | shouldBeDeferred(Function *Caller, CallSite CS, InlineCost IC, |
| 268 | int &TotalSecondaryCost, |
Benjamin Kramer | 41e66da | 2016-08-06 12:33:46 +0000 | [diff] [blame] | 269 | function_ref<InlineCost(CallSite CS)> GetInlineCost) { |
Xinliang David Li | 4b2fdcc | 2016-04-29 22:59:36 +0000 | [diff] [blame] | 270 | |
| 271 | // For now we only handle local or inline functions. |
| 272 | if (!Caller->hasLocalLinkage() && !Caller->hasLinkOnceODRLinkage()) |
| 273 | return false; |
| 274 | // Try to detect the case where the current inlining candidate caller (call |
| 275 | // it B) is a static or linkonce-ODR function and is an inlining candidate |
| 276 | // elsewhere, and the current candidate callee (call it C) is large enough |
| 277 | // that inlining it into B would make B too big to inline later. In these |
| 278 | // circumstances it may be best not to inline C into B, but to inline B into |
| 279 | // its callers. |
| 280 | // |
| 281 | // This only applies to static and linkonce-ODR functions because those are |
| 282 | // expected to be available for inlining in the translation units where they |
| 283 | // are used. Thus we will always have the opportunity to make local inlining |
| 284 | // decisions. Importantly the linkonce-ODR linkage covers inline functions |
| 285 | // and templates in C++. |
| 286 | // |
| 287 | // FIXME: All of this logic should be sunk into getInlineCost. It relies on |
| 288 | // the internal implementation of the inline cost metrics rather than |
| 289 | // treating them as truly abstract units etc. |
| 290 | TotalSecondaryCost = 0; |
| 291 | // The candidate cost to be imposed upon the current function. |
Evgeny Astigeevich | 7823c66 | 2017-03-22 12:01:57 +0000 | [diff] [blame] | 292 | int CandidateCost = IC.getCost() - 1; |
Xinliang David Li | 4b2fdcc | 2016-04-29 22:59:36 +0000 | [diff] [blame] | 293 | // This bool tracks what happens if we do NOT inline C into B. |
| 294 | bool callerWillBeRemoved = Caller->hasLocalLinkage(); |
| 295 | // This bool tracks what happens if we DO inline C into B. |
| 296 | bool inliningPreventsSomeOuterInline = false; |
| 297 | for (User *U : Caller->users()) { |
| 298 | CallSite CS2(U); |
| 299 | |
| 300 | // If this isn't a call to Caller (it could be some other sort |
| 301 | // of reference) skip it. Such references will prevent the caller |
| 302 | // from being removed. |
| 303 | if (!CS2 || CS2.getCalledFunction() != Caller) { |
| 304 | callerWillBeRemoved = false; |
| 305 | continue; |
| 306 | } |
| 307 | |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 308 | InlineCost IC2 = GetInlineCost(CS2); |
Xinliang David Li | 4b2fdcc | 2016-04-29 22:59:36 +0000 | [diff] [blame] | 309 | ++NumCallerCallersAnalyzed; |
| 310 | if (!IC2) { |
| 311 | callerWillBeRemoved = false; |
| 312 | continue; |
| 313 | } |
| 314 | if (IC2.isAlways()) |
| 315 | continue; |
| 316 | |
Xinliang David Li | f450b88 | 2016-11-04 03:00:52 +0000 | [diff] [blame] | 317 | // See if inlining of the original callsite would erase the cost delta of |
Xinliang David Li | 4b2fdcc | 2016-04-29 22:59:36 +0000 | [diff] [blame] | 318 | // this callsite. We subtract off the penalty for the call instruction, |
| 319 | // which we would be deleting. |
| 320 | if (IC2.getCostDelta() <= CandidateCost) { |
| 321 | inliningPreventsSomeOuterInline = true; |
| 322 | TotalSecondaryCost += IC2.getCost(); |
| 323 | } |
| 324 | } |
| 325 | // If all outer calls to Caller would get inlined, the cost for the last |
| 326 | // one is set very low by getInlineCost, in anticipation that Caller will |
| 327 | // be removed entirely. We did not account for this above unless there |
| 328 | // is only one caller of Caller. |
Taewook Oh | f22fa72 | 2017-02-14 17:30:05 +0000 | [diff] [blame] | 329 | if (callerWillBeRemoved && !Caller->hasOneUse()) |
Piotr Padlewski | d89875c | 2016-08-10 21:15:22 +0000 | [diff] [blame] | 330 | TotalSecondaryCost -= InlineConstants::LastCallToStaticBonus; |
Xinliang David Li | 4b2fdcc | 2016-04-29 22:59:36 +0000 | [diff] [blame] | 331 | |
| 332 | if (inliningPreventsSomeOuterInline && TotalSecondaryCost < IC.getCost()) |
| 333 | return true; |
| 334 | |
| 335 | return false; |
| 336 | } |
| 337 | |
Sanjay Patel | f1b0db1 | 2015-03-10 16:42:24 +0000 | [diff] [blame] | 338 | /// Return true if the inliner should attempt to inline at the given CallSite. |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 339 | static bool shouldInline(CallSite CS, |
Adam Nemet | 896c09b | 2016-08-10 00:44:44 +0000 | [diff] [blame] | 340 | function_ref<InlineCost(CallSite CS)> GetInlineCost, |
| 341 | OptimizationRemarkEmitter &ORE) { |
Adam Nemet | c507ac9 | 2016-09-27 23:47:03 +0000 | [diff] [blame] | 342 | using namespace ore; |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 343 | InlineCost IC = GetInlineCost(CS); |
Adam Nemet | c507ac9 | 2016-09-27 23:47:03 +0000 | [diff] [blame] | 344 | Instruction *Call = CS.getInstruction(); |
| 345 | Function *Callee = CS.getCalledFunction(); |
Adam Nemet | e7bdf22 | 2017-01-30 16:22:45 +0000 | [diff] [blame] | 346 | Function *Caller = CS.getCaller(); |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 347 | |
Daniel Dunbar | 3933e66 | 2008-10-30 19:26:59 +0000 | [diff] [blame] | 348 | if (IC.isAlways()) { |
David Greene | 0122fc4 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 349 | DEBUG(dbgs() << " Inlining: cost=always" |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 350 | << ", Call: " << *CS.getInstruction() << "\n"); |
Adam Nemet | c507ac9 | 2016-09-27 23:47:03 +0000 | [diff] [blame] | 351 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AlwaysInline", Call) |
| 352 | << NV("Callee", Callee) |
| 353 | << " should always be inlined (cost=always)"); |
Daniel Dunbar | 3933e66 | 2008-10-30 19:26:59 +0000 | [diff] [blame] | 354 | return true; |
| 355 | } |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 356 | |
Daniel Dunbar | 3933e66 | 2008-10-30 19:26:59 +0000 | [diff] [blame] | 357 | if (IC.isNever()) { |
David Greene | 0122fc4 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 358 | DEBUG(dbgs() << " NOT Inlining: cost=never" |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 359 | << ", Call: " << *CS.getInstruction() << "\n"); |
Adam Nemet | e7bdf22 | 2017-01-30 16:22:45 +0000 | [diff] [blame] | 360 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline", Call) |
| 361 | << NV("Callee", Callee) << " not inlined into " |
| 362 | << NV("Caller", Caller) |
| 363 | << " because it should never be inlined (cost=never)"); |
Daniel Dunbar | 3933e66 | 2008-10-30 19:26:59 +0000 | [diff] [blame] | 364 | return false; |
| 365 | } |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 366 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 367 | if (!IC) { |
| 368 | DEBUG(dbgs() << " NOT Inlining: cost=" << IC.getCost() |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 369 | << ", thres=" << (IC.getCostDelta() + IC.getCost()) |
| 370 | << ", Call: " << *CS.getInstruction() << "\n"); |
Adam Nemet | e7bdf22 | 2017-01-30 16:22:45 +0000 | [diff] [blame] | 371 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "TooCostly", Call) |
| 372 | << NV("Callee", Callee) << " not inlined into " |
| 373 | << NV("Caller", Caller) << " because too costly to inline (cost=" |
Adam Nemet | c507ac9 | 2016-09-27 23:47:03 +0000 | [diff] [blame] | 374 | << NV("Cost", IC.getCost()) << ", threshold=" |
| 375 | << NV("Threshold", IC.getCostDelta() + IC.getCost()) << ")"); |
Daniel Dunbar | e7fbf9f4 | 2008-10-29 01:02:02 +0000 | [diff] [blame] | 376 | return false; |
Daniel Dunbar | e7fbf9f4 | 2008-10-29 01:02:02 +0000 | [diff] [blame] | 377 | } |
Dale Johannesen | 3059924 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 378 | |
Xinliang David Li | 4b2fdcc | 2016-04-29 22:59:36 +0000 | [diff] [blame] | 379 | int TotalSecondaryCost = 0; |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 380 | if (shouldBeDeferred(Caller, CS, IC, TotalSecondaryCost, GetInlineCost)) { |
Xinliang David Li | 4b2fdcc | 2016-04-29 22:59:36 +0000 | [diff] [blame] | 381 | DEBUG(dbgs() << " NOT Inlining: " << *CS.getInstruction() |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 382 | << " Cost = " << IC.getCost() |
| 383 | << ", outer Cost = " << TotalSecondaryCost << '\n'); |
Adam Nemet | e7bdf22 | 2017-01-30 16:22:45 +0000 | [diff] [blame] | 384 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "IncreaseCostInOtherContexts", |
| 385 | Call) |
Adam Nemet | c507ac9 | 2016-09-27 23:47:03 +0000 | [diff] [blame] | 386 | << "Not inlining. Cost of inlining " << NV("Callee", Callee) |
| 387 | << " increases the cost of inlining " << NV("Caller", Caller) |
| 388 | << " in other contexts"); |
Xinliang David Li | 4b2fdcc | 2016-04-29 22:59:36 +0000 | [diff] [blame] | 389 | return false; |
Dale Johannesen | 3059924 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 392 | DEBUG(dbgs() << " Inlining: cost=" << IC.getCost() |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 393 | << ", thres=" << (IC.getCostDelta() + IC.getCost()) |
| 394 | << ", Call: " << *CS.getInstruction() << '\n'); |
Adam Nemet | c507ac9 | 2016-09-27 23:47:03 +0000 | [diff] [blame] | 395 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "CanBeInlined", Call) |
| 396 | << NV("Callee", Callee) << " can be inlined into " |
| 397 | << NV("Caller", Caller) << " with cost=" << NV("Cost", IC.getCost()) |
| 398 | << " (threshold=" |
| 399 | << NV("Threshold", IC.getCostDelta() + IC.getCost()) << ")"); |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 400 | return true; |
Daniel Dunbar | e7fbf9f4 | 2008-10-29 01:02:02 +0000 | [diff] [blame] | 401 | } |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 402 | |
Sanjay Patel | f1b0db1 | 2015-03-10 16:42:24 +0000 | [diff] [blame] | 403 | /// Return true if the specified inline history ID |
Chris Lattner | e826267 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 404 | /// indicates an inline history that includes the specified function. |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 405 | static bool InlineHistoryIncludes( |
| 406 | Function *F, int InlineHistoryID, |
| 407 | const SmallVectorImpl<std::pair<Function *, int>> &InlineHistory) { |
Chris Lattner | e826267 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 408 | while (InlineHistoryID != -1) { |
| 409 | assert(unsigned(InlineHistoryID) < InlineHistory.size() && |
| 410 | "Invalid inline history ID"); |
| 411 | if (InlineHistory[InlineHistoryID].first == F) |
| 412 | return true; |
| 413 | InlineHistoryID = InlineHistory[InlineHistoryID].second; |
| 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 418 | bool LegacyInlinerBase::doInitialization(CallGraph &CG) { |
Piotr Padlewski | 84abc74 | 2016-07-29 00:27:16 +0000 | [diff] [blame] | 419 | if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No) |
| 420 | ImportedFunctionsStats.setModuleInfo(CG.getModule()); |
| 421 | return false; // No changes to CallGraph. |
| 422 | } |
| 423 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 424 | bool LegacyInlinerBase::runOnSCC(CallGraphSCC &SCC) { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 425 | if (skipSCC(SCC)) |
| 426 | return false; |
Andrew Kaylor | 9c81d0f | 2016-05-23 21:57:54 +0000 | [diff] [blame] | 427 | return inlineCalls(SCC); |
| 428 | } |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 429 | |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 430 | static bool |
| 431 | inlineCallsImpl(CallGraphSCC &SCC, CallGraph &CG, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 432 | std::function<AssumptionCache &(Function &)> GetAssumptionCache, |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 433 | ProfileSummaryInfo *PSI, TargetLibraryInfo &TLI, |
| 434 | bool InsertLifetime, |
Benjamin Kramer | 41e66da | 2016-08-06 12:33:46 +0000 | [diff] [blame] | 435 | function_ref<InlineCost(CallSite CS)> GetInlineCost, |
| 436 | function_ref<AAResults &(Function &)> AARGetter, |
Piotr Padlewski | 84abc74 | 2016-07-29 00:27:16 +0000 | [diff] [blame] | 437 | ImportedFunctionsInliningStatistics &ImportedFunctionsStats) { |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 438 | SmallPtrSet<Function *, 8> SCCFunctions; |
David Greene | 0122fc4 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 439 | DEBUG(dbgs() << "Inliner visiting SCC:"); |
Yaron Keren | 4c548f2 | 2015-06-20 07:12:33 +0000 | [diff] [blame] | 440 | for (CallGraphNode *Node : SCC) { |
| 441 | Function *F = Node->getFunction(); |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 442 | if (F) |
| 443 | SCCFunctions.insert(F); |
David Greene | 0122fc4 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 444 | DEBUG(dbgs() << " " << (F ? F->getName() : "INDIRECTNODE")); |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 445 | } |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 6754b82 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 447 | // Scan through and identify all call sites ahead of time so that we only |
| 448 | // inline call sites in the original functions, not call sites that result |
| 449 | // from inlining other functions. |
Chris Lattner | e826267 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 450 | SmallVector<std::pair<CallSite, int>, 16> CallSites; |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 451 | |
Chris Lattner | e826267 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 452 | // When inlining a callee produces new call sites, we want to keep track of |
| 453 | // the fact that they were inlined from the callee. This allows us to avoid |
| 454 | // infinite inlining in some obscure cases. To represent this, we use an |
| 455 | // index into the InlineHistory vector. |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 456 | SmallVector<std::pair<Function *, int>, 8> InlineHistory; |
Chris Lattner | 6754b82 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 457 | |
Yaron Keren | 4c548f2 | 2015-06-20 07:12:33 +0000 | [diff] [blame] | 458 | for (CallGraphNode *Node : SCC) { |
| 459 | Function *F = Node->getFunction(); |
Adam Nemet | cef3314 | 2016-08-26 20:21:05 +0000 | [diff] [blame] | 460 | if (!F || F->isDeclaration()) |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 461 | continue; |
| 462 | |
Adam Nemet | cef3314 | 2016-08-26 20:21:05 +0000 | [diff] [blame] | 463 | OptimizationRemarkEmitter ORE(F); |
Yaron Keren | 4c548f2 | 2015-06-20 07:12:33 +0000 | [diff] [blame] | 464 | for (BasicBlock &BB : *F) |
| 465 | for (Instruction &I : BB) { |
| 466 | CallSite CS(cast<Value>(&I)); |
Dale Johannesen | 3059924 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 467 | // If this isn't a call, or it is a call to an intrinsic, it can |
Chris Lattner | 9e50747 | 2009-08-31 05:34:32 +0000 | [diff] [blame] | 468 | // never be inlined. |
Gabor Greif | 62f0aac | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 469 | if (!CS || isa<IntrinsicInst>(I)) |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 470 | continue; |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 471 | |
Chris Lattner | 9e50747 | 2009-08-31 05:34:32 +0000 | [diff] [blame] | 472 | // If this is a direct call to an external function, we can never inline |
| 473 | // it. If it is an indirect call, inlining may resolve it to be a |
| 474 | // direct call, so we keep it. |
Yaron Keren | c66c06b | 2015-07-19 15:48:07 +0000 | [diff] [blame] | 475 | if (Function *Callee = CS.getCalledFunction()) |
Adam Nemet | cef3314 | 2016-08-26 20:21:05 +0000 | [diff] [blame] | 476 | if (Callee->isDeclaration()) { |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 477 | using namespace ore; |
Adam Nemet | 04758ba | 2016-09-27 22:19:23 +0000 | [diff] [blame] | 478 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "NoDefinition", &I) |
Adam Nemet | a62b7e1 | 2016-09-27 20:55:07 +0000 | [diff] [blame] | 479 | << NV("Callee", Callee) << " will not be inlined into " |
Adam Nemet | 1142147 | 2016-09-27 21:58:17 +0000 | [diff] [blame] | 480 | << NV("Caller", CS.getCaller()) |
| 481 | << " because its definition is unavailable" |
| 482 | << setIsVerbose()); |
Yaron Keren | c66c06b | 2015-07-19 15:48:07 +0000 | [diff] [blame] | 483 | continue; |
Adam Nemet | cef3314 | 2016-08-26 20:21:05 +0000 | [diff] [blame] | 484 | } |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 485 | |
Chris Lattner | e826267 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 486 | CallSites.push_back(std::make_pair(CS, -1)); |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 489 | |
David Greene | 0122fc4 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 490 | DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 491 | |
Chris Lattner | a5cdd5e | 2010-04-20 00:47:08 +0000 | [diff] [blame] | 492 | // If there are no calls in this function, exit early. |
| 493 | if (CallSites.empty()) |
| 494 | return false; |
Yaron Keren | 6967cbb | 2015-07-02 14:25:09 +0000 | [diff] [blame] | 495 | |
Chris Lattner | 6754b82 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 496 | // Now that we have all of the call sites, move the ones to functions in the |
| 497 | // current SCC to the end of the list. |
| 498 | unsigned FirstCallInSCC = CallSites.size(); |
| 499 | for (unsigned i = 0; i < FirstCallInSCC; ++i) |
Chris Lattner | e826267 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 500 | if (Function *F = CallSites[i].first.getCalledFunction()) |
Chris Lattner | 6754b82 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 501 | if (SCCFunctions.count(F)) |
| 502 | std::swap(CallSites[i--], CallSites[--FirstCallInSCC]); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 503 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 504 | InlinedArrayAllocasTy InlinedArrayAllocas; |
Easwaran Raman | f5f9160 | 2017-05-09 23:21:10 +0000 | [diff] [blame] | 505 | InlineFunctionInfo InlineInfo(&CG, &GetAssumptionCache, PSI); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 506 | |
Chris Lattner | 6754b82 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 507 | // Now that we have all of the call sites, loop over them and inline them if |
| 508 | // it looks profitable to do so. |
| 509 | bool Changed = false; |
| 510 | bool LocalChange; |
| 511 | do { |
| 512 | LocalChange = false; |
| 513 | // Iterate over the outer loop because inlining functions can cause indirect |
| 514 | // calls to become direct calls. |
Yaron Keren | 4c548f2 | 2015-06-20 07:12:33 +0000 | [diff] [blame] | 515 | // CallSites may be modified inside so ranged for loop can not be used. |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 516 | for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) { |
Chris Lattner | e826267 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 517 | CallSite CS = CallSites[CSi].first; |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 519 | Function *Caller = CS.getCaller(); |
Chris Lattner | eb9acbf | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 520 | Function *Callee = CS.getCalledFunction(); |
| 521 | |
David Blaikie | cb9327b | 2017-06-09 03:29:20 +0000 | [diff] [blame] | 522 | // We can only inline direct calls to non-declarations. |
| 523 | if (!Callee || Callee->isDeclaration()) |
| 524 | continue; |
| 525 | |
David Blaikie | 6d0f394 | 2017-06-13 02:24:09 +0000 | [diff] [blame] | 526 | Instruction *Instr = CS.getInstruction(); |
| 527 | |
| 528 | bool IsTriviallyDead = isInstructionTriviallyDead(Instr, &TLI); |
| 529 | |
| 530 | int InlineHistoryID; |
| 531 | if (!IsTriviallyDead) { |
| 532 | // If this call site was obtained by inlining another function, verify |
| 533 | // that the include path for the function did not include the callee |
| 534 | // itself. If so, we'd be recursively inlining the same function, |
| 535 | // which would provide the same callsites, which would cause us to |
| 536 | // infinitely inline. |
| 537 | InlineHistoryID = CallSites[CSi].second; |
| 538 | if (InlineHistoryID != -1 && |
| 539 | InlineHistoryIncludes(Callee, InlineHistoryID, InlineHistory)) |
| 540 | continue; |
| 541 | } |
| 542 | |
David Blaikie | ae8c4af | 2017-06-12 23:01:17 +0000 | [diff] [blame] | 543 | // FIXME for new PM: because of the old PM we currently generate ORE and |
| 544 | // in turn BFI on demand. With the new PM, the ORE dependency should |
| 545 | // just become a regular analysis dependency. |
| 546 | OptimizationRemarkEmitter ORE(Caller); |
| 547 | |
| 548 | // If the policy determines that we should inline this function, |
| 549 | // delete the call instead. |
| 550 | if (!shouldInline(CS, GetInlineCost, ORE)) |
| 551 | continue; |
| 552 | |
Chris Lattner | eb9acbf | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 553 | // If this call site is dead and it is to a readonly function, we should |
| 554 | // just delete the call instead of trying to inline it, regardless of |
| 555 | // size. This happens because IPSCCP propagates the result out of the |
| 556 | // call and then we're left with the dead call. |
David Blaikie | 6d0f394 | 2017-06-13 02:24:09 +0000 | [diff] [blame] | 557 | if (IsTriviallyDead) { |
| 558 | DEBUG(dbgs() << " -> Deleting dead call: " << *Instr << "\n"); |
Chris Lattner | eb9acbf | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 559 | // Update the call graph by deleting the edge from Callee to Caller. |
| 560 | CG[Caller]->removeCallEdgeFor(CS); |
David Blaikie | 6d0f394 | 2017-06-13 02:24:09 +0000 | [diff] [blame] | 561 | Instr->eraseFromParent(); |
Chris Lattner | eb9acbf | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 562 | ++NumCallsDeleted; |
| 563 | } else { |
NAKAMURA Takumi | cd1fc4b | 2014-04-17 12:22:14 +0000 | [diff] [blame] | 564 | // Get DebugLoc to report. CS will be invalid after Inliner. |
David Blaikie | 6d0f394 | 2017-06-13 02:24:09 +0000 | [diff] [blame] | 565 | DebugLoc DLoc = Instr->getDebugLoc(); |
Adam Nemet | 896c09b | 2016-08-10 00:44:44 +0000 | [diff] [blame] | 566 | BasicBlock *Block = CS.getParent(); |
Diego Novillo | 7f8af8b | 2014-05-22 14:19:46 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 2eee5d3 | 2010-04-22 23:37:35 +0000 | [diff] [blame] | 568 | // Attempt to inline the function. |
Adam Nemet | e7bdf22 | 2017-01-30 16:22:45 +0000 | [diff] [blame] | 569 | using namespace ore; |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 570 | if (!InlineCallIfPossible(CS, InlineInfo, InlinedArrayAllocas, |
Piotr Padlewski | 84abc74 | 2016-07-29 00:27:16 +0000 | [diff] [blame] | 571 | InlineHistoryID, InsertLifetime, AARGetter, |
| 572 | ImportedFunctionsStats)) { |
Adam Nemet | c507ac9 | 2016-09-27 23:47:03 +0000 | [diff] [blame] | 573 | ORE.emit( |
| 574 | OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block) |
| 575 | << NV("Callee", Callee) << " will not be inlined into " |
| 576 | << NV("Caller", Caller)); |
Chris Lattner | eb9acbf | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 577 | continue; |
Diego Novillo | 7f8af8b | 2014-05-22 14:19:46 +0000 | [diff] [blame] | 578 | } |
Chris Lattner | eb9acbf | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 579 | ++NumInlined; |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 580 | |
| 581 | // Report the inline decision. |
Adam Nemet | c507ac9 | 2016-09-27 23:47:03 +0000 | [diff] [blame] | 582 | ORE.emit(OptimizationRemark(DEBUG_TYPE, "Inlined", DLoc, Block) |
| 583 | << NV("Callee", Callee) << " inlined into " |
| 584 | << NV("Caller", Caller)); |
Diego Novillo | a9298b2 | 2014-04-08 16:42:34 +0000 | [diff] [blame] | 585 | |
Chris Lattner | c2432b9 | 2010-05-01 01:26:13 +0000 | [diff] [blame] | 586 | // If inlining this function gave us any new call sites, throw them |
Chris Lattner | 2eee5d3 | 2010-04-22 23:37:35 +0000 | [diff] [blame] | 587 | // onto our worklist to process. They are useful inline candidates. |
Chris Lattner | c2432b9 | 2010-05-01 01:26:13 +0000 | [diff] [blame] | 588 | if (!InlineInfo.InlinedCalls.empty()) { |
Chris Lattner | e826267 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 589 | // Create a new inline history entry for this, so that we remember |
| 590 | // that these new callsites came about due to inlining Callee. |
| 591 | int NewHistoryID = InlineHistory.size(); |
| 592 | InlineHistory.push_back(std::make_pair(Callee, InlineHistoryID)); |
| 593 | |
Yaron Keren | 4c548f2 | 2015-06-20 07:12:33 +0000 | [diff] [blame] | 594 | for (Value *Ptr : InlineInfo.InlinedCalls) |
Chandler Carruth | 2121199 | 2012-03-25 04:03:40 +0000 | [diff] [blame] | 595 | CallSites.push_back(std::make_pair(CallSite(Ptr), NewHistoryID)); |
Chris Lattner | c691de3 | 2010-04-23 18:37:01 +0000 | [diff] [blame] | 596 | } |
Chris Lattner | eb9acbf | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 597 | } |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 598 | |
Chris Lattner | eb9acbf | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 599 | // If we inlined or deleted the last possible call site to the function, |
| 600 | // delete the function body now. |
| 601 | if (Callee && Callee->use_empty() && Callee->hasLocalLinkage() && |
Chris Lattner | 9e50747 | 2009-08-31 05:34:32 +0000 | [diff] [blame] | 602 | // TODO: Can remove if in SCC now. |
Chris Lattner | 081375b | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 603 | !SCCFunctions.count(Callee) && |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 604 | |
Chris Lattner | 081375b | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 605 | // The function may be apparently dead, but if there are indirect |
| 606 | // callgraph references to the node, we cannot delete it yet, this |
| 607 | // could invalidate the CGSCC iterator. |
| 608 | CG[Callee]->getNumReferences() == 0) { |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 609 | DEBUG(dbgs() << " -> Deleting dead function: " << Callee->getName() |
| 610 | << "\n"); |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 611 | CallGraphNode *CalleeNode = CG[Callee]; |
Yaron Keren | 6967cbb | 2015-07-02 14:25:09 +0000 | [diff] [blame] | 612 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 613 | // Remove any call graph edges from the callee to its callees. |
| 614 | CalleeNode->removeAllCalledFunctions(); |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 615 | |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 616 | // Removing the node for callee from the call graph and delete it. |
Easwaran Raman | b1bd398 | 2016-03-08 00:36:35 +0000 | [diff] [blame] | 617 | delete CG.removeFunctionFromModule(CalleeNode); |
Chris Lattner | d3374e8 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 618 | ++NumDeleted; |
| 619 | } |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 620 | |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 621 | // Remove this call site from the list. If possible, use |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 622 | // swap/pop_back for efficiency, but do not use it if doing so would |
| 623 | // move a call site to a function in this SCC before the |
| 624 | // 'FirstCallInSCC' barrier. |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 625 | if (SCC.isSingular()) { |
Benjamin Kramer | 5ac57e3 | 2010-05-31 12:50:41 +0000 | [diff] [blame] | 626 | CallSites[CSi] = CallSites.back(); |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 627 | CallSites.pop_back(); |
| 628 | } else { |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 629 | CallSites.erase(CallSites.begin() + CSi); |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 630 | } |
| 631 | --CSi; |
| 632 | |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 633 | Changed = true; |
| 634 | LocalChange = true; |
| 635 | } |
Chris Lattner | 6754b82 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 636 | } while (LocalChange); |
Chris Lattner | 4d25c86 | 2004-04-08 06:34:31 +0000 | [diff] [blame] | 637 | |
Chris Lattner | d075cc2 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 638 | return Changed; |
| 639 | } |
| 640 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 641 | bool LegacyInlinerBase::inlineCalls(CallGraphSCC &SCC) { |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 642 | CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 643 | ACT = &getAnalysis<AssumptionCacheTracker>(); |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 644 | PSI = getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 645 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 646 | auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & { |
| 647 | return ACT->getAssumptionCache(F); |
| 648 | }; |
| 649 | return inlineCallsImpl(SCC, CG, GetAssumptionCache, PSI, TLI, InsertLifetime, |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 650 | [this](CallSite CS) { return getInlineCost(CS); }, |
Peter Collingbourne | cea1e4e | 2017-02-09 23:11:52 +0000 | [diff] [blame] | 651 | LegacyAARGetter(*this), ImportedFunctionsStats); |
Sean Silva | ab6a683 | 2016-07-23 04:22:50 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Sanjay Patel | f1b0db1 | 2015-03-10 16:42:24 +0000 | [diff] [blame] | 654 | /// Remove now-dead linkonce functions at the end of |
| 655 | /// processing to avoid breaking the SCC traversal. |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 656 | bool LegacyInlinerBase::doFinalization(CallGraph &CG) { |
Piotr Padlewski | 84abc74 | 2016-07-29 00:27:16 +0000 | [diff] [blame] | 657 | if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No) |
| 658 | ImportedFunctionsStats.dump(InlinerFunctionImportStats == |
| 659 | InlinerFunctionImportStatsOpts::Verbose); |
Devang Patel | f0ef357 | 2008-11-05 01:39:16 +0000 | [diff] [blame] | 660 | return removeDeadFunctions(CG); |
| 661 | } |
| 662 | |
Sanjay Patel | f1b0db1 | 2015-03-10 16:42:24 +0000 | [diff] [blame] | 663 | /// Remove dead functions that are not included in DNR (Do Not Remove) list. |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 664 | bool LegacyInlinerBase::removeDeadFunctions(CallGraph &CG, |
| 665 | bool AlwaysInlineOnly) { |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 666 | SmallVector<CallGraphNode *, 16> FunctionsToRemove; |
Chandler Carruth | 6e9bb7e | 2016-12-26 23:43:27 +0000 | [diff] [blame] | 667 | SmallVector<Function *, 16> DeadFunctionsInComdats; |
David Majnemer | ac256cf | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 668 | |
| 669 | auto RemoveCGN = [&](CallGraphNode *CGN) { |
| 670 | // Remove any call graph edges from the function to its callees. |
| 671 | CGN->removeAllCalledFunctions(); |
| 672 | |
| 673 | // Remove any edges from the external node to the function's call graph |
| 674 | // node. These edges might have been made irrelegant due to |
| 675 | // optimization of the program. |
| 676 | CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN); |
| 677 | |
| 678 | // Removing the node for callee from the call graph and delete it. |
| 679 | FunctionsToRemove.push_back(CGN); |
| 680 | }; |
Chris Lattner | be8bb80 | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 681 | |
| 682 | // Scan for all of the functions, looking for ones that should now be removed |
| 683 | // from the program. Insert the dead ones in the FunctionsToRemove set. |
David Blaikie | a5d7de9 | 2015-08-05 20:55:50 +0000 | [diff] [blame] | 684 | for (const auto &I : CG) { |
| 685 | CallGraphNode *CGN = I.second.get(); |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 686 | Function *F = CGN->getFunction(); |
Chandler Carruth | d7a5f2a | 2012-03-16 06:10:13 +0000 | [diff] [blame] | 687 | if (!F || F->isDeclaration()) |
| 688 | continue; |
| 689 | |
| 690 | // Handle the case when this function is called and we only want to care |
| 691 | // about always-inline functions. This is a bit of a hack to share code |
| 692 | // between here and the InlineAlways pass. |
Duncan P. N. Exon Smith | 2c79ad9 | 2015-02-14 01:11:29 +0000 | [diff] [blame] | 693 | if (AlwaysInlineOnly && !F->hasFnAttribute(Attribute::AlwaysInline)) |
Chandler Carruth | d7a5f2a | 2012-03-16 06:10:13 +0000 | [diff] [blame] | 694 | continue; |
| 695 | |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 696 | // If the only remaining users of the function are dead constants, remove |
| 697 | // them. |
| 698 | F->removeDeadConstantUsers(); |
Chris Lattner | be8bb80 | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 699 | |
Eli Friedman | 1923a33 | 2011-10-20 05:23:42 +0000 | [diff] [blame] | 700 | if (!F->isDefTriviallyDead()) |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 701 | continue; |
David Majnemer | ac07703 | 2014-10-08 19:32:32 +0000 | [diff] [blame] | 702 | |
| 703 | // It is unsafe to drop a function with discardable linkage from a COMDAT |
| 704 | // without also dropping the other members of the COMDAT. |
| 705 | // The inliner doesn't visit non-function entities which are in COMDAT |
| 706 | // groups so it is unsafe to do so *unless* the linkage is local. |
David Majnemer | ac256cf | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 707 | if (!F->hasLocalLinkage()) { |
Chandler Carruth | 6e9bb7e | 2016-12-26 23:43:27 +0000 | [diff] [blame] | 708 | if (F->hasComdat()) { |
| 709 | DeadFunctionsInComdats.push_back(F); |
David Majnemer | ac256cf | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 710 | continue; |
| 711 | } |
| 712 | } |
Devang Patel | f0ef357 | 2008-11-05 01:39:16 +0000 | [diff] [blame] | 713 | |
David Majnemer | ac256cf | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 714 | RemoveCGN(CGN); |
Chris Lattner | c87784f | 2004-04-20 22:06:53 +0000 | [diff] [blame] | 715 | } |
David Majnemer | ac256cf | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 716 | if (!DeadFunctionsInComdats.empty()) { |
Chandler Carruth | 6e9bb7e | 2016-12-26 23:43:27 +0000 | [diff] [blame] | 717 | // Filter out the functions whose comdats remain alive. |
| 718 | filterDeadComdatFunctions(CG.getModule(), DeadFunctionsInComdats); |
| 719 | // Remove the rest. |
| 720 | for (Function *F : DeadFunctionsInComdats) |
| 721 | RemoveCGN(CG[F]); |
David Majnemer | ac256cf | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Chandler Carruth | d7a5f2a | 2012-03-16 06:10:13 +0000 | [diff] [blame] | 724 | if (FunctionsToRemove.empty()) |
| 725 | return false; |
Chris Lattner | be8bb80 | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 726 | |
| 727 | // Now that we know which functions to delete, do so. We didn't want to do |
| 728 | // this inline, because that would invalidate our CallGraph::iterator |
| 729 | // objects. :( |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 730 | // |
Chandler Carruth | d7a5f2a | 2012-03-16 06:10:13 +0000 | [diff] [blame] | 731 | // Note that it doesn't matter that we are iterating over a non-stable order |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 732 | // here to do this, it doesn't matter which order the functions are deleted |
| 733 | // in. |
Chandler Carruth | 45ae88f | 2012-04-01 10:41:24 +0000 | [diff] [blame] | 734 | array_pod_sort(FunctionsToRemove.begin(), FunctionsToRemove.end()); |
Chandler Carruth | 8562d3a | 2016-08-03 01:02:31 +0000 | [diff] [blame] | 735 | FunctionsToRemove.erase( |
| 736 | std::unique(FunctionsToRemove.begin(), FunctionsToRemove.end()), |
| 737 | FunctionsToRemove.end()); |
Yaron Keren | 62064d6 | 2015-06-25 19:28:24 +0000 | [diff] [blame] | 738 | for (CallGraphNode *CGN : FunctionsToRemove) { |
Easwaran Raman | b1bd398 | 2016-03-08 00:36:35 +0000 | [diff] [blame] | 739 | delete CG.removeFunctionFromModule(CGN); |
Chris Lattner | be8bb80 | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 740 | ++NumDeleted; |
Chris Lattner | be8bb80 | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 741 | } |
Chandler Carruth | d7a5f2a | 2012-03-16 06:10:13 +0000 | [diff] [blame] | 742 | return true; |
Chris Lattner | c87784f | 2004-04-20 22:06:53 +0000 | [diff] [blame] | 743 | } |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 744 | |
| 745 | PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC, |
| 746 | CGSCCAnalysisManager &AM, LazyCallGraph &CG, |
| 747 | CGSCCUpdateResult &UR) { |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 748 | const ModuleAnalysisManager &MAM = |
| 749 | AM.getResult<ModuleAnalysisManagerCGSCCProxy>(InitialC, CG).getManager(); |
| 750 | bool Changed = false; |
| 751 | |
| 752 | assert(InitialC.size() > 0 && "Cannot handle an empty SCC!"); |
| 753 | Module &M = *InitialC.begin()->getFunction().getParent(); |
| 754 | ProfileSummaryInfo *PSI = MAM.getCachedResult<ProfileSummaryAnalysis>(M); |
| 755 | |
Chandler Carruth | 20e588e | 2017-03-09 11:35:40 +0000 | [diff] [blame] | 756 | // We use a single common worklist for calls across the entire SCC. We |
| 757 | // process these in-order and append new calls introduced during inlining to |
| 758 | // the end. |
| 759 | // |
| 760 | // Note that this particular order of processing is actually critical to |
| 761 | // avoid very bad behaviors. Consider *highly connected* call graphs where |
| 762 | // each function contains a small amonut of code and a couple of calls to |
| 763 | // other functions. Because the LLVM inliner is fundamentally a bottom-up |
| 764 | // inliner, it can handle gracefully the fact that these all appear to be |
| 765 | // reasonable inlining candidates as it will flatten things until they become |
| 766 | // too big to inline, and then move on and flatten another batch. |
| 767 | // |
| 768 | // However, when processing call edges *within* an SCC we cannot rely on this |
| 769 | // bottom-up behavior. As a consequence, with heavily connected *SCCs* of |
| 770 | // functions we can end up incrementally inlining N calls into each of |
| 771 | // N functions because each incremental inlining decision looks good and we |
| 772 | // don't have a topological ordering to prevent explosions. |
| 773 | // |
| 774 | // To compensate for this, we don't process transitive edges made immediate |
| 775 | // by inlining until we've done one pass of inlining across the entire SCC. |
| 776 | // Large, highly connected SCCs still lead to some amount of code bloat in |
| 777 | // this model, but it is uniformly spread across all the functions in the SCC |
| 778 | // and eventually they all become too large to inline, rather than |
| 779 | // incrementally maknig a single function grow in a super linear fashion. |
| 780 | SmallVector<std::pair<CallSite, int>, 16> Calls; |
| 781 | |
| 782 | // Populate the initial list of calls in this SCC. |
| 783 | for (auto &N : InitialC) { |
| 784 | // We want to generally process call sites top-down in order for |
| 785 | // simplifications stemming from replacing the call with the returned value |
| 786 | // after inlining to be visible to subsequent inlining decisions. |
| 787 | // FIXME: Using instructions sequence is a really bad way to do this. |
| 788 | // Instead we should do an actual RPO walk of the function body. |
| 789 | for (Instruction &I : instructions(N.getFunction())) |
| 790 | if (auto CS = CallSite(&I)) |
| 791 | if (Function *Callee = CS.getCalledFunction()) |
| 792 | if (!Callee->isDeclaration()) |
| 793 | Calls.push_back({CS, -1}); |
| 794 | } |
| 795 | if (Calls.empty()) |
| 796 | return PreservedAnalyses::all(); |
| 797 | |
| 798 | // Capture updatable variables for the current SCC and RefSCC. |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 799 | auto *C = &InitialC; |
| 800 | auto *RC = &C->getOuterRefSCC(); |
| 801 | |
Chandler Carruth | 141bf5d | 2016-12-27 06:46:20 +0000 | [diff] [blame] | 802 | // When inlining a callee produces new call sites, we want to keep track of |
| 803 | // the fact that they were inlined from the callee. This allows us to avoid |
| 804 | // infinite inlining in some obscure cases. To represent this, we use an |
| 805 | // index into the InlineHistory vector. |
| 806 | SmallVector<std::pair<Function *, int>, 16> InlineHistory; |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 807 | |
| 808 | // Track a set vector of inlined callees so that we can augment the caller |
| 809 | // with all of their edges in the call graph before pruning out the ones that |
| 810 | // got simplified away. |
| 811 | SmallSetVector<Function *, 4> InlinedCallees; |
| 812 | |
| 813 | // Track the dead functions to delete once finished with inlining calls. We |
| 814 | // defer deleting these to make it easier to handle the call graph updates. |
| 815 | SmallVector<Function *, 4> DeadFunctions; |
| 816 | |
Chandler Carruth | 20e588e | 2017-03-09 11:35:40 +0000 | [diff] [blame] | 817 | // Loop forward over all of the calls. Note that we cannot cache the size as |
| 818 | // inlining can introduce new calls that need to be processed. |
| 819 | for (int i = 0; i < (int)Calls.size(); ++i) { |
| 820 | // We expect the calls to typically be batched with sequences of calls that |
| 821 | // have the same caller, so we first set up some shared infrastructure for |
| 822 | // this caller. We also do any pruning we can at this layer on the caller |
| 823 | // alone. |
| 824 | Function &F = *Calls[i].first.getCaller(); |
| 825 | LazyCallGraph::Node &N = *CG.lookup(F); |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 826 | if (CG.lookupSCC(N) != C) |
| 827 | continue; |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 828 | if (F.hasFnAttribute(Attribute::OptimizeNone)) |
| 829 | continue; |
| 830 | |
Chandler Carruth | d4be9f4 | 2017-01-22 10:33:58 +0000 | [diff] [blame] | 831 | DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n"); |
| 832 | |
Chandler Carruth | b698d59 | 2017-01-22 10:34:01 +0000 | [diff] [blame] | 833 | // Get a FunctionAnalysisManager via a proxy for this particular node. We |
| 834 | // do this each time we visit a node as the SCC may have changed and as |
| 835 | // we're going to mutate this particular function we want to make sure the |
| 836 | // proxy is in place to forward any invalidation events. We can use the |
| 837 | // manager we get here for looking up results for functions other than this |
| 838 | // node however because those functions aren't going to be mutated by this |
| 839 | // pass. |
| 840 | FunctionAnalysisManager &FAM = |
| 841 | AM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, CG) |
| 842 | .getManager(); |
| 843 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 844 | [&](Function &F) -> AssumptionCache & { |
| 845 | return FAM.getResult<AssumptionAnalysis>(F); |
| 846 | }; |
| 847 | auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & { |
| 848 | return FAM.getResult<BlockFrequencyAnalysis>(F); |
| 849 | }; |
| 850 | |
| 851 | auto GetInlineCost = [&](CallSite CS) { |
| 852 | Function &Callee = *CS.getCalledFunction(); |
| 853 | auto &CalleeTTI = FAM.getResult<TargetIRAnalysis>(Callee); |
| 854 | return getInlineCost(CS, Params, CalleeTTI, GetAssumptionCache, {GetBFI}, |
| 855 | PSI); |
| 856 | }; |
| 857 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 858 | // Get the remarks emission analysis for the caller. |
| 859 | auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F); |
| 860 | |
Chandler Carruth | 20e588e | 2017-03-09 11:35:40 +0000 | [diff] [blame] | 861 | // Now process as many calls as we have within this caller in the sequnece. |
| 862 | // We bail out as soon as the caller has to change so we can update the |
| 863 | // call graph and prepare the context of that new caller. |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 864 | bool DidInline = false; |
Chandler Carruth | 20e588e | 2017-03-09 11:35:40 +0000 | [diff] [blame] | 865 | for (; i < (int)Calls.size() && Calls[i].first.getCaller() == &F; ++i) { |
Chandler Carruth | 141bf5d | 2016-12-27 06:46:20 +0000 | [diff] [blame] | 866 | int InlineHistoryID; |
| 867 | CallSite CS; |
Chandler Carruth | 20e588e | 2017-03-09 11:35:40 +0000 | [diff] [blame] | 868 | std::tie(CS, InlineHistoryID) = Calls[i]; |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 869 | Function &Callee = *CS.getCalledFunction(); |
| 870 | |
Chandler Carruth | 141bf5d | 2016-12-27 06:46:20 +0000 | [diff] [blame] | 871 | if (InlineHistoryID != -1 && |
| 872 | InlineHistoryIncludes(&Callee, InlineHistoryID, InlineHistory)) |
| 873 | continue; |
| 874 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 875 | // Check whether we want to inline this callsite. |
| 876 | if (!shouldInline(CS, GetInlineCost, ORE)) |
| 877 | continue; |
| 878 | |
Easwaran Raman | 12585b0 | 2017-01-20 22:44:04 +0000 | [diff] [blame] | 879 | // Setup the data structure used to plumb customization into the |
| 880 | // `InlineFunction` routine. |
| 881 | InlineFunctionInfo IFI( |
Easwaran Raman | f5f9160 | 2017-05-09 23:21:10 +0000 | [diff] [blame] | 882 | /*cg=*/nullptr, &GetAssumptionCache, PSI, |
Easwaran Raman | 12585b0 | 2017-01-20 22:44:04 +0000 | [diff] [blame] | 883 | &FAM.getResult<BlockFrequencyAnalysis>(*(CS.getCaller())), |
| 884 | &FAM.getResult<BlockFrequencyAnalysis>(Callee)); |
| 885 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 886 | if (!InlineFunction(CS, IFI)) |
| 887 | continue; |
| 888 | DidInline = true; |
| 889 | InlinedCallees.insert(&Callee); |
| 890 | |
| 891 | // Add any new callsites to defined functions to the worklist. |
Chandler Carruth | 141bf5d | 2016-12-27 06:46:20 +0000 | [diff] [blame] | 892 | if (!IFI.InlinedCallSites.empty()) { |
| 893 | int NewHistoryID = InlineHistory.size(); |
| 894 | InlineHistory.push_back({&Callee, InlineHistoryID}); |
| 895 | for (CallSite &CS : reverse(IFI.InlinedCallSites)) |
| 896 | if (Function *NewCallee = CS.getCalledFunction()) |
| 897 | if (!NewCallee->isDeclaration()) |
| 898 | Calls.push_back({CS, NewHistoryID}); |
| 899 | } |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 900 | |
Chandler Carruth | 03130d9 | 2016-12-27 03:39:54 +0000 | [diff] [blame] | 901 | // Merge the attributes based on the inlining. |
| 902 | AttributeFuncs::mergeAttributesForInlining(F, Callee); |
| 903 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 904 | // For local functions, check whether this makes the callee trivially |
| 905 | // dead. In that case, we can drop the body of the function eagerly |
| 906 | // which may reduce the number of callers of other functions to one, |
| 907 | // changing inline cost thresholds. |
| 908 | if (Callee.hasLocalLinkage()) { |
| 909 | // To check this we also need to nuke any dead constant uses (perhaps |
| 910 | // made dead by this operation on other functions). |
| 911 | Callee.removeDeadConstantUsers(); |
| 912 | if (Callee.use_empty()) { |
Chandler Carruth | 814e0df | 2017-03-16 10:45:42 +0000 | [diff] [blame] | 913 | Calls.erase( |
| 914 | std::remove_if(Calls.begin() + i + 1, Calls.end(), |
| 915 | [&Callee](const std::pair<CallSite, int> &Call) { |
| 916 | return Call.first.getCaller() == &Callee; |
| 917 | }), |
| 918 | Calls.end()); |
Chandler Carruth | 6acdca7 | 2017-01-24 12:55:57 +0000 | [diff] [blame] | 919 | // Clear the body and queue the function itself for deletion when we |
| 920 | // finish inlining and call graph updates. |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 921 | // Note that after this point, it is an error to do anything other |
| 922 | // than use the callee's address or delete it. |
| 923 | Callee.dropAllReferences(); |
| 924 | assert(find(DeadFunctions, &Callee) == DeadFunctions.end() && |
| 925 | "Cannot put cause a function to become dead twice!"); |
| 926 | DeadFunctions.push_back(&Callee); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
Chandler Carruth | 20e588e | 2017-03-09 11:35:40 +0000 | [diff] [blame] | 931 | // Back the call index up by one to put us in a good position to go around |
| 932 | // the outer loop. |
| 933 | --i; |
| 934 | |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 935 | if (!DidInline) |
| 936 | continue; |
| 937 | Changed = true; |
| 938 | |
Chandler Carruth | 9900d18 | 2016-12-28 03:13:12 +0000 | [diff] [blame] | 939 | // Add all the inlined callees' edges as ref edges to the caller. These are |
| 940 | // by definition trivial edges as we always have *some* transitive ref edge |
| 941 | // chain. While in some cases these edges are direct calls inside the |
| 942 | // callee, they have to be modeled in the inliner as reference edges as |
| 943 | // there may be a reference edge anywhere along the chain from the current |
| 944 | // caller to the callee that causes the whole thing to appear like |
| 945 | // a (transitive) reference edge that will require promotion to a call edge |
| 946 | // below. |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 947 | for (Function *InlinedCallee : InlinedCallees) { |
| 948 | LazyCallGraph::Node &CalleeN = *CG.lookup(*InlinedCallee); |
Chandler Carruth | aaad9f8 | 2017-02-09 23:24:13 +0000 | [diff] [blame] | 949 | for (LazyCallGraph::Edge &E : *CalleeN) |
| 950 | RC->insertTrivialRefEdge(N, E.getNode()); |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 951 | } |
| 952 | InlinedCallees.clear(); |
| 953 | |
| 954 | // At this point, since we have made changes we have at least removed |
| 955 | // a call instruction. However, in the process we do some incremental |
| 956 | // simplification of the surrounding code. This simplification can |
| 957 | // essentially do all of the same things as a function pass and we can |
| 958 | // re-use the exact same logic for updating the call graph to reflect the |
| 959 | // change.. |
| 960 | C = &updateCGAndAnalysisManagerForFunctionPass(CG, *C, N, AM, UR); |
Chandler Carruth | d4be9f4 | 2017-01-22 10:33:58 +0000 | [diff] [blame] | 961 | DEBUG(dbgs() << "Updated inlining SCC: " << *C << "\n"); |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 962 | RC = &C->getOuterRefSCC(); |
Chandler Carruth | 20e588e | 2017-03-09 11:35:40 +0000 | [diff] [blame] | 963 | } |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 964 | |
| 965 | // Now that we've finished inlining all of the calls across this SCC, delete |
| 966 | // all of the trivially dead functions, updating the call graph and the CGSCC |
| 967 | // pass manager in the process. |
| 968 | // |
| 969 | // Note that this walks a pointer set which has non-deterministic order but |
| 970 | // that is OK as all we do is delete things and add pointers to unordered |
| 971 | // sets. |
| 972 | for (Function *DeadF : DeadFunctions) { |
| 973 | // Get the necessary information out of the call graph and nuke the |
Chandler Carruth | 6acdca7 | 2017-01-24 12:55:57 +0000 | [diff] [blame] | 974 | // function there. Also, cclear out any cached analyses. |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 975 | auto &DeadC = *CG.lookupSCC(*CG.lookup(*DeadF)); |
Chandler Carruth | 6acdca7 | 2017-01-24 12:55:57 +0000 | [diff] [blame] | 976 | FunctionAnalysisManager &FAM = |
| 977 | AM.getResult<FunctionAnalysisManagerCGSCCProxy>(DeadC, CG) |
| 978 | .getManager(); |
| 979 | FAM.clear(*DeadF); |
| 980 | AM.clear(DeadC); |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 981 | auto &DeadRC = DeadC.getOuterRefSCC(); |
| 982 | CG.removeDeadFunction(*DeadF); |
| 983 | |
| 984 | // Mark the relevant parts of the call graph as invalid so we don't visit |
| 985 | // them. |
| 986 | UR.InvalidatedSCCs.insert(&DeadC); |
| 987 | UR.InvalidatedRefSCCs.insert(&DeadRC); |
| 988 | |
| 989 | // And delete the actual function from the module. |
| 990 | M.getFunctionList().erase(DeadF); |
| 991 | } |
Chandler Carruth | bd9c290 | 2017-07-09 03:59:31 +0000 | [diff] [blame] | 992 | |
| 993 | if (!Changed) |
| 994 | return PreservedAnalyses::all(); |
| 995 | |
| 996 | // Even if we change the IR, we update the core CGSCC data structures and so |
| 997 | // can preserve the proxy to the function analysis manager. |
| 998 | PreservedAnalyses PA; |
| 999 | PA.preserve<FunctionAnalysisManagerCGSCCProxy>(); |
| 1000 | return PA; |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 1001 | } |