bugpoint.cpp revision 276479
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" 19276479Sdim#include "llvm/IR/LegacyPassNameParser.h" 20249423Sdim#include "llvm/LinkAllIR.h" 21193323Sed#include "llvm/LinkAllPasses.h" 22226584Sdim#include "llvm/PassManager.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." 53261991Sdim " Defaults to 300MB (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> 66276479SdimStandardCompileOpts("std-compile-opts", 67198090Srdivacky cl::desc("Include the standard compile time optimizations")); 68198090Srdivacky 69198090Srdivackystatic cl::opt<bool> 70276479SdimStandardLinkOpts("std-link-opts", 71198090Srdivacky cl::desc("Include the standard link time optimizations")); 72198090Srdivacky 73223013Sdimstatic cl::opt<bool> 74223013SdimOptLevelO1("O1", 75276479Sdim cl::desc("Optimization level 1. Identical to 'opt -O1'")); 76223013Sdim 77223013Sdimstatic cl::opt<bool> 78223013SdimOptLevelO2("O2", 79276479Sdim cl::desc("Optimization level 2. Identical to 'opt -O2'")); 80223013Sdim 81223013Sdimstatic cl::opt<bool> 82223013SdimOptLevelO3("O3", 83276479Sdim cl::desc("Optimization level 3. Identical to 'opt -O3'")); 84223013Sdim 85198090Srdivackystatic cl::opt<std::string> 86198090SrdivackyOverrideTriple("mtriple", cl::desc("Override target triple for module")); 87198090Srdivacky 88193323Sed/// BugpointIsInterrupted - Set to true when the user presses ctrl-c. 89193323Sedbool llvm::BugpointIsInterrupted = false; 90193323Sed 91218885Sdim#ifndef DEBUG_BUGPOINT 92193323Sedstatic void BugpointInterruptFunction() { 93193323Sed BugpointIsInterrupted = true; 94193323Sed} 95218885Sdim#endif 96193323Sed 97198090Srdivacky// Hack to capture a pass list. 98198090Srdivackynamespace { 99223013Sdim class AddToDriver : public FunctionPassManager { 100198090Srdivacky BugDriver &D; 101198090Srdivacky public: 102276479Sdim AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {} 103276479Sdim 104276479Sdim void add(Pass *P) override { 105212793Sdim const void *ID = P->getPassID(); 106212793Sdim const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID); 107212793Sdim D.addPass(PI->getPassArgument()); 108198090Srdivacky } 109198090Srdivacky }; 110198090Srdivacky} 111198090Srdivacky 112276479Sdim#ifdef LINK_POLLY_INTO_TOOLS 113276479Sdimnamespace polly { 114276479Sdimvoid initializePollyPasses(llvm::PassRegistry &Registry); 115276479Sdim} 116276479Sdim#endif 117276479Sdim 118193323Sedint main(int argc, char **argv) { 119218885Sdim#ifndef DEBUG_BUGPOINT 120193323Sed llvm::sys::PrintStackTraceOnErrorSignal(); 121193323Sed llvm::PrettyStackTraceProgram X(argc, argv); 122193323Sed llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 123218885Sdim#endif 124276479Sdim 125218885Sdim // Initialize passes 126218885Sdim PassRegistry &Registry = *PassRegistry::getPassRegistry(); 127218885Sdim initializeCore(Registry); 128218885Sdim initializeScalarOpts(Registry); 129249423Sdim initializeObjCARCOpts(Registry); 130234353Sdim initializeVectorization(Registry); 131218885Sdim initializeIPO(Registry); 132218885Sdim initializeAnalysis(Registry); 133218885Sdim initializeIPA(Registry); 134218885Sdim initializeTransformUtils(Registry); 135218885Sdim initializeInstCombine(Registry); 136218885Sdim initializeInstrumentation(Registry); 137218885Sdim initializeTarget(Registry); 138276479Sdim 139276479Sdim#ifdef LINK_POLLY_INTO_TOOLS 140276479Sdim polly::initializePollyPasses(Registry); 141276479Sdim#endif 142276479Sdim 143193323Sed cl::ParseCommandLineOptions(argc, argv, 144193323Sed "LLVM automatic testcase reducer. See\nhttp://" 145193323Sed "llvm.org/cmds/bugpoint.html" 146193323Sed " for more information.\n"); 147218885Sdim#ifndef DEBUG_BUGPOINT 148193323Sed sys::SetInterruptFunction(BugpointInterruptFunction); 149218885Sdim#endif 150195340Sed 151198090Srdivacky LLVMContext& Context = getGlobalContext(); 152198090Srdivacky // If we have an override, set it and then track the triple we want Modules 153198090Srdivacky // to use. 154198090Srdivacky if (!OverrideTriple.empty()) { 155212793Sdim TargetTriple.setTriple(Triple::normalize(OverrideTriple)); 156212793Sdim outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n"; 157198090Srdivacky } 158198090Srdivacky 159205407Srdivacky if (MemoryLimit < 0) { 160205407Srdivacky // Set the default MemoryLimit. Be sure to update the flag's description if 161205407Srdivacky // you change this. 162205407Srdivacky if (sys::RunningOnValgrind() || UseValgrind) 163205407Srdivacky MemoryLimit = 800; 164205407Srdivacky else 165261991Sdim MemoryLimit = 300; 166205407Srdivacky } 167205407Srdivacky 168212793Sdim BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, 169205407Srdivacky UseValgrind, Context); 170193323Sed if (D.addSources(InputFilenames)) return 1; 171276479Sdim 172198090Srdivacky AddToDriver PM(D); 173198090Srdivacky if (StandardCompileOpts) { 174223013Sdim PassManagerBuilder Builder; 175223013Sdim Builder.OptLevel = 3; 176223013Sdim Builder.Inliner = createFunctionInliningPass(); 177223013Sdim Builder.populateModulePassManager(PM); 178198090Srdivacky } 179276479Sdim 180223013Sdim if (StandardLinkOpts) { 181223013Sdim PassManagerBuilder Builder; 182223013Sdim Builder.populateLTOPassManager(PM, /*Internalize=*/true, 183223013Sdim /*RunInliner=*/true); 184223013Sdim } 185198090Srdivacky 186223013Sdim if (OptLevelO1 || OptLevelO2 || OptLevelO3) { 187223013Sdim PassManagerBuilder Builder; 188223013Sdim if (OptLevelO1) 189223013Sdim Builder.Inliner = createAlwaysInlinerPass(); 190223013Sdim else if (OptLevelO2) 191223013Sdim Builder.Inliner = createFunctionInliningPass(225); 192223013Sdim else 193223013Sdim Builder.Inliner = createFunctionInliningPass(275); 194193323Sed 195223013Sdim // Note that although clang/llvm-gcc use two separate passmanagers 196223013Sdim // here, it shouldn't normally make a difference. 197223013Sdim Builder.populateFunctionPassManager(PM); 198223013Sdim Builder.populateModulePassManager(PM); 199223013Sdim } 200223013Sdim 201212793Sdim for (std::vector<const PassInfo*>::iterator I = PassList.begin(), 202212793Sdim E = PassList.end(); 203212793Sdim I != E; ++I) { 204212793Sdim const PassInfo* PI = *I; 205212793Sdim D.addPass(PI->getPassArgument()); 206212793Sdim } 207212793Sdim 208193323Sed // Bugpoint has the ability of generating a plethora of core files, so to 209193323Sed // avoid filling up the disk, we prevent it 210218885Sdim#ifndef DEBUG_BUGPOINT 211193323Sed sys::Process::PreventCoreFiles(); 212218885Sdim#endif 213193323Sed 214207618Srdivacky std::string Error; 215207618Srdivacky bool Failure = D.run(Error); 216207618Srdivacky if (!Error.empty()) { 217207618Srdivacky errs() << Error; 218207618Srdivacky return 1; 219193323Sed } 220207618Srdivacky return Failure; 221193323Sed} 222