LLVM API Documentation
00001 //===- Inliner.cpp - Code common to all inliners --------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the mechanics required to implement inlining without 00011 // missing any calls and updating the call graph. The decisions of which calls 00012 // are profitable to inline are implemented elsewhere. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #define DEBUG_TYPE "inline" 00017 #include "llvm/Module.h" 00018 #include "llvm/Instructions.h" 00019 #include "llvm/Analysis/CallGraph.h" 00020 #include "llvm/Support/CallSite.h" 00021 #include "llvm/Target/TargetData.h" 00022 #include "llvm/Transforms/IPO/InlinerPass.h" 00023 #include "llvm/Transforms/Utils/Cloning.h" 00024 #include "llvm/Support/CommandLine.h" 00025 #include "llvm/Support/Debug.h" 00026 #include "llvm/ADT/Statistic.h" 00027 #include <set> 00028 using namespace llvm; 00029 00030 STATISTIC(NumInlined, "Number of functions inlined"); 00031 STATISTIC(NumDeleted, "Number of functions deleted because all callers found"); 00032 00033 static cl::opt<int> 00034 InlineLimit("inline-threshold", cl::Hidden, cl::init(200), 00035 cl::desc("Control the amount of inlining to perform (default = 200)")); 00036 00037 Inliner::Inliner(void *ID) 00038 : CallGraphSCCPass(ID), InlineThreshold(InlineLimit) {} 00039 00040 Inliner::Inliner(void *ID, int Threshold) 00041 : CallGraphSCCPass(ID), InlineThreshold(Threshold) {} 00042 00043 /// getAnalysisUsage - For this class, we declare that we require and preserve 00044 /// the call graph. If the derived class implements this method, it should 00045 /// always explicitly call the implementation here. 00046 void Inliner::getAnalysisUsage(AnalysisUsage &Info) const { 00047 Info.addRequired<TargetData>(); 00048 CallGraphSCCPass::getAnalysisUsage(Info); 00049 } 00050 00051 // InlineCallIfPossible - If it is possible to inline the specified call site, 00052 // do so and update the CallGraph for this operation. 00053 static bool InlineCallIfPossible(CallSite CS, CallGraph &CG, 00054 const std::set<Function*> &SCCFunctions, 00055 const TargetData &TD) { 00056 Function *Callee = CS.getCalledFunction(); 00057 Function *Caller = CS.getCaller(); 00058 00059 if (!InlineFunction(CS, &CG, &TD)) return false; 00060 00061 // If the inlined function had a higher stack protection level than the 00062 // calling function, then bump up the caller's stack protection level. 00063 if (Callee->hasFnAttr(Attribute::StackProtectReq)) 00064 Caller->addFnAttr(Attribute::StackProtectReq); 00065 else if (Callee->hasFnAttr(Attribute::StackProtect) && 00066 !Caller->hasFnAttr(Attribute::StackProtectReq)) 00067 Caller->addFnAttr(Attribute::StackProtect); 00068 00069 // If we inlined the last possible call site to the function, delete the 00070 // function body now. 00071 if (Callee->use_empty() && Callee->hasLocalLinkage() && 00072 !SCCFunctions.count(Callee)) { 00073 DOUT << " -> Deleting dead function: " << Callee->getName() << "\n"; 00074 CallGraphNode *CalleeNode = CG[Callee]; 00075 00076 // Remove any call graph edges from the callee to its callees. 00077 CalleeNode->removeAllCalledFunctions(); 00078 00079 // Removing the node for callee from the call graph and delete it. 00080 delete CG.removeFunctionFromModule(CalleeNode); 00081 ++NumDeleted; 00082 } 00083 return true; 00084 } 00085 00086 /// shouldInline - Return true if the inliner should attempt to inline 00087 /// at the given CallSite. 00088 bool Inliner::shouldInline(CallSite CS) { 00089 InlineCost IC = getInlineCost(CS); 00090 float FudgeFactor = getInlineFudgeFactor(CS); 00091 00092 if (IC.isAlways()) { 00093 DOUT << " Inlining: cost=always" 00094 << ", Call: " << *CS.getInstruction(); 00095 return true; 00096 } 00097 00098 if (IC.isNever()) { 00099 DOUT << " NOT Inlining: cost=never" 00100 << ", Call: " << *CS.getInstruction(); 00101 return false; 00102 } 00103 00104 int Cost = IC.getValue(); 00105 int CurrentThreshold = InlineThreshold; 00106 Function *Fn = CS.getCaller(); 00107 if (Fn && !Fn->isDeclaration() 00108 && Fn->hasFnAttr(Attribute::OptimizeForSize) 00109 && InlineThreshold != 50) { 00110 CurrentThreshold = 50; 00111 } 00112 00113 if (Cost >= (int)(CurrentThreshold * FudgeFactor)) { 00114 DOUT << " NOT Inlining: cost=" << Cost 00115 << ", Call: " << *CS.getInstruction(); 00116 return false; 00117 } else { 00118 DOUT << " Inlining: cost=" << Cost 00119 << ", Call: " << *CS.getInstruction(); 00120 return true; 00121 } 00122 } 00123 00124 bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) { 00125 CallGraph &CG = getAnalysis<CallGraph>(); 00126 00127 std::set<Function*> SCCFunctions; 00128 DOUT << "Inliner visiting SCC:"; 00129 for (unsigned i = 0, e = SCC.size(); i != e; ++i) { 00130 Function *F = SCC[i]->getFunction(); 00131 if (F) SCCFunctions.insert(F); 00132 DOUT << " " << (F ? F->getName() : "INDIRECTNODE"); 00133 } 00134 00135 // Scan through and identify all call sites ahead of time so that we only 00136 // inline call sites in the original functions, not call sites that result 00137 // from inlining other functions. 00138 std::vector<CallSite> CallSites; 00139 00140 for (unsigned i = 0, e = SCC.size(); i != e; ++i) 00141 if (Function *F = SCC[i]->getFunction()) 00142 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 00143 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { 00144 CallSite CS = CallSite::get(I); 00145 if (CS.getInstruction() && (!CS.getCalledFunction() || 00146 !CS.getCalledFunction()->isDeclaration())) 00147 CallSites.push_back(CS); 00148 } 00149 00150 DOUT << ": " << CallSites.size() << " call sites.\n"; 00151 00152 // Now that we have all of the call sites, move the ones to functions in the 00153 // current SCC to the end of the list. 00154 unsigned FirstCallInSCC = CallSites.size(); 00155 for (unsigned i = 0; i < FirstCallInSCC; ++i) 00156 if (Function *F = CallSites[i].getCalledFunction()) 00157 if (SCCFunctions.count(F)) 00158 std::swap(CallSites[i--], CallSites[--FirstCallInSCC]); 00159 00160 // Now that we have all of the call sites, loop over them and inline them if 00161 // it looks profitable to do so. 00162 bool Changed = false; 00163 bool LocalChange; 00164 do { 00165 LocalChange = false; 00166 // Iterate over the outer loop because inlining functions can cause indirect 00167 // calls to become direct calls. 00168 for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) 00169 if (Function *Callee = CallSites[CSi].getCalledFunction()) { 00170 // Calls to external functions are never inlinable. 00171 if (Callee->isDeclaration()) { 00172 if (SCC.size() == 1) { 00173 std::swap(CallSites[CSi], CallSites.back()); 00174 CallSites.pop_back(); 00175 } else { 00176 // Keep the 'in SCC / not in SCC' boundary correct. 00177 CallSites.erase(CallSites.begin()+CSi); 00178 } 00179 --CSi; 00180 continue; 00181 } 00182 00183 // If the policy determines that we should inline this function, 00184 // try to do so. 00185 CallSite CS = CallSites[CSi]; 00186 if (shouldInline(CS)) { 00187 Function *Caller = CS.getCaller(); 00188 // Attempt to inline the function... 00189 if (InlineCallIfPossible(CS, CG, SCCFunctions, 00190 getAnalysis<TargetData>())) { 00191 // Remove any cached cost info for this caller, as inlining the callee 00192 // has increased the size of the caller (which may be the same as the 00193 // callee). 00194 resetCachedCostInfo(Caller); 00195 00196 // Remove this call site from the list. If possible, use 00197 // swap/pop_back for efficiency, but do not use it if doing so would 00198 // move a call site to a function in this SCC before the 00199 // 'FirstCallInSCC' barrier. 00200 if (SCC.size() == 1) { 00201 std::swap(CallSites[CSi], CallSites.back()); 00202 CallSites.pop_back(); 00203 } else { 00204 CallSites.erase(CallSites.begin()+CSi); 00205 } 00206 --CSi; 00207 00208 ++NumInlined; 00209 Changed = true; 00210 LocalChange = true; 00211 } 00212 } 00213 } 00214 } while (LocalChange); 00215 00216 return Changed; 00217 } 00218 00219 // doFinalization - Remove now-dead linkonce functions at the end of 00220 // processing to avoid breaking the SCC traversal. 00221 bool Inliner::doFinalization(CallGraph &CG) { 00222 return removeDeadFunctions(CG); 00223 } 00224 00225 /// removeDeadFunctions - Remove dead functions that are not included in 00226 /// DNR (Do Not Remove) list. 00227 bool Inliner::removeDeadFunctions(CallGraph &CG, 00228 SmallPtrSet<const Function *, 16> *DNR) { 00229 std::set<CallGraphNode*> FunctionsToRemove; 00230 00231 // Scan for all of the functions, looking for ones that should now be removed 00232 // from the program. Insert the dead ones in the FunctionsToRemove set. 00233 for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) { 00234 CallGraphNode *CGN = I->second; 00235 if (Function *F = CGN ? CGN->getFunction() : 0) { 00236 // If the only remaining users of the function are dead constants, remove 00237 // them. 00238 F->removeDeadConstantUsers(); 00239 00240 if (DNR && DNR->count(F)) 00241 continue; 00242 00243 if ((F->hasLinkOnceLinkage() || F->hasLocalLinkage()) && 00244 F->use_empty()) { 00245 00246 // Remove any call graph edges from the function to its callees. 00247 CGN->removeAllCalledFunctions(); 00248 00249 // Remove any edges from the external node to the function's call graph 00250 // node. These edges might have been made irrelegant due to 00251 // optimization of the program. 00252 CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN); 00253 00254 // Removing the node for callee from the call graph and delete it. 00255 FunctionsToRemove.insert(CGN); 00256 } 00257 } 00258 } 00259 00260 // Now that we know which functions to delete, do so. We didn't want to do 00261 // this inline, because that would invalidate our CallGraph::iterator 00262 // objects. :( 00263 bool Changed = false; 00264 for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(), 00265 E = FunctionsToRemove.end(); I != E; ++I) { 00266 delete CG.removeFunctionFromModule(*I); 00267 ++NumDeleted; 00268 Changed = true; 00269 } 00270 00271 return Changed; 00272 }