Inliner.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//===- Inliner.cpp - Code common to all inliners --------------------------===//
258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// License. See LICENSE.TXT for details.
758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//===----------------------------------------------------------------------===//
958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
1058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// This file implements the mechanics required to implement inlining without
1158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// missing any calls and updating the call graph.  The decisions of which calls
1258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// are profitable to inline are implemented elsewhere.
1358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//
1458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)//===----------------------------------------------------------------------===//
1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#define DEBUG_TYPE "inline"
1758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "llvm/Transforms/IPO/InlinerPass.h"
1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "llvm/ADT/SmallPtrSet.h"
1958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "llvm/ADT/Statistic.h"
2058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "llvm/Analysis/CallGraph.h"
2158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "llvm/Analysis/InlineCost.h"
2258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "llvm/IR/CallSite.h"
2358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "llvm/IR/DataLayout.h"
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/IR/Instructions.h"
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/IR/IntrinsicInst.h"
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/IR/Module.h"
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Support/CommandLine.h"
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Support/Debug.h"
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Support/raw_ostream.h"
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Target/TargetLibraryInfo.h"
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Transforms/Utils/Cloning.h"
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Transforms/Utils/Local.h"
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using namespace llvm;
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)STATISTIC(NumInlined, "Number of functions inlined");
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)STATISTIC(NumCallsDeleted, "Number of call sites deleted, not inlined");
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)STATISTIC(NumMergedAllocas, "Number of allocas merged together");
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// This weirdly named statistic tracks the number of times that, when attempting
4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// to inline a function A into B, we analyze the callers of B in order to see
4258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// if those would be more profitable and blocked inline steps.
4358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)STATISTIC(NumCallerCallersAnalyzed, "Number of caller-callers analyzed");
4458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
4558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)static cl::opt<int>
4658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)InlineLimit("inline-threshold", cl::Hidden, cl::init(225), cl::ZeroOrMore,
4758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        cl::desc("Control the amount of inlining to perform (default = 225)"));
4858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
4958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)static cl::opt<int>
5058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)HintThreshold("inlinehint-threshold", cl::Hidden, cl::init(325),
5158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)              cl::desc("Threshold for inlining functions with inline hint"));
5258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// We instroduce this threshold to help performance of instrumentation based
5458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// PGO before we actually hook up inliner with analysis passes such as BPI and
5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// BFI.
5658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)static cl::opt<int>
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)ColdThreshold("inlinecold-threshold", cl::Hidden, cl::init(225),
5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)              cl::desc("Threshold for inlining functions with cold attribute"));
5958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Threshold to use when optsize is specified (and there is no -inline-limit).
6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)const int OptSizeThreshold = 75;
6258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)Inliner::Inliner(char &ID)
6458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  : CallGraphSCCPass(ID), InlineThreshold(InlineLimit), InsertLifetime(true) {}
6558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)Inliner::Inliner(char &ID, int Threshold, bool InsertLifetime)
6758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  : CallGraphSCCPass(ID), InlineThreshold(InlineLimit.getNumOccurrences() > 0 ?
6858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                          InlineLimit : Threshold),
6958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    InsertLifetime(InsertLifetime) {}
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/// getAnalysisUsage - For this class, we declare that we require and preserve
7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/// the call graph.  If the derived class implements this method, it should
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/// always explicitly call the implementation here.
7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void Inliner::getAnalysisUsage(AnalysisUsage &AU) const {
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  CallGraphSCCPass::getAnalysisUsage(AU);
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
7958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)typedef DenseMap<ArrayType*, std::vector<AllocaInst*> >
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)InlinedArrayAllocasTy;
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/// \brief If the inlined function had a higher stack protection level than the
8358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/// calling function, then bump up the caller's stack protection level.
8458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)static void AdjustCallerSSPLevel(Function *Caller, Function *Callee) {
8558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // If upgrading the SSP attribute, clear out the old SSP Attributes first.
8658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Having multiple SSP attributes doesn't actually hurt, but it adds useless
8758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // clutter to the IR.
8858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  AttrBuilder B;
8958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  B.addAttribute(Attribute::StackProtect)
9058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    .addAttribute(Attribute::StackProtectStrong);
9158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  AttributeSet OldSSPAttr = AttributeSet::get(Caller->getContext(),
9258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                              AttributeSet::FunctionIndex,
9358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                              B);
9458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  AttributeSet CallerAttr = Caller->getAttributes(),
9558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)               CalleeAttr = Callee->getAttributes();
9658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
9758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (CalleeAttr.hasAttribute(AttributeSet::FunctionIndex,
9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                              Attribute::StackProtectReq)) {
9958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    Caller->removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
10058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    Caller->addFnAttr(Attribute::StackProtectReq);
10158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  } else if (CalleeAttr.hasAttribute(AttributeSet::FunctionIndex,
10258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                     Attribute::StackProtectStrong) &&
10358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)             !CallerAttr.hasAttribute(AttributeSet::FunctionIndex,
10458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                      Attribute::StackProtectReq)) {
10558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    Caller->removeAttributes(AttributeSet::FunctionIndex, OldSSPAttr);
10658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    Caller->addFnAttr(Attribute::StackProtectStrong);
10758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  } else if (CalleeAttr.hasAttribute(AttributeSet::FunctionIndex,
10858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                     Attribute::StackProtect) &&
10958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)           !CallerAttr.hasAttribute(AttributeSet::FunctionIndex,
11058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                    Attribute::StackProtectReq) &&
11158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)           !CallerAttr.hasAttribute(AttributeSet::FunctionIndex,
11258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                    Attribute::StackProtectStrong))
11358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    Caller->addFnAttr(Attribute::StackProtect);
11458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
11558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
11658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/// InlineCallIfPossible - If it is possible to inline the specified call site,
11758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/// do so and update the CallGraph for this operation.
11858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)///
11958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/// This function also does some basic book-keeping to update the IR.  The
12058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)/// InlinedArrayAllocas map keeps track of any allocas that are already
1214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/// available from other  functions inlined into the caller.  If we are able to
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/// inline this call site we attempt to reuse already available allocas or add
1234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)/// any new allocas to the set if not possible.
1244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static bool InlineCallIfPossible(CallSite CS, InlineFunctionInfo &IFI,
1254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                 InlinedArrayAllocasTy &InlinedArrayAllocas,
1264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                 int InlineHistory, bool InsertLifetime,
1274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                 const DataLayout *DL) {
1284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  Function *Callee = CS.getCalledFunction();
1294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  Function *Caller = CS.getCaller();
1304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Try to inline the function.  Get the list of static allocas that were
1324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // inlined.
1334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!InlineFunction(CS, IFI, InsertLifetime))
1344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return false;
1354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  AdjustCallerSSPLevel(Caller, Callee);
1374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Look at all of the allocas that we inlined through this call site.  If we
1394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // have already inlined other allocas through other calls into this function,
1404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // then we know that they have disjoint lifetimes and that we can merge them.
1414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //
142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // There are many heuristics possible for merging these allocas, and the
14358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // different options have different tradeoffs.  One thing that we *really*
144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // don't want to hurt is SRoA: once inlining happens, often allocas are no
145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // longer address taken and so they can be promoted.
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Our "solution" for that is to only merge allocas whose outermost type is an
148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // array type.  These are usually not promoted because someone is using a
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // variable index into them.  These are also often the most important ones to
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // merge.
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // A better solution would be to have real memory lifetime markers in the IR
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // and not have the inliner do any merging of allocas at all.  This would
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // allow the backend to do proper stack slot coloring of all allocas that
15558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // *actually make it to the backend*, which is really what we want.
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Because we don't have this information, we do this simple and useful hack.
158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //
159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SmallPtrSet<AllocaInst*, 16> UsedAllocas;
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // When processing our SCC, check to see if CS was inlined from some other
16258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // call site.  For example, if we're processing "A" in this code:
163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //   A() { B() }
164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //   B() { x = alloca ... C() }
16558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  //   C() { y = alloca ... }
166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Assume that C was not inlined into B initially, and so we're processing A
167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // and decide to inline B into A.  Doing this makes an alloca available for
168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // reuse and makes a callsite (C) available for inlining.  When we process
169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // the C call site we don't want to do any alloca merging between X and Y
17058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // because their scopes are not disjoint.  We could make this smarter by
171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // keeping track of the inline history for each alloca in the
172a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // InlinedArrayAllocas but this isn't likely to be a significant win.
173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (InlineHistory != -1)  // Only do merging for top-level call sites in SCC.
174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return true;
1754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
176a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Loop over all the allocas we have so far and see if they can be merged with
177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // a previously inlined alloca.  If not, remember that we had it.
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (unsigned AllocaNo = 0, e = IFI.StaticAllocas.size();
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)       AllocaNo != e; ++AllocaNo) {
180a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    AllocaInst *AI = IFI.StaticAllocas[AllocaNo];
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
182a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Don't bother trying to merge array allocations (they will usually be
18358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    // canonicalized to be an allocation *of* an array), or allocations whose
184a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // type is not itself an array (because we're afraid of pessimizing SRoA).
185a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType());
186a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (ATy == 0 || AI->isArrayAllocation())
187a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      continue;
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Get the list of all available allocas for this array type.
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    std::vector<AllocaInst*> &AllocasForType = InlinedArrayAllocas[ATy];
19158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Loop over the allocas in AllocasForType to see if we can reuse one.  Note
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // that we have to be careful not to reuse the same "available" alloca for
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // multiple different allocas that we just inlined, we use the 'UsedAllocas'
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // set to keep track of which "available" allocas are being used by this
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // function.  Also, AllocasForType can be empty of course!
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool MergedAwayAlloca = false;
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    for (unsigned i = 0, e = AllocasForType.size(); i != e; ++i) {
19958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      AllocaInst *AvailableAlloca = AllocasForType[i];
200a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
201a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      unsigned Align1 = AI->getAlignment(),
202a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)               Align2 = AvailableAlloca->getAlignment();
203a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // If we don't have data layout information, and only one alloca is using
204a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // the target default, then we can't safely merge them because we can't
205a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // pick the greater alignment.
206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      if (!DL && (!Align1 || !Align2) && Align1 != Align2)
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        continue;
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // The available alloca has to be in the right function, not in some other
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // function in this SCC.
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      if (AvailableAlloca->getParent() != AI->getParent())
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        continue;
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // If the inlined function already uses this alloca then we can't reuse
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // it.
216a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      if (!UsedAllocas.insert(AvailableAlloca))
217a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        continue;
218a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
219a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare
220a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // success!
221a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      DEBUG(dbgs() << "    ***MERGED ALLOCA: " << *AI << "\n\t\tINTO: "
222a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                   << *AvailableAlloca << '\n');
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AI->replaceAllUsesWith(AvailableAlloca);
225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      if (Align1 != Align2) {
227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if (!Align1 || !Align2) {
228a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          assert(DL && "DataLayout required to compare default alignments");
229cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          unsigned TypeAlign = DL->getABITypeAlignment(AI->getAllocatedType());
230cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
231a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          Align1 = Align1 ? Align1 : TypeAlign;
232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          Align2 = Align2 ? Align2 : TypeAlign;
233010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)        }
234010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        if (Align1 > Align2)
236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          AvailableAlloca->setAlignment(AI->getAlignment());
237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      }
238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
239a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      AI->eraseFromParent();
240a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      MergedAwayAlloca = true;
241a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      ++NumMergedAllocas;
242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      IFI.StaticAllocas[AllocaNo] = 0;
243a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      break;
244a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
245a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
246a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // If we already nuked the alloca, we're done with it.
247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (MergedAwayAlloca)
248a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      continue;
249c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
250c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    // If we were unable to merge away the alloca either because there are no
251a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // allocas of the right type available or because we reused them all
252a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // already, remember that this alloca came from an inlined function and mark
253a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // it used so we don't reuse it for other allocas from this inline
254a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    // operation.
255a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    AllocasForType.push_back(AI);
256116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    UsedAllocas.insert(AI);
257116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
258116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
259116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return true;
2605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
2615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
262a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)unsigned Inliner::getInlineThreshold(CallSite CS) const {
26358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  int thres = InlineThreshold; // -inline-threshold or else selected by
264                               // overall opt level
265
266  // If -inline-threshold is not given, listen to the optsize attribute when it
267  // would decrease the threshold.
268  Function *Caller = CS.getCaller();
269  bool OptSize = Caller && !Caller->isDeclaration() &&
270    Caller->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
271                                         Attribute::OptimizeForSize);
272  if (!(InlineLimit.getNumOccurrences() > 0) && OptSize &&
273      OptSizeThreshold < thres)
274    thres = OptSizeThreshold;
275
276  // Listen to the inlinehint attribute when it would increase the threshold
277  // and the caller does not need to minimize its size.
278  Function *Callee = CS.getCalledFunction();
279  bool InlineHint = Callee && !Callee->isDeclaration() &&
280    Callee->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
281                                         Attribute::InlineHint);
282  if (InlineHint && HintThreshold > thres
283      && !Caller->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
284                                               Attribute::MinSize))
285    thres = HintThreshold;
286
287  // Listen to the cold attribute when it would decrease the threshold.
288  bool ColdCallee = Callee && !Callee->isDeclaration() &&
289    Callee->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
290                                         Attribute::Cold);
291  if (ColdCallee && ColdThreshold < thres)
292    thres = ColdThreshold;
293
294  return thres;
295}
296
297/// shouldInline - Return true if the inliner should attempt to inline
298/// at the given CallSite.
299bool Inliner::shouldInline(CallSite CS) {
300  InlineCost IC = getInlineCost(CS);
301
302  if (IC.isAlways()) {
303    DEBUG(dbgs() << "    Inlining: cost=always"
304          << ", Call: " << *CS.getInstruction() << "\n");
305    return true;
306  }
307
308  if (IC.isNever()) {
309    DEBUG(dbgs() << "    NOT Inlining: cost=never"
310          << ", Call: " << *CS.getInstruction() << "\n");
311    return false;
312  }
313
314  Function *Caller = CS.getCaller();
315  if (!IC) {
316    DEBUG(dbgs() << "    NOT Inlining: cost=" << IC.getCost()
317          << ", thres=" << (IC.getCostDelta() + IC.getCost())
318          << ", Call: " << *CS.getInstruction() << "\n");
319    return false;
320  }
321
322  // Try to detect the case where the current inlining candidate caller (call
323  // it B) is a static or linkonce-ODR function and is an inlining candidate
324  // elsewhere, and the current candidate callee (call it C) is large enough
325  // that inlining it into B would make B too big to inline later. In these
326  // circumstances it may be best not to inline C into B, but to inline B into
327  // its callers.
328  //
329  // This only applies to static and linkonce-ODR functions because those are
330  // expected to be available for inlining in the translation units where they
331  // are used. Thus we will always have the opportunity to make local inlining
332  // decisions. Importantly the linkonce-ODR linkage covers inline functions
333  // and templates in C++.
334  //
335  // FIXME: All of this logic should be sunk into getInlineCost. It relies on
336  // the internal implementation of the inline cost metrics rather than
337  // treating them as truly abstract units etc.
338  if (Caller->hasLocalLinkage() ||
339      Caller->getLinkage() == GlobalValue::LinkOnceODRLinkage) {
340    int TotalSecondaryCost = 0;
341    // The candidate cost to be imposed upon the current function.
342    int CandidateCost = IC.getCost() - (InlineConstants::CallPenalty + 1);
343    // This bool tracks what happens if we do NOT inline C into B.
344    bool callerWillBeRemoved = Caller->hasLocalLinkage();
345    // This bool tracks what happens if we DO inline C into B.
346    bool inliningPreventsSomeOuterInline = false;
347    for (User *U : Caller->users()) {
348      CallSite CS2(U);
349
350      // If this isn't a call to Caller (it could be some other sort
351      // of reference) skip it.  Such references will prevent the caller
352      // from being removed.
353      if (!CS2 || CS2.getCalledFunction() != Caller) {
354        callerWillBeRemoved = false;
355        continue;
356      }
357
358      InlineCost IC2 = getInlineCost(CS2);
359      ++NumCallerCallersAnalyzed;
360      if (!IC2) {
361        callerWillBeRemoved = false;
362        continue;
363      }
364      if (IC2.isAlways())
365        continue;
366
367      // See if inlining or original callsite would erase the cost delta of
368      // this callsite. We subtract off the penalty for the call instruction,
369      // which we would be deleting.
370      if (IC2.getCostDelta() <= CandidateCost) {
371        inliningPreventsSomeOuterInline = true;
372        TotalSecondaryCost += IC2.getCost();
373      }
374    }
375    // If all outer calls to Caller would get inlined, the cost for the last
376    // one is set very low by getInlineCost, in anticipation that Caller will
377    // be removed entirely.  We did not account for this above unless there
378    // is only one caller of Caller.
379    if (callerWillBeRemoved && !Caller->use_empty())
380      TotalSecondaryCost += InlineConstants::LastCallToStaticBonus;
381
382    if (inliningPreventsSomeOuterInline && TotalSecondaryCost < IC.getCost()) {
383      DEBUG(dbgs() << "    NOT Inlining: " << *CS.getInstruction() <<
384           " Cost = " << IC.getCost() <<
385           ", outer Cost = " << TotalSecondaryCost << '\n');
386      return false;
387    }
388  }
389
390  DEBUG(dbgs() << "    Inlining: cost=" << IC.getCost()
391        << ", thres=" << (IC.getCostDelta() + IC.getCost())
392        << ", Call: " << *CS.getInstruction() << '\n');
393  return true;
394}
395
396/// InlineHistoryIncludes - Return true if the specified inline history ID
397/// indicates an inline history that includes the specified function.
398static bool InlineHistoryIncludes(Function *F, int InlineHistoryID,
399            const SmallVectorImpl<std::pair<Function*, int> > &InlineHistory) {
400  while (InlineHistoryID != -1) {
401    assert(unsigned(InlineHistoryID) < InlineHistory.size() &&
402           "Invalid inline history ID");
403    if (InlineHistory[InlineHistoryID].first == F)
404      return true;
405    InlineHistoryID = InlineHistory[InlineHistoryID].second;
406  }
407  return false;
408}
409
410bool Inliner::runOnSCC(CallGraphSCC &SCC) {
411  CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
412  DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
413  const DataLayout *DL = DLP ? &DLP->getDataLayout() : 0;
414  const TargetLibraryInfo *TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
415
416  SmallPtrSet<Function*, 8> SCCFunctions;
417  DEBUG(dbgs() << "Inliner visiting SCC:");
418  for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
419    Function *F = (*I)->getFunction();
420    if (F) SCCFunctions.insert(F);
421    DEBUG(dbgs() << " " << (F ? F->getName() : "INDIRECTNODE"));
422  }
423
424  // Scan through and identify all call sites ahead of time so that we only
425  // inline call sites in the original functions, not call sites that result
426  // from inlining other functions.
427  SmallVector<std::pair<CallSite, int>, 16> CallSites;
428
429  // When inlining a callee produces new call sites, we want to keep track of
430  // the fact that they were inlined from the callee.  This allows us to avoid
431  // infinite inlining in some obscure cases.  To represent this, we use an
432  // index into the InlineHistory vector.
433  SmallVector<std::pair<Function*, int>, 8> InlineHistory;
434
435  for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
436    Function *F = (*I)->getFunction();
437    if (!F) continue;
438
439    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
440      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
441        CallSite CS(cast<Value>(I));
442        // If this isn't a call, or it is a call to an intrinsic, it can
443        // never be inlined.
444        if (!CS || isa<IntrinsicInst>(I))
445          continue;
446
447        // If this is a direct call to an external function, we can never inline
448        // it.  If it is an indirect call, inlining may resolve it to be a
449        // direct call, so we keep it.
450        if (CS.getCalledFunction() && CS.getCalledFunction()->isDeclaration())
451          continue;
452
453        CallSites.push_back(std::make_pair(CS, -1));
454      }
455  }
456
457  DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n");
458
459  // If there are no calls in this function, exit early.
460  if (CallSites.empty())
461    return false;
462
463  // Now that we have all of the call sites, move the ones to functions in the
464  // current SCC to the end of the list.
465  unsigned FirstCallInSCC = CallSites.size();
466  for (unsigned i = 0; i < FirstCallInSCC; ++i)
467    if (Function *F = CallSites[i].first.getCalledFunction())
468      if (SCCFunctions.count(F))
469        std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
470
471
472  InlinedArrayAllocasTy InlinedArrayAllocas;
473  InlineFunctionInfo InlineInfo(&CG, DL);
474
475  // Now that we have all of the call sites, loop over them and inline them if
476  // it looks profitable to do so.
477  bool Changed = false;
478  bool LocalChange;
479  do {
480    LocalChange = false;
481    // Iterate over the outer loop because inlining functions can cause indirect
482    // calls to become direct calls.
483    for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) {
484      CallSite CS = CallSites[CSi].first;
485
486      Function *Caller = CS.getCaller();
487      Function *Callee = CS.getCalledFunction();
488
489      // If this call site is dead and it is to a readonly function, we should
490      // just delete the call instead of trying to inline it, regardless of
491      // size.  This happens because IPSCCP propagates the result out of the
492      // call and then we're left with the dead call.
493      if (isInstructionTriviallyDead(CS.getInstruction(), TLI)) {
494        DEBUG(dbgs() << "    -> Deleting dead call: "
495                     << *CS.getInstruction() << "\n");
496        // Update the call graph by deleting the edge from Callee to Caller.
497        CG[Caller]->removeCallEdgeFor(CS);
498        CS.getInstruction()->eraseFromParent();
499        ++NumCallsDeleted;
500      } else {
501        // We can only inline direct calls to non-declarations.
502        if (Callee == 0 || Callee->isDeclaration()) continue;
503
504        // If this call site was obtained by inlining another function, verify
505        // that the include path for the function did not include the callee
506        // itself.  If so, we'd be recursively inlining the same function,
507        // which would provide the same callsites, which would cause us to
508        // infinitely inline.
509        int InlineHistoryID = CallSites[CSi].second;
510        if (InlineHistoryID != -1 &&
511            InlineHistoryIncludes(Callee, InlineHistoryID, InlineHistory))
512          continue;
513
514
515        // If the policy determines that we should inline this function,
516        // try to do so.
517        if (!shouldInline(CS))
518          continue;
519
520        // Attempt to inline the function.
521        if (!InlineCallIfPossible(CS, InlineInfo, InlinedArrayAllocas,
522                                  InlineHistoryID, InsertLifetime, DL))
523          continue;
524        ++NumInlined;
525
526        // If inlining this function gave us any new call sites, throw them
527        // onto our worklist to process.  They are useful inline candidates.
528        if (!InlineInfo.InlinedCalls.empty()) {
529          // Create a new inline history entry for this, so that we remember
530          // that these new callsites came about due to inlining Callee.
531          int NewHistoryID = InlineHistory.size();
532          InlineHistory.push_back(std::make_pair(Callee, InlineHistoryID));
533
534          for (unsigned i = 0, e = InlineInfo.InlinedCalls.size();
535               i != e; ++i) {
536            Value *Ptr = InlineInfo.InlinedCalls[i];
537            CallSites.push_back(std::make_pair(CallSite(Ptr), NewHistoryID));
538          }
539        }
540      }
541
542      // If we inlined or deleted the last possible call site to the function,
543      // delete the function body now.
544      if (Callee && Callee->use_empty() && Callee->hasLocalLinkage() &&
545          // TODO: Can remove if in SCC now.
546          !SCCFunctions.count(Callee) &&
547
548          // The function may be apparently dead, but if there are indirect
549          // callgraph references to the node, we cannot delete it yet, this
550          // could invalidate the CGSCC iterator.
551          CG[Callee]->getNumReferences() == 0) {
552        DEBUG(dbgs() << "    -> Deleting dead function: "
553              << Callee->getName() << "\n");
554        CallGraphNode *CalleeNode = CG[Callee];
555
556        // Remove any call graph edges from the callee to its callees.
557        CalleeNode->removeAllCalledFunctions();
558
559        // Removing the node for callee from the call graph and delete it.
560        delete CG.removeFunctionFromModule(CalleeNode);
561        ++NumDeleted;
562      }
563
564      // Remove this call site from the list.  If possible, use
565      // swap/pop_back for efficiency, but do not use it if doing so would
566      // move a call site to a function in this SCC before the
567      // 'FirstCallInSCC' barrier.
568      if (SCC.isSingular()) {
569        CallSites[CSi] = CallSites.back();
570        CallSites.pop_back();
571      } else {
572        CallSites.erase(CallSites.begin()+CSi);
573      }
574      --CSi;
575
576      Changed = true;
577      LocalChange = true;
578    }
579  } while (LocalChange);
580
581  return Changed;
582}
583
584// doFinalization - Remove now-dead linkonce functions at the end of
585// processing to avoid breaking the SCC traversal.
586bool Inliner::doFinalization(CallGraph &CG) {
587  return removeDeadFunctions(CG);
588}
589
590/// removeDeadFunctions - Remove dead functions that are not included in
591/// DNR (Do Not Remove) list.
592bool Inliner::removeDeadFunctions(CallGraph &CG, bool AlwaysInlineOnly) {
593  SmallVector<CallGraphNode*, 16> FunctionsToRemove;
594
595  // Scan for all of the functions, looking for ones that should now be removed
596  // from the program.  Insert the dead ones in the FunctionsToRemove set.
597  for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
598    CallGraphNode *CGN = I->second;
599    Function *F = CGN->getFunction();
600    if (!F || F->isDeclaration())
601      continue;
602
603    // Handle the case when this function is called and we only want to care
604    // about always-inline functions. This is a bit of a hack to share code
605    // between here and the InlineAlways pass.
606    if (AlwaysInlineOnly &&
607        !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
608                                         Attribute::AlwaysInline))
609      continue;
610
611    // If the only remaining users of the function are dead constants, remove
612    // them.
613    F->removeDeadConstantUsers();
614
615    if (!F->isDefTriviallyDead())
616      continue;
617
618    // Remove any call graph edges from the function to its callees.
619    CGN->removeAllCalledFunctions();
620
621    // Remove any edges from the external node to the function's call graph
622    // node.  These edges might have been made irrelegant due to
623    // optimization of the program.
624    CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
625
626    // Removing the node for callee from the call graph and delete it.
627    FunctionsToRemove.push_back(CGN);
628  }
629  if (FunctionsToRemove.empty())
630    return false;
631
632  // Now that we know which functions to delete, do so.  We didn't want to do
633  // this inline, because that would invalidate our CallGraph::iterator
634  // objects. :(
635  //
636  // Note that it doesn't matter that we are iterating over a non-stable order
637  // here to do this, it doesn't matter which order the functions are deleted
638  // in.
639  array_pod_sort(FunctionsToRemove.begin(), FunctionsToRemove.end());
640  FunctionsToRemove.erase(std::unique(FunctionsToRemove.begin(),
641                                      FunctionsToRemove.end()),
642                          FunctionsToRemove.end());
643  for (SmallVectorImpl<CallGraphNode *>::iterator I = FunctionsToRemove.begin(),
644                                                  E = FunctionsToRemove.end();
645       I != E; ++I) {
646    delete CG.removeFunctionFromModule(*I);
647    ++NumDeleted;
648  }
649  return true;
650}
651