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" |
Rafael Espindola | 7f99f74 | 2010-08-07 23:03:21 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 32 | #include "llvm/System/Path.h" |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 33 | #include "llvm/System/Program.h" |
Andrew Lenharth | e4da1dd | 2006-01-26 18:37:21 +0000 | [diff] [blame] | 34 | |
| 35 | #define DONT_GET_PLUGIN_LOADER_OPTION |
| 36 | #include "llvm/Support/PluginLoader.h" |
| 37 | |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 38 | #include <fstream> |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 41 | namespace llvm { |
| 42 | extern cl::opt<std::string> OutputPrefix; |
| 43 | } |
Chris Lattner | 03b6963 | 2007-05-06 05:47:06 +0000 | [diff] [blame] | 44 | |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | // ChildOutput - This option captures the name of the child output file that |
| 47 | // is set up by the parent bugpoint process |
| 48 | cl::opt<std::string> ChildOutput("child-output", cl::ReallyHidden); |
| 49 | } |
| 50 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 51 | /// writeProgramToFile - This writes the current "Program" to the named bitcode |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 52 | /// file. If an error occurs, true is returned. |
| 53 | /// |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 54 | bool BugDriver::writeProgramToFile(const std::string &Filename, |
Rafael Espindola | bae1b71 | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 55 | const Module *M) const { |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 56 | std::string ErrInfo; |
| 57 | raw_fd_ostream Out(Filename.c_str(), ErrInfo, |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 58 | raw_fd_ostream::F_Binary); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 59 | if (!ErrInfo.empty()) return true; |
Chris Lattner | 744879e | 2007-05-06 09:32:02 +0000 | [diff] [blame] | 60 | |
Rafael Espindola | bae1b71 | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 61 | WriteBitcodeToFile(M, Out); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
| 65 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 66 | /// EmitProgressBitcode - This function is used to output the current Program |
Misha Brukman | 265789f | 2003-07-21 21:58:16 +0000 | [diff] [blame] | 67 | /// to a file named "bugpoint-ID.bc". |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 68 | /// |
Rafael Espindola | bae1b71 | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 69 | void BugDriver::EmitProgressBitcode(const Module *M, |
Rafael Espindola | ca356af | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 70 | const std::string &ID, |
| 71 | bool NoFlyer) const { |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 72 | // 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] | 73 | // telling the user how to reproduce it: opt -foo blah.bc |
| 74 | // |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 75 | std::string Filename = OutputPrefix + "-" + ID + ".bc"; |
Rafael Espindola | bae1b71 | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 76 | if (writeProgramToFile(Filename, M)) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 77 | errs() << "Error opening file '" << Filename << "' for writing!\n"; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 81 | outs() << "Emitted bitcode to '" << Filename << "'\n"; |
Chris Lattner | ca00512 | 2004-02-18 23:24:56 +0000 | [diff] [blame] | 82 | if (NoFlyer || PassesToRun.empty()) return; |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 83 | outs() << "\n*** You can reproduce the problem with: "; |
Nick Lewycky | 40cce09 | 2009-08-18 06:08:01 +0000 | [diff] [blame] | 84 | if (UseValgrind) outs() << "valgrind "; |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 85 | outs() << "opt " << Filename << " "; |
| 86 | outs() << getPassesString(PassesToRun) << "\n"; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Matthijs Kooijman | fbea227 | 2008-06-12 13:02:26 +0000 | [diff] [blame] | 89 | cl::opt<bool> SilencePasses("silence-passes", cl::desc("Suppress output of running passes (both stdout and stderr)")); |
| 90 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 91 | /// runPasses - Run the specified passes on Program, outputting a bitcode file |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 92 | /// and writing the filename into OutputFile if successful. If the |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 93 | /// optimizations fail for some reason (optimizer crashes), return true, |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 94 | /// otherwise return false. If DeleteOutput is set to true, the bitcode is |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 95 | /// deleted on success, and the filename string is undefined. This prints to |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 96 | /// outs() a single line message indicating whether compilation was successful |
| 97 | /// or failed. |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 98 | /// |
Rafael Espindola | ca356af | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 99 | bool BugDriver::runPasses(Module *Program, |
| 100 | const std::vector<const PassInfo*> &Passes, |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 101 | std::string &OutputFilename, bool DeleteOutput, |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 102 | bool Quiet, unsigned NumExtraArgs, |
| 103 | const char * const *ExtraArgs) const { |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 104 | // setup the output file name |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 105 | outs().flush(); |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 106 | sys::Path uniqueFilename(OutputPrefix + "-output.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 107 | std::string ErrMsg; |
| 108 | if (uniqueFilename.makeUnique(true, &ErrMsg)) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 109 | errs() << getToolName() << ": Error making unique filename: " |
| 110 | << ErrMsg << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 111 | return(1); |
| 112 | } |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 113 | OutputFilename = uniqueFilename.str(); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 114 | |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 115 | // set up the input file name |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 116 | sys::Path inputFilename(OutputPrefix + "-input.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 117 | if (inputFilename.makeUnique(true, &ErrMsg)) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 118 | errs() << getToolName() << ": Error making unique filename: " |
| 119 | << ErrMsg << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 120 | return(1); |
| 121 | } |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 122 | |
| 123 | std::string ErrInfo; |
| 124 | raw_fd_ostream InFile(inputFilename.c_str(), ErrInfo, |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 125 | raw_fd_ostream::F_Binary); |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 126 | |
| 127 | |
| 128 | if (!ErrInfo.empty()) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 129 | errs() << "Error opening bitcode file: " << inputFilename.str() << "\n"; |
Chris Lattner | b515d75 | 2009-08-23 07:49:08 +0000 | [diff] [blame] | 130 | return 1; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 131 | } |
Chris Lattner | 744879e | 2007-05-06 09:32:02 +0000 | [diff] [blame] | 132 | WriteBitcodeToFile(Program, InFile); |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 133 | InFile.close(); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 134 | |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 135 | // setup the child process' arguments |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 136 | SmallVector<const char*, 8> Args; |
Rafael Espindola | 7f99f74 | 2010-08-07 23:03:21 +0000 | [diff] [blame] | 137 | std::string Opt; |
| 138 | llvm::StringRef TN(ToolName); |
| 139 | if (TN.find('/') == llvm::StringRef::npos) { |
| 140 | Opt = "opt"; |
| 141 | } else { |
| 142 | std::pair<llvm::StringRef, llvm::StringRef> P = TN.rsplit('/'); |
| 143 | Opt = P.first.str() + "/" + "opt"; |
| 144 | } |
| 145 | |
| 146 | sys::Path tool = sys::Program::FindProgramByName(Opt); |
Nick Lewycky | 40394bc | 2006-09-14 03:49:54 +0000 | [diff] [blame] | 147 | if (UseValgrind) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 148 | Args.push_back("valgrind"); |
| 149 | Args.push_back("--error-exitcode=1"); |
| 150 | Args.push_back("-q"); |
| 151 | Args.push_back(tool.c_str()); |
Nick Lewycky | 40394bc | 2006-09-14 03:49:54 +0000 | [diff] [blame] | 152 | } else |
Rafael Espindola | 7f99f74 | 2010-08-07 23:03:21 +0000 | [diff] [blame] | 153 | Args.push_back(Opt.c_str()); |
Nick Lewycky | 40394bc | 2006-09-14 03:49:54 +0000 | [diff] [blame] | 154 | |
Rafael Espindola | 7f99f74 | 2010-08-07 23:03:21 +0000 | [diff] [blame] | 155 | Args.push_back("-o"); |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 156 | Args.push_back(OutputFilename.c_str()); |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 157 | std::vector<std::string> pass_args; |
Andrew Lenharth | e4da1dd | 2006-01-26 18:37:21 +0000 | [diff] [blame] | 158 | for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) { |
| 159 | pass_args.push_back( std::string("-load")); |
| 160 | pass_args.push_back( PluginLoader::getPlugin(i)); |
| 161 | } |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 162 | for (std::vector<const PassInfo*>::const_iterator I = Passes.begin(), |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 163 | E = Passes.end(); I != E; ++I ) |
| 164 | pass_args.push_back( std::string("-") + (*I)->getPassArgument() ); |
| 165 | for (std::vector<std::string>::const_iterator I = pass_args.begin(), |
| 166 | E = pass_args.end(); I != E; ++I ) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 167 | Args.push_back(I->c_str()); |
| 168 | Args.push_back(inputFilename.c_str()); |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 169 | for (unsigned i = 0; i < NumExtraArgs; ++i) |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 170 | Args.push_back(*ExtraArgs); |
| 171 | Args.push_back(0); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 172 | |
Rafael Espindola | 7f99f74 | 2010-08-07 23:03:21 +0000 | [diff] [blame] | 173 | DEBUG(errs() << "\nAbout to run:\t"; |
| 174 | for (unsigned i = 0, e = Args.size()-1; i != e; ++i) |
| 175 | errs() << " " << Args[i]; |
| 176 | errs() << "\n"; |
| 177 | ); |
| 178 | |
Nick Lewycky | 40394bc | 2006-09-14 03:49:54 +0000 | [diff] [blame] | 179 | sys::Path prog; |
| 180 | if (UseValgrind) |
| 181 | prog = sys::Program::FindProgramByName("valgrind"); |
| 182 | else |
Nick Lewycky | e290c6d | 2006-09-14 04:20:17 +0000 | [diff] [blame] | 183 | prog = tool; |
Matthijs Kooijman | fbea227 | 2008-06-12 13:02:26 +0000 | [diff] [blame] | 184 | |
| 185 | // Redirect stdout and stderr to nowhere if SilencePasses is given |
| 186 | sys::Path Nowhere; |
| 187 | const sys::Path *Redirects[3] = {0, &Nowhere, &Nowhere}; |
| 188 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 189 | int result = sys::Program::ExecuteAndWait(prog, Args.data(), 0, |
| 190 | (SilencePasses ? Redirects : 0), |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 191 | Timeout, MemoryLimit, &ErrMsg); |
Chris Lattner | b83c0f3 | 2004-05-12 02:55:45 +0000 | [diff] [blame] | 192 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 193 | // 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] | 194 | // 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] | 195 | if (DeleteOutput || result != 0) |
Reid Spencer | a229c5c | 2005-07-08 03:08:58 +0000 | [diff] [blame] | 196 | sys::Path(OutputFilename).eraseFromDisk(); |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 197 | |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 198 | // Remove the temporary input file as well |
| 199 | inputFilename.eraseFromDisk(); |
| 200 | |
Chris Lattner | 24271cf | 2003-06-02 04:54:16 +0000 | [diff] [blame] | 201 | if (!Quiet) { |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 202 | if (result == 0) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 203 | outs() << "Success!\n"; |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 204 | else if (result > 0) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 205 | outs() << "Exited with error code '" << result << "'\n"; |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 206 | else if (result < 0) { |
| 207 | if (result == -1) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 208 | outs() << "Execute failed: " << ErrMsg << "\n"; |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 209 | else |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 210 | outs() << "Crashed with signal #" << abs(result) << "\n"; |
Reid Spencer | 8ea5ecb | 2006-08-21 06:04:45 +0000 | [diff] [blame] | 211 | } |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 212 | if (result & 0x01000000) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 213 | outs() << "Dumped core\n"; |
Chris Lattner | 24271cf | 2003-06-02 04:54:16 +0000 | [diff] [blame] | 214 | } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 215 | |
| 216 | // Was the child successful? |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 217 | return result != 0; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 218 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 220 | |
| 221 | /// runPassesOn - Carefully run the specified set of pass on the specified |
| 222 | /// module, returning the transformed module on success, or a null pointer on |
| 223 | /// failure. |
| 224 | Module *BugDriver::runPassesOn(Module *M, |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 225 | const std::vector<const PassInfo*> &Passes, |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 226 | bool AutoDebugCrashes, unsigned NumExtraArgs, |
| 227 | const char * const *ExtraArgs) { |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 228 | std::string BitcodeResult; |
Rafael Espindola | ca356af | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 229 | if (runPasses(M, Passes, BitcodeResult, false/*delete*/, true/*quiet*/, |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 230 | NumExtraArgs, ExtraArgs)) { |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 231 | if (AutoDebugCrashes) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 232 | errs() << " Error running this sequence of passes" |
| 233 | << " on the input program!\n"; |
Rafael Espindola | ca356af | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 234 | delete swapProgramIn(M); |
| 235 | EmitProgressBitcode(M, "pass-error", false); |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 236 | exit(debugOptimizerCrash()); |
| 237 | } |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 238 | return 0; |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 240 | |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 241 | Module *Ret = ParseInputFile(BitcodeResult, Context); |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 242 | if (Ret == 0) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 243 | errs() << getToolName() << ": Error reading bitcode file '" |
| 244 | << BitcodeResult << "'!\n"; |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 245 | exit(1); |
| 246 | } |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 247 | sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 248 | return Ret; |
| 249 | } |