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