Inliner.cpp revision 570a4a5d9ca31f276a67502d1e0533d59d331fea
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"
21e4aeec003f82a5263ffb168e175e6fca8b6f681dDan Gohman#include "llvm/Analysis/InlineCost.h"
22ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner#include "llvm/Target/TargetData.h"
236f7426ec2e46bb19cc9f9e75f1c355b35cf12d7dTanya Lattner#include "llvm/Transforms/IPO/InlinerPass.h"
24237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Transforms/Utils/Cloning.h"
254ff4141a9ee9ce705f2a709f3372acaf58d86ea3Chris Lattner#include "llvm/Transforms/Utils/Local.h"
264ff4141a9ee9ce705f2a709f3372acaf58d86ea3Chris Lattner#include "llvm/Support/CallSite.h"
27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
28551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h"
29ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar#include "llvm/Support/raw_ostream.h"
3012f0babca4459c253675700e1d707652d5b6ba17Chris Lattner#include "llvm/ADT/SmallPtrSet.h"
31551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h"
32befa499d45ffcc32bd9902518aec18589464e47cChris Lattner#include <set>
33a51bcb50b0c74adc741361824ef81dbefb715c53Chris Lattnerusing namespace llvm;
34d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
3586453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumInlined, "Number of functions inlined");
36dbab4dc942e0c3286415908762de71a9447f9dfaChris LattnerSTATISTIC(NumCallsDeleted, "Number of call sites deleted, not inlined");
3786453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumDeleted, "Number of functions deleted because all callers found");
38199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris LattnerSTATISTIC(NumMergedAllocas, "Number of allocas merged together");
3986453c52ba02e743d29c08456e51006500041456Chris Lattner
40844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic cl::opt<int>
41f9c3b228e5579e0d2a9cd05a2191fe17b4c58b23Jakob Stoklund OlesenInlineLimit("inline-threshold", cl::Hidden, cl::init(225), cl::ZeroOrMore,
42f9c3b228e5579e0d2a9cd05a2191fe17b4c58b23Jakob Stoklund Olesen        cl::desc("Control the amount of inlining to perform (default = 225)"));
43237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
44570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesenstatic cl::opt<bool>
45570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund OlesenRespectHint("respect-inlinehint", cl::Hidden,
46570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen            cl::desc("Respect the inlinehint attribute"));
47570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen
48570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen// Threshold to use when inlinehint is given.
49570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesenconst int HintThreshold = 300;
50570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen
51570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen// Threshold to use when optsize is specified (and there is no -inline-limit).
52570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesenconst int OptSizeThreshold = 75;
53570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen
54ae73dc1448d25b02cabc7c64c86c64371453dda8Dan GohmanInliner::Inliner(void *ID)
55ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman  : CallGraphSCCPass(ID), InlineThreshold(InlineLimit) {}
56237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
57ae73dc1448d25b02cabc7c64c86c64371453dda8Dan GohmanInliner::Inliner(void *ID, int Threshold)
58ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman  : CallGraphSCCPass(ID), InlineThreshold(Threshold) {}
59120d053e3ba810b44047fbcb719824bed5673ca9Chris Lattner
60ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// getAnalysisUsage - For this class, we declare that we require and preserve
61ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// the call graph.  If the derived class implements this method, it should
62ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// always explicitly call the implementation here.
63ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattnervoid Inliner::getAnalysisUsage(AnalysisUsage &Info) const {
64ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner  CallGraphSCCPass::getAnalysisUsage(Info);
65ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner}
66ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner
67199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
68199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattnertypedef DenseMap<const ArrayType*, std::vector<AllocaInst*> >
69199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris LattnerInlinedArrayAllocasTy;
70199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
71199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// InlineCallIfPossible - If it is possible to inline the specified call site,
72199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// do so and update the CallGraph for this operation.
73199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner///
74199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// This function also does some basic book-keeping to update the IR.  The
75cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// InlinedArrayAllocas map keeps track of any allocas that are already
76cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// available from other  functions inlined into the caller.  If we are able to
77cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// inline this call site we attempt to reuse already available allocas or add
78cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// any new allocas to the set if not possible.
79199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattnerstatic bool InlineCallIfPossible(CallSite CS, CallGraph &CG,
80199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner                                 const TargetData *TD,
81199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner                                 InlinedArrayAllocasTy &InlinedArrayAllocas) {
82befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  Function *Callee = CS.getCalledFunction();
8366c75aaa028683c389c55b377ee2411b61081677Bill Wendling  Function *Caller = CS.getCaller();
8466c75aaa028683c389c55b377ee2411b61081677Bill Wendling
85199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // Try to inline the function.  Get the list of static allocas that were
86199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // inlined.
87199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  SmallVector<AllocaInst*, 16> StaticAllocas;
88199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  if (!InlineFunction(CS, &CG, TD, &StaticAllocas))
8912f0babca4459c253675700e1d707652d5b6ba17Chris Lattner    return false;
90fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
918c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  // If the inlined function had a higher stack protection level than the
928c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  // calling function, then bump up the caller's stack protection level.
938c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  if (Callee->hasFnAttr(Attribute::StackProtectReq))
948c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling    Caller->addFnAttr(Attribute::StackProtectReq);
958c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  else if (Callee->hasFnAttr(Attribute::StackProtect) &&
968c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling           !Caller->hasFnAttr(Attribute::StackProtectReq))
978c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling    Caller->addFnAttr(Attribute::StackProtect);
988c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling
99199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
100199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // Look at all of the allocas that we inlined through this call site.  If we
101199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // have already inlined other allocas through other calls into this function,
102199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // then we know that they have disjoint lifetimes and that we can merge them.
103199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  //
104199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // There are many heuristics possible for merging these allocas, and the
105199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // different options have different tradeoffs.  One thing that we *really*
106199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // don't want to hurt is SRoA: once inlining happens, often allocas are no
107199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // longer address taken and so they can be promoted.
108199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  //
109199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // Our "solution" for that is to only merge allocas whose outermost type is an
110199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // array type.  These are usually not promoted because someone is using a
111199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // variable index into them.  These are also often the most important ones to
112199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // merge.
113199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  //
114199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // A better solution would be to have real memory lifetime markers in the IR
115199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // and not have the inliner do any merging of allocas at all.  This would
116199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // allow the backend to do proper stack slot coloring of all allocas that
117199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // *actually make it to the backend*, which is really what we want.
118199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  //
119199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // Because we don't have this information, we do this simple and useful hack.
120199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  //
121199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  SmallPtrSet<AllocaInst*, 16> UsedAllocas;
122199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
123199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // Loop over all the allocas we have so far and see if they can be merged with
124199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // a previously inlined alloca.  If not, remember that we had it.
125199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  for (unsigned AllocaNo = 0, e = StaticAllocas.size();
126199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner       AllocaNo != e; ++AllocaNo) {
127199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    AllocaInst *AI = StaticAllocas[AllocaNo];
128199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
129199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // Don't bother trying to merge array allocations (they will usually be
130199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // canonicalized to be an allocation *of* an array), or allocations whose
131199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // type is not itself an array (because we're afraid of pessimizing SRoA).
132199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    const ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType());
133199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    if (ATy == 0 || AI->isArrayAllocation())
134199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      continue;
135199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
136199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // Get the list of all available allocas for this array type.
137199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    std::vector<AllocaInst*> &AllocasForType = InlinedArrayAllocas[ATy];
138199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
139199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // Loop over the allocas in AllocasForType to see if we can reuse one.  Note
140199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // that we have to be careful not to reuse the same "available" alloca for
141199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // multiple different allocas that we just inlined, we use the 'UsedAllocas'
142199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // set to keep track of which "available" allocas are being used by this
143199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // function.  Also, AllocasForType can be empty of course!
144199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    bool MergedAwayAlloca = false;
145199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    for (unsigned i = 0, e = AllocasForType.size(); i != e; ++i) {
146199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      AllocaInst *AvailableAlloca = AllocasForType[i];
147199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
148199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // The available alloca has to be in the right function, not in some other
149199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // function in this SCC.
150199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      if (AvailableAlloca->getParent() != AI->getParent())
151199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        continue;
152199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
153199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // If the inlined function already uses this alloca then we can't reuse
154199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // it.
155199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      if (!UsedAllocas.insert(AvailableAlloca))
156199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        continue;
157199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
158199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare
159199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // success!
160c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene      DEBUG(dbgs() << "    ***MERGED ALLOCA: " << *AI);
161199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
162199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      AI->replaceAllUsesWith(AvailableAlloca);
163199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      AI->eraseFromParent();
164199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      MergedAwayAlloca = true;
165199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      ++NumMergedAllocas;
166199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      break;
167199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    }
168fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
169199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // If we already nuked the alloca, we're done with it.
170199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    if (MergedAwayAlloca)
171199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      continue;
1721f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen
173199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // If we were unable to merge away the alloca either because there are no
174199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // allocas of the right type available or because we reused them all
175199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // already, remember that this alloca came from an inlined function and mark
176199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // it used so we don't reuse it for other allocas from this inline
177199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // operation.
178199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    AllocasForType.push_back(AI);
179199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    UsedAllocas.insert(AI);
180befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  }
181199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
182befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  return true;
183237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner}
184f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen
185570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesenunsigned Inliner::getInlineThreshold(CallSite CS) const {
186570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen  // Listen to inlinehint when -respect-inlinehint is given.
187570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen  Function *Callee = CS.getCalledFunction();
188570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen  if (RespectHint && Callee && !Callee->isDeclaration() &&
189570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen      Callee->hasFnAttr(Attribute::InlineHint))
190570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen    return HintThreshold;
191570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen
192570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen  // Listen to optsize when -inline-limit is not given.
193570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen  Function *Caller = CS.getCaller();
194f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen  if (Caller && !Caller->isDeclaration() &&
195f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen      Caller->hasFnAttr(Attribute::OptimizeForSize) &&
196f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen      InlineLimit.getNumOccurrences() == 0)
197570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen      return OptSizeThreshold;
198570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen
199570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen  return InlineThreshold;
200f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen}
201f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen
2021a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar/// shouldInline - Return true if the inliner should attempt to inline
2031a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar/// at the given CallSite.
2041a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbarbool Inliner::shouldInline(CallSite CS) {
205c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  InlineCost IC = getInlineCost(CS);
2061a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar
207c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  if (IC.isAlways()) {
208c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene    DEBUG(dbgs() << "    Inlining: cost=always"
20984a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling          << ", Call: " << *CS.getInstruction() << "\n");
210c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar    return true;
211c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  }
212c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar
213c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  if (IC.isNever()) {
214c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene    DEBUG(dbgs() << "    NOT Inlining: cost=never"
21584a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling          << ", Call: " << *CS.getInstruction() << "\n");
216c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar    return false;
217c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  }
218c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar
219c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  int Cost = IC.getValue();
220e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  Function *Caller = CS.getCaller();
221570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen  int CurrentThreshold = getInlineThreshold(CS);
222135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  float FudgeFactor = getInlineFudgeFactor(CS);
2231a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  if (Cost >= (int)(CurrentThreshold * FudgeFactor)) {
224c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene    DEBUG(dbgs() << "    NOT Inlining: cost=" << Cost
22584a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling          << ", Call: " << *CS.getInstruction() << "\n");
2261a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar    return false;
2271a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  }
228135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
229e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  // Try to detect the case where the current inlining candidate caller
230e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  // (call it B) is a static function and is an inlining candidate elsewhere,
231e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  // and the current candidate callee (call it C) is large enough that
232e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  // inlining it into B would make B too big to inline later.  In these
233e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  // circumstances it may be best not to inline C into B, but to inline B
234e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  // into its callers.
235e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  if (Caller->hasLocalLinkage()) {
236e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    int TotalSecondaryCost = 0;
237e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    bool outerCallsFound = false;
238e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    bool allOuterCallsWillBeInlined = true;
239e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    bool someOuterCallWouldNotBeInlined = false;
240e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    for (Value::use_iterator I = Caller->use_begin(), E =Caller->use_end();
241e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen         I != E; ++I) {
242e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      CallSite CS2 = CallSite::get(*I);
243e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
244e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      // If this isn't a call to Caller (it could be some other sort
245e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      // of reference) skip it.
246e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      if (CS2.getInstruction() == 0 || CS2.getCalledFunction() != Caller)
247e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        continue;
248e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
249e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      InlineCost IC2 = getInlineCost(CS2);
250e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      if (IC2.isNever())
251e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        allOuterCallsWillBeInlined = false;
252e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      if (IC2.isAlways() || IC2.isNever())
253e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        continue;
254e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
255e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      outerCallsFound = true;
256e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      int Cost2 = IC2.getValue();
257570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen      int CurrentThreshold2 = getInlineThreshold(CS2);
258e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      float FudgeFactor2 = getInlineFudgeFactor(CS2);
259e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
260e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      if (Cost2 >= (int)(CurrentThreshold2 * FudgeFactor2))
261e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        allOuterCallsWillBeInlined = false;
262e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
263bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen      // See if we have this case.  We subtract off the penalty
264e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      // for the call instruction, which we would be deleting.
265e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      if (Cost2 < (int)(CurrentThreshold2 * FudgeFactor2) &&
266bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen          Cost2 + Cost - (InlineConstants::CallPenalty + 1) >=
267bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen                (int)(CurrentThreshold2 * FudgeFactor2)) {
268e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        someOuterCallWouldNotBeInlined = true;
269e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        TotalSecondaryCost += Cost2;
270e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      }
271e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    }
272e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    // If all outer calls to Caller would get inlined, the cost for the last
273e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    // one is set very low by getInlineCost, in anticipation that Caller will
274e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    // be removed entirely.  We did not account for this above unless there
275e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    // is only one caller of Caller.
276e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    if (allOuterCallsWillBeInlined && Caller->use_begin() != Caller->use_end())
277bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen      TotalSecondaryCost += InlineConstants::LastCallToStaticBonus;
278e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
279e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    if (outerCallsFound && someOuterCallWouldNotBeInlined &&
280e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        TotalSecondaryCost < Cost) {
281c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene      DEBUG(dbgs() << "    NOT Inlining: " << *CS.getInstruction() <<
282e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen           " Cost = " << Cost <<
283e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen           ", outer Cost = " << TotalSecondaryCost << '\n');
284e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      return false;
285e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    }
286e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  }
287e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
288c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene  DEBUG(dbgs() << "    Inlining: cost=" << Cost
289e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        << ", Call: " << *CS.getInstruction() << '\n');
290135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  return true;
2911a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar}
292237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
2935095e3d1d1caef8d573534d369e37277c623064cChris Lattnerbool Inliner::runOnSCC(std::vector<CallGraphNode*> &SCC) {
294237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner  CallGraph &CG = getAnalysis<CallGraph>();
29502a436c48ecff9e34d50ce0a2f861e5acdd9bf3fDan Gohman  const TargetData *TD = getAnalysisIfAvailable<TargetData>();
296237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
29716581bf931c0ccf2f8993397acfa4e1d509a68dcDale Johannesen  SmallPtrSet<Function*, 8> SCCFunctions;
298c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene  DEBUG(dbgs() << "Inliner visiting SCC:");
299237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner  for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
300befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    Function *F = SCC[i]->getFunction();
301befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    if (F) SCCFunctions.insert(F);
302c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene    DEBUG(dbgs() << " " << (F ? F->getName() : "INDIRECTNODE"));
303237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner  }
304237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
305befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // Scan through and identify all call sites ahead of time so that we only
306befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // inline call sites in the original functions, not call sites that result
307befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // from inlining other functions.
308199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  SmallVector<CallSite, 16> CallSites;
309befa499d45ffcc32bd9902518aec18589464e47cChris Lattner
310135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
311135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    Function *F = SCC[i]->getFunction();
312135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (!F) continue;
313135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
314135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
315135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
316135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        CallSite CS = CallSite::get(I);
317e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        // If this isn't a call, or it is a call to an intrinsic, it can
318d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        // never be inlined.
319d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        if (CS.getInstruction() == 0 || isa<IntrinsicInst>(I))
320135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner          continue;
321135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
322d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        // If this is a direct call to an external function, we can never inline
323d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        // it.  If it is an indirect call, inlining may resolve it to be a
324d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        // direct call, so we keep it.
325d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        if (CS.getCalledFunction() && CS.getCalledFunction()->isDeclaration())
326d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner          continue;
327d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner
328d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        CallSites.push_back(CS);
329135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      }
330135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  }
331237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
332c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene  DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n");
333fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
334befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // Now that we have all of the call sites, move the ones to functions in the
335befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // current SCC to the end of the list.
336befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  unsigned FirstCallInSCC = CallSites.size();
337befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  for (unsigned i = 0; i < FirstCallInSCC; ++i)
338befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    if (Function *F = CallSites[i].getCalledFunction())
339befa499d45ffcc32bd9902518aec18589464e47cChris Lattner      if (SCCFunctions.count(F))
340befa499d45ffcc32bd9902518aec18589464e47cChris Lattner        std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
341fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
342199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
343199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  InlinedArrayAllocasTy InlinedArrayAllocas;
344199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
345befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // Now that we have all of the call sites, loop over them and inline them if
346befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // it looks profitable to do so.
347befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  bool Changed = false;
348befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  bool LocalChange;
349befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  do {
350befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    LocalChange = false;
351befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    // Iterate over the outer loop because inlining functions can cause indirect
352befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    // calls to become direct calls.
353135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) {
354199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      CallSite CS = CallSites[CSi];
355199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
356dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      Function *Caller = CS.getCaller();
357199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      Function *Callee = CS.getCalledFunction();
358dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner
359dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // If this call site is dead and it is to a readonly function, we should
360dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // just delete the call instead of trying to inline it, regardless of
361dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // size.  This happens because IPSCCP propagates the result out of the
362dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // call and then we're left with the dead call.
3634ff4141a9ee9ce705f2a709f3372acaf58d86ea3Chris Lattner      if (isInstructionTriviallyDead(CS.getInstruction())) {
364c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene        DEBUG(dbgs() << "    -> Deleting dead call: "
365dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner                     << *CS.getInstruction() << "\n");
366dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        // Update the call graph by deleting the edge from Callee to Caller.
367dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        CG[Caller]->removeCallEdgeFor(CS);
368dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        CS.getInstruction()->eraseFromParent();
369dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        ++NumCallsDeleted;
370dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      } else {
371dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        // We can only inline direct calls to non-declarations.
372dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        if (Callee == 0 || Callee->isDeclaration()) continue;
373135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
374dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        // If the policy determines that we should inline this function,
375dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        // try to do so.
376dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        if (!shouldInline(CS))
377dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner          continue;
378e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
379dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        // Attempt to inline the function...
380dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        if (!InlineCallIfPossible(CS, CG, TD, InlinedArrayAllocas))
381dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner          continue;
382dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        ++NumInlined;
383dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      }
384135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
385dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // If we inlined or deleted the last possible call site to the function,
386dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // delete the function body now.
387dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      if (Callee && Callee->use_empty() && Callee->hasLocalLinkage() &&
388d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner          // TODO: Can remove if in SCC now.
389b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner          !SCCFunctions.count(Callee) &&
390d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner
391b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner          // The function may be apparently dead, but if there are indirect
392b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner          // callgraph references to the node, we cannot delete it yet, this
393b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner          // could invalidate the CGSCC iterator.
394b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner          CG[Callee]->getNumReferences() == 0) {
395c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene        DEBUG(dbgs() << "    -> Deleting dead function: "
396199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner              << Callee->getName() << "\n");
397199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        CallGraphNode *CalleeNode = CG[Callee];
398199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
399199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        // Remove any call graph edges from the callee to its callees.
400199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        CalleeNode->removeAllCalledFunctions();
401199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
402199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        resetCachedCostInfo(Callee);
403199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
404199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        // Removing the node for callee from the call graph and delete it.
405199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        delete CG.removeFunctionFromModule(CalleeNode);
406199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        ++NumDeleted;
407199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      }
408199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
409135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Remove any cached cost info for this caller, as inlining the
410135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // callee has increased the size of the caller (which may be the
411135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // same as the callee).
412135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      resetCachedCostInfo(Caller);
413135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
414135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Remove this call site from the list.  If possible, use
415135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // swap/pop_back for efficiency, but do not use it if doing so would
416135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // move a call site to a function in this SCC before the
417135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // 'FirstCallInSCC' barrier.
418135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      if (SCC.size() == 1) {
419135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        std::swap(CallSites[CSi], CallSites.back());
420135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        CallSites.pop_back();
421135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      } else {
422135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        CallSites.erase(CallSites.begin()+CSi);
423237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner      }
424135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      --CSi;
425135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
426135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      Changed = true;
427135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      LocalChange = true;
428135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    }
429befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  } while (LocalChange);
430237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
431775cbdd51a3b33dd5eb343689f65ab5cc8ac7118Chris Lattner  return Changed;
432237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner}
433d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
43468d57e7ae80044401efd889270a12c71b3efb9abChris Lattner// doFinalization - Remove now-dead linkonce functions at the end of
43568d57e7ae80044401efd889270a12c71b3efb9abChris Lattner// processing to avoid breaking the SCC traversal.
43668d57e7ae80044401efd889270a12c71b3efb9abChris Lattnerbool Inliner::doFinalization(CallGraph &CG) {
437b7c6bf1e073088635951435acedff793add1cefdDevang Patel  return removeDeadFunctions(CG);
438b7c6bf1e073088635951435acedff793add1cefdDevang Patel}
439b7c6bf1e073088635951435acedff793add1cefdDevang Patel
440135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// removeDeadFunctions - Remove dead functions that are not included in
441135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// DNR (Do Not Remove) list.
442b7c6bf1e073088635951435acedff793add1cefdDevang Patelbool Inliner::removeDeadFunctions(CallGraph &CG,
443135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner                                  SmallPtrSet<const Function *, 16> *DNR) {
444135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  SmallPtrSet<CallGraphNode*, 16> FunctionsToRemove;
4453e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner
4463e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // Scan for all of the functions, looking for ones that should now be removed
4473e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // from the program.  Insert the dead ones in the FunctionsToRemove set.
4483e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
4493e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    CallGraphNode *CGN = I->second;
450b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner    if (CGN->getFunction() == 0)
451135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
452135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
453135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    Function *F = CGN->getFunction();
454135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
455135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // If the only remaining users of the function are dead constants, remove
456135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // them.
457135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    F->removeDeadConstantUsers();
458135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
459135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (DNR && DNR->count(F))
460135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
461b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner    if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
462b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner        !F->hasAvailableExternallyLinkage())
463135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
464135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (!F->use_empty())
465135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
466135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
467135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Remove any call graph edges from the function to its callees.
468135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    CGN->removeAllCalledFunctions();
469135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
470135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Remove any edges from the external node to the function's call graph
471135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // node.  These edges might have been made irrelegant due to
472135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // optimization of the program.
473135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
474fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
475135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Removing the node for callee from the call graph and delete it.
476135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    FunctionsToRemove.insert(CGN);
47768d57e7ae80044401efd889270a12c71b3efb9abChris Lattner  }
4783e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner
4793e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // Now that we know which functions to delete, do so.  We didn't want to do
4803e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // this inline, because that would invalidate our CallGraph::iterator
4813e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // objects. :(
482135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  //
483135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // Note that it doesn't matter that we are iterating over a non-stable set
484135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // here to do this, it doesn't matter which order the functions are deleted
485135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // in.
4863e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  bool Changed = false;
487135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  for (SmallPtrSet<CallGraphNode*, 16>::iterator I = FunctionsToRemove.begin(),
488135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner       E = FunctionsToRemove.end(); I != E; ++I) {
4891f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen    resetCachedCostInfo((*I)->getFunction());
4903e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    delete CG.removeFunctionFromModule(*I);
4913e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    ++NumDeleted;
4923e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    Changed = true;
4933e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  }
4943e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner
49568d57e7ae80044401efd889270a12c71b3efb9abChris Lattner  return Changed;
49668d57e7ae80044401efd889270a12c71b3efb9abChris Lattner}
497