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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | |
| 16 | #include "Inliner.h" |
| 17 | #include "llvm/Module.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CallGraph.h" |
| 20 | #include "llvm/Support/CallSite.h" |
| 21 | #include "llvm/Transforms/Utils/Cloning.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | dac58ad | 2006-01-22 23:32:06 +0000 | [diff] [blame^] | 25 | #include <iostream> |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 26 | #include <set> |
Chris Lattner | a51bcb5 | 2003-11-21 21:45:31 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | Statistic<> NumInlined("inline", "Number of functions inlined"); |
Chris Lattner | 8acb249 | 2006-01-13 18:06:56 +0000 | [diff] [blame] | 31 | Statistic<> NumDeleted("inline", |
| 32 | "Number of functions deleted because all callers found"); |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 33 | cl::opt<unsigned> // FIXME: 200 is VERY conservative |
| 34 | InlineLimit("inline-threshold", cl::Hidden, cl::init(200), |
Chris Lattner | 8acb249 | 2006-01-13 18:06:56 +0000 | [diff] [blame] | 35 | cl::desc("Control the amount of inlining to perform (default = 200)")); |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | Inliner::Inliner() : InlineThreshold(InlineLimit) {} |
| 39 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 40 | // InlineCallIfPossible - If it is possible to inline the specified call site, |
| 41 | // do so and update the CallGraph for this operation. |
| 42 | static bool InlineCallIfPossible(CallSite CS, CallGraph &CG, |
| 43 | const std::set<Function*> &SCCFunctions) { |
| 44 | Function *Caller = CS.getInstruction()->getParent()->getParent(); |
| 45 | Function *Callee = CS.getCalledFunction(); |
Chris Lattner | 432a205 | 2006-01-14 20:09:18 +0000 | [diff] [blame] | 46 | if (!InlineFunction(CS, &CG)) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 54970c0 | 2004-08-08 03:29:50 +0000 | [diff] [blame] | 48 | // If we inlined the last possible call site to the function, delete the |
| 49 | // function body now. |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 50 | if (Callee->use_empty() && Callee->hasInternalLinkage() && |
| 51 | !SCCFunctions.count(Callee)) { |
| 52 | DEBUG(std::cerr << " -> Deleting dead function: " |
| 53 | << Callee->getName() << "\n"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 54 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 55 | // Remove any call graph edges from the callee to its callees. |
Chris Lattner | 432a205 | 2006-01-14 20:09:18 +0000 | [diff] [blame] | 56 | CallGraphNode *CalleeNode = CG[Callee]; |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 57 | while (CalleeNode->begin() != CalleeNode->end()) |
| 58 | CalleeNode->removeCallEdgeTo(*(CalleeNode->end()-1)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 59 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 60 | // Removing the node for callee from the call graph and delete it. |
| 61 | delete CG.removeFunctionFromModule(CalleeNode); |
| 62 | ++NumDeleted; |
| 63 | } |
| 64 | return true; |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) { |
| 68 | CallGraph &CG = getAnalysis<CallGraph>(); |
| 69 | |
| 70 | std::set<Function*> SCCFunctions; |
| 71 | DEBUG(std::cerr << "Inliner visiting SCC:"); |
| 72 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 73 | Function *F = SCC[i]->getFunction(); |
| 74 | if (F) SCCFunctions.insert(F); |
| 75 | DEBUG(std::cerr << " " << (F ? F->getName() : "INDIRECTNODE")); |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 76 | } |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 77 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 78 | // Scan through and identify all call sites ahead of time so that we only |
| 79 | // inline call sites in the original functions, not call sites that result |
| 80 | // from inlining other functions. |
| 81 | std::vector<CallSite> CallSites; |
| 82 | |
Chris Lattner | cf5933a | 2004-06-20 04:11:48 +0000 | [diff] [blame] | 83 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) |
| 84 | if (Function *F = SCC[i]->getFunction()) |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 85 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 86 | for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { |
| 87 | CallSite CS = CallSite::get(I); |
| 88 | if (CS.getInstruction() && (!CS.getCalledFunction() || |
| 89 | !CS.getCalledFunction()->isExternal())) |
| 90 | CallSites.push_back(CS); |
| 91 | } |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 92 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 93 | DEBUG(std::cerr << ": " << CallSites.size() << " call sites.\n"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 94 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 95 | // Now that we have all of the call sites, move the ones to functions in the |
| 96 | // current SCC to the end of the list. |
| 97 | unsigned FirstCallInSCC = CallSites.size(); |
| 98 | for (unsigned i = 0; i < FirstCallInSCC; ++i) |
| 99 | if (Function *F = CallSites[i].getCalledFunction()) |
| 100 | if (SCCFunctions.count(F)) |
| 101 | std::swap(CallSites[i--], CallSites[--FirstCallInSCC]); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 102 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 103 | // Now that we have all of the call sites, loop over them and inline them if |
| 104 | // it looks profitable to do so. |
| 105 | bool Changed = false; |
| 106 | bool LocalChange; |
| 107 | do { |
| 108 | LocalChange = false; |
| 109 | // Iterate over the outer loop because inlining functions can cause indirect |
| 110 | // calls to become direct calls. |
| 111 | for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) |
| 112 | if (Function *Callee = CallSites[CSi].getCalledFunction()) { |
| 113 | // Calls to external functions are never inlinable. |
| 114 | if (Callee->isExternal() || |
| 115 | CallSites[CSi].getInstruction()->getParent()->getParent() ==Callee){ |
| 116 | std::swap(CallSites[CSi], CallSites.back()); |
Chris Lattner | b11a99b | 2004-05-24 06:24:46 +0000 | [diff] [blame] | 117 | CallSites.pop_back(); |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 118 | --CSi; |
| 119 | continue; |
| 120 | } |
Chris Lattner | 775cbdd | 2004-04-08 06:34:31 +0000 | [diff] [blame] | 121 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 122 | // If the policy determines that we should inline this function, |
| 123 | // try to do so. |
| 124 | CallSite CS = CallSites[CSi]; |
| 125 | int InlineCost = getInlineCost(CS); |
| 126 | if (InlineCost >= (int)InlineThreshold) { |
| 127 | DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost |
| 128 | << ", Call: " << *CS.getInstruction()); |
| 129 | } else { |
| 130 | DEBUG(std::cerr << " Inlining: cost=" << InlineCost |
| 131 | << ", Call: " << *CS.getInstruction()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 132 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 133 | Function *Caller = CS.getInstruction()->getParent()->getParent(); |
Chris Lattner | 775cbdd | 2004-04-08 06:34:31 +0000 | [diff] [blame] | 134 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 135 | // Attempt to inline the function... |
| 136 | if (InlineCallIfPossible(CS, CG, SCCFunctions)) { |
| 137 | // Remove this call site from the list. |
| 138 | std::swap(CallSites[CSi], CallSites.back()); |
| 139 | CallSites.pop_back(); |
| 140 | --CSi; |
Chris Lattner | ce1a3a2 | 2004-04-12 05:37:29 +0000 | [diff] [blame] | 141 | |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 142 | ++NumInlined; |
| 143 | Changed = true; |
| 144 | LocalChange = true; |
Chris Lattner | 775cbdd | 2004-04-08 06:34:31 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | 775cbdd | 2004-04-08 06:34:31 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
Chris Lattner | befa499 | 2004-05-23 21:22:17 +0000 | [diff] [blame] | 148 | } while (LocalChange); |
Chris Lattner | 775cbdd | 2004-04-08 06:34:31 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 237ef56 | 2003-08-31 19:10:30 +0000 | [diff] [blame] | 150 | return Changed; |
| 151 | } |
| 152 | |
Chris Lattner | 68d57e7 | 2004-04-20 22:06:53 +0000 | [diff] [blame] | 153 | // doFinalization - Remove now-dead linkonce functions at the end of |
| 154 | // processing to avoid breaking the SCC traversal. |
| 155 | bool Inliner::doFinalization(CallGraph &CG) { |
Chris Lattner | 3e1358a | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 156 | std::set<CallGraphNode*> FunctionsToRemove; |
| 157 | |
| 158 | // Scan for all of the functions, looking for ones that should now be removed |
| 159 | // from the program. Insert the dead ones in the FunctionsToRemove set. |
| 160 | for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) { |
| 161 | CallGraphNode *CGN = I->second; |
Chris Lattner | 54970c0 | 2004-08-08 03:29:50 +0000 | [diff] [blame] | 162 | if (Function *F = CGN ? CGN->getFunction() : 0) { |
Chris Lattner | 0c0aa71 | 2004-09-18 21:37:03 +0000 | [diff] [blame] | 163 | // If the only remaining users of the function are dead constants, remove |
| 164 | // them. |
Chris Lattner | 54970c0 | 2004-08-08 03:29:50 +0000 | [diff] [blame] | 165 | F->removeDeadConstantUsers(); |
Chris Lattner | 3e1358a | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 54970c0 | 2004-08-08 03:29:50 +0000 | [diff] [blame] | 167 | if ((F->hasLinkOnceLinkage() || F->hasInternalLinkage()) && |
| 168 | F->use_empty()) { |
Chris Lattner | 0c0aa71 | 2004-09-18 21:37:03 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 54970c0 | 2004-08-08 03:29:50 +0000 | [diff] [blame] | 170 | // Remove any call graph edges from the function to its callees. |
| 171 | while (CGN->begin() != CGN->end()) |
| 172 | CGN->removeCallEdgeTo(*(CGN->end()-1)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 0c0aa71 | 2004-09-18 21:37:03 +0000 | [diff] [blame] | 174 | // Remove any edges from the external node to the function's call graph |
| 175 | // node. These edges might have been made irrelegant due to |
| 176 | // optimization of the program. |
| 177 | CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 54970c0 | 2004-08-08 03:29:50 +0000 | [diff] [blame] | 179 | // Removing the node for callee from the call graph and delete it. |
| 180 | FunctionsToRemove.insert(CGN); |
| 181 | } |
Chris Lattner | 68d57e7 | 2004-04-20 22:06:53 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Chris Lattner | 3e1358a | 2004-04-21 20:44:33 +0000 | [diff] [blame] | 184 | |
| 185 | // Now that we know which functions to delete, do so. We didn't want to do |
| 186 | // this inline, because that would invalidate our CallGraph::iterator |
| 187 | // objects. :( |
| 188 | bool Changed = false; |
| 189 | for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(), |
| 190 | E = FunctionsToRemove.end(); I != E; ++I) { |
| 191 | delete CG.removeFunctionFromModule(*I); |
| 192 | ++NumDeleted; |
| 193 | Changed = true; |
| 194 | } |
| 195 | |
Chris Lattner | 68d57e7 | 2004-04-20 22:06:53 +0000 | [diff] [blame] | 196 | return Changed; |
| 197 | } |