| 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" |
| Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/LLVMContext.h" |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassNameParser.h" |
| Jakub Staszak | 446991d | 2013-01-10 21:56:40 +0000 | [diff] [blame] | 20 | #include "llvm/LinkAllIR.h" |
| Chandler Carruth | 90230c8 | 2013-01-19 08:03:47 +0000 | [diff] [blame] | 21 | #include "llvm/LinkAllPasses.h" |
| Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 22 | #include "llvm/PassManager.h" |
| Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
| Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ManagedStatic.h" |
| Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/PluginLoader.h" |
| Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 26 | #include "llvm/Support/PrettyStackTrace.h" |
| Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Process.h" |
| 28 | #include "llvm/Support/Signals.h" |
| 29 | #include "llvm/Support/Valgrind.h" |
| Rafael Espindola | 3d453ac | 2011-08-02 21:50:24 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| Devang Patel | bc8d5f1 | 2011-01-13 19:48:54 +0000 | [diff] [blame] | 31 | |
| Devang Patel | 3f84a45 | 2011-01-14 15:55:50 +0000 | [diff] [blame] | 32 | //Enable this macro to debug bugpoint itself. |
| 33 | //#define DEBUG_BUGPOINT 1 |
| Devang Patel | bc8d5f1 | 2011-01-13 19:48:54 +0000 | [diff] [blame] | 34 | |
| Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 37 | static cl::opt<bool> |
| Dan Gohman | 3956449 | 2008-02-18 17:15:45 +0000 | [diff] [blame] | 38 | FindBugs("find-bugs", cl::desc("Run many different optimization sequences " |
| Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 39 | "on program to find bugs"), cl::init(false)); |
| Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 40 | |
| Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 41 | static cl::list<std::string> |
| 42 | InputFilenames(cl::Positional, cl::OneOrMore, |
| 43 | cl::desc("<input llvm ll/bc files>")); |
| 44 | |
| Chris Lattner | 9686ae7 | 2006-06-13 03:10:48 +0000 | [diff] [blame] | 45 | static cl::opt<unsigned> |
| 46 | TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"), |
| 47 | cl::desc("Number of seconds program is allowed to run before it " |
| 48 | "is killed (default is 300s), 0 disables timeout")); |
| 49 | |
| Jeffrey Yasskin | c3e6859 | 2010-03-19 00:09:28 +0000 | [diff] [blame] | 50 | static cl::opt<int> |
| 51 | MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"), |
| Daniel Sanders | 69f4280 | 2013-10-25 17:41:41 +0000 | [diff] [blame] | 52 | cl::desc("Maximum amount of memory to use. 0 disables check." |
| 53 | " Defaults to 300MB (800MB under valgrind).")); |
| Jeffrey Yasskin | c3e6859 | 2010-03-19 00:09:28 +0000 | [diff] [blame] | 54 | |
| 55 | static cl::opt<bool> |
| 56 | UseValgrind("enable-valgrind", |
| 57 | cl::desc("Run optimizations through valgrind")); |
| Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 58 | |
| Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 59 | // The AnalysesList is automatically populated with registered Passes by the |
| 60 | // PassNameParser. |
| 61 | // |
| Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 62 | static cl::list<const PassInfo*, bool, PassNameParser> |
| Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 63 | PassList(cl::desc("Passes available:"), cl::ZeroOrMore); |
| Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 64 | |
| Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 65 | static cl::opt<bool> |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 66 | StandardCompileOpts("std-compile-opts", |
| Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 67 | cl::desc("Include the standard compile time optimizations")); |
| 68 | |
| 69 | static cl::opt<bool> |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 70 | StandardLinkOpts("std-link-opts", |
| Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 71 | cl::desc("Include the standard link time optimizations")); |
| 72 | |
| Eli Friedman | be2d123 | 2011-06-06 22:45:46 +0000 | [diff] [blame] | 73 | static cl::opt<bool> |
| 74 | OptLevelO1("O1", |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 75 | cl::desc("Optimization level 1. Identical to 'opt -O1'")); |
| Eli Friedman | be2d123 | 2011-06-06 22:45:46 +0000 | [diff] [blame] | 76 | |
| 77 | static cl::opt<bool> |
| 78 | OptLevelO2("O2", |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 79 | cl::desc("Optimization level 2. Identical to 'opt -O2'")); |
| Eli Friedman | be2d123 | 2011-06-06 22:45:46 +0000 | [diff] [blame] | 80 | |
| 81 | static cl::opt<bool> |
| 82 | OptLevelO3("O3", |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 83 | cl::desc("Optimization level 3. Identical to 'opt -O3'")); |
| Eli Friedman | be2d123 | 2011-06-06 22:45:46 +0000 | [diff] [blame] | 84 | |
| Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 85 | static cl::opt<std::string> |
| 86 | OverrideTriple("mtriple", cl::desc("Override target triple for module")); |
| 87 | |
| Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 88 | /// BugpointIsInterrupted - Set to true when the user presses ctrl-c. |
| 89 | bool llvm::BugpointIsInterrupted = false; |
| 90 | |
| Devang Patel | bc8d5f1 | 2011-01-13 19:48:54 +0000 | [diff] [blame] | 91 | #ifndef DEBUG_BUGPOINT |
| Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 92 | static void BugpointInterruptFunction() { |
| 93 | BugpointIsInterrupted = true; |
| 94 | } |
| Devang Patel | bc8d5f1 | 2011-01-13 19:48:54 +0000 | [diff] [blame] | 95 | #endif |
| Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 96 | |
| Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 97 | // Hack to capture a pass list. |
| 98 | namespace { |
| Eli Friedman | be2d123 | 2011-06-06 22:45:46 +0000 | [diff] [blame] | 99 | class AddToDriver : public FunctionPassManager { |
| Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 100 | BugDriver &D; |
| 101 | public: |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 102 | AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {} |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 103 | |
| 104 | void add(Pass *P) override { |
| Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 105 | const void *ID = P->getPassID(); |
| 106 | const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID); |
| Rafael Espindola | 8261dfe | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 107 | D.addPass(PI->getPassArgument()); |
| Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 108 | } |
| 109 | }; |
| 110 | } |
| 111 | |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 112 | #ifdef LINK_POLLY_INTO_TOOLS |
| 113 | namespace polly { |
| 114 | void initializePollyPasses(llvm::PassRegistry &Registry); |
| 115 | } |
| 116 | #endif |
| 117 | |
| Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 118 | int main(int argc, char **argv) { |
| Devang Patel | bc8d5f1 | 2011-01-13 19:48:54 +0000 | [diff] [blame] | 119 | #ifndef DEBUG_BUGPOINT |
| Chris Lattner | cc14d25 | 2009-03-06 05:34:10 +0000 | [diff] [blame] | 120 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 121 | llvm::PrettyStackTraceProgram X(argc, argv); |
| 122 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| Devang Patel | bc8d5f1 | 2011-01-13 19:48:54 +0000 | [diff] [blame] | 123 | #endif |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 124 | |
| Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 125 | // Initialize passes |
| 126 | PassRegistry &Registry = *PassRegistry::getPassRegistry(); |
| 127 | initializeCore(Registry); |
| 128 | initializeScalarOpts(Registry); |
| Michael Gottesman | 24c4898 | 2013-01-28 01:35:51 +0000 | [diff] [blame] | 129 | initializeObjCARCOpts(Registry); |
| Hal Finkel | 0ae2510 | 2012-02-07 21:11:12 +0000 | [diff] [blame] | 130 | initializeVectorization(Registry); |
| Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 131 | initializeIPO(Registry); |
| 132 | initializeAnalysis(Registry); |
| 133 | initializeIPA(Registry); |
| 134 | initializeTransformUtils(Registry); |
| 135 | initializeInstCombine(Registry); |
| 136 | initializeInstrumentation(Registry); |
| 137 | initializeTarget(Registry); |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 138 | |
| 139 | #ifdef LINK_POLLY_INTO_TOOLS |
| 140 | polly::initializePollyPasses(Registry); |
| 141 | #endif |
| 142 | |
| Chris Lattner | 670406d | 2003-10-18 21:55:35 +0000 | [diff] [blame] | 143 | cl::ParseCommandLineOptions(argc, argv, |
| Dan Gohman | 82a13c9 | 2007-10-08 15:45:12 +0000 | [diff] [blame] | 144 | "LLVM automatic testcase reducer. See\nhttp://" |
| Chris Lattner | 3a4baf1 | 2009-02-07 18:56:30 +0000 | [diff] [blame] | 145 | "llvm.org/cmds/bugpoint.html" |
| Chris Lattner | 670406d | 2003-10-18 21:55:35 +0000 | [diff] [blame] | 146 | " for more information.\n"); |
| Devang Patel | bc8d5f1 | 2011-01-13 19:48:54 +0000 | [diff] [blame] | 147 | #ifndef DEBUG_BUGPOINT |
| Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 148 | sys::SetInterruptFunction(BugpointInterruptFunction); |
| Devang Patel | bc8d5f1 | 2011-01-13 19:48:54 +0000 | [diff] [blame] | 149 | #endif |
| Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 150 | |
| Owen Anderson | 0d7c695 | 2009-07-15 22:16:10 +0000 | [diff] [blame] | 151 | LLVMContext& Context = getGlobalContext(); |
| Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 152 | // If we have an override, set it and then track the triple we want Modules |
| 153 | // to use. |
| Chris Lattner | 3061108 | 2009-08-31 03:22:35 +0000 | [diff] [blame] | 154 | if (!OverrideTriple.empty()) { |
| Duncan Sands | 75ebbce | 2010-08-28 01:30:02 +0000 | [diff] [blame] | 155 | TargetTriple.setTriple(Triple::normalize(OverrideTriple)); |
| 156 | outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n"; |
| Chris Lattner | 3061108 | 2009-08-31 03:22:35 +0000 | [diff] [blame] | 157 | } |
| Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 158 | |
| Jeffrey Yasskin | c3e6859 | 2010-03-19 00:09:28 +0000 | [diff] [blame] | 159 | if (MemoryLimit < 0) { |
| 160 | // Set the default MemoryLimit. Be sure to update the flag's description if |
| 161 | // you change this. |
| 162 | if (sys::RunningOnValgrind() || UseValgrind) |
| 163 | MemoryLimit = 800; |
| 164 | else |
| Daniel Sanders | 69f4280 | 2013-10-25 17:41:41 +0000 | [diff] [blame] | 165 | MemoryLimit = 300; |
| Jeffrey Yasskin | c3e6859 | 2010-03-19 00:09:28 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| Rafael Espindola | 7f99f74 | 2010-08-07 23:03:21 +0000 | [diff] [blame] | 168 | BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, |
| Jeffrey Yasskin | c3e6859 | 2010-03-19 00:09:28 +0000 | [diff] [blame] | 169 | UseValgrind, Context); |
| Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 170 | if (D.addSources(InputFilenames)) return 1; |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 171 | |
| Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 172 | AddToDriver PM(D); |
| 173 | if (StandardCompileOpts) { |
| Chris Lattner | 817a01f | 2011-05-22 00:20:07 +0000 | [diff] [blame] | 174 | PassManagerBuilder Builder; |
| 175 | Builder.OptLevel = 3; |
| 176 | Builder.Inliner = createFunctionInliningPass(); |
| 177 | Builder.populateModulePassManager(PM); |
| Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 178 | } |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 179 | |
| Chris Lattner | 817a01f | 2011-05-22 00:20:07 +0000 | [diff] [blame] | 180 | if (StandardLinkOpts) { |
| 181 | PassManagerBuilder Builder; |
| 182 | Builder.populateLTOPassManager(PM, /*Internalize=*/true, |
| 183 | /*RunInliner=*/true); |
| 184 | } |
| Daniel Dunbar | 1488670 | 2009-07-20 07:01:01 +0000 | [diff] [blame] | 185 | |
| Eli Friedman | be2d123 | 2011-06-06 22:45:46 +0000 | [diff] [blame] | 186 | if (OptLevelO1 || OptLevelO2 || OptLevelO3) { |
| 187 | PassManagerBuilder Builder; |
| 188 | if (OptLevelO1) |
| 189 | Builder.Inliner = createAlwaysInlinerPass(); |
| 190 | else if (OptLevelO2) |
| 191 | Builder.Inliner = createFunctionInliningPass(225); |
| 192 | else |
| 193 | Builder.Inliner = createFunctionInliningPass(275); |
| 194 | |
| 195 | // Note that although clang/llvm-gcc use two separate passmanagers |
| 196 | // here, it shouldn't normally make a difference. |
| 197 | Builder.populateFunctionPassManager(PM); |
| 198 | Builder.populateModulePassManager(PM); |
| 199 | } |
| Rafael Espindola | 8261dfe | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 200 | |
| 201 | for (std::vector<const PassInfo*>::iterator I = PassList.begin(), |
| 202 | E = PassList.end(); |
| 203 | I != E; ++I) { |
| 204 | const PassInfo* PI = *I; |
| 205 | D.addPass(PI->getPassArgument()); |
| 206 | } |
| Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 207 | |
| Misha Brukman | 67b36e4 | 2003-09-12 20:42:57 +0000 | [diff] [blame] | 208 | // 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] | 209 | // avoid filling up the disk, we prevent it |
| Devang Patel | bc8d5f1 | 2011-01-13 19:48:54 +0000 | [diff] [blame] | 210 | #ifndef DEBUG_BUGPOINT |
| Reid Spencer | 5116330 | 2004-12-27 06:18:02 +0000 | [diff] [blame] | 211 | sys::Process::PreventCoreFiles(); |
| Devang Patel | bc8d5f1 | 2011-01-13 19:48:54 +0000 | [diff] [blame] | 212 | #endif |
| Misha Brukman | 67b36e4 | 2003-09-12 20:42:57 +0000 | [diff] [blame] | 213 | |
| Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 214 | std::string Error; |
| 215 | bool Failure = D.run(Error); |
| 216 | if (!Error.empty()) { |
| 217 | errs() << Error; |
| 218 | return 1; |
| Chris Lattner | 74d4527 | 2004-02-18 17:32:54 +0000 | [diff] [blame] | 219 | } |
| Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 220 | return Failure; |
| Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 221 | } |