Fix bug: Dominators/2003-05-12-UnreachableCode.ll
[oota-llvm.git] / lib / Support / Statistic.cpp
index 6345fbfcfcf83a57aeb7964af1280689376b06c2..1141086c3f3c34f2b49998f0446ffe7222db0967 100644 (file)
@@ -20,6 +20,9 @@
 #include <sstream>
 #include <algorithm>
 
+// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
+std::ostream *GetLibSupportInfoOutputFile();
+
 bool DebugFlag;  // DebugFlag - Exported boolean set by the -debug option
 
 unsigned StatisticBase::NumStats = 0;
@@ -30,10 +33,13 @@ unsigned StatisticBase::NumStats = 0;
 static cl::opt<bool>
 Enabled("stats", cl::desc("Enable statistics output from program"));
 
+#ifndef NDEBUG
 // -debug - Command line option to enable the DEBUG statements in the passes.
+// This flag may only be enabled in debug builds.
 static cl::opt<bool, true>
 Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
       cl::location(DebugFlag));
+#endif
 
 struct StatRecord {
   std::string Value;
@@ -46,11 +52,12 @@ struct StatRecord {
     return std::strcmp(Name, SR.Name) < 0;
   }
 
-  void print(unsigned ValFieldSize, unsigned NameFieldSize) {
-    std::cerr << std::string(ValFieldSize-Value.length(), ' ')
-              << Value << " " << Name
-              << std::string(NameFieldSize-std::strlen(Name), ' ')
-              << " - " << Desc << "\n";
+  void print(unsigned ValFieldSize, unsigned NameFieldSize,
+             std::ostream &OS) {
+    OS << std::string(ValFieldSize-Value.length(), ' ')
+       << Value << " " << Name
+       << std::string(NameFieldSize-std::strlen(Name), ' ')
+       << " - " << Desc << "\n";
   }
 };
 
@@ -68,6 +75,8 @@ void StatisticBase::destroy() const {
   }
 
   if (--NumStats == 0 && AccumStats) {
+    std::ostream *OutStream = GetLibSupportInfoOutputFile();
+
     // Figure out how long the biggest Value and Name fields are...
     unsigned MaxNameLen = 0, MaxValLen = 0;
     for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
@@ -81,18 +90,20 @@ void StatisticBase::destroy() const {
     std::stable_sort(AccumStats->begin(), AccumStats->end());
 
     // Print out the statistics header...
-    std::cerr << "===" << std::string(73, '-') << "===\n"
-              << "                          ... Statistics Collected ...\n"
-              << "===" << std::string(73, '-') << "===\n\n";
+    *OutStream << "===" << std::string(73, '-') << "===\n"
+               << "                          ... Statistics Collected ...\n"
+               << "===" << std::string(73, '-') << "===\n\n";
 
     // Print all of the statistics accumulated...
     for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
-      (*AccumStats)[i].print(MaxValLen, MaxNameLen);
+      (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
 
-    std::cerr << std::endl;  // Flush the output stream...
+    *OutStream << std::endl;  // Flush the output stream...
 
     // Free all accumulated statistics...
     delete AccumStats;
     AccumStats = 0;
+    if (OutStream != &std::cerr && OutStream != &std::cout)
+      delete OutStream;   // Close the file...
   }
 }