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 16d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Transforms/IPO/InlinerPass.h" 17d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallPtrSet.h" 18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/Statistic.h" 19237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Analysis/CallGraph.h" 20e4aeec003f82a5263ffb168e175e6fca8b6f681dDan Gohman#include "llvm/Analysis/InlineCost.h" 2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/CallSite.h" 220b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h" 23dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "llvm/IR/DiagnosticInfo.h" 240b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instructions.h" 250b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IntrinsicInst.h" 260b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h" 27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h" 28551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h" 29ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar#include "llvm/Support/raw_ostream.h" 30d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetLibraryInfo.h" 31d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Transforms/Utils/Cloning.h" 32d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Transforms/Utils/Local.h" 33a51bcb50b0c74adc741361824ef81dbefb715c53Chris Lattnerusing namespace llvm; 34d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 35dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "inline" 36dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 3786453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumInlined, "Number of functions inlined"); 3883f66fe6144c2041f1f7897f7015b0e2e68faad3Chris LattnerSTATISTIC(NumCallsDeleted, "Number of call sites deleted, not inlined"); 3986453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumDeleted, "Number of functions deleted because all callers found"); 40199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris LattnerSTATISTIC(NumMergedAllocas, "Number of allocas merged together"); 4186453c52ba02e743d29c08456e51006500041456Chris Lattner 42d9b0b025612992a0b724eeca8bdf10b1d7a5c355Benjamin Kramer// This weirdly named statistic tracks the number of times that, when attempting 43d6fc26217e194372cabe4ef9e2514beac511a943Chandler Carruth// to inline a function A into B, we analyze the callers of B in order to see 44d6fc26217e194372cabe4ef9e2514beac511a943Chandler Carruth// if those would be more profitable and blocked inline steps. 45d6fc26217e194372cabe4ef9e2514beac511a943Chandler CarruthSTATISTIC(NumCallerCallersAnalyzed, "Number of caller-callers analyzed"); 46d6fc26217e194372cabe4ef9e2514beac511a943Chandler Carruth 47844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic cl::opt<int> 48f9c3b228e5579e0d2a9cd05a2191fe17b4c58b23Jakob Stoklund OlesenInlineLimit("inline-threshold", cl::Hidden, cl::init(225), cl::ZeroOrMore, 49f9c3b228e5579e0d2a9cd05a2191fe17b4c58b23Jakob Stoklund Olesen cl::desc("Control the amount of inlining to perform (default = 225)")); 50237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 51f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesenstatic cl::opt<int> 52f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund OlesenHintThreshold("inlinehint-threshold", cl::Hidden, cl::init(325), 53f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen cl::desc("Threshold for inlining functions with inline hint")); 54570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen 5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// We instroduce this threshold to help performance of instrumentation based 5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// PGO before we actually hook up inliner with analysis passes such as BPI and 5736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// BFI. 5836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic cl::opt<int> 5936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesColdThreshold("inlinecold-threshold", cl::Hidden, cl::init(225), 6036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines cl::desc("Threshold for inlining functions with cold attribute")); 6136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 62570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen// Threshold to use when optsize is specified (and there is no -inline-limit). 63570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesenconst int OptSizeThreshold = 75; 64570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen 6590c579de5a383cee278acc3f7e7b9d0a656e6a35Owen AndersonInliner::Inliner(char &ID) 66fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier : CallGraphSCCPass(ID), InlineThreshold(InlineLimit), InsertLifetime(true) {} 67237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 68fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad RosierInliner::Inliner(char &ID, int Threshold, bool InsertLifetime) 69930f5efac0f76aa9e3772d9a36757f18b3573112Jakob Stoklund Olesen : CallGraphSCCPass(ID), InlineThreshold(InlineLimit.getNumOccurrences() > 0 ? 70fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier InlineLimit : Threshold), 71fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier InsertLifetime(InsertLifetime) {} 72120d053e3ba810b44047fbcb719824bed5673ca9Chris Lattner 73ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// getAnalysisUsage - For this class, we declare that we require and preserve 74ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// the call graph. If the derived class implements this method, it should 75ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// always explicitly call the implementation here. 767ccc2f7a7859a72ce73e6d8dc4242c6dd7984c45Chandler Carruthvoid Inliner::getAnalysisUsage(AnalysisUsage &AU) const { 777ccc2f7a7859a72ce73e6d8dc4242c6dd7984c45Chandler Carruth CallGraphSCCPass::getAnalysisUsage(AU); 78ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner} 79ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner 80199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 81db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnertypedef DenseMap<ArrayType*, std::vector<AllocaInst*> > 82199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris LattnerInlinedArrayAllocasTy; 83199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 84114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling/// \brief If the inlined function had a higher stack protection level than the 85114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling/// calling function, then bump up the caller's stack protection level. 86114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendlingstatic void AdjustCallerSSPLevel(Function *Caller, Function *Callee) { 87114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling // If upgrading the SSP attribute, clear out the old SSP Attributes first. 88114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling // Having multiple SSP attributes doesn't actually hurt, but it adds useless 89114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling // clutter to the IR. 90114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling AttrBuilder B; 91114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling B.addAttribute(Attribute::StackProtect) 92114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling .addAttribute(Attribute::StackProtectStrong); 93114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling AttributeSet OldSSPAttr = AttributeSet::get(Caller->getContext(), 94114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling AttributeSet::FunctionIndex, 95114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling B); 96114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling AttributeSet CallerAttr = Caller->getAttributes(), 97114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling CalleeAttr = Callee->getAttributes(); 98114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling 99114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling if (CalleeAttr.hasAttribute(AttributeSet::FunctionIndex, 100114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Attribute::StackProtectReq)) { 101114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Caller->removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr); 102114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Caller->addFnAttr(Attribute::StackProtectReq); 103114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling } else if (CalleeAttr.hasAttribute(AttributeSet::FunctionIndex, 104114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Attribute::StackProtectStrong) && 105114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling !CallerAttr.hasAttribute(AttributeSet::FunctionIndex, 106114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Attribute::StackProtectReq)) { 107114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Caller->removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr); 108114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Caller->addFnAttr(Attribute::StackProtectStrong); 109114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling } else if (CalleeAttr.hasAttribute(AttributeSet::FunctionIndex, 110114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Attribute::StackProtect) && 111114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling !CallerAttr.hasAttribute(AttributeSet::FunctionIndex, 112114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Attribute::StackProtectReq) && 113114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling !CallerAttr.hasAttribute(AttributeSet::FunctionIndex, 114114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Attribute::StackProtectStrong)) 115114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling Caller->addFnAttr(Attribute::StackProtect); 116114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling} 117114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling 118199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// InlineCallIfPossible - If it is possible to inline the specified call site, 119199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// do so and update the CallGraph for this operation. 120199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// 121199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// This function also does some basic book-keeping to update the IR. The 122cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// InlinedArrayAllocas map keeps track of any allocas that are already 123cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// available from other functions inlined into the caller. If we are able to 124cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// inline this call site we attempt to reuse already available allocas or add 125cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// any new allocas to the set if not possible. 12660915146f4d35e12f10dcdaa155596fac79184daChris Lattnerstatic bool InlineCallIfPossible(CallSite CS, InlineFunctionInfo &IFI, 1276c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner InlinedArrayAllocasTy &InlinedArrayAllocas, 1285a5ebb7f9fa7fa82c0c466a36a90e5c18bb13073Hal Finkel int InlineHistory, bool InsertLifetime, 12936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const DataLayout *DL) { 130befa499d45ffcc32bd9902518aec18589464e47cChris Lattner Function *Callee = CS.getCalledFunction(); 13166c75aaa028683c389c55b377ee2411b61081677Bill Wendling Function *Caller = CS.getCaller(); 13266c75aaa028683c389c55b377ee2411b61081677Bill Wendling 133199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Try to inline the function. Get the list of static allocas that were 134199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // inlined. 135fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier if (!InlineFunction(CS, IFI, InsertLifetime)) 13612f0babca4459c253675700e1d707652d5b6ba17Chris Lattner return false; 137fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 138114baee1fa017daefad2339c77b45b9ca3d79a41Bill Wendling AdjustCallerSSPLevel(Caller, Callee); 1398c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling 140199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Look at all of the allocas that we inlined through this call site. If we 141199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // have already inlined other allocas through other calls into this function, 142199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // then we know that they have disjoint lifetimes and that we can merge them. 143199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // 144199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // There are many heuristics possible for merging these allocas, and the 145199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // different options have different tradeoffs. One thing that we *really* 146199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // don't want to hurt is SRoA: once inlining happens, often allocas are no 147199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // longer address taken and so they can be promoted. 148199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // 149199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Our "solution" for that is to only merge allocas whose outermost type is an 150199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // array type. These are usually not promoted because someone is using a 151199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // variable index into them. These are also often the most important ones to 152199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // merge. 153199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // 154199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // A better solution would be to have real memory lifetime markers in the IR 155199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // and not have the inliner do any merging of allocas at all. This would 156199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // allow the backend to do proper stack slot coloring of all allocas that 157199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // *actually make it to the backend*, which is really what we want. 158199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // 159199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Because we don't have this information, we do this simple and useful hack. 160199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // 161199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner SmallPtrSet<AllocaInst*, 16> UsedAllocas; 162199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 1636c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // When processing our SCC, check to see if CS was inlined from some other 1646c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // call site. For example, if we're processing "A" in this code: 1656c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // A() { B() } 1666c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // B() { x = alloca ... C() } 1676c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // C() { y = alloca ... } 1686c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // Assume that C was not inlined into B initially, and so we're processing A 1696c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // and decide to inline B into A. Doing this makes an alloca available for 1706c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // reuse and makes a callsite (C) available for inlining. When we process 1716c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // the C call site we don't want to do any alloca merging between X and Y 1726c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // because their scopes are not disjoint. We could make this smarter by 1736c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // keeping track of the inline history for each alloca in the 1746c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner // InlinedArrayAllocas but this isn't likely to be a significant win. 1756c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner if (InlineHistory != -1) // Only do merging for top-level call sites in SCC. 1766c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner return true; 1776c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner 178199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Loop over all the allocas we have so far and see if they can be merged with 179199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // a previously inlined alloca. If not, remember that we had it. 18060915146f4d35e12f10dcdaa155596fac79184daChris Lattner for (unsigned AllocaNo = 0, e = IFI.StaticAllocas.size(); 181199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AllocaNo != e; ++AllocaNo) { 18260915146f4d35e12f10dcdaa155596fac79184daChris Lattner AllocaInst *AI = IFI.StaticAllocas[AllocaNo]; 183199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 184199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Don't bother trying to merge array allocations (they will usually be 185199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // canonicalized to be an allocation *of* an array), or allocations whose 186199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // type is not itself an array (because we're afraid of pessimizing SRoA). 187db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType()); 188dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!ATy || AI->isArrayAllocation()) 189199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner continue; 190199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 191199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Get the list of all available allocas for this array type. 192199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner std::vector<AllocaInst*> &AllocasForType = InlinedArrayAllocas[ATy]; 193199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 194199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Loop over the allocas in AllocasForType to see if we can reuse one. Note 195199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // that we have to be careful not to reuse the same "available" alloca for 196199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // multiple different allocas that we just inlined, we use the 'UsedAllocas' 197199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // set to keep track of which "available" allocas are being used by this 198199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // function. Also, AllocasForType can be empty of course! 199199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner bool MergedAwayAlloca = false; 200199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner for (unsigned i = 0, e = AllocasForType.size(); i != e; ++i) { 201199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AllocaInst *AvailableAlloca = AllocasForType[i]; 2025a5ebb7f9fa7fa82c0c466a36a90e5c18bb13073Hal Finkel 2035a5ebb7f9fa7fa82c0c466a36a90e5c18bb13073Hal Finkel unsigned Align1 = AI->getAlignment(), 2045a5ebb7f9fa7fa82c0c466a36a90e5c18bb13073Hal Finkel Align2 = AvailableAlloca->getAlignment(); 2055a5ebb7f9fa7fa82c0c466a36a90e5c18bb13073Hal Finkel // If we don't have data layout information, and only one alloca is using 2065a5ebb7f9fa7fa82c0c466a36a90e5c18bb13073Hal Finkel // the target default, then we can't safely merge them because we can't 2075a5ebb7f9fa7fa82c0c466a36a90e5c18bb13073Hal Finkel // pick the greater alignment. 20836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (!DL && (!Align1 || !Align2) && Align1 != Align2) 2095a5ebb7f9fa7fa82c0c466a36a90e5c18bb13073Hal Finkel continue; 210199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 211199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // The available alloca has to be in the right function, not in some other 212199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // function in this SCC. 213199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if (AvailableAlloca->getParent() != AI->getParent()) 214199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner continue; 215199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 216199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // If the inlined function already uses this alloca then we can't reuse 217199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // it. 218199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if (!UsedAllocas.insert(AvailableAlloca)) 219199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner continue; 220199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 221199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare 222199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // success! 2237d32b8032d7ec2472b994aab2ac3459e8d47c496Chris Lattner DEBUG(dbgs() << " ***MERGED ALLOCA: " << *AI << "\n\t\tINTO: " 2247d32b8032d7ec2472b994aab2ac3459e8d47c496Chris Lattner << *AvailableAlloca << '\n'); 225199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 226199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AI->replaceAllUsesWith(AvailableAlloca); 2275a5ebb7f9fa7fa82c0c466a36a90e5c18bb13073Hal Finkel 22886f4f6526b18765cdb78bee593e1354bc9f55085Hal Finkel if (Align1 != Align2) { 22986f4f6526b18765cdb78bee593e1354bc9f55085Hal Finkel if (!Align1 || !Align2) { 23036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines assert(DL && "DataLayout required to compare default alignments"); 23136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines unsigned TypeAlign = DL->getABITypeAlignment(AI->getAllocatedType()); 23286f4f6526b18765cdb78bee593e1354bc9f55085Hal Finkel 23386f4f6526b18765cdb78bee593e1354bc9f55085Hal Finkel Align1 = Align1 ? Align1 : TypeAlign; 23486f4f6526b18765cdb78bee593e1354bc9f55085Hal Finkel Align2 = Align2 ? Align2 : TypeAlign; 23586f4f6526b18765cdb78bee593e1354bc9f55085Hal Finkel } 23686f4f6526b18765cdb78bee593e1354bc9f55085Hal Finkel 23786f4f6526b18765cdb78bee593e1354bc9f55085Hal Finkel if (Align1 > Align2) 23886f4f6526b18765cdb78bee593e1354bc9f55085Hal Finkel AvailableAlloca->setAlignment(AI->getAlignment()); 23986f4f6526b18765cdb78bee593e1354bc9f55085Hal Finkel } 2405a5ebb7f9fa7fa82c0c466a36a90e5c18bb13073Hal Finkel 241199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AI->eraseFromParent(); 242199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner MergedAwayAlloca = true; 243199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner ++NumMergedAllocas; 244dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines IFI.StaticAllocas[AllocaNo] = nullptr; 245199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner break; 246199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 247fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 248199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // If we already nuked the alloca, we're done with it. 249199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if (MergedAwayAlloca) 250199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner continue; 2517d32b8032d7ec2472b994aab2ac3459e8d47c496Chris Lattner 252199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // If we were unable to merge away the alloca either because there are no 253199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // allocas of the right type available or because we reused them all 254199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // already, remember that this alloca came from an inlined function and mark 255199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // it used so we don't reuse it for other allocas from this inline 256199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // operation. 257199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner AllocasForType.push_back(AI); 258199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner UsedAllocas.insert(AI); 259befa499d45ffcc32bd9902518aec18589464e47cChris Lattner } 260199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 261befa499d45ffcc32bd9902518aec18589464e47cChris Lattner return true; 262237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner} 263f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen 264570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesenunsigned Inliner::getInlineThreshold(CallSite CS) const { 265ab767213fdfc219384e26b8073783cb883974dcdPatrik Hägglund int thres = InlineThreshold; // -inline-threshold or else selected by 266ab767213fdfc219384e26b8073783cb883974dcdPatrik Hägglund // overall opt level 267570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen 268ab767213fdfc219384e26b8073783cb883974dcdPatrik Hägglund // If -inline-threshold is not given, listen to the optsize attribute when it 269ab767213fdfc219384e26b8073783cb883974dcdPatrik Hägglund // would decrease the threshold. 270570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen Function *Caller = CS.getCaller(); 271ab767213fdfc219384e26b8073783cb883974dcdPatrik Hägglund bool OptSize = Caller && !Caller->isDeclaration() && 272831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling Caller->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 273831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling Attribute::OptimizeForSize); 274aa8405811ed9ad84e5afb70dac04ebfbae519316Nadav Rotem if (!(InlineLimit.getNumOccurrences() > 0) && OptSize && 275aa8405811ed9ad84e5afb70dac04ebfbae519316Nadav Rotem OptSizeThreshold < thres) 276f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen thres = OptSizeThreshold; 277f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen 278fcc934322bc6e0b89df8fbcdde59145f5af1179aQuentin Colombet // Listen to the inlinehint attribute when it would increase the threshold 279fcc934322bc6e0b89df8fbcdde59145f5af1179aQuentin Colombet // and the caller does not need to minimize its size. 280f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen Function *Callee = CS.getCalledFunction(); 281ab767213fdfc219384e26b8073783cb883974dcdPatrik Hägglund bool InlineHint = Callee && !Callee->isDeclaration() && 282831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling Callee->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 283831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling Attribute::InlineHint); 284fcc934322bc6e0b89df8fbcdde59145f5af1179aQuentin Colombet if (InlineHint && HintThreshold > thres 285831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling && !Caller->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 286831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling Attribute::MinSize)) 287f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen thres = HintThreshold; 288570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen 28936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines // Listen to the cold attribute when it would decrease the threshold. 29036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines bool ColdCallee = Callee && !Callee->isDeclaration() && 29136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Callee->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 29236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Attribute::Cold); 293dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // Command line argument for InlineLimit will override the default 294dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // ColdThreshold. If we have -inline-threshold but no -inlinecold-threshold, 295dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // do not use the default cold threshold even if it is smaller. 296dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if ((InlineLimit.getNumOccurrences() == 0 || 297dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines ColdThreshold.getNumOccurrences() > 0) && ColdCallee && 298dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines ColdThreshold < thres) 29936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines thres = ColdThreshold; 30036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 301f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen return thres; 302f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen} 303f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen 304dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesstatic void emitAnalysis(CallSite CS, const Twine &Msg) { 305dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Function *Caller = CS.getCaller(); 306dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines LLVMContext &Ctx = Caller->getContext(); 307dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines DebugLoc DLoc = CS.getInstruction()->getDebugLoc(); 308dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines emitOptimizationRemarkAnalysis(Ctx, DEBUG_TYPE, *Caller, DLoc, Msg); 309dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines} 310dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 3111a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar/// shouldInline - Return true if the inliner should attempt to inline 3121a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar/// at the given CallSite. 3131a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbarbool Inliner::shouldInline(CallSite CS) { 314c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar InlineCost IC = getInlineCost(CS); 3151a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar 316c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar if (IC.isAlways()) { 317c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene DEBUG(dbgs() << " Inlining: cost=always" 31884a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling << ", Call: " << *CS.getInstruction() << "\n"); 319dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines emitAnalysis(CS, Twine(CS.getCalledFunction()->getName()) + 320dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines " should always be inlined (cost=always)"); 321c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar return true; 322c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar } 323c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar 324c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar if (IC.isNever()) { 325c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene DEBUG(dbgs() << " NOT Inlining: cost=never" 32684a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling << ", Call: " << *CS.getInstruction() << "\n"); 327dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines emitAnalysis(CS, Twine(CS.getCalledFunction()->getName() + 328dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines " should never be inlined (cost=never)")); 329c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar return false; 330c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar } 331c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar 332e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen Function *Caller = CS.getCaller(); 333f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth if (!IC) { 334f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth DEBUG(dbgs() << " NOT Inlining: cost=" << IC.getCost() 335f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth << ", thres=" << (IC.getCostDelta() + IC.getCost()) 33684a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling << ", Call: " << *CS.getInstruction() << "\n"); 337dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines emitAnalysis(CS, Twine(CS.getCalledFunction()->getName() + 338dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines " too costly to inline (cost=") + 339dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Twine(IC.getCost()) + ", threshold=" + 340dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Twine(IC.getCostDelta() + IC.getCost()) + ")"); 3411a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar return false; 3421a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar } 343135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 344b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // Try to detect the case where the current inlining candidate caller (call 345b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // it B) is a static or linkonce-ODR function and is an inlining candidate 346b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // elsewhere, and the current candidate callee (call it C) is large enough 347b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // that inlining it into B would make B too big to inline later. In these 348b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // circumstances it may be best not to inline C into B, but to inline B into 349b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // its callers. 350b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // 351b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // This only applies to static and linkonce-ODR functions because those are 352b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // expected to be available for inlining in the translation units where they 353b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // are used. Thus we will always have the opportunity to make local inlining 354b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // decisions. Importantly the linkonce-ODR linkage covers inline functions 355b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth // and templates in C++. 356f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth // 357f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth // FIXME: All of this logic should be sunk into getInlineCost. It relies on 358f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth // the internal implementation of the inline cost metrics rather than 359f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth // treating them as truly abstract units etc. 360b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth if (Caller->hasLocalLinkage() || 361b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth Caller->getLinkage() == GlobalValue::LinkOnceODRLinkage) { 362e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen int TotalSecondaryCost = 0; 363f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth // The candidate cost to be imposed upon the current function. 364f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth int CandidateCost = IC.getCost() - (InlineConstants::CallPenalty + 1); 365c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen // This bool tracks what happens if we do NOT inline C into B. 366dacffb6679ff99fec4cc9c54f4257d2913870d01Chandler Carruth bool callerWillBeRemoved = Caller->hasLocalLinkage(); 367c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen // This bool tracks what happens if we DO inline C into B. 368c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen bool inliningPreventsSomeOuterInline = false; 36936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines for (User *U : Caller->users()) { 37036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines CallSite CS2(U); 371e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen 372e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen // If this isn't a call to Caller (it could be some other sort 373c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen // of reference) skip it. Such references will prevent the caller 374c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen // from being removed. 375c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen if (!CS2 || CS2.getCalledFunction() != Caller) { 376c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen callerWillBeRemoved = false; 377e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen continue; 378c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen } 379e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen 380e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen InlineCost IC2 = getInlineCost(CS2); 381d6fc26217e194372cabe4ef9e2514beac511a943Chandler Carruth ++NumCallerCallersAnalyzed; 382f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth if (!IC2) { 383c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen callerWillBeRemoved = false; 384f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth continue; 385f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth } 386f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth if (IC2.isAlways()) 387e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen continue; 388e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen 389f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth // See if inlining or original callsite would erase the cost delta of 390f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth // this callsite. We subtract off the penalty for the call instruction, 391f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth // which we would be deleting. 392f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth if (IC2.getCostDelta() <= CandidateCost) { 393c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen inliningPreventsSomeOuterInline = true; 394f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth TotalSecondaryCost += IC2.getCost(); 395e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen } 396e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen } 397e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen // If all outer calls to Caller would get inlined, the cost for the last 398e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen // one is set very low by getInlineCost, in anticipation that Caller will 399e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen // be removed entirely. We did not account for this above unless there 400e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen // is only one caller of Caller. 40136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (callerWillBeRemoved && !Caller->use_empty()) 402bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen TotalSecondaryCost += InlineConstants::LastCallToStaticBonus; 403e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen 404f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth if (inliningPreventsSomeOuterInline && TotalSecondaryCost < IC.getCost()) { 405f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth DEBUG(dbgs() << " NOT Inlining: " << *CS.getInstruction() << 406f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth " Cost = " << IC.getCost() << 407e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen ", outer Cost = " << TotalSecondaryCost << '\n'); 408dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines emitAnalysis( 409dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CS, Twine("Not inlining. Cost of inlining " + 410dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CS.getCalledFunction()->getName() + 411dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines " increases the cost of inlining " + 412dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CS.getCaller()->getName() + " in other contexts")); 413e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen return false; 414e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen } 415e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen } 416e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen 417f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth DEBUG(dbgs() << " Inlining: cost=" << IC.getCost() 418f2286b0152f0b942e82d8e809186e5cc0d247131Chandler Carruth << ", thres=" << (IC.getCostDelta() + IC.getCost()) 419e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen << ", Call: " << *CS.getInstruction() << '\n'); 420dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines emitAnalysis( 421dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CS, CS.getCalledFunction()->getName() + Twine(" can be inlined into ") + 422dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CS.getCaller()->getName() + " with cost=" + Twine(IC.getCost()) + 423dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines " (threshold=" + Twine(IC.getCostDelta() + IC.getCost()) + ")"); 424135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner return true; 4251a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar} 426237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 427159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner/// InlineHistoryIncludes - Return true if the specified inline history ID 428159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner/// indicates an inline history that includes the specified function. 429159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattnerstatic bool InlineHistoryIncludes(Function *F, int InlineHistoryID, 430159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner const SmallVectorImpl<std::pair<Function*, int> > &InlineHistory) { 431159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner while (InlineHistoryID != -1) { 432159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner assert(unsigned(InlineHistoryID) < InlineHistory.size() && 433159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner "Invalid inline history ID"); 434159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner if (InlineHistory[InlineHistoryID].first == F) 435159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner return true; 436159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner InlineHistoryID = InlineHistory[InlineHistoryID].second; 437159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner } 438159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner return false; 439159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner} 440159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner 4412decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattnerbool Inliner::runOnSCC(CallGraphSCC &SCC) { 44236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); 44336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); 444dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr; 4458e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer const TargetLibraryInfo *TLI = getAnalysisIfAvailable<TargetLibraryInfo>(); 446237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 44716581bf931c0ccf2f8993397acfa4e1d509a68dcDale Johannesen SmallPtrSet<Function*, 8> SCCFunctions; 448c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene DEBUG(dbgs() << "Inliner visiting SCC:"); 4492decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 4502decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner Function *F = (*I)->getFunction(); 451befa499d45ffcc32bd9902518aec18589464e47cChris Lattner if (F) SCCFunctions.insert(F); 452c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene DEBUG(dbgs() << " " << (F ? F->getName() : "INDIRECTNODE")); 453237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner } 454237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 455befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // Scan through and identify all call sites ahead of time so that we only 456befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // inline call sites in the original functions, not call sites that result 457befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // from inlining other functions. 458159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner SmallVector<std::pair<CallSite, int>, 16> CallSites; 459159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner 460159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner // When inlining a callee produces new call sites, we want to keep track of 461159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner // the fact that they were inlined from the callee. This allows us to avoid 462159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner // infinite inlining in some obscure cases. To represent this, we use an 463159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner // index into the InlineHistory vector. 464159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner SmallVector<std::pair<Function*, int>, 8> InlineHistory; 465befa499d45ffcc32bd9902518aec18589464e47cChris Lattner 4662decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 4672decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner Function *F = (*I)->getFunction(); 468135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner if (!F) continue; 469135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 470135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 471135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 4727d3056b16038a6a09c452c0dfcc3c8f4e421506aGabor Greif CallSite CS(cast<Value>(I)); 473e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen // If this isn't a call, or it is a call to an intrinsic, it can 474d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner // never be inlined. 4757d3056b16038a6a09c452c0dfcc3c8f4e421506aGabor Greif if (!CS || isa<IntrinsicInst>(I)) 476135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 477135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 478d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner // If this is a direct call to an external function, we can never inline 479d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner // it. If it is an indirect call, inlining may resolve it to be a 480d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner // direct call, so we keep it. 481d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner if (CS.getCalledFunction() && CS.getCalledFunction()->isDeclaration()) 482d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner continue; 483d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner 484159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner CallSites.push_back(std::make_pair(CS, -1)); 485135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 486135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 487237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 488c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n"); 489fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 4904471136e4db67f5b6cf064cb3b0a7668f15bfe6cChris Lattner // If there are no calls in this function, exit early. 4914471136e4db67f5b6cf064cb3b0a7668f15bfe6cChris Lattner if (CallSites.empty()) 4924471136e4db67f5b6cf064cb3b0a7668f15bfe6cChris Lattner return false; 4934471136e4db67f5b6cf064cb3b0a7668f15bfe6cChris Lattner 494befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // Now that we have all of the call sites, move the ones to functions in the 495befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // current SCC to the end of the list. 496befa499d45ffcc32bd9902518aec18589464e47cChris Lattner unsigned FirstCallInSCC = CallSites.size(); 497befa499d45ffcc32bd9902518aec18589464e47cChris Lattner for (unsigned i = 0; i < FirstCallInSCC; ++i) 498159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner if (Function *F = CallSites[i].first.getCalledFunction()) 499befa499d45ffcc32bd9902518aec18589464e47cChris Lattner if (SCCFunctions.count(F)) 500befa499d45ffcc32bd9902518aec18589464e47cChris Lattner std::swap(CallSites[i--], CallSites[--FirstCallInSCC]); 501fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 502199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 503199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner InlinedArrayAllocasTy InlinedArrayAllocas; 50436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines InlineFunctionInfo InlineInfo(&CG, DL); 505199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 506befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // Now that we have all of the call sites, loop over them and inline them if 507befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // it looks profitable to do so. 508befa499d45ffcc32bd9902518aec18589464e47cChris Lattner bool Changed = false; 509befa499d45ffcc32bd9902518aec18589464e47cChris Lattner bool LocalChange; 510befa499d45ffcc32bd9902518aec18589464e47cChris Lattner do { 511befa499d45ffcc32bd9902518aec18589464e47cChris Lattner LocalChange = false; 512befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // Iterate over the outer loop because inlining functions can cause indirect 513befa499d45ffcc32bd9902518aec18589464e47cChris Lattner // calls to become direct calls. 514135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) { 515159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner CallSite CS = CallSites[CSi].first; 516199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 517dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner Function *Caller = CS.getCaller(); 518199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner Function *Callee = CS.getCalledFunction(); 519dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner 520dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner // If this call site is dead and it is to a readonly function, we should 521dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner // just delete the call instead of trying to inline it, regardless of 522dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner // size. This happens because IPSCCP propagates the result out of the 523dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner // call and then we're left with the dead call. 5248e0d1c03ca7fd86e6879b4e37d0d7f0e982feef6Benjamin Kramer if (isInstructionTriviallyDead(CS.getInstruction(), TLI)) { 525c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene DEBUG(dbgs() << " -> Deleting dead call: " 526dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner << *CS.getInstruction() << "\n"); 527dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner // Update the call graph by deleting the edge from Callee to Caller. 528dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner CG[Caller]->removeCallEdgeFor(CS); 529dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner CS.getInstruction()->eraseFromParent(); 530dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner ++NumCallsDeleted; 531dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner } else { 532dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner // We can only inline direct calls to non-declarations. 533dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!Callee || Callee->isDeclaration()) continue; 534135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 535f0193ed8decb2e78d8d5ec4a4eaeed8f3036bf6eEric Christopher // If this call site was obtained by inlining another function, verify 536159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner // that the include path for the function did not include the callee 5377d32b8032d7ec2472b994aab2ac3459e8d47c496Chris Lattner // itself. If so, we'd be recursively inlining the same function, 538159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner // which would provide the same callsites, which would cause us to 539159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner // infinitely inline. 540159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner int InlineHistoryID = CallSites[CSi].second; 541159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner if (InlineHistoryID != -1 && 542159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner InlineHistoryIncludes(Callee, InlineHistoryID, InlineHistory)) 543159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner continue; 544159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner 545dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines LLVMContext &CallerCtx = Caller->getContext(); 546dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 547dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // Get DebugLoc to report. CS will be invalid after Inliner. 548dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines DebugLoc DLoc = CS.getInstruction()->getDebugLoc(); 549dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 550dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner // If the policy determines that we should inline this function, 551dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner // try to do so. 552dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!shouldInline(CS)) { 553dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines emitOptimizationRemarkMissed(CallerCtx, DEBUG_TYPE, *Caller, DLoc, 554dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Twine(Callee->getName() + 555dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines " will not be inlined into " + 556dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Caller->getName())); 557dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner continue; 558dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines } 559e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen 560fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner // Attempt to inline the function. 5616c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner if (!InlineCallIfPossible(CS, InlineInfo, InlinedArrayAllocas, 562dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines InlineHistoryID, InsertLifetime, DL)) { 563dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines emitOptimizationRemarkMissed(CallerCtx, DEBUG_TYPE, *Caller, DLoc, 564dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Twine(Callee->getName() + 565dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines " will not be inlined into " + 566dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Caller->getName())); 567dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner continue; 568dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines } 569dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner ++NumInlined; 570dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 571dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines // Report the inline decision. 572dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines emitOptimizationRemark( 573dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines CallerCtx, DEBUG_TYPE, *Caller, DLoc, 574dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines Twine(Callee->getName() + " inlined into " + Caller->getName())); 575dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 5760ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner // If inlining this function gave us any new call sites, throw them 577fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner // onto our worklist to process. They are useful inline candidates. 5780ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner if (!InlineInfo.InlinedCalls.empty()) { 579159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner // Create a new inline history entry for this, so that we remember 580159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner // that these new callsites came about due to inlining Callee. 581159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner int NewHistoryID = InlineHistory.size(); 582159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner InlineHistory.push_back(std::make_pair(Callee, InlineHistoryID)); 583159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner 5840ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner for (unsigned i = 0, e = InlineInfo.InlinedCalls.size(); 585159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner i != e; ++i) { 5860ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner Value *Ptr = InlineInfo.InlinedCalls[i]; 587d54f9a4c3bcdb247ea4aa311251c19242b03be63Chandler Carruth CallSites.push_back(std::make_pair(CallSite(Ptr), NewHistoryID)); 588159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner } 589076863225ce070345ff7048f48b3550e00598a10Chris Lattner } 590dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner } 591135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 592dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner // If we inlined or deleted the last possible call site to the function, 593dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner // delete the function body now. 594dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner if (Callee && Callee->use_empty() && Callee->hasLocalLinkage() && 595d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner // TODO: Can remove if in SCC now. 596b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner !SCCFunctions.count(Callee) && 597d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner 598b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner // The function may be apparently dead, but if there are indirect 599b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner // callgraph references to the node, we cannot delete it yet, this 600b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner // could invalidate the CGSCC iterator. 601b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner CG[Callee]->getNumReferences() == 0) { 602c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene DEBUG(dbgs() << " -> Deleting dead function: " 603199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner << Callee->getName() << "\n"); 604199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner CallGraphNode *CalleeNode = CG[Callee]; 605199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 606199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Remove any call graph edges from the callee to its callees. 607199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner CalleeNode->removeAllCalledFunctions(); 608199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 609199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner // Removing the node for callee from the call graph and delete it. 610199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner delete CG.removeFunctionFromModule(CalleeNode); 611199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner ++NumDeleted; 612199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 613135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 614135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Remove this call site from the list. If possible, use 615135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // swap/pop_back for efficiency, but do not use it if doing so would 616135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // move a call site to a function in this SCC before the 617135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // 'FirstCallInSCC' barrier. 6182decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner if (SCC.isSingular()) { 619c29df3cac735bc85d16e4ef3186cb50e41bec7bbBenjamin Kramer CallSites[CSi] = CallSites.back(); 620135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CallSites.pop_back(); 621135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } else { 622135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CallSites.erase(CallSites.begin()+CSi); 623237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner } 624135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner --CSi; 625135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 626135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Changed = true; 627135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner LocalChange = true; 628135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner } 629befa499d45ffcc32bd9902518aec18589464e47cChris Lattner } while (LocalChange); 630237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner 631775cbdd51a3b33dd5eb343689f65ab5cc8ac7118Chris Lattner return Changed; 632237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner} 633d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 63468d57e7ae80044401efd889270a12c71b3efb9abChris Lattner// doFinalization - Remove now-dead linkonce functions at the end of 63568d57e7ae80044401efd889270a12c71b3efb9abChris Lattner// processing to avoid breaking the SCC traversal. 63668d57e7ae80044401efd889270a12c71b3efb9abChris Lattnerbool Inliner::doFinalization(CallGraph &CG) { 637b7c6bf1e073088635951435acedff793add1cefdDevang Patel return removeDeadFunctions(CG); 638b7c6bf1e073088635951435acedff793add1cefdDevang Patel} 639b7c6bf1e073088635951435acedff793add1cefdDevang Patel 640135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// removeDeadFunctions - Remove dead functions that are not included in 641135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// DNR (Do Not Remove) list. 642f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruthbool Inliner::removeDeadFunctions(CallGraph &CG, bool AlwaysInlineOnly) { 643f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth SmallVector<CallGraphNode*, 16> FunctionsToRemove; 6443e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner 6453e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner // Scan for all of the functions, looking for ones that should now be removed 6463e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner // from the program. Insert the dead ones in the FunctionsToRemove set. 6473e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) { 6483e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner CallGraphNode *CGN = I->second; 649135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner Function *F = CGN->getFunction(); 650f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth if (!F || F->isDeclaration()) 651f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth continue; 652f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth 653f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth // Handle the case when this function is called and we only want to care 654f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth // about always-inline functions. This is a bit of a hack to share code 655f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth // between here and the InlineAlways pass. 6566765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling if (AlwaysInlineOnly && 657831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 658831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling Attribute::AlwaysInline)) 659f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth continue; 660f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth 661135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // If the only remaining users of the function are dead constants, remove 662135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // them. 663135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner F->removeDeadConstantUsers(); 664135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 665c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman if (!F->isDefTriviallyDead()) 666135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner continue; 667135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 668135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Remove any call graph edges from the function to its callees. 669135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CGN->removeAllCalledFunctions(); 670135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner 671135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Remove any edges from the external node to the function's call graph 672135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // node. These edges might have been made irrelegant due to 673135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // optimization of the program. 674135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN); 675fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 676135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // Removing the node for callee from the call graph and delete it. 677f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth FunctionsToRemove.push_back(CGN); 67868d57e7ae80044401efd889270a12c71b3efb9abChris Lattner } 679f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth if (FunctionsToRemove.empty()) 680f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth return false; 6813e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner 6823e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner // Now that we know which functions to delete, do so. We didn't want to do 6833e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner // this inline, because that would invalidate our CallGraph::iterator 6843e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner // objects. :( 685135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // 686f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth // Note that it doesn't matter that we are iterating over a non-stable order 687135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // here to do this, it doesn't matter which order the functions are deleted 688135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner // in. 689dafe48e230916ce0de4228d81dece732159994f1Chandler Carruth array_pod_sort(FunctionsToRemove.begin(), FunctionsToRemove.end()); 690f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth FunctionsToRemove.erase(std::unique(FunctionsToRemove.begin(), 691f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth FunctionsToRemove.end()), 692f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth FunctionsToRemove.end()); 693f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth for (SmallVectorImpl<CallGraphNode *>::iterator I = FunctionsToRemove.begin(), 694f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth E = FunctionsToRemove.end(); 695f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth I != E; ++I) { 6963e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner delete CG.removeFunctionFromModule(*I); 6973e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner ++NumDeleted; 6983e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner } 699f91f5af802bd4487c49ee17cd0d3e46c6456263eChandler Carruth return true; 70068d57e7ae80044401efd889270a12c71b3efb9abChris Lattner} 701