Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines an interface that allows bugpoint to run various passes |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 11 | // without the threat of a buggy pass corrupting bugpoint (of course, bugpoint |
| 12 | // may have its own bugs, but that's another story...). It achieves this by |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 13 | // forking a copy of itself and having the child process do the optimizations. |
| 14 | // If this client dies, we can always fork a new one. :) |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 18 | // Note: as a short term hack, the old Unix-specific code and platform- |
| 19 | // independent code co-exist via conditional compilation until it is verified |
| 20 | // that the new code works correctly on Unix. |
| 21 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 22 | #include "BugDriver.h" |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 24 | #include "llvm/PassManager.h" |
| 25 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | 03b6963 | 2007-05-06 05:47:06 +0000 | [diff] [blame] | 26 | #include "llvm/Bitcode/ReaderWriter.h" |
Chris Lattner | f2bcccf | 2003-01-22 23:24:11 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/Support/FileUtilities.h" |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 31 | #include "llvm/System/Path.h" |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 32 | #include "llvm/System/Program.h" |
Andrew Lenharth | e4da1dd | 2006-01-26 18:37:21 +0000 | [diff] [blame] | 33 | |
| 34 | #define DONT_GET_PLUGIN_LOADER_OPTION |
| 35 | #include "llvm/Support/PluginLoader.h" |
| 36 | |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 37 | #include <fstream> |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 40 | namespace llvm { |
| 41 | extern cl::opt<std::string> OutputPrefix; |
| 42 | } |
Chris Lattner | 03b6963 | 2007-05-06 05:47:06 +0000 | [diff] [blame] | 43 | |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 44 | namespace { |
| 45 | // ChildOutput - This option captures the name of the child output file that |
| 46 | // is set up by the parent bugpoint process |
| 47 | cl::opt<std::string> ChildOutput("child-output", cl::ReallyHidden); |
| 48 | } |
| 49 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 50 | /// writeProgramToFile - This writes the current "Program" to the named bitcode |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 51 | /// file. If an error occurs, true is returned. |
| 52 | /// |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 53 | bool BugDriver::writeProgramToFile(const std::string &Filename, |
Rafael Espindola | bae1b71 | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 54 | const Module *M) const { |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 55 | std::string ErrInfo; |
| 56 | raw_fd_ostream Out(Filename.c_str(), ErrInfo, |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 57 | raw_fd_ostream::F_Binary); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 58 | if (!ErrInfo.empty()) return true; |
Chris Lattner | 744879e | 2007-05-06 09:32:02 +0000 | [diff] [blame] | 59 | |
Rafael Espindola | bae1b71 | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 60 | WriteBitcodeToFile(M, Out); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | |
| 64 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 65 | /// EmitProgressBitcode - This function is used to output the current Program |
Misha Brukman | 265789f | 2003-07-21 21:58:16 +0000 | [diff] [blame] | 66 | /// to a file named "bugpoint-ID.bc". |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 67 | /// |
Rafael Espindola | bae1b71 | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 68 | void BugDriver::EmitProgressBitcode(const Module *M, |
Rafael Espindola | ca356af | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 69 | const std::string &ID, |
| 70 | bool NoFlyer) const { |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 71 | // Output the input to the current pass to a bitcode file, emit a message |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 72 | // telling the user how to reproduce it: opt -foo blah.bc |
| 73 | // |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 74 | std::string Filename = OutputPrefix + "-" + ID + ".bc"; |
Rafael Espindola | bae1b71 | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 75 | if (writeProgramToFile(Filename, M)) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 76 | errs() << "Error opening file '" << Filename << "' for writing!\n"; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 80 | outs() << "Emitted bitcode to '" << Filename << "'\n"; |
Chris Lattner | ca00512 | 2004-02-18 23:24:56 +0000 | [diff] [blame] | 81 | if (NoFlyer || PassesToRun.empty()) return; |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 82 | outs() << "\n*** You can reproduce the problem with: "; |
Nick Lewycky | 40cce09 | 2009-08-18 06:08:01 +0000 | [diff] [blame] | 83 | if (UseValgrind) outs() << "valgrind "; |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 84 | outs() << "opt " << Filename << " "; |
| 85 | outs() << getPassesString(PassesToRun) << "\n"; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Bob Wilson | b8be03b0 | 2010-08-05 16:26:32 +0000 | [diff] [blame^] | 88 | int BugDriver::runPassesAsChild(const std::vector<const PassInfo*> &Passes) { |
| 89 | std::string ErrInfo; |
| 90 | raw_fd_ostream OutFile(ChildOutput.c_str(), ErrInfo, |
| 91 | raw_fd_ostream::F_Binary); |
| 92 | if (!ErrInfo.empty()) { |
| 93 | errs() << "Error opening bitcode file: " << ChildOutput << "\n"; |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | PassManager PM; |
| 98 | // Make sure that the appropriate target data is always used... |
| 99 | PM.add(new TargetData(Program)); |
| 100 | |
| 101 | for (unsigned i = 0, e = Passes.size(); i != e; ++i) { |
| 102 | if (Passes[i]->getNormalCtor()) |
| 103 | PM.add(Passes[i]->getNormalCtor()()); |
| 104 | else |
| 105 | errs() << "Cannot create pass yet: " << Passes[i]->getPassName() << "\n"; |
| 106 | } |
| 107 | // Check that the module is well formed on completion of optimization |
| 108 | PM.add(createVerifierPass()); |
| 109 | |
| 110 | // Write bitcode out to disk as the last step... |
| 111 | PM.add(createBitcodeWriterPass(OutFile)); |
| 112 | |
| 113 | // Run all queued passes. |
| 114 | PM.run(*Program); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
Matthijs Kooijman | fbea227 | 2008-06-12 13:02:26 +0000 | [diff] [blame] | 119 | cl::opt<bool> SilencePasses("silence-passes", cl::desc("Suppress output of running passes (both stdout and stderr)")); |
| 120 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 121 | /// runPasses - Run the specified passes on Program, outputting a bitcode file |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 122 | /// and writing the filename into OutputFile if successful. If the |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 123 | /// optimizations fail for some reason (optimizer crashes), return true, |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 124 | /// otherwise return false. If DeleteOutput is set to true, the bitcode is |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 125 | /// deleted on success, and the filename string is undefined. This prints to |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 126 | /// outs() a single line message indicating whether compilation was successful |
| 127 | /// or failed. |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 128 | /// |
Rafael Espindola | ca356af | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 129 | bool BugDriver::runPasses(Module *Program, |
| 130 | const std::vector<const PassInfo*> &Passes, |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 131 | std::string &OutputFilename, bool DeleteOutput, |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 132 | bool Quiet, unsigned NumExtraArgs, |
| 133 | const char * const *ExtraArgs) const { |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 134 | // setup the output file name |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 135 | outs().flush(); |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 136 | sys::Path uniqueFilename(OutputPrefix + "-output.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 137 | std::string ErrMsg; |
| 138 | if (uniqueFilename.makeUnique(true, &ErrMsg)) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 139 | errs() << getToolName() << ": Error making unique filename: " |
| 140 | << ErrMsg << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 141 | return(1); |
| 142 | } |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 143 | OutputFilename = uniqueFilename.str(); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 144 | |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 145 | // set up the input file name |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 146 | sys::Path inputFilename(OutputPrefix + "-input.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 147 | if (inputFilename.makeUnique(true, &ErrMsg)) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 148 | errs() << getToolName() << ": Error making unique filename: " |
| 149 | << ErrMsg << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 150 | return(1); |
| 151 | } |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 152 | |
| 153 | std::string ErrInfo; |
| 154 | raw_fd_ostream InFile(inputFilename.c_str(), ErrInfo, |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 155 | raw_fd_ostream::F_Binary); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | if (!ErrInfo.empty()) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 159 | errs() << "Error opening bitcode file: " << inputFilename.str() << "\n"; |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 160 | return 1; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 161 | } |
Chris Lattner | 744879e | 2007-05-06 09:32:02 +0000 | [diff] [blame] | 162 | WriteBitcodeToFile(Program, InFile); |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 163 | InFile.close(); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 164 | |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 165 | // setup the child process' arguments |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 166 | SmallVector<const char*, 8> Args; |
Bob Wilson | b8be03b0 | 2010-08-05 16:26:32 +0000 | [diff] [blame^] | 167 | sys::Path tool = sys::Program::FindProgramByName(ToolName); |
Nick Lewycky | 40394bc | 2006-09-14 03:49:54 +0000 | [diff] [blame] | 168 | if (UseValgrind) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 169 | Args.push_back("valgrind"); |
| 170 | Args.push_back("--error-exitcode=1"); |
| 171 | Args.push_back("-q"); |
| 172 | Args.push_back(tool.c_str()); |
Nick Lewycky | 40394bc | 2006-09-14 03:49:54 +0000 | [diff] [blame] | 173 | } else |
Bob Wilson | b8be03b0 | 2010-08-05 16:26:32 +0000 | [diff] [blame^] | 174 | Args.push_back(ToolName); |
Nick Lewycky | 40394bc | 2006-09-14 03:49:54 +0000 | [diff] [blame] | 175 | |
Bob Wilson | b8be03b0 | 2010-08-05 16:26:32 +0000 | [diff] [blame^] | 176 | Args.push_back("-as-child"); |
| 177 | Args.push_back("-child-output"); |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 178 | Args.push_back(OutputFilename.c_str()); |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 179 | std::vector<std::string> pass_args; |
Andrew Lenharth | e4da1dd | 2006-01-26 18:37:21 +0000 | [diff] [blame] | 180 | for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) { |
| 181 | pass_args.push_back( std::string("-load")); |
| 182 | pass_args.push_back( PluginLoader::getPlugin(i)); |
| 183 | } |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 184 | for (std::vector<const PassInfo*>::const_iterator I = Passes.begin(), |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 185 | E = Passes.end(); I != E; ++I ) |
| 186 | pass_args.push_back( std::string("-") + (*I)->getPassArgument() ); |
| 187 | for (std::vector<std::string>::const_iterator I = pass_args.begin(), |
| 188 | E = pass_args.end(); I != E; ++I ) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 189 | Args.push_back(I->c_str()); |
| 190 | Args.push_back(inputFilename.c_str()); |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 191 | for (unsigned i = 0; i < NumExtraArgs; ++i) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 192 | Args.push_back(*ExtraArgs); |
| 193 | Args.push_back(0); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 194 | |
Nick Lewycky | 40394bc | 2006-09-14 03:49:54 +0000 | [diff] [blame] | 195 | sys::Path prog; |
| 196 | if (UseValgrind) |
| 197 | prog = sys::Program::FindProgramByName("valgrind"); |
| 198 | else |
Nick Lewycky | e290c6d | 2006-09-14 04:20:17 +0000 | [diff] [blame] | 199 | prog = tool; |
Matthijs Kooijman | fbea227 | 2008-06-12 13:02:26 +0000 | [diff] [blame] | 200 | |
| 201 | // Redirect stdout and stderr to nowhere if SilencePasses is given |
| 202 | sys::Path Nowhere; |
| 203 | const sys::Path *Redirects[3] = {0, &Nowhere, &Nowhere}; |
| 204 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 205 | int result = sys::Program::ExecuteAndWait(prog, Args.data(), 0, |
| 206 | (SilencePasses ? Redirects : 0), |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 207 | Timeout, MemoryLimit, &ErrMsg); |
Chris Lattner | b83c0f3 | 2004-05-12 02:55:45 +0000 | [diff] [blame] | 208 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 209 | // If we are supposed to delete the bitcode file or if the passes crashed, |
Chris Lattner | b83c0f3 | 2004-05-12 02:55:45 +0000 | [diff] [blame] | 210 | // remove it now. This may fail if the file was never created, but that's ok. |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 211 | if (DeleteOutput || result != 0) |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 212 | sys::Path(OutputFilename).eraseFromDisk(); |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 213 | |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 214 | // Remove the temporary input file as well |
| 215 | inputFilename.eraseFromDisk(); |
| 216 | |
Chris Lattner | 24271cf | 2003-06-02 04:54:16 +0000 | [diff] [blame] | 217 | if (!Quiet) { |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 218 | if (result == 0) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 219 | outs() << "Success!\n"; |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 220 | else if (result > 0) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 221 | outs() << "Exited with error code '" << result << "'\n"; |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 222 | else if (result < 0) { |
| 223 | if (result == -1) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 224 | outs() << "Execute failed: " << ErrMsg << "\n"; |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 225 | else |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 226 | outs() << "Crashed with signal #" << abs(result) << "\n"; |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 227 | } |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 228 | if (result & 0x01000000) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 229 | outs() << "Dumped core\n"; |
Chris Lattner | 24271cf | 2003-06-02 04:54:16 +0000 | [diff] [blame] | 230 | } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 231 | |
| 232 | // Was the child successful? |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 233 | return result != 0; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 234 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 236 | |
| 237 | /// runPassesOn - Carefully run the specified set of pass on the specified |
| 238 | /// module, returning the transformed module on success, or a null pointer on |
| 239 | /// failure. |
| 240 | Module *BugDriver::runPassesOn(Module *M, |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 241 | const std::vector<const PassInfo*> &Passes, |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 242 | bool AutoDebugCrashes, unsigned NumExtraArgs, |
| 243 | const char * const *ExtraArgs) { |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 244 | std::string BitcodeResult; |
Rafael Espindola | ca356af | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 245 | if (runPasses(M, Passes, BitcodeResult, false/*delete*/, true/*quiet*/, |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 246 | NumExtraArgs, ExtraArgs)) { |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 247 | if (AutoDebugCrashes) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 248 | errs() << " Error running this sequence of passes" |
| 249 | << " on the input program!\n"; |
Rafael Espindola | ca356af | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 250 | delete swapProgramIn(M); |
| 251 | EmitProgressBitcode(M, "pass-error", false); |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 252 | exit(debugOptimizerCrash()); |
| 253 | } |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 254 | return 0; |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 255 | } |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 256 | |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 257 | Module *Ret = ParseInputFile(BitcodeResult, Context); |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 258 | if (Ret == 0) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 259 | errs() << getToolName() << ": Error reading bitcode file '" |
| 260 | << BitcodeResult << "'!\n"; |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 261 | exit(1); |
| 262 | } |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 263 | sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 264 | return Ret; |
| 265 | } |