[llvm-commits] CVS: llvm/tools/bugpoint/BugDriver.cpp BugDriver.h CodeGeneratorBug.cpp ExtractFunction.cpp Miscompilation.cpp

Chris Lattner lattner at cs.uiuc.edu
Sun Mar 14 13:28:01 PST 2004


Changes in directory llvm/tools/bugpoint:

BugDriver.cpp updated: 1.29 -> 1.30
BugDriver.h updated: 1.30 -> 1.31
CodeGeneratorBug.cpp updated: 1.38 -> 1.39
ExtractFunction.cpp updated: 1.24 -> 1.25
Miscompilation.cpp updated: 1.28 -> 1.29

---
Log message:

Refactor all of the "splitting a module into two pieces" code to avoid
code duplication.  Also, don't use ReduceMiscompilingFunctions::TestFuncs
to print out the final message.


---
Diffs of the changes:  (+92 -129)

Index: llvm/tools/bugpoint/BugDriver.cpp
diff -u llvm/tools/bugpoint/BugDriver.cpp:1.29 llvm/tools/bugpoint/BugDriver.cpp:1.30
--- llvm/tools/bugpoint/BugDriver.cpp:1.29	Thu Feb 19 23:56:48 2004
+++ llvm/tools/bugpoint/BugDriver.cpp	Sun Mar 14 13:27:19 2004
@@ -60,15 +60,6 @@
   return Result;
 }
 
-// DeleteFunctionBody - "Remove" the function by deleting all of its basic
-// blocks, making it external.
-//
-void llvm::DeleteFunctionBody(Function *F) {
-  // delete the body of the function...
-  F->deleteBody();
-  assert(F->isExternal() && "This didn't make the function external!");
-}
-
 BugDriver::BugDriver(const char *toolname)
   : ToolName(toolname), ReferenceOutputFile(OutputFile),
     Program(0), Interpreter(0), cbe(0), gcc(0) {}


Index: llvm/tools/bugpoint/BugDriver.h
diff -u llvm/tools/bugpoint/BugDriver.h:1.30 llvm/tools/bugpoint/BugDriver.h:1.31
--- llvm/tools/bugpoint/BugDriver.h:1.30	Wed Feb 18 17:25:22 2004
+++ llvm/tools/bugpoint/BugDriver.h	Sun Mar 14 13:27:19 2004
@@ -231,6 +231,11 @@
 //
 void DeleteFunctionBody(Function *F);
 
+/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
+/// module, split the functions OUT of the specified module, and place them in
+/// the new module.
+Module *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F);
+
 } // End llvm namespace
 
 #endif


Index: llvm/tools/bugpoint/CodeGeneratorBug.cpp
diff -u llvm/tools/bugpoint/CodeGeneratorBug.cpp:1.38 llvm/tools/bugpoint/CodeGeneratorBug.cpp:1.39
--- llvm/tools/bugpoint/CodeGeneratorBug.cpp:1.38	Fri Mar 12 15:37:46 2004
+++ llvm/tools/bugpoint/CodeGeneratorBug.cpp	Sun Mar 14 13:27:19 2004
@@ -63,40 +63,15 @@
   std::cout << "\t";
 
   // Clone the module for the two halves of the program we want.
-  Module *SafeModule = CloneModule(BD.Program);
+  Module *SafeModule = CloneModule(BD.getProgram());
 
-  // Make sure functions & globals are all external so that linkage
-  // between the two modules will work.
-  for (Module::iterator I = SafeModule->begin(), E = SafeModule->end();I!=E;++I)
-    I->setLinkage(GlobalValue::ExternalLinkage);
-  for (Module::giterator I=SafeModule->gbegin(),E = SafeModule->gend();I!=E;++I)
-    I->setLinkage(GlobalValue::ExternalLinkage);
-
-  Module *TestModule = CloneModule(SafeModule);
-
-  // Make sure global initializers exist only in the safe module (CBE->.so)
-  for (Module::giterator I=TestModule->gbegin(),E = TestModule->gend();I!=E;++I)
-    I->setInitializer(0);  // Delete the initializer to make it external
-
-  // Remove the Test functions from the Safe module
-  for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
-    Function *TNOF = SafeModule->getFunction(Funcs[i]->getName(),
-                                             Funcs[i]->getFunctionType());
-    DEBUG(std::cerr << "Removing function " << Funcs[i]->getName() << "\n");
-    assert(TNOF && "Function doesn't exist in module!");
-    DeleteFunctionBody(TNOF);       // Function is now external in this module!
-  }
-
-  // Remove the Safe functions from the Test module
-  for (Module::iterator I=TestModule->begin(),E=TestModule->end(); I!=E; ++I) {
-    bool funcFound = false;
-    for (std::vector<Function*>::const_iterator F=Funcs.begin(),Fe=Funcs.end();
-         F != Fe; ++F)
-      if (I->getName() == (*F)->getName()) funcFound = true;
-
-    if (!funcFound && !(BD.isExecutingJIT() && I->getName() == "main"))
-      DeleteFunctionBody(I);
+  // The JIT must extract the 'main' function.
+  std::vector<Function*> RealFuncs(Funcs);
+  if (BD.isExecutingJIT()) {
+    if (Function *F = BD.Program->getMainFunction())
+      RealFuncs.push_back(F);
   }
+  Module *TestModule = SplitFunctionsOutOfModule(SafeModule, RealFuncs);
 
   // This is only applicable if we are debugging the JIT:
   // Find all external functions in the Safe modules that are actually used


Index: llvm/tools/bugpoint/ExtractFunction.cpp
diff -u llvm/tools/bugpoint/ExtractFunction.cpp:1.24 llvm/tools/bugpoint/ExtractFunction.cpp:1.25
--- llvm/tools/bugpoint/ExtractFunction.cpp:1.24	Sat Mar 13 13:35:54 2004
+++ llvm/tools/bugpoint/ExtractFunction.cpp	Sun Mar 14 13:27:19 2004
@@ -24,6 +24,7 @@
 #include "llvm/Transforms/Utils/Cloning.h"
 #include "llvm/Target/TargetData.h"
 #include "Support/CommandLine.h"
+#include "Support/Debug.h"
 #include "Support/FileUtilities.h"
 using namespace llvm;
 
@@ -128,4 +129,53 @@
     removeFile(Filename);
   }
   return M;
