Eliminate static ctors due to Statistic objects
[oota-llvm.git] / lib / Transforms / IPO / Inliner.cpp
index 86d343af6a04643bebca206e28b75326f3b4a842..27dbf8ba50fd3e1f292efb3b89b527e9636bdb38 100644 (file)
@@ -1,10 +1,10 @@
 //===- Inliner.cpp - Code common to all inliners --------------------------===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
 // This file was developed by the LLVM research group and is distributed under
 // the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+//
 //===----------------------------------------------------------------------===//
 //
 // This file implements the mechanics required to implement inlining without
@@ -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)"));
+        cl::desc("Control the amount of inlining to perform (default = 200)"));
 }
 
 Inliner::Inliner() : InlineThreshold(InlineLimit) {}
@@ -39,32 +41,20 @@ 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);
     ++NumDeleted;
@@ -76,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
@@ -98,8 +88,8 @@ 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.
   unsigned FirstCallInSCC = CallSites.size();
@@ -107,7 +97,7 @@ bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
     if (Function *F = CallSites[i].getCalledFunction())
       if (SCCFunctions.count(F))
         std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
-  
+
   // Now that we have all of the call sites, loop over them and inline them if
   // it looks profitable to do so.
   bool Changed = false;
@@ -121,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;
         }
@@ -132,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;
@@ -168,23 +168,22 @@ bool Inliner::doFinalization(CallGraph &CG) {
   for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
     CallGraphNode *CGN = I->second;
     if (Function *F = CGN ? CGN->getFunction() : 0) {
-      // If the only remaining users of the function are dead constants,
-      // remove them.
-      bool HadDeadConstantUsers = !F->use_empty();
+      // If the only remaining users of the function are dead constants, remove
+      // them.
       F->removeDeadConstantUsers();
 
       if ((F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
           F->use_empty()) {
+
         // Remove any call graph edges from the function to its callees.
         while (CGN->begin() != CGN->end())
-          CGN->removeCallEdgeTo(*(CGN->end()-1));
-        
-        // If the function has external linkage (basically if it's a linkonce
-        // function) remove the edge from the external node to the callee
-        // node.
-        if (!F->hasInternalLinkage() || HadDeadConstantUsers)
-          CG.getExternalCallingNode()->removeCallEdgeTo(CGN);
-        
+          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
+        // optimization of the program.
+        CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
+
         // Removing the node for callee from the call graph and delete it.
         FunctionsToRemove.insert(CGN);
       }