Added copyright header to all C++ source files.
[oota-llvm.git] / tools / bugpoint / OptimizerDriver.cpp
index 610c524c26c2b274d23169e78479defd6bf86b2c..424a7aad140a96a7827dfdece1bd93906566af7d 100644 (file)
@@ -1,24 +1,31 @@
 //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
+// 
+//                     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 defines an interface that allows bugpoint to run various passes
-// without the threat of a buggy pass corrupting bugpoint (of course bugpoint
-// may have it's own bugs, but that's another story...).  It acheives this by
+// without the threat of a buggy pass corrupting bugpoint (of course, bugpoint
+// may have its own bugs, but that's another story...).  It achieves this by
 // forking a copy of itself and having the child process do the optimizations.
 // If this client dies, we can always fork a new one.  :)
 //
 //===----------------------------------------------------------------------===//
 
 #include "BugDriver.h"
-#include "SystemUtils.h"
 #include "llvm/PassManager.h"
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Bytecode/WriteBytecodePass.h"
 #include "llvm/Target/TargetData.h"
+#include "Support/FileUtilities.h"
+#include <fstream>
+#include <unistd.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <fstream>
 
 /// writeProgramToFile - This writes the current "Program" to the named bytecode
 /// file.  If an error occurs, true is returned.
@@ -33,7 +40,7 @@ bool BugDriver::writeProgramToFile(const std::string &Filename,
 
 
 /// EmitProgressBytecode - This function is used to output the current Program
-/// to a file named "bugpoing-ID.bc".
+/// to a file named "bugpoint-ID.bc".
 ///
 void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) {
   // Output the input to the current pass to a bytecode file, emit a message
@@ -65,9 +72,6 @@ void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) {
   std::cout << getPassesString(PassesToRun) << "\n";
 }
 
-/// FIXME: This should be parameterizable!!
-static TargetData TD("bugpoint target");
-
 static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes,
                      const std::string &OutFilename) {
   std::ofstream OutFile(OutFilename.c_str());
@@ -77,11 +81,12 @@ static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes,
   }
 
   PassManager PM;
+  // Make sure that the appropriate target data is always used...
+  PM.add(new TargetData("bugpoint", Program));
+
   for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
     if (Passes[i]->getNormalCtor())
       PM.add(Passes[i]->getNormalCtor()());
-    else if (Passes[i]->getDataCtor())
-      PM.add(Passes[i]->getDataCtor()(TD));    // Provide dummy target data...
     else
       std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName()
                 << "\n";
@@ -97,7 +102,7 @@ static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes,
 }
 
 /// runPasses - Run the specified passes on Program, outputting a bytecode file
-/// and writting the filename into OutputFile if successful.  If the
+/// and writing the filename into OutputFile if successful.  If the
 /// optimizations fail for some reason (optimizer crashes), return true,
 /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
 /// deleted on success, and the filename string is undefined.  This prints to
@@ -135,8 +140,23 @@ bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
   if (DeleteOutput)
     removeFile(OutputFilename);
 
-  if (!Quiet) std::cout << (Status ? "Crashed!\n" : "Success!\n");
+  bool ExitedOK = WIFEXITED(Status) && WEXITSTATUS(Status) == 0;
+  
+  if (!Quiet) {
+    if (ExitedOK)
+      std::cout << "Success!\n";
+    else if (WIFEXITED(Status))
+      std::cout << "Exited with error code '" << WEXITSTATUS(Status) << "'\n";
+    else if (WIFSIGNALED(Status))
+      std::cout << "Crashed with signal #" << WTERMSIG(Status) << "\n";
+#ifdef WCOREDUMP
+    else if (WCOREDUMP(Status))
+      std::cout << "Dumped core\n";
+#endif
+    else
+      std::cout << "Failed for unknown reason!\n";
+  }
 
   // Was the child successful?
-  return Status != 0;
+  return !ExitedOK;
 }