Chris Lattner | cf5933a | 2004-06-20 04:11:48 +0000 | [diff] [blame] | 1 | //===- Inliner.cpp - Code common to all inliners --------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | befa499 | 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 | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "inline" |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Dale Johannesen | 1f67ce4 | 2009-03-19 18:03:56 +0000 | [diff] [blame] | 19 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CallGraph.h" |
Dan Gohman | e4aeec0 | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/InlineCost.h" |
Chandler Carruth | 6c0b3ac | 2012-03-12 11:19:33 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/InstructionSimplify.h" |
Chris Lattner | ff2dad3 | 2007-01-30 23:28:39 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Tanya Lattner | 6f7426e | 2007-06-19 22:29:50 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/IPO/InlinerPass.h" |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 4ff4141 | 2009-11-12 21:58:18 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/Local.h" |
| 27 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
| 29 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 12f0bab | 2009-08-27 04:32:07 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | a51bcb5 | 2003-11-21 21:45:31 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 35 | STATISTIC(NumInlined, "Number of functions inlined"); |
Chris Lattner | 83f66fe | 2010-05-01 17:19:38 +0000 | [diff] [blame] | 36 | STATISTIC(NumCallsDeleted, "Number of call sites deleted, not inlined"); |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 37 | STATISTIC(NumDeleted, "Number of functions deleted because all callers found"); |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 38 | STATISTIC(NumMergedAllocas, "Number of allocas merged together"); |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 39 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 40 | static cl::opt<int> |
Jakob Stoklund Olesen | f9c3b22 | 2010-02-04 18:48:20 +0000 | [diff] [blame] | 41 | InlineLimit("inline-threshold", cl::Hidden, cl::init(225), cl::ZeroOrMore, |
| 42 | cl::desc("Control the amount of inlining to perform (default = 225)")); |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 43 | |
Jakob Stoklund Olesen | f0907fe | 2010-02-13 01:51:53 +0000 | [diff] [blame] | 44 | static cl::opt<int> |
| 45 | HintThreshold("inlinehint-threshold", cl::Hidden, cl::init(325), |
| 46 | cl::desc("Threshold for inlining functions with inline hint")); |
Jakob Stoklund Olesen | 570a4a5 | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 47 | |
| 48 | // Threshold to use when optsize is specified (and there is no -inline-limit). |
| 49 | const int OptSizeThreshold = 75; |
| 50 | |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 51 | Inliner::Inliner(char &ID) |
Chad Rosier | fa086f1 | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 52 | : CallGraphSCCPass(ID), InlineThreshold(InlineLimit), InsertLifetime(true) {} |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 53 | |
Chad Rosier | fa086f1 | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 54 | Inliner::Inliner(char &ID, int Threshold, bool InsertLifetime) |
Jakob Stoklund Olesen | 930f5ef | 2010-11-02 23:40:26 +0000 | [diff] [blame] | 55 | : CallGraphSCCPass(ID), InlineThreshold(InlineLimit.getNumOccurrences() > 0 ? |
Chad Rosier | fa086f1 | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 56 | InlineLimit : Threshold), |
| 57 | InsertLifetime(InsertLifetime) {} |
Chris Lattner | 120d053 | 2008-01-12 06:49:13 +0000 | [diff] [blame] | 58 | |
Chris Lattner | ff2dad3 | 2007-01-30 23:28:39 +0000 | [diff] [blame] | 59 | /// getAnalysisUsage - For this class, we declare that we require and preserve |
| 60 | /// the call graph. If the derived class implements this method, it should |
| 61 | /// always explicitly call the implementation here. |
| 62 | void Inliner::getAnalysisUsage(AnalysisUsage &Info) const { |
Chris Lattner | ff2dad3 | 2007-01-30 23:28:39 +0000 | [diff] [blame] | 63 | CallGraphSCCPass::getAnalysisUsage(Info); |
| 64 | } |
| 65 | |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 66 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 67 | typedef DenseMap<ArrayType*, std::vector<AllocaInst*> > |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 68 | InlinedArrayAllocasTy; |
| 69 | |
| 70 | /// InlineCallIfPossible - If it is possible to inline the specified call site, |
| 71 | /// do so and update the CallGraph for this operation. |
| 72 | /// |
| 73 | /// This function also does some basic book-keeping to update the IR. The |
Chris Lattner | cc0a029 | 2009-08-28 04:48:54 +0000 | [diff] [blame] | 74 | /// InlinedArrayAllocas map keeps track of any allocas that are already |
| 75 | /// available from other functions inlined into the caller. If we are able to |
| 76 | /// inline this call site we attempt to reuse already available allocas or add |
| 77 | /// any new allocas to the set if not possible. |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 78 | static bool InlineCallIfPossible(CallSite CS, InlineFunctionInfo &IFI, |
Chris Lattner | 6c3ee0f | 2010-12-06 07:52:42 +0000 | [diff] [blame] | 79 | InlinedArrayAllocasTy &InlinedArrayAllocas, |
Chad Rosier | fa086f1 | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 80 | int InlineHistory, bool InsertLifetime) { |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 81 | Function *Callee = CS.getCalledFunction(); |
Bill Wendling | 66c75aa | 2008-11-21 00:09:21 +0000 | [diff] [blame] | 82 | Function *Caller = CS.getCaller(); |
| 83 | |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 84 | // Try to inline the function. Get the list of static allocas that were |
| 85 | // inlined. |
Chad Rosier | fa086f1 | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 86 | if (!InlineFunction(CS, IFI, InsertLifetime)) |
Chris Lattner | 12f0bab | 2009-08-27 04:32:07 +0000 | [diff] [blame] | 87 | return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 88 | |
Bill Wendling | 8c1604e | 2008-11-21 00:06:32 +0000 | [diff] [blame] | 89 | // If the inlined function had a higher stack protection level than the |
| 90 | // calling function, then bump up the caller's stack protection level. |
Bill Wendling | 8c1604e | 2008-11-21 00:06:32 +0000 | [diff] [blame] | 91 | if (Callee->hasFnAttr(Attribute::StackProtectReq)) |
| 92 | Caller->addFnAttr(Attribute::StackProtectReq); |
| 93 | else if (Callee->hasFnAttr(Attribute::StackProtect) && |
| 94 | !Caller->hasFnAttr(Attribute::StackProtectReq)) |
| 95 | Caller->addFnAttr(Attribute::StackProtect); |
| 96 | |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 97 | // Look at all of the allocas that we inlined through this call site. If we |
| 98 | // have already inlined other allocas through other calls into this function, |
| 99 | // then we know that they have disjoint lifetimes and that we can merge them. |
| 100 | // |
| 101 | // There are many heuristics possible for merging these allocas, and the |
| 102 | // different options have different tradeoffs. One thing that we *really* |
| 103 | // don't want to hurt is SRoA: once inlining happens, often allocas are no |
| 104 | // longer address taken and so they can be promoted. |
| 105 | // |
| 106 | // Our "solution" for that is to only merge allocas whose outermost type is an |
| 107 | // array type. These are usually not promoted because someone is using a |
| 108 | // variable index into them. These are also often the most important ones to |
| 109 | // merge. |
| 110 | // |
| 111 | // A better solution would be to have real memory lifetime markers in the IR |
| 112 | // and not have the inliner do any merging of allocas at all. This would |
| 113 | // allow the backend to do proper stack slot coloring of all allocas that |
| 114 | // *actually make it to the backend*, which is really what we want. |
| 115 | // |
| 116 | // Because we don't have this information, we do this simple and useful hack. |
| 117 | // |
| 118 | SmallPtrSet<AllocaInst*, 16> UsedAllocas; |
| 119 | |
Chris Lattner | 6c3ee0f | 2010-12-06 07:52:42 +0000 | [diff] [blame] | 120 | // When processing our SCC, check to see if CS was inlined from some other |
| 121 | // call site. For example, if we're processing "A" in this code: |
| 122 | // A() { B() } |
| 123 | // B() { x = alloca ... C() } |
| 124 | // C() { y = alloca ... } |
| 125 | // Assume that C was not inlined into B initially, and so we're processing A |
| 126 | // and decide to inline B into A. Doing this makes an alloca available for |
| 127 | // reuse and makes a callsite (C) available for inlining. When we process |
| 128 | // the C call site we don't want to do any alloca merging between X and Y |
| 129 | // because their scopes are not disjoint. We could make this smarter by |
| 130 | // keeping track of the inline history for each alloca in the |
| 131 | // InlinedArrayAllocas but this isn't likely to be a significant win. |
| 132 | if (InlineHistory != -1) // Only do merging for top-level call sites in SCC. |
| 133 | return true; |
| 134 | |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 135 | // Loop over all the allocas we have so far and see if they can be merged with |
| 136 | // a previously inlined alloca. If not, remember that we had it. |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 137 | for (unsigned AllocaNo = 0, e = IFI.StaticAllocas.size(); |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 138 | AllocaNo != e; ++AllocaNo) { |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 139 | AllocaInst *AI = IFI.StaticAllocas[AllocaNo]; |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 140 | |
| 141 | // Don't bother trying to merge array allocations (they will usually be |
| 142 | // canonicalized to be an allocation *of* an array), or allocations whose |
| 143 | // type is not itself an array (because we're afraid of pessimizing SRoA). |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 144 | ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType()); |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 145 | if (ATy == 0 || AI->isArrayAllocation()) |
| 146 | continue; |
| 147 | |
| 148 | // Get the list of all available allocas for this array type. |
| 149 | std::vector<AllocaInst*> &AllocasForType = InlinedArrayAllocas[ATy]; |
| 150 | |
| 151 | // Loop over the allocas in AllocasForType to see if we can reuse one. Note |
| 152 | // that we have to be careful not to reuse the same "available" alloca for |
| 153 | // multiple different allocas that we just inlined, we use the 'UsedAllocas' |
| 154 | // set to keep track of which "available" allocas are being used by this |
| 155 | // function. Also, AllocasForType can be empty of course! |
| 156 | bool MergedAwayAlloca = false; |
| 157 | for (unsigned i = 0, e = AllocasForType.size(); i != e; ++i) { |
| 158 | AllocaInst *AvailableAlloca = AllocasForType[i]; |
| 159 | |
| 160 | // The available alloca has to be in the right function, not in some other |
| 161 | // function in this SCC. |
| 162 | if (AvailableAlloca->getParent() != AI->getParent()) |
| 163 | continue; |
| 164 | |
| 165 | // If the inlined function already uses this alloca then we can't reuse |
| 166 | // it. |
| 167 | if (!UsedAllocas.insert(AvailableAlloca)) |
| 168 | continue; |
| 169 | |
| 170 | // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare |
| 171 | // success! |
Chris Lattner | 7d32b80 | 2010-12-06 07:38:40 +0000 | [diff] [blame] | 172 | DEBUG(dbgs() << " ***MERGED ALLOCA: " << *AI << "\n\t\tINTO: " |
| 173 | << *AvailableAlloca << '\n'); |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 174 | |
| 175 | AI->replaceAllUsesWith(AvailableAlloca); |
| 176 | AI->eraseFromParent(); |
| 177 | MergedAwayAlloca = true; |
| 178 | ++NumMergedAllocas; |
Chris Lattner | 7d32b80 | 2010-12-06 07:38:40 +0000 | [diff] [blame] | 179 | IFI.StaticAllocas[AllocaNo] = 0; |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 180 | break; |
| 181 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 183 | // If we already nuked the alloca, we're done with it. |
| 184 | if (MergedAwayAlloca) |
| 185 | continue; |
Chris Lattner | 7d32b80 | 2010-12-06 07:38:40 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 187 | // If we were unable to merge away the alloca either because there are no |
| 188 | // allocas of the right type available or because we reused them all |
| 189 | // already, remember that this alloca came from an inlined function and mark |
| 190 | // it used so we don't reuse it for other allocas from this inline |
| 191 | // operation. |
| 192 | AllocasForType.push_back(AI); |
| 193 | UsedAllocas.insert(AI); |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 194 | } |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 195 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 196 | return true; |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 197 | } |
Jakob Stoklund Olesen | f8526cb | 2010-01-20 17:51:28 +0000 | [diff] [blame] | 198 | |
Jakob Stoklund Olesen | 570a4a5 | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 199 | unsigned Inliner::getInlineThreshold(CallSite CS) const { |
Jakob Stoklund Olesen | f0907fe | 2010-02-13 01:51:53 +0000 | [diff] [blame] | 200 | int thres = InlineThreshold; |
Jakob Stoklund Olesen | 570a4a5 | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 201 | |
| 202 | // Listen to optsize when -inline-limit is not given. |
| 203 | Function *Caller = CS.getCaller(); |
Jakob Stoklund Olesen | f8526cb | 2010-01-20 17:51:28 +0000 | [diff] [blame] | 204 | if (Caller && !Caller->isDeclaration() && |
| 205 | Caller->hasFnAttr(Attribute::OptimizeForSize) && |
| 206 | InlineLimit.getNumOccurrences() == 0) |
Jakob Stoklund Olesen | f0907fe | 2010-02-13 01:51:53 +0000 | [diff] [blame] | 207 | thres = OptSizeThreshold; |
Jakob Stoklund Olesen | 570a4a5 | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 208 | |
Jakob Stoklund Olesen | f0907fe | 2010-02-13 01:51:53 +0000 | [diff] [blame] | 209 | // Listen to inlinehint when it would increase the threshold. |
| 210 | Function *Callee = CS.getCalledFunction(); |
| 211 | if (HintThreshold > thres && Callee && !Callee->isDeclaration() && |
| 212 | Callee->hasFnAttr(Attribute::InlineHint)) |
| 213 | thres = HintThreshold; |
| 214 | |
| 215 | return thres; |
Jakob Stoklund Olesen | f8526cb | 2010-01-20 17:51:28 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Daniel Dunbar | 1a99dbf | 2008-10-29 01:02:02 +0000 | [diff] [blame] | 218 | /// shouldInline - Return true if the inliner should attempt to inline |
| 219 | /// at the given CallSite. |
| 220 | bool Inliner::shouldInline(CallSite CS) { |
Daniel Dunbar | c5e1ec4 | 2008-10-30 19:26:59 +0000 | [diff] [blame] | 221 | InlineCost IC = getInlineCost(CS); |
Daniel Dunbar | 1a99dbf | 2008-10-29 01:02:02 +0000 | [diff] [blame] | 222 | |
Daniel Dunbar | c5e1ec4 | 2008-10-30 19:26:59 +0000 | [diff] [blame] | 223 | if (IC.isAlways()) { |
David Greene | c0aa679 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 224 | DEBUG(dbgs() << " Inlining: cost=always" |
Bill Wendling | 84a832f | 2009-07-31 19:52:24 +0000 | [diff] [blame] | 225 | << ", Call: " << *CS.getInstruction() << "\n"); |
Daniel Dunbar | c5e1ec4 | 2008-10-30 19:26:59 +0000 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
| 229 | if (IC.isNever()) { |
David Greene | c0aa679 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 230 | DEBUG(dbgs() << " NOT Inlining: cost=never" |
Bill Wendling | 84a832f | 2009-07-31 19:52:24 +0000 | [diff] [blame] | 231 | << ", Call: " << *CS.getInstruction() << "\n"); |
Daniel Dunbar | c5e1ec4 | 2008-10-30 19:26:59 +0000 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | |
| 235 | int Cost = IC.getValue(); |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 236 | Function *Caller = CS.getCaller(); |
Jakob Stoklund Olesen | 570a4a5 | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 237 | int CurrentThreshold = getInlineThreshold(CS); |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 238 | float FudgeFactor = getInlineFudgeFactor(CS); |
Jakob Stoklund Olesen | 2ddbf82 | 2010-03-09 00:59:53 +0000 | [diff] [blame] | 239 | int AdjThreshold = (int)(CurrentThreshold * FudgeFactor); |
| 240 | if (Cost >= AdjThreshold) { |
David Greene | c0aa679 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 241 | DEBUG(dbgs() << " NOT Inlining: cost=" << Cost |
Jakob Stoklund Olesen | 2ddbf82 | 2010-03-09 00:59:53 +0000 | [diff] [blame] | 242 | << ", thres=" << AdjThreshold |
Bill Wendling | 84a832f | 2009-07-31 19:52:24 +0000 | [diff] [blame] | 243 | << ", Call: " << *CS.getInstruction() << "\n"); |
Daniel Dunbar | 1a99dbf | 2008-10-29 01:02:02 +0000 | [diff] [blame] | 244 | return false; |
Daniel Dunbar | 1a99dbf | 2008-10-29 01:02:02 +0000 | [diff] [blame] | 245 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 246 | |
Chandler Carruth | b16117c | 2012-03-14 20:16:41 +0000 | [diff] [blame^] | 247 | // Try to detect the case where the current inlining candidate caller (call |
| 248 | // it B) is a static or linkonce-ODR function and is an inlining candidate |
| 249 | // elsewhere, and the current candidate callee (call it C) is large enough |
| 250 | // that inlining it into B would make B too big to inline later. In these |
| 251 | // circumstances it may be best not to inline C into B, but to inline B into |
| 252 | // its callers. |
| 253 | // |
| 254 | // This only applies to static and linkonce-ODR functions because those are |
| 255 | // expected to be available for inlining in the translation units where they |
| 256 | // are used. Thus we will always have the opportunity to make local inlining |
| 257 | // decisions. Importantly the linkonce-ODR linkage covers inline functions |
| 258 | // and templates in C++. |
| 259 | if (Caller->hasLocalLinkage() || |
| 260 | Caller->getLinkage() == GlobalValue::LinkOnceODRLinkage) { |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 261 | int TotalSecondaryCost = 0; |
| 262 | bool outerCallsFound = false; |
Dale Johannesen | c84e3c0 | 2011-01-04 19:01:54 +0000 | [diff] [blame] | 263 | // This bool tracks what happens if we do NOT inline C into B. |
| 264 | bool callerWillBeRemoved = true; |
| 265 | // This bool tracks what happens if we DO inline C into B. |
| 266 | bool inliningPreventsSomeOuterInline = false; |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 267 | for (Value::use_iterator I = Caller->use_begin(), E =Caller->use_end(); |
| 268 | I != E; ++I) { |
Gabor Greif | 7d3056b | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 269 | CallSite CS2(*I); |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 270 | |
| 271 | // If this isn't a call to Caller (it could be some other sort |
Dale Johannesen | c84e3c0 | 2011-01-04 19:01:54 +0000 | [diff] [blame] | 272 | // of reference) skip it. Such references will prevent the caller |
| 273 | // from being removed. |
| 274 | if (!CS2 || CS2.getCalledFunction() != Caller) { |
| 275 | callerWillBeRemoved = false; |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 276 | continue; |
Dale Johannesen | c84e3c0 | 2011-01-04 19:01:54 +0000 | [diff] [blame] | 277 | } |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 278 | |
| 279 | InlineCost IC2 = getInlineCost(CS2); |
| 280 | if (IC2.isNever()) |
Dale Johannesen | c84e3c0 | 2011-01-04 19:01:54 +0000 | [diff] [blame] | 281 | callerWillBeRemoved = false; |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 282 | if (IC2.isAlways() || IC2.isNever()) |
| 283 | continue; |
| 284 | |
| 285 | outerCallsFound = true; |
| 286 | int Cost2 = IC2.getValue(); |
Jakob Stoklund Olesen | 570a4a5 | 2010-02-06 01:16:28 +0000 | [diff] [blame] | 287 | int CurrentThreshold2 = getInlineThreshold(CS2); |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 288 | float FudgeFactor2 = getInlineFudgeFactor(CS2); |
| 289 | |
| 290 | if (Cost2 >= (int)(CurrentThreshold2 * FudgeFactor2)) |
Dale Johannesen | c84e3c0 | 2011-01-04 19:01:54 +0000 | [diff] [blame] | 291 | callerWillBeRemoved = false; |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 292 | |
Dale Johannesen | bdb984b | 2009-10-09 21:42:02 +0000 | [diff] [blame] | 293 | // See if we have this case. We subtract off the penalty |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 294 | // for the call instruction, which we would be deleting. |
| 295 | if (Cost2 < (int)(CurrentThreshold2 * FudgeFactor2) && |
Dale Johannesen | bdb984b | 2009-10-09 21:42:02 +0000 | [diff] [blame] | 296 | Cost2 + Cost - (InlineConstants::CallPenalty + 1) >= |
| 297 | (int)(CurrentThreshold2 * FudgeFactor2)) { |
Dale Johannesen | c84e3c0 | 2011-01-04 19:01:54 +0000 | [diff] [blame] | 298 | inliningPreventsSomeOuterInline = true; |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 299 | TotalSecondaryCost += Cost2; |
| 300 | } |
| 301 | } |
| 302 | // If all outer calls to Caller would get inlined, the cost for the last |
| 303 | // one is set very low by getInlineCost, in anticipation that Caller will |
| 304 | // be removed entirely. We did not account for this above unless there |
| 305 | // is only one caller of Caller. |
Dale Johannesen | c84e3c0 | 2011-01-04 19:01:54 +0000 | [diff] [blame] | 306 | if (callerWillBeRemoved && Caller->use_begin() != Caller->use_end()) |
Dale Johannesen | bdb984b | 2009-10-09 21:42:02 +0000 | [diff] [blame] | 307 | TotalSecondaryCost += InlineConstants::LastCallToStaticBonus; |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 308 | |
Dale Johannesen | c84e3c0 | 2011-01-04 19:01:54 +0000 | [diff] [blame] | 309 | if (outerCallsFound && inliningPreventsSomeOuterInline && |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 310 | TotalSecondaryCost < Cost) { |
David Greene | c0aa679 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 311 | DEBUG(dbgs() << " NOT Inlining: " << *CS.getInstruction() << |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 312 | " Cost = " << Cost << |
| 313 | ", outer Cost = " << TotalSecondaryCost << '\n'); |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | |
David Greene | c0aa679 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 318 | DEBUG(dbgs() << " Inlining: cost=" << Cost |
Jakob Stoklund Olesen | 2ddbf82 | 2010-03-09 00:59:53 +0000 | [diff] [blame] | 319 | << ", thres=" << AdjThreshold |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 320 | << ", Call: " << *CS.getInstruction() << '\n'); |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 321 | return true; |
Daniel Dunbar | 1a99dbf | 2008-10-29 01:02:02 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 324 | /// InlineHistoryIncludes - Return true if the specified inline history ID |
| 325 | /// indicates an inline history that includes the specified function. |
| 326 | static bool InlineHistoryIncludes(Function *F, int InlineHistoryID, |
| 327 | const SmallVectorImpl<std::pair<Function*, int> > &InlineHistory) { |
| 328 | while (InlineHistoryID != -1) { |
| 329 | assert(unsigned(InlineHistoryID) < InlineHistory.size() && |
| 330 | "Invalid inline history ID"); |
| 331 | if (InlineHistory[InlineHistoryID].first == F) |
| 332 | return true; |
| 333 | InlineHistoryID = InlineHistory[InlineHistoryID].second; |
| 334 | } |
| 335 | return false; |
| 336 | } |
| 337 | |
Chandler Carruth | 6c0b3ac | 2012-03-12 11:19:33 +0000 | [diff] [blame] | 338 | /// \brief Simplify arguments going into a particular callsite. |
| 339 | /// |
| 340 | /// This is important to do each time we add a callsite due to inlining so that |
| 341 | /// constants and other entities which feed into inline cost estimation are |
| 342 | /// properly recognized when analyzing the new callsite. Consider: |
| 343 | /// void outer(int x) { |
| 344 | /// if (x < 42) |
| 345 | /// return inner(42 - x); |
| 346 | /// ... |
| 347 | /// } |
| 348 | /// void inner(int x) { |
| 349 | /// ... |
| 350 | /// } |
| 351 | /// |
| 352 | /// The inliner gives calls to 'outer' with a constant argument a bonus because |
| 353 | /// it will delete one side of a branch. But the resulting call to 'inner' |
| 354 | /// will, after inlining, also have a constant operand. We need to do just |
| 355 | /// enough constant folding to expose this for callsite arguments. The rest |
| 356 | /// will be taken care of after the inliner finishes running. |
| 357 | static void simplifyCallSiteArguments(const TargetData *TD, CallSite CS) { |
| 358 | // FIXME: It would be nice to avoid this smallvector if RAUW doesn't |
| 359 | // invalidate operand iterators in any cases. |
| 360 | SmallVector<std::pair<Value *, Value*>, 4> SimplifiedArgs; |
| 361 | for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 362 | I != E; ++I) |
| 363 | if (Instruction *Inst = dyn_cast<Instruction>(*I)) |
| 364 | if (Value *SimpleArg = SimplifyInstruction(Inst, TD)) |
| 365 | SimplifiedArgs.push_back(std::make_pair(Inst, SimpleArg)); |
| 366 | for (unsigned Idx = 0, Size = SimplifiedArgs.size(); Idx != Size; ++Idx) |
| 367 | SimplifiedArgs[Idx].first->replaceAllUsesWith(SimplifiedArgs[Idx].second); |
| 368 | } |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 370 | bool Inliner::runOnSCC(CallGraphSCC &SCC) { |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 371 | CallGraph &CG = getAnalysis<CallGraph>(); |
Dan Gohman | 02a436c | 2009-07-24 18:13:53 +0000 | [diff] [blame] | 372 | const TargetData *TD = getAnalysisIfAvailable<TargetData>(); |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 373 | |
Dale Johannesen | 16581bf | 2009-03-23 23:39:20 +0000 | [diff] [blame] | 374 | SmallPtrSet<Function*, 8> SCCFunctions; |
David Greene | c0aa679 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 375 | DEBUG(dbgs() << "Inliner visiting SCC:"); |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 376 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 377 | Function *F = (*I)->getFunction(); |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 378 | if (F) SCCFunctions.insert(F); |
David Greene | c0aa679 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 379 | DEBUG(dbgs() << " " << (F ? F->getName() : "INDIRECTNODE")); |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 380 | } |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 381 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 382 | // Scan through and identify all call sites ahead of time so that we only |
| 383 | // inline call sites in the original functions, not call sites that result |
| 384 | // from inlining other functions. |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 385 | SmallVector<std::pair<CallSite, int>, 16> CallSites; |
| 386 | |
| 387 | // When inlining a callee produces new call sites, we want to keep track of |
| 388 | // the fact that they were inlined from the callee. This allows us to avoid |
| 389 | // infinite inlining in some obscure cases. To represent this, we use an |
| 390 | // index into the InlineHistory vector. |
| 391 | SmallVector<std::pair<Function*, int>, 8> InlineHistory; |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 393 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 394 | Function *F = (*I)->getFunction(); |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 395 | if (!F) continue; |
| 396 | |
| 397 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 398 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
Gabor Greif | 7d3056b | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 399 | CallSite CS(cast<Value>(I)); |
Dale Johannesen | e91b9a3 | 2009-10-09 00:11:32 +0000 | [diff] [blame] | 400 | // If this isn't a call, or it is a call to an intrinsic, it can |
Chris Lattner | d43d5e8 | 2009-08-31 05:34:32 +0000 | [diff] [blame] | 401 | // never be inlined. |
Gabor Greif | 7d3056b | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 402 | if (!CS || isa<IntrinsicInst>(I)) |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 403 | continue; |
| 404 | |
Chris Lattner | d43d5e8 | 2009-08-31 05:34:32 +0000 | [diff] [blame] | 405 | // If this is a direct call to an external function, we can never inline |
| 406 | // it. If it is an indirect call, inlining may resolve it to be a |
| 407 | // direct call, so we keep it. |
| 408 | if (CS.getCalledFunction() && CS.getCalledFunction()->isDeclaration()) |
| 409 | continue; |
| 410 | |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 411 | CallSites.push_back(std::make_pair(CS, -1)); |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 414 | |
David Greene | c0aa679 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 415 | DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 4471136 | 2010-04-20 00:47:08 +0000 | [diff] [blame] | 417 | // If there are no calls in this function, exit early. |
| 418 | if (CallSites.empty()) |
| 419 | return false; |
| 420 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 421 | // Now that we have all of the call sites, move the ones to functions in the |
| 422 | // current SCC to the end of the list. |
| 423 | unsigned FirstCallInSCC = CallSites.size(); |
| 424 | for (unsigned i = 0; i < FirstCallInSCC; ++i) |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 425 | if (Function *F = CallSites[i].first.getCalledFunction()) |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 426 | if (SCCFunctions.count(F)) |
| 427 | std::swap(CallSites[i--], CallSites[--FirstCallInSCC]); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 429 | |
| 430 | InlinedArrayAllocasTy InlinedArrayAllocas; |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 431 | InlineFunctionInfo InlineInfo(&CG, TD); |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 432 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 433 | // Now that we have all of the call sites, loop over them and inline them if |
| 434 | // it looks profitable to do so. |
| 435 | bool Changed = false; |
| 436 | bool LocalChange; |
| 437 | do { |
| 438 | LocalChange = false; |
| 439 | // Iterate over the outer loop because inlining functions can cause indirect |
| 440 | // calls to become direct calls. |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 441 | for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) { |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 442 | CallSite CS = CallSites[CSi].first; |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 444 | Function *Caller = CS.getCaller(); |
Chris Lattner | dbab4dc | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 445 | Function *Callee = CS.getCalledFunction(); |
| 446 | |
| 447 | // If this call site is dead and it is to a readonly function, we should |
| 448 | // just delete the call instead of trying to inline it, regardless of |
| 449 | // size. This happens because IPSCCP propagates the result out of the |
| 450 | // call and then we're left with the dead call. |
Chris Lattner | 4ff4141 | 2009-11-12 21:58:18 +0000 | [diff] [blame] | 451 | if (isInstructionTriviallyDead(CS.getInstruction())) { |
David Greene | c0aa679 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 452 | DEBUG(dbgs() << " -> Deleting dead call: " |
Chris Lattner | dbab4dc | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 453 | << *CS.getInstruction() << "\n"); |
| 454 | // Update the call graph by deleting the edge from Callee to Caller. |
| 455 | CG[Caller]->removeCallEdgeFor(CS); |
| 456 | CS.getInstruction()->eraseFromParent(); |
| 457 | ++NumCallsDeleted; |
Jakob Stoklund Olesen | f747747 | 2010-03-09 23:02:17 +0000 | [diff] [blame] | 458 | // Update the cached cost info with the missing call |
| 459 | growCachedCostInfo(Caller, NULL); |
Chris Lattner | dbab4dc | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 460 | } else { |
| 461 | // We can only inline direct calls to non-declarations. |
| 462 | if (Callee == 0 || Callee->isDeclaration()) continue; |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 463 | |
Eric Christopher | f0193ed | 2010-07-13 18:27:13 +0000 | [diff] [blame] | 464 | // If this call site was obtained by inlining another function, verify |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 465 | // that the include path for the function did not include the callee |
Chris Lattner | 7d32b80 | 2010-12-06 07:38:40 +0000 | [diff] [blame] | 466 | // itself. If so, we'd be recursively inlining the same function, |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 467 | // which would provide the same callsites, which would cause us to |
| 468 | // infinitely inline. |
| 469 | int InlineHistoryID = CallSites[CSi].second; |
| 470 | if (InlineHistoryID != -1 && |
| 471 | InlineHistoryIncludes(Callee, InlineHistoryID, InlineHistory)) |
| 472 | continue; |
| 473 | |
| 474 | |
Chris Lattner | dbab4dc | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 475 | // If the policy determines that we should inline this function, |
| 476 | // try to do so. |
| 477 | if (!shouldInline(CS)) |
| 478 | continue; |
| 479 | |
Chris Lattner | fe9af3b | 2010-04-22 23:37:35 +0000 | [diff] [blame] | 480 | // Attempt to inline the function. |
Chris Lattner | 6c3ee0f | 2010-12-06 07:52:42 +0000 | [diff] [blame] | 481 | if (!InlineCallIfPossible(CS, InlineInfo, InlinedArrayAllocas, |
Chad Rosier | fa086f1 | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 482 | InlineHistoryID, InsertLifetime)) |
Chris Lattner | dbab4dc | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 483 | continue; |
| 484 | ++NumInlined; |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 0ca2f28 | 2010-05-01 01:26:13 +0000 | [diff] [blame] | 486 | // If inlining this function gave us any new call sites, throw them |
Chris Lattner | fe9af3b | 2010-04-22 23:37:35 +0000 | [diff] [blame] | 487 | // onto our worklist to process. They are useful inline candidates. |
Chris Lattner | 0ca2f28 | 2010-05-01 01:26:13 +0000 | [diff] [blame] | 488 | if (!InlineInfo.InlinedCalls.empty()) { |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 489 | // Create a new inline history entry for this, so that we remember |
| 490 | // that these new callsites came about due to inlining Callee. |
| 491 | int NewHistoryID = InlineHistory.size(); |
| 492 | InlineHistory.push_back(std::make_pair(Callee, InlineHistoryID)); |
| 493 | |
Chris Lattner | 0ca2f28 | 2010-05-01 01:26:13 +0000 | [diff] [blame] | 494 | for (unsigned i = 0, e = InlineInfo.InlinedCalls.size(); |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 495 | i != e; ++i) { |
Chris Lattner | 0ca2f28 | 2010-05-01 01:26:13 +0000 | [diff] [blame] | 496 | Value *Ptr = InlineInfo.InlinedCalls[i]; |
Chandler Carruth | 6c0b3ac | 2012-03-12 11:19:33 +0000 | [diff] [blame] | 497 | CallSite NewCS = Ptr; |
| 498 | simplifyCallSiteArguments(TD, NewCS); |
| 499 | CallSites.push_back(std::make_pair(NewCS, NewHistoryID)); |
Chris Lattner | 1595287 | 2010-05-01 01:05:10 +0000 | [diff] [blame] | 500 | } |
Chris Lattner | 0768632 | 2010-04-23 18:37:01 +0000 | [diff] [blame] | 501 | } |
Chris Lattner | fe9af3b | 2010-04-22 23:37:35 +0000 | [diff] [blame] | 502 | |
Jakob Stoklund Olesen | f747747 | 2010-03-09 23:02:17 +0000 | [diff] [blame] | 503 | // Update the cached cost info with the inlined call. |
| 504 | growCachedCostInfo(Caller, Callee); |
Chris Lattner | dbab4dc | 2009-11-12 07:56:08 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // If we inlined or deleted the last possible call site to the function, |
| 508 | // delete the function body now. |
| 509 | if (Callee && Callee->use_empty() && Callee->hasLocalLinkage() && |
Chris Lattner | d43d5e8 | 2009-08-31 05:34:32 +0000 | [diff] [blame] | 510 | // TODO: Can remove if in SCC now. |
Chris Lattner | b374b90 | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 511 | !SCCFunctions.count(Callee) && |
Chris Lattner | d43d5e8 | 2009-08-31 05:34:32 +0000 | [diff] [blame] | 512 | |
Chris Lattner | b374b90 | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 513 | // The function may be apparently dead, but if there are indirect |
| 514 | // callgraph references to the node, we cannot delete it yet, this |
| 515 | // could invalidate the CGSCC iterator. |
| 516 | CG[Callee]->getNumReferences() == 0) { |
David Greene | c0aa679 | 2010-01-05 01:27:51 +0000 | [diff] [blame] | 517 | DEBUG(dbgs() << " -> Deleting dead function: " |
Chris Lattner | 199ba42 | 2009-08-27 06:29:33 +0000 | [diff] [blame] | 518 | << Callee->getName() << "\n"); |
| 519 | CallGraphNode *CalleeNode = CG[Callee]; |
| 520 | |
| 521 | // Remove any call graph edges from the callee to its callees. |
| 522 | CalleeNode->removeAllCalledFunctions(); |
| 523 | |
| 524 | resetCachedCostInfo(Callee); |
| 525 | |
| 526 | // Removing the node for callee from the call graph and delete it. |
| 527 | delete CG.removeFunctionFromModule(CalleeNode); |
| 528 | ++NumDeleted; |
| 529 | } |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 530 | |
| 531 | // Remove this call site from the list. If possible, use |
| 532 | // swap/pop_back for efficiency, but do not use it if doing so would |
| 533 | // move a call site to a function in this SCC before the |
| 534 | // 'FirstCallInSCC' barrier. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 535 | if (SCC.isSingular()) { |
Benjamin Kramer | c29df3c | 2010-05-31 12:50:41 +0000 | [diff] [blame] | 536 | CallSites[CSi] = CallSites.back(); |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 537 | CallSites.pop_back(); |
| 538 | } else { |
| 539 | CallSites.erase(CallSites.begin()+CSi); |
| 540 | } |
| 541 | --CSi; |
| 542 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 543 | Changed = true; |
| 544 | LocalChange = true; |
| 545 | } |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 546 | } while (LocalChange); |
Chris Lattner | 775cbdd | 2004-04-08 06:34:31 +0000 | [diff] [blame] | 547 | |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 548 | return Changed; |
| 549 | } |
| 550 | |
Chris Lattner | 68d57e7 | 2004-04-20 22:06:53 +0000 | [diff] [blame] | 551 | // doFinalization - Remove now-dead linkonce functions at the end of |
| 552 | // processing to avoid breaking the SCC traversal. |
| 553 | bool Inliner::doFinalization(CallGraph &CG) { |
Devang Patel | b7c6bf1 | 2008-11-05 01:39:16 +0000 | [diff] [blame] | 554 | return removeDeadFunctions(CG); |
| 555 | } |
| 556 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 557 | /// removeDeadFunctions - Remove dead functions that are not included in |
| 558 | /// DNR (Do Not Remove) list. |
Devang Patel | b7c6bf1 | 2008-11-05 01:39:16 +0000 | [diff] [blame] | 559 | bool Inliner::removeDeadFunctions(CallGraph &CG, |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 560 | SmallPtrSet<const Function *, 16> *DNR) { |
| 561 | SmallPtrSet<CallGraphNode*, 16> FunctionsToRemove; |
Chris Lattner | 3e1358a | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 562 | |
| 563 | // Scan for all of the functions, looking for ones that should now be removed |
| 564 | // from the program. Insert the dead ones in the FunctionsToRemove set. |
| 565 | for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) { |
| 566 | CallGraphNode *CGN = I->second; |
Chris Lattner | b374b90 | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 567 | if (CGN->getFunction() == 0) |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 568 | continue; |
| 569 | |
| 570 | Function *F = CGN->getFunction(); |
| 571 | |
| 572 | // If the only remaining users of the function are dead constants, remove |
| 573 | // them. |
| 574 | F->removeDeadConstantUsers(); |
Chris Lattner | 3e1358a | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 575 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 576 | if (DNR && DNR->count(F)) |
| 577 | continue; |
Eli Friedman | c663305 | 2011-10-20 05:23:42 +0000 | [diff] [blame] | 578 | if (!F->isDefTriviallyDead()) |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 579 | continue; |
| 580 | |
| 581 | // Remove any call graph edges from the function to its callees. |
| 582 | CGN->removeAllCalledFunctions(); |
Devang Patel | b7c6bf1 | 2008-11-05 01:39:16 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 584 | // Remove any edges from the external node to the function's call graph |
| 585 | // node. These edges might have been made irrelegant due to |
| 586 | // optimization of the program. |
| 587 | CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN); |
Chris Lattner | 0c0aa71 | 2004-09-18 21:37:03 +0000 | [diff] [blame] | 588 | |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 589 | // Removing the node for callee from the call graph and delete it. |
| 590 | FunctionsToRemove.insert(CGN); |
Chris Lattner | 68d57e7 | 2004-04-20 22:06:53 +0000 | [diff] [blame] | 591 | } |
Chris Lattner | 3e1358a | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 592 | |
| 593 | // Now that we know which functions to delete, do so. We didn't want to do |
| 594 | // this inline, because that would invalidate our CallGraph::iterator |
| 595 | // objects. :( |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 596 | // |
| 597 | // Note that it doesn't matter that we are iterating over a non-stable set |
| 598 | // here to do this, it doesn't matter which order the functions are deleted |
| 599 | // in. |
Chris Lattner | 3e1358a | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 600 | bool Changed = false; |
Chris Lattner | 135755d | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 601 | for (SmallPtrSet<CallGraphNode*, 16>::iterator I = FunctionsToRemove.begin(), |
| 602 | E = FunctionsToRemove.end(); I != E; ++I) { |
Dale Johannesen | 1f67ce4 | 2009-03-19 18:03:56 +0000 | [diff] [blame] | 603 | resetCachedCostInfo((*I)->getFunction()); |
Chris Lattner | 3e1358a | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 604 | delete CG.removeFunctionFromModule(*I); |
| 605 | ++NumDeleted; |
| 606 | Changed = true; |
| 607 | } |
| 608 | |
Chris Lattner | 68d57e7 | 2004-04-20 22:06:53 +0000 | [diff] [blame] | 609 | return Changed; |
| 610 | } |