Eliminate static ctors due to Statistic objects
[oota-llvm.git] / lib / Transforms / IPO / Inliner.cpp
index a10879a15079e8ea5a54feeeedf0ed3f7e82cbbb..27dbf8ba50fd3e1f292efb3b89b527e9636bdb38 100644 (file)
@@ -13,6 +13,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#define DEBUG_TYPE "inline"
 #include "Inliner.h"
 #include "llvm/Module.h"
 #include "llvm/Instructions.h"
 #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)"));
@@ -40,31 +41,19 @@ Inliner::Inliner() : InlineThreshold(InlineLimit) {}
 // 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)) return false;
-
-  // Update the call graph by deleting the edge from Callee to Caller
-  CallGraphNode *CalleeNode = CG[Callee];
-  CallGraphNode *CallerNode = CG[Caller];
-  CallerNode->removeCallEdgeTo(CalleeNode);
-
-  // Since we inlined all uninlined call sites in the callee into the caller,
-  // add edges from the caller to all of the callees of the callee.
-  for (CallGraphNode::iterator I = CalleeNode->begin(),
-         E = CalleeNode->end(); I != E; ++I)
-    CallerNode->addCalledFunction(*I);
+  if (!InlineFunction(CS, &CG)) return false;
 
   // If we inlined the last possible call site to the function, delete the
   // 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);
@@ -77,11 +66,11 @@ bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
   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
@@ -99,7 +88,7 @@ bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
             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.
@@ -122,8 +111,13 @@ bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
         // 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;
         }
@@ -133,19 +127,24 @@ bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
         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;
@@ -178,7 +177,7 @@ bool Inliner::doFinalization(CallGraph &CG) {
 
         // 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