//
//===----------------------------------------------------------------------===//
+#define DEBUG_TYPE "inline"
#include "Inliner.h"
#include "llvm/Module.h"
#include "llvm/Instructions.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/ADT/Statistic.h"
-#include <iostream>
#include <set>
using namespace llvm;
+STATISTIC(NumInlined, "Number of functions inlined");
+STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
+
namespace {
- Statistic<> NumInlined("inline", "Number of functions inlined");
- Statistic<> NumDeleted("inline",
- "Number of functions deleted because all callers found");
cl::opt<unsigned> // FIXME: 200 is VERY conservative
InlineLimit("inline-threshold", cl::Hidden, cl::init(200),
cl::desc("Control the amount of inlining to perform (default = 200)"));
// do so and update the CallGraph for this operation.
static bool InlineCallIfPossible(CallSite CS, CallGraph &CG,
const std::set<Function*> &SCCFunctions) {
- Function *Caller = CS.getInstruction()->getParent()->getParent();
Function *Callee = CS.getCalledFunction();
if (!InlineFunction(CS, &CG)) return false;
// function body now.
if (Callee->use_empty() && Callee->hasInternalLinkage() &&
!SCCFunctions.count(Callee)) {
- DEBUG(std::cerr << " -> Deleting dead function: "
- << Callee->getName() << "\n");
+ DOUT << " -> Deleting dead function: " << Callee->getName() << "\n";
// Remove any call graph edges from the callee to its callees.
CallGraphNode *CalleeNode = CG[Callee];
while (CalleeNode->begin() != CalleeNode->end())
- CalleeNode->removeCallEdgeTo(*(CalleeNode->end()-1));
+ CalleeNode->removeCallEdgeTo((CalleeNode->end()-1)->second);
// Removing the node for callee from the call graph and delete it.
delete CG.removeFunctionFromModule(CalleeNode);
CallGraph &CG = getAnalysis<CallGraph>();
std::set<Function*> SCCFunctions;
- DEBUG(std::cerr << "Inliner visiting SCC:");
+ DOUT << "Inliner visiting SCC:";
for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
Function *F = SCC[i]->getFunction();
if (F) SCCFunctions.insert(F);
- DEBUG(std::cerr << " " << (F ? F->getName() : "INDIRECTNODE"));
+ DOUT << " " << (F ? F->getName() : "INDIRECTNODE");
}
// Scan through and identify all call sites ahead of time so that we only
CallSites.push_back(CS);
}
- DEBUG(std::cerr << ": " << CallSites.size() << " call sites.\n");
+ DOUT << ": " << CallSites.size() << " call sites.\n";
// Now that we have all of the call sites, move the ones to functions in the
// current SCC to the end of the list.
// Calls to external functions are never inlinable.
if (Callee->isExternal() ||
CallSites[CSi].getInstruction()->getParent()->getParent() ==Callee){
- std::swap(CallSites[CSi], CallSites.back());
- CallSites.pop_back();
+ if (SCC.size() == 1) {
+ std::swap(CallSites[CSi], CallSites.back());
+ CallSites.pop_back();
+ } else {
+ // Keep the 'in SCC / not in SCC' boundary correct.
+ CallSites.erase(CallSites.begin()+CSi);
+ }
--CSi;
continue;
}
CallSite CS = CallSites[CSi];
int InlineCost = getInlineCost(CS);
if (InlineCost >= (int)InlineThreshold) {
- DEBUG(std::cerr << " NOT Inlining: cost=" << InlineCost
- << ", Call: " << *CS.getInstruction());
+ DOUT << " NOT Inlining: cost=" << InlineCost
+ << ", Call: " << *CS.getInstruction();
} else {
- DEBUG(std::cerr << " Inlining: cost=" << InlineCost
- << ", Call: " << *CS.getInstruction());
-
- Function *Caller = CS.getInstruction()->getParent()->getParent();
+ DOUT << " Inlining: cost=" << InlineCost
+ << ", Call: " << *CS.getInstruction();
// Attempt to inline the function...
if (InlineCallIfPossible(CS, CG, SCCFunctions)) {
- // Remove this call site from the list.
- std::swap(CallSites[CSi], CallSites.back());
- CallSites.pop_back();
+ // Remove this call site from the list. If possible, use
+ // swap/pop_back for efficiency, but do not use it if doing so would
+ // move a call site to a function in this SCC before the
+ // 'FirstCallInSCC' barrier.
+ if (SCC.size() == 1) {
+ std::swap(CallSites[CSi], CallSites.back());
+ CallSites.pop_back();
+ } else {
+ CallSites.erase(CallSites.begin()+CSi);
+ }
--CSi;
++NumInlined;
// Remove any call graph edges from the function to its callees.
while (CGN->begin() != CGN->end())
- CGN->removeCallEdgeTo(*(CGN->end()-1));
+ CGN->removeCallEdgeTo((CGN->end()-1)->second);
// Remove any edges from the external node to the function's call graph
// node. These edges might have been made irrelegant due to