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