BugDriver.cpp revision 296417
1193323Sed//===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===// 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 class contains all of the shared state and information that is used by 11193323Sed// the BugPoint tool to track down errors in optimizations. This class is the 12193323Sed// main driver class that invokes all sub-functionality. 13193323Sed// 14193323Sed//===----------------------------------------------------------------------===// 15193323Sed 16193323Sed#include "BugDriver.h" 17193323Sed#include "ToolRunner.h" 18249423Sdim#include "llvm/IR/Module.h" 19288943Sdim#include "llvm/IR/Verifier.h" 20249423Sdim#include "llvm/IRReader/IRReader.h" 21276479Sdim#include "llvm/Linker/Linker.h" 22193323Sed#include "llvm/Pass.h" 23193323Sed#include "llvm/Support/CommandLine.h" 24193323Sed#include "llvm/Support/FileUtilities.h" 25249423Sdim#include "llvm/Support/Host.h" 26195340Sed#include "llvm/Support/SourceMgr.h" 27193323Sed#include "llvm/Support/raw_ostream.h" 28193323Sed#include <memory> 29193323Sedusing namespace llvm; 30193323Sed 31198090Srdivackynamespace llvm { 32198090Srdivacky Triple TargetTriple; 33198090Srdivacky} 34198090Srdivacky 35193323Sed// Anonymous namespace to define command line options for debugging. 36193323Sed// 37193323Sednamespace { 38193323Sed // Output - The user can specify a file containing the expected output of the 39193323Sed // program. If this filename is set, it is used as the reference diff source, 40193323Sed // otherwise the raw input run through an interpreter is used as the reference 41193323Sed // source. 42193323Sed // 43193323Sed cl::opt<std::string> 44193323Sed OutputFile("output", cl::desc("Specify a reference program output " 45193323Sed "(for miscompilation detection)")); 46193323Sed} 47193323Sed 48193323Sed/// setNewProgram - If we reduce or update the program somehow, call this method 49193323Sed/// to update bugdriver with it. This deletes the old module and sets the 50193323Sed/// specified one as the current program. 51193323Sedvoid BugDriver::setNewProgram(Module *M) { 52193323Sed delete Program; 53193323Sed Program = M; 54193323Sed} 55193323Sed 56193323Sed 57193323Sed/// getPassesString - Turn a list of passes into a string which indicates the 58193323Sed/// command line options that must be passed to add the passes. 59193323Sed/// 60212793Sdimstd::string llvm::getPassesString(const std::vector<std::string> &Passes) { 61193323Sed std::string Result; 62193323Sed for (unsigned i = 0, e = Passes.size(); i != e; ++i) { 63193323Sed if (i) Result += " "; 64193323Sed Result += "-"; 65212793Sdim Result += Passes[i]; 66193323Sed } 67193323Sed return Result; 68193323Sed} 69193323Sed 70212793SdimBugDriver::BugDriver(const char *toolname, bool find_bugs, 71205407Srdivacky unsigned timeout, unsigned memlimit, bool use_valgrind, 72195340Sed LLVMContext& ctxt) 73195340Sed : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile), 74276479Sdim Program(nullptr), Interpreter(nullptr), SafeInterpreter(nullptr), 75296417Sdim cc(nullptr), run_find_bugs(find_bugs), Timeout(timeout), 76205407Srdivacky MemoryLimit(memlimit), UseValgrind(use_valgrind) {} 77193323Sed 78206083SrdivackyBugDriver::~BugDriver() { 79206083Srdivacky delete Program; 80276479Sdim if (Interpreter != SafeInterpreter) 81276479Sdim delete Interpreter; 82276479Sdim delete SafeInterpreter; 83296417Sdim delete cc; 84206083Srdivacky} 85193323Sed 86280031Sdimstd::unique_ptr<Module> llvm::parseInputFile(StringRef Filename, 87280031Sdim LLVMContext &Ctxt) { 88195340Sed SMDiagnostic Err; 89280031Sdim std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt); 90288943Sdim if (!Result) { 91234353Sdim Err.print("bugpoint", errs()); 92288943Sdim return Result; 93288943Sdim } 94198090Srdivacky 95288943Sdim if (verifyModule(*Result, &errs())) { 96288943Sdim errs() << "bugpoint: " << Filename << ": error: input module is broken!\n"; 97288943Sdim return std::unique_ptr<Module>(); 98288943Sdim } 99288943Sdim 100198090Srdivacky // If we don't have an override triple, use the first one to configure 101198090Srdivacky // bugpoint, or use the host triple if none provided. 102288943Sdim if (TargetTriple.getTriple().empty()) { 103288943Sdim Triple TheTriple(Result->getTargetTriple()); 104198090Srdivacky 105288943Sdim if (TheTriple.getTriple().empty()) 106288943Sdim TheTriple.setTriple(sys::getDefaultTargetTriple()); 107221337Sdim 108288943Sdim TargetTriple.setTriple(TheTriple.getTriple()); 109288943Sdim } 110198090Srdivacky 111288943Sdim Result->setTargetTriple(TargetTriple.getTriple()); // override the triple 112193323Sed return Result; 113193323Sed} 114193323Sed 115193323Sed// This method takes the specified list of LLVM input files, attempts to load 116193323Sed// them, either as assembly or bitcode, then link them together. It returns 117193323Sed// true on failure (if, for example, an input bitcode file could not be 118193323Sed// parsed), and false on success. 119193323Sed// 120193323Sedbool BugDriver::addSources(const std::vector<std::string> &Filenames) { 121276479Sdim assert(!Program && "Cannot call addSources multiple times!"); 122193323Sed assert(!Filenames.empty() && "Must specify at least on input filename!"); 123193323Sed 124207618Srdivacky // Load the first input file. 125280031Sdim Program = parseInputFile(Filenames[0], Context).release(); 126276479Sdim if (!Program) return true; 127221337Sdim 128212793Sdim outs() << "Read input file : '" << Filenames[0] << "'\n"; 129193323Sed 130207618Srdivacky for (unsigned i = 1, e = Filenames.size(); i != e; ++i) { 131280031Sdim std::unique_ptr<Module> M = parseInputFile(Filenames[i], Context); 132276479Sdim if (!M.get()) return true; 133193323Sed 134212793Sdim outs() << "Linking in input file: '" << Filenames[i] << "'\n"; 135296417Sdim if (Linker::linkModules(*Program, std::move(M))) 136207618Srdivacky return true; 137193323Sed } 138193323Sed 139212793Sdim outs() << "*** All input ok\n"; 140193323Sed 141193323Sed // All input files read successfully! 142193323Sed return false; 143193323Sed} 144193323Sed 145193323Sed 146193323Sed 147193323Sed/// run - The top level method that is invoked after all of the instance 148193323Sed/// variables are set up from command line arguments. 149193323Sed/// 150207618Srdivackybool BugDriver::run(std::string &ErrMsg) { 151193323Sed if (run_find_bugs) { 152193323Sed // Rearrange the passes and apply them to the program. Repeat this process 153193323Sed // until the user kills the program or we find a bug. 154207618Srdivacky return runManyPasses(PassesToRun, ErrMsg); 155193323Sed } 156193323Sed 157221337Sdim // If we're not running as a child, the first thing that we must do is 158221337Sdim // determine what the problem is. Does the optimization series crash the 159221337Sdim // compiler, or does it produce illegal code? We make the top-level 160239462Sdim // decision by trying to run all of the passes on the input program, 161221337Sdim // which should generate a bitcode file. If it does generate a bitcode 162221337Sdim // file, then we know the compiler didn't crash, so try to diagnose a 163193323Sed // miscompilation. 164193323Sed if (!PassesToRun.empty()) { 165198090Srdivacky outs() << "Running selected passes on program to test for crash: "; 166212793Sdim if (runPasses(Program, PassesToRun)) 167193323Sed return debugOptimizerCrash(); 168193323Sed } 169193323Sed 170193323Sed // Set up the execution environment, selecting a method to run LLVM bitcode. 171193323Sed if (initializeExecutionEnvironment()) return true; 172193323Sed 173193323Sed // Test to see if we have a code generator crash. 174198090Srdivacky outs() << "Running the code generator to test for a crash: "; 175207618Srdivacky std::string Error; 176207618Srdivacky compileProgram(Program, &Error); 177207618Srdivacky if (!Error.empty()) { 178207618Srdivacky outs() << Error; 179207618Srdivacky return debugCodeGeneratorCrash(ErrMsg); 180193323Sed } 181207618Srdivacky outs() << '\n'; 182193323Sed 183193323Sed // Run the raw input to see where we are coming from. If a reference output 184193323Sed // was specified, make sure that the raw output matches it. If not, it's a 185193323Sed // problem in the front-end or the code generator. 186193323Sed // 187193323Sed bool CreatedOutput = false; 188193323Sed if (ReferenceOutputFile.empty()) { 189198090Srdivacky outs() << "Generating reference output from raw program: "; 190207618Srdivacky if (!createReferenceFile(Program)) { 191207618Srdivacky return debugCodeGeneratorCrash(ErrMsg); 192193323Sed } 193193323Sed CreatedOutput = true; 194193323Sed } 195193323Sed 196193323Sed // Make sure the reference output file gets deleted on exit from this 197193323Sed // function, if appropriate. 198261991Sdim std::string ROF(ReferenceOutputFile); 199261991Sdim FileRemover RemoverInstance(ROF, CreatedOutput && !SaveTemps); 200193323Sed 201193323Sed // Diff the output of the raw program against the reference output. If it 202221337Sdim // matches, then we assume there is a miscompilation bug and try to 203193323Sed // diagnose it. 204198090Srdivacky outs() << "*** Checking the code generator...\n"; 205212793Sdim bool Diff = diffProgram(Program, "", "", false, &Error); 206207618Srdivacky if (!Error.empty()) { 207207618Srdivacky errs() << Error; 208207618Srdivacky return debugCodeGeneratorCrash(ErrMsg); 209207618Srdivacky } 210207618Srdivacky if (!Diff) { 211207618Srdivacky outs() << "\n*** Output matches: Debugging miscompilation!\n"; 212207618Srdivacky debugMiscompilation(&Error); 213207618Srdivacky if (!Error.empty()) { 214207618Srdivacky errs() << Error; 215207618Srdivacky return debugCodeGeneratorCrash(ErrMsg); 216193323Sed } 217207618Srdivacky return false; 218193323Sed } 219193323Sed 220198090Srdivacky outs() << "\n*** Input program does not match reference diff!\n"; 221198090Srdivacky outs() << "Debugging code generator problem!\n"; 222207618Srdivacky bool Failure = debugCodeGenerator(&Error); 223207618Srdivacky if (!Error.empty()) { 224207618Srdivacky errs() << Error; 225207618Srdivacky return debugCodeGeneratorCrash(ErrMsg); 226193323Sed } 227207618Srdivacky return Failure; 228193323Sed} 229193323Sed 230193323Sedvoid llvm::PrintFunctionList(const std::vector<Function*> &Funcs) { 231193323Sed unsigned NumPrint = Funcs.size(); 232193323Sed if (NumPrint > 10) NumPrint = 10; 233193323Sed for (unsigned i = 0; i != NumPrint; ++i) 234198090Srdivacky outs() << " " << Funcs[i]->getName(); 235193323Sed if (NumPrint < Funcs.size()) 236198090Srdivacky outs() << "... <" << Funcs.size() << " total>"; 237198090Srdivacky outs().flush(); 238193323Sed} 239193323Sed 240193323Sedvoid llvm::PrintGlobalVariableList(const std::vector<GlobalVariable*> &GVs) { 241193323Sed unsigned NumPrint = GVs.size(); 242193323Sed if (NumPrint > 10) NumPrint = 10; 243193323Sed for (unsigned i = 0; i != NumPrint; ++i) 244198090Srdivacky outs() << " " << GVs[i]->getName(); 245193323Sed if (NumPrint < GVs.size()) 246198090Srdivacky outs() << "... <" << GVs.size() << " total>"; 247198090Srdivacky outs().flush(); 248193323Sed} 249