diff --git a/tools/bugpoint/BugDriver.cpp b/tools/bugpoint/BugDriver.cpp index c770299e4d9..2c5cf24890f 100644 --- a/tools/bugpoint/BugDriver.cpp +++ b/tools/bugpoint/BugDriver.cpp @@ -60,15 +60,6 @@ std::string llvm::getPassesString(const std::vector &Passes) { 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) {} diff --git a/tools/bugpoint/BugDriver.h b/tools/bugpoint/BugDriver.h index 297bab53b4e..f15481fde72 100644 --- a/tools/bugpoint/BugDriver.h +++ b/tools/bugpoint/BugDriver.h @@ -231,6 +231,11 @@ std::string getPassesString(const std::vector &Passes); // 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 &F); + } // End llvm namespace #endif diff --git a/tools/bugpoint/CodeGeneratorBug.cpp b/tools/bugpoint/CodeGeneratorBug.cpp index 4da709a3f8c..449aadd62f2 100644 --- a/tools/bugpoint/CodeGeneratorBug.cpp +++ b/tools/bugpoint/CodeGeneratorBug.cpp @@ -63,40 +63,15 @@ bool ReduceMisCodegenFunctions::TestFuncs(const std::vector &Funcs, 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::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 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 diff --git a/tools/bugpoint/ExtractFunction.cpp b/tools/bugpoint/ExtractFunction.cpp index b9298d3db35..29ad23c1495 100644 --- a/tools/bugpoint/ExtractFunction.cpp +++ b/tools/bugpoint/ExtractFunction.cpp @@ -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; @@ -129,3 +130,52 @@ Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) { } 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 &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::const_iterator FI = F.begin(), Fe = F.end(); + FI != Fe; ++FI) + if (I->getName() == (*FI)->getName()) funcFound = true; + + if (!funcFound) + DeleteFunctionBody(I); + } + return New; +} diff --git a/tools/bugpoint/Miscompilation.cpp b/tools/bugpoint/Miscompilation.cpp index 98e299d76d8..29274c9df01 100644 --- a/tools/bugpoint/Miscompilation.cpp +++ b/tools/bugpoint/Miscompilation.cpp @@ -132,93 +132,37 @@ namespace llvm { virtual TestResult doTest(std::vector &Prefix, std::vector &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 &Prefix, bool EmitBytecode); + bool TestFuncs(const std::vector &Prefix); }; } -bool ReduceMiscompilingFunctions::TestFuncs(const std::vector &Funcs, - bool EmitBytecode) { +bool ReduceMiscompilingFunctions::TestFuncs(const std::vector&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 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector &Funcs, 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 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector &Funcs, } 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 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector &Funcs, 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 @@ bool BugDriver::debugMiscompilation() { 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; }