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