Miscompilation.cpp revision 551ccae044b0ff658fe629dd67edd5ffe75d10e8
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"
25008248f2c098f7680b5b393d6e3006cd7d974b77Misha Brukman#include "llvm/Support/Linker.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;
174a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  if (!DeleteInputs) M1 = CloneModule(M1);
175efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  if (LinkModules(M1, M2, &ErrorMsg)) {
176efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    std::cerr << BD.getToolName() << ": Error linking modules together:"
177eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman              << ErrorMsg << '\n';
178efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    exit(1);
179efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  }
180a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  if (DeleteInputs) delete M2;  // We are done with this module...
181efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
182efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *OldProgram = BD.swapProgramIn(M1);
183efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
184efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // Execute the program.  If it does not match the expected output, we must
185efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // return true.
186efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  bool Broken = BD.diffProgram();
187efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
188efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  // Delete the linked module & restore the original
189a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  BD.swapProgramIn(OldProgram);
1905313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner  delete M1;
191efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  return Broken;
192efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner}
193efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
1948c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// TestFuncs - split functions in a Module into two groups: those that are
1958c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// under consideration for miscompilation vs. those that are not, and test
1968c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// accordingly. Each group of functions becomes a separate Module.
1978c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
198be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattnerbool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
199640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Test to see if the function is misoptimized if we ONLY run it on the
200640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // functions listed in Funcs.
201be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "Checking to see if the program is misoptimized when "
202be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner            << (Funcs.size()==1 ? "this function is" : "these functions are")
203be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner            << " run through the pass"
204a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner            << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
205efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  PrintFunctionList(Funcs);
206eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman  std::cout << '\n';
207640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
208be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // Split the module into the two halves of the program we want.
209efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToNotOptimize = CloneModule(BD.getProgram());
210efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs);
211640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
212b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Run the predicate, not that the predicate will delete both input modules.
213b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return TestFn(BD, ToOptimize, ToNotOptimize);
214640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner}
215640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
2168c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// DisambiguateGlobalSymbols - Mangle symbols to guarantee uniqueness by
2178c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// modifying predominantly internal symbols rather than external ones.
2188c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
21936ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattnerstatic void DisambiguateGlobalSymbols(Module *M) {
22036ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // Try not to cause collisions by minimizing chances of renaming an
22136ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // already-external symbol, so take in external globals and functions as-is.
22236ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // The code should work correctly without disambiguation (assuming the same
22336ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // mangler is used by the two code generators), but having symbols with the
22436ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  // same name causes warnings to be emitted by the code generator.
22536ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  Mangler Mang(*M);
22636ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
22736ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    I->setName(Mang.getValueName(I));
22836ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner  for (Module::iterator  I = M->begin(),  E = M->end();  I != E; ++I)
22936ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    I->setName(Mang.getValueName(I));
23036ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner}
23136ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner
232a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// ExtractLoops - Given a reduced list of functions that still exposed the bug,
233a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// check to see if we can extract the loops in the region without obscuring the
234a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// bug.  If so, it reduces the amount of code identified.
2358c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
236b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic bool ExtractLoops(BugDriver &BD,
237b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                         bool (*TestFn)(BugDriver &, Module *, Module *),
238a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                         std::vector<Function*> &MiscompiledFunctions) {
239a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  bool MadeChange = false;
240a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  while (1) {
241a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToNotOptimize = CloneModule(BD.getProgram());
242a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
243a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                                                   MiscompiledFunctions);
244a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
245a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (!ToOptimizeLoopExtracted) {
246a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // If the loop extractor crashed or if there were no extractible loops,
247a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // then this chapter of our odyssey is over with.
248a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToNotOptimize;
249a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToOptimize;
250a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
251a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
252a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
253a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cerr << "Extracted a loop from the breaking portion of the program.\n";
254a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    delete ToOptimize;
255a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
256a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Bugpoint is intentionally not very trusting of LLVM transformations.  In
257a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // particular, we're not going to assume that the loop extractor works, so
258a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // we're going to test the newly loop extracted program to make sure nothing
259a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // has broken.  If something broke, then we'll inform the user and stop
260a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extraction.
261a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    AbstractInterpreter *AI = BD.switchToCBE();
262a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) {
263a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BD.switchToInterpreter(AI);
264a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
265a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // Merged program doesn't work anymore!
266a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << "  *** ERROR: Loop extraction broke the program. :("
267a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner                << " Please report a bug!\n";
268a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << "      Continuing on with un-loop-extracted version.\n";
269a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToNotOptimize;
270a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      delete ToOptimizeLoopExtracted;
271a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
272a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
273a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    BD.switchToInterpreter(AI);
274a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
275b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cout << "  Testing after loop extraction:\n";
276b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    // Clone modules, the tester function will free them.
277b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
278b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    Module *TNOBackup  = CloneModule(ToNotOptimize);
279b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) {
280b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      std::cout << "*** Loop extraction masked the problem.  Undoing.\n";
281a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // If the program is not still broken, then loop extraction did something
282a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      // that masked the error.  Stop loop extraction now.
283b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      delete TOLEBackup;
284b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner      delete TNOBackup;
285a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      return MadeChange;
286a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
287b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ToOptimizeLoopExtracted = TOLEBackup;
288b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ToNotOptimize = TNOBackup;
289b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
290b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cout << "*** Loop extraction successful!\n";
291a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
292a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Okay, great!  Now we know that we extracted a loop and that loop
293a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extraction both didn't break the program, and didn't mask the problem.
294a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Replace the current program with the loop extracted version, and try to
295a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // extract another loop.
296a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::string ErrorMsg;
297a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    if (LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)) {
298a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      std::cerr << BD.getToolName() << ": Error linking modules together:"
299eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman                << ErrorMsg << '\n';
300a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner      exit(1);
301a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    }
302d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner
303d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner    // All of the Function*'s in the MiscompiledFunctions list are in the old
3045313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    // module.  Update this list to include all of the functions in the
3055313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    // optimized and loop extracted module.
3065313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    MiscompiledFunctions.clear();
3075313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
3085313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner           E = ToOptimizeLoopExtracted->end(); I != E; ++I) {
3095313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner      if (!I->isExternal()) {
31002bb481881fe6aaa876f9bf79f38f40f56a35a01Chris Lattner        Function *NewF = ToNotOptimize->getFunction(I->getName(),
31102bb481881fe6aaa876f9bf79f38f40f56a35a01Chris Lattner                                                    I->getFunctionType());
3125313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner        assert(NewF && "Function not found??");
3135313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner        MiscompiledFunctions.push_back(NewF);
3145313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner      }
315d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner    }
3165313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner    delete ToOptimizeLoopExtracted;
317d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner
318a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    BD.setNewProgram(ToNotOptimize);
319a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    MadeChange = true;
320a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  }
321a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner}
322a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
3235e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnernamespace {
3245e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
3255e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    BugDriver &BD;
3265e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    bool (*TestFn)(BugDriver &, Module *, Module *);
3275e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    std::vector<Function*> FunctionsBeingTested;
3285e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  public:
3295e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    ReduceMiscompiledBlocks(BugDriver &bd,
3305e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                            bool (*F)(BugDriver &, Module *, Module *),
3315e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                            const std::vector<Function*> &Fns)
3325e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
3335e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3345e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
3355e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                              std::vector<BasicBlock*> &Suffix) {
3365e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      if (!Suffix.empty() && TestFuncs(Suffix))
3375e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner        return KeepSuffix;
3385e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      if (TestFuncs(Prefix))
3395e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner        return KeepPrefix;
3405e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      return NoFailure;
3415e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    }
3425e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3435e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    bool TestFuncs(const std::vector<BasicBlock*> &Prefix);
3445e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  };
3455e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
3465e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3475e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// TestFuncs - Extract all blocks for the miscompiled functions except for the
3485e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// specified blocks.  If the problem still exists, return true.
3495e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner///
3505e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnerbool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs) {
3515e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Test to see if the function is misoptimized if we ONLY run it on the
3525e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // functions listed in Funcs.
35368bee938e539d884ee89ce4dfebbad777896960eChris Lattner  std::cout << "Checking to see if the program is misoptimized when all ";
35468bee938e539d884ee89ce4dfebbad777896960eChris Lattner  if (!BBs.empty()) {
35568bee938e539d884ee89ce4dfebbad777896960eChris Lattner    std::cout << "but these " << BBs.size() << " blocks are extracted: ";
35668bee938e539d884ee89ce4dfebbad777896960eChris Lattner    for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
35768bee938e539d884ee89ce4dfebbad777896960eChris Lattner      std::cout << BBs[i]->getName() << " ";
35868bee938e539d884ee89ce4dfebbad777896960eChris Lattner    if (BBs.size() > 10) std::cout << "...";
35968bee938e539d884ee89ce4dfebbad777896960eChris Lattner  } else {
36068bee938e539d884ee89ce4dfebbad777896960eChris Lattner    std::cout << "blocks are extracted.";
36168bee938e539d884ee89ce4dfebbad777896960eChris Lattner  }
362eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman  std::cout << '\n';
3635e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3645e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Split the module into the two halves of the program we want.
3655e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  Module *ToNotOptimize = CloneModule(BD.getProgram());
3665e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
3675e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                                                 FunctionsBeingTested);
3685e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3695e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Try the extraction.  If it doesn't work, then the block extractor crashed
3705e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // or something, in which case bugpoint can't chase down this possibility.
3715e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) {
3725e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    delete ToOptimize;
3735e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Run the predicate, not that the predicate will delete both input modules.
3745e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    return TestFn(BD, New, ToNotOptimize);
3755e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  }
3765e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  delete ToOptimize;
3775e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  delete ToNotOptimize;
3785e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  return false;
3795e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
3805e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3815e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3825e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// ExtractBlocks - Given a reduced list of functions that still expose the bug,
3835e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// extract as many basic blocks from the region as possible without obscuring
3845e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// the bug.
3855e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner///
3865e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnerstatic bool ExtractBlocks(BugDriver &BD,
3875e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                          bool (*TestFn)(BugDriver &, Module *, Module *),
3885e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                          std::vector<Function*> &MiscompiledFunctions) {
3895e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  std::vector<BasicBlock*> Blocks;
3905e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
3915e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    for (Function::iterator I = MiscompiledFunctions[i]->begin(),
3925e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner           E = MiscompiledFunctions[i]->end(); I != E; ++I)
3935e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      Blocks.push_back(I);
3945e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3955e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Use the list reducer to identify blocks that can be extracted without
3965e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // obscuring the bug.  The Blocks list will end up containing blocks that must
3975e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // be retained from the original program.
3985e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  unsigned OldSize = Blocks.size();
39968bee938e539d884ee89ce4dfebbad777896960eChris Lattner
40068bee938e539d884ee89ce4dfebbad777896960eChris Lattner  // Check to see if all blocks are extractible first.
40168bee938e539d884ee89ce4dfebbad777896960eChris Lattner  if (ReduceMiscompiledBlocks(BD, TestFn,
40268bee938e539d884ee89ce4dfebbad777896960eChris Lattner                  MiscompiledFunctions).TestFuncs(std::vector<BasicBlock*>())) {
40368bee938e539d884ee89ce4dfebbad777896960eChris Lattner    Blocks.clear();
40468bee938e539d884ee89ce4dfebbad777896960eChris Lattner  } else {
40568bee938e539d884ee89ce4dfebbad777896960eChris Lattner    ReduceMiscompiledBlocks(BD, TestFn,MiscompiledFunctions).reduceList(Blocks);
40668bee938e539d884ee89ce4dfebbad777896960eChris Lattner    if (Blocks.size() == OldSize)
40768bee938e539d884ee89ce4dfebbad777896960eChris Lattner      return false;
40868bee938e539d884ee89ce4dfebbad777896960eChris Lattner  }
4095e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4102290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  Module *ProgClone = CloneModule(BD.getProgram());
4112290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  Module *ToExtract = SplitFunctionsOutOfModule(ProgClone,
4122290e754061f1393bb96b1808ac33dc03399c939Chris Lattner                                                MiscompiledFunctions);
4132290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract);
4142290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  if (Extracted == 0) {
4152290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    // Wierd, extraction should have worked.
4162290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    std::cerr << "Nondeterministic problem extracting blocks??\n";
4172290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    delete ProgClone;
4182290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    delete ToExtract;
4192290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    return false;
4202290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  }
4215e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4222290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  // Otherwise, block extraction succeeded.  Link the two program fragments back
4232290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  // together.
4242290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  delete ToExtract;
4255e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4262290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  std::string ErrorMsg;
4272290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  if (LinkModules(ProgClone, Extracted, &ErrorMsg)) {
4282290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    std::cerr << BD.getToolName() << ": Error linking modules together:"
429eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman              << ErrorMsg << '\n';
4302290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    exit(1);
4312290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  }
4325e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4332290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  // Set the new program and delete the old one.
4342290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  BD.setNewProgram(ProgClone);
4355e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4362290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  // Update the list of miscompiled functions.
4372290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  MiscompiledFunctions.clear();
4385e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4392290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  for (Module::iterator I = Extracted->begin(), E = Extracted->end(); I != E;
4402290e754061f1393bb96b1808ac33dc03399c939Chris Lattner       ++I)
4412290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    if (!I->isExternal()) {
4422290e754061f1393bb96b1808ac33dc03399c939Chris Lattner      Function *NF = ProgClone->getFunction(I->getName(), I->getFunctionType());
4432290e754061f1393bb96b1808ac33dc03399c939Chris Lattner      assert(NF && "Mapped function not found!");
4442290e754061f1393bb96b1808ac33dc03399c939Chris Lattner      MiscompiledFunctions.push_back(NF);
4452290e754061f1393bb96b1808ac33dc03399c939Chris Lattner    }
4462290e754061f1393bb96b1808ac33dc03399c939Chris Lattner
4472290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  delete Extracted;
4482290e754061f1393bb96b1808ac33dc03399c939Chris Lattner
4492290e754061f1393bb96b1808ac33dc03399c939Chris Lattner  return true;
4505e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
4515e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4525e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
453b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// DebugAMiscompilation - This is a generic driver to narrow down
454b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// miscompilations, either in an optimization or a code generator.
4558c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
456b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic std::vector<Function*>
457b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris LattnerDebugAMiscompilation(BugDriver &BD,
458b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                     bool (*TestFn)(BugDriver &, Module *, Module *)) {
459640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Okay, now that we have reduced the list of passes which are causing the
460640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // failure, see if we can pin down which functions are being
461640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // miscompiled... first build a list of all of the non-external functions in
462640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // the program.
463640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  std::vector<Function*> MiscompiledFunctions;
464b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  Module *Prog = BD.getProgram();
465b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
466640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner    if (!I->isExternal())
467640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner      MiscompiledFunctions.push_back(I);
4684a10645c70199c8d8567fbc46312158c419720abChris Lattner
469640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Do the reduction...
470b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
471640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
472de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner  std::cout << "\n*** The following function"
473de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner            << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
474de9750def7c5ca6cb789f3bba7c913e237cdf849Chris Lattner            << " being miscompiled: ";
475640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  PrintFunctionList(MiscompiledFunctions);
476eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman  std::cout << '\n';
4774a10645c70199c8d8567fbc46312158c419720abChris Lattner
478a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  // See if we can rip any loops out of the miscompiled functions and still
479a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  // trigger the problem.
480b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  if (ExtractLoops(BD, TestFn, MiscompiledFunctions)) {
481a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Okay, we extracted some loops and the problem still appears.  See if we
482a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // can eliminate some of the created functions from being candidates.
483a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
48436ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    // Loop extraction can introduce functions with the same name (foo_code).
48536ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    // Make sure to disambiguate the symbols so that when the program is split
4865e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // apart that we can link it back together again.
4875e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    DisambiguateGlobalSymbols(BD.getProgram());
4885e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4895e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Do the reduction...
4905e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
4915e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4925e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    std::cout << "\n*** The following function"
4935e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner              << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
4945e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner              << " being miscompiled: ";
4955e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    PrintFunctionList(MiscompiledFunctions);
496eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman    std::cout << '\n';
4975e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  }
4985e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
4995e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  if (ExtractBlocks(BD, TestFn, MiscompiledFunctions)) {
5005e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Okay, we extracted some blocks and the problem still appears.  See if we
5015e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // can eliminate some of the created functions from being candidates.
5025e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
5035e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Block extraction can introduce functions with the same name (foo_code).
5045e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Make sure to disambiguate the symbols so that when the program is split
50536ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    // apart that we can link it back together again.
50636ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner    DisambiguateGlobalSymbols(BD.getProgram());
50736ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner
508a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    // Do the reduction...
509b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
510a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
511a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cout << "\n*** The following function"
512a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner              << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
513a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner              << " being miscompiled: ";
514a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    PrintFunctionList(MiscompiledFunctions);
515eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman    std::cout << '\n';
516a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner  }
517a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner
518b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return MiscompiledFunctions;
519b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner}
520b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
521a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// TestOptimizer - This is the predicate function used to check to see if the
522a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// "Test" portion of the program is misoptimized.  If so, return true.  In any
523a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// case, both module arguments are deleted.
5248c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
525b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
526b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Run the optimization passes on ToOptimize, producing a transformed version
527b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // of the functions being tested.
528b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "  Optimizing functions being tested: ";
529b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
530b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner                                     /*AutoDebugCrashes*/true);
531b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "done.\n";
532b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  delete Test;
533b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
534b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "  Checking to see if the merged program executes correctly: ";
5352423db0e8577e769ac5ad4e567808e43daf37945Chris Lattner  bool Broken = TestMergedProgram(BD, Optimized, Safe, true);
536b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << (Broken ? " nope.\n" : " yup.\n");
537b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  return Broken;
538b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner}
539b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
540b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
541b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// debugMiscompilation - This method is used when the passes selected are not
542b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// crashing, but the generated output is semantically different from the
543b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// input.
544b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner///
545b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerbool BugDriver::debugMiscompilation() {
546b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  // Make sure something was miscompiled...
547b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
548b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    std::cerr << "*** Optimized program matches reference output!  No problem "
549b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner	      << "detected...\nbugpoint can't help you with your problem!\n";
550b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    return false;
551b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  }
552b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
553b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::cout << "\n*** Found miscompiling pass"
554b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner            << (getPassesToRun().size() == 1 ? "" : "es") << ": "
555eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman            << getPassesString(getPassesToRun()) << '\n';
556b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  EmitProgressBytecode("passinput");
557b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
558b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  std::vector<Function*> MiscompiledFunctions =
559b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner    DebugAMiscompilation(*this, TestOptimizer);
560b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner
561640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  // Output a bunch of bytecode files for the user...
562be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "Outputting reduced bytecode files which expose the problem:\n";
563efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToNotOptimize = CloneModule(getProgram());
564efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
565efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner                                                 MiscompiledFunctions);
566be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
567be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "  Non-optimized portion: ";
568b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ToNotOptimize = swapProgramIn(ToNotOptimize);
569be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  EmitProgressBytecode("tonotoptimize", true);
570be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  setNewProgram(ToNotOptimize);   // Delete hacked module.
571be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
572be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  std::cout << "  Portion that is input to optimizer: ";
573b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner  ToOptimize = swapProgramIn(ToOptimize);
574be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  EmitProgressBytecode("tooptimize");
575be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  setNewProgram(ToOptimize);      // Delete hacked module.
5764a10645c70199c8d8567fbc46312158c419720abChris Lattner
5774a10645c70199c8d8567fbc46312158c419720abChris Lattner  return false;
5784a10645c70199c8d8567fbc46312158c419720abChris Lattner}
579d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
580a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// CleanupAndPrepareModules - Get the specified modules ready for code
581a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// generator testing.
5828c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
583a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerstatic void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
584a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                     Module *Safe) {
585a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Clean up the modules, removing extra cruft that we don't need anymore...
586a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Test = BD.performFinalCleanups(Test);
587a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
588a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // If we are executing the JIT, we have several nasty issues to take care of.
589a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (!BD.isExecutingJIT()) return;
590a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
591a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // First, if the main function is in the Safe module, we must add a stub to
592a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // the Test module to call into it.  Thus, we create a new function `main'
593a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // which just calls the old one.
594a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (Function *oldMain = Safe->getNamedFunction("main"))
595a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    if (!oldMain->isExternal()) {
596a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Rename it
597a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      oldMain->setName("llvm_bugpoint_old_main");
598a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Create a NEW `main' function with same type in the test module.
599a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      Function *newMain = new Function(oldMain->getFunctionType(),
600a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                       GlobalValue::ExternalLinkage,
601a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                       "main", Test);
602a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Create an `oldmain' prototype in the test module, which will
603a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // corresponds to the real main function in the same module.
604a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      Function *oldMainProto = new Function(oldMain->getFunctionType(),
605a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                            GlobalValue::ExternalLinkage,
606a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                                            oldMain->getName(), Test);
607a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Set up and remember the argument list for the main function.
608a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      std::vector<Value*> args;
609a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      for (Function::aiterator I = newMain->abegin(), E = newMain->aend(),
610a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner             OI = oldMain->abegin(); I != E; ++I, ++OI) {
611a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        I->setName(OI->getName());    // Copy argument names from oldMain
612a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        args.push_back(I);
613a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      }
614a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
615a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Call the old main function and return its result
616a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BasicBlock *BB = new BasicBlock("entry", newMain);
617a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      CallInst *call = new CallInst(oldMainProto, args);
618a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      BB->getInstList().push_back(call);
619a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
620a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // If the type of old function wasn't void, return value of call
621a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      new ReturnInst(oldMain->getReturnType() != Type::VoidTy ? call : 0, BB);
622a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    }
623a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
624a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // The second nasty issue we must deal with in the JIT is that the Safe
625a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // module cannot directly reference any functions defined in the test
626a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // module.  Instead, we use a JIT API call to dynamically resolve the
627a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // symbol.
628a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
629a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Add the resolver to the Safe module.
630a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Prototype: void *getPointerToNamedFunction(const char* Name)
631a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Function *resolverFunc =
632a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    Safe->getOrInsertFunction("getPointerToNamedFunction",
633a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                              PointerType::get(Type::SByteTy),
634a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                              PointerType::get(Type::SByteTy), 0);
635a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
636a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Use the function we just added to get addresses of functions we need.
637dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman  for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
638a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    if (F->isExternal() && !F->use_empty() && &*F != resolverFunc &&
639a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        F->getIntrinsicID() == 0 /* ignore intrinsics */) {
640dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman      Function *TestFn = Test->getFunction(F->getName(), F->getFunctionType());
641a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
642a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      // Don't forward functions which are external in the test module too.
643a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      if (TestFn && !TestFn->isExternal()) {
644a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // 1. Add a string constant with its name to the global file
645a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        Constant *InitArray = ConstantArray::get(F->getName());
646a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        GlobalVariable *funcName =
647a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner          new GlobalVariable(InitArray->getType(), true /*isConstant*/,
648a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                             GlobalValue::InternalLinkage, InitArray,
649a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner                             F->getName() + "_name", Safe);
650a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
651a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
652a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // sbyte* so it matches the signature of the resolver function.
653a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
654a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        // GetElementPtr *funcName, ulong 0, ulong 0
655a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::IntTy));
656a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        Value *GEP =
657518310cb0d136906ff0a99d7a24cb460794de5bfReid Spencer          ConstantExpr::getGetElementPtr(funcName, GEPargs);
658a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        std::vector<Value*> ResolverArgs;
659a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner        ResolverArgs.push_back(GEP);
660a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
661de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman        // Rewrite uses of F in global initializers, etc. to uses of a wrapper
662de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman        // function that dynamically resolves the calls to F via our JIT API
663de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman        if (F->use_begin() != F->use_end()) {
664de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // Construct a new stub function that will re-route calls to F
665dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          const FunctionType *FuncTy = F->getFunctionType();
666de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          Function *FuncWrapper = new Function(FuncTy,
667de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman                                               GlobalValue::InternalLinkage,
668dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman                                               F->getName() + "_wrapper",
669dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman                                               F->getParent());
670dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          BasicBlock *Header = new BasicBlock("header", FuncWrapper);
671dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman
672de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // Resolve the call to function F via the JIT API:
673de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          //
674de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // call resolver(GetElementPtr...)
675de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          CallInst *resolve = new CallInst(resolverFunc, ResolverArgs,
676de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman                                           "resolver");
677de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          Header->getInstList().push_back(resolve);
678de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // cast the result from the resolver to correctly-typed function
679de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          CastInst *castResolver =
680de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman            new CastInst(resolve, PointerType::get(F->getFunctionType()),
681de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman                         "resolverCast");
682de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          Header->getInstList().push_back(castResolver);
683de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman
684dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          // Save the argument list
685dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          std::vector<Value*> Args;
686dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          for (Function::aiterator i = FuncWrapper->abegin(),
687dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman                 e = FuncWrapper->aend(); i != e; ++i)
688dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Args.push_back(i);
689dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman
690dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          // Pass on the arguments to the real function, return its result
691dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          if (F->getReturnType() == Type::VoidTy) {
692de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman            CallInst *Call = new CallInst(castResolver, Args);
693dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Call);
694dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            ReturnInst *Ret = new ReturnInst();
695dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Ret);
696dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          } else {
697de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman            CallInst *Call = new CallInst(castResolver, Args, "redir");
698dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Call);
699dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            ReturnInst *Ret = new ReturnInst(Call);
700dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman            Header->getInstList().push_back(Ret);
701dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman          }
702dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman
703de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          // Use the wrapper function instead of the old function
704de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman          F->replaceAllUsesWith(FuncWrapper);
705dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman        }
706a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner      }
707a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    }
708a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
709a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
710a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (verifyModule(*Test) || verifyModule(*Safe)) {
711a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Bugpoint has a bug, which corrupted a module!!\n";
712a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    abort();
713a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
714a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
715a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
716a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
717a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
718a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// TestCodeGenerator - This is the predicate function used to check to see if
719a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// the "Test" portion of the program is miscompiled by the code generator under
720a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// test.  If so, return true.  In any case, both module arguments are deleted.
7218c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
722a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerstatic bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) {
723a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  CleanupAndPrepareModules(BD, Test, Safe);
724a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
725a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
726a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (BD.writeProgramToFile(TestModuleBC, Test)) {
727a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
728a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
729a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
730a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete Test;
731a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
732a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Make the shared library
733a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
734a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
735a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (BD.writeProgramToFile(SafeModuleBC, Safe)) {
736a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
737a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
738a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
739a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SharedObject = BD.compileSharedObject(SafeModuleBC);
740a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete Safe;
741a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
742a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Run the code generator on the `Test' code, loading the shared library.
743a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // The function returns whether or not the new output differs from reference.
744a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
745a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
746a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (Result)
747a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << ": still failing!\n";
748a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  else
749a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << ": didn't fail.\n";
750a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(TestModuleBC);
751a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(SafeModuleBC);
752a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  removeFile(SharedObject);
753a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
754a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  return Result;
755a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
756a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
757a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
7588c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
7598c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman///
760a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerbool BugDriver::debugCodeGenerator() {
761a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if ((void*)cbe == (void*)Interpreter) {
762a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::string Result = executeProgramWithCBE("bugpoint.cbe.out");
763a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "\n*** The C backend cannot match the reference diff, but it "
764a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << "is used as the 'known good'\n    code generator, so I can't"
765a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << " debug it.  Perhaps you have a front-end problem?\n    As a"
766a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << " sanity check, I left the result of executing the program "
767a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << "with the C backend\n    in this file for you: '"
768a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << Result << "'.\n";
769a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    return true;
770a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
771a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
772a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  DisambiguateGlobalSymbols(Program);
773a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
774a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator);
775a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
776a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Split the module into the two halves of the program we want.
777a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Module *ToNotCodeGen = CloneModule(getProgram());
778a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs);
779a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
780a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Condition the modules
781a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
782a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
783a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
784a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (writeProgramToFile(TestModuleBC, ToCodeGen)) {
785a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
786a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
787a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
788a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete ToCodeGen;
789a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
790a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  // Make the shared library
791a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
792a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (writeProgramToFile(SafeModuleBC, ToNotCodeGen)) {
793a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
794a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    exit(1);
795a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
796a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::string SharedObject = compileSharedObject(SafeModuleBC);
797a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  delete ToNotCodeGen;
798a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
799a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::cout << "You can reproduce the problem with the command line: \n";
800a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  if (isExecutingJIT()) {
801a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  lli -load " << SharedObject << " " << TestModuleBC;
802a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  } else {
803a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
804a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  gcc " << SharedObject << " " << TestModuleBC
805a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner              << ".s -o " << TestModuleBC << ".exe -Wl,-R.\n";
806a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << "  " << TestModuleBC << ".exe";
807a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
808a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
809a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    std::cout << " " << InputArgv[i];
810eed80e23751ecc50c1fa5604f67be4b826d5b417Misha Brukman  std::cout << '\n';
811a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  std::cout << "The shared object was created with:\n  llc -march=c "
812a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << SafeModuleBC << " -o temporary.c\n"
813a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << "  gcc -xc temporary.c -O2 -o " << SharedObject
814a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
815a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -G"            // Compile a shared library, `-G' for Sparc
816a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#else
817a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -shared"       // `-shared' for Linux/X86, maybe others
818a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#endif
819a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner            << " -fno-strict-aliasing\n";
820a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
821a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  return false;
822a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner}
823