14a10645c70199c8d8567fbc46312158c419720abChris Lattner//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
23da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman//
37c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//                     The LLVM Compiler Infrastructure
47c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
521c62da287237d39d0d95004881ea4baae3be6daChris Lattner// This file is distributed under the University of Illinois Open Source
621c62da287237d39d0d95004881ea4baae3be6daChris Lattner// License. See LICENSE.TXT for details.
73da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman//
87c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//===----------------------------------------------------------------------===//
94a10645c70199c8d8567fbc46312158c419720abChris Lattner//
104a10645c70199c8d8567fbc46312158c419720abChris Lattner// This file contains code used to execute the program utilizing one of the
118ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif// various ways of running LLVM bitcode.
124a10645c70199c8d8567fbc46312158c419720abChris Lattner//
134a10645c70199c8d8567fbc46312158c419720abChris Lattner//===----------------------------------------------------------------------===//
144a10645c70199c8d8567fbc46312158c419720abChris Lattner
154a10645c70199c8d8567fbc46312158c419720abChris Lattner#include "BugDriver.h"
16f1b20d8620b05abaa52f40ac6d21f839b265fb00Chris Lattner#include "ToolRunner.h"
17551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
18551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h"
19551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/FileUtilities.h"
20551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/SystemUtils.h"
2174382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner#include "llvm/Support/raw_ostream.h"
224a10645c70199c8d8567fbc46312158c419720abChris Lattner#include <fstream>
2351ab5c8862466bbddcd5c4369779c472978ed309Reid Spencer
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekeusing namespace llvm;
25d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
264a10645c70199c8d8567fbc46312158c419720abChris Lattnernamespace {
274a10645c70199c8d8567fbc46312158c419720abChris Lattner  // OutputType - Allow the user to specify the way code should be run, to test
284a10645c70199c8d8567fbc46312158c419720abChris Lattner  // for miscompilation.
294a10645c70199c8d8567fbc46312158c419720abChris Lattner  //
304a10645c70199c8d8567fbc46312158c419720abChris Lattner  enum OutputType {
31a443e5b1f1013612950fc3c9ebfafca60a1c20dfEric Christopher    AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, LLC_Safe, CompileCustom, Custom
324a10645c70199c8d8567fbc46312158c419720abChris Lattner  };
334148556a9c767fdf3047ae8e004a759356b893ddMisha Brukman
34a328c51bb99666ee0c045a57ea6d6ce2b0198f9bChris Lattner  cl::opt<double>
35a328c51bb99666ee0c045a57ea6d6ce2b0198f9bChris Lattner  AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
36a328c51bb99666ee0c045a57ea6d6ce2b0198f9bChris Lattner               cl::init(0.0));
37a328c51bb99666ee0c045a57ea6d6ce2b0198f9bChris Lattner  cl::opt<double>
38a328c51bb99666ee0c045a57ea6d6ce2b0198f9bChris Lattner  RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
39a328c51bb99666ee0c045a57ea6d6ce2b0198f9bChris Lattner               cl::init(0.0));
40a328c51bb99666ee0c045a57ea6d6ce2b0198f9bChris Lattner
414a10645c70199c8d8567fbc46312158c419720abChris Lattner  cl::opt<OutputType>
4270ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"),
43b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke                 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
44b687d82b029ba448b978e2c9ed424ee5d1f9f093Misha Brukman                            clEnumValN(RunLLI, "run-int",
45b687d82b029ba448b978e2c9ed424ee5d1f9f093Misha Brukman                                       "Execute with the interpreter"),
465073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman                            clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
475073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman                            clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
4850010429a7c3285dab15ede68acae7231fade367Chris Lattner                            clEnumValN(RunLLCIA, "run-llc-ia",
4950010429a7c3285dab15ede68acae7231fade367Chris Lattner                                  "Compile with LLC with integrated assembler"),
50cd6f46e2ac4c1d64067237c0b28eccfae22bd9f4Chris Lattner                            clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
51f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick                            clEnumValN(CompileCustom, "compile-custom",
52f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick                            "Use -compile-command to define a command to "
53f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick                            "compile the bitcode. Useful to avoid linking."),
549ef7425a4d44ff129495da2357c98c67162fbeffAnton Korobeynikov                            clEnumValN(Custom, "run-custom",
559ef7425a4d44ff129495da2357c98c67162fbeffAnton Korobeynikov                            "Use -exec-command to define a command to execute "
569ef7425a4d44ff129495da2357c98c67162fbeffAnton Korobeynikov                            "the bitcode. Useful for cross-compilation."),
574d143ee01988e1b52e106ffccbb313937ca5e886Chris Lattner                            clEnumValEnd),
58b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke                 cl::init(AutoPick));
593c053a0a98e19f5ac8905011ad4c8238b08d6aeaChris Lattner
6070ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  cl::opt<OutputType>
6170ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
6249419e28fe400509363cecf63a3a824002c1d9e0Evan Cheng              cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
6349419e28fe400509363cecf63a3a824002c1d9e0Evan Cheng                         clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
6449419e28fe400509363cecf63a3a824002c1d9e0Evan Cheng                         clEnumValN(Custom, "safe-run-custom",
6549419e28fe400509363cecf63a3a824002c1d9e0Evan Cheng                         "Use -exec-command to define a command to execute "
6649419e28fe400509363cecf63a3a824002c1d9e0Evan Cheng                         "the bitcode. Useful for cross-compilation."),
6749419e28fe400509363cecf63a3a824002c1d9e0Evan Cheng                         clEnumValEnd),
6870ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman                     cl::init(AutoPick));
6970ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman
7070ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  cl::opt<std::string>
7170ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  SafeInterpreterPath("safe-path",
7249419e28fe400509363cecf63a3a824002c1d9e0Evan Cheng                   cl::desc("Specify the path to the \"safe\" backend program"),
7349419e28fe400509363cecf63a3a824002c1d9e0Evan Cheng                   cl::init(""));
7470ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman
75c5cad211d6ec50fe90a0a716dee701c6c4721385Brian Gaeke  cl::opt<bool>
765e1452c856a8bcf39ab7a67a5af946c8162f942dReid Spencer  AppendProgramExitCode("append-exit-code",
775e1452c856a8bcf39ab7a67a5af946c8162f942dReid Spencer      cl::desc("Append the exit code to the output so it gets diff'd too"),
785e1452c856a8bcf39ab7a67a5af946c8162f942dReid Spencer      cl::init(false));
795e1452c856a8bcf39ab7a67a5af946c8162f942dReid Spencer
803c053a0a98e19f5ac8905011ad4c8238b08d6aeaChris Lattner  cl::opt<std::string>
813c053a0a98e19f5ac8905011ad4c8238b08d6aeaChris Lattner  InputFile("input", cl::init("/dev/null"),
823c053a0a98e19f5ac8905011ad4c8238b08d6aeaChris Lattner            cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
837dac658792425c10274594782d6fcf10208a16f0Chris Lattner
847dac658792425c10274594782d6fcf10208a16f0Chris Lattner  cl::list<std::string>
857dac658792425c10274594782d6fcf10208a16f0Chris Lattner  AdditionalSOs("additional-so",
867dac658792425c10274594782d6fcf10208a16f0Chris Lattner                cl::desc("Additional shared objects to load "
877dac658792425c10274594782d6fcf10208a16f0Chris Lattner                         "into executing programs"));
887d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner
8951ab5c8862466bbddcd5c4369779c472978ed309Reid Spencer  cl::list<std::string>
90de86cbdc57f30621d3692da7a36613bb623d13b5Andrew Trick  AdditionalLinkerArgs("Xlinker",
9151ab5c8862466bbddcd5c4369779c472978ed309Reid Spencer      cl::desc("Additional arguments to pass to the linker"));
929ef7425a4d44ff129495da2357c98c67162fbeffAnton Korobeynikov
939ef7425a4d44ff129495da2357c98c67162fbeffAnton Korobeynikov  cl::opt<std::string>
94f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick  CustomCompileCommand("compile-command", cl::init("llc"),
95f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick      cl::desc("Command to compile the bitcode (use with -compile-custom) "
96f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick               "(default: llc)"));
97f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick
98f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick  cl::opt<std::string>
999ef7425a4d44ff129495da2357c98c67162fbeffAnton Korobeynikov  CustomExecCommand("exec-command", cl::init("simulate"),
1009ef7425a4d44ff129495da2357c98c67162fbeffAnton Korobeynikov      cl::desc("Command to execute the bitcode (use with -run-custom) "
1019ef7425a4d44ff129495da2357c98c67162fbeffAnton Korobeynikov               "(default: simulate)"));
1024a10645c70199c8d8567fbc46312158c419720abChris Lattner}
1034a10645c70199c8d8567fbc46312158c419720abChris Lattner
104d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
105fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  // Anything specified after the --args option are taken as arguments to the
106fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  // program being debugged.
107fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  cl::list<std::string>
108fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
10960083e2fc0f1165b7511757449f1ee3852b7229cChris Lattner            cl::ZeroOrMore, cl::PositionalEatsArgs);
11068ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar
11168ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar  cl::opt<std::string>
11268ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar  OutputPrefix("output-prefix", cl::init("bugpoint"),
11368ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar            cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
11470ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman}
115636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke
11670ef449741da8b1ef035e04a55958652a0200ba1Dan Gohmannamespace {
117636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke  cl::list<std::string>
118636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke  ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
11960083e2fc0f1165b7511757449f1ee3852b7229cChris Lattner           cl::ZeroOrMore, cl::PositionalEatsArgs);
12070ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman
12170ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  cl::list<std::string>
12270ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  SafeToolArgv("safe-tool-args", cl::Positional,
12370ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman               cl::desc("<safe-tool arguments>..."),
12470ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman               cl::ZeroOrMore, cl::PositionalEatsArgs);
12538efa38c864f6d29892d92102c0232b234b526edBill Wendling
126faa95763ebb081769bf6ac35e170394c9d477813Kalle Raiskila  cl::opt<std::string>
127de86cbdc57f30621d3692da7a36613bb623d13b5Andrew Trick  GCCBinary("gcc", cl::init("gcc"),
128faa95763ebb081769bf6ac35e170394c9d477813Kalle Raiskila              cl::desc("The gcc binary to use. (default 'gcc')"));
129faa95763ebb081769bf6ac35e170394c9d477813Kalle Raiskila
13038efa38c864f6d29892d92102c0232b234b526edBill Wendling  cl::list<std::string>
13138efa38c864f6d29892d92102c0232b234b526edBill Wendling  GCCToolArgv("gcc-tool-args", cl::Positional,
13238efa38c864f6d29892d92102c0232b234b526edBill Wendling              cl::desc("<gcc-tool arguments>..."),
13338efa38c864f6d29892d92102c0232b234b526edBill Wendling              cl::ZeroOrMore, cl::PositionalEatsArgs);
134fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner}
1359d679cbc6cb5c7dc8cca87a1e1548c480fb056b8Misha Brukman
1364a10645c70199c8d8567fbc46312158c419720abChris Lattner//===----------------------------------------------------------------------===//
1374a10645c70199c8d8567fbc46312158c419720abChris Lattner// BugDriver method implementation
1384a10645c70199c8d8567fbc46312158c419720abChris Lattner//
1394a10645c70199c8d8567fbc46312158c419720abChris Lattner
1404a10645c70199c8d8567fbc46312158c419720abChris Lattner/// initializeExecutionEnvironment - This method is used to set up the
1414a10645c70199c8d8567fbc46312158c419720abChris Lattner/// environment for executing LLVM programs.
1424a10645c70199c8d8567fbc46312158c419720abChris Lattner///
1434a10645c70199c8d8567fbc46312158c419720abChris Lattnerbool BugDriver::initializeExecutionEnvironment() {
144ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman  outs() << "Initializing execution environment: ";
1454a10645c70199c8d8567fbc46312158c419720abChris Lattner
1464148556a9c767fdf3047ae8e004a759356b893ddMisha Brukman  // Create an instance of the AbstractInterpreter interface as specified on
1474148556a9c767fdf3047ae8e004a759356b893ddMisha Brukman  // the command line
148dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  SafeInterpreter = nullptr;
1494a10645c70199c8d8567fbc46312158c419720abChris Lattner  std::string Message;
150636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke
151cc876a7421f6dbcca98446058d5f0637092c6e1aChris Lattner  switch (InterpreterSel) {
152b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke  case AutoPick:
153b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke    if (!Interpreter) {
154b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke      InterpreterSel = RunJIT;
155636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke      Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
156636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke                                                   &ToolArgv);
157b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke    }
158b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke    if (!Interpreter) {
159b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke      InterpreterSel = RunLLC;
160636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke      Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
161de86cbdc57f30621d3692da7a36613bb623d13b5Andrew Trick                                                   GCCBinary, &ToolArgv,
162faa95763ebb081769bf6ac35e170394c9d477813Kalle Raiskila                                                   &GCCToolArgv);
163b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke    }
164b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke    if (!Interpreter) {
165b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke      InterpreterSel = RunLLI;
166636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke      Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
167636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke                                                   &ToolArgv);
168b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke    }
169b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke    if (!Interpreter) {
170b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke      InterpreterSel = AutoPick;
171b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke      Message = "Sorry, I can't automatically select an interpreter!\n";
172b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke    }
173b5ee509be2ce78badc11fd649dda90ec78394d1aBrian Gaeke    break;
174769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner  case RunLLI:
175636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke    Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
176636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke                                                 &ToolArgv);
177769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner    break;
178769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner  case RunLLC:
17950010429a7c3285dab15ede68acae7231fade367Chris Lattner  case RunLLCIA:
18070ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  case LLC_Safe:
181636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke    Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
182de86cbdc57f30621d3692da7a36613bb623d13b5Andrew Trick                                                 GCCBinary, &ToolArgv,
183faa95763ebb081769bf6ac35e170394c9d477813Kalle Raiskila                                                 &GCCToolArgv,
18450010429a7c3285dab15ede68acae7231fade367Chris Lattner                                                 InterpreterSel == RunLLCIA);
185769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner    break;
186769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner  case RunJIT:
187636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke    Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
188636df3d7ec51a8c6fd759a98853ff709ae54d64eBrian Gaeke                                                 &ToolArgv);
189769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner    break;
190f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick  case CompileCustom:
191f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick    Interpreter =
192f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick      AbstractInterpreter::createCustomCompiler(Message, CustomCompileCommand);
193f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick    break;
1949ef7425a4d44ff129495da2357c98c67162fbeffAnton Korobeynikov  case Custom:
195f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick    Interpreter =
196f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick      AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
1979ef7425a4d44ff129495da2357c98c67162fbeffAnton Korobeynikov    break;
1984a10645c70199c8d8567fbc46312158c419720abChris Lattner  }
199ad6996d74f60340d6139af8f345d93735661fbbaMatthijs Kooijman  if (!Interpreter)
20065f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman    errs() << Message;
201ad6996d74f60340d6139af8f345d93735661fbbaMatthijs Kooijman  else // Display informational messages on stdout instead of stderr
202ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman    outs() << Message;
2034a10645c70199c8d8567fbc46312158c419720abChris Lattner
20470ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  std::string Path = SafeInterpreterPath;
20570ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  if (Path.empty())
20670ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    Path = getToolName();
20770ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  std::vector<std::string> SafeToolArgs = SafeToolArgv;
20870ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  switch (SafeInterpreterSel) {
20970ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  case AutoPick:
21070ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    // In "llc-safe" mode, default to using LLC as the "safe" backend.
21170ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    if (!SafeInterpreter &&
21270ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman        InterpreterSel == LLC_Safe) {
21370ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman      SafeInterpreterSel = RunLLC;
21470ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman      SafeToolArgs.push_back("--relocation-model=pic");
215197f728d49fa0cc0baa5aadb2b905fbd8c22a81eDan Gohman      SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
216de86cbdc57f30621d3692da7a36613bb623d13b5Andrew Trick                                                       GCCBinary,
21738efa38c864f6d29892d92102c0232b234b526edBill Wendling                                                       &SafeToolArgs,
21838efa38c864f6d29892d92102c0232b234b526edBill Wendling                                                       &GCCToolArgv);
21970ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    }
22070ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman
22170ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    if (!SafeInterpreter &&
22270ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman        InterpreterSel != RunLLC &&
22370ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman        InterpreterSel != RunJIT) {
22470ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman      SafeInterpreterSel = RunLLC;
22570ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman      SafeToolArgs.push_back("--relocation-model=pic");
226197f728d49fa0cc0baa5aadb2b905fbd8c22a81eDan Gohman      SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
227de86cbdc57f30621d3692da7a36613bb623d13b5Andrew Trick                                                       GCCBinary,
22838efa38c864f6d29892d92102c0232b234b526edBill Wendling                                                       &SafeToolArgs,
22938efa38c864f6d29892d92102c0232b234b526edBill Wendling                                                       &GCCToolArgv);
23070ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    }
23170ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    if (!SafeInterpreter) {
23270ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman      SafeInterpreterSel = AutoPick;
233ed7fcf4fd93b16bfa698451862355fdd17239736Saleem Abdulrasool      Message = "Sorry, I can't automatically select a safe interpreter!\n";
23470ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    }
23570ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    break;
23670ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  case RunLLC:
23750010429a7c3285dab15ede68acae7231fade367Chris Lattner  case RunLLCIA:
23870ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    SafeToolArgs.push_back("--relocation-model=pic");
239197f728d49fa0cc0baa5aadb2b905fbd8c22a81eDan Gohman    SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
240faa95763ebb081769bf6ac35e170394c9d477813Kalle Raiskila                                                     GCCBinary, &SafeToolArgs,
24150010429a7c3285dab15ede68acae7231fade367Chris Lattner                                                     &GCCToolArgv,
24250010429a7c3285dab15ede68acae7231fade367Chris Lattner                                                SafeInterpreterSel == RunLLCIA);
24370ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    break;
24470ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  case Custom:
245f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick    SafeInterpreter =
246f73311bb646bf970806ba7caba220e6bffff7b49Andrew Trick      AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
24770ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    break;
24870ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  default:
24970ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    Message = "Sorry, this back-end is not supported by bugpoint as the "
25070ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman              "\"safe\" backend right now!\n";
25170ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    break;
2527bb11547e497d7b8fc87f61c1089eee808e3a1eeChris Lattner  }
253ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman  if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
254de86cbdc57f30621d3692da7a36613bb623d13b5Andrew Trick
255faa95763ebb081769bf6ac35e170394c9d477813Kalle Raiskila  gcc = GCC::create(Message, GCCBinary, &GCCToolArgv);
256ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman  if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
257a259c9be2acc9528ec7feb3cfd51dcde36d87bb3Misha Brukman
2584a10645c70199c8d8567fbc46312158c419720abChris Lattner  // If there was an error creating the selected interpreter, quit with error.
259dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  return Interpreter == nullptr;
2604a10645c70199c8d8567fbc46312158c419720abChris Lattner}
2614a10645c70199c8d8567fbc46312158c419720abChris Lattner
26222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky/// compileProgram - Try to compile the specified module, returning false and
26322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky/// setting Error if an error occurs.  This is used for code generation
26422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky/// crash testing.
265ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner///
266248d1c65f1ce5bc04b892998b2c2061e6a5f8e1cRafael Espindolavoid BugDriver::compileProgram(Module *M, std::string *Error) const {
2678ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  // Emit the program to a bitcode file...
2689a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola  SmallString<128> BitcodeFile;
2699a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola  int BitcodeFD;
270cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code EC = sys::fs::createUniqueFile(
2719a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola      OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile);
2729a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola  if (EC) {
2739a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola    errs() << ToolName << ": Error making unique filename: " << EC.message()
27465f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman           << "\n";
27551c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer    exit(1);
27651c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer  }
2779a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola  if (writeProgramToFile(BitcodeFile.str(), BitcodeFD, M)) {
2789a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola    errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
2799a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola           << "'!\n";
280ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner    exit(1);
281ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner  }
282ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner
28322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  // Remove the temporary bitcode file when we are done.
284c9c08fb3a7fb5e8ea3e1477a88507704c7a70ba1Michael J. Spencer  FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps);
285ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner
286ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner  // Actually compile the program!
28741396302126c24f1d5231e191852ebf2ff37fe23Duncan Sands  Interpreter->compileProgram(BitcodeFile.str(), Error, Timeout, MemoryLimit);
288ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner}
289ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner
2904a10645c70199c8d8567fbc46312158c419720abChris Lattner
2914a10645c70199c8d8567fbc46312158c419720abChris Lattner/// executeProgram - This method runs "Program", capturing the output of the
2924a10645c70199c8d8567fbc46312158c419720abChris Lattner/// program to a file, returning the filename of the file.  A recommended
2934a10645c70199c8d8567fbc46312158c419720abChris Lattner/// filename may be optionally specified.
2944a10645c70199c8d8567fbc46312158c419720abChris Lattner///
29510757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindolastd::string BugDriver::executeProgram(const Module *Program,
29610757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola                                      std::string OutputFile,
2978ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif                                      std::string BitcodeFile,
298769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner                                      const std::string &SharedObj,
29922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                                      AbstractInterpreter *AI,
30013793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola                                      std::string *Error) const {
301dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (!AI) AI = Interpreter;
302769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner  assert(AI && "Interpreter should have been created already!");
3038ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  bool CreatedBitcode = false;
3048ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  if (BitcodeFile.empty()) {
3058ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif    // Emit the program to a bitcode file...
3069a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola    SmallString<128> UniqueFilename;
3079a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola    int UniqueFD;
308cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    std::error_code EC = sys::fs::createUniqueFile(
3099a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola        OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename);
3109a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola    if (EC) {
31165f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman      errs() << ToolName << ": Error making unique filename: "
3129a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola             << EC.message() << "!\n";
31351c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer      exit(1);
31451c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer    }
3159a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola    BitcodeFile = UniqueFilename.str();
3164a10645c70199c8d8567fbc46312158c419720abChris Lattner
3179a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola    if (writeProgramToFile(BitcodeFile, UniqueFD, Program)) {
31865f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman      errs() << ToolName << ": Error emitting bitcode to file '"
31965f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman             << BitcodeFile << "'!\n";
3204a10645c70199c8d8567fbc46312158c419720abChris Lattner      exit(1);
3214a10645c70199c8d8567fbc46312158c419720abChris Lattner    }
3228ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif    CreatedBitcode = true;
3234a10645c70199c8d8567fbc46312158c419720abChris Lattner  }
3244a10645c70199c8d8567fbc46312158c419720abChris Lattner
3258ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  // Remove the temporary bitcode file when we are done.
3269a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola  std::string BitcodePath(BitcodeFile);
3279a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola  FileRemover BitcodeFileRemover(BitcodePath,
328c9c08fb3a7fb5e8ea3e1477a88507704c7a70ba1Michael J. Spencer    CreatedBitcode && !SaveTemps);
3299709272675feaca030cce14d0f5eb7f342b3fc1dChris Lattner
330e528a2c21a63a06c5e3cdf929000d77c59ac0937Hal Finkel  if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
3315073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
3324a10645c70199c8d8567fbc46312158c419720abChris Lattner  // Check to see if this is a valid output filename...
3339a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola  SmallString<128> UniqueFile;
334cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
3359a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola  if (EC) {
33665f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman    errs() << ToolName << ": Error making unique filename: "
3379a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola           << EC.message() << "\n";
33851c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer    exit(1);
33951c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer  }
3409a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola  OutputFile = UniqueFile.str();
3414a10645c70199c8d8567fbc46312158c419720abChris Lattner
342769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner  // Figure out which shared objects to run, if any.
3437dac658792425c10274594782d6fcf10208a16f0Chris Lattner  std::vector<std::string> SharedObjs(AdditionalSOs);
344769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner  if (!SharedObj.empty())
345769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner    SharedObjs.push_back(SharedObj);
346769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner
34722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile,
34822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                                  Error, AdditionalLinkerArgs, SharedObjs,
34970ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman                                  Timeout, MemoryLimit);
35022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  if (!Error->empty())
35122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky    return OutputFile;
3527d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner
3537d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner  if (RetVal == -1) {
35465f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman    errs() << "<timeout>";
3557d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner    static bool FirstTimeout = true;
3567d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner    if (FirstTimeout) {
357ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman      outs() << "\n"
3587d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner "*** Program execution timed out!  This mechanism is designed to handle\n"
3597d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner "    programs stuck in infinite loops gracefully.  The -timeout option\n"
3607d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner "    can be used to change the timeout threshold or disable it completely\n"
3617d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner "    (with -timeout=0).  This message is only displayed once.\n";
3627d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner      FirstTimeout = false;
3637d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner    }
3647d91e49ff7bcc0fd10a54d45a6185bb05adf3d20Chris Lattner  }
365769f1fe6728ffb5627ae0cedc392576d6e701a5aChris Lattner
3665e1452c856a8bcf39ab7a67a5af946c8162f942dReid Spencer  if (AppendProgramExitCode) {
3675e1452c856a8bcf39ab7a67a5af946c8162f942dReid Spencer    std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
3685e1452c856a8bcf39ab7a67a5af946c8162f942dReid Spencer    outFile << "exit " << RetVal << '\n';
3695e1452c856a8bcf39ab7a67a5af946c8162f942dReid Spencer    outFile.close();
3705e1452c856a8bcf39ab7a67a5af946c8162f942dReid Spencer  }
3715e1452c856a8bcf39ab7a67a5af946c8162f942dReid Spencer
3724a10645c70199c8d8567fbc46312158c419720abChris Lattner  // Return the filename we captured the output to.
3734a10645c70199c8d8567fbc46312158c419720abChris Lattner  return OutputFile;
3744a10645c70199c8d8567fbc46312158c419720abChris Lattner}
3754a10645c70199c8d8567fbc46312158c419720abChris Lattner
37670ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman/// executeProgramSafely - Used to create reference output with the "safe"
377c5cad211d6ec50fe90a0a716dee701c6c4721385Brian Gaeke/// backend, if reference output is not provided.
378c5cad211d6ec50fe90a0a716dee701c6c4721385Brian Gaeke///
37910757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindolastd::string BugDriver::executeProgramSafely(const Module *Program,
38010757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola                                            std::string OutputFile,
38113793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola                                            std::string *Error) const {
38210757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola  return executeProgram(Program, OutputFile, "", "", SafeInterpreter, Error);
383c5cad211d6ec50fe90a0a716dee701c6c4721385Brian Gaeke}
3845073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
38522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewyckystd::string BugDriver::compileSharedObject(const std::string &BitcodeFile,
38622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                                           std::string &Error) {
3875073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  assert(Interpreter && "Interpreter should have been created already!");
388f656a1d4991e52725f73308ee829ffce812c96d1Rafael Espindola  std::string OutputFile;
3895073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
39070ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  // Using the known-good backend.
39122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile,
39222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                                                 Error);
39322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  if (!Error.empty())
39422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky    return "";
3955073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
396a0f5b15e1eb8642d92b3141a6b88a5729ea979dcChris Lattner  std::string SharedObjectFile;
397f656a1d4991e52725f73308ee829ffce812c96d1Rafael Espindola  bool Failure = gcc->MakeSharedObject(OutputFile, FT, SharedObjectFile,
39822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                                       AdditionalLinkerArgs, Error);
39922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  if (!Error.empty())
40022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky    return "";
40122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  if (Failure)
402a0f5b15e1eb8642d92b3141a6b88a5729ea979dcChris Lattner    exit(1);
4035073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
4045073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  // Remove the intermediate C file
405f656a1d4991e52725f73308ee829ffce812c96d1Rafael Espindola  sys::fs::remove(OutputFile);
4065073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
40736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  return SharedObjectFile;
4085073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman}
4095073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
4106a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins/// createReferenceFile - calls compileProgram and then records the output
411de86cbdc57f30621d3692da7a36613bb623d13b5Andrew Trick/// into ReferenceOutputFile. Returns true if reference file created, false
4126a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
4136a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins/// this function.
4146a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins///
415c600f3c337f18c62116ac58b701e4f7ae6d2fb1aChris Lattnerbool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
41622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  std::string Error;
41722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  compileProgram(Program, &Error);
41822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  if (!Error.empty())
4196a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins    return false;
42022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky
42110757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola  ReferenceOutputFile = executeProgramSafely(Program, Filename, &Error);
42222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  if (!Error.empty()) {
42322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky    errs() << Error;
42470ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    if (Interpreter != SafeInterpreter) {
42565f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman      errs() << "*** There is a bug running the \"safe\" backend.  Either"
426a443e5b1f1013612950fc3c9ebfafca60a1c20dfEric Christopher             << " debug it (for example with the -run-jit bugpoint option,"
427a443e5b1f1013612950fc3c9ebfafca60a1c20dfEric Christopher             << " if JIT is being used as the \"safe\" backend), or fix the"
42865f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman             << " error some other way.\n";
4296a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins    }
4306a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins    return false;
4316a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  }
43222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
4336a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  return true;
4346a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins}
4355073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
4366a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins/// diffProgram - This method executes the specified module and diffs the
4376a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins/// output against the file specified by ReferenceOutputFile.  If the output
43822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky/// is different, 1 is returned.  If there is a problem with the code
4397c863eb8cc34c8ae97ae90672758eb6637b1125fAndrew Trick/// generator (e.g., llc crashes), this will set ErrMsg.
4404a10645c70199c8d8567fbc46312158c419720abChris Lattner///
44110757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindolabool BugDriver::diffProgram(const Module *Program,
44210757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola                            const std::string &BitcodeFile,
4435073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman                            const std::string &SharedObject,
44422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                            bool RemoveBitcode,
44513793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola                            std::string *ErrMsg) const {
4464a10645c70199c8d8567fbc46312158c419720abChris Lattner  // Execute the program, generating an output file...
4479a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola  std::string Output(
448dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      executeProgram(Program, "", BitcodeFile, SharedObject, nullptr, ErrMsg));
44922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  if (!ErrMsg->empty())
45022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky    return false;
451c5cad211d6ec50fe90a0a716dee701c6c4721385Brian Gaeke
45265f62790d6e3e7f7ceb9cd12a7a51a66d95a3b03Chris Lattner  std::string Error;
4534a10645c70199c8d8567fbc46312158c419720abChris Lattner  bool FilesDifferent = false;
454bbf97ea7d5832922b3c40b67701b5719bfcb772bRafael Espindola  if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile,
4559a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola                                        Output,
456a328c51bb99666ee0c045a57ea6d6ce2b0198f9bChris Lattner                                        AbsTolerance, RelTolerance, &Error)) {
457a328c51bb99666ee0c045a57ea6d6ce2b0198f9bChris Lattner    if (Diff == 2) {
45865f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman      errs() << "While diffing output: " << Error << '\n';
45965f62790d6e3e7f7ceb9cd12a7a51a66d95a3b03Chris Lattner      exit(1);
46065f62790d6e3e7f7ceb9cd12a7a51a66d95a3b03Chris Lattner    }
46165f62790d6e3e7f7ceb9cd12a7a51a66d95a3b03Chris Lattner    FilesDifferent = true;
46265f62790d6e3e7f7ceb9cd12a7a51a66d95a3b03Chris Lattner  }
46380becf194d56524e8c75618ac57a34520f87a684David Goodwin  else {
46480becf194d56524e8c75618ac57a34520f87a684David Goodwin    // Remove the generated output if there are no differences.
4659a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola    sys::fs::remove(Output);
46680becf194d56524e8c75618ac57a34520f87a684David Goodwin  }
4674a10645c70199c8d8567fbc46312158c419720abChris Lattner
4688ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  // Remove the bitcode file if we are supposed to.
4698ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  if (RemoveBitcode)
4709a23039e42ce494feaf4b5efa806494026329b9fRafael Espindola    sys::fs::remove(BitcodeFile);
4714a10645c70199c8d8567fbc46312158c419720abChris Lattner  return FilesDifferent;
4724a10645c70199c8d8567fbc46312158c419720abChris Lattner}
47391eabc13d3a456cc4b387d3d7fdb041d976732c7Misha Brukman
47491eabc13d3a456cc4b387d3d7fdb041d976732c7Misha Brukmanbool BugDriver::isExecutingJIT() {
47591eabc13d3a456cc4b387d3d7fdb041d976732c7Misha Brukman  return InterpreterSel == RunJIT;
47691eabc13d3a456cc4b387d3d7fdb041d976732c7Misha Brukman}
477d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
478