bugpoint.cpp revision 288943
1193323Sed//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This program is an automated compiler debugger tool. It is used to narrow 11193323Sed// down miscompilations and crash problems to a specific pass in the compiler, 12193323Sed// and the specific Module or Function input that is causing the problem. 13193323Sed// 14193323Sed//===----------------------------------------------------------------------===// 15193323Sed 16193323Sed#include "BugDriver.h" 17193323Sed#include "ToolRunner.h" 18249423Sdim#include "llvm/IR/LLVMContext.h" 19288943Sdim#include "llvm/IR/LegacyPassManager.h" 20276479Sdim#include "llvm/IR/LegacyPassNameParser.h" 21249423Sdim#include "llvm/LinkAllIR.h" 22193323Sed#include "llvm/LinkAllPasses.h" 23193323Sed#include "llvm/Support/CommandLine.h" 24193323Sed#include "llvm/Support/ManagedStatic.h" 25193323Sed#include "llvm/Support/PluginLoader.h" 26193323Sed#include "llvm/Support/PrettyStackTrace.h" 27218885Sdim#include "llvm/Support/Process.h" 28218885Sdim#include "llvm/Support/Signals.h" 29218885Sdim#include "llvm/Support/Valgrind.h" 30226584Sdim#include "llvm/Transforms/IPO/PassManagerBuilder.h" 31218885Sdim 32218885Sdim//Enable this macro to debug bugpoint itself. 33218885Sdim//#define DEBUG_BUGPOINT 1 34218885Sdim 35193323Sedusing namespace llvm; 36193323Sed 37276479Sdimstatic cl::opt<bool> 38193323SedFindBugs("find-bugs", cl::desc("Run many different optimization sequences " 39193323Sed "on program to find bugs"), cl::init(false)); 40193323Sed 41193323Sedstatic cl::list<std::string> 42193323SedInputFilenames(cl::Positional, cl::OneOrMore, 43193323Sed cl::desc("<input llvm ll/bc files>")); 44193323Sed 45193323Sedstatic cl::opt<unsigned> 46193323SedTimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"), 47193323Sed cl::desc("Number of seconds program is allowed to run before it " 48193323Sed "is killed (default is 300s), 0 disables timeout")); 49193323Sed 50205407Srdivackystatic cl::opt<int> 51205407SrdivackyMemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"), 52261991Sdim cl::desc("Maximum amount of memory to use. 0 disables check." 53288943Sdim " Defaults to 400MB (800MB under valgrind).")); 54193323Sed 55205407Srdivackystatic cl::opt<bool> 56205407SrdivackyUseValgrind("enable-valgrind", 57205407Srdivacky cl::desc("Run optimizations through valgrind")); 58205407Srdivacky 59193323Sed// The AnalysesList is automatically populated with registered Passes by the 60193323Sed// PassNameParser. 61193323Sed// 62193323Sedstatic cl::list<const PassInfo*, bool, PassNameParser> 63193323SedPassList(cl::desc("Passes available:"), cl::ZeroOrMore); 64193323Sed 65198090Srdivackystatic cl::opt<bool> 66276479SdimStandardLinkOpts("std-link-opts", 67198090Srdivacky cl::desc("Include the standard link time optimizations")); 68198090Srdivacky 69223013Sdimstatic cl::opt<bool> 70223013SdimOptLevelO1("O1", 71276479Sdim cl::desc("Optimization level 1. Identical to 'opt -O1'")); 72223013Sdim 73223013Sdimstatic cl::opt<bool> 74223013SdimOptLevelO2("O2", 75276479Sdim cl::desc("Optimization level 2. Identical to 'opt -O2'")); 76223013Sdim 77223013Sdimstatic cl::opt<bool> 78223013SdimOptLevelO3("O3", 79276479Sdim cl::desc("Optimization level 3. Identical to 'opt -O3'")); 80223013Sdim 81198090Srdivackystatic cl::opt<std::string> 82198090SrdivackyOverrideTriple("mtriple", cl::desc("Override target triple for module")); 83198090Srdivacky 84193323Sed/// BugpointIsInterrupted - Set to true when the user presses ctrl-c. 85193323Sedbool llvm::BugpointIsInterrupted = false; 86193323Sed 87218885Sdim#ifndef DEBUG_BUGPOINT 88193323Sedstatic void BugpointInterruptFunction() { 89193323Sed BugpointIsInterrupted = true; 90193323Sed} 91218885Sdim#endif 92193323Sed 93198090Srdivacky// Hack to capture a pass list. 94198090Srdivackynamespace { 95288943Sdim class AddToDriver : public legacy::FunctionPassManager { 96198090Srdivacky BugDriver &D; 97198090Srdivacky public: 98276479Sdim AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {} 99276479Sdim 100276479Sdim void add(Pass *P) override { 101212793Sdim const void *ID = P->getPassID(); 102212793Sdim const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID); 103212793Sdim D.addPass(PI->getPassArgument()); 104198090Srdivacky } 105198090Srdivacky }; 106198090Srdivacky} 107198090Srdivacky 108276479Sdim#ifdef LINK_POLLY_INTO_TOOLS 109276479Sdimnamespace polly { 110276479Sdimvoid initializePollyPasses(llvm::PassRegistry &Registry); 111276479Sdim} 112276479Sdim#endif 113276479Sdim 114193323Sedint main(int argc, char **argv) { 115218885Sdim#ifndef DEBUG_BUGPOINT 116193323Sed llvm::sys::PrintStackTraceOnErrorSignal(); 117193323Sed llvm::PrettyStackTraceProgram X(argc, argv); 118193323Sed llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 119218885Sdim#endif 120276479Sdim 121218885Sdim // Initialize passes 122218885Sdim PassRegistry &Registry = *PassRegistry::getPassRegistry(); 123218885Sdim initializeCore(Registry); 124218885Sdim initializeScalarOpts(Registry); 125249423Sdim initializeObjCARCOpts(Registry); 126234353Sdim initializeVectorization(Registry); 127218885Sdim initializeIPO(Registry); 128218885Sdim initializeAnalysis(Registry); 129218885Sdim initializeIPA(Registry); 130218885Sdim initializeTransformUtils(Registry); 131218885Sdim initializeInstCombine(Registry); 132218885Sdim initializeInstrumentation(Registry); 133218885Sdim initializeTarget(Registry); 134276479Sdim 135276479Sdim#ifdef LINK_POLLY_INTO_TOOLS 136276479Sdim polly::initializePollyPasses(Registry); 137276479Sdim#endif 138276479Sdim 139193323Sed cl::ParseCommandLineOptions(argc, argv, 140193323Sed "LLVM automatic testcase reducer. See\nhttp://" 141193323Sed "llvm.org/cmds/bugpoint.html" 142193323Sed " for more information.\n"); 143218885Sdim#ifndef DEBUG_BUGPOINT 144193323Sed sys::SetInterruptFunction(BugpointInterruptFunction); 145218885Sdim#endif 146195340Sed 147198090Srdivacky LLVMContext& Context = getGlobalContext(); 148198090Srdivacky // If we have an override, set it and then track the triple we want Modules 149198090Srdivacky // to use. 150198090Srdivacky if (!OverrideTriple.empty()) { 151212793Sdim TargetTriple.setTriple(Triple::normalize(OverrideTriple)); 152212793Sdim outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n"; 153198090Srdivacky } 154198090Srdivacky 155205407Srdivacky if (MemoryLimit < 0) { 156205407Srdivacky // Set the default MemoryLimit. Be sure to update the flag's description if 157205407Srdivacky // you change this. 158205407Srdivacky if (sys::RunningOnValgrind() || UseValgrind) 159205407Srdivacky MemoryLimit = 800; 160205407Srdivacky else 161288943Sdim MemoryLimit = 400; 162205407Srdivacky } 163205407Srdivacky 164212793Sdim BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, 165205407Srdivacky UseValgrind, Context); 166193323Sed if (D.addSources(InputFilenames)) return 1; 167276479Sdim 168198090Srdivacky AddToDriver PM(D); 169276479Sdim 170223013Sdim if (StandardLinkOpts) { 171223013Sdim PassManagerBuilder Builder; 172280031Sdim Builder.Inliner = createFunctionInliningPass(); 173280031Sdim Builder.populateLTOPassManager(PM); 174223013Sdim } 175198090Srdivacky 176223013Sdim if (OptLevelO1 || OptLevelO2 || OptLevelO3) { 177223013Sdim PassManagerBuilder Builder; 178223013Sdim if (OptLevelO1) 179223013Sdim Builder.Inliner = createAlwaysInlinerPass(); 180223013Sdim else if (OptLevelO2) 181223013Sdim Builder.Inliner = createFunctionInliningPass(225); 182223013Sdim else 183223013Sdim Builder.Inliner = createFunctionInliningPass(275); 184193323Sed 185223013Sdim // Note that although clang/llvm-gcc use two separate passmanagers 186223013Sdim // here, it shouldn't normally make a difference. 187223013Sdim Builder.populateFunctionPassManager(PM); 188223013Sdim Builder.populateModulePassManager(PM); 189223013Sdim } 190223013Sdim 191212793Sdim for (std::vector<const PassInfo*>::iterator I = PassList.begin(), 192212793Sdim E = PassList.end(); 193212793Sdim I != E; ++I) { 194212793Sdim const PassInfo* PI = *I; 195212793Sdim D.addPass(PI->getPassArgument()); 196212793Sdim } 197212793Sdim 198193323Sed // Bugpoint has the ability of generating a plethora of core files, so to 199193323Sed // avoid filling up the disk, we prevent it 200218885Sdim#ifndef DEBUG_BUGPOINT 201193323Sed sys::Process::PreventCoreFiles(); 202218885Sdim#endif 203193323Sed 204207618Srdivacky std::string Error; 205207618Srdivacky bool Failure = D.run(Error); 206207618Srdivacky if (!Error.empty()) { 207207618Srdivacky errs() << Error; 208207618Srdivacky return 1; 209193323Sed } 210207618Srdivacky return Failure; 211193323Sed} 212