1 //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 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" 17 #include "ToolRunner.h" 18 #include "llvm/LinkAllPasses.h" 19 #include "llvm/LLVMContext.h" 20 #include "llvm/Support/PassNameParser.h" 21 #include "llvm/Support/CommandLine.h" 22 #include "llvm/Support/ManagedStatic.h" 23 #include "llvm/Support/PluginLoader.h" 24 #include "llvm/Support/PrettyStackTrace.h" 25 #include "llvm/Support/StandardPasses.h" 26 #include "llvm/Support/Process.h" 27 #include "llvm/Support/Signals.h" 28 #include "llvm/Support/Valgrind.h" 29 #include "llvm/LinkAllVMCore.h" 30 31 //Enable this macro to debug bugpoint itself. 32 //#define DEBUG_BUGPOINT 1 33 34 using namespace llvm; 35 36 static cl::opt<bool> 37 FindBugs("find-bugs", cl::desc("Run many different optimization sequences " 38 "on program to find bugs"), cl::init(false)); 39 40 static cl::list<std::string> 41 InputFilenames(cl::Positional, cl::OneOrMore, 42 cl::desc("<input llvm ll/bc files>")); 43 44 static cl::opt<unsigned> 45 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"), 46 cl::desc("Number of seconds program is allowed to run before it " 47 "is killed (default is 300s), 0 disables timeout")); 48 49 static cl::opt<int> 50 MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"), 51 cl::desc("Maximum amount of memory to use. 0 disables check." 52 " Defaults to 100MB (800MB under valgrind).")); 53 54 static cl::opt<bool> 55 UseValgrind("enable-valgrind", 56 cl::desc("Run optimizations through valgrind")); 57 58 // The AnalysesList is automatically populated with registered Passes by the 59 // PassNameParser. 60 // 61 static cl::list<const PassInfo*, bool, PassNameParser> 62 PassList(cl::desc("Passes available:"), cl::ZeroOrMore); 63 64 static cl::opt<bool> 65 StandardCompileOpts("std-compile-opts", 66 cl::desc("Include the standard compile time optimizations")); 67 68 static cl::opt<bool> 69 StandardLinkOpts("std-link-opts", 70 cl::desc("Include the standard link time optimizations")); 71 72 static cl::opt<std::string> 73 OverrideTriple("mtriple", cl::desc("Override target triple for module")); 74 75 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c. 76 bool llvm::BugpointIsInterrupted = false; 77 78 #ifndef DEBUG_BUGPOINT 79 static void BugpointInterruptFunction() { 80 BugpointIsInterrupted = true; 81 } 82 #endif 83 84 // Hack to capture a pass list. 85 namespace { 86 class AddToDriver : public PassManager { 87 BugDriver &D; 88 public: 89 AddToDriver(BugDriver &_D) : D(_D) {} 90 91 virtual void add(Pass *P) { 92 const void *ID = P->getPassID(); 93 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID); 94 D.addPass(PI->getPassArgument()); 95 } 96 }; 97 } 98 99 int main(int argc, char **argv) { 100 #ifndef DEBUG_BUGPOINT 101 llvm::sys::PrintStackTraceOnErrorSignal(); 102 llvm::PrettyStackTraceProgram X(argc, argv); 103 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 104 #endif 105 106 // Initialize passes 107 PassRegistry &Registry = *PassRegistry::getPassRegistry(); 108 initializeCore(Registry); 109 initializeScalarOpts(Registry); 110 initializeIPO(Registry); 111 initializeAnalysis(Registry); 112 initializeIPA(Registry); 113 initializeTransformUtils(Registry); 114 initializeInstCombine(Registry); 115 initializeInstrumentation(Registry); 116 initializeTarget(Registry); 117 118 cl::ParseCommandLineOptions(argc, argv, 119 "LLVM automatic testcase reducer. See\nhttp://" 120 "llvm.org/cmds/bugpoint.html" 121 " for more information.\n"); 122 #ifndef DEBUG_BUGPOINT 123 sys::SetInterruptFunction(BugpointInterruptFunction); 124 #endif 125 126 LLVMContext& Context = getGlobalContext(); 127 // If we have an override, set it and then track the triple we want Modules 128 // to use. 129 if (!OverrideTriple.empty()) { 130 TargetTriple.setTriple(Triple::normalize(OverrideTriple)); 131 outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n"; 132 } 133 134 if (MemoryLimit < 0) { 135 // Set the default MemoryLimit. Be sure to update the flag's description if 136 // you change this. 137 if (sys::RunningOnValgrind() || UseValgrind) 138 MemoryLimit = 800; 139 else 140 MemoryLimit = 100; 141 } 142 143 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, 144 UseValgrind, Context); 145 if (D.addSources(InputFilenames)) return 1; 146 147 AddToDriver PM(D); 148 if (StandardCompileOpts) { 149 createStandardModulePasses(&PM, 3, 150 /*OptimizeSize=*/ false, 151 /*UnitAtATime=*/ true, 152 /*UnrollLoops=*/ true, 153 /*SimplifyLibCalls=*/ true, 154 /*HaveExceptions=*/ true, 155 createFunctionInliningPass()); 156 } 157 158 if (StandardLinkOpts) 159 createStandardLTOPasses(&PM, /*Internalize=*/true, 160 /*RunInliner=*/true, 161 /*VerifyEach=*/false); 162 163 164 for (std::vector<const PassInfo*>::iterator I = PassList.begin(), 165 E = PassList.end(); 166 I != E; ++I) { 167 const PassInfo* PI = *I; 168 D.addPass(PI->getPassArgument()); 169 } 170 171 // Bugpoint has the ability of generating a plethora of core files, so to 172 // avoid filling up the disk, we prevent it 173 #ifndef DEBUG_BUGPOINT 174 sys::Process::PreventCoreFiles(); 175 #endif 176 177 std::string Error; 178 bool Failure = D.run(Error); 179 if (!Error.empty()) { 180 errs() << Error; 181 return 1; 182 } 183 return Failure; 184 } 185