X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=tools%2Fbugpoint%2FOptimizerDriver.cpp;h=3ded5e866b3e025baaea3eee40a49dcd3df6ff2d;hb=73b43b9b549a75fb0015c825df68abd95705a67c;hp=dddf7ceb6123ebb6970c6f994d293bcdc0b6bd87;hpb=e290c6d250cdf67887a5f16ac9d459f83f9f0e30;p=oota-llvm.git diff --git a/tools/bugpoint/OptimizerDriver.cpp b/tools/bugpoint/OptimizerDriver.cpp index dddf7ceb612..3ded5e866b3 100644 --- a/tools/bugpoint/OptimizerDriver.cpp +++ b/tools/bugpoint/OptimizerDriver.cpp @@ -2,8 +2,8 @@ // // 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 is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // @@ -23,10 +23,11 @@ #include "llvm/Module.h" #include "llvm/PassManager.h" #include "llvm/Analysis/Verifier.h" -#include "llvm/Bytecode/WriteBytecodePass.h" +#include "llvm/Bitcode/ReaderWriter.h" #include "llvm/Target/TargetData.h" #include "llvm/Support/FileUtilities.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Streams.h" #include "llvm/System/Path.h" #include "llvm/System/Program.h" #include "llvm/Config/alloca.h" @@ -37,6 +38,7 @@ #include using namespace llvm; + namespace { // ChildOutput - This option captures the name of the child output file that // is set up by the parent bugpoint process @@ -45,7 +47,7 @@ namespace { cl::desc("Run optimizations through valgrind")); } -/// writeProgramToFile - This writes the current "Program" to the named bytecode +/// writeProgramToFile - This writes the current "Program" to the named bitcode /// file. If an error occurs, true is returned. /// bool BugDriver::writeProgramToFile(const std::string &Filename, @@ -54,33 +56,30 @@ bool BugDriver::writeProgramToFile(const std::string &Filename, std::ios::binary; std::ofstream Out(Filename.c_str(), io_mode); if (!Out.good()) return true; - try { - WriteBytecodeToFile(M ? M : Program, Out, /*compression=*/true); - } catch (...) { - return true; - } + + WriteBitcodeToFile(M ? M : Program, Out); return false; } -/// EmitProgressBytecode - This function is used to output the current Program +/// EmitProgressBitcode - This function is used to output the current Program /// 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 +void BugDriver::EmitProgressBitcode(const std::string &ID, bool NoFlyer) { + // Output the input to the current pass to a bitcode file, emit a message // telling the user how to reproduce it: opt -foo blah.bc // std::string Filename = "bugpoint-" + ID + ".bc"; if (writeProgramToFile(Filename)) { - std::cerr << "Error opening file '" << Filename << "' for writing!\n"; + cerr << "Error opening file '" << Filename << "' for writing!\n"; return; } - std::cout << "Emitted bytecode to '" << Filename << "'\n"; + cout << "Emitted bitcode to '" << Filename << "'\n"; if (NoFlyer || PassesToRun.empty()) return; - std::cout << "\n*** You can reproduce the problem with: "; - std::cout << "opt " << Filename << " "; - std::cout << getPassesString(PassesToRun) << "\n"; + cout << "\n*** You can reproduce the problem with: "; + cout << "opt " << Filename << " "; + cout << getPassesString(PassesToRun) << "\n"; } int BugDriver::runPassesAsChild(const std::vector &Passes) { @@ -89,7 +88,7 @@ int BugDriver::runPassesAsChild(const std::vector &Passes) { std::ios::binary; std::ofstream OutFile(ChildOutput.c_str(), io_mode); if (!OutFile.good()) { - std::cerr << "Error opening bytecode file: " << ChildOutput << "\n"; + cerr << "Error opening bitcode file: " << ChildOutput << "\n"; return 1; } @@ -101,14 +100,13 @@ int BugDriver::runPassesAsChild(const std::vector &Passes) { if (Passes[i]->getNormalCtor()) PM.add(Passes[i]->getNormalCtor()()); else - std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName() - << "\n"; + cerr << "Cannot create pass yet: " << Passes[i]->getPassName() << "\n"; } // Check that the module is well formed on completion of optimization PM.add(createVerifierPass()); - // Write bytecode out to disk as the last step... - PM.add(new WriteBytecodePass(&OutFile)); + // Write bitcode out to disk as the last step... + PM.add(CreateBitcodeWriterPass(OutFile)); // Run all queued passes. PM.run(*Program); @@ -116,24 +114,27 @@ int BugDriver::runPassesAsChild(const std::vector &Passes) { return 0; } -/// runPasses - Run the specified passes on Program, outputting a bytecode file +cl::opt SilencePasses("silence-passes", cl::desc("Suppress output of running passes (both stdout and stderr)")); + +/// runPasses - Run the specified passes on Program, outputting a bitcode file /// 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 +/// otherwise return false. If DeleteOutput is set to true, the bitcode is /// deleted on success, and the filename string is undefined. This prints to /// cout a single line message indicating whether compilation was successful or /// failed. /// bool BugDriver::runPasses(const std::vector &Passes, std::string &OutputFilename, bool DeleteOutput, - bool Quiet) const { + bool Quiet, unsigned NumExtraArgs, + const char * const *ExtraArgs) const { // setup the output file name - std::cout << std::flush; + cout << std::flush; sys::Path uniqueFilename("bugpoint-output.bc"); std::string ErrMsg; if (uniqueFilename.makeUnique(true, &ErrMsg)) { - std::cerr << getToolName() << ": Error making unique filename: " - << ErrMsg << "\n"; + cerr << getToolName() << ": Error making unique filename: " + << ErrMsg << "\n"; return(1); } OutputFilename = uniqueFilename.toString(); @@ -141,24 +142,24 @@ bool BugDriver::runPasses(const std::vector &Passes, // set up the input file name sys::Path inputFilename("bugpoint-input.bc"); if (inputFilename.makeUnique(true, &ErrMsg)) { - std::cerr << getToolName() << ": Error making unique filename: " - << ErrMsg << "\n"; + cerr << getToolName() << ": Error making unique filename: " + << ErrMsg << "\n"; return(1); } std::ios::openmode io_mode = std::ios::out | std::ios::trunc | std::ios::binary; std::ofstream InFile(inputFilename.c_str(), io_mode); if (!InFile.good()) { - std::cerr << "Error opening bytecode file: " << inputFilename << "\n"; + cerr << "Error opening bitcode file: " << inputFilename << "\n"; return(1); } - WriteBytecodeToFile(Program,InFile,false); + WriteBitcodeToFile(Program, InFile); InFile.close(); // setup the child process' arguments const char** args = (const char**) alloca(sizeof(const char*) * - (Passes.size()+13+2*PluginLoader::getNumPlugins())); + (Passes.size()+13+2*PluginLoader::getNumPlugins()+NumExtraArgs)); int n = 0; sys::Path tool = sys::Program::FindProgramByName(ToolName); if (UseValgrind) { @@ -184,6 +185,8 @@ bool BugDriver::runPasses(const std::vector &Passes, E = pass_args.end(); I != E; ++I ) args[n++] = I->c_str(); args[n++] = inputFilename.c_str(); + for (unsigned i = 0; i < NumExtraArgs; ++i) + args[n++] = *ExtraArgs; args[n++] = 0; sys::Path prog; @@ -191,9 +194,15 @@ bool BugDriver::runPasses(const std::vector &Passes, prog = sys::Program::FindProgramByName("valgrind"); else prog = tool; - int result = sys::Program::ExecuteAndWait(prog,args,0,0,Timeout,&ErrMsg); + + // Redirect stdout and stderr to nowhere if SilencePasses is given + sys::Path Nowhere; + const sys::Path *Redirects[3] = {0, &Nowhere, &Nowhere}; + + int result = sys::Program::ExecuteAndWait(prog, args, 0, (SilencePasses ? Redirects : 0), + Timeout, MemoryLimit, &ErrMsg); - // If we are supposed to delete the bytecode file or if the passes crashed, + // If we are supposed to delete the bitcode file or if the passes crashed, // remove it now. This may fail if the file was never created, but that's ok. if (DeleteOutput || result != 0) sys::Path(OutputFilename).eraseFromDisk(); @@ -203,17 +212,17 @@ bool BugDriver::runPasses(const std::vector &Passes, if (!Quiet) { if (result == 0) - std::cout << "Success!\n"; + cout << "Success!\n"; else if (result > 0) - std::cout << "Exited with error code '" << result << "'\n"; + cout << "Exited with error code '" << result << "'\n"; else if (result < 0) { if (result == -1) - std::cout << "Execute failed: " << ErrMsg << "\n"; + cout << "Execute failed: " << ErrMsg << "\n"; else - std::cout << "Crashed with signal #" << abs(result) << "\n"; + cout << "Crashed with signal #" << abs(result) << "\n"; } if (result & 0x01000000) - std::cout << "Dumped core\n"; + cout << "Dumped core\n"; } // Was the child successful? @@ -226,15 +235,17 @@ bool BugDriver::runPasses(const std::vector &Passes, /// failure. Module *BugDriver::runPassesOn(Module *M, const std::vector &Passes, - bool AutoDebugCrashes) { + bool AutoDebugCrashes, unsigned NumExtraArgs, + const char * const *ExtraArgs) { Module *OldProgram = swapProgramIn(M); - std::string BytecodeResult; - if (runPasses(Passes, BytecodeResult, false/*delete*/, true/*quiet*/)) { + std::string BitcodeResult; + if (runPasses(Passes, BitcodeResult, false/*delete*/, true/*quiet*/, + NumExtraArgs, ExtraArgs)) { if (AutoDebugCrashes) { - std::cerr << " Error running this sequence of passes" - << " on the input program!\n"; + cerr << " Error running this sequence of passes" + << " on the input program!\n"; delete OldProgram; - EmitProgressBytecode("pass-error", false); + EmitProgressBitcode("pass-error", false); exit(debugOptimizerCrash()); } swapProgramIn(OldProgram); @@ -244,12 +255,12 @@ Module *BugDriver::runPassesOn(Module *M, // Restore the current program. swapProgramIn(OldProgram); - Module *Ret = ParseInputFile(BytecodeResult); + Module *Ret = ParseInputFile(BitcodeResult); if (Ret == 0) { - std::cerr << getToolName() << ": Error reading bytecode file '" - << BytecodeResult << "'!\n"; + cerr << getToolName() << ": Error reading bitcode file '" + << BitcodeResult << "'!\n"; exit(1); } - sys::Path(BytecodeResult).eraseFromDisk(); // No longer need the file on disk + sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk return Ret; }