bugpoint.cpp revision 353358
1193323Sed//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===// 2193323Sed// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6193323Sed// 7193323Sed//===----------------------------------------------------------------------===// 8193323Sed// 9193323Sed// This program is an automated compiler debugger tool. It is used to narrow 10193323Sed// down miscompilations and crash problems to a specific pass in the compiler, 11193323Sed// and the specific Module or Function input that is causing the problem. 12193323Sed// 13193323Sed//===----------------------------------------------------------------------===// 14193323Sed 15193323Sed#include "BugDriver.h" 16193323Sed#include "ToolRunner.h" 17341825Sdim#include "llvm/Config/llvm-config.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" 24341825Sdim#include "llvm/Support/InitLLVM.h" 25193323Sed#include "llvm/Support/ManagedStatic.h" 26193323Sed#include "llvm/Support/PluginLoader.h" 27193323Sed#include "llvm/Support/PrettyStackTrace.h" 28218885Sdim#include "llvm/Support/Process.h" 29321369Sdim#include "llvm/Support/TargetSelect.h" 30218885Sdim#include "llvm/Support/Valgrind.h" 31314564Sdim#include "llvm/Transforms/IPO/AlwaysInliner.h" 32226584Sdim#include "llvm/Transforms/IPO/PassManagerBuilder.h" 33218885Sdim 34314564Sdim// Enable this macro to debug bugpoint itself. 35218885Sdim//#define DEBUG_BUGPOINT 1 36218885Sdim 37193323Sedusing namespace llvm; 38193323Sed 39276479Sdimstatic cl::opt<bool> 40314564Sdim FindBugs("find-bugs", cl::desc("Run many different optimization sequences " 41314564Sdim "on program to find bugs"), 42314564Sdim cl::init(false)); 43193323Sed 44193323Sedstatic cl::list<std::string> 45314564Sdim InputFilenames(cl::Positional, cl::OneOrMore, 46314564Sdim cl::desc("<input llvm ll/bc files>")); 47193323Sed 48314564Sdimstatic cl::opt<unsigned> TimeoutValue( 49314564Sdim "timeout", cl::init(300), cl::value_desc("seconds"), 50314564Sdim cl::desc("Number of seconds program is allowed to run before it " 51314564Sdim "is killed (default is 300s), 0 disables timeout")); 52193323Sed 53327952Sdimstatic cl::opt<int> MemoryLimit( 54327952Sdim "mlimit", cl::init(-1), cl::value_desc("MBytes"), 55327952Sdim cl::desc("Maximum amount of memory to use. 0 disables check. Defaults to " 56327952Sdim "400MB (800MB under valgrind, 0 with sanitizers).")); 57193323Sed 58205407Srdivackystatic cl::opt<bool> 59314564Sdim UseValgrind("enable-valgrind", 60314564Sdim cl::desc("Run optimizations through valgrind")); 61205407Srdivacky 62193323Sed// The AnalysesList is automatically populated with registered Passes by the 63193323Sed// PassNameParser. 64193323Sed// 65314564Sdimstatic cl::list<const PassInfo *, bool, PassNameParser> 66314564Sdim PassList(cl::desc("Passes available:"), cl::ZeroOrMore); 67193323Sed 68198090Srdivackystatic cl::opt<bool> 69314564Sdim StandardLinkOpts("std-link-opts", 70314564Sdim cl::desc("Include the standard link time optimizations")); 71198090Srdivacky 72223013Sdimstatic cl::opt<bool> 73314564Sdim OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'")); 74223013Sdim 75223013Sdimstatic cl::opt<bool> 76314564Sdim OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'")); 77223013Sdim 78314564Sdimstatic cl::opt<bool> OptLevelOs( 79314564Sdim "Os", 80314564Sdim cl::desc( 81314564Sdim "Like -O2 with extra optimizations for size. Similar to clang -Os")); 82314564Sdim 83223013Sdimstatic cl::opt<bool> 84314564Sdim OptLevelO3("O3", cl::desc("Optimization level 3. Identical to 'opt -O3'")); 85223013Sdim 86198090Srdivackystatic cl::opt<std::string> 87314564Sdim OverrideTriple("mtriple", cl::desc("Override target triple for module")); 88198090Srdivacky 89193323Sed/// BugpointIsInterrupted - Set to true when the user presses ctrl-c. 90193323Sedbool llvm::BugpointIsInterrupted = false; 91193323Sed 92218885Sdim#ifndef DEBUG_BUGPOINT 93314564Sdimstatic void BugpointInterruptFunction() { BugpointIsInterrupted = true; } 94218885Sdim#endif 95193323Sed 96198090Srdivacky// Hack to capture a pass list. 97198090Srdivackynamespace { 98314564Sdimclass AddToDriver : public legacy::FunctionPassManager { 99314564Sdim BugDriver &D; 100276479Sdim 101314564Sdimpublic: 102314564Sdim AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {} 103314564Sdim 104314564Sdim void add(Pass *P) override { 105314564Sdim const void *ID = P->getPassID(); 106314564Sdim const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID); 107314564Sdim D.addPass(PI->getPassArgument()); 108314564Sdim } 109314564Sdim}; 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 120341825Sdim InitLLVM X(argc, argv); 121218885Sdim#endif 122276479Sdim 123218885Sdim // Initialize passes 124218885Sdim PassRegistry &Registry = *PassRegistry::getPassRegistry(); 125218885Sdim initializeCore(Registry); 126218885Sdim initializeScalarOpts(Registry); 127249423Sdim initializeObjCARCOpts(Registry); 128234353Sdim initializeVectorization(Registry); 129218885Sdim initializeIPO(Registry); 130218885Sdim initializeAnalysis(Registry); 131218885Sdim initializeTransformUtils(Registry); 132218885Sdim initializeInstCombine(Registry); 133341825Sdim initializeAggressiveInstCombine(Registry); 134218885Sdim initializeInstrumentation(Registry); 135218885Sdim initializeTarget(Registry); 136276479Sdim 137276479Sdim#ifdef LINK_POLLY_INTO_TOOLS 138276479Sdim polly::initializePollyPasses(Registry); 139276479Sdim#endif 140276479Sdim 141321369Sdim if (std::getenv("bar") == (char*) -1) { 142321369Sdim InitializeAllTargets(); 143321369Sdim InitializeAllTargetMCs(); 144321369Sdim InitializeAllAsmPrinters(); 145321369Sdim InitializeAllAsmParsers(); 146321369Sdim } 147321369Sdim 148193323Sed cl::ParseCommandLineOptions(argc, argv, 149193323Sed "LLVM automatic testcase reducer. See\nhttp://" 150193323Sed "llvm.org/cmds/bugpoint.html" 151193323Sed " for more information.\n"); 152218885Sdim#ifndef DEBUG_BUGPOINT 153193323Sed sys::SetInterruptFunction(BugpointInterruptFunction); 154218885Sdim#endif 155195340Sed 156309124Sdim LLVMContext Context; 157198090Srdivacky // If we have an override, set it and then track the triple we want Modules 158198090Srdivacky // to use. 159198090Srdivacky if (!OverrideTriple.empty()) { 160212793Sdim TargetTriple.setTriple(Triple::normalize(OverrideTriple)); 161212793Sdim outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n"; 162198090Srdivacky } 163198090Srdivacky 164205407Srdivacky if (MemoryLimit < 0) { 165205407Srdivacky // Set the default MemoryLimit. Be sure to update the flag's description if 166205407Srdivacky // you change this. 167205407Srdivacky if (sys::RunningOnValgrind() || UseValgrind) 168205407Srdivacky MemoryLimit = 800; 169205407Srdivacky else 170288943Sdim MemoryLimit = 400; 171327952Sdim#if (LLVM_ADDRESS_SANITIZER_BUILD || LLVM_MEMORY_SANITIZER_BUILD || \ 172327952Sdim LLVM_THREAD_SANITIZER_BUILD) 173327952Sdim // Starting from kernel 4.9 memory allocated with mmap is counted against 174327952Sdim // RLIMIT_DATA. Sanitizers need to allocate tens of terabytes for shadow. 175327952Sdim MemoryLimit = 0; 176327952Sdim#endif 177205407Srdivacky } 178205407Srdivacky 179314564Sdim BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind, 180314564Sdim Context); 181314564Sdim if (D.addSources(InputFilenames)) 182314564Sdim return 1; 183276479Sdim 184198090Srdivacky AddToDriver PM(D); 185276479Sdim 186223013Sdim if (StandardLinkOpts) { 187223013Sdim PassManagerBuilder Builder; 188280031Sdim Builder.Inliner = createFunctionInliningPass(); 189280031Sdim Builder.populateLTOPassManager(PM); 190223013Sdim } 191198090Srdivacky 192223013Sdim if (OptLevelO1 || OptLevelO2 || OptLevelO3) { 193223013Sdim PassManagerBuilder Builder; 194223013Sdim if (OptLevelO1) 195314564Sdim Builder.Inliner = createAlwaysInlinerLegacyPass(); 196314564Sdim else if (OptLevelOs || OptLevelO2) 197321369Sdim Builder.Inliner = createFunctionInliningPass( 198321369Sdim 2, OptLevelOs ? 1 : 0, false); 199223013Sdim else 200223013Sdim Builder.Inliner = createFunctionInliningPass(275); 201223013Sdim Builder.populateFunctionPassManager(PM); 202223013Sdim Builder.populateModulePassManager(PM); 203223013Sdim } 204223013Sdim 205296417Sdim for (const PassInfo *PI : PassList) 206212793Sdim D.addPass(PI->getPassArgument()); 207212793Sdim 208314564Sdim// Bugpoint has the ability of generating a plethora of core files, so to 209314564Sdim// avoid filling up the disk, we prevent it 210218885Sdim#ifndef DEBUG_BUGPOINT 211193323Sed sys::Process::PreventCoreFiles(); 212218885Sdim#endif 213193323Sed 214314564Sdim if (Error E = D.run()) { 215314564Sdim errs() << toString(std::move(E)); 216207618Srdivacky return 1; 217193323Sed } 218314564Sdim return 0; 219193323Sed} 220