Miscompilation.cpp revision 90c18c5c69d9c451e5fdca1e4b4b95e8ed13291a
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"
20605b9e2c5bd1b0c151a0b15d01e6df3aba93d52fReid Spencer#include "llvm/Linker.h"
214a10645c70199c8d8567fbc46312158c419720abChris Lattner#include "llvm/Module.h"
22e49603d79d220a795bd50684c8b1f503ee40f97fMisha Brukman#include "llvm/Pass.h"
23a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/Analysis/Verifier.h"
24a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/Support/Mangler.h"
25640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner#include "llvm/Transforms/Utils/Cloning.h"
26551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/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;
17490c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  if (!DeleteInputs) {
17590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    M1 = CloneModule(M1);
17690c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    M2 = CloneModule(M2);
17790c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  }
178efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  if (LinkModules(M1, M2, &ErrorMsg)) {
179efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    std::cerr << BD.getToolName() << ": Error linking modules together:"
180eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman              << ErrorMsg << '\n';
181efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    exit(1);
182efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  }
18390c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  delete M2;   // We are done with this module.
184efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
185efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *OldProgram = BD.swapProgramIn(M1);
186efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
187efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // Execute the program.  If it does not match the expected output, we must
188efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // return true.
189efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  bool Broken = BD.diffProgram();
190efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
191efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // Delete the linked module & restore the original
192a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  BD.swapProgramIn(OldProgram);
1935313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner  delete M1;
194efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  return Broken;
195efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner}
196efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
1978c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// TestFuncs - split functions in a Module into two groups: those that are
1988c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// under consideration for miscompilation vs. those that are not, and test
1998c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// accordingly. Each group of functions becomes a separate Module.
2008c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
201be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattnerbool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
202640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Test to see if the function is misoptimized if we ONLY run it on the
203640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // functions listed in Funcs.
204be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "Checking to see if the program is misoptimized when "
205be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner            << (Funcs.size()==1 ? "this function is" : "these functions are")
206be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner            << " run through the pass"
207a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner            << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
208efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  PrintFunctionList(Funcs);
209eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman  std::cout << '\n';
210640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
211be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // Split the module into the two halves of the program we want.
212efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToNotOptimize = CloneModule(BD.getProgram());
213efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs);
214640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
215b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Run the predicate, not that the predicate will delete both input modules.
216b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return TestFn(BD, ToOptimize, ToNotOptimize);
217640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner}
218640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
2198c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// DisambiguateGlobalSymbols - Mangle symbols to guarantee uniqueness by
2208c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// modifying predominantly internal symbols rather than external ones.
2218c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
22236ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattnerstatic void DisambiguateGlobalSymbols(Module *M) {
22336ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // Try not to cause collisions by minimizing chances of renaming an
22436ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // already-external symbol, so take in external globals and functions as-is.
22536ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // The code should work correctly without disambiguation (assuming the same
22636ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // mangler is used by the two code generators), but having symbols with the
22736ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // same name causes warnings to be emitted by the code generator.
22836ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  Mangler Mang(*M);
22936ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
23036ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    I->setName(Mang.getValueName(I));
23136ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  for (Module::iterator  I = M->begin(),  E = M->end();  I != E; ++I)
23236ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    I->setName(Mang.getValueName(I));
23336ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner}
23436ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner
235a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// ExtractLoops - Given a reduced list of functions that still exposed the bug,
236a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// check to see if we can extract the loops in the region without obscuring the
237a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// bug.  If so, it reduces the amount of code identified.
2388c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
239b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic bool ExtractLoops(BugDriver &BD,
240b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                         bool (*TestFn)(BugDriver &, Module *, Module *),
241a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                         std::vector<Function*> &MiscompiledFunctions) {
242a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  bool MadeChange = false;
243a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  while (1) {
244a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToNotOptimize = CloneModule(BD.getProgram());
245a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
246a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                                                   MiscompiledFunctions);
247a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
248a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (!ToOptimizeLoopExtracted) {
249a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // If the loop extractor crashed or if there were no extractible loops,
250a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // then this chapter of our odyssey is over with.
251a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToNotOptimize;
252a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToOptimize;
253a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
254a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
255a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
256a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cerr << "Extracted a loop from the breaking portion of the program.\n";
257a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    delete ToOptimize;
258a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
259a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Bugpoint is intentionally not very trusting of LLVM transformations.  In
260a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // particular, we're not going to assume that the loop extractor works, so
261a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // we're going to test the newly loop extracted program to make sure nothing
262a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // has broken.  If something broke, then we'll inform the user and stop
263a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extraction.
264a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    AbstractInterpreter *AI = BD.switchToCBE();
265a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) {
266a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BD.switchToInterpreter(AI);
267a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
268a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // Merged program doesn't work anymore!
269a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << "  *** ERROR: Loop extraction broke the program. :("
270a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                << " Please report a bug!\n";
271a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << "      Continuing on with un-loop-extracted version.\n";
272a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToNotOptimize;
273a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToOptimizeLoopExtracted;
274a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
275a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
276a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    BD.switchToInterpreter(AI);
277a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
278b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cout << "  Testing after loop extraction:\n";
279b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    // Clone modules, the tester function will free them.
280b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
281b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    Module *TNOBackup  = CloneModule(ToNotOptimize);
282b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) {
283b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      std::cout << "*** Loop extraction masked the problem.  Undoing.\n";
284a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // If the program is not still broken, then loop extraction did something
285a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // that masked the error.  Stop loop extraction now.
286b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      delete TOLEBackup;
287b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      delete TNOBackup;
288a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
289a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
290b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ToOptimizeLoopExtracted = TOLEBackup;
291b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ToNotOptimize = TNOBackup;
292b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
293b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cout << "*** Loop extraction successful!\n";
294a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
29590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions;
29690c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
29790c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner           E = ToOptimizeLoopExtracted->end(); I != E; ++I)
29890c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner      MisCompFunctions.push_back(std::make_pair(I->getName(),
29990c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner                                                I->getFunctionType()));
30090c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner
301a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Okay, great!  Now we know that we extracted a loop and that loop
302a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extraction both didn't break the program, and didn't mask the problem.
303a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Replace the current program with the loop extracted version, and try to
304a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extract another loop.
305a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::string ErrorMsg;
306a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)) {
307a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << BD.getToolName() << ": Error linking modules together:"
308eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman                << ErrorMsg << '\n';
309a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      exit(1);
310a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
31190c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    delete ToOptimizeLoopExtracted;
312d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner
313d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner    // All of the Function*'s in the MiscompiledFunctions list are in the old
3145313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    // module.  Update this list to include all of the functions in the
3155313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    // optimized and loop extracted module.
3165313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    MiscompiledFunctions.clear();
31790c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
31890c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner      Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first,
31990c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner                                                  MisCompFunctions[i].second);
32090c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner      assert(NewF && "Function not found??");
32190c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner      MiscompiledFunctions.push_back(NewF);
322d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner    }
323d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner
324a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    BD.setNewProgram(ToNotOptimize);
325a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    MadeChange = true;
326a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  }
327a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner}
328a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
3295e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnernamespace {
3305e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
3315e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    BugDriver &BD;
3325e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    bool (*TestFn)(BugDriver &, Module *, Module *);
3335e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    std::vector<Function*> FunctionsBeingTested;
3345e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  public:
3355e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    ReduceMiscompiledBlocks(BugDriver &bd,
3365e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                            bool (*F)(BugDriver &, Module *, Module *),
3375e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                            const std::vector<Function*> &Fns)
3385e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
3395e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3405e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
3415e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                              std::vector<BasicBlock*> &Suffix) {
3425e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      if (!Suffix.empty() && TestFuncs(Suffix))
3435e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner        return KeepSuffix;
3445e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      if (TestFuncs(Prefix))
3455e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner        return KeepPrefix;
3465e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      return NoFailure;
3475e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    }
3485e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3495e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    bool TestFuncs(const std::vector<BasicBlock*> &Prefix);
3505e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  };
3515e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
3525e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3535e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// TestFuncs - Extract all blocks for the miscompiled functions except for the
3545e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// specified blocks.  If the problem still exists, return true.
3555e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner///
3565e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnerbool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs) {
3575e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Test to see if the function is misoptimized if we ONLY run it on the
3585e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // functions listed in Funcs.
35968bee938e539d884ee89ce4dfebbad777896960eChris Lattner  std::cout << "Checking to see if the program is misoptimized when all ";
36068bee938e539d884ee89ce4dfebbad777896960eChris Lattner  if (!BBs.empty()) {
36168bee938e539d884ee89ce4dfebbad777896960eChris Lattner    std::cout << "but these " << BBs.size() << " blocks are extracted: ";
36268bee938e539d884ee89ce4dfebbad777896960eChris Lattner    for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
36368bee938e539d884ee89ce4dfebbad777896960eChris Lattner      std::cout << BBs[i]->getName() << " ";
36468bee938e539d884ee89ce4dfebbad777896960eChris Lattner    if (BBs.size() > 10) std::cout << "...";
36568bee938e539d884ee89ce4dfebbad777896960eChris Lattner  } else {
36668bee938e539d884ee89ce4dfebbad777896960eChris Lattner    std::cout << "blocks are extracted.";
36768bee938e539d884ee89ce4dfebbad777896960eChris Lattner  }
368eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman  std::cout << '\n';
3695e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3705e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Split the module into the two halves of the program we want.
3715e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  Module *ToNotOptimize = CloneModule(BD.getProgram());
3725e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
3735e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                                                 FunctionsBeingTested);
3745e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3755e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Try the extraction.  If it doesn't work, then the block extractor crashed
3765e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // or something, in which case bugpoint can't chase down this possibility.
3775e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) {
3785e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    delete ToOptimize;
3795e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Run the predicate, not that the predicate will delete both input modules.
3805e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    return TestFn(BD, New, ToNotOptimize);
3815e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  }
3825e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  delete ToOptimize;
3835e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  delete ToNotOptimize;
3845e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  return false;
3855e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
3865e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3875e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3885e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// ExtractBlocks - Given a reduced list of functions that still expose the bug,
3895e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// extract as many basic blocks from the region as possible without obscuring
3905e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// the bug.
3915e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner///
3925e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnerstatic bool ExtractBlocks(BugDriver &BD,
3935e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                          bool (*TestFn)(BugDriver &, Module *, Module *),
3945e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                          std::vector<Function*> &MiscompiledFunctions) {
3955e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  std::vector<BasicBlock*> Blocks;
3965e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
3975e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    for (Function::iterator I = MiscompiledFunctions[i]->begin(),
3985e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner           E = MiscompiledFunctions[i]->end(); I != E; ++I)
3995e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      Blocks.push_back(I);
4005e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4015e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Use the list reducer to identify blocks that can be extracted without
4025e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // obscuring the bug.  The Blocks list will end up containing blocks that must
4035e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // be retained from the original program.
4045e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  unsigned OldSize = Blocks.size();
40568bee938e539d884ee89ce4dfebbad777896960eChris Lattner
40668bee938e539d884ee89ce4dfebbad777896960eChris Lattner  // Check to see if all blocks are extractible first.
40768bee938e539d884ee89ce4dfebbad777896960eChris Lattner  if (ReduceMiscompiledBlocks(BD, TestFn,
40868bee938e539d884ee89ce4dfebbad777896960eChris Lattner                  MiscompiledFunctions).TestFuncs(std::vector<BasicBlock*>())) {
40968bee938e539d884ee89ce4dfebbad777896960eChris Lattner    Blocks.clear();
41068bee938e539d884ee89ce4dfebbad777896960eChris Lattner  } else {
41168bee938e539d884ee89ce4dfebbad777896960eChris Lattner    ReduceMiscompiledBlocks(BD, TestFn,MiscompiledFunctions).reduceList(Blocks);
41268bee938e539d884ee89ce4dfebbad777896960eChris Lattner    if (Blocks.size() == OldSize)
41368bee938e539d884ee89ce4dfebbad777896960eChris Lattner      return false;
41468bee938e539d884ee89ce4dfebbad777896960eChris Lattner  }
4155e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4162290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  Module *ProgClone = CloneModule(BD.getProgram());
4172290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  Module *ToExtract = SplitFunctionsOutOfModule(ProgClone,
4182290e754061f1393bb96b1808ac33dc03399c939Chris Lattner                                                MiscompiledFunctions);
4192290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract);
4202290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  if (Extracted == 0) {
4212290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    // Wierd, extraction should have worked.
4222290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    std::cerr << "Nondeterministic problem extracting blocks??\n";
4232290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    delete ProgClone;
4242290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    delete ToExtract;
4252290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    return false;
4262290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  }
4275e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4282290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  // Otherwise, block extraction succeeded.  Link the two program fragments back
4292290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  // together.
4302290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  delete ToExtract;
4315e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
43290c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions;
43390c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  for (Module::iterator I = Extracted->begin(), E = Extracted->end();
43490c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner       I != E; ++I)
43590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    MisCompFunctions.push_back(std::make_pair(I->getName(),
43690c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner                                              I->getFunctionType()));
43790c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner
4382290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  std::string ErrorMsg;
4392290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  if (LinkModules(ProgClone, Extracted, &ErrorMsg)) {
4402290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    std::cerr << BD.getToolName() << ": Error linking modules together:"
441eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman              << ErrorMsg << '\n';
4422290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    exit(1);
4432290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  }
44490c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  delete Extracted;
4455e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4462290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  // Set the new program and delete the old one.
4472290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  BD.setNewProgram(ProgClone);
4485e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4492290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  // Update the list of miscompiled functions.
4502290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  MiscompiledFunctions.clear();
4515e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
45290c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
45390c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first,
45490c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner                                            MisCompFunctions[i].second);
45590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    assert(NewF && "Function not found??");
45690c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    MiscompiledFunctions.push_back(NewF);
45790c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  }
4582290e754061f1393bb96b1808ac33dc03399c939Chris Lattner
4592290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  return true;
4605e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
4615e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4625e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
463b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// DebugAMiscompilation - This is a generic driver to narrow down
464b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// miscompilations, either in an optimization or a code generator.
4658c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
466b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic std::vector<Function*>
467b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris LattnerDebugAMiscompilation(BugDriver &BD,
468b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                     bool (*TestFn)(BugDriver &, Module *, Module *)) {
469640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Okay, now that we have reduced the list of passes which are causing the
470640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // failure, see if we can pin down which functions are being
471640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // miscompiled... first build a list of all of the non-external functions in
472640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // the program.
473640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::vector<Function*> MiscompiledFunctions;
474b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  Module *Prog = BD.getProgram();
475b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
476640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    if (!I->isExternal())
477640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner      MiscompiledFunctions.push_back(I);
4784a10645c70199c8d8567fbc46312158c419720abChris Lattner
479640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Do the reduction...
480b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
481640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
482de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner  std::cout << "\n*** The following function"
483de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner            << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
484de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner            << " being miscompiled: ";
485640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  PrintFunctionList(MiscompiledFunctions);
486eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman  std::cout << '\n';
4874a10645c70199c8d8567fbc46312158c419720abChris Lattner
488a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  // See if we can rip any loops out of the miscompiled functions and still
489a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  // trigger the problem.
490b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  if (ExtractLoops(BD, TestFn, MiscompiledFunctions)) {
491a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Okay, we extracted some loops and the problem still appears.  See if we
492a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // can eliminate some of the created functions from being candidates.
493a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
49436ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    // Loop extraction can introduce functions with the same name (foo_code).
49536ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    // Make sure to disambiguate the symbols so that when the program is split
4965e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // apart that we can link it back together again.
4975e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    DisambiguateGlobalSymbols(BD.getProgram());
4985e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4995e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Do the reduction...
5005e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
5015e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
5025e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    std::cout << "\n*** The following function"
5035e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner              << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
5045e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner              << " being miscompiled: ";
5055e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    PrintFunctionList(MiscompiledFunctions);
506eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman    std::cout << '\n';
5075e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  }
5085e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
5095e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  if (ExtractBlocks(BD, TestFn, MiscompiledFunctions)) {
5105e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Okay, we extracted some blocks and the problem still appears.  See if we
5115e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // can eliminate some of the created functions from being candidates.
5125e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
5135e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Block extraction can introduce functions with the same name (foo_code).
5145e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Make sure to disambiguate the symbols so that when the program is split
51536ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    // apart that we can link it back together again.
51636ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    DisambiguateGlobalSymbols(BD.getProgram());
51736ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner
518a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Do the reduction...
519b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
520a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
521a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cout << "\n*** The following function"
522a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner              << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
523a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner              << " being miscompiled: ";
524a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    PrintFunctionList(MiscompiledFunctions);
525eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman    std::cout << '\n';
526a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  }
527a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
528b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return MiscompiledFunctions;
529b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner}
530b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
531a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// TestOptimizer - This is the predicate function used to check to see if the
532a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// "Test" portion of the program is misoptimized.  If so, return true.  In any
533a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// case, both module arguments are deleted.
5348c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
535b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
536b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Run the optimization passes on ToOptimize, producing a transformed version
537b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // of the functions being tested.
538b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "  Optimizing functions being tested: ";
539b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
540b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                                     /*AutoDebugCrashes*/true);
541b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "done.\n";
542b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  delete Test;
543b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
544b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "  Checking to see if the merged program executes correctly: ";
5452423db0e8577e769ac5ad4e567808e43daf37945Chris Lattner  bool Broken = TestMergedProgram(BD, Optimized, Safe, true);
546b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << (Broken ? " nope.\n" : " yup.\n");
547b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return Broken;
548b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner}
549b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
550b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
551b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// debugMiscompilation - This method is used when the passes selected are not
552b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// crashing, but the generated output is semantically different from the
553b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// input.
554b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner///
555b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerbool BugDriver::debugMiscompilation() {
556b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Make sure something was miscompiled...
557b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
558b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cerr << "*** Optimized program matches reference output!  No problem "
559b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner	      << "detected...\nbugpoint can't help you with your problem!\n";
560b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    return false;
561b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  }
562b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
563b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "\n*** Found miscompiling pass"
564b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner            << (getPassesToRun().size() == 1 ? "" : "es") << ": "
565eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman            << getPassesString(getPassesToRun()) << '\n';
566b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  EmitProgressBytecode("passinput");
567b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
568b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::vector<Function*> MiscompiledFunctions =
569b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    DebugAMiscompilation(*this, TestOptimizer);
570b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
571640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Output a bunch of bytecode files for the user...
572be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "Outputting reduced bytecode files which expose the problem:\n";
573efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToNotOptimize = CloneModule(getProgram());
574efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
575efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner                                                 MiscompiledFunctions);
576be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
577be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "  Non-optimized portion: ";
578b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ToNotOptimize = swapProgramIn(ToNotOptimize);
579be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  EmitProgressBytecode("tonotoptimize", true);
580be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  setNewProgram(ToNotOptimize);   // Delete hacked module.
581be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
582be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "  Portion that is input to optimizer: ";
583b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ToOptimize = swapProgramIn(ToOptimize);
584be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  EmitProgressBytecode("tooptimize");
585be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  setNewProgram(ToOptimize);      // Delete hacked module.
5864a10645c70199c8d8567fbc46312158c419720abChris Lattner
5874a10645c70199c8d8567fbc46312158c419720abChris Lattner  return false;
5884a10645c70199c8d8567fbc46312158c419720abChris Lattner}
589d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
590a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// CleanupAndPrepareModules - Get the specified modules ready for code
591a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// generator testing.
5928c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
593a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerstatic void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
594a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                     Module *Safe) {
595a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Clean up the modules, removing extra cruft that we don't need anymore...
596a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Test = BD.performFinalCleanups(Test);
597a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
598a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // If we are executing the JIT, we have several nasty issues to take care of.
599a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (!BD.isExecutingJIT()) return;
600a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
601a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // First, if the main function is in the Safe module, we must add a stub to
602a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // the Test module to call into it.  Thus, we create a new function `main'
603a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // which just calls the old one.
604a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (Function *oldMain = Safe->getNamedFunction("main"))
605a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    if (!oldMain->isExternal()) {
606a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Rename it
607a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      oldMain->setName("llvm_bugpoint_old_main");
608a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Create a NEW `main' function with same type in the test module.
609a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      Function *newMain = new Function(oldMain->getFunctionType(),
610a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                       GlobalValue::ExternalLinkage,
611a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                       "main", Test);
612a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Create an `oldmain' prototype in the test module, which will
613a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // corresponds to the real main function in the same module.
614a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      Function *oldMainProto = new Function(oldMain->getFunctionType(),
615a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                            GlobalValue::ExternalLinkage,
616a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                            oldMain->getName(), Test);
617a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Set up and remember the argument list for the main function.
618a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      std::vector<Value*> args;
619a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      for (Function::aiterator I = newMain->abegin(), E = newMain->aend(),
620a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner             OI = oldMain->abegin(); I != E; ++I, ++OI) {
621a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        I->setName(OI->getName());    // Copy argument names from oldMain
622a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        args.push_back(I);
623a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      }
624a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
625a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Call the old main function and return its result
626a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BasicBlock *BB = new BasicBlock("entry", newMain);
627a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      CallInst *call = new CallInst(oldMainProto, args);
628a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BB->getInstList().push_back(call);
629a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
630a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // If the type of old function wasn't void, return value of call
631a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      new ReturnInst(oldMain->getReturnType() != Type::VoidTy ? call : 0, BB);
632a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    }
633a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
634a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // The second nasty issue we must deal with in the JIT is that the Safe
635a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // module cannot directly reference any functions defined in the test
636a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // module.  Instead, we use a JIT API call to dynamically resolve the
637a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // symbol.
638a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
639a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Add the resolver to the Safe module.
640a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Prototype: void *getPointerToNamedFunction(const char* Name)
641a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Function *resolverFunc =
642a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    Safe->getOrInsertFunction("getPointerToNamedFunction",
643a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                              PointerType::get(Type::SByteTy),
644a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                              PointerType::get(Type::SByteTy), 0);
645a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
646a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Use the function we just added to get addresses of functions we need.
647dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman  for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
648a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    if (F->isExternal() && !F->use_empty() && &*F != resolverFunc &&
649a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        F->getIntrinsicID() == 0 /* ignore intrinsics */) {
650dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman      Function *TestFn = Test->getFunction(F->getName(), F->getFunctionType());
651a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
652a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Don't forward functions which are external in the test module too.
653a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      if (TestFn && !TestFn->isExternal()) {
654a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // 1. Add a string constant with its name to the global file
655a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        Constant *InitArray = ConstantArray::get(F->getName());
656a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        GlobalVariable *funcName =
657a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner          new GlobalVariable(InitArray->getType(), true /*isConstant*/,
658a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                             GlobalValue::InternalLinkage, InitArray,
659a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                             F->getName() + "_name", Safe);
660a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
661a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
662a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // sbyte* so it matches the signature of the resolver function.
663a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
664a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // GetElementPtr *funcName, ulong 0, ulong 0
665a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::IntTy));
666a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        Value *GEP =
667518310cb0d136906ff0a99d7a24cb460794de5bfReid Spencer          ConstantExpr::getGetElementPtr(funcName, GEPargs);
668a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        std::vector<Value*> ResolverArgs;
669a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        ResolverArgs.push_back(GEP);
670a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
671de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman        // Rewrite uses of F in global initializers, etc. to uses of a wrapper
672de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman        // function that dynamically resolves the calls to F via our JIT API
673de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman        if (F->use_begin() != F->use_end()) {
674de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // Construct a new stub function that will re-route calls to F
675dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          const FunctionType *FuncTy = F->getFunctionType();
676de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          Function *FuncWrapper = new Function(FuncTy,
677de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman                                               GlobalValue::InternalLinkage,
678dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman                                               F->getName() + "_wrapper",
679dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman                                               F->getParent());
680dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          BasicBlock *Header = new BasicBlock("header", FuncWrapper);
681dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman
682de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // Resolve the call to function F via the JIT API:
683de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          //
684de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // call resolver(GetElementPtr...)
685de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          CallInst *resolve = new CallInst(resolverFunc, ResolverArgs,
686de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman                                           "resolver");
687de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          Header->getInstList().push_back(resolve);
688de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // cast the result from the resolver to correctly-typed function
689de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          CastInst *castResolver =
690de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman            new CastInst(resolve, PointerType::get(F->getFunctionType()),
691de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman                         "resolverCast");
692de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          Header->getInstList().push_back(castResolver);
693de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman
694dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          // Save the argument list
695dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          std::vector<Value*> Args;
696dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          for (Function::aiterator i = FuncWrapper->abegin(),
697dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman                 e = FuncWrapper->aend(); i != e; ++i)
698dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Args.push_back(i);
699dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman
700dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          // Pass on the arguments to the real function, return its result
701dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          if (F->getReturnType() == Type::VoidTy) {
702de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman            CallInst *Call = new CallInst(castResolver, Args);
703dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Call);
704dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            ReturnInst *Ret = new ReturnInst();
705dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Ret);
706dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          } else {
707de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman            CallInst *Call = new CallInst(castResolver, Args, "redir");
708dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Call);
709dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            ReturnInst *Ret = new ReturnInst(Call);
710dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Ret);
711dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          }
712dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman
713de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // Use the wrapper function instead of the old function
714de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          F->replaceAllUsesWith(FuncWrapper);
715dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman        }
716a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      }
717a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    }
718a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
719a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
720a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (verifyModule(*Test) || verifyModule(*Safe)) {
721a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Bugpoint has a bug, which corrupted a module!!\n";
722a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    abort();
723a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
724a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
725a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
726a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
727a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
728a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// TestCodeGenerator - This is the predicate function used to check to see if
729a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// the "Test" portion of the program is miscompiled by the code generator under
730a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// test.  If so, return true.  In any case, both module arguments are deleted.
7318c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
732a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerstatic bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) {
733a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  CleanupAndPrepareModules(BD, Test, Safe);
734a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
735a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
736a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (BD.writeProgramToFile(TestModuleBC, Test)) {
737a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
738a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
739a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
740a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete Test;
741a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
742a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Make the shared library
743a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
744a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
745a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (BD.writeProgramToFile(SafeModuleBC, Safe)) {
746a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
747a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
748a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
749a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SharedObject = BD.compileSharedObject(SafeModuleBC);
750a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete Safe;
751a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
752a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Run the code generator on the `Test' code, loading the shared library.
753a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // The function returns whether or not the new output differs from reference.
754a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
755a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
756a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (Result)
757a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << ": still failing!\n";
758a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  else
759a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << ": didn't fail.\n";
760a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(TestModuleBC);
761a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(SafeModuleBC);
762a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(SharedObject);
763a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
764a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  return Result;
765a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
766a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
767a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
7688c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
7698c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
770a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerbool BugDriver::debugCodeGenerator() {
771a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if ((void*)cbe == (void*)Interpreter) {
772a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::string Result = executeProgramWithCBE("bugpoint.cbe.out");
773a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "\n*** The C backend cannot match the reference diff, but it "
774a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << "is used as the 'known good'\n    code generator, so I can't"
775a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << " debug it.  Perhaps you have a front-end problem?\n    As a"
776a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << " sanity check, I left the result of executing the program "
777a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << "with the C backend\n    in this file for you: '"
778a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << Result << "'.\n";
779a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    return true;
780a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
781a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
782a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  DisambiguateGlobalSymbols(Program);
783a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
784a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator);
785a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
786a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Split the module into the two halves of the program we want.
787a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Module *ToNotCodeGen = CloneModule(getProgram());
788a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs);
789a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
790a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Condition the modules
791a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
792a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
793a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
794a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (writeProgramToFile(TestModuleBC, ToCodeGen)) {
795a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
796a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
797a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
798a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete ToCodeGen;
799a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
800a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Make the shared library
801a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
802a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (writeProgramToFile(SafeModuleBC, ToNotCodeGen)) {
803a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
804a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
805a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
806a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SharedObject = compileSharedObject(SafeModuleBC);
807a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete ToNotCodeGen;
808a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
809a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::cout << "You can reproduce the problem with the command line: \n";
810a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (isExecutingJIT()) {
811a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  lli -load " << SharedObject << " " << TestModuleBC;
812a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  } else {
813a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
814a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  gcc " << SharedObject << " " << TestModuleBC
815a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << ".s -o " << TestModuleBC << ".exe -Wl,-R.\n";
816a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  " << TestModuleBC << ".exe";
817a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
818a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
819a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << " " << InputArgv[i];
820eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman  std::cout << '\n';
821a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::cout << "The shared object was created with:\n  llc -march=c "
822a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << SafeModuleBC << " -o temporary.c\n"
823a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << "  gcc -xc temporary.c -O2 -o " << SharedObject
824a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
825a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -G"            // Compile a shared library, `-G' for Sparc
826a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#else
827a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -shared"       // `-shared' for Linux/X86, maybe others
828a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#endif
829a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -fno-strict-aliasing\n";
830a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
831a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  return false;
832a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
833