Chris Lattner | 9432111 | 2003-10-20 17:57:13 +0000 | [diff] [blame] | 1 | //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===// |
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 program is an automated compiler debugger tool. It is used to narrow |
| 11 | // down miscompilations and crash problems to a specific pass in the compiler, |
| 12 | // and the specific Module or Function input that is causing the problem. |
| 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 | 62c5105 | 2006-08-21 05:34:03 +0000 | [diff] [blame] | 18 | #include "llvm/LinkAllPasses.h" |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 19 | #include "llvm/LLVMContext.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 20 | #include "llvm/Support/PassNameParser.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 24 | #include "llvm/Support/PrettyStackTrace.h" |
Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 25 | #include "llvm/Support/StandardPasses.h" |
Reid Spencer | 5116330 | 2004-12-27 06:18:02 +0000 | [diff] [blame] | 26 | #include "llvm/System/Process.h" |
Chris Lattner | bed85ff | 2004-05-27 05:41:36 +0000 | [diff] [blame] | 27 | #include "llvm/System/Signals.h" |
Reid Spencer | e3f0561 | 2006-06-07 23:06:50 +0000 | [diff] [blame] | 28 | #include "llvm/LinkAllVMCore.h" |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 31 | // AsChild - Specifies that this invocation of bugpoint is being generated |
| 32 | // from a parent process. It is not intended to be used by users so the |
| 33 | // option is hidden. |
| 34 | static cl::opt<bool> |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 35 | AsChild("as-child", cl::desc("Run bugpoint as child process"), |
| 36 | cl::ReallyHidden); |
| 37 | |
| 38 | static cl::opt<bool> |
Dan Gohman | 3956449 | 2008-02-18 17:15:45 +0000 | [diff] [blame] | 39 | FindBugs("find-bugs", cl::desc("Run many different optimization sequences " |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 40 | "on program to find bugs"), cl::init(false)); |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 41 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 42 | static cl::list<std::string> |
| 43 | InputFilenames(cl::Positional, cl::OneOrMore, |
| 44 | cl::desc("<input llvm ll/bc files>")); |
| 45 | |
Chris Lattner | 9686ae7 | 2006-06-13 03:10:48 +0000 | [diff] [blame] | 46 | static cl::opt<unsigned> |
| 47 | TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"), |
| 48 | cl::desc("Number of seconds program is allowed to run before it " |
| 49 | "is killed (default is 300s), 0 disables timeout")); |
| 50 | |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 51 | static cl::opt<unsigned> |
| 52 | MemoryLimit("mlimit", cl::init(100), cl::value_desc("MBytes"), |
| 53 | cl::desc("Maximum amount of memory to use. 0 disables check.")); |
| 54 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 55 | // The AnalysesList is automatically populated with registered Passes by the |
| 56 | // PassNameParser. |
| 57 | // |
| 58 | static cl::list<const PassInfo*, bool, PassNameParser> |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 59 | PassList(cl::desc("Passes available:"), cl::ZeroOrMore); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 60 | |
Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 61 | static cl::opt<bool> |
| 62 | StandardCompileOpts("std-compile-opts", |
| 63 | cl::desc("Include the standard compile time optimizations")); |
| 64 | |
| 65 | static cl::opt<bool> |
| 66 | StandardLinkOpts("std-link-opts", |
| 67 | cl::desc("Include the standard link time optimizations")); |
| 68 | |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 69 | static cl::opt<std::string> |
| 70 | OverrideTriple("mtriple", cl::desc("Override target triple for module")); |
| 71 | |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 72 | /// BugpointIsInterrupted - Set to true when the user presses ctrl-c. |
| 73 | bool llvm::BugpointIsInterrupted = false; |
| 74 | |
| 75 | static void BugpointInterruptFunction() { |
| 76 | BugpointIsInterrupted = true; |
| 77 | } |
| 78 | |
Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 79 | // Hack to capture a pass list. |
| 80 | namespace { |
| 81 | class AddToDriver : public PassManager { |
| 82 | BugDriver &D; |
| 83 | public: |
| 84 | AddToDriver(BugDriver &_D) : D(_D) {} |
| 85 | |
| 86 | virtual void add(Pass *P) { |
| 87 | const PassInfo *PI = P->getPassInfo(); |
| 88 | D.addPasses(&PI, &PI + 1); |
| 89 | } |
| 90 | }; |
| 91 | } |
| 92 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 93 | int main(int argc, char **argv) { |
Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 94 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 95 | llvm::PrettyStackTraceProgram X(argc, argv); |
| 96 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Chris Lattner | 670406d | 2003-10-18 21:55:35 +0000 | [diff] [blame] | 97 | cl::ParseCommandLineOptions(argc, argv, |
Dan Gohman | 82a13c9 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 98 | "LLVM automatic testcase reducer. See\nhttp://" |
Chris Lattner | 3a4baf1 | 2009-02-07 18:56:30 +0000 | [diff] [blame] | 99 | "llvm.org/cmds/bugpoint.html" |
Chris Lattner | 670406d | 2003-10-18 21:55:35 +0000 | [diff] [blame] | 100 | " for more information.\n"); |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 101 | sys::SetInterruptFunction(BugpointInterruptFunction); |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 102 | |
Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 103 | LLVMContext& Context = getGlobalContext(); |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 104 | // If we have an override, set it and then track the triple we want Modules |
| 105 | // to use. |
Chris Lattner | 3061108 | 2009-08-31 03:22:35 +0000 | [diff] [blame^] | 106 | if (!OverrideTriple.empty()) { |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 107 | TargetTriple.setTriple(OverrideTriple); |
Chris Lattner | 3061108 | 2009-08-31 03:22:35 +0000 | [diff] [blame^] | 108 | outs() << "Override triple set to '" << OverrideTriple << "'\n"; |
| 109 | } |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 110 | |
Owen Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 111 | BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit, Context); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 112 | if (D.addSources(InputFilenames)) return 1; |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 113 | |
Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 114 | AddToDriver PM(D); |
| 115 | if (StandardCompileOpts) { |
| 116 | createStandardModulePasses(&PM, 3, |
| 117 | /*OptimizeSize=*/ false, |
| 118 | /*UnitAtATime=*/ true, |
| 119 | /*UnrollLoops=*/ true, |
| 120 | /*SimplifyLibCalls=*/ true, |
| 121 | /*HaveExceptions=*/ true, |
| 122 | createFunctionInliningPass()); |
| 123 | } |
| 124 | |
| 125 | if (StandardLinkOpts) |
Dan Gohman | b6dec1b | 2009-07-27 23:23:47 +0000 | [diff] [blame] | 126 | createStandardLTOPasses(&PM, /*Internalize=*/true, |
Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 127 | /*RunInliner=*/true, |
| 128 | /*VerifyEach=*/false); |
| 129 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 130 | D.addPasses(PassList.begin(), PassList.end()); |
| 131 | |
Misha Brukman | 67b36e4 | 2003-09-12 20:42:57 +0000 | [diff] [blame] | 132 | // Bugpoint has the ability of generating a plethora of core files, so to |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 133 | // avoid filling up the disk, we prevent it |
Reid Spencer | 5116330 | 2004-12-27 06:18:02 +0000 | [diff] [blame] | 134 | sys::Process::PreventCoreFiles(); |
Misha Brukman | 67b36e4 | 2003-09-12 20:42:57 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 74d4527 | 2004-02-18 17:32:54 +0000 | [diff] [blame] | 136 | try { |
| 137 | return D.run(); |
Chris Lattner | 230fef8 | 2004-02-18 20:22:11 +0000 | [diff] [blame] | 138 | } catch (ToolExecutionError &TEE) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 139 | errs() << "Tool execution error: " << TEE.what() << '\n'; |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 140 | } catch (const std::string& msg) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 141 | errs() << argv[0] << ": " << msg << "\n"; |
Daniel Dunbar | 482cccd | 2009-08-07 20:50:09 +0000 | [diff] [blame] | 142 | } catch (const std::bad_alloc&) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 143 | errs() << "Oh no, a bugpoint process ran out of memory!\n" |
| 144 | "To increase the allocation limits for bugpoint child\n" |
| 145 | "processes, use the -mlimit option.\n"; |
Dan Gohman | 0324205 | 2009-04-27 01:30:37 +0000 | [diff] [blame] | 146 | } catch (const std::exception &e) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 147 | errs() << "Whoops, a std::exception leaked out of bugpoint: " |
| 148 | << e.what() << "\n" |
| 149 | << "This is a bug in bugpoint!\n"; |
Chris Lattner | 74d4527 | 2004-02-18 17:32:54 +0000 | [diff] [blame] | 150 | } catch (...) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 151 | errs() << "Whoops, an exception leaked out of bugpoint. " |
| 152 | << "This is a bug in bugpoint!\n"; |
Chris Lattner | 74d4527 | 2004-02-18 17:32:54 +0000 | [diff] [blame] | 153 | } |
Reid Spencer | 1ef8bda | 2004-12-30 05:36:08 +0000 | [diff] [blame] | 154 | return 1; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 155 | } |