Inliner.cpp revision b16117c368ad4e6d004ac912549b2c6ed06731a5
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"
226c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth#include "llvm/Analysis/InstructionSimplify.h"
23ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner#include "llvm/Target/TargetData.h"
246f7426ec2e46bb19cc9f9e75f1c355b35cf12d7dTanya Lattner#include "llvm/Transforms/IPO/InlinerPass.h"
25237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Transforms/Utils/Cloning.h"
264ff4141a9ee9ce705f2a709f3372acaf58d86ea3Chris Lattner#include "llvm/Transforms/Utils/Local.h"
274ff4141a9ee9ce705f2a709f3372acaf58d86ea3Chris Lattner#include "llvm/Support/CallSite.h"
28551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
29551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h"
30ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar#include "llvm/Support/raw_ostream.h"
3112f0babca4459c253675700e1d707652d5b6ba17Chris Lattner#include "llvm/ADT/SmallPtrSet.h"
32551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h"
33a51bcb50b0c74adc741361824ef81dbefb715c53Chris Lattnerusing namespace llvm;
34d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
3586453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumInlined, "Number of functions inlined");
3683f66fe6144c2041f1f7897f7015b0e2e68faad3Chris 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
44f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesenstatic cl::opt<int>
45f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund OlesenHintThreshold("inlinehint-threshold", cl::Hidden, cl::init(325),
46f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen              cl::desc("Threshold for inlining functions with inline hint"));
47570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen
48570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen// Threshold to use when optsize is specified (and there is no -inline-limit).
49570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesenconst int OptSizeThreshold = 75;
50570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen
5190c579de5a383cee278acc3f7e7b9d0a656e6a35Owen AndersonInliner::Inliner(char &ID)
52fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier  : CallGraphSCCPass(ID), InlineThreshold(InlineLimit), InsertLifetime(true) {}
53237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
54fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad RosierInliner::Inliner(char &ID, int Threshold, bool InsertLifetime)
55930f5efac0f76aa9e3772d9a36757f18b3573112Jakob Stoklund Olesen  : CallGraphSCCPass(ID), InlineThreshold(InlineLimit.getNumOccurrences() > 0 ?
56fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier                                          InlineLimit : Threshold),
57fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier    InsertLifetime(InsertLifetime) {}
58120d053e3ba810b44047fbcb719824bed5673ca9Chris Lattner
59ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// getAnalysisUsage - For this class, we declare that we require and preserve
60ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// the call graph.  If the derived class implements this method, it should
61ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// always explicitly call the implementation here.
62ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattnervoid Inliner::getAnalysisUsage(AnalysisUsage &Info) const {
63ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner  CallGraphSCCPass::getAnalysisUsage(Info);
64ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner}
65ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner
66199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
67db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnertypedef DenseMap<ArrayType*, std::vector<AllocaInst*> >
68199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris LattnerInlinedArrayAllocasTy;
69199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
70199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// InlineCallIfPossible - If it is possible to inline the specified call site,
71199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// do so and update the CallGraph for this operation.
72199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner///
73199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner/// This function also does some basic book-keeping to update the IR.  The
74cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// InlinedArrayAllocas map keeps track of any allocas that are already
75cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// available from other  functions inlined into the caller.  If we are able to
76cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// inline this call site we attempt to reuse already available allocas or add
77cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner/// any new allocas to the set if not possible.
7860915146f4d35e12f10dcdaa155596fac79184daChris Lattnerstatic bool InlineCallIfPossible(CallSite CS, InlineFunctionInfo &IFI,
796c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner                                 InlinedArrayAllocasTy &InlinedArrayAllocas,
80fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier                                 int InlineHistory, bool InsertLifetime) {
81befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  Function *Callee = CS.getCalledFunction();
8266c75aaa028683c389c55b377ee2411b61081677Bill Wendling  Function *Caller = CS.getCaller();
8366c75aaa028683c389c55b377ee2411b61081677Bill Wendling
84199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // Try to inline the function.  Get the list of static allocas that were
85199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // inlined.
86fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier  if (!InlineFunction(CS, IFI, InsertLifetime))
8712f0babca4459c253675700e1d707652d5b6ba17Chris Lattner    return false;
88fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
898c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  // If the inlined function had a higher stack protection level than the
908c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  // calling function, then bump up the caller's stack protection level.
918c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  if (Callee->hasFnAttr(Attribute::StackProtectReq))
928c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling    Caller->addFnAttr(Attribute::StackProtectReq);
938c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  else if (Callee->hasFnAttr(Attribute::StackProtect) &&
948c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling           !Caller->hasFnAttr(Attribute::StackProtectReq))
958c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling    Caller->addFnAttr(Attribute::StackProtect);
968c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling
97199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // Look at all of the allocas that we inlined through this call site.  If we
98199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // have already inlined other allocas through other calls into this function,
99199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // then we know that they have disjoint lifetimes and that we can merge them.
100199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  //
101199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // There are many heuristics possible for merging these allocas, and the
102199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // different options have different tradeoffs.  One thing that we *really*
103199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // don't want to hurt is SRoA: once inlining happens, often allocas are no
104199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // longer address taken and so they can be promoted.
105199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  //
106199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // Our "solution" for that is to only merge allocas whose outermost type is an
107199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // array type.  These are usually not promoted because someone is using a
108199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // variable index into them.  These are also often the most important ones to
109199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // merge.
110199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  //
111199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // A better solution would be to have real memory lifetime markers in the IR
112199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // and not have the inliner do any merging of allocas at all.  This would
113199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // allow the backend to do proper stack slot coloring of all allocas that
114199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // *actually make it to the backend*, which is really what we want.
115199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  //
116199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // Because we don't have this information, we do this simple and useful hack.
117199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  //
118199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  SmallPtrSet<AllocaInst*, 16> UsedAllocas;
119199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
1206c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  // When processing our SCC, check to see if CS was inlined from some other
1216c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  // call site.  For example, if we're processing "A" in this code:
1226c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  //   A() { B() }
1236c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  //   B() { x = alloca ... C() }
1246c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  //   C() { y = alloca ... }
1256c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  // Assume that C was not inlined into B initially, and so we're processing A
1266c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  // and decide to inline B into A.  Doing this makes an alloca available for
1276c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  // reuse and makes a callsite (C) available for inlining.  When we process
1286c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  // the C call site we don't want to do any alloca merging between X and Y
1296c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  // because their scopes are not disjoint.  We could make this smarter by
1306c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  // keeping track of the inline history for each alloca in the
1316c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  // InlinedArrayAllocas but this isn't likely to be a significant win.
1326c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner  if (InlineHistory != -1)  // Only do merging for top-level call sites in SCC.
1336c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner    return true;
1346c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner
135199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // Loop over all the allocas we have so far and see if they can be merged with
136199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  // a previously inlined alloca.  If not, remember that we had it.
13760915146f4d35e12f10dcdaa155596fac79184daChris Lattner  for (unsigned AllocaNo = 0, e = IFI.StaticAllocas.size();
138199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner       AllocaNo != e; ++AllocaNo) {
13960915146f4d35e12f10dcdaa155596fac79184daChris Lattner    AllocaInst *AI = IFI.StaticAllocas[AllocaNo];
140199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
141199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // Don't bother trying to merge array allocations (they will usually be
142199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // canonicalized to be an allocation *of* an array), or allocations whose
143199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // type is not itself an array (because we're afraid of pessimizing SRoA).
144db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType());
145199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    if (ATy == 0 || AI->isArrayAllocation())
146199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      continue;
147199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
148199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // Get the list of all available allocas for this array type.
149199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    std::vector<AllocaInst*> &AllocasForType = InlinedArrayAllocas[ATy];
150199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
151199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // Loop over the allocas in AllocasForType to see if we can reuse one.  Note
152199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // that we have to be careful not to reuse the same "available" alloca for
153199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // multiple different allocas that we just inlined, we use the 'UsedAllocas'
154199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // set to keep track of which "available" allocas are being used by this
155199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // function.  Also, AllocasForType can be empty of course!
156199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    bool MergedAwayAlloca = false;
157199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    for (unsigned i = 0, e = AllocasForType.size(); i != e; ++i) {
158199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      AllocaInst *AvailableAlloca = AllocasForType[i];
159199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
160199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // The available alloca has to be in the right function, not in some other
161199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // function in this SCC.
162199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      if (AvailableAlloca->getParent() != AI->getParent())
163199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        continue;
164199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
165199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // If the inlined function already uses this alloca then we can't reuse
166199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // it.
167199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      if (!UsedAllocas.insert(AvailableAlloca))
168199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        continue;
169199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
170199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare
171199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      // success!
1727d32b8032d7ec2472b994aab2ac3459e8d47c496Chris Lattner      DEBUG(dbgs() << "    ***MERGED ALLOCA: " << *AI << "\n\t\tINTO: "
1737d32b8032d7ec2472b994aab2ac3459e8d47c496Chris Lattner                   << *AvailableAlloca << '\n');
174199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
175199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      AI->replaceAllUsesWith(AvailableAlloca);
176199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      AI->eraseFromParent();
177199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      MergedAwayAlloca = true;
178199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      ++NumMergedAllocas;
1797d32b8032d7ec2472b994aab2ac3459e8d47c496Chris Lattner      IFI.StaticAllocas[AllocaNo] = 0;
180199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      break;
181199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    }
182fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
183199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // If we already nuked the alloca, we're done with it.
184199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    if (MergedAwayAlloca)
185199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      continue;
1867d32b8032d7ec2472b994aab2ac3459e8d47c496Chris Lattner
187199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // If we were unable to merge away the alloca either because there are no
188199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // allocas of the right type available or because we reused them all
189199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // already, remember that this alloca came from an inlined function and mark
190199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // it used so we don't reuse it for other allocas from this inline
191199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    // operation.
192199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    AllocasForType.push_back(AI);
193199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner    UsedAllocas.insert(AI);
194befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  }
195199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
196befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  return true;
197237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner}
198f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen
199570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesenunsigned Inliner::getInlineThreshold(CallSite CS) const {
200f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen  int thres = InlineThreshold;
201570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen
202570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen  // Listen to optsize when -inline-limit is not given.
203570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen  Function *Caller = CS.getCaller();
204f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen  if (Caller && !Caller->isDeclaration() &&
205f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen      Caller->hasFnAttr(Attribute::OptimizeForSize) &&
206f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen      InlineLimit.getNumOccurrences() == 0)
207f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen    thres = OptSizeThreshold;
208f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen
209f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen  // Listen to inlinehint when it would increase the threshold.
210f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen  Function *Callee = CS.getCalledFunction();
211f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen  if (HintThreshold > thres && Callee && !Callee->isDeclaration() &&
212f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen      Callee->hasFnAttr(Attribute::InlineHint))
213f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen    thres = HintThreshold;
214570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen
215f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen  return thres;
216f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen}
217f8526cb711ec96cd2d5aff30da57f65ae8e5b7b8Jakob Stoklund Olesen
2181a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar/// shouldInline - Return true if the inliner should attempt to inline
2191a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar/// at the given CallSite.
2201a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbarbool Inliner::shouldInline(CallSite CS) {
221c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  InlineCost IC = getInlineCost(CS);
2221a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar
223c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  if (IC.isAlways()) {
224c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene    DEBUG(dbgs() << "    Inlining: cost=always"
22584a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling          << ", Call: " << *CS.getInstruction() << "\n");
226c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar    return true;
227c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  }
228c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar
229c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  if (IC.isNever()) {
230c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene    DEBUG(dbgs() << "    NOT Inlining: cost=never"
23184a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling          << ", Call: " << *CS.getInstruction() << "\n");
232c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar    return false;
233c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  }
234c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar
235c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  int Cost = IC.getValue();
236e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  Function *Caller = CS.getCaller();
237570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen  int CurrentThreshold = getInlineThreshold(CS);
238135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  float FudgeFactor = getInlineFudgeFactor(CS);
2392ddbf8208e0ee50cb1aeb24d509b52549a85c211Jakob Stoklund Olesen  int AdjThreshold = (int)(CurrentThreshold * FudgeFactor);
2402ddbf8208e0ee50cb1aeb24d509b52549a85c211Jakob Stoklund Olesen  if (Cost >= AdjThreshold) {
241c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene    DEBUG(dbgs() << "    NOT Inlining: cost=" << Cost
2422ddbf8208e0ee50cb1aeb24d509b52549a85c211Jakob Stoklund Olesen          << ", thres=" << AdjThreshold
24384a832f9272ed7f1a47c3e019c770b62e373cc6cBill Wendling          << ", Call: " << *CS.getInstruction() << "\n");
2441a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar    return false;
2451a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  }
246135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
247b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // Try to detect the case where the current inlining candidate caller (call
248b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // it B) is a static or linkonce-ODR function and is an inlining candidate
249b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // elsewhere, and the current candidate callee (call it C) is large enough
250b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // that inlining it into B would make B too big to inline later. In these
251b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // circumstances it may be best not to inline C into B, but to inline B into
252b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // its callers.
253b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  //
254b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // This only applies to static and linkonce-ODR functions because those are
255b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // expected to be available for inlining in the translation units where they
256b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // are used. Thus we will always have the opportunity to make local inlining
257b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // decisions. Importantly the linkonce-ODR linkage covers inline functions
258b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  // and templates in C++.
259b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth  if (Caller->hasLocalLinkage() ||
260b16117c368ad4e6d004ac912549b2c6ed06731a5Chandler Carruth      Caller->getLinkage() == GlobalValue::LinkOnceODRLinkage) {
261e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    int TotalSecondaryCost = 0;
262e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    bool outerCallsFound = false;
263c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen    // This bool tracks what happens if we do NOT inline C into B.
264c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen    bool callerWillBeRemoved = true;
265c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen    // This bool tracks what happens if we DO inline C into B.
266c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen    bool inliningPreventsSomeOuterInline = false;
267e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    for (Value::use_iterator I = Caller->use_begin(), E =Caller->use_end();
268e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen         I != E; ++I) {
2697d3056b16038a6a09c452c0dfcc3c8f4e421506aGabor Greif      CallSite CS2(*I);
270e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
271e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      // If this isn't a call to Caller (it could be some other sort
272c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen      // of reference) skip it.  Such references will prevent the caller
273c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen      // from being removed.
274c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen      if (!CS2 || CS2.getCalledFunction() != Caller) {
275c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen        callerWillBeRemoved = false;
276e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        continue;
277c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen      }
278e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
279e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      InlineCost IC2 = getInlineCost(CS2);
280e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      if (IC2.isNever())
281c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen        callerWillBeRemoved = false;
282e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      if (IC2.isAlways() || IC2.isNever())
283e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        continue;
284e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
285e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      outerCallsFound = true;
286e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      int Cost2 = IC2.getValue();
287570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen      int CurrentThreshold2 = getInlineThreshold(CS2);
288e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      float FudgeFactor2 = getInlineFudgeFactor(CS2);
289e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
290e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      if (Cost2 >= (int)(CurrentThreshold2 * FudgeFactor2))
291c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen        callerWillBeRemoved = false;
292e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
293bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen      // See if we have this case.  We subtract off the penalty
294e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      // for the call instruction, which we would be deleting.
295e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      if (Cost2 < (int)(CurrentThreshold2 * FudgeFactor2) &&
296bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen          Cost2 + Cost - (InlineConstants::CallPenalty + 1) >=
297bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen                (int)(CurrentThreshold2 * FudgeFactor2)) {
298c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen        inliningPreventsSomeOuterInline = true;
299e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        TotalSecondaryCost += Cost2;
300e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      }
301e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    }
302e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    // If all outer calls to Caller would get inlined, the cost for the last
303e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    // one is set very low by getInlineCost, in anticipation that Caller will
304e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    // be removed entirely.  We did not account for this above unless there
305e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    // is only one caller of Caller.
306c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen    if (callerWillBeRemoved && Caller->use_begin() != Caller->use_end())
307bdb984bc2757114bc706026603ed40d7f508c4c1Dale Johannesen      TotalSecondaryCost += InlineConstants::LastCallToStaticBonus;
308e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
309c84e3c0c03a0dab7aea7047e7b8e38051542f7e4Dale Johannesen    if (outerCallsFound && inliningPreventsSomeOuterInline &&
310e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        TotalSecondaryCost < Cost) {
311c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene      DEBUG(dbgs() << "    NOT Inlining: " << *CS.getInstruction() <<
312e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen           " Cost = " << Cost <<
313e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen           ", outer Cost = " << TotalSecondaryCost << '\n');
314e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen      return false;
315e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen    }
316e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen  }
317e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
318c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene  DEBUG(dbgs() << "    Inlining: cost=" << Cost
3192ddbf8208e0ee50cb1aeb24d509b52549a85c211Jakob Stoklund Olesen        << ", thres=" << AdjThreshold
320e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        << ", Call: " << *CS.getInstruction() << '\n');
321135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  return true;
3221a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar}
323237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
324159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner/// InlineHistoryIncludes - Return true if the specified inline history ID
325159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner/// indicates an inline history that includes the specified function.
326159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattnerstatic bool InlineHistoryIncludes(Function *F, int InlineHistoryID,
327159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner            const SmallVectorImpl<std::pair<Function*, int> > &InlineHistory) {
328159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner  while (InlineHistoryID != -1) {
329159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner    assert(unsigned(InlineHistoryID) < InlineHistory.size() &&
330159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner           "Invalid inline history ID");
331159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner    if (InlineHistory[InlineHistoryID].first == F)
332159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner      return true;
333159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner    InlineHistoryID = InlineHistory[InlineHistoryID].second;
334159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner  }
335159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner  return false;
336159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner}
337159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner
3386c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth/// \brief Simplify arguments going into a particular callsite.
3396c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth///
3406c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth/// This is important to do each time we add a callsite due to inlining so that
3416c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth/// constants and other entities which feed into inline cost estimation are
3426c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth/// properly recognized when analyzing the new callsite. Consider:
3436c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth///   void outer(int x) {
3446c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth///     if (x < 42)
3456c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth///       return inner(42 - x);
3466c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth///     ...
3476c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth///   }
3486c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth///   void inner(int x) {
3496c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth///     ...
3506c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth///   }
3516c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth///
3526c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth/// The inliner gives calls to 'outer' with a constant argument a bonus because
3536c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth/// it will delete one side of a branch. But the resulting call to 'inner'
3546c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth/// will, after inlining, also have a constant operand. We need to do just
3556c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth/// enough constant folding to expose this for callsite arguments. The rest
3566c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth/// will be taken care of after the inliner finishes running.
3576c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruthstatic void simplifyCallSiteArguments(const TargetData *TD, CallSite CS) {
3586c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth  // FIXME: It would be nice to avoid this smallvector if RAUW doesn't
3596c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth  // invalidate operand iterators in any cases.
3606c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth  SmallVector<std::pair<Value *, Value*>, 4> SimplifiedArgs;
3616c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth  for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
3626c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth       I != E; ++I)
3636c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth    if (Instruction *Inst = dyn_cast<Instruction>(*I))
3646c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth      if (Value *SimpleArg = SimplifyInstruction(Inst, TD))
3656c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth        SimplifiedArgs.push_back(std::make_pair(Inst, SimpleArg));
3666c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth  for (unsigned Idx = 0, Size = SimplifiedArgs.size(); Idx != Size; ++Idx)
3676c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth    SimplifiedArgs[Idx].first->replaceAllUsesWith(SimplifiedArgs[Idx].second);
3686c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth}
369159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner
3702decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattnerbool Inliner::runOnSCC(CallGraphSCC &SCC) {
371237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner  CallGraph &CG = getAnalysis<CallGraph>();
37202a436c48ecff9e34d50ce0a2f861e5acdd9bf3fDan Gohman  const TargetData *TD = getAnalysisIfAvailable<TargetData>();
373237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
37416581bf931c0ccf2f8993397acfa4e1d509a68dcDale Johannesen  SmallPtrSet<Function*, 8> SCCFunctions;
375c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene  DEBUG(dbgs() << "Inliner visiting SCC:");
3762decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner  for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
3772decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner    Function *F = (*I)->getFunction();
378befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    if (F) SCCFunctions.insert(F);
379c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene    DEBUG(dbgs() << " " << (F ? F->getName() : "INDIRECTNODE"));
380237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner  }
381237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
382befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // Scan through and identify all call sites ahead of time so that we only
383befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // inline call sites in the original functions, not call sites that result
384befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // from inlining other functions.
385159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner  SmallVector<std::pair<CallSite, int>, 16> CallSites;
386159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner
387159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner  // When inlining a callee produces new call sites, we want to keep track of
388159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner  // the fact that they were inlined from the callee.  This allows us to avoid
389159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner  // infinite inlining in some obscure cases.  To represent this, we use an
390159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner  // index into the InlineHistory vector.
391159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner  SmallVector<std::pair<Function*, int>, 8> InlineHistory;
392befa499d45ffcc32bd9902518aec18589464e47cChris Lattner
3932decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner  for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
3942decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner    Function *F = (*I)->getFunction();
395135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (!F) continue;
396135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
397135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
398135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
3997d3056b16038a6a09c452c0dfcc3c8f4e421506aGabor Greif        CallSite CS(cast<Value>(I));
400e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen        // If this isn't a call, or it is a call to an intrinsic, it can
401d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        // never be inlined.
4027d3056b16038a6a09c452c0dfcc3c8f4e421506aGabor Greif        if (!CS || isa<IntrinsicInst>(I))
403135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner          continue;
404135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
405d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        // If this is a direct call to an external function, we can never inline
406d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        // it.  If it is an indirect call, inlining may resolve it to be a
407d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        // direct call, so we keep it.
408d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner        if (CS.getCalledFunction() && CS.getCalledFunction()->isDeclaration())
409d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner          continue;
410d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner
411159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner        CallSites.push_back(std::make_pair(CS, -1));
412135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      }
413135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  }
414237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
415c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene  DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n");
416fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4174471136e4db67f5b6cf064cb3b0a7668f15bfe6cChris Lattner  // If there are no calls in this function, exit early.
4184471136e4db67f5b6cf064cb3b0a7668f15bfe6cChris Lattner  if (CallSites.empty())
4194471136e4db67f5b6cf064cb3b0a7668f15bfe6cChris Lattner    return false;
4204471136e4db67f5b6cf064cb3b0a7668f15bfe6cChris Lattner
421befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // Now that we have all of the call sites, move the ones to functions in the
422befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // current SCC to the end of the list.
423befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  unsigned FirstCallInSCC = CallSites.size();
424befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  for (unsigned i = 0; i < FirstCallInSCC; ++i)
425159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner    if (Function *F = CallSites[i].first.getCalledFunction())
426befa499d45ffcc32bd9902518aec18589464e47cChris Lattner      if (SCCFunctions.count(F))
427befa499d45ffcc32bd9902518aec18589464e47cChris Lattner        std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
428fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
429199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
430199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner  InlinedArrayAllocasTy InlinedArrayAllocas;
43160915146f4d35e12f10dcdaa155596fac79184daChris Lattner  InlineFunctionInfo InlineInfo(&CG, TD);
432199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
433befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // Now that we have all of the call sites, loop over them and inline them if
434befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // it looks profitable to do so.
435befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  bool Changed = false;
436befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  bool LocalChange;
437befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  do {
438befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    LocalChange = false;
439befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    // Iterate over the outer loop because inlining functions can cause indirect
440befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    // calls to become direct calls.
441135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) {
442159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner      CallSite CS = CallSites[CSi].first;
443199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
444dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      Function *Caller = CS.getCaller();
445199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      Function *Callee = CS.getCalledFunction();
446dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner
447dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // If this call site is dead and it is to a readonly function, we should
448dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // just delete the call instead of trying to inline it, regardless of
449dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // size.  This happens because IPSCCP propagates the result out of the
450dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // call and then we're left with the dead call.
4514ff4141a9ee9ce705f2a709f3372acaf58d86ea3Chris Lattner      if (isInstructionTriviallyDead(CS.getInstruction())) {
452c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene        DEBUG(dbgs() << "    -> Deleting dead call: "
453dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner                     << *CS.getInstruction() << "\n");
454dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        // Update the call graph by deleting the edge from Callee to Caller.
455dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        CG[Caller]->removeCallEdgeFor(CS);
456dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        CS.getInstruction()->eraseFromParent();
457dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        ++NumCallsDeleted;
458f7477470d37ee2ab9075eaee4745fa084d424ab8Jakob Stoklund Olesen        // Update the cached cost info with the missing call
459f7477470d37ee2ab9075eaee4745fa084d424ab8Jakob Stoklund Olesen        growCachedCostInfo(Caller, NULL);
460dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      } else {
461dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        // We can only inline direct calls to non-declarations.
462dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        if (Callee == 0 || Callee->isDeclaration()) continue;
463135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
464f0193ed8decb2e78d8d5ec4a4eaeed8f3036bf6eEric Christopher        // If this call site was obtained by inlining another function, verify
465159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner        // that the include path for the function did not include the callee
4667d32b8032d7ec2472b994aab2ac3459e8d47c496Chris Lattner        // itself.  If so, we'd be recursively inlining the same function,
467159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner        // which would provide the same callsites, which would cause us to
468159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner        // infinitely inline.
469159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner        int InlineHistoryID = CallSites[CSi].second;
470159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner        if (InlineHistoryID != -1 &&
471159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner            InlineHistoryIncludes(Callee, InlineHistoryID, InlineHistory))
472159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner          continue;
473159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner
474159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner
475dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        // If the policy determines that we should inline this function,
476dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        // try to do so.
477dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        if (!shouldInline(CS))
478dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner          continue;
479e91b9a3b59688023e20cee8441179300b87c844eDale Johannesen
480fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner        // Attempt to inline the function.
4816c3ee0f3c9684e588c8852d90c891d6354175c9eChris Lattner        if (!InlineCallIfPossible(CS, InlineInfo, InlinedArrayAllocas,
482fa086f1f00a8b75ab2e2208bd7a028e62f9854dbChad Rosier                                  InlineHistoryID, InsertLifetime))
483dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner          continue;
484dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner        ++NumInlined;
485159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner
4860ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner        // If inlining this function gave us any new call sites, throw them
487fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner        // onto our worklist to process.  They are useful inline candidates.
4880ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner        if (!InlineInfo.InlinedCalls.empty()) {
489159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner          // Create a new inline history entry for this, so that we remember
490159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner          // that these new callsites came about due to inlining Callee.
491159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner          int NewHistoryID = InlineHistory.size();
492159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner          InlineHistory.push_back(std::make_pair(Callee, InlineHistoryID));
493159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner
4940ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner          for (unsigned i = 0, e = InlineInfo.InlinedCalls.size();
495159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner               i != e; ++i) {
4960ca2f28458ae9122f413a4092ddcee33a9dd21c6Chris Lattner            Value *Ptr = InlineInfo.InlinedCalls[i];
4976c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth            CallSite NewCS = Ptr;
4986c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth            simplifyCallSiteArguments(TD, NewCS);
4996c0b3ac8ea51d35bbd2565f17f1a73c3bfcb5cdfChandler Carruth            CallSites.push_back(std::make_pair(NewCS, NewHistoryID));
500159528702aed7222cb30c3e8b55287e4ca8068cfChris Lattner          }
501076863225ce070345ff7048f48b3550e00598a10Chris Lattner        }
502fe9af3b1f7e5d68ecc330bdf4f047d76838f8cc3Chris Lattner
503f7477470d37ee2ab9075eaee4745fa084d424ab8Jakob Stoklund Olesen        // Update the cached cost info with the inlined call.
504f7477470d37ee2ab9075eaee4745fa084d424ab8Jakob Stoklund Olesen        growCachedCostInfo(Caller, Callee);
505dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      }
506135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
507dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // If we inlined or deleted the last possible call site to the function,
508dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      // delete the function body now.
509dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattner      if (Callee && Callee->use_empty() && Callee->hasLocalLinkage() &&
510d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner          // TODO: Can remove if in SCC now.
511b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner          !SCCFunctions.count(Callee) &&
512d43d5e832f756c9d2c0c8ff4d2f51807a27cab8dChris Lattner
513b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner          // The function may be apparently dead, but if there are indirect
514b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner          // callgraph references to the node, we cannot delete it yet, this
515b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner          // could invalidate the CGSCC iterator.
516b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner          CG[Callee]->getNumReferences() == 0) {
517c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene        DEBUG(dbgs() << "    -> Deleting dead function: "
518199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner              << Callee->getName() << "\n");
519199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        CallGraphNode *CalleeNode = CG[Callee];
520199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
521199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        // Remove any call graph edges from the callee to its callees.
522199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        CalleeNode->removeAllCalledFunctions();
523199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
524199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        resetCachedCostInfo(Callee);
525199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner
526199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        // Removing the node for callee from the call graph and delete it.
527199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        delete CG.removeFunctionFromModule(CalleeNode);
528199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner        ++NumDeleted;
529199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner      }
530135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
531135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // Remove this call site from the list.  If possible, use
532135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // swap/pop_back for efficiency, but do not use it if doing so would
533135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // move a call site to a function in this SCC before the
534135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      // 'FirstCallInSCC' barrier.
5352decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner      if (SCC.isSingular()) {
536c29df3cac735bc85d16e4ef3186cb50e41bec7bbBenjamin Kramer        CallSites[CSi] = CallSites.back();
537135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        CallSites.pop_back();
538135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      } else {
539135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner        CallSites.erase(CallSites.begin()+CSi);
540237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner      }
541135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      --CSi;
542135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
543135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      Changed = true;
544135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      LocalChange = true;
545135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    }
546befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  } while (LocalChange);
547237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
548775cbdd51a3b33dd5eb343689f65ab5cc8ac7118Chris Lattner  return Changed;
549237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner}
550d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
55168d57e7ae80044401efd889270a12c71b3efb9abChris Lattner// doFinalization - Remove now-dead linkonce functions at the end of
55268d57e7ae80044401efd889270a12c71b3efb9abChris Lattner// processing to avoid breaking the SCC traversal.
55368d57e7ae80044401efd889270a12c71b3efb9abChris Lattnerbool Inliner::doFinalization(CallGraph &CG) {
554b7c6bf1e073088635951435acedff793add1cefdDevang Patel  return removeDeadFunctions(CG);
555b7c6bf1e073088635951435acedff793add1cefdDevang Patel}
556b7c6bf1e073088635951435acedff793add1cefdDevang Patel
557135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// removeDeadFunctions - Remove dead functions that are not included in
558135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner/// DNR (Do Not Remove) list.
559b7c6bf1e073088635951435acedff793add1cefdDevang Patelbool Inliner::removeDeadFunctions(CallGraph &CG,
560135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner                                  SmallPtrSet<const Function *, 16> *DNR) {
561135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  SmallPtrSet<CallGraphNode*, 16> FunctionsToRemove;
5623e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner
5633e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // Scan for all of the functions, looking for ones that should now be removed
5643e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // from the program.  Insert the dead ones in the FunctionsToRemove set.
5653e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
5663e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    CallGraphNode *CGN = I->second;
567b374b90e81d0ce6b5d02041ba4f7b15a945b38d8Chris Lattner    if (CGN->getFunction() == 0)
568135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
569135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
570135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    Function *F = CGN->getFunction();
571135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
572135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // If the only remaining users of the function are dead constants, remove
573135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // them.
574135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    F->removeDeadConstantUsers();
575135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
576135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    if (DNR && DNR->count(F))
577135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
578c66330504c3f433430a28cd7f7f981e555c51bceEli Friedman    if (!F->isDefTriviallyDead())
579135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner      continue;
580135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
581135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Remove any call graph edges from the function to its callees.
582135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    CGN->removeAllCalledFunctions();
583135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner
584135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Remove any edges from the external node to the function's call graph
585135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // node.  These edges might have been made irrelegant due to
586135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // optimization of the program.
587135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
588fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
589135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    // Removing the node for callee from the call graph and delete it.
590135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner    FunctionsToRemove.insert(CGN);
59168d57e7ae80044401efd889270a12c71b3efb9abChris Lattner  }
5923e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner
5933e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // Now that we know which functions to delete, do so.  We didn't want to do
5943e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // this inline, because that would invalidate our CallGraph::iterator
5953e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // objects. :(
596135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  //
597135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // Note that it doesn't matter that we are iterating over a non-stable set
598135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // here to do this, it doesn't matter which order the functions are deleted
599135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  // in.
6003e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  bool Changed = false;
601135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner  for (SmallPtrSet<CallGraphNode*, 16>::iterator I = FunctionsToRemove.begin(),
602135755dae4c3fa8003b76150689d5064aa4612eeChris Lattner       E = FunctionsToRemove.end(); I != E; ++I) {
6031f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen    resetCachedCostInfo((*I)->getFunction());
6043e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    delete CG.removeFunctionFromModule(*I);
6053e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    ++NumDeleted;
6063e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    Changed = true;
6073e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  }
6083e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner
60968d57e7ae80044401efd889270a12c71b3efb9abChris Lattner  return Changed;
61068d57e7ae80044401efd889270a12c71b3efb9abChris Lattner}
611