Use isNull instead of getNode() to test for existence of a node, this is cheaper.
[oota-llvm.git] / tools / bugpoint / Miscompilation.cpp
index 833dd437b63129ba48ef42b1a1e9d73199ee8322..98e299d76d82ddce5d5677e024ddf90ddf2ab21d 100644 (file)
@@ -1,25 +1,36 @@
 //===- Miscompilation.cpp - Debug program miscompilations -----------------===//
+// 
+//                     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 program miscompilation debugging support.
 //
 //===----------------------------------------------------------------------===//
 
 #include "BugDriver.h"
-#include "SystemUtils.h"
 #include "ListReducer.h"
-#include "llvm/Pass.h"
 #include "llvm/Module.h"
+#include "llvm/Pass.h"
 #include "llvm/Transforms/Utils/Cloning.h"
 #include "llvm/Transforms/Utils/Linker.h"
+#include "Support/FileUtilities.h"
+using namespace llvm;
 
-class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
-  BugDriver &BD;
-public:
-  ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
+namespace llvm {
 
-  virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
-                            std::vector<const PassInfo*> &Suffix);
-};
+  class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
+    BugDriver &BD;
+  public:
+    ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
+    
+    virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
+                              std::vector<const PassInfo*> &Suffix);
+  };
+}
 
 ReduceMiscompilingPasses::TestResult
 ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
@@ -31,9 +42,11 @@ ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
 
   std::string BytecodeResult;
   if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
-    std::cerr << BD.getToolName() << ": Error running this sequence of passes"
+    std::cerr << " Error running this sequence of passes" 
               << " on the input program!\n";
-    exit(1);
+    BD.setPassesToRun(Suffix);
+    BD.EmitProgressBytecode("pass-error",  false);
+    exit(BD.debugOptimizerCrash());
   }
 
   // Check to see if the finished program matches the reference output...
@@ -57,9 +70,11 @@ ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
   // prefix passes, then discard the prefix passes.
   //
   if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
-    std::cerr << BD.getToolName() << ": Error running this sequence of passes"
+    std::cerr << " Error running this sequence of passes" 
               << " on the input program!\n";
-    exit(1);
+    BD.setPassesToRun(Prefix);
+    BD.EmitProgressBytecode("pass-error",  false);
+    exit(BD.debugOptimizerCrash());
   }
 
   // If the prefix maintains the predicate by itself, only keep the prefix!
@@ -88,9 +103,11 @@ ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
   Module *OriginalInput = BD.Program;
   BD.Program = PrefixOutput;
   if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
-    std::cerr << BD.getToolName() << ": Error running this sequence of passes"
+    std::cerr << " Error running this sequence of passes" 
               << " on the input program!\n";
-    exit(1);
+    BD.setPassesToRun(Suffix);
+    BD.EmitProgressBytecode("pass-error",  false);
+    exit(BD.debugOptimizerCrash());
   }
 
   // Run the result...
@@ -107,30 +124,34 @@ ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
   return NoFailure;
 }
 
