Miscompilation.cpp revision a57d86b436549503a7f96c5266444e022bdbaf55
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
45640f22e66d90439857a97a83896ee68c4f7128c9Chris LattnerReduceMiscompilingPasses::TestResult
4639aebca3a2d1dd389a6d9cdfb51a53f625e244f0Chris LattnerReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
4706943add8b2b764e131979cca064eda9f28826c9Chris Lattner                                 std::vector<const PassInfo*> &Suffix) {
4806943add8b2b764e131979cca064eda9f28826c9Chris Lattner  // First, run the program with just the Suffix passes.  If it is still broken
49640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // with JUST the kept passes, discard the prefix passes.
5006943add8b2b764e131979cca064eda9f28826c9Chris Lattner  std::cout << "Checking to see if '" << getPassesString(Suffix)
51640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner            << "' compile correctly: ";
52640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
53640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::string BytecodeResult;
5406943add8b2b764e131979cca064eda9f28826c9Chris Lattner  if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
559f71e799c3e6e4cc0c71de82bda81f8753e82942Chris Lattner    std::cerr << " Error running this sequence of passes"
56640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner              << " on the input program!\n";
575ef681c19de9c675a265211f8fb0ae49cc3a3a66Chris Lattner    BD.setPassesToRun(Suffix);
585ef681c19de9c675a265211f8fb0ae49cc3a3a66Chris Lattner    BD.EmitProgressBytecode("pass-error",  false);
59025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner    exit(BD.debugOptimizerCrash());
60640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
61640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
62640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Check to see if the finished program matches the reference output...
635073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) {
64640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    std::cout << "nope.\n";
65640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    return KeepSuffix;        // Miscompilation detected!
66640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
67640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::cout << "yup.\n";      // No miscompilation!
68640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
69640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  if (Prefix.empty()) return NoFailure;
70640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
7106943add8b2b764e131979cca064eda9f28826c9Chris Lattner  // Next, see if the program is broken if we run the "prefix" passes first,
72bc0e998c497446f5448425b3cbd7f8f19a458764Misha Brukman  // then separately run the "kept" passes.
73640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::cout << "Checking to see if '" << getPassesString(Prefix)
74640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner            << "' compile correctly: ";
75640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
76640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // If it is not broken with the kept passes, it's possible that the prefix
77640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // passes must be run before the kept passes to break it.  If the program
78640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // WORKS after the prefix passes, but then fails if running the prefix AND
79640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // kept passes, we can update our bytecode file to include the result of the
80640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // prefix passes, then discard the prefix passes.
81640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  //
82640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
839f71e799c3e6e4cc0c71de82bda81f8753e82942Chris Lattner    std::cerr << " Error running this sequence of passes"
84640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner              << " on the input program!\n";
859c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner    BD.setPassesToRun(Prefix);
869c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner    BD.EmitProgressBytecode("pass-error",  false);
87025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner    exit(BD.debugOptimizerCrash());
88640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
89640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
90640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // If the prefix maintains the predicate by itself, only keep the prefix!
915073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  if (BD.diffProgram(BytecodeResult)) {
92640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    std::cout << "nope.\n";
93640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    removeFile(BytecodeResult);
94640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    return KeepPrefix;
95640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
96640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::cout << "yup.\n";      // No miscompilation!
97640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
98640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Ok, so now we know that the prefix passes work, try running the suffix
99640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // passes on the result of the prefix passes.
100640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  //
101efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *PrefixOutput = ParseInputFile(BytecodeResult);
102640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  if (PrefixOutput == 0) {
103640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    std::cerr << BD.getToolName() << ": Error reading bytecode file '"
104640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner              << BytecodeResult << "'!\n";
105640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    exit(1);
106640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
107640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  removeFile(BytecodeResult);  // No longer need the file on disk
108640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
10906943add8b2b764e131979cca064eda9f28826c9Chris Lattner  std::cout << "Checking to see if '" << getPassesString(Suffix)
110640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner            << "' passes compile correctly after the '"
111640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner            << getPassesString(Prefix) << "' passes: ";
112640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
113efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *OriginalInput = BD.swapProgramIn(PrefixOutput);
11406943add8b2b764e131979cca064eda9f28826c9Chris Lattner  if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
1159f71e799c3e6e4cc0c71de82bda81f8753e82942Chris Lattner    std::cerr << " Error running this sequence of passes"
116640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner              << " on the input program!\n";
1175ef681c19de9c675a265211f8fb0ae49cc3a3a66Chris Lattner    BD.setPassesToRun(Suffix);
1189c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner    BD.EmitProgressBytecode("pass-error",  false);
119025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner    exit(BD.debugOptimizerCrash());
120640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
121640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
122640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Run the result...
1235073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) {
124640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    std::cout << "nope.\n";
125640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    delete OriginalInput;     // We pruned down the original input...
12606943add8b2b764e131979cca064eda9f28826c9Chris Lattner    return KeepSuffix;
127640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  }
128640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
129640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Otherwise, we must not be running the bad pass anymore.
130640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::cout << "yup.\n";      // No miscompilation!
131efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test
132640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  return NoFailure;
133640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner}
134640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
135efdc0b505712d1ca4460def27e51c430f033d58dChris Lattnernamespace {
136fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  class ReduceMiscompilingFunctions : public ListReducer<Function*> {
137fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner    BugDriver &BD;
138b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    bool (*TestFn)(BugDriver &, Module *, Module *);
139fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  public:
140b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ReduceMiscompilingFunctions(BugDriver &bd,
141b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                                bool (*F)(BugDriver &, Module *, Module *))
142b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      : BD(bd), TestFn(F) {}
143fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner
144fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner    virtual TestResult doTest(std::vector<Function*> &Prefix,
145fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner                              std::vector<Function*> &Suffix) {
146be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner      if (!Suffix.empty() && TestFuncs(Suffix))
147fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner        return KeepSuffix;
148be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner      if (!Prefix.empty() && TestFuncs(Prefix))
149fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner        return KeepPrefix;
150fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner      return NoFailure;
151fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner    }
152fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner
153be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    bool TestFuncs(const std::vector<Function*> &Prefix);
154fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner  };
155fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner}
156640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
157efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// TestMergedProgram - Given two modules, link them together and run the
158efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// program, checking to see if the program matches the diff.  If the diff
159a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// matches, return false, otherwise return true.  If the DeleteInputs argument
160a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// is set to true then this function deletes both input modules before it
161a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// returns.
162a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattnerstatic bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
163a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                              bool DeleteInputs) {
164efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // Link the two portions of the program back to together.
165efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  std::string ErrorMsg;
166a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  if (!DeleteInputs) M1 = CloneModule(M1);
167efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  if (LinkModules(M1, M2, &ErrorMsg)) {
168efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    std::cerr << BD.getToolName() << ": Error linking modules together:"
169efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner              << ErrorMsg << "\n";
170efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    exit(1);
171efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  }
172a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  if (DeleteInputs) delete M2;  // We are done with this module...
173efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
174efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *OldProgram = BD.swapProgramIn(M1);
175efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
176efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // Execute the program.  If it does not match the expected output, we must
177efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // return true.
178efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  bool Broken = BD.diffProgram();
179efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
180efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // Delete the linked module & restore the original
181a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  BD.swapProgramIn(OldProgram);
1825313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner  delete M1;
183efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  return Broken;
184efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner}
185efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
186be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattnerbool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
187640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Test to see if the function is misoptimized if we ONLY run it on the
188640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // functions listed in Funcs.
189be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "Checking to see if the program is misoptimized when "
190be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner            << (Funcs.size()==1 ? "this function is" : "these functions are")
191be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner            << " run through the pass"
192a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner            << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
193efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  PrintFunctionList(Funcs);
194be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "\n";
195640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
196be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // Split the module into the two halves of the program we want.
197efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToNotOptimize = CloneModule(BD.getProgram());
198efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs);
199640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
200b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Run the predicate, not that the predicate will delete both input modules.
201b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return TestFn(BD, ToOptimize, ToNotOptimize);
202640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner}
203640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
204a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// ExtractLoops - Given a reduced list of functions that still exposed the bug,
205a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// check to see if we can extract the loops in the region without obscuring the
206a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// bug.  If so, it reduces the amount of code identified.
207b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic bool ExtractLoops(BugDriver &BD,
208b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                         bool (*TestFn)(BugDriver &, Module *, Module *),
209a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                         std::vector<Function*> &MiscompiledFunctions) {
210a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  bool MadeChange = false;
211a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  while (1) {
212a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToNotOptimize = CloneModule(BD.getProgram());
213a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
214a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                                                   MiscompiledFunctions);
215a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
216a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (!ToOptimizeLoopExtracted) {
217a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // If the loop extractor crashed or if there were no extractible loops,
218a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // then this chapter of our odyssey is over with.
219a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToNotOptimize;
220a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToOptimize;
221a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
222a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
223a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
224a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cerr << "Extracted a loop from the breaking portion of the program.\n";
225a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    delete ToOptimize;
226a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
227a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Bugpoint is intentionally not very trusting of LLVM transformations.  In
228a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // particular, we're not going to assume that the loop extractor works, so
229a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // we're going to test the newly loop extracted program to make sure nothing
230a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // has broken.  If something broke, then we'll inform the user and stop
231a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extraction.
232a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    AbstractInterpreter *AI = BD.switchToCBE();
233a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) {
234a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BD.switchToInterpreter(AI);
235a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
236a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // Merged program doesn't work anymore!
237a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << "  *** ERROR: Loop extraction broke the program. :("
238a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                << " Please report a bug!\n";
239a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << "      Continuing on with un-loop-extracted version.\n";
240a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToNotOptimize;
241a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToOptimizeLoopExtracted;
242a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
243a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
244a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    BD.switchToInterpreter(AI);
245a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
246b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cout << "  Testing after loop extraction:\n";
247b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    // Clone modules, the tester function will free them.
248b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
249b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    Module *TNOBackup  = CloneModule(ToNotOptimize);
250b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) {
251b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      std::cout << "*** Loop extraction masked the problem.  Undoing.\n";
252a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // If the program is not still broken, then loop extraction did something
253a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // that masked the error.  Stop loop extraction now.
254b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      delete TOLEBackup;
255b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      delete TNOBackup;
256a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
257a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
258b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ToOptimizeLoopExtracted = TOLEBackup;
259b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ToNotOptimize = TNOBackup;
260b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
261b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cout << "*** Loop extraction successful!\n";
262a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
263a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Okay, great!  Now we know that we extracted a loop and that loop
264a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extraction both didn't break the program, and didn't mask the problem.
265a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Replace the current program with the loop extracted version, and try to
266a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extract another loop.
267a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::string ErrorMsg;
268a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)) {
269a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << BD.getToolName() << ": Error linking modules together:"
270a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                << ErrorMsg << "\n";
271a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      exit(1);
272a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
273d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner
274d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner    // All of the Function*'s in the MiscompiledFunctions list are in the old
2755313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    // module.  Update this list to include all of the functions in the
2765313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    // optimized and loop extracted module.
2775313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    MiscompiledFunctions.clear();
2785313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
2795313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner           E = ToOptimizeLoopExtracted->end(); I != E; ++I) {
2805313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner      if (!I->isExternal()) {
28102bb481881fe6aaa876f9bf79f38f40f56a35a01Chris Lattner        Function *NewF = ToNotOptimize->getFunction(I->getName(),
28202bb481881fe6aaa876f9bf79f38f40f56a35a01Chris Lattner                                                    I->getFunctionType());
2835313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner        assert(NewF && "Function not found??");
2845313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner        MiscompiledFunctions.push_back(NewF);
2855313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner      }
286d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner    }
2875313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    delete ToOptimizeLoopExtracted;
288d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner
289a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    BD.setNewProgram(ToNotOptimize);
290a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    MadeChange = true;
291a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  }
292a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner}
293a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
294b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// DebugAMiscompilation - This is a generic driver to narrow down
295b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// miscompilations, either in an optimization or a code generator.
296b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic std::vector<Function*>
297b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris LattnerDebugAMiscompilation(BugDriver &BD,
298b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                     bool (*TestFn)(BugDriver &, Module *, Module *)) {
299640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Okay, now that we have reduced the list of passes which are causing the
300640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // failure, see if we can pin down which functions are being
301640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // miscompiled... first build a list of all of the non-external functions in
302640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // the program.
303640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::vector<Function*> MiscompiledFunctions;
304b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  Module *Prog = BD.getProgram();
305b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
306640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    if (!I->isExternal())
307640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner      MiscompiledFunctions.push_back(I);
3084a10645c70199c8d8567fbc46312158c419720abChris Lattner
309640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Do the reduction...
310b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
311640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
312de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner  std::cout << "\n*** The following function"
313de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner            << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
314de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner            << " being miscompiled: ";
315640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  PrintFunctionList(MiscompiledFunctions);
316640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::cout << "\n";
3174a10645c70199c8d8567fbc46312158c419720abChris Lattner
318a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  // See if we can rip any loops out of the miscompiled functions and still
319a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  // trigger the problem.
320b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  if (ExtractLoops(BD, TestFn, MiscompiledFunctions)) {
321a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Okay, we extracted some loops and the problem still appears.  See if we
322a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // can eliminate some of the created functions from being candidates.
323a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
324a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Do the reduction...
325b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
326a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
327a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cout << "\n*** The following function"
328a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner              << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
329a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner              << " being miscompiled: ";
330a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    PrintFunctionList(MiscompiledFunctions);
331a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cout << "\n";
332a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  }
333a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
334b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return MiscompiledFunctions;
335b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner}
336b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
337a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// TestOptimizer - This is the predicate function used to check to see if the
338a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// "Test" portion of the program is misoptimized.  If so, return true.  In any
339a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// case, both module arguments are deleted.
340b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
341b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Run the optimization passes on ToOptimize, producing a transformed version
342b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // of the functions being tested.
343b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "  Optimizing functions being tested: ";
344b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
345b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                                     /*AutoDebugCrashes*/true);
346b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "done.\n";
347b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  delete Test;
348b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
349b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "  Checking to see if the merged program executes correctly: ";
350b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  bool Broken = TestMergedProgram(BD, Test, Safe, true);
351b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << (Broken ? " nope.\n" : " yup.\n");
352b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return Broken;
353b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner}
354b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
355b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
356b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// debugMiscompilation - This method is used when the passes selected are not
357b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// crashing, but the generated output is semantically different from the
358b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// input.
359b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner///
360b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerbool BugDriver::debugMiscompilation() {
361b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Make sure something was miscompiled...
362b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
363b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cerr << "*** Optimized program matches reference output!  No problem "
364b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner	      << "detected...\nbugpoint can't help you with your problem!\n";
365b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    return false;
366b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  }
367b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
368b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "\n*** Found miscompiling pass"
369b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner            << (getPassesToRun().size() == 1 ? "" : "es") << ": "
370b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner            << getPassesString(getPassesToRun()) << "\n";
371b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  EmitProgressBytecode("passinput");
372b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
373b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::vector<Function*> MiscompiledFunctions =
374b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    DebugAMiscompilation(*this, TestOptimizer);
375b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
376640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Output a bunch of bytecode files for the user...
377be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "Outputting reduced bytecode files which expose the problem:\n";
378efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToNotOptimize = CloneModule(getProgram());
379efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
380efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner                                                 MiscompiledFunctions);
381be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
382be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "  Non-optimized portion: ";
383b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ToNotOptimize = swapProgramIn(ToNotOptimize);
384be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  EmitProgressBytecode("tonotoptimize", true);
385be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  setNewProgram(ToNotOptimize);   // Delete hacked module.
386be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
387be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "  Portion that is input to optimizer: ";
388b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ToOptimize = swapProgramIn(ToOptimize);
389be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  EmitProgressBytecode("tooptimize");
390be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  setNewProgram(ToOptimize);      // Delete hacked module.
3914a10645c70199c8d8567fbc46312158c419720abChris Lattner
3924a10645c70199c8d8567fbc46312158c419720abChris Lattner  return false;
3934a10645c70199c8d8567fbc46312158c419720abChris Lattner}
394d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
395a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// CleanupAndPrepareModules - Get the specified modules ready for code
396a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// generator testing.
397a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerstatic void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
398a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                     Module *Safe) {
399a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Clean up the modules, removing extra cruft that we don't need anymore...
400a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Test = BD.performFinalCleanups(Test);
401a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
402a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // If we are executing the JIT, we have several nasty issues to take care of.
403a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (!BD.isExecutingJIT()) return;
404a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
405a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // First, if the main function is in the Safe module, we must add a stub to
406a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // the Test module to call into it.  Thus, we create a new function `main'
407a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // which just calls the old one.
408a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (Function *oldMain = Safe->getNamedFunction("main"))
409a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    if (!oldMain->isExternal()) {
410a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Rename it
411a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      oldMain->setName("llvm_bugpoint_old_main");
412a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Create a NEW `main' function with same type in the test module.
413a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      Function *newMain = new Function(oldMain->getFunctionType(),
414a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                       GlobalValue::ExternalLinkage,
415a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                       "main", Test);
416a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Create an `oldmain' prototype in the test module, which will
417a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // corresponds to the real main function in the same module.
418a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      Function *oldMainProto = new Function(oldMain->getFunctionType(),
419a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                            GlobalValue::ExternalLinkage,
420a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                            oldMain->getName(), Test);
421a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Set up and remember the argument list for the main function.
422a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      std::vector<Value*> args;
423a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      for (Function::aiterator I = newMain->abegin(), E = newMain->aend(),
424a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner             OI = oldMain->abegin(); I != E; ++I, ++OI) {
425a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        I->setName(OI->getName());    // Copy argument names from oldMain
426a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        args.push_back(I);
427a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      }
428a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
429a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Call the old main function and return its result
430a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BasicBlock *BB = new BasicBlock("entry", newMain);
431a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      CallInst *call = new CallInst(oldMainProto, args);
432a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BB->getInstList().push_back(call);
433a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
434a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // If the type of old function wasn't void, return value of call
435a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      new ReturnInst(oldMain->getReturnType() != Type::VoidTy ? call : 0, BB);
436a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    }
437a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
438a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // The second nasty issue we must deal with in the JIT is that the Safe
439a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // module cannot directly reference any functions defined in the test
440a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // module.  Instead, we use a JIT API call to dynamically resolve the
441a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // symbol.
442a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
443a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Add the resolver to the Safe module.
444a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Prototype: void *getPointerToNamedFunction(const char* Name)
445a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Function *resolverFunc =
446a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    Safe->getOrInsertFunction("getPointerToNamedFunction",
447a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                              PointerType::get(Type::SByteTy),
448a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                              PointerType::get(Type::SByteTy), 0);
449a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
450a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Use the function we just added to get addresses of functions we need.
451a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F){
452a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    if (F->isExternal() && !F->use_empty() && &*F != resolverFunc &&
453a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        F->getIntrinsicID() == 0 /* ignore intrinsics */) {
454a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      Function *TestFn =Test->getFunction(F->getName(), F->getFunctionType());
455a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
456a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Don't forward functions which are external in the test module too.
457a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      if (TestFn && !TestFn->isExternal()) {
458a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // 1. Add a string constant with its name to the global file
459a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        Constant *InitArray = ConstantArray::get(F->getName());
460a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        GlobalVariable *funcName =
461a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner          new GlobalVariable(InitArray->getType(), true /*isConstant*/,
462a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                             GlobalValue::InternalLinkage, InitArray,
463a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                             F->getName() + "_name", Safe);
464a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
465a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
466a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // sbyte* so it matches the signature of the resolver function.
467a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
468a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // GetElementPtr *funcName, ulong 0, ulong 0
469a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::IntTy));
470a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        Value *GEP =
471a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner          ConstantExpr::getGetElementPtr(ConstantPointerRef::get(funcName),
472a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                         GEPargs);
473a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        std::vector<Value*> ResolverArgs;
474a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        ResolverArgs.push_back(GEP);
475a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
476a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // 3. Replace all uses of `func' with calls to resolver by:
477a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // (a) Iterating through the list of uses of this function
478a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // (b) Insert a cast instruction in front of each use
479a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // (c) Replace use of old call with new call
480a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
481a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // Insert code at the beginning of the function
482a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        while (!F->use_empty())
483a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner          if (Instruction *Inst = dyn_cast<Instruction>(F->use_back())) {
484a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            // call resolver(GetElementPtr...)
485a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            CallInst *resolve = new CallInst(resolverFunc, ResolverArgs,
486a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                             "resolver", Inst);
487a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            // cast the result from the resolver to correctly-typed function
488a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            CastInst *castResolver =
489a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              new CastInst(resolve, PointerType::get(F->getFunctionType()),
490a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                           "resolverCast", Inst);
491a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            // actually use the resolved function
492a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            Inst->replaceUsesOfWith(F, castResolver);
493a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner          } else {
494a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            // FIXME: need to take care of cases where a function is used by
495a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            // something other than an instruction; e.g., global variable
496a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            // initializers and constant expressions.
497a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            std::cerr << "UNSUPPORTED: Non-instruction is using an external "
498a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                      << "function, " << F->getName() << "().\n";
499a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            abort();
500a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner          }
501a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      }
502a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    }
503a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
504a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
505a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (verifyModule(*Test) || verifyModule(*Safe)) {
506a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Bugpoint has a bug, which corrupted a module!!\n";
507a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    abort();
508a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
509a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
510a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
511a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
512a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
513a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// TestCodeGenerator - This is the predicate function used to check to see if
514a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// the "Test" portion of the program is miscompiled by the code generator under
515a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// test.  If so, return true.  In any case, both module arguments are deleted.
516a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerstatic bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) {
517a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  CleanupAndPrepareModules(BD, Test, Safe);
518a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
519a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
520a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (BD.writeProgramToFile(TestModuleBC, Test)) {
521a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
522a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
523a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
524a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete Test;
525a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
526a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Make the shared library
527a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
528a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
529a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (BD.writeProgramToFile(SafeModuleBC, Safe)) {
530a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
531a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
532a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
533a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SharedObject = BD.compileSharedObject(SafeModuleBC);
534a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete Safe;
535a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
536a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Run the code generator on the `Test' code, loading the shared library.
537a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // The function returns whether or not the new output differs from reference.
538a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
539a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
540a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (Result)
541a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << ": still failing!\n";
542a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  else
543a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << ": didn't fail.\n";
544a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(TestModuleBC);
545a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(SafeModuleBC);
546a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(SharedObject);
547a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
548a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  return Result;
549a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
550a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
551a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
552a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
553a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerstatic void DisambiguateGlobalSymbols(Module *M) {
554a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Try not to cause collisions by minimizing chances of renaming an
555a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // already-external symbol, so take in external globals and functions as-is.
556a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // The code should work correctly without disambiguation (assuming the same
557a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // mangler is used by the two code generators), but having symbols with the
558a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // same name causes warnings to be emitted by the code generator.
559a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Mangler Mang(*M);
560a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
561a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    I->setName(Mang.getValueName(I));
562a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  for (Module::iterator  I = M->begin(),  E = M->end();  I != E; ++I)
563a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    I->setName(Mang.getValueName(I));
564a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
565a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
566a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
567a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
568a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerbool BugDriver::debugCodeGenerator() {
569a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if ((void*)cbe == (void*)Interpreter) {
570a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::string Result = executeProgramWithCBE("bugpoint.cbe.out");
571a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "\n*** The C backend cannot match the reference diff, but it "
572a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << "is used as the 'known good'\n    code generator, so I can't"
573a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << " debug it.  Perhaps you have a front-end problem?\n    As a"
574a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << " sanity check, I left the result of executing the program "
575a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << "with the C backend\n    in this file for you: '"
576a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << Result << "'.\n";
577a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    return true;
578a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
579a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
580a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  DisambiguateGlobalSymbols(Program);
581a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
582a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator);
583a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
584a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Split the module into the two halves of the program we want.
585a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Module *ToNotCodeGen = CloneModule(getProgram());
586a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs);
587a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
588a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Condition the modules
589a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
590a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
591a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
592a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (writeProgramToFile(TestModuleBC, ToCodeGen)) {
593a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
594a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
595a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
596a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete ToCodeGen;
597a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
598a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Make the shared library
599a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
600a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (writeProgramToFile(SafeModuleBC, ToNotCodeGen)) {
601a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
602a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
603a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
604a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SharedObject = compileSharedObject(SafeModuleBC);
605a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete ToNotCodeGen;
606a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
607a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::cout << "You can reproduce the problem with the command line: \n";
608a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (isExecutingJIT()) {
609a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  lli -load " << SharedObject << " " << TestModuleBC;
610a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  } else {
611a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
612a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  gcc " << SharedObject << " " << TestModuleBC
613a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << ".s -o " << TestModuleBC << ".exe -Wl,-R.\n";
614a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  " << TestModuleBC << ".exe";
615a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
616a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
617a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << " " << InputArgv[i];
618a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::cout << "\n";
619a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::cout << "The shared object was created with:\n  llc -march=c "
620a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << SafeModuleBC << " -o temporary.c\n"
621a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << "  gcc -xc temporary.c -O2 -o " << SharedObject
622a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
623a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -G"            // Compile a shared library, `-G' for Sparc
624a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#else
625a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -shared"       // `-shared' for Linux/X86, maybe others
626a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#endif
627a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -fno-strict-aliasing\n";
628a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
629a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  return false;
630a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
631