+}
+
+
+// DeleteFunctionBody - "Remove" the function by deleting all of its basic
+// blocks, making it external.
+//
+void llvm::DeleteFunctionBody(Function *F) {
+  // delete the body of the function...
+  F->deleteBody();
+  assert(F->isExternal() && "This didn't make the function external!");
+}
+
+/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
+/// module, split the functions OUT of the specified module, and place them in
+/// the new module.
+Module *llvm::SplitFunctionsOutOfModule(Module *M,
+                                        const std::vector<Function*> &F) {
+  // Make sure functions & globals are all external so that linkage
+  // between the two modules will work.
+  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
+    I->setLinkage(GlobalValue::ExternalLinkage);
+  for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
+    I->setLinkage(GlobalValue::ExternalLinkage);
+
+  Module *New = CloneModule(M);
+
+  // Make sure global initializers exist only in the safe module (CBE->.so)
+  for (Module::giterator I = New->gbegin(), E = New->gend(); I != E; ++I)
+    I->setInitializer(0);  // Delete the initializer to make it external
+
+  // Remove the Test functions from the Safe module
+  for (unsigned i = 0, e = F.size(); i != e; ++i) {
+    Function *TNOF = M->getFunction(F[i]->getName(), F[i]->getFunctionType());
+    DEBUG(std::cerr << "Removing function " << F[i]->getName() << "\n");
+    assert(TNOF && "Function doesn't exist in module!");
+    DeleteFunctionBody(TNOF);       // Function is now external in this module!
+  }
+
+  // Remove the Safe functions from the Test module
+  for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I) {
+    bool funcFound = false;
+    for (std::vector<Function*>::const_iterator FI = F.begin(), Fe = F.end();
+         FI != Fe; ++FI)
+      if (I->getName() == (*FI)->getName()) funcFound = true;
+
+    if (!funcFound)
+      DeleteFunctionBody(I);
+  }
+  return New;
 }


Index: llvm/tools/bugpoint/Miscompilation.cpp
diff -u llvm/tools/bugpoint/Miscompilation.cpp:1.28 llvm/tools/bugpoint/Miscompilation.cpp:1.29
--- llvm/tools/bugpoint/Miscompilation.cpp:1.28	Wed Feb 18 15:29:46 2004
+++ llvm/tools/bugpoint/Miscompilation.cpp	Sun Mar 14 13:27:19 2004
@@ -132,93 +132,37 @@
     
     virtual TestResult doTest(std::vector<Function*> &Prefix,
                               std::vector<Function*> &Suffix) {
-      if (!Suffix.empty() && TestFuncs(Suffix, false))
+      if (!Suffix.empty() && TestFuncs(Suffix))
         return KeepSuffix;
-      if (!Prefix.empty() && TestFuncs(Prefix, false))
+      if (!Prefix.empty() && TestFuncs(Prefix))
         return KeepPrefix;
       return NoFailure;
     }
     
-    bool TestFuncs(const std::vector<Function*> &Prefix, bool EmitBytecode);
+    bool TestFuncs(const std::vector<Function*> &Prefix);
   };
 }
 
-bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
-                                            bool EmitBytecode) {
+bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
   // 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 "
-              << (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 {
-    std::cout <<"Outputting reduced bytecode files which expose the problem:\n";
-  }
+  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";
 
-  // First step: clone the module for the two halves of the program we want.
+  // Split the module into the two halves of the program we want.
   Module *ToOptimize = CloneModule(BD.getProgram());
+  Module *ToNotOptimize = SplitFunctionsOutOfModule(ToOptimize, Funcs);
 
-  // Second step: Make sure functions & globals are all external so that linkage
-  // between the two modules will work.
-  for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
-    I->setLinkage(GlobalValue::ExternalLinkage);
-  for (Module::giterator I = ToOptimize->gbegin(), E = ToOptimize->gend();
-       I != E; ++I)
-    I->setLinkage(GlobalValue::ExternalLinkage);
-
-  // Third step: make a clone of the externalized program for the non-optimized
-  // part.
-  Module *ToNotOptimize = CloneModule(ToOptimize);
-
-  // Fourth step: Remove the test functions from the ToNotOptimize module, and
-  // all of the global variables.
-  for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
-    Function *TNOF = ToNotOptimize->getFunction(Funcs[i]->getName(),
-                                                Funcs[i]->getFunctionType());
-    assert(TNOF && "Function doesn't exist in module!");
-    DeleteFunctionBody(TNOF);       // Function is now external in this module!
-  }
-  for (Module::giterator I = ToNotOptimize->gbegin(), E = ToNotOptimize->gend();
-       I != E; ++I)
-    I->setInitializer(0);  // Delete the initializer to make it external
-
-  if (EmitBytecode) {
-    std::cout << "  Non-optimized portion: ";
-    std::swap(BD.Program, ToNotOptimize);
-    BD.EmitProgressBytecode("tonotoptimize", true);
-    std::swap(BD.Program, ToNotOptimize);
-  }
-
-  // Fifth step: Remove all functions from the ToOptimize module EXCEPT for the
-  // ones specified in Funcs.  We know which ones these are because they are
-  // non-external in ToOptimize, but external in ToNotOptimize.
-  //
-  for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
-    if (!I->isExternal()) {
-      Function *TNOF = ToNotOptimize->getFunction(I->getName(),
-                                                  I->getFunctionType());
-      assert(TNOF && "Function doesn't exist in ToNotOptimize module??");
-      if (!TNOF->isExternal())
-        DeleteFunctionBody(I);
-    }
-
-  if (EmitBytecode) {
-    std::cout << "  Portion that is input to optimizer: ";
-    std::swap(BD.Program, ToOptimize);
-    BD.EmitProgressBytecode("tooptimize");
-    std::swap(BD.Program, ToOptimize);
-  }
-
-  // Sixth step: Run the optimization passes on ToOptimize, producing a
-  // transformed version of the functions being tested.
+  // Run the optimization passes on ToOptimize, producing a transformed version
+  // of the functions being tested.
   Module *OldProgram = BD.Program;
   BD.Program = ToOptimize;
 
-  if (!EmitBytecode)
-    std::cout << "  Optimizing functions being tested: ";
+  std::cout << "  Optimizing functions being tested: ";
   std::string BytecodeResult;
   if (BD.runPasses(BD.PassesToRun, BytecodeResult, false/*delete*/,
                    true/*quiet*/)) {
@@ -228,17 +172,11 @@
     exit(BD.debugOptimizerCrash());
   }
 
-  if (!EmitBytecode)
-    std::cout << "done.\n";
+  std::cout << "done.\n";
 
   delete BD.getProgram();   // Delete the old "ToOptimize" module
   BD.Program = BD.ParseInputFile(BytecodeResult);
 
-  if (EmitBytecode) {
-    std::cout << "  'tooptimize' after being optimized: ";
-    BD.EmitProgressBytecode("optimized", true);
-  }
-
   if (BD.Program == 0) {
     std::cerr << BD.getToolName() << ": Error reading bytecode file '"
               << BytecodeResult << "'!\n";
@@ -256,14 +194,6 @@
   }
   delete ToNotOptimize;  // We are done with this module...
 
-  if (EmitBytecode) {
-    std::cout << "  Program as tested: ";
-    BD.EmitProgressBytecode("linked", true);
-    delete BD.Program;
-    BD.Program = OldProgram;
-    return false;   // We don't need to actually execute the program here.
-  }
-
   std::cout << "  Checking to see if the merged program executes correctly: ";
 
   // Eighth step: Execute the program.  If it does not match the expected
@@ -277,7 +207,6 @@
   return Broken;
 }
 
-
 /// debugMiscompilation - This method is used when the passes selected are not
 /// crashing, but the generated output is semantically different from the
 /// input.
@@ -314,7 +243,20 @@
   std::cout << "\n";
 
   // Output a bunch of bytecode files for the user...
-  ReduceMiscompilingFunctions(*this).TestFuncs(MiscompiledFunctions, true);
+  std::cout << "Outputting reduced bytecode files which expose the problem:\n";
+  Module *ToOptimize = CloneModule(getProgram());
+  Module *ToNotOptimize = SplitFunctionsOutOfModule(ToOptimize,
+                                                    MiscompiledFunctions);
+
+  std::cout << "  Non-optimized portion: ";
+  std::swap(Program, ToNotOptimize);
+  EmitProgressBytecode("tonotoptimize", true);
+  setNewProgram(ToNotOptimize);   // Delete hacked module.
+  
+  std::cout << "  Portion that is input to optimizer: ";
+  std::swap(Program, ToOptimize);
+  EmitProgressBytecode("tooptimize");
+  setNewProgram(ToOptimize);      // Delete hacked module.
 
   return false;
 }





More information about the llvm-commits mailing list