Inliner.cpp revision 1f67ce4aa3f65619f54c8a3072539da5b0022841
1cf5933a716e7eb6bd5ff49aa62f3e76379ebaf51Chris Lattner//===- Inliner.cpp - Code common to all inliners --------------------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
9237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner//
10befa499d45ffcc32bd9902518aec18589464e47cChris Lattner// This file implements the mechanics required to implement inlining without
11befa499d45ffcc32bd9902518aec18589464e47cChris Lattner// missing any calls and updating the call graph.  The decisions of which calls
12befa499d45ffcc32bd9902518aec18589464e47cChris Lattner// are profitable to inline are implemented elsewhere.
13237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner//
14237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner//===----------------------------------------------------------------------===//
15237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
1686453c52ba02e743d29c08456e51006500041456Chris Lattner#define DEBUG_TYPE "inline"
17237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Module.h"
1847b14a4a6a455c7be169cfd312fcbe796f0ad426Misha Brukman#include "llvm/Instructions.h"
191f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen#include "llvm/IntrinsicInst.h"
20237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Analysis/CallGraph.h"
21237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Support/CallSite.h"
22ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner#include "llvm/Target/TargetData.h"
236f7426ec2e46bb19cc9f9e75f1c355b35cf12d7dTanya Lattner#include "llvm/Transforms/IPO/InlinerPass.h"
24237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner#include "llvm/Transforms/Utils/Cloning.h"
25551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
26551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h"
27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h"
28befa499d45ffcc32bd9902518aec18589464e47cChris Lattner#include <set>
29a51bcb50b0c74adc741361824ef81dbefb715c53Chris Lattnerusing namespace llvm;
30d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
3186453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumInlined, "Number of functions inlined");
3286453c52ba02e743d29c08456e51006500041456Chris LattnerSTATISTIC(NumDeleted, "Number of functions deleted because all callers found");
3386453c52ba02e743d29c08456e51006500041456Chris Lattner
34844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic cl::opt<int>
35cbfdf9644ce38fd3404469c26ac3c8466c940b6eDale JohannesenInlineLimit("inline-threshold", cl::Hidden, cl::init(200),
36cbfdf9644ce38fd3404469c26ac3c8466c940b6eDale Johannesen        cl::desc("Control the amount of inlining to perform (default = 200)"));
37237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
38ae73dc1448d25b02cabc7c64c86c64371453dda8Dan GohmanInliner::Inliner(void *ID)
39ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman  : CallGraphSCCPass(ID), InlineThreshold(InlineLimit) {}
40237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
41ae73dc1448d25b02cabc7c64c86c64371453dda8Dan GohmanInliner::Inliner(void *ID, int Threshold)
42ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman  : CallGraphSCCPass(ID), InlineThreshold(Threshold) {}
43120d053e3ba810b44047fbcb719824bed5673ca9Chris Lattner
44ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// getAnalysisUsage - For this class, we declare that we require and preserve
45ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// the call graph.  If the derived class implements this method, it should
46ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner/// always explicitly call the implementation here.
47ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattnervoid Inliner::getAnalysisUsage(AnalysisUsage &Info) const {
48ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner  Info.addRequired<TargetData>();
49ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner  CallGraphSCCPass::getAnalysisUsage(Info);
50ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner}
51ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner
52befa499d45ffcc32bd9902518aec18589464e47cChris Lattner// InlineCallIfPossible - If it is possible to inline the specified call site,
53befa499d45ffcc32bd9902518aec18589464e47cChris Lattner// do so and update the CallGraph for this operation.
541f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesenbool Inliner::InlineCallIfPossible(CallSite CS, CallGraph &CG,
55ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner                                 const std::set<Function*> &SCCFunctions,
56ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner                                 const TargetData &TD) {
57befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  Function *Callee = CS.getCalledFunction();
5866c75aaa028683c389c55b377ee2411b61081677Bill Wendling  Function *Caller = CS.getCaller();
5966c75aaa028683c389c55b377ee2411b61081677Bill Wendling
60ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner  if (!InlineFunction(CS, &CG, &TD)) return false;
61fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
628c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  // If the inlined function had a higher stack protection level than the
638c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  // calling function, then bump up the caller's stack protection level.
648c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  if (Callee->hasFnAttr(Attribute::StackProtectReq))
658c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling    Caller->addFnAttr(Attribute::StackProtectReq);
668c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling  else if (Callee->hasFnAttr(Attribute::StackProtect) &&
678c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling           !Caller->hasFnAttr(Attribute::StackProtectReq))
688c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling    Caller->addFnAttr(Attribute::StackProtect);
698c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling
7054970c032815edadb1b2988ea33f5a1173e5b29cChris Lattner  // If we inlined the last possible call site to the function, delete the
7154970c032815edadb1b2988ea33f5a1173e5b29cChris Lattner  // function body now.
72bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola  if (Callee->use_empty() && Callee->hasLocalLinkage() &&
73befa499d45ffcc32bd9902518aec18589464e47cChris Lattner      !SCCFunctions.count(Callee)) {
740a81aac4b46eed130d20714af5a1c01b05d0275eBill Wendling    DOUT << "    -> Deleting dead function: " << Callee->getName() << "\n";
756f0a7687ab9a0509e847279fae27554ce7da0ba1Duncan Sands    CallGraphNode *CalleeNode = CG[Callee];
76fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
77befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    // Remove any call graph edges from the callee to its callees.
786f0a7687ab9a0509e847279fae27554ce7da0ba1Duncan Sands    CalleeNode->removeAllCalledFunctions();
79fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
801f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen    resetCachedCostInfo(CalleeNode->getFunction());
811f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen
82befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    // Removing the node for callee from the call graph and delete it.
83befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    delete CG.removeFunctionFromModule(CalleeNode);
84befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    ++NumDeleted;
85befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  }
86befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  return true;
87237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner}
881a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar
891a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar/// shouldInline - Return true if the inliner should attempt to inline
901a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar/// at the given CallSite.
911a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbarbool Inliner::shouldInline(CallSite CS) {
92c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  InlineCost IC = getInlineCost(CS);
931a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  float FudgeFactor = getInlineFudgeFactor(CS);
941a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar
95c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  if (IC.isAlways()) {
96c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar    DOUT << "    Inlining: cost=always"
97c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar         << ", Call: " << *CS.getInstruction();
98c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar    return true;
99c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  }
100c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar
101c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  if (IC.isNever()) {
102c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar    DOUT << "    NOT Inlining: cost=never"
103c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar         << ", Call: " << *CS.getInstruction();
104c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar    return false;
105c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  }
106c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar
107c5e1ec47c719806fcc882470595960512edc7441Daniel Dunbar  int Cost = IC.getValue();
1081a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  int CurrentThreshold = InlineThreshold;
1091a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  Function *Fn = CS.getCaller();
1101a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  if (Fn && !Fn->isDeclaration()
1111a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar      && Fn->hasFnAttr(Attribute::OptimizeForSize)
1121a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar      && InlineThreshold != 50) {
1131a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar    CurrentThreshold = 50;
1141a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  }
1151a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar
1161a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  if (Cost >= (int)(CurrentThreshold * FudgeFactor)) {
1171a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar    DOUT << "    NOT Inlining: cost=" << Cost
1181a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar         << ", Call: " << *CS.getInstruction();
1191a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar    return false;
1201a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  } else {
1211a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar    DOUT << "    Inlining: cost=" << Cost
1221a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar         << ", Call: " << *CS.getInstruction();
1231a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar    return true;
1241a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar  }
1251a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar}
126237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
127237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattnerbool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
128237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner  CallGraph &CG = getAnalysis<CallGraph>();
1291f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen  TargetData &TD = getAnalysis<TargetData>();
130237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
131237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner  std::set<Function*> SCCFunctions;
1320a81aac4b46eed130d20714af5a1c01b05d0275eBill Wendling  DOUT << "Inliner visiting SCC:";
133237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner  for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
134befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    Function *F = SCC[i]->getFunction();
135befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    if (F) SCCFunctions.insert(F);
1360a81aac4b46eed130d20714af5a1c01b05d0275eBill Wendling    DOUT << " " << (F ? F->getName() : "INDIRECTNODE");
137237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner  }
138237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
139befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // Scan through and identify all call sites ahead of time so that we only
140befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // inline call sites in the original functions, not call sites that result
141befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // from inlining other functions.
142befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  std::vector<CallSite> CallSites;
143befa499d45ffcc32bd9902518aec18589464e47cChris Lattner
144cf5933a716e7eb6bd5ff49aa62f3e76379ebaf51Chris Lattner  for (unsigned i = 0, e = SCC.size(); i != e; ++i)
145cf5933a716e7eb6bd5ff49aa62f3e76379ebaf51Chris Lattner    if (Function *F = SCC[i]->getFunction())
146befa499d45ffcc32bd9902518aec18589464e47cChris Lattner      for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
147befa499d45ffcc32bd9902518aec18589464e47cChris Lattner        for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
148befa499d45ffcc32bd9902518aec18589464e47cChris Lattner          CallSite CS = CallSite::get(I);
1491f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen          if (CS.getInstruction() && !isa<DbgInfoIntrinsic>(I) &&
1501f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen                                     (!CS.getCalledFunction() ||
1515cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer                                      !CS.getCalledFunction()->isDeclaration()))
152befa499d45ffcc32bd9902518aec18589464e47cChris Lattner            CallSites.push_back(CS);
153befa499d45ffcc32bd9902518aec18589464e47cChris Lattner        }
154237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
1550a81aac4b46eed130d20714af5a1c01b05d0275eBill Wendling  DOUT << ": " << CallSites.size() << " call sites.\n";
156fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
157befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // Now that we have all of the call sites, move the ones to functions in the
158befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // current SCC to the end of the list.
159befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  unsigned FirstCallInSCC = CallSites.size();
160befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  for (unsigned i = 0; i < FirstCallInSCC; ++i)
161befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    if (Function *F = CallSites[i].getCalledFunction())
162befa499d45ffcc32bd9902518aec18589464e47cChris Lattner      if (SCCFunctions.count(F))
163befa499d45ffcc32bd9902518aec18589464e47cChris Lattner        std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
164fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
165befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // Now that we have all of the call sites, loop over them and inline them if
166befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  // it looks profitable to do so.
167befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  bool Changed = false;
168befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  bool LocalChange;
169befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  do {
170befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    LocalChange = false;
171befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    // Iterate over the outer loop because inlining functions can cause indirect
172befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    // calls to become direct calls.
173befa499d45ffcc32bd9902518aec18589464e47cChris Lattner    for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi)
174befa499d45ffcc32bd9902518aec18589464e47cChris Lattner      if (Function *Callee = CallSites[CSi].getCalledFunction()) {
175befa499d45ffcc32bd9902518aec18589464e47cChris Lattner        // Calls to external functions are never inlinable.
176cbfdf9644ce38fd3404469c26ac3c8466c940b6eDale Johannesen        if (Callee->isDeclaration()) {
17708ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner          if (SCC.size() == 1) {
17808ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            std::swap(CallSites[CSi], CallSites.back());
17908ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            CallSites.pop_back();
18008ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner          } else {
18108ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            // Keep the 'in SCC / not in SCC' boundary correct.
18208ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            CallSites.erase(CallSites.begin()+CSi);
18308ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner          }
184befa499d45ffcc32bd9902518aec18589464e47cChris Lattner          --CSi;
185befa499d45ffcc32bd9902518aec18589464e47cChris Lattner          continue;
186befa499d45ffcc32bd9902518aec18589464e47cChris Lattner        }
187befa499d45ffcc32bd9902518aec18589464e47cChris Lattner
188befa499d45ffcc32bd9902518aec18589464e47cChris Lattner        // If the policy determines that we should inline this function,
189befa499d45ffcc32bd9902518aec18589464e47cChris Lattner        // try to do so.
190befa499d45ffcc32bd9902518aec18589464e47cChris Lattner        CallSite CS = CallSites[CSi];
1911a99dbfe3b70c83d3f3e4648b5868c04697cd77cDaniel Dunbar        if (shouldInline(CS)) {
192e345566f8eaeeda45e29e3709114a42209a360ccDale Johannesen          Function *Caller = CS.getCaller();
193befa499d45ffcc32bd9902518aec18589464e47cChris Lattner          // Attempt to inline the function...
1941f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen          if (InlineCallIfPossible(CS, CG, SCCFunctions, TD)) {
1951f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen            // Remove any cached cost info for this caller, as inlining the
1961f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen            // callee has increased the size of the caller (which may be the
1971f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen            // same as the callee).
198e345566f8eaeeda45e29e3709114a42209a360ccDale Johannesen            resetCachedCostInfo(Caller);
199e345566f8eaeeda45e29e3709114a42209a360ccDale Johannesen
20008ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            // Remove this call site from the list.  If possible, use
20108ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            // swap/pop_back for efficiency, but do not use it if doing so would
20208ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            // move a call site to a function in this SCC before the
20308ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            // 'FirstCallInSCC' barrier.
20408ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            if (SCC.size() == 1) {
20508ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner              std::swap(CallSites[CSi], CallSites.back());
20608ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner              CallSites.pop_back();
20708ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            } else {
20808ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner              CallSites.erase(CallSites.begin()+CSi);
20908ff1480ffcb22e946c7bb6c7d66c5d977ae3d6eChris Lattner            }
210befa499d45ffcc32bd9902518aec18589464e47cChris Lattner            --CSi;
211befa499d45ffcc32bd9902518aec18589464e47cChris Lattner
212befa499d45ffcc32bd9902518aec18589464e47cChris Lattner            ++NumInlined;
213befa499d45ffcc32bd9902518aec18589464e47cChris Lattner            Changed = true;
214befa499d45ffcc32bd9902518aec18589464e47cChris Lattner            LocalChange = true;
215775cbdd51a3b33dd5eb343689f65ab5cc8ac7118Chris Lattner          }
216237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner        }
217237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner      }
218befa499d45ffcc32bd9902518aec18589464e47cChris Lattner  } while (LocalChange);
219237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner
220775cbdd51a3b33dd5eb343689f65ab5cc8ac7118Chris Lattner  return Changed;
221237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner}
222d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
22368d57e7ae80044401efd889270a12c71b3efb9abChris Lattner// doFinalization - Remove now-dead linkonce functions at the end of
22468d57e7ae80044401efd889270a12c71b3efb9abChris Lattner// processing to avoid breaking the SCC traversal.
22568d57e7ae80044401efd889270a12c71b3efb9abChris Lattnerbool Inliner::doFinalization(CallGraph &CG) {
226b7c6bf1e073088635951435acedff793add1cefdDevang Patel  return removeDeadFunctions(CG);
227b7c6bf1e073088635951435acedff793add1cefdDevang Patel}
228b7c6bf1e073088635951435acedff793add1cefdDevang Patel
229b7c6bf1e073088635951435acedff793add1cefdDevang Patel  /// removeDeadFunctions - Remove dead functions that are not included in
230b7c6bf1e073088635951435acedff793add1cefdDevang Patel  /// DNR (Do Not Remove) list.
231b7c6bf1e073088635951435acedff793add1cefdDevang Patelbool Inliner::removeDeadFunctions(CallGraph &CG,
232b7c6bf1e073088635951435acedff793add1cefdDevang Patel                                 SmallPtrSet<const Function *, 16> *DNR) {
2333e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  std::set<CallGraphNode*> FunctionsToRemove;
2343e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner
2353e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // Scan for all of the functions, looking for ones that should now be removed
2363e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // from the program.  Insert the dead ones in the FunctionsToRemove set.
2373e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
2383e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    CallGraphNode *CGN = I->second;
23954970c032815edadb1b2988ea33f5a1173e5b29cChris Lattner    if (Function *F = CGN ? CGN->getFunction() : 0) {
2400c0aa711b8a0550c21f032125c4663ff45864f81Chris Lattner      // If the only remaining users of the function are dead constants, remove
2410c0aa711b8a0550c21f032125c4663ff45864f81Chris Lattner      // them.
24254970c032815edadb1b2988ea33f5a1173e5b29cChris Lattner      F->removeDeadConstantUsers();
24354970c032815edadb1b2988ea33f5a1173e5b29cChris Lattner
244b7c6bf1e073088635951435acedff793add1cefdDevang Patel      if (DNR && DNR->count(F))
245b7c6bf1e073088635951435acedff793add1cefdDevang Patel        continue;
246b7c6bf1e073088635951435acedff793add1cefdDevang Patel
247bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola      if ((F->hasLinkOnceLinkage() || F->hasLocalLinkage()) &&
24854970c032815edadb1b2988ea33f5a1173e5b29cChris Lattner          F->use_empty()) {
2490c0aa711b8a0550c21f032125c4663ff45864f81Chris Lattner
25054970c032815edadb1b2988ea33f5a1173e5b29cChris Lattner        // Remove any call graph edges from the function to its callees.
2516f0a7687ab9a0509e847279fae27554ce7da0ba1Duncan Sands        CGN->removeAllCalledFunctions();
252fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
2530c0aa711b8a0550c21f032125c4663ff45864f81Chris Lattner        // Remove any edges from the external node to the function's call graph
2540c0aa711b8a0550c21f032125c4663ff45864f81Chris Lattner        // node.  These edges might have been made irrelegant due to
2550c0aa711b8a0550c21f032125c4663ff45864f81Chris Lattner        // optimization of the program.
2560c0aa711b8a0550c21f032125c4663ff45864f81Chris Lattner        CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
257fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
25854970c032815edadb1b2988ea33f5a1173e5b29cChris Lattner        // Removing the node for callee from the call graph and delete it.
25954970c032815edadb1b2988ea33f5a1173e5b29cChris Lattner        FunctionsToRemove.insert(CGN);
26054970c032815edadb1b2988ea33f5a1173e5b29cChris Lattner      }
26168d57e7ae80044401efd889270a12c71b3efb9abChris Lattner    }
26268d57e7ae80044401efd889270a12c71b3efb9abChris Lattner  }
2633e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner
2643e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // Now that we know which functions to delete, do so.  We didn't want to do
2653e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // this inline, because that would invalidate our CallGraph::iterator
2663e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  // objects. :(
2673e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  bool Changed = false;
2683e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(),
2693e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner         E = FunctionsToRemove.end(); I != E; ++I) {
2701f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen    resetCachedCostInfo((*I)->getFunction());
2713e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    delete CG.removeFunctionFromModule(*I);
2723e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    ++NumDeleted;
2733e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner    Changed = true;
2743e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner  }
2753e1358a9fa1ebd3f51c94eb69da55d693895fe7cChris Lattner
27668d57e7ae80044401efd889270a12c71b3efb9abChris Lattner  return Changed;
27768d57e7ae80044401efd889270a12c71b3efb9abChris Lattner}
278