Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===// |
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 class contains all of the shared state and information that is used by |
| 11 | // the BugPoint tool to track down errors in optimizations. This class is the |
| 12 | // main driver class that invokes all sub-functionality. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "BugDriver.h" |
Chris Lattner | f1b20d8 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 17 | #include "ToolRunner.h" |
Reid Spencer | 605b9e2 | 2004-11-14 23:00:08 +0000 | [diff] [blame] | 18 | #include "llvm/Linker.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Dan Gohman | dad45ea | 2009-09-03 16:32:58 +0000 | [diff] [blame] | 21 | #include "llvm/Support/IRReader.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | 92bcb42 | 2009-07-02 22:46:18 +0000 | [diff] [blame] | 24 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Host.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 27 | #include <memory> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 30 | namespace llvm { |
| 31 | Triple TargetTriple; |
| 32 | } |
| 33 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 34 | // Anonymous namespace to define command line options for debugging. |
| 35 | // |
| 36 | namespace { |
| 37 | // Output - The user can specify a file containing the expected output of the |
| 38 | // program. If this filename is set, it is used as the reference diff source, |
| 39 | // otherwise the raw input run through an interpreter is used as the reference |
| 40 | // source. |
| 41 | // |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 42 | cl::opt<std::string> |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 43 | OutputFile("output", cl::desc("Specify a reference program output " |
| 44 | "(for miscompilation detection)")); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Chris Lattner | 06905db | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 47 | /// setNewProgram - If we reduce or update the program somehow, call this method |
| 48 | /// to update bugdriver with it. This deletes the old module and sets the |
| 49 | /// specified one as the current program. |
| 50 | void BugDriver::setNewProgram(Module *M) { |
| 51 | delete Program; |
| 52 | Program = M; |
| 53 | } |
| 54 | |
| 55 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 56 | /// getPassesString - Turn a list of passes into a string which indicates the |
| 57 | /// command line options that must be passed to add the passes. |
| 58 | /// |
Rafael Espindola | 8261dfe | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 59 | std::string llvm::getPassesString(const std::vector<std::string> &Passes) { |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 60 | std::string Result; |
| 61 | for (unsigned i = 0, e = Passes.size(); i != e; ++i) { |
| 62 | if (i) Result += " "; |
| 63 | Result += "-"; |
Rafael Espindola | 8261dfe | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 64 | Result += Passes[i]; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 65 | } |
| 66 | return Result; |
| 67 | } |
| 68 | |
Rafael Espindola | 7f99f74 | 2010-08-07 23:03:21 +0000 | [diff] [blame] | 69 | BugDriver::BugDriver(const char *toolname, bool find_bugs, |
Jeffrey Yasskin | c3e6859 | 2010-03-19 00:09:28 +0000 | [diff] [blame] | 70 | unsigned timeout, unsigned memlimit, bool use_valgrind, |
Owen Anderson | 4434ed4 | 2009-07-01 23:13:44 +0000 | [diff] [blame] | 71 | LLVMContext& ctxt) |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 72 | : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile), |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 73 | Program(0), Interpreter(0), SafeInterpreter(0), gcc(0), |
Michael J. Spencer | 56584fc | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 74 | run_find_bugs(find_bugs), Timeout(timeout), |
Jeffrey Yasskin | c3e6859 | 2010-03-19 00:09:28 +0000 | [diff] [blame] | 75 | MemoryLimit(memlimit), UseValgrind(use_valgrind) {} |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 76 | |
Jeffrey Yasskin | c1dc067 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 77 | BugDriver::~BugDriver() { |
| 78 | delete Program; |
| 79 | } |
| 80 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 81 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 82 | /// ParseInputFile - Given a bitcode or assembly input filename, parse and |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 83 | /// return it, or return null if not possible. |
| 84 | /// |
Owen Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 85 | Module *llvm::ParseInputFile(const std::string &Filename, |
Owen Anderson | 4434ed4 | 2009-07-01 23:13:44 +0000 | [diff] [blame] | 86 | LLVMContext& Ctxt) { |
Chris Lattner | 92bcb42 | 2009-07-02 22:46:18 +0000 | [diff] [blame] | 87 | SMDiagnostic Err; |
Dan Gohman | dad45ea | 2009-09-03 16:32:58 +0000 | [diff] [blame] | 88 | Module *Result = ParseIRFile(Filename, Err, Ctxt); |
| 89 | if (!Result) |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 90 | Err.print("bugpoint", errs()); |
Dan Gohman | dad45ea | 2009-09-03 16:32:58 +0000 | [diff] [blame] | 91 | |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 92 | // If we don't have an override triple, use the first one to configure |
| 93 | // bugpoint, or use the host triple if none provided. |
| 94 | if (Result) { |
| 95 | if (TargetTriple.getTriple().empty()) { |
| 96 | Triple TheTriple(Result->getTargetTriple()); |
| 97 | |
| 98 | if (TheTriple.getTriple().empty()) |
Sebastian Pop | 0173864 | 2011-11-01 21:32:20 +0000 | [diff] [blame] | 99 | TheTriple.setTriple(sys::getDefaultTargetTriple()); |
Michael J. Spencer | 56584fc | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 100 | |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 101 | TargetTriple.setTriple(TheTriple.getTriple()); |
| 102 | } |
| 103 | |
| 104 | Result->setTargetTriple(TargetTriple.getTriple()); // override the triple |
| 105 | } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 106 | return Result; |
| 107 | } |
| 108 | |
| 109 | // This method takes the specified list of LLVM input files, attempts to load |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 110 | // them, either as assembly or bitcode, then link them together. It returns |
| 111 | // true on failure (if, for example, an input bitcode file could not be |
Brian Gaeke | dae7f92 | 2003-05-23 05:34:32 +0000 | [diff] [blame] | 112 | // parsed), and false on success. |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 113 | // |
| 114 | bool BugDriver::addSources(const std::vector<std::string> &Filenames) { |
| 115 | assert(Program == 0 && "Cannot call addSources multiple times!"); |
| 116 | assert(!Filenames.empty() && "Must specify at least on input filename!"); |
| 117 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 118 | // Load the first input file. |
| 119 | Program = ParseInputFile(Filenames[0], Context); |
| 120 | if (Program == 0) return true; |
Michael J. Spencer | 56584fc | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 121 | |
Rafael Espindola | 7f99f74 | 2010-08-07 23:03:21 +0000 | [diff] [blame] | 122 | outs() << "Read input file : '" << Filenames[0] << "'\n"; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 123 | |
| 124 | for (unsigned i = 1, e = Filenames.size(); i != e; ++i) { |
| 125 | std::auto_ptr<Module> M(ParseInputFile(Filenames[i], Context)); |
| 126 | if (M.get() == 0) return true; |
| 127 | |
Rafael Espindola | 7f99f74 | 2010-08-07 23:03:21 +0000 | [diff] [blame] | 128 | outs() << "Linking in input file: '" << Filenames[i] << "'\n"; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 129 | std::string ErrorMessage; |
Tanya Lattner | f1f1a4f | 2011-10-11 00:24:54 +0000 | [diff] [blame] | 130 | if (Linker::LinkModules(Program, M.get(), Linker::DestroySource, |
| 131 | &ErrorMessage)) { |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 132 | errs() << ToolName << ": error linking in '" << Filenames[i] << "': " |
| 133 | << ErrorMessage << '\n'; |
| 134 | return true; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Rafael Espindola | 7f99f74 | 2010-08-07 23:03:21 +0000 | [diff] [blame] | 138 | outs() << "*** All input ok\n"; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 139 | |
| 140 | // All input files read successfully! |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | /// run - The top level method that is invoked after all of the instance |
| 147 | /// variables are set up from command line arguments. |
| 148 | /// |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 149 | bool BugDriver::run(std::string &ErrMsg) { |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 150 | if (run_find_bugs) { |
| 151 | // Rearrange the passes and apply them to the program. Repeat this process |
| 152 | // until the user kills the program or we find a bug. |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 153 | return runManyPasses(PassesToRun, ErrMsg); |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 154 | } |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 155 | |
Michael J. Spencer | 56584fc | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 156 | // If we're not running as a child, the first thing that we must do is |
| 157 | // determine what the problem is. Does the optimization series crash the |
| 158 | // compiler, or does it produce illegal code? We make the top-level |
Sylvestre Ledru | c8e41c5 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 159 | // decision by trying to run all of the passes on the input program, |
Michael J. Spencer | 56584fc | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 160 | // which should generate a bitcode file. If it does generate a bitcode |
| 161 | // file, then we know the compiler didn't crash, so try to diagnose a |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 162 | // miscompilation. |
Chris Lattner | 99b8533 | 2003-10-13 21:04:26 +0000 | [diff] [blame] | 163 | if (!PassesToRun.empty()) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 164 | outs() << "Running selected passes on program to test for crash: "; |
Rafael Espindola | 5d8cace | 2010-08-05 02:16:32 +0000 | [diff] [blame] | 165 | if (runPasses(Program, PassesToRun)) |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 166 | return debugOptimizerCrash(); |
Chris Lattner | 99b8533 | 2003-10-13 21:04:26 +0000 | [diff] [blame] | 167 | } |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 168 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 169 | // Set up the execution environment, selecting a method to run LLVM bitcode. |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 170 | if (initializeExecutionEnvironment()) return true; |
| 171 | |
Chris Lattner | 7c955fd | 2004-02-19 17:03:49 +0000 | [diff] [blame] | 172 | // Test to see if we have a code generator crash. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 173 | outs() << "Running the code generator to test for a crash: "; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 174 | std::string Error; |
| 175 | compileProgram(Program, &Error); |
| 176 | if (!Error.empty()) { |
| 177 | outs() << Error; |
| 178 | return debugCodeGeneratorCrash(ErrMsg); |
Chris Lattner | 7c955fd | 2004-02-19 17:03:49 +0000 | [diff] [blame] | 179 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 180 | outs() << '\n'; |
Chris Lattner | 7c955fd | 2004-02-19 17:03:49 +0000 | [diff] [blame] | 181 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 182 | // Run the raw input to see where we are coming from. If a reference output |
| 183 | // was specified, make sure that the raw output matches it. If not, it's a |
| 184 | // problem in the front-end or the code generator. |
| 185 | // |
Chris Lattner | c28c1d3 | 2003-08-22 18:57:43 +0000 | [diff] [blame] | 186 | bool CreatedOutput = false; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 187 | if (ReferenceOutputFile.empty()) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 188 | outs() << "Generating reference output from raw program: "; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 189 | if (!createReferenceFile(Program)) { |
| 190 | return debugCodeGeneratorCrash(ErrMsg); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 191 | } |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 192 | CreatedOutput = true; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chris Lattner | a5a96a9 | 2003-10-14 20:52:55 +0000 | [diff] [blame] | 195 | // Make sure the reference output file gets deleted on exit from this |
| 196 | // function, if appropriate. |
Reid Spencer | 5d28218 | 2004-12-20 19:16:12 +0000 | [diff] [blame] | 197 | sys::Path ROF(ReferenceOutputFile); |
Michael J. Spencer | c9c08fb | 2011-03-31 13:04:19 +0000 | [diff] [blame] | 198 | FileRemover RemoverInstance(ROF.str(), CreatedOutput && !SaveTemps); |
Chris Lattner | a5a96a9 | 2003-10-14 20:52:55 +0000 | [diff] [blame] | 199 | |
| 200 | // Diff the output of the raw program against the reference output. If it |
Michael J. Spencer | 56584fc | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 201 | // matches, then we assume there is a miscompilation bug and try to |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 202 | // diagnose it. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 203 | outs() << "*** Checking the code generator...\n"; |
Rafael Espindola | 10757dd | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 204 | bool Diff = diffProgram(Program, "", "", false, &Error); |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 205 | if (!Error.empty()) { |
| 206 | errs() << Error; |
| 207 | return debugCodeGeneratorCrash(ErrMsg); |
| 208 | } |
| 209 | if (!Diff) { |
| 210 | outs() << "\n*** Output matches: Debugging miscompilation!\n"; |
| 211 | debugMiscompilation(&Error); |
| 212 | if (!Error.empty()) { |
| 213 | errs() << Error; |
| 214 | return debugCodeGeneratorCrash(ErrMsg); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 215 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 216 | return false; |
Chris Lattner | a5a96a9 | 2003-10-14 20:52:55 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 219 | outs() << "\n*** Input program does not match reference diff!\n"; |
| 220 | outs() << "Debugging code generator problem!\n"; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 221 | bool Failure = debugCodeGenerator(&Error); |
| 222 | if (!Error.empty()) { |
| 223 | errs() << Error; |
| 224 | return debugCodeGeneratorCrash(ErrMsg); |
Chris Lattner | 7c955fd | 2004-02-19 17:03:49 +0000 | [diff] [blame] | 225 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 226 | return Failure; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 229 | void llvm::PrintFunctionList(const std::vector<Function*> &Funcs) { |
| 230 | unsigned NumPrint = Funcs.size(); |
| 231 | if (NumPrint > 10) NumPrint = 10; |
| 232 | for (unsigned i = 0; i != NumPrint; ++i) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 233 | outs() << " " << Funcs[i]->getName(); |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 234 | if (NumPrint < Funcs.size()) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 235 | outs() << "... <" << Funcs.size() << " total>"; |
| 236 | outs().flush(); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 237 | } |
Bill Wendling | 4e3be89 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 238 | |
| 239 | void llvm::PrintGlobalVariableList(const std::vector<GlobalVariable*> &GVs) { |
| 240 | unsigned NumPrint = GVs.size(); |
| 241 | if (NumPrint > 10) NumPrint = 10; |
| 242 | for (unsigned i = 0; i != NumPrint; ++i) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 243 | outs() << " " << GVs[i]->getName(); |
Bill Wendling | 4e3be89 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 244 | if (NumPrint < GVs.size()) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 245 | outs() << "... <" << GVs.size() << " total>"; |
| 246 | outs().flush(); |
Bill Wendling | 4e3be89 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 247 | } |