Miscompilation.cpp revision f4789e6d04c1fddb40092a1193c4a5eb67387acc
14a10645c70199c8d8567fbc46312158c419720abChris Lattner//===- Miscompilation.cpp - Debug program miscompilations -----------------===//
27c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
37c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//                     The LLVM Compiler Infrastructure
47c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
57c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell// This file was developed by the LLVM research group and is distributed under
67c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell// the University of Illinois Open Source License. See LICENSE.TXT for details.
77c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
87c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//===----------------------------------------------------------------------===//
94a10645c70199c8d8567fbc46312158c419720abChris Lattner//
10a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner// This file implements optimizer and code generation miscompilation debugging
11a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner// support.
124a10645c70199c8d8567fbc46312158c419720abChris Lattner//
134a10645c70199c8d8567fbc46312158c419720abChris Lattner//===----------------------------------------------------------------------===//
144a10645c70199c8d8567fbc46312158c419720abChris Lattner
154a10645c70199c8d8567fbc46312158c419720abChris Lattner#include "BugDriver.h"
16126840f49e8d49156a342e836d4b2adca46dc3baChris Lattner#include "ListReducer.h"
17a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/Constants.h"
18a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/DerivedTypes.h"
19a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/Instructions.h"
204a10645c70199c8d8567fbc46312158c419720abChris Lattner#include "llvm/Module.h"
21e49603d79d220a795bd50684c8b1f503ee40f97fMisha Brukman#include "llvm/Pass.h"
22a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/Analysis/Verifier.h"
23a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/Support/Mangler.h"
24640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner#include "llvm/Transforms/Utils/Cloning.h"
25640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner#include "llvm/Transforms/Utils/Linker.h"
26a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "Support/CommandLine.h"
273d9cafa003a114bf9974bc80d5b69b0ed1d29290Misha Brukman#include "Support/FileUtilities.h"
28fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattnerusing namespace llvm;
294a10645c70199c8d8567fbc46312158c419720abChris Lattner
30a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnernamespace llvm {
31a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  extern cl::list<std::string> InputArgv;
32a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
33a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
34efdc0b505712d1ca4460def27e51c430f033d58dChris Lattnernamespace {
35fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
36fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner    BugDriver &BD;
37fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  public:
38fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner    ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
39fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner
40fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner    virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
41fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner                              std::vector<const PassInfo*> &Suffix);
42fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  };
43fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner}
44640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
458c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// TestResult - After passes have been split into a test group and a control
468c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// group, see if they still break the program.
478c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
48640f22e66d90439857a97a83896ee68c4f7128c9Chris LattnerReduceMiscompilingPasses::TestResult
4939aebca3a2d1dd389a6d9cdfb51a53f625e244f0Chris LattnerReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
5006943add8b2b764e131979cca064eda9f28826c9Chris Lattner                                 std::vector<const PassInfo*> &Suffix) {
5106943add8b2b764e131979cca064eda9f28826c9Chris Lattner  // First, run the program with just the Suffix passes.  If it is still broken
52640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // with JUST the kept passes, discard the prefix passes.
5306943add8b2b764e131979cca064eda9f28826c9Chris Lattner  std::cout << "Checking to see if '" << getPassesString(Suffix)
54640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner            << "' compile correctly: ";
55640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
56640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::string BytecodeResult;
5706943add8b2b764e131979cca064eda9f28826c9Chris Lattner  if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
589f71e799c3e6e4cc0c71de82bda81f8753e82942Chris Lattner    std::cerr << " Error running this sequence of passes"
59640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner              << " on the input program!\n";
605ef681c19de9c675a265211f8fb0ae49cc3a3a66Chris Lattner    BD.setPassesToRun(Suffix);
615ef681c19de9c675a265211f8fb0ae49cc3a3a66Chris Lattner    BD.EmitProgressBytecode("pass-error",  false);
62025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner    exit(BD.debugOptimizerCrash());
63640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
64640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
65640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Check to see if the finished program matches the reference output...
665073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) {
67123f8fec94d1f22d876382897231868c62f8eabbMisha Brukman    std::cout << " nope.\n";
68123f8fec94d1f22d876382897231868c62f8eabbMisha Brukman    return KeepSuffix;         // Miscompilation detected!
69640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
70123f8fec94d1f22d876382897231868c62f8eabbMisha Brukman  std::cout << " yup.\n";      // No miscompilation!
71640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
72640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  if (Prefix.empty()) return NoFailure;
73640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
7406943add8b2b764e131979cca064eda9f28826c9Chris Lattner  // Next, see if the program is broken if we run the "prefix" passes first,
75bc0e998c497446f5448425b3cbd7f8f19a458764Misha Brukman  // then separately run the "kept" passes.
76640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::cout << "Checking to see if '" << getPassesString(Prefix)
77640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner            << "' compile correctly: ";
78640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
79640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // If it is not broken with the kept passes, it's possible that the prefix
80640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // passes must be run before the kept passes to break it.  If the program
81640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // WORKS after the prefix passes, but then fails if running the prefix AND
82640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // kept passes, we can update our bytecode file to include the result of the
83640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // prefix passes, then discard the prefix passes.
84640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  //
85640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
869f71e799c3e6e4cc0c71de82bda81f8753e82942Chris Lattner    std::cerr << " Error running this sequence of passes"
87640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner              << " on the input program!\n";
889c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner    BD.setPassesToRun(Prefix);
899c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner    BD.EmitProgressBytecode("pass-error",  false);
90025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner    exit(BD.debugOptimizerCrash());
91640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
92640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
93640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // If the prefix maintains the predicate by itself, only keep the prefix!
945073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  if (BD.diffProgram(BytecodeResult)) {
95123f8fec94d1f22d876382897231868c62f8eabbMisha Brukman    std::cout << " nope.\n";
96640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    removeFile(BytecodeResult);
97640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    return KeepPrefix;
98640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
99123f8fec94d1f22d876382897231868c62f8eabbMisha Brukman  std::cout << " yup.\n";      // No miscompilation!
100640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
101640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Ok, so now we know that the prefix passes work, try running the suffix
102640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // passes on the result of the prefix passes.
103640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  //
104efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *PrefixOutput = ParseInputFile(BytecodeResult);
105640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  if (PrefixOutput == 0) {
106640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    std::cerr << BD.getToolName() << ": Error reading bytecode file '"
107640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner              << BytecodeResult << "'!\n";
108640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    exit(1);
109640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
110640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  removeFile(BytecodeResult);  // No longer need the file on disk
111f4789e6d04c1fddb40092a1193c4a5eb67387accChris Lattner
112f4789e6d04c1fddb40092a1193c4a5eb67387accChris Lattner  // Don't check if there are no passes in the suffix.
113f4789e6d04c1fddb40092a1193c4a5eb67387accChris Lattner  if (Suffix.empty())
114f4789e6d04c1fddb40092a1193c4a5eb67387accChris Lattner    return NoFailure;
115f4789e6d04c1fddb40092a1193c4a5eb67387accChris Lattner
11606943add8b2b764e131979cca064eda9f28826c9Chris Lattner  std::cout << "Checking to see if '" << getPassesString(Suffix)
117640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner            << "' passes compile correctly after the '"
118640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner            << getPassesString(Prefix) << "' passes: ";
119640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
120efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *OriginalInput = BD.swapProgramIn(PrefixOutput);
12106943add8b2b764e131979cca064eda9f28826c9Chris Lattner  if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
1229f71e799c3e6e4cc0c71de82bda81f8753e82942Chris Lattner    std::cerr << " Error running this sequence of passes"
123640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner              << " on the input program!\n";
1245ef681c19de9c675a265211f8fb0ae49cc3a3a66Chris Lattner    BD.setPassesToRun(Suffix);
1259c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner    BD.EmitProgressBytecode("pass-error",  false);
126025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner    exit(BD.debugOptimizerCrash());
127640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
128640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
129640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Run the result...
1305073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) {
131123f8fec94d1f22d876382897231868c62f8eabbMisha Brukman    std::cout << " nope.\n";
132640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    delete OriginalInput;     // We pruned down the original input...
13306943add8b2b764e131979cca064eda9f28826c9Chris Lattner    return KeepSuffix;
134640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
135640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
136640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Otherwise, we must not be running the bad pass anymore.
137123f8fec94d1f22d876382897231868c62f8eabbMisha Brukman  std::cout << " yup.\n";      // No miscompilation!
138efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test
139640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  return NoFailure;
140640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner}
141640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
142efdc0b505712d1ca4460def27e51c430f033d58dChris Lattnernamespace {
143fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  class ReduceMiscompilingFunctions : public ListReducer<Function*> {
144fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner    BugDriver &BD;
145b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    bool (*TestFn)(BugDriver &, Module *, Module *);
146fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  public:
147b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ReduceMiscompilingFunctions(BugDriver &bd,
148b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                                bool (*F)(BugDriver &, Module *, Module *))
149b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      : BD(bd), TestFn(F) {}
150fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner
151fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner    virtual TestResult doTest(std::vector<Function*> &Prefix,
152fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner                              std::vector<Function*> &Suffix) {
153be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner      if (!Suffix.empty() && TestFuncs(Suffix))
154fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner        return KeepSuffix;
155be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner      if (!Prefix.empty() && TestFuncs(Prefix))
156fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner        return KeepPrefix;
157fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner      return NoFailure;
158fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner    }
159fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner
160be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    bool TestFuncs(const std::vector<Function*> &Prefix);
161fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  };
162fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner}
163640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
164efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// TestMergedProgram - Given two modules, link them together and run the
165efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// program, checking to see if the program matches the diff.  If the diff
166a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// matches, return false, otherwise return true.  If the DeleteInputs argument
167a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// is set to true then this function deletes both input modules before it
168a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// returns.
1698c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
170a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattnerstatic bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
171a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                              bool DeleteInputs) {
172efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // Link the two portions of the program back to together.
173efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  std::string ErrorMsg;
174a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  if (!DeleteInputs) M1 = CloneModule(M1);
175efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  if (LinkModules(M1, M2, &ErrorMsg)) {
176efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    std::cerr << BD.getToolName() << ": Error linking modules together:"
177efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner              << ErrorMsg << "\n";
178efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    exit(1);
179efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  }
180a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  if (DeleteInputs) delete M2;  // We are done with this module...
181efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
182efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *OldProgram = BD.swapProgramIn(M1);
183efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
184efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // Execute the program.  If it does not match the expected output, we must
185efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // return true.
186efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  bool Broken = BD.diffProgram();
187efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
188efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // Delete the linked module & restore the original
189a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  BD.swapProgramIn(OldProgram);
1905313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner  delete M1;
191efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  return Broken;
192efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner}
193efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
1948c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// TestFuncs - split functions in a Module into two groups: those that are
1958c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// under consideration for miscompilation vs. those that are not, and test
1968c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// accordingly. Each group of functions becomes a separate Module.
1978c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
198be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattnerbool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
199640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Test to see if the function is misoptimized if we ONLY run it on the
200640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // functions listed in Funcs.
201be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "Checking to see if the program is misoptimized when "
202be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner            << (Funcs.size()==1 ? "this function is" : "these functions are")
203be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner            << " run through the pass"
204a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner            << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
205efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  PrintFunctionList(Funcs);
206be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "\n";
207640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
208be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // Split the module into the two halves of the program we want.
209efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToNotOptimize = CloneModule(BD.getProgram());
210efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs);
211640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
212b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Run the predicate, not that the predicate will delete both input modules.
213b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return TestFn(BD, ToOptimize, ToNotOptimize);
214640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner}
215640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
2168c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// DisambiguateGlobalSymbols - Mangle symbols to guarantee uniqueness by
2178c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// modifying predominantly internal symbols rather than external ones.
2188c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
21936ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattnerstatic void DisambiguateGlobalSymbols(Module *M) {
22036ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // Try not to cause collisions by minimizing chances of renaming an
22136ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // already-external symbol, so take in external globals and functions as-is.
22236ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // The code should work correctly without disambiguation (assuming the same
22336ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // mangler is used by the two code generators), but having symbols with the
22436ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // same name causes warnings to be emitted by the code generator.
22536ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  Mangler Mang(*M);
22636ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
22736ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    I->setName(Mang.getValueName(I));
22836ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  for (Module::iterator  I = M->begin(),  E = M->end();  I != E; ++I)
22936ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    I->setName(Mang.getValueName(I));
23036ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner}
23136ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner
232a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// ExtractLoops - Given a reduced list of functions that still exposed the bug,
233a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// check to see if we can extract the loops in the region without obscuring the
234a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// bug.  If so, it reduces the amount of code identified.
2358c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
236b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic bool ExtractLoops(BugDriver &BD,
237b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                         bool (*TestFn)(BugDriver &, Module *, Module *),
238a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                         std::vector<Function*> &MiscompiledFunctions) {
239a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  bool MadeChange = false;
240a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  while (1) {
241a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToNotOptimize = CloneModule(BD.getProgram());
242a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
243a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                                                   MiscompiledFunctions);
244a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
245a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (!ToOptimizeLoopExtracted) {
246a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // If the loop extractor crashed or if there were no extractible loops,
247a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // then this chapter of our odyssey is over with.
248a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToNotOptimize;
249a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToOptimize;
250a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
251a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
252a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
253a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cerr << "Extracted a loop from the breaking portion of the program.\n";
254a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    delete ToOptimize;
255a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
256a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Bugpoint is intentionally not very trusting of LLVM transformations.  In
257a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // particular, we're not going to assume that the loop extractor works, so
258a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // we're going to test the newly loop extracted program to make sure nothing
259a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // has broken.  If something broke, then we'll inform the user and stop
260a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extraction.
261a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    AbstractInterpreter *AI = BD.switchToCBE();
262a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) {
263a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BD.switchToInterpreter(AI);
264a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
265a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // Merged program doesn't work anymore!
266a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << "  *** ERROR: Loop extraction broke the program. :("
267a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                << " Please report a bug!\n";
268a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << "      Continuing on with un-loop-extracted version.\n";
269a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToNotOptimize;
270a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToOptimizeLoopExtracted;
271a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
272a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
273a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    BD.switchToInterpreter(AI);
274a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
275b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cout << "  Testing after loop extraction:\n";
276b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    // Clone modules, the tester function will free them.
277b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
278b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    Module *TNOBackup  = CloneModule(ToNotOptimize);
279b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) {
280b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      std::cout << "*** Loop extraction masked the problem.  Undoing.\n";
281a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // If the program is not still broken, then loop extraction did something
282a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // that masked the error.  Stop loop extraction now.
283b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      delete TOLEBackup;
284b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      delete TNOBackup;
285a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
286a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
287b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ToOptimizeLoopExtracted = TOLEBackup;
288b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ToNotOptimize = TNOBackup;
289b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
290b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cout << "*** Loop extraction successful!\n";
291a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
292a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Okay, great!  Now we know that we extracted a loop and that loop
293a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extraction both didn't break the program, and didn't mask the problem.
294a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Replace the current program with the loop extracted version, and try to
295a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extract another loop.
296a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::string ErrorMsg;
297a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)) {
298a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << BD.getToolName() << ": Error linking modules together:"
299a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                << ErrorMsg << "\n";
300a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      exit(1);
301a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
302d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner
303d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner    // All of the Function*'s in the MiscompiledFunctions list are in the old
3045313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    // module.  Update this list to include all of the functions in the
3055313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    // optimized and loop extracted module.
3065313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    MiscompiledFunctions.clear();
3075313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
3085313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner           E = ToOptimizeLoopExtracted->end(); I != E; ++I) {
3095313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner      if (!I->isExternal()) {
31002bb481881fe6aaa876f9bf79f38f40f56a35a01Chris Lattner        Function *NewF = ToNotOptimize->getFunction(I->getName(),
31102bb481881fe6aaa876f9bf79f38f40f56a35a01Chris Lattner                                                    I->getFunctionType());
3125313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner        assert(NewF && "Function not found??");
3135313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner        MiscompiledFunctions.push_back(NewF);
3145313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner      }
315d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner    }
3165313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    delete ToOptimizeLoopExtracted;
317d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner
318a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    BD.setNewProgram(ToNotOptimize);
319a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    MadeChange = true;
320a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  }
321a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner}
322a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
323b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// DebugAMiscompilation - This is a generic driver to narrow down
324b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// miscompilations, either in an optimization or a code generator.
3258c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
326b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic std::vector<Function*>
327b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris LattnerDebugAMiscompilation(BugDriver &BD,
328b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                     bool (*TestFn)(BugDriver &, Module *, Module *)) {
329640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Okay, now that we have reduced the list of passes which are causing the
330640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // failure, see if we can pin down which functions are being
331640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // miscompiled... first build a list of all of the non-external functions in
332640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // the program.
333640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::vector<Function*> MiscompiledFunctions;
334b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  Module *Prog = BD.getProgram();
335b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
336640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    if (!I->isExternal())
337640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner      MiscompiledFunctions.push_back(I);
3384a10645c70199c8d8567fbc46312158c419720abChris Lattner
339640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Do the reduction...
340b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
341640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
342de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner  std::cout << "\n*** The following function"
343de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner            << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
344de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner            << " being miscompiled: ";
345640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  PrintFunctionList(MiscompiledFunctions);
346640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::cout << "\n";
3474a10645c70199c8d8567fbc46312158c419720abChris Lattner
348a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  // See if we can rip any loops out of the miscompiled functions and still
349a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  // trigger the problem.
350b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  if (ExtractLoops(BD, TestFn, MiscompiledFunctions)) {
351a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Okay, we extracted some loops and the problem still appears.  See if we
352a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // can eliminate some of the created functions from being candidates.
353a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
35436ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    // Loop extraction can introduce functions with the same name (foo_code).
35536ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    // Make sure to disambiguate the symbols so that when the program is split
35636ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    // apart that we can link it back together again.
35736ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    DisambiguateGlobalSymbols(BD.getProgram());
35836ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner
359a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Do the reduction...
360b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
361a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
362a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cout << "\n*** The following function"
363a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner              << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
364a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner              << " being miscompiled: ";
365a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    PrintFunctionList(MiscompiledFunctions);
366a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cout << "\n";
367a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  }
368a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
369b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return MiscompiledFunctions;
370b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner}
371b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
372a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// TestOptimizer - This is the predicate function used to check to see if the
373a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// "Test" portion of the program is misoptimized.  If so, return true.  In any
374a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// case, both module arguments are deleted.
3758c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
376b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
377b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Run the optimization passes on ToOptimize, producing a transformed version
378b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // of the functions being tested.
379b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "  Optimizing functions being tested: ";
380b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
381b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                                     /*AutoDebugCrashes*/true);
382b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "done.\n";
383b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  delete Test;
384b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
385b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "  Checking to see if the merged program executes correctly: ";
3862423db0e8577e769ac5ad4e567808e43daf37945Chris Lattner  bool Broken = TestMergedProgram(BD, Optimized, Safe, true);
387b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << (Broken ? " nope.\n" : " yup.\n");
388b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return Broken;
389b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner}
390b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
391b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
392b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// debugMiscompilation - This method is used when the passes selected are not
393b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// crashing, but the generated output is semantically different from the
394b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// input.
395b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner///
396b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerbool BugDriver::debugMiscompilation() {
397b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Make sure something was miscompiled...
398b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
399b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cerr << "*** Optimized program matches reference output!  No problem "
400b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner	      << "detected...\nbugpoint can't help you with your problem!\n";
401b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    return false;
402b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  }
403b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
404b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "\n*** Found miscompiling pass"
405b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner            << (getPassesToRun().size() == 1 ? "" : "es") << ": "
406b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner            << getPassesString(getPassesToRun()) << "\n";
407b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  EmitProgressBytecode("passinput");
408b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
409b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::vector<Function*> MiscompiledFunctions =
410b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    DebugAMiscompilation(*this, TestOptimizer);
411b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
412640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Output a bunch of bytecode files for the user...
413be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "Outputting reduced bytecode files which expose the problem:\n";
414efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToNotOptimize = CloneModule(getProgram());
415efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
416efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner                                                 MiscompiledFunctions);
417be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
418be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "  Non-optimized portion: ";
419b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ToNotOptimize = swapProgramIn(ToNotOptimize);
420be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  EmitProgressBytecode("tonotoptimize", true);
421be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  setNewProgram(ToNotOptimize);   // Delete hacked module.
422be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
423be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "  Portion that is input to optimizer: ";
424b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ToOptimize = swapProgramIn(ToOptimize);
425be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  EmitProgressBytecode("tooptimize");
426be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  setNewProgram(ToOptimize);      // Delete hacked module.
4274a10645c70199c8d8567fbc46312158c419720abChris Lattner
4284a10645c70199c8d8567fbc46312158c419720abChris Lattner  return false;
4294a10645c70199c8d8567fbc46312158c419720abChris Lattner}
430d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
431a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// CleanupAndPrepareModules - Get the specified modules ready for code
432a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// generator testing.
4338c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
434a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerstatic void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
435a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                     Module *Safe) {
436a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Clean up the modules, removing extra cruft that we don't need anymore...
437a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Test = BD.performFinalCleanups(Test);
438a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
439a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // If we are executing the JIT, we have several nasty issues to take care of.
440a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (!BD.isExecutingJIT()) return;
441a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
442a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // First, if the main function is in the Safe module, we must add a stub to
443a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // the Test module to call into it.  Thus, we create a new function `main'
444a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // which just calls the old one.
445a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (Function *oldMain = Safe->getNamedFunction("main"))
446a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    if (!oldMain->isExternal()) {
447a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Rename it
448a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      oldMain->setName("llvm_bugpoint_old_main");
449a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Create a NEW `main' function with same type in the test module.
450a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      Function *newMain = new Function(oldMain->getFunctionType(),
451a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                       GlobalValue::ExternalLinkage,
452a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                       "main", Test);
453a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Create an `oldmain' prototype in the test module, which will
454a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // corresponds to the real main function in the same module.
455a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      Function *oldMainProto = new Function(oldMain->getFunctionType(),
456a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                            GlobalValue::ExternalLinkage,
457a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                            oldMain->getName(), Test);
458a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Set up and remember the argument list for the main function.
459a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      std::vector<Value*> args;
460a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      for (Function::aiterator I = newMain->abegin(), E = newMain->aend(),
461a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner             OI = oldMain->abegin(); I != E; ++I, ++OI) {
462a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        I->setName(OI->getName());    // Copy argument names from oldMain
463a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        args.push_back(I);
464a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      }
465a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
466a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Call the old main function and return its result
467a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BasicBlock *BB = new BasicBlock("entry", newMain);
468a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      CallInst *call = new CallInst(oldMainProto, args);
469a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BB->getInstList().push_back(call);
470a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
471a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // If the type of old function wasn't void, return value of call
472a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      new ReturnInst(oldMain->getReturnType() != Type::VoidTy ? call : 0, BB);
473a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    }
474a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
475a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // The second nasty issue we must deal with in the JIT is that the Safe
476a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // module cannot directly reference any functions defined in the test
477a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // module.  Instead, we use a JIT API call to dynamically resolve the
478a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // symbol.
479a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
480a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Add the resolver to the Safe module.
481a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Prototype: void *getPointerToNamedFunction(const char* Name)
482a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Function *resolverFunc =
483a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    Safe->getOrInsertFunction("getPointerToNamedFunction",
484a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                              PointerType::get(Type::SByteTy),
485a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                              PointerType::get(Type::SByteTy), 0);
486a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
487a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Use the function we just added to get addresses of functions we need.
488dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman  for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
489a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    if (F->isExternal() && !F->use_empty() && &*F != resolverFunc &&
490a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        F->getIntrinsicID() == 0 /* ignore intrinsics */) {
491dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman      Function *TestFn = Test->getFunction(F->getName(), F->getFunctionType());
492a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
493a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Don't forward functions which are external in the test module too.
494a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      if (TestFn && !TestFn->isExternal()) {
495a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // 1. Add a string constant with its name to the global file
496a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        Constant *InitArray = ConstantArray::get(F->getName());
497a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        GlobalVariable *funcName =
498a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner          new GlobalVariable(InitArray->getType(), true /*isConstant*/,
499a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                             GlobalValue::InternalLinkage, InitArray,
500a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                             F->getName() + "_name", Safe);
501a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
502a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
503a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // sbyte* so it matches the signature of the resolver function.
504a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
505a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // GetElementPtr *funcName, ulong 0, ulong 0
506a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::IntTy));
507a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        Value *GEP =
508a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner          ConstantExpr::getGetElementPtr(ConstantPointerRef::get(funcName),
509a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                         GEPargs);
510a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        std::vector<Value*> ResolverArgs;
511a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        ResolverArgs.push_back(GEP);
512a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
513de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman        // Rewrite uses of F in global initializers, etc. to uses of a wrapper
514de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman        // function that dynamically resolves the calls to F via our JIT API
515de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman        if (F->use_begin() != F->use_end()) {
516de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // Construct a new stub function that will re-route calls to F
517dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          const FunctionType *FuncTy = F->getFunctionType();
518de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          Function *FuncWrapper = new Function(FuncTy,
519de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman                                               GlobalValue::InternalLinkage,
520dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman                                               F->getName() + "_wrapper",
521dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman                                               F->getParent());
522dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          BasicBlock *Header = new BasicBlock("header", FuncWrapper);
523dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman
524de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // Resolve the call to function F via the JIT API:
525de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          //
526de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // call resolver(GetElementPtr...)
527de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          CallInst *resolve = new CallInst(resolverFunc, ResolverArgs,
528de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman                                           "resolver");
529de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          Header->getInstList().push_back(resolve);
530de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // cast the result from the resolver to correctly-typed function
531de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          CastInst *castResolver =
532de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman            new CastInst(resolve, PointerType::get(F->getFunctionType()),
533de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman                         "resolverCast");
534de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          Header->getInstList().push_back(castResolver);
535de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman
536dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          // Save the argument list
537dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          std::vector<Value*> Args;
538dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          for (Function::aiterator i = FuncWrapper->abegin(),
539dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman                 e = FuncWrapper->aend(); i != e; ++i)
540dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Args.push_back(i);
541dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman
542dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          // Pass on the arguments to the real function, return its result
543dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          if (F->getReturnType() == Type::VoidTy) {
544de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman            CallInst *Call = new CallInst(castResolver, Args);
545dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Call);
546dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            ReturnInst *Ret = new ReturnInst();
547dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Ret);
548dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          } else {
549de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman            CallInst *Call = new CallInst(castResolver, Args, "redir");
550dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Call);
551dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            ReturnInst *Ret = new ReturnInst(Call);
552dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Ret);
553dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          }
554dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman
555de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // Use the wrapper function instead of the old function
556de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          F->replaceAllUsesWith(FuncWrapper);
557dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman        }
558a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      }
559a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    }
560a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
561a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
562a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (verifyModule(*Test) || verifyModule(*Safe)) {
563a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Bugpoint has a bug, which corrupted a module!!\n";
564a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    abort();
565a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
566a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
567a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
568a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
569a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
570a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// TestCodeGenerator - This is the predicate function used to check to see if
571a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// the "Test" portion of the program is miscompiled by the code generator under
572a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// test.  If so, return true.  In any case, both module arguments are deleted.
5738c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
574a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerstatic bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) {
575a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  CleanupAndPrepareModules(BD, Test, Safe);
576a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
577a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
578a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (BD.writeProgramToFile(TestModuleBC, Test)) {
579a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
580a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
581a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
582a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete Test;
583a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
584a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Make the shared library
585a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
586a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
587a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (BD.writeProgramToFile(SafeModuleBC, Safe)) {
588a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
589a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
590a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
591a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SharedObject = BD.compileSharedObject(SafeModuleBC);
592a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete Safe;
593a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
594a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Run the code generator on the `Test' code, loading the shared library.
595a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // The function returns whether or not the new output differs from reference.
596a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
597a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
598a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (Result)
599a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << ": still failing!\n";
600a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  else
601a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << ": didn't fail.\n";
602a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(TestModuleBC);
603a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(SafeModuleBC);
604a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(SharedObject);
605a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
606a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  return Result;
607a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
608a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
609a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
6108c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
6118c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
612a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerbool BugDriver::debugCodeGenerator() {
613a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if ((void*)cbe == (void*)Interpreter) {
614a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::string Result = executeProgramWithCBE("bugpoint.cbe.out");
615a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "\n*** The C backend cannot match the reference diff, but it "
616a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << "is used as the 'known good'\n    code generator, so I can't"
617a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << " debug it.  Perhaps you have a front-end problem?\n    As a"
618a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << " sanity check, I left the result of executing the program "
619a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << "with the C backend\n    in this file for you: '"
620a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << Result << "'.\n";
621a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    return true;
622a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
623a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
624a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  DisambiguateGlobalSymbols(Program);
625a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
626a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator);
627a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
628a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Split the module into the two halves of the program we want.
629a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Module *ToNotCodeGen = CloneModule(getProgram());
630a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs);
631a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
632a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Condition the modules
633a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
634a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
635a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
636a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (writeProgramToFile(TestModuleBC, ToCodeGen)) {
637a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
638a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
639a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
640a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete ToCodeGen;
641a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
642a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Make the shared library
643a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
644a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (writeProgramToFile(SafeModuleBC, ToNotCodeGen)) {
645a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
646a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
647a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
648a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SharedObject = compileSharedObject(SafeModuleBC);
649a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete ToNotCodeGen;
650a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
651a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::cout << "You can reproduce the problem with the command line: \n";
652a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (isExecutingJIT()) {
653a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  lli -load " << SharedObject << " " << TestModuleBC;
654a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  } else {
655a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
656a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  gcc " << SharedObject << " " << TestModuleBC
657a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << ".s -o " << TestModuleBC << ".exe -Wl,-R.\n";
658a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  " << TestModuleBC << ".exe";
659a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
660a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
661a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << " " << InputArgv[i];
662a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::cout << "\n";
663a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::cout << "The shared object was created with:\n  llc -march=c "
664a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << SafeModuleBC << " -o temporary.c\n"
665a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << "  gcc -xc temporary.c -O2 -o " << SharedObject
666a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
667a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -G"            // Compile a shared library, `-G' for Sparc
668a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#else
669a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -shared"       // `-shared' for Linux/X86, maybe others
670a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#endif
671a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -fno-strict-aliasing\n";
672a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
673a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  return false;
674a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
675