Inliner.cpp revision cc0a0299d96676e0a51e9b8f5bf617d8025f09a7
1cf5933a716e7eb6bd5ff49aa62f3e76379ebaf51Chris Lattner//===- Inliner.cpp - Code common to all inliners --------------------------===// 2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// The LLVM Compiler Infrastructure 4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details. 7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===// 9237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner// 10befa499d45ffcc32bd9902518aec18589464e47cChris Lattner// This file implements the mechanics required to implement inlining without 11befa499d45ffcc32bd9902518aec18589464e47cChris Lattner// missing any calls and updating the call graph. The decisions of which calls 12befa499d45ffcc32bd9902518aec18589464e47cChris Lattner// are profitable to inline are implemented elsewhere. 13237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner// 14237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner//===----------------------------------------------------------------------===// 15237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 1686453c52ba02e743d29c08456e51006500041456Chris Lattner#define DEBUG_TYPE "inline" 17237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Module.h" 1847b14a4a6a455c7be169cfd312fcbe796f0ad426Misha Brukman#include "llvm/Instructions.h" 191f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen#include "llvm/IntrinsicInst.h" 20237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Analysis/CallGraph.h" 21237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Support/CallSite.h" 22ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner#include "llvm/Target/TargetData.h" 236f7426ec2e46bb19cc9f9e75f1c355b35cf12d7dTanya Lattner#include "llvm/Transforms/IPO/InlinerPass.h" 2412f0babca4459c253675700e1d707652d5b6ba17Chris Lattner#include "llvm/Transforms/Utils/InlineCost.h" 25237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Transforms/Utils/Cloning.h" 26551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h" 27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h" 28ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar#include "llvm/Support/raw_ostream.h" 2912f0babca4459c253675700e1d707652d5b6ba17Chris Lattner#include "llvm/ADT/SmallPtrSet.h" 30551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h" 31befa499d45ffcc32bd9902518aec18589464e47cChris Lattner#include <set> 32a51bcb50b0c74adc741361824ef81dbefb715c53Chris Lattnerusing namespace llvm; 33d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 3486453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumInlined, "Number of functions inlined"); 3586453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumDeleted, "Number of functions deleted because all callers found"); 36199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris LattnerSTATISTIC(NumMergedAllocas, "Number of allocas merged together"); 3786453c52ba02e743d29c08456e51006500041456Chris Lattner 38844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic cl::opt<int> 395fee49eff92c2ae2f70eb84d136c31a706560750Dale JohannesenInlineLimit("inline-threshold", cl::Hidden, cl::init(200), cl::ZeroOrMore, 40cbfdf9644ce38fd3404469c26ac3c8466c940b6eDale Johannesen cl::desc("Control the amount of inlining to perform (default = 200)")); 41237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 42ae73dc1448d25b02cabc7c64c86c64371453dda8Dan GohmanInliner::Inliner(void *ID) 43ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman : CallGraphSCCPass(ID), InlineThreshold(InlineLimit) {} 44237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 45ae73dc1448d25b02cabc7c64c86c64371453dda8Dan GohmanInliner::Inliner(void *ID, int Threshold) 46ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman : CallGraphSCCPass(ID), InlineThreshold(Threshold) {} 47120d053e3ba810b44047fbcb719824bed5673ca9Chris Lattner 48ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// getAnalysisUsage - For this class, we declare that we require and preserve 49ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// the call graph. If the derived class implements this method, it should 50ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// always explicitly call the implementation here. 51ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattnervoid Inliner::getAnalysisUsage(AnalysisUsage &Info) const { 52ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner CallGraphSCCPass::getAnalysisUsage(Info); 53ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner} 54ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner 55199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 56199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattnertypedef DenseMap<const ArrayType*, std::vector<AllocaInst*> > 57199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris LattnerInlinedArrayAllocasTy; 58199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 59199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// InlineCallIfPossible - If it is possible to inline the specified call site, 60199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// do so and update the CallGraph for this operation. 61199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// 62199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// This function also does some basic book-keeping to update the IR. The 63cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// InlinedArrayAllocas map keeps track of any allocas that are already 64cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// available from other functions inlined into the caller. If we are able to 65cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// inline this call site we attempt to reuse already available allocas or add 66cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// any new allocas to the set if not possible. 67199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattnerstatic bool InlineCallIfPossible(CallSite CS, CallGraph &CG, 68199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner const TargetData *TD, 69199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner InlinedArrayAllocasTy &InlinedArrayAllocas) { 70befa499d45ffcc32bd9902518aec18589464e47cChris Lattner Function *Callee = CS.getCalledFunction(); 7166c75aaa028683c389c55b377ee2411b61081677Bill Wendling Function *Caller = CS.getCaller(); 7266c75aaa028683c389c55b377ee2411b61081677Bill Wendling 73199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Try to inline the function. Get the list of static allocas that were 74199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // inlined. 75199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner SmallVector<AllocaInst*, 16> StaticAllocas; 76199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if (!InlineFunction(CS, &CG, TD, &StaticAllocas)) 7712f0babca4459c253675700e1d707652d5b6ba17Chris Lattner return false; 78fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 798c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling // If the inlined function had a higher stack protection level than the 808c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling // calling function, then bump up the caller's stack protection level. 818c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling if (Callee->hasFnAttr(Attribute::StackProtectReq)) 828c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling Caller->addFnAttr(Attribute::StackProtectReq); 838c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling else if (Callee->hasFnAttr(Attribute::StackProtect) && 848c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling !Caller->hasFnAttr(Attribute::StackProtectReq)) 858c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling Caller->addFnAttr(Attribute::StackProtect); 868c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling 87199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 88199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Look at all of the allocas that we inlined through this call site. If we 89199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // have already inlined other allocas through other calls into this function, 90199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // then we know that they have disjoint lifetimes and that we can merge them. 91199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // 92199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // There are many heuristics possible for merging these allocas, and the 93199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // different options have different tradeoffs. One thing that we *really* 94199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // don't want to hurt is SRoA: once inlining happens, often allocas are no 95199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // longer address taken and so they can be promoted. 96199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // 97199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Our "solution" for that is to only merge allocas whose outermost type is an 98199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // array type. These are usually not promoted because someone is using a 99199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // variable index into them. These are also often the most important ones to 100199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // merge. 101199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // 102199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // A better solution would be to have real memory lifetime markers in the IR 103199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // and not have the inliner do any merging of allocas at all. This would 104199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // allow the backend to do proper stack slot coloring of all allocas that 105199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // *actually make it to the backend*, which is really what we want. 106199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // 107199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Because we don't have this information, we do this simple and useful hack. 108199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // 109199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner SmallPtrSet<AllocaInst*, 16> UsedAllocas; 110199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 111199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Loop over all the allocas we have so far and see if they can be merged with 112199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // a previously inlined alloca. If not, remember that we had it. 113199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner for (unsigned AllocaNo = 0, e = StaticAllocas.size(); 114199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AllocaNo != e; ++AllocaNo) { 115199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AllocaInst *AI = StaticAllocas[AllocaNo]; 116199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 117199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Don't bother trying to merge array allocations (they will usually be 118199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // canonicalized to be an allocation *of* an array), or allocations whose 119199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // type is not itself an array (because we're afraid of pessimizing SRoA). 120199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner const ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType()); 121199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if (ATy == 0 || AI->isArrayAllocation()) 122199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner continue; 123199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 124199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Get the list of all available allocas for this array type. 125199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner std::vector<AllocaInst*> &AllocasForType = InlinedArrayAllocas[ATy]; 126199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 127199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Loop over the allocas in AllocasForType to see if we can reuse one. Note 128199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // that we have to be careful not to reuse the same "available" alloca for 129199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // multiple different allocas that we just inlined, we use the 'UsedAllocas' 130199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // set to keep track of which "available" allocas are being used by this 131199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // function. Also, AllocasForType can be empty of course! 132199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner bool MergedAwayAlloca = false; 133199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner for (unsigned i = 0, e = AllocasForType.size(); i != e; ++i) { 134199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AllocaInst *AvailableAlloca = AllocasForType[i]; 135199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 136199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // The available alloca has to be in the right function, not in some other 137199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // function in this SCC. 138199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if (AvailableAlloca->getParent() != AI->getParent()) 139199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner continue; 140199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 141199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // If the inlined function already uses this alloca then we can't reuse 142199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // it. 143199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if (!UsedAllocas.insert(AvailableAlloca)) 144199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner continue; 145199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 146199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare 147199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // success! 148199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner DEBUG(errs() << " ***MERGED ALLOCA: " << *AI); 149199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 150199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AI->replaceAllUsesWith(AvailableAlloca); 151199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AI->eraseFromParent(); 152199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner MergedAwayAlloca = true; 153199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner ++NumMergedAllocas; 154199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner break; 155199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 156fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 157199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // If we already nuked the alloca, we're done with it. 158199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if (MergedAwayAlloca) 159199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner continue; 1601f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen 161199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // If we were unable to merge away the alloca either because there are no 162199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // allocas of the right type available or because we reused them all 163199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // already, remember that this alloca came from an inlined function and mark 164199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // it used so we don't reuse it for other allocas from this inline 165199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // operation. 166199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AllocasForType.push_back(AI); 167199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner UsedAllocas.insert(AI); 168befa499d45ffcc32bd9902518aec18589464e47cChris Lattner } 169199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 170befa499d45ffcc32bd9902518aec18589464e47cChris Lattner return true; 171237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner} 1721a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar 1731a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar/// shouldInline - Return true if the inliner should attempt to inline 1741a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar/// at the given CallSite. 1751a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbarbool Inliner::shouldInline(CallSite CS) { 176c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar InlineCost IC = getInlineCost(CS); 1771a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar 178c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar if (IC.isAlways()) { 17984a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling DEBUG(errs() << " Inlining: cost=always" 18084a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling << ", Call: " << *CS.getInstruction() << "\n"); 181c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar return true; 182c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar } 183c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar 184c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar if (IC.isNever()) { 18584a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling DEBUG(errs() << " NOT Inlining: cost=never" 18684a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling << ", Call: " << *CS.getInstruction() << "\n"); 187c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar return false; 188c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar } 189c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar 190c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar int Cost = IC.getValue(); 1911a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar int CurrentThreshold = InlineThreshold; 1921a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar Function *Fn = CS.getCaller(); 19384a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling if (Fn && !Fn->isDeclaration() && 19484a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling Fn->hasFnAttr(Attribute::OptimizeForSize) && 19584a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling InlineThreshold != 50) 1961a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar CurrentThreshold = 50; 1971a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar 198135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner float FudgeFactor = getInlineFudgeFactor(CS); 1991a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar if (Cost >= (int)(CurrentThreshold * FudgeFactor)) { 20084a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling DEBUG(errs() << " NOT Inlining: cost=" << Cost 20184a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling << ", Call: " << *CS.getInstruction() << "\n"); 2021a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar return false; 2031a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar } 204135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 205135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner DEBUG(errs() << " Inlining: cost=" << Cost 206135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner << ", Call: " << *CS.getInstruction() << "\n"); 207135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner return true; 2081a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar} 209237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 210237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattnerbool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) { 211237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner CallGraph &CG = getAnalysis<CallGraph>(); 21202a436c48ecff9e34d50ce0a2f861e5acdd9bf3fDan Gohman const TargetData *TD = getAnalysisIfAvailable<TargetData>(); 213237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 21416581bf931c0ccf2f8993397acfa4e1d509a68dcDale Johannesen SmallPtrSet<Function*, 8> SCCFunctions; 21584a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling DEBUG(errs() << "Inliner visiting SCC:"); 216237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner for (unsigned i = 0, e = SCC.size(); i != e; ++i) { 217befa499d45ffcc32bd9902518aec18589464e47cChris Lattner Function *F = SCC[i]->getFunction(); 218befa499d45ffcc32bd9902518aec18589464e47cChris Lattner if (F) SCCFunctions.insert(F); 219ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar DEBUG(errs() << " " << (F ? F->getName() : "INDIRECTNODE")); 220237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner } 221237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 222befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // Scan through and identify all call sites ahead of time so that we only 223befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // inline call sites in the original functions, not call sites that result 224befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // from inlining other functions. 225199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner SmallVector<CallSite, 16> CallSites; 226befa499d45ffcc32bd9902518aec18589464e47cChris Lattner 227135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (unsigned i = 0, e = SCC.size(); i != e; ++i) { 228135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Function *F = SCC[i]->getFunction(); 229135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (!F) continue; 230135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 231135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 232135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 233135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CallSite CS = CallSite::get(I); 234135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (CS.getInstruction() == 0 || isa<DbgInfoIntrinsic>(I)) 235135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 236135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 237135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (CS.getCalledFunction() == 0 || 238135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner !CS.getCalledFunction()->isDeclaration()) 239135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CallSites.push_back(CS); 240135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 241135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 242237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 24384a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling DEBUG(errs() << ": " << CallSites.size() << " call sites.\n"); 244fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 245befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // Now that we have all of the call sites, move the ones to functions in the 246befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // current SCC to the end of the list. 247befa499d45ffcc32bd9902518aec18589464e47cChris Lattner unsigned FirstCallInSCC = CallSites.size(); 248befa499d45ffcc32bd9902518aec18589464e47cChris Lattner for (unsigned i = 0; i < FirstCallInSCC; ++i) 249befa499d45ffcc32bd9902518aec18589464e47cChris Lattner if (Function *F = CallSites[i].getCalledFunction()) 250befa499d45ffcc32bd9902518aec18589464e47cChris Lattner if (SCCFunctions.count(F)) 251befa499d45ffcc32bd9902518aec18589464e47cChris Lattner std::swap(CallSites[i--], CallSites[--FirstCallInSCC]); 252fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 253199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 254199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner InlinedArrayAllocasTy InlinedArrayAllocas; 255199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 256befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // Now that we have all of the call sites, loop over them and inline them if 257befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // it looks profitable to do so. 258befa499d45ffcc32bd9902518aec18589464e47cChris Lattner bool Changed = false; 259befa499d45ffcc32bd9902518aec18589464e47cChris Lattner bool LocalChange; 260befa499d45ffcc32bd9902518aec18589464e47cChris Lattner do { 261befa499d45ffcc32bd9902518aec18589464e47cChris Lattner LocalChange = false; 262befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // Iterate over the outer loop because inlining functions can cause indirect 263befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // calls to become direct calls. 264135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) { 265135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // We can only inline direct calls. 266199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner CallSite CS = CallSites[CSi]; 267199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 268199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner Function *Callee = CS.getCalledFunction(); 269135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (!Callee) continue; 270135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 271135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Calls to external functions are never inlinable. 272135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (Callee->isDeclaration()) { 273135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (SCC.size() == 1) { 274135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner std::swap(CallSites[CSi], CallSites.back()); 275135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CallSites.pop_back(); 276135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } else { 277135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Keep the 'in SCC / not in SCC' boundary correct. 278135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CallSites.erase(CallSites.begin()+CSi); 279befa499d45ffcc32bd9902518aec18589464e47cChris Lattner } 280135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner --CSi; 281135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 282135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 283befa499d45ffcc32bd9902518aec18589464e47cChris Lattner 284135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // If the policy determines that we should inline this function, 285135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // try to do so. 286135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (!shouldInline(CS)) 287135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 288135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 289135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Function *Caller = CS.getCaller(); 290135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Attempt to inline the function... 291199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if (!InlineCallIfPossible(CS, CG, TD, InlinedArrayAllocas)) 292135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 293135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 294199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // If we inlined the last possible call site to the function, delete the 295199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // function body now. 296199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if (Callee->use_empty() && 297199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner (Callee->hasLocalLinkage() || 298199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner Callee->hasAvailableExternallyLinkage()) && 299199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner !SCCFunctions.count(Callee)) { 300199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner DEBUG(errs() << " -> Deleting dead function: " 301199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner << Callee->getName() << "\n"); 302199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner CallGraphNode *CalleeNode = CG[Callee]; 303199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 304199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Remove any call graph edges from the callee to its callees. 305199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner CalleeNode->removeAllCalledFunctions(); 306199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 307199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner resetCachedCostInfo(Callee); 308199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 309199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Removing the node for callee from the call graph and delete it. 310199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner delete CG.removeFunctionFromModule(CalleeNode); 311199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner ++NumDeleted; 312199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 313199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 314135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Remove any cached cost info for this caller, as inlining the 315135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // callee has increased the size of the caller (which may be the 316135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // same as the callee). 317135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner resetCachedCostInfo(Caller); 318135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 319135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Remove this call site from the list. If possible, use 320135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // swap/pop_back for efficiency, but do not use it if doing so would 321135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // move a call site to a function in this SCC before the 322135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // 'FirstCallInSCC' barrier. 323135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (SCC.size() == 1) { 324135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner std::swap(CallSites[CSi], CallSites.back()); 325135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CallSites.pop_back(); 326135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } else { 327135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CallSites.erase(CallSites.begin()+CSi); 328237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner } 329135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner --CSi; 330135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 331135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner ++NumInlined; 332135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Changed = true; 333135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner LocalChange = true; 334135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 335befa499d45ffcc32bd9902518aec18589464e47cChris Lattner } while (LocalChange); 336237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 337775cbdd51a3b33dd5eb343689f65ab5cc8ac7118Chris Lattner return Changed; 338237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner} 339d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 34068d57e7ae80044401efd889270a12c71b3efb9abChris Lattner// doFinalization - Remove now-dead linkonce functions at the end of 34168d57e7ae80044401efd889270a12c71b3efb9abChris Lattner// processing to avoid breaking the SCC traversal. 34268d57e7ae80044401efd889270a12c71b3efb9abChris Lattnerbool Inliner::doFinalization(CallGraph &CG) { 343b7c6bf1e073088635951435acedff793add1cefdDevang Patel return removeDeadFunctions(CG); 344b7c6bf1e073088635951435acedff793add1cefdDevang Patel} 345b7c6bf1e073088635951435acedff793add1cefdDevang Patel 346135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// removeDeadFunctions - Remove dead functions that are not included in 347135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// DNR (Do Not Remove) list. 348b7c6bf1e073088635951435acedff793add1cefdDevang Patelbool Inliner::removeDeadFunctions(CallGraph &CG, 349135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner SmallPtrSet<const Function *, 16> *DNR) { 350135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner SmallPtrSet<CallGraphNode*, 16> FunctionsToRemove; 3513e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner 3523e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner // Scan for all of the functions, looking for ones that should now be removed 3533e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner // from the program. Insert the dead ones in the FunctionsToRemove set. 3543e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) { 3553e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner CallGraphNode *CGN = I->second; 356135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (CGN == 0 || CGN->getFunction() == 0) 357135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 358135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 359135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Function *F = CGN->getFunction(); 360135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 361135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // If the only remaining users of the function are dead constants, remove 362135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // them. 363135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner F->removeDeadConstantUsers(); 364135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 365135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (DNR && DNR->count(F)) 366135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 367135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage()) 368135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 369135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (!F->use_empty()) 370135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 371135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 372135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Remove any call graph edges from the function to its callees. 373135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CGN->removeAllCalledFunctions(); 374135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 375135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Remove any edges from the external node to the function's call graph 376135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // node. These edges might have been made irrelegant due to 377135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // optimization of the program. 378135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN); 379fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 380135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Removing the node for callee from the call graph and delete it. 381135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner FunctionsToRemove.insert(CGN); 38268d57e7ae80044401efd889270a12c71b3efb9abChris Lattner } 3833e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner 3843e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner // Now that we know which functions to delete, do so. We didn't want to do 3853e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner // this inline, because that would invalidate our CallGraph::iterator 3863e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner // objects. :( 387135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // 388135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Note that it doesn't matter that we are iterating over a non-stable set 389135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // here to do this, it doesn't matter which order the functions are deleted 390135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // in. 3913e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner bool Changed = false; 392135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (SmallPtrSet<CallGraphNode*, 16>::iterator I = FunctionsToRemove.begin(), 393135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner E = FunctionsToRemove.end(); I != E; ++I) { 3941f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen resetCachedCostInfo((*I)->getFunction()); 3953e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner delete CG.removeFunctionFromModule(*I); 3963e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner ++NumDeleted; 3973e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner Changed = true; 3983e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner } 3993e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner 40068d57e7ae80044401efd889270a12c71b3efb9abChris Lattner return Changed; 40168d57e7ae80044401efd889270a12c71b3efb9abChris Lattner} 402