-class ReduceMiscompilingFunctions : public ListReducer<Function*> {
-  BugDriver &BD;
-public:
-  ReduceMiscompilingFunctions(BugDriver &bd) : BD(bd) {}
-
-  virtual TestResult doTest(std::vector<Function*> &Prefix,
-                            std::vector<Function*> &Suffix) {
-    if (TestFuncs(Suffix, false))
-      return KeepSuffix;
-    if (!Prefix.empty() && TestFuncs(Prefix, false))
-      return KeepPrefix;
-    return NoFailure;
-  }
-  
-  bool TestFuncs(const std::vector<Function*> &Prefix, bool EmitBytecode);
-};
+namespace llvm {
+  class ReduceMiscompilingFunctions : public ListReducer<Function*> {
+    BugDriver &BD;
+  public:
+    ReduceMiscompilingFunctions(BugDriver &bd) : BD(bd) {}
+    
+    virtual TestResult doTest(std::vector<Function*> &Prefix,
+                              std::vector<Function*> &Suffix) {
+      if (!Suffix.empty() && TestFuncs(Suffix, false))
+        return KeepSuffix;
+      if (!Prefix.empty() && TestFuncs(Prefix, false))
+        return KeepPrefix;
+      return NoFailure;
+    }
+    
+    bool TestFuncs(const std::vector<Function*> &Prefix, bool EmitBytecode);
+  };
+}
 
 bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
                                             bool EmitBytecode) {
   // Test to see if the function is misoptimized if we ONLY run it on the
   // functions listed in Funcs.
   if (!EmitBytecode) {
-    std::cout << "Checking to see if the program is misoptimized when these "
-              << "functions are run\nthrough the passes: ";
+    std::cout << "Checking to see if the program is misoptimized when "
+              << (Funcs.size()==1 ? "this function is" : "these functions are")
+              << " run through the pass"
+              << (BD.PassesToRun.size() == 1 ? "" : "es") << ": ";
     BD.PrintFunctionList(Funcs);
     std::cout << "\n";
   } else {
@@ -138,7 +159,7 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
   }
 
   // First step: clone the module for the two halves of the program we want.
-  Module *ToOptimize = CloneModule(BD.Program);
+  Module *ToOptimize = CloneModule(BD.getProgram());
 
   // Second step: Make sure functions & globals are all external so that linkage
   // between the two modules will work.
@@ -201,15 +222,16 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
   std::string BytecodeResult;
   if (BD.runPasses(BD.PassesToRun, BytecodeResult, false/*delete*/,
                    true/*quiet*/)) {
-    std::cerr << BD.getToolName() << ": Error running this sequence of passes"
+    std::cerr << " Error running this sequence of passes" 
               << " on the input program!\n";
-    exit(1);
+    BD.EmitProgressBytecode("pass-error",  false);
+    exit(BD.debugOptimizerCrash());
   }
 
   if (!EmitBytecode)
     std::cout << "done.\n";
 
-  delete BD.Program;   // Delete the old "ToOptimize" module
+  delete BD.getProgram();   // Delete the old "ToOptimize" module
   BD.Program = BD.ParseInputFile(BytecodeResult);
 
   if (EmitBytecode) {
@@ -248,10 +270,10 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
   // output, then 'Funcs' are being misoptimized!
   bool Broken = BD.diffProgram();
 
-  delete BD.Program;  // Delete the hacked up program
+  delete BD.Program;         // Delete the hacked up program
   BD.Program = OldProgram;   // Restore the original
 
-  std::cout << (Broken ? "nope.\n" : "yup.\n");
+  std::cout << (Broken ? " nope.\n" : " yup.\n");
   return Broken;
 }
 
@@ -261,19 +283,8 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
 /// input.
 ///
 bool BugDriver::debugMiscompilation() {
-
-  if (diffProgram()) {
-    std::cout << "\n*** Input program does not match reference diff!\n"
-              << "    Must be problem with input source!\n";
-    return false;  // Problem found
-  }
-
-  // Figure out which transformations miscompile the input program.
-  unsigned OldSize = PassesToRun.size();
-  ReduceMiscompilingPasses(*this).reduceList(PassesToRun);
-
   // Make sure something was miscompiled...
-  if (PassesToRun.size() == OldSize) {
+  if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
     std::cerr << "*** Optimized program matches reference output!  No problem "
              << "detected...\nbugpoint can't help you with your problem!\n";
     return false;
@@ -296,7 +307,9 @@ bool BugDriver::debugMiscompilation() {
   // Do the reduction...
   ReduceMiscompilingFunctions(*this).reduceList(MiscompiledFunctions);
 
-  std::cout << "\n*** The following functions are being miscompiled: ";
+  std::cout << "\n*** The following function"
+            << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
+            << " being miscompiled: ";
   PrintFunctionList(MiscompiledFunctions);
   std::cout << "\n";
 
@@ -305,3 +318,4 @@ bool BugDriver::debugMiscompilation() {
 
   return false;
 }
+