1 //===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===// 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 class contains all of the shared state and information that is used by 11 // the BugPoint tool to track down errors in optimizations. This class is the 12 // main driver class that invokes all sub-functionality. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "BugDriver.h" 17 #include "ToolRunner.h" 18 #include "llvm/IR/Module.h" 19 #include "llvm/IR/Verifier.h" 20 #include "llvm/IRReader/IRReader.h" 21 #include "llvm/Linker/Linker.h" 22 #include "llvm/Pass.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/FileUtilities.h" 25 #include "llvm/Support/Host.h" 26 #include "llvm/Support/SourceMgr.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include <memory> 29 using namespace llvm; 30 31 namespace llvm { 32 Triple TargetTriple; 33 } 34 35 DiscardTemp::~DiscardTemp() { 36 if (SaveTemps) { 37 if (Error E = File.keep()) 38 errs() << "Failed to keep temp file " << toString(std::move(E)) << '\n'; 39 return; 40 } 41 if (Error E = File.discard()) 42 errs() << "Failed to delete temp file " << toString(std::move(E)) << '\n'; 43 } 44 45 // Anonymous namespace to define command line options for debugging. 46 // 47 namespace { 48 // Output - The user can specify a file containing the expected output of the 49 // program. If this filename is set, it is used as the reference diff source, 50 // otherwise the raw input run through an interpreter is used as the reference 51 // source. 52 // 53 cl::opt<std::string> OutputFile("output", 54 cl::desc("Specify a reference program output " 55 "(for miscompilation detection)")); 56 } 57 58 /// setNewProgram - If we reduce or update the program somehow, call this method 59 /// to update bugdriver with it. This deletes the old module and sets the 60 /// specified one as the current program. 61 void BugDriver::setNewProgram(Module *M) { 62 delete Program; 63 Program = M; 64 } 65 66 /// getPassesString - Turn a list of passes into a string which indicates the 67 /// command line options that must be passed to add the passes. 68 /// 69 std::string llvm::getPassesString(const std::vector<std::string> &Passes) { 70 std::string Result; 71 for (unsigned i = 0, e = Passes.size(); i != e; ++i) { 72 if (i) 73 Result += " "; 74 Result += "-"; 75 Result += Passes[i]; 76 } 77 return Result; 78 } 79 80 BugDriver::BugDriver(const char *toolname, bool find_bugs, unsigned timeout, 81 unsigned memlimit, bool use_valgrind, LLVMContext &ctxt) 82 : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile), 83 Program(nullptr), Interpreter(nullptr), SafeInterpreter(nullptr), 84 cc(nullptr), run_find_bugs(find_bugs), Timeout(timeout), 85 MemoryLimit(memlimit), UseValgrind(use_valgrind) {} 86 87 BugDriver::~BugDriver() { 88 delete Program; 89 if (Interpreter != SafeInterpreter) 90 delete Interpreter; 91 delete SafeInterpreter; 92 delete cc; 93 } 94 95 std::unique_ptr<Module> llvm::parseInputFile(StringRef Filename, 96 LLVMContext &Ctxt) { 97 SMDiagnostic Err; 98 std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt); 99 if (!Result) { 100 Err.print("bugpoint", errs()); 101 return Result; 102 } 103 104 if (verifyModule(*Result, &errs())) { 105 errs() << "bugpoint: " << Filename << ": error: input module is broken!\n"; 106 return std::unique_ptr<Module>(); 107 } 108 109 // If we don't have an override triple, use the first one to configure 110 // bugpoint, or use the host triple if none provided. 111 if (TargetTriple.getTriple().empty()) { 112 Triple TheTriple(Result->getTargetTriple()); 113 114 if (TheTriple.getTriple().empty()) 115 TheTriple.setTriple(sys::getDefaultTargetTriple()); 116 117 TargetTriple.setTriple(TheTriple.getTriple()); 118 } 119 120 Result->setTargetTriple(TargetTriple.getTriple()); // override the triple 121 return Result; 122 } 123 124 // This method takes the specified list of LLVM input files, attempts to load 125 // them, either as assembly or bitcode, then link them together. It returns 126 // true on failure (if, for example, an input bitcode file could not be 127 // parsed), and false on success. 128 // 129 bool BugDriver::addSources(const std::vector<std::string> &Filenames) { 130 assert(!Program && "Cannot call addSources multiple times!"); 131 assert(!Filenames.empty() && "Must specify at least on input filename!"); 132 133 // Load the first input file. 134 Program = parseInputFile(Filenames[0], Context).release(); 135 if (!Program) 136 return true; 137 138 outs() << "Read input file : '" << Filenames[0] << "'\n"; 139 140 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) { 141 std::unique_ptr<Module> M = parseInputFile(Filenames[i], Context); 142 if (!M.get()) 143 return true; 144 145 outs() << "Linking in input file: '" << Filenames[i] << "'\n"; 146 if (Linker::linkModules(*Program, std::move(M))) 147 return true; 148 } 149 150 outs() << "*** All input ok\n"; 151 152 // All input files read successfully! 153 return false; 154 } 155 156 /// run - The top level method that is invoked after all of the instance 157 /// variables are set up from command line arguments. 158 /// 159 Error BugDriver::run() { 160 if (run_find_bugs) { 161 // Rearrange the passes and apply them to the program. Repeat this process 162 // until the user kills the program or we find a bug. 163 return runManyPasses(PassesToRun); 164 } 165 166 // If we're not running as a child, the first thing that we must do is 167 // determine what the problem is. Does the optimization series crash the 168 // compiler, or does it produce illegal code? We make the top-level 169 // decision by trying to run all of the passes on the input program, 170 // which should generate a bitcode file. If it does generate a bitcode 171 // file, then we know the compiler didn't crash, so try to diagnose a 172 // miscompilation. 173 if (!PassesToRun.empty()) { 174 outs() << "Running selected passes on program to test for crash: "; 175 if (runPasses(Program, PassesToRun)) 176 return debugOptimizerCrash(); 177 } 178 179 // Set up the execution environment, selecting a method to run LLVM bitcode. 180 if (Error E = initializeExecutionEnvironment()) 181 return E; 182 183 // Test to see if we have a code generator crash. 184 outs() << "Running the code generator to test for a crash: "; 185 if (Error E = compileProgram(Program)) { 186 outs() << toString(std::move(E)); 187 return debugCodeGeneratorCrash(); 188 } 189 outs() << '\n'; 190 191 // Run the raw input to see where we are coming from. If a reference output 192 // was specified, make sure that the raw output matches it. If not, it's a 193 // problem in the front-end or the code generator. 194 // 195 bool CreatedOutput = false; 196 if (ReferenceOutputFile.empty()) { 197 outs() << "Generating reference output from raw program: "; 198 if (Error E = createReferenceFile(Program)) { 199 errs() << toString(std::move(E)); 200 return debugCodeGeneratorCrash(); 201 } 202 CreatedOutput = true; 203 } 204 205 // Make sure the reference output file gets deleted on exit from this 206 // function, if appropriate. 207 std::string ROF(ReferenceOutputFile); 208 FileRemover RemoverInstance(ROF, CreatedOutput && !SaveTemps); 209 210 // Diff the output of the raw program against the reference output. If it 211 // matches, then we assume there is a miscompilation bug and try to 212 // diagnose it. 213 outs() << "*** Checking the code generator...\n"; 214 Expected<bool> Diff = diffProgram(Program, "", "", false); 215 if (Error E = Diff.takeError()) { 216 errs() << toString(std::move(E)); 217 return debugCodeGeneratorCrash(); 218 } 219 if (!*Diff) { 220 outs() << "\n*** Output matches: Debugging miscompilation!\n"; 221 if (Error E = debugMiscompilation()) { 222 errs() << toString(std::move(E)); 223 return debugCodeGeneratorCrash(); 224 } 225 return Error::success(); 226 } 227 228 outs() << "\n*** Input program does not match reference diff!\n"; 229 outs() << "Debugging code generator problem!\n"; 230 if (Error E = debugCodeGenerator()) { 231 errs() << toString(std::move(E)); 232 return debugCodeGeneratorCrash(); 233 } 234 return Error::success(); 235 } 236 237 void llvm::PrintFunctionList(const std::vector<Function *> &Funcs) { 238 unsigned NumPrint = Funcs.size(); 239 if (NumPrint > 10) 240 NumPrint = 10; 241 for (unsigned i = 0; i != NumPrint; ++i) 242 outs() << " " << Funcs[i]->getName(); 243 if (NumPrint < Funcs.size()) 244 outs() << "... <" << Funcs.size() << " total>"; 245 outs().flush(); 246 } 247 248 void llvm::PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs) { 249 unsigned NumPrint = GVs.size(); 250 if (NumPrint > 10) 251 NumPrint = 10; 252 for (unsigned i = 0; i != NumPrint; ++i) 253 outs() << " " << GVs[i]->getName(); 254 if (NumPrint < GVs.size()) 255 outs() << "... <" << GVs.size() << " total>"; 256 outs().flush(); 257 } 258