1193323Sed//===- Inliner.cpp - Code common to all inliners --------------------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file implements the mechanics required to implement inlining without 11193323Sed// missing any calls and updating the call graph. The decisions of which calls 12193323Sed// are profitable to inline are implemented elsewhere. 13193323Sed// 14193323Sed//===----------------------------------------------------------------------===// 15193323Sed 16193323Sed#define DEBUG_TYPE "inline" 17249423Sdim#include "llvm/Transforms/IPO/InlinerPass.h" 18249423Sdim#include "llvm/ADT/SmallPtrSet.h" 19249423Sdim#include "llvm/ADT/Statistic.h" 20193323Sed#include "llvm/Analysis/CallGraph.h" 21198090Srdivacky#include "llvm/Analysis/InlineCost.h" 22249423Sdim#include "llvm/IR/DataLayout.h" 23249423Sdim#include "llvm/IR/Instructions.h" 24249423Sdim#include "llvm/IR/IntrinsicInst.h" 25249423Sdim#include "llvm/IR/Module.h" 26199481Srdivacky#include "llvm/Support/CallSite.h" 27193323Sed#include "llvm/Support/CommandLine.h" 28193323Sed#include "llvm/Support/Debug.h" 29198090Srdivacky#include "llvm/Support/raw_ostream.h" 30249423Sdim#include "llvm/Target/TargetLibraryInfo.h" 31249423Sdim#include "llvm/Transforms/Utils/Cloning.h" 32249423Sdim#include "llvm/Transforms/Utils/Local.h" 33193323Sedusing namespace llvm; 34193323Sed 35193323SedSTATISTIC(NumInlined, "Number of functions inlined"); 36199481SrdivackySTATISTIC(NumCallsDeleted, "Number of call sites deleted, not inlined"); 37193323SedSTATISTIC(NumDeleted, "Number of functions deleted because all callers found"); 38198090SrdivackySTATISTIC(NumMergedAllocas, "Number of allocas merged together"); 39193323Sed 40239462Sdim// This weirdly named statistic tracks the number of times that, when attempting 41234353Sdim// to inline a function A into B, we analyze the callers of B in order to see 42234353Sdim// if those would be more profitable and blocked inline steps. 43234353SdimSTATISTIC(NumCallerCallersAnalyzed, "Number of caller-callers analyzed"); 44234353Sdim 45193323Sedstatic cl::opt<int> 46203954SrdivackyInlineLimit("inline-threshold", cl::Hidden, cl::init(225), cl::ZeroOrMore, 47203954Srdivacky cl::desc("Control the amount of inlining to perform (default = 225)")); 48193323Sed 49203954Srdivackystatic cl::opt<int> 50203954SrdivackyHintThreshold("inlinehint-threshold", cl::Hidden, cl::init(325), 51203954Srdivacky cl::desc("Threshold for inlining functions with inline hint")); 52203954Srdivacky 53203954Srdivacky// Threshold to use when optsize is specified (and there is no -inline-limit). 54203954Srdivackyconst int OptSizeThreshold = 75; 55203954Srdivacky 56212904SdimInliner::Inliner(char &ID) 57234353Sdim : CallGraphSCCPass(ID), InlineThreshold(InlineLimit), InsertLifetime(true) {} 58193323Sed 59234353SdimInliner::Inliner(char &ID, int Threshold, bool InsertLifetime) 60218893Sdim : CallGraphSCCPass(ID), InlineThreshold(InlineLimit.getNumOccurrences() > 0 ? 61234353Sdim InlineLimit : Threshold), 62234353Sdim InsertLifetime(InsertLifetime) {} 63193323Sed 64193323Sed/// getAnalysisUsage - For this class, we declare that we require and preserve 65193323Sed/// the call graph. If the derived class implements this method, it should 66193323Sed/// always explicitly call the implementation here. 67249423Sdimvoid Inliner::getAnalysisUsage(AnalysisUsage &AU) const { 68249423Sdim CallGraphSCCPass::getAnalysisUsage(AU); 69193323Sed} 70193323Sed 71198090Srdivacky 72226633Sdimtypedef DenseMap<ArrayType*, std::vector<AllocaInst*> > 73198090SrdivackyInlinedArrayAllocasTy; 74198090Srdivacky 75249423Sdim/// \brief If the inlined function had a higher stack protection level than the 76249423Sdim/// calling function, then bump up the caller's stack protection level. 77249423Sdimstatic void AdjustCallerSSPLevel(Function *Caller, Function *Callee) { 78249423Sdim // If upgrading the SSP attribute, clear out the old SSP Attributes first. 79249423Sdim // Having multiple SSP attributes doesn't actually hurt, but it adds useless 80249423Sdim // clutter to the IR. 81249423Sdim AttrBuilder B; 82249423Sdim B.addAttribute(Attribute::StackProtect) 83249423Sdim .addAttribute(Attribute::StackProtectStrong); 84249423Sdim AttributeSet OldSSPAttr = AttributeSet::get(Caller->getContext(), 85249423Sdim AttributeSet::FunctionIndex, 86249423Sdim B); 87249423Sdim AttributeSet CallerAttr = Caller->getAttributes(), 88249423Sdim CalleeAttr = Callee->getAttributes(); 89249423Sdim 90249423Sdim if (CalleeAttr.hasAttribute(AttributeSet::FunctionIndex, 91249423Sdim Attribute::StackProtectReq)) { 92249423Sdim Caller->removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr); 93249423Sdim Caller->addFnAttr(Attribute::StackProtectReq); 94249423Sdim } else if (CalleeAttr.hasAttribute(AttributeSet::FunctionIndex, 95249423Sdim Attribute::StackProtectStrong) && 96249423Sdim !CallerAttr.hasAttribute(AttributeSet::FunctionIndex, 97249423Sdim Attribute::StackProtectReq)) { 98249423Sdim Caller->removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr); 99249423Sdim Caller->addFnAttr(Attribute::StackProtectStrong); 100249423Sdim } else if (CalleeAttr.hasAttribute(AttributeSet::FunctionIndex, 101249423Sdim Attribute::StackProtect) && 102249423Sdim !CallerAttr.hasAttribute(AttributeSet::FunctionIndex, 103249423Sdim Attribute::StackProtectReq) && 104249423Sdim !CallerAttr.hasAttribute(AttributeSet::FunctionIndex, 105249423Sdim Attribute::StackProtectStrong)) 106249423Sdim Caller->addFnAttr(Attribute::StackProtect); 107249423Sdim} 108249423Sdim 109198090Srdivacky/// InlineCallIfPossible - If it is possible to inline the specified call site, 110198090Srdivacky/// do so and update the CallGraph for this operation. 111198090Srdivacky/// 112198090Srdivacky/// This function also does some basic book-keeping to update the IR. The 113198090Srdivacky/// InlinedArrayAllocas map keeps track of any allocas that are already 114198090Srdivacky/// available from other functions inlined into the caller. If we are able to 115198090Srdivacky/// inline this call site we attempt to reuse already available allocas or add 116198090Srdivacky/// any new allocas to the set if not possible. 117207618Srdivackystatic bool InlineCallIfPossible(CallSite CS, InlineFunctionInfo &IFI, 118218893Sdim InlinedArrayAllocasTy &InlinedArrayAllocas, 119263508Sdim int InlineHistory, bool InsertLifetime, 120263508Sdim const DataLayout *TD) { 121193323Sed Function *Callee = CS.getCalledFunction(); 122193323Sed Function *Caller = CS.getCaller(); 123193323Sed 124198090Srdivacky // Try to inline the function. Get the list of static allocas that were 125198090Srdivacky // inlined. 126234353Sdim if (!InlineFunction(CS, IFI, InsertLifetime)) 127198090Srdivacky return false; 128193323Sed 129249423Sdim AdjustCallerSSPLevel(Caller, Callee); 130193323Sed 131198090Srdivacky // Look at all of the allocas that we inlined through this call site. If we 132198090Srdivacky // have already inlined other allocas through other calls into this function, 133198090Srdivacky // then we know that they have disjoint lifetimes and that we can merge them. 134198090Srdivacky // 135198090Srdivacky // There are many heuristics possible for merging these allocas, and the 136198090Srdivacky // different options have different tradeoffs. One thing that we *really* 137198090Srdivacky // don't want to hurt is SRoA: once inlining happens, often allocas are no 138198090Srdivacky // longer address taken and so they can be promoted. 139198090Srdivacky // 140198090Srdivacky // Our "solution" for that is to only merge allocas whose outermost type is an 141198090Srdivacky // array type. These are usually not promoted because someone is using a 142198090Srdivacky // variable index into them. These are also often the most important ones to 143198090Srdivacky // merge. 144198090Srdivacky // 145198090Srdivacky // A better solution would be to have real memory lifetime markers in the IR 146198090Srdivacky // and not have the inliner do any merging of allocas at all. This would 147198090Srdivacky // allow the backend to do proper stack slot coloring of all allocas that 148198090Srdivacky // *actually make it to the backend*, which is really what we want. 149198090Srdivacky // 150198090Srdivacky // Because we don't have this information, we do this simple and useful hack. 151198090Srdivacky // 152198090Srdivacky SmallPtrSet<AllocaInst*, 16> UsedAllocas; 153198090Srdivacky 154218893Sdim // When processing our SCC, check to see if CS was inlined from some other 155218893Sdim // call site. For example, if we're processing "A" in this code: 156218893Sdim // A() { B() } 157218893Sdim // B() { x = alloca ... C() } 158218893Sdim // C() { y = alloca ... } 159218893Sdim // Assume that C was not inlined into B initially, and so we're processing A 160218893Sdim // and decide to inline B into A. Doing this makes an alloca available for 161218893Sdim // reuse and makes a callsite (C) available for inlining. When we process 162218893Sdim // the C call site we don't want to do any alloca merging between X and Y 163218893Sdim // because their scopes are not disjoint. We could make this smarter by 164218893Sdim // keeping track of the inline history for each alloca in the 165218893Sdim // InlinedArrayAllocas but this isn't likely to be a significant win. 166218893Sdim if (InlineHistory != -1) // Only do merging for top-level call sites in SCC. 167218893Sdim return true; 168218893Sdim 169198090Srdivacky // Loop over all the allocas we have so far and see if they can be merged with 170198090Srdivacky // a previously inlined alloca. If not, remember that we had it. 171207618Srdivacky for (unsigned AllocaNo = 0, e = IFI.StaticAllocas.size(); 172198090Srdivacky AllocaNo != e; ++AllocaNo) { 173207618Srdivacky AllocaInst *AI = IFI.StaticAllocas[AllocaNo]; 174198090Srdivacky 175198090Srdivacky // Don't bother trying to merge array allocations (they will usually be 176198090Srdivacky // canonicalized to be an allocation *of* an array), or allocations whose 177198090Srdivacky // type is not itself an array (because we're afraid of pessimizing SRoA). 178226633Sdim ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType()); 179198090Srdivacky if (ATy == 0 || AI->isArrayAllocation()) 180198090Srdivacky continue; 181198090Srdivacky 182198090Srdivacky // Get the list of all available allocas for this array type. 183198090Srdivacky std::vector<AllocaInst*> &AllocasForType = InlinedArrayAllocas[ATy]; 184198090Srdivacky 185198090Srdivacky // Loop over the allocas in AllocasForType to see if we can reuse one. Note 186198090Srdivacky // that we have to be careful not to reuse the same "available" alloca for 187198090Srdivacky // multiple different allocas that we just inlined, we use the 'UsedAllocas' 188198090Srdivacky // set to keep track of which "available" allocas are being used by this 189198090Srdivacky // function. Also, AllocasForType can be empty of course! 190198090Srdivacky bool MergedAwayAlloca = false; 191198090Srdivacky for (unsigned i = 0, e = AllocasForType.size(); i != e; ++i) { 192198090Srdivacky AllocaInst *AvailableAlloca = AllocasForType[i]; 193263508Sdim 194263508Sdim unsigned Align1 = AI->getAlignment(), 195263508Sdim Align2 = AvailableAlloca->getAlignment(); 196263508Sdim // If we don't have data layout information, and only one alloca is using 197263508Sdim // the target default, then we can't safely merge them because we can't 198263508Sdim // pick the greater alignment. 199263508Sdim if (!TD && (!Align1 || !Align2) && Align1 != Align2) 200263508Sdim continue; 201198090Srdivacky 202198090Srdivacky // The available alloca has to be in the right function, not in some other 203198090Srdivacky // function in this SCC. 204198090Srdivacky if (AvailableAlloca->getParent() != AI->getParent()) 205198090Srdivacky continue; 206198090Srdivacky 207198090Srdivacky // If the inlined function already uses this alloca then we can't reuse 208198090Srdivacky // it. 209198090Srdivacky if (!UsedAllocas.insert(AvailableAlloca)) 210198090Srdivacky continue; 211198090Srdivacky 212198090Srdivacky // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare 213198090Srdivacky // success! 214218893Sdim DEBUG(dbgs() << " ***MERGED ALLOCA: " << *AI << "\n\t\tINTO: " 215218893Sdim << *AvailableAlloca << '\n'); 216198090Srdivacky 217198090Srdivacky AI->replaceAllUsesWith(AvailableAlloca); 218263508Sdim 219263508Sdim if (Align1 != Align2) { 220263508Sdim if (!Align1 || !Align2) { 221263508Sdim assert(TD && "DataLayout required to compare default alignments"); 222263508Sdim unsigned TypeAlign = TD->getABITypeAlignment(AI->getAllocatedType()); 223263508Sdim 224263508Sdim Align1 = Align1 ? Align1 : TypeAlign; 225263508Sdim Align2 = Align2 ? Align2 : TypeAlign; 226263508Sdim } 227263508Sdim 228263508Sdim if (Align1 > Align2) 229263508Sdim AvailableAlloca->setAlignment(AI->getAlignment()); 230263508Sdim } 231263508Sdim 232198090Srdivacky AI->eraseFromParent(); 233198090Srdivacky MergedAwayAlloca = true; 234198090Srdivacky ++NumMergedAllocas; 235218893Sdim IFI.StaticAllocas[AllocaNo] = 0; 236198090Srdivacky break; 237198090Srdivacky } 238193323Sed 239198090Srdivacky // If we already nuked the alloca, we're done with it. 240198090Srdivacky if (MergedAwayAlloca) 241198090Srdivacky continue; 242218893Sdim 243198090Srdivacky // If we were unable to merge away the alloca either because there are no 244198090Srdivacky // allocas of the right type available or because we reused them all 245198090Srdivacky // already, remember that this alloca came from an inlined function and mark 246198090Srdivacky // it used so we don't reuse it for other allocas from this inline 247198090Srdivacky // operation. 248198090Srdivacky AllocasForType.push_back(AI); 249198090Srdivacky UsedAllocas.insert(AI); 250193323Sed } 251198090Srdivacky 252193323Sed return true; 253193323Sed} 254202878Srdivacky 255203954Srdivackyunsigned Inliner::getInlineThreshold(CallSite CS) const { 256239462Sdim int thres = InlineThreshold; // -inline-threshold or else selected by 257239462Sdim // overall opt level 258203954Srdivacky 259239462Sdim // If -inline-threshold is not given, listen to the optsize attribute when it 260239462Sdim // would decrease the threshold. 261203954Srdivacky Function *Caller = CS.getCaller(); 262239462Sdim bool OptSize = Caller && !Caller->isDeclaration() && 263249423Sdim Caller->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 264249423Sdim Attribute::OptimizeForSize); 265243830Sdim if (!(InlineLimit.getNumOccurrences() > 0) && OptSize && 266243830Sdim OptSizeThreshold < thres) 267203954Srdivacky thres = OptSizeThreshold; 268203954Srdivacky 269249423Sdim // Listen to the inlinehint attribute when it would increase the threshold 270249423Sdim // and the caller does not need to minimize its size. 271203954Srdivacky Function *Callee = CS.getCalledFunction(); 272239462Sdim bool InlineHint = Callee && !Callee->isDeclaration() && 273249423Sdim Callee->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 274249423Sdim Attribute::InlineHint); 275249423Sdim if (InlineHint && HintThreshold > thres 276249423Sdim && !Caller->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 277249423Sdim Attribute::MinSize)) 278203954Srdivacky thres = HintThreshold; 279203954Srdivacky 280203954Srdivacky return thres; 281202878Srdivacky} 282202878Srdivacky 283193323Sed/// shouldInline - Return true if the inliner should attempt to inline 284193323Sed/// at the given CallSite. 285193323Sedbool Inliner::shouldInline(CallSite CS) { 286193323Sed InlineCost IC = getInlineCost(CS); 287193323Sed 288193323Sed if (IC.isAlways()) { 289202375Srdivacky DEBUG(dbgs() << " Inlining: cost=always" 290198090Srdivacky << ", Call: " << *CS.getInstruction() << "\n"); 291193323Sed return true; 292193323Sed } 293193323Sed 294193323Sed if (IC.isNever()) { 295202375Srdivacky DEBUG(dbgs() << " NOT Inlining: cost=never" 296198090Srdivacky << ", Call: " << *CS.getInstruction() << "\n"); 297193323Sed return false; 298193323Sed } 299193323Sed 300198090Srdivacky Function *Caller = CS.getCaller(); 301234353Sdim if (!IC) { 302234353Sdim DEBUG(dbgs() << " NOT Inlining: cost=" << IC.getCost() 303234353Sdim << ", thres=" << (IC.getCostDelta() + IC.getCost()) 304198090Srdivacky << ", Call: " << *CS.getInstruction() << "\n"); 305193323Sed return false; 306193323Sed } 307198090Srdivacky 308234353Sdim // Try to detect the case where the current inlining candidate caller (call 309234353Sdim // it B) is a static or linkonce-ODR function and is an inlining candidate 310234353Sdim // elsewhere, and the current candidate callee (call it C) is large enough 311234353Sdim // that inlining it into B would make B too big to inline later. In these 312234353Sdim // circumstances it may be best not to inline C into B, but to inline B into 313234353Sdim // its callers. 314234353Sdim // 315234353Sdim // This only applies to static and linkonce-ODR functions because those are 316234353Sdim // expected to be available for inlining in the translation units where they 317234353Sdim // are used. Thus we will always have the opportunity to make local inlining 318234353Sdim // decisions. Importantly the linkonce-ODR linkage covers inline functions 319234353Sdim // and templates in C++. 320234353Sdim // 321234353Sdim // FIXME: All of this logic should be sunk into getInlineCost. It relies on 322234353Sdim // the internal implementation of the inline cost metrics rather than 323234353Sdim // treating them as truly abstract units etc. 324234353Sdim if (Caller->hasLocalLinkage() || 325234353Sdim Caller->getLinkage() == GlobalValue::LinkOnceODRLinkage) { 326198090Srdivacky int TotalSecondaryCost = 0; 327234353Sdim // The candidate cost to be imposed upon the current function. 328234353Sdim int CandidateCost = IC.getCost() - (InlineConstants::CallPenalty + 1); 329218893Sdim // This bool tracks what happens if we do NOT inline C into B. 330234353Sdim bool callerWillBeRemoved = Caller->hasLocalLinkage(); 331218893Sdim // This bool tracks what happens if we DO inline C into B. 332218893Sdim bool inliningPreventsSomeOuterInline = false; 333198090Srdivacky for (Value::use_iterator I = Caller->use_begin(), E =Caller->use_end(); 334198090Srdivacky I != E; ++I) { 335212904Sdim CallSite CS2(*I); 336198090Srdivacky 337198090Srdivacky // If this isn't a call to Caller (it could be some other sort 338218893Sdim // of reference) skip it. Such references will prevent the caller 339218893Sdim // from being removed. 340218893Sdim if (!CS2 || CS2.getCalledFunction() != Caller) { 341218893Sdim callerWillBeRemoved = false; 342198090Srdivacky continue; 343218893Sdim } 344198090Srdivacky 345198090Srdivacky InlineCost IC2 = getInlineCost(CS2); 346234353Sdim ++NumCallerCallersAnalyzed; 347234353Sdim if (!IC2) { 348218893Sdim callerWillBeRemoved = false; 349198090Srdivacky continue; 350234353Sdim } 351234353Sdim if (IC2.isAlways()) 352234353Sdim continue; 353198090Srdivacky 354234353Sdim // See if inlining or original callsite would erase the cost delta of 355234353Sdim // this callsite. We subtract off the penalty for the call instruction, 356234353Sdim // which we would be deleting. 357234353Sdim if (IC2.getCostDelta() <= CandidateCost) { 358218893Sdim inliningPreventsSomeOuterInline = true; 359234353Sdim TotalSecondaryCost += IC2.getCost(); 360198090Srdivacky } 361198090Srdivacky } 362198090Srdivacky // If all outer calls to Caller would get inlined, the cost for the last 363198090Srdivacky // one is set very low by getInlineCost, in anticipation that Caller will 364198090Srdivacky // be removed entirely. We did not account for this above unless there 365198090Srdivacky // is only one caller of Caller. 366218893Sdim if (callerWillBeRemoved && Caller->use_begin() != Caller->use_end()) 367198090Srdivacky TotalSecondaryCost += InlineConstants::LastCallToStaticBonus; 368198090Srdivacky 369234353Sdim if (inliningPreventsSomeOuterInline && TotalSecondaryCost < IC.getCost()) { 370234353Sdim DEBUG(dbgs() << " NOT Inlining: " << *CS.getInstruction() << 371234353Sdim " Cost = " << IC.getCost() << 372198090Srdivacky ", outer Cost = " << TotalSecondaryCost << '\n'); 373198090Srdivacky return false; 374198090Srdivacky } 375198090Srdivacky } 376198090Srdivacky 377234353Sdim DEBUG(dbgs() << " Inlining: cost=" << IC.getCost() 378234353Sdim << ", thres=" << (IC.getCostDelta() + IC.getCost()) 379198090Srdivacky << ", Call: " << *CS.getInstruction() << '\n'); 380198090Srdivacky return true; 381193323Sed} 382193323Sed 383207618Srdivacky/// InlineHistoryIncludes - Return true if the specified inline history ID 384207618Srdivacky/// indicates an inline history that includes the specified function. 385207618Srdivackystatic bool InlineHistoryIncludes(Function *F, int InlineHistoryID, 386207618Srdivacky const SmallVectorImpl<std::pair<Function*, int> > &InlineHistory) { 387207618Srdivacky while (InlineHistoryID != -1) { 388207618Srdivacky assert(unsigned(InlineHistoryID) < InlineHistory.size() && 389207618Srdivacky "Invalid inline history ID"); 390207618Srdivacky if (InlineHistory[InlineHistoryID].first == F) 391207618Srdivacky return true; 392207618Srdivacky InlineHistoryID = InlineHistory[InlineHistoryID].second; 393207618Srdivacky } 394207618Srdivacky return false; 395207618Srdivacky} 396207618Srdivacky 397207618Srdivackybool Inliner::runOnSCC(CallGraphSCC &SCC) { 398193323Sed CallGraph &CG = getAnalysis<CallGraph>(); 399243830Sdim const DataLayout *TD = getAnalysisIfAvailable<DataLayout>(); 400243830Sdim const TargetLibraryInfo *TLI = getAnalysisIfAvailable<TargetLibraryInfo>(); 401193323Sed 402193323Sed SmallPtrSet<Function*, 8> SCCFunctions; 403202375Srdivacky DEBUG(dbgs() << "Inliner visiting SCC:"); 404207618Srdivacky for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 405207618Srdivacky Function *F = (*I)->getFunction(); 406193323Sed if (F) SCCFunctions.insert(F); 407202375Srdivacky DEBUG(dbgs() << " " << (F ? F->getName() : "INDIRECTNODE")); 408193323Sed } 409193323Sed 410193323Sed // Scan through and identify all call sites ahead of time so that we only 411193323Sed // inline call sites in the original functions, not call sites that result 412193323Sed // from inlining other functions. 413207618Srdivacky SmallVector<std::pair<CallSite, int>, 16> CallSites; 414207618Srdivacky 415207618Srdivacky // When inlining a callee produces new call sites, we want to keep track of 416207618Srdivacky // the fact that they were inlined from the callee. This allows us to avoid 417207618Srdivacky // infinite inlining in some obscure cases. To represent this, we use an 418207618Srdivacky // index into the InlineHistory vector. 419207618Srdivacky SmallVector<std::pair<Function*, int>, 8> InlineHistory; 420193323Sed 421207618Srdivacky for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 422207618Srdivacky Function *F = (*I)->getFunction(); 423198090Srdivacky if (!F) continue; 424198090Srdivacky 425198090Srdivacky for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 426198090Srdivacky for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 427212904Sdim CallSite CS(cast<Value>(I)); 428198090Srdivacky // If this isn't a call, or it is a call to an intrinsic, it can 429198090Srdivacky // never be inlined. 430212904Sdim if (!CS || isa<IntrinsicInst>(I)) 431198090Srdivacky continue; 432198090Srdivacky 433198090Srdivacky // If this is a direct call to an external function, we can never inline 434198090Srdivacky // it. If it is an indirect call, inlining may resolve it to be a 435198090Srdivacky // direct call, so we keep it. 436198090Srdivacky if (CS.getCalledFunction() && CS.getCalledFunction()->isDeclaration()) 437198090Srdivacky continue; 438198090Srdivacky 439207618Srdivacky CallSites.push_back(std::make_pair(CS, -1)); 440198090Srdivacky } 441198090Srdivacky } 442193323Sed 443202375Srdivacky DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n"); 444193323Sed 445207618Srdivacky // If there are no calls in this function, exit early. 446207618Srdivacky if (CallSites.empty()) 447207618Srdivacky return false; 448207618Srdivacky 449193323Sed // Now that we have all of the call sites, move the ones to functions in the 450193323Sed // current SCC to the end of the list. 451193323Sed unsigned FirstCallInSCC = CallSites.size(); 452193323Sed for (unsigned i = 0; i < FirstCallInSCC; ++i) 453207618Srdivacky if (Function *F = CallSites[i].first.getCalledFunction()) 454193323Sed if (SCCFunctions.count(F)) 455193323Sed std::swap(CallSites[i--], CallSites[--FirstCallInSCC]); 456193323Sed 457198090Srdivacky 458198090Srdivacky InlinedArrayAllocasTy InlinedArrayAllocas; 459207618Srdivacky InlineFunctionInfo InlineInfo(&CG, TD); 460198090Srdivacky 461193323Sed // Now that we have all of the call sites, loop over them and inline them if 462193323Sed // it looks profitable to do so. 463193323Sed bool Changed = false; 464193323Sed bool LocalChange; 465193323Sed do { 466193323Sed LocalChange = false; 467193323Sed // Iterate over the outer loop because inlining functions can cause indirect 468193323Sed // calls to become direct calls. 469198090Srdivacky for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) { 470207618Srdivacky CallSite CS = CallSites[CSi].first; 471198090Srdivacky 472199481Srdivacky Function *Caller = CS.getCaller(); 473198090Srdivacky Function *Callee = CS.getCalledFunction(); 474199481Srdivacky 475199481Srdivacky // If this call site is dead and it is to a readonly function, we should 476199481Srdivacky // just delete the call instead of trying to inline it, regardless of 477199481Srdivacky // size. This happens because IPSCCP propagates the result out of the 478199481Srdivacky // call and then we're left with the dead call. 479243830Sdim if (isInstructionTriviallyDead(CS.getInstruction(), TLI)) { 480202375Srdivacky DEBUG(dbgs() << " -> Deleting dead call: " 481199481Srdivacky << *CS.getInstruction() << "\n"); 482199481Srdivacky // Update the call graph by deleting the edge from Callee to Caller. 483199481Srdivacky CG[Caller]->removeCallEdgeFor(CS); 484199481Srdivacky CS.getInstruction()->eraseFromParent(); 485199481Srdivacky ++NumCallsDeleted; 486199481Srdivacky } else { 487199481Srdivacky // We can only inline direct calls to non-declarations. 488199481Srdivacky if (Callee == 0 || Callee->isDeclaration()) continue; 489198090Srdivacky 490210299Sed // If this call site was obtained by inlining another function, verify 491207618Srdivacky // that the include path for the function did not include the callee 492218893Sdim // itself. If so, we'd be recursively inlining the same function, 493207618Srdivacky // which would provide the same callsites, which would cause us to 494207618Srdivacky // infinitely inline. 495207618Srdivacky int InlineHistoryID = CallSites[CSi].second; 496207618Srdivacky if (InlineHistoryID != -1 && 497207618Srdivacky InlineHistoryIncludes(Callee, InlineHistoryID, InlineHistory)) 498207618Srdivacky continue; 499207618Srdivacky 500207618Srdivacky 501199481Srdivacky // If the policy determines that we should inline this function, 502199481Srdivacky // try to do so. 503199481Srdivacky if (!shouldInline(CS)) 504199481Srdivacky continue; 505193323Sed 506207618Srdivacky // Attempt to inline the function. 507218893Sdim if (!InlineCallIfPossible(CS, InlineInfo, InlinedArrayAllocas, 508263508Sdim InlineHistoryID, InsertLifetime, TD)) 509199481Srdivacky continue; 510199481Srdivacky ++NumInlined; 511207618Srdivacky 512207618Srdivacky // If inlining this function gave us any new call sites, throw them 513207618Srdivacky // onto our worklist to process. They are useful inline candidates. 514207618Srdivacky if (!InlineInfo.InlinedCalls.empty()) { 515207618Srdivacky // Create a new inline history entry for this, so that we remember 516207618Srdivacky // that these new callsites came about due to inlining Callee. 517207618Srdivacky int NewHistoryID = InlineHistory.size(); 518207618Srdivacky InlineHistory.push_back(std::make_pair(Callee, InlineHistoryID)); 519204961Srdivacky 520207618Srdivacky for (unsigned i = 0, e = InlineInfo.InlinedCalls.size(); 521207618Srdivacky i != e; ++i) { 522207618Srdivacky Value *Ptr = InlineInfo.InlinedCalls[i]; 523207618Srdivacky CallSites.push_back(std::make_pair(CallSite(Ptr), NewHistoryID)); 524207618Srdivacky } 525207618Srdivacky } 526199481Srdivacky } 527198090Srdivacky 528199481Srdivacky // If we inlined or deleted the last possible call site to the function, 529199481Srdivacky // delete the function body now. 530199481Srdivacky if (Callee && Callee->use_empty() && Callee->hasLocalLinkage() && 531198090Srdivacky // TODO: Can remove if in SCC now. 532198090Srdivacky !SCCFunctions.count(Callee) && 533198090Srdivacky 534198090Srdivacky // The function may be apparently dead, but if there are indirect 535198090Srdivacky // callgraph references to the node, we cannot delete it yet, this 536198090Srdivacky // could invalidate the CGSCC iterator. 537198090Srdivacky CG[Callee]->getNumReferences() == 0) { 538202375Srdivacky DEBUG(dbgs() << " -> Deleting dead function: " 539198090Srdivacky << Callee->getName() << "\n"); 540198090Srdivacky CallGraphNode *CalleeNode = CG[Callee]; 541198090Srdivacky 542198090Srdivacky // Remove any call graph edges from the callee to its callees. 543198090Srdivacky CalleeNode->removeAllCalledFunctions(); 544198090Srdivacky 545198090Srdivacky // Removing the node for callee from the call graph and delete it. 546198090Srdivacky delete CG.removeFunctionFromModule(CalleeNode); 547198090Srdivacky ++NumDeleted; 548198090Srdivacky } 549193323Sed 550198090Srdivacky // Remove this call site from the list. If possible, use 551198090Srdivacky // swap/pop_back for efficiency, but do not use it if doing so would 552198090Srdivacky // move a call site to a function in this SCC before the 553198090Srdivacky // 'FirstCallInSCC' barrier. 554207618Srdivacky if (SCC.isSingular()) { 555210299Sed CallSites[CSi] = CallSites.back(); 556198090Srdivacky CallSites.pop_back(); 557198090Srdivacky } else { 558198090Srdivacky CallSites.erase(CallSites.begin()+CSi); 559198090Srdivacky } 560198090Srdivacky --CSi; 561193323Sed 562198090Srdivacky Changed = true; 563198090Srdivacky LocalChange = true; 564198090Srdivacky } 565193323Sed } while (LocalChange); 566193323Sed 567193323Sed return Changed; 568193323Sed} 569193323Sed 570193323Sed// doFinalization - Remove now-dead linkonce functions at the end of 571193323Sed// processing to avoid breaking the SCC traversal. 572193323Sedbool Inliner::doFinalization(CallGraph &CG) { 573193323Sed return removeDeadFunctions(CG); 574193323Sed} 575193323Sed 576198090Srdivacky/// removeDeadFunctions - Remove dead functions that are not included in 577198090Srdivacky/// DNR (Do Not Remove) list. 578234353Sdimbool Inliner::removeDeadFunctions(CallGraph &CG, bool AlwaysInlineOnly) { 579234353Sdim SmallVector<CallGraphNode*, 16> FunctionsToRemove; 580193323Sed 581193323Sed // Scan for all of the functions, looking for ones that should now be removed 582193323Sed // from the program. Insert the dead ones in the FunctionsToRemove set. 583193323Sed for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) { 584193323Sed CallGraphNode *CGN = I->second; 585234353Sdim Function *F = CGN->getFunction(); 586234353Sdim if (!F || F->isDeclaration()) 587198090Srdivacky continue; 588234353Sdim 589234353Sdim // Handle the case when this function is called and we only want to care 590234353Sdim // about always-inline functions. This is a bit of a hack to share code 591234353Sdim // between here and the InlineAlways pass. 592243830Sdim if (AlwaysInlineOnly && 593249423Sdim !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 594249423Sdim Attribute::AlwaysInline)) 595234353Sdim continue; 596234353Sdim 597198090Srdivacky // If the only remaining users of the function are dead constants, remove 598198090Srdivacky // them. 599198090Srdivacky F->removeDeadConstantUsers(); 600193323Sed 601234353Sdim if (!F->isDefTriviallyDead()) 602198090Srdivacky continue; 603198090Srdivacky 604198090Srdivacky // Remove any call graph edges from the function to its callees. 605198090Srdivacky CGN->removeAllCalledFunctions(); 606193323Sed 607198090Srdivacky // Remove any edges from the external node to the function's call graph 608198090Srdivacky // node. These edges might have been made irrelegant due to 609198090Srdivacky // optimization of the program. 610198090Srdivacky CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN); 611193323Sed 612198090Srdivacky // Removing the node for callee from the call graph and delete it. 613234353Sdim FunctionsToRemove.push_back(CGN); 614193323Sed } 615234353Sdim if (FunctionsToRemove.empty()) 616234353Sdim return false; 617193323Sed 618193323Sed // Now that we know which functions to delete, do so. We didn't want to do 619193323Sed // this inline, because that would invalidate our CallGraph::iterator 620193323Sed // objects. :( 621198090Srdivacky // 622234353Sdim // Note that it doesn't matter that we are iterating over a non-stable order 623198090Srdivacky // here to do this, it doesn't matter which order the functions are deleted 624198090Srdivacky // in. 625234353Sdim array_pod_sort(FunctionsToRemove.begin(), FunctionsToRemove.end()); 626234353Sdim FunctionsToRemove.erase(std::unique(FunctionsToRemove.begin(), 627234353Sdim FunctionsToRemove.end()), 628234353Sdim FunctionsToRemove.end()); 629234353Sdim for (SmallVectorImpl<CallGraphNode *>::iterator I = FunctionsToRemove.begin(), 630234353Sdim E = FunctionsToRemove.end(); 631234353Sdim I != E; ++I) { 632193323Sed delete CG.removeFunctionFromModule(*I); 633193323Sed ++NumDeleted; 634193323Sed } 635234353Sdim return true; 636193323Sed} 637