blob: 88bbe228d54be6686235ec63296ef0ad4fa3b8f6 [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- Miscompilation.cpp - Debug program miscompilations -----------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a106452002-12-23 23:50:16 +00009//
Chris Lattnera57d86b2004-04-05 22:58:16 +000010// This file implements optimizer and code generation miscompilation debugging
11// support.
Chris Lattner4a106452002-12-23 23:50:16 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "BugDriver.h"
Chris Lattner126840f2003-04-24 20:16:29 +000016#include "ListReducer.h"
Daniel Dunbarca740962009-08-18 03:35:57 +000017#include "ToolRunner.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000018#include "llvm/Analysis/Verifier.h"
19#include "llvm/Config/config.h" // for HAVE_LINK_R
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/Module.h"
Reid Spencer605b9e22004-11-14 23:00:08 +000024#include "llvm/Linker.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000025#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/FileUtilities.h"
Rafael Espindolaf656a1d2013-06-17 19:21:38 +000028#include "llvm/Support/PathV1.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000029#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattnerfa761832004-01-14 03:38:37 +000030using namespace llvm;
Chris Lattner4a106452002-12-23 23:50:16 +000031
Chris Lattnera57d86b2004-04-05 22:58:16 +000032namespace llvm {
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +000033 extern cl::opt<std::string> OutputPrefix;
Chris Lattnera57d86b2004-04-05 22:58:16 +000034 extern cl::list<std::string> InputArgv;
35}
36
Chris Lattnerefdc0b52004-03-14 20:50:42 +000037namespace {
Michael J. Spencer56584fc2011-03-31 13:06:39 +000038 static llvm::cl::opt<bool>
39 DisableLoopExtraction("disable-loop-extraction",
Reid Spencerdc31a8a2006-11-11 19:05:02 +000040 cl::desc("Don't extract loops when searching for miscompilations"),
41 cl::init(false));
Michael J. Spencer56584fc2011-03-31 13:06:39 +000042 static llvm::cl::opt<bool>
43 DisableBlockExtraction("disable-block-extraction",
David Goodwin265d82e2009-07-28 23:08:36 +000044 cl::desc("Don't extract blocks when searching for miscompilations"),
45 cl::init(false));
Reid Spencerdc31a8a2006-11-11 19:05:02 +000046
Rafael Espindola8261dfe2010-08-08 03:55:08 +000047 class ReduceMiscompilingPasses : public ListReducer<std::string> {
Chris Lattnerfa761832004-01-14 03:38:37 +000048 BugDriver &BD;
49 public:
50 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000051
Rafael Espindola8261dfe2010-08-08 03:55:08 +000052 virtual TestResult doTest(std::vector<std::string> &Prefix,
53 std::vector<std::string> &Suffix,
Nick Lewycky22ff7482010-04-12 05:08:25 +000054 std::string &Error);
Chris Lattnerfa761832004-01-14 03:38:37 +000055 };
56}
Chris Lattner640f22e2003-04-24 17:02:17 +000057
Misha Brukman8c194ea2004-04-21 18:36:43 +000058/// TestResult - After passes have been split into a test group and a control
59/// group, see if they still break the program.
60///
Chris Lattner640f22e2003-04-24 17:02:17 +000061ReduceMiscompilingPasses::TestResult
Rafael Espindola8261dfe2010-08-08 03:55:08 +000062ReduceMiscompilingPasses::doTest(std::vector<std::string> &Prefix,
63 std::vector<std::string> &Suffix,
Nick Lewycky22ff7482010-04-12 05:08:25 +000064 std::string &Error) {
Chris Lattner06943ad2003-04-25 03:16:05 +000065 // First, run the program with just the Suffix passes. If it is still broken
Chris Lattner640f22e2003-04-24 17:02:17 +000066 // with JUST the kept passes, discard the prefix passes.
Dan Gohmanac95cc72009-07-16 15:30:09 +000067 outs() << "Checking to see if '" << getPassesString(Suffix)
68 << "' compiles correctly: ";
Chris Lattner640f22e2003-04-24 17:02:17 +000069
Gabor Greif8ff70c22007-07-04 21:55:50 +000070 std::string BitcodeResult;
Rafael Espindolaca356af2010-08-05 00:29:04 +000071 if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false/*delete*/,
72 true/*quiet*/)) {
Dan Gohman65f57c22009-07-15 16:35:29 +000073 errs() << " Error running this sequence of passes"
74 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +000075 BD.setPassesToRun(Suffix);
Rafael Espindolabae1b712010-07-28 18:12:30 +000076 BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +000077 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000078 }
Michael J. Spencer56584fc2011-03-31 13:06:39 +000079
Chris Lattner640f22e2003-04-24 17:02:17 +000080 // Check to see if the finished program matches the reference output...
Rafael Espindola10757dd2010-07-30 14:19:00 +000081 bool Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "",
82 true /*delete bitcode*/, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +000083 if (!Error.empty())
84 return InternalError;
85 if (Diff) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000086 outs() << " nope.\n";
Chris Lattner59615f02005-01-15 00:07:19 +000087 if (Suffix.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +000088 errs() << BD.getToolName() << ": I'm confused: the test fails when "
89 << "no passes are run, nondeterministic program?\n";
Chris Lattner59615f02005-01-15 00:07:19 +000090 exit(1);
91 }
Misha Brukman123f8fe2004-04-22 20:02:09 +000092 return KeepSuffix; // Miscompilation detected!
Chris Lattner640f22e2003-04-24 17:02:17 +000093 }
Dan Gohmanac95cc72009-07-16 15:30:09 +000094 outs() << " yup.\n"; // No miscompilation!
Chris Lattner640f22e2003-04-24 17:02:17 +000095
96 if (Prefix.empty()) return NoFailure;
97
Chris Lattner06943ad2003-04-25 03:16:05 +000098 // Next, see if the program is broken if we run the "prefix" passes first,
Misha Brukmanbc0e9982003-07-14 17:20:40 +000099 // then separately run the "kept" passes.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000100 outs() << "Checking to see if '" << getPassesString(Prefix)
101 << "' compiles correctly: ";
Chris Lattner640f22e2003-04-24 17:02:17 +0000102
103 // If it is not broken with the kept passes, it's possible that the prefix
104 // passes must be run before the kept passes to break it. If the program
105 // WORKS after the prefix passes, but then fails if running the prefix AND
Gabor Greif8ff70c22007-07-04 21:55:50 +0000106 // kept passes, we can update our bitcode file to include the result of the
Chris Lattner640f22e2003-04-24 17:02:17 +0000107 // prefix passes, then discard the prefix passes.
108 //
Rafael Espindolaca356af2010-08-05 00:29:04 +0000109 if (BD.runPasses(BD.getProgram(), Prefix, BitcodeResult, false/*delete*/,
110 true/*quiet*/)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000111 errs() << " Error running this sequence of passes"
112 << " on the input program!\n";
Chris Lattner9c6cfe12003-10-17 23:03:16 +0000113 BD.setPassesToRun(Prefix);
Rafael Espindolabae1b712010-07-28 18:12:30 +0000114 BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000115 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000116 }
117
118 // If the prefix maintains the predicate by itself, only keep the prefix!
Rafael Espindola10757dd2010-07-30 14:19:00 +0000119 Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", false, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000120 if (!Error.empty())
121 return InternalError;
122 if (Diff) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000123 outs() << " nope.\n";
Rafael Espindola74aec352013-06-17 21:50:28 +0000124 sys::fs::remove(BitcodeResult);
Chris Lattner640f22e2003-04-24 17:02:17 +0000125 return KeepPrefix;
126 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000127 outs() << " yup.\n"; // No miscompilation!
Chris Lattner640f22e2003-04-24 17:02:17 +0000128
129 // Ok, so now we know that the prefix passes work, try running the suffix
130 // passes on the result of the prefix passes.
131 //
Jeffrey Yasskin6865f292010-05-11 23:25:16 +0000132 OwningPtr<Module> PrefixOutput(ParseInputFile(BitcodeResult,
133 BD.getContext()));
David Blaikie453f4f02013-05-15 07:36:59 +0000134 if (!PrefixOutput) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000135 errs() << BD.getToolName() << ": Error reading bitcode file '"
136 << BitcodeResult << "'!\n";
Chris Lattner640f22e2003-04-24 17:02:17 +0000137 exit(1);
138 }
Rafael Espindola74aec352013-06-17 21:50:28 +0000139 sys::fs::remove(BitcodeResult);
Chris Lattnerf4789e62004-04-23 20:36:51 +0000140
141 // Don't check if there are no passes in the suffix.
142 if (Suffix.empty())
143 return NoFailure;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000144
Dan Gohmanac95cc72009-07-16 15:30:09 +0000145 outs() << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +0000146 << "' passes compile correctly after the '"
147 << getPassesString(Prefix) << "' passes: ";
148
Jeffrey Yasskin6865f292010-05-11 23:25:16 +0000149 OwningPtr<Module> OriginalInput(BD.swapProgramIn(PrefixOutput.take()));
Rafael Espindolaca356af2010-08-05 00:29:04 +0000150 if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false/*delete*/,
151 true/*quiet*/)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000152 errs() << " Error running this sequence of passes"
153 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000154 BD.setPassesToRun(Suffix);
Rafael Espindolabae1b712010-07-28 18:12:30 +0000155 BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000156 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000157 }
158
159 // Run the result...
Rafael Espindola10757dd2010-07-30 14:19:00 +0000160 Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "",
161 true /*delete bitcode*/, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000162 if (!Error.empty())
163 return InternalError;
164 if (Diff) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000165 outs() << " nope.\n";
Chris Lattner06943ad2003-04-25 03:16:05 +0000166 return KeepSuffix;
Chris Lattner640f22e2003-04-24 17:02:17 +0000167 }
168
169 // Otherwise, we must not be running the bad pass anymore.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000170 outs() << " yup.\n"; // No miscompilation!
Jeffrey Yasskin6865f292010-05-11 23:25:16 +0000171 // Restore orig program & free test.
172 delete BD.swapProgramIn(OriginalInput.take());
Chris Lattner640f22e2003-04-24 17:02:17 +0000173 return NoFailure;
174}
175
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000176namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +0000177 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
178 BugDriver &BD;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000179 bool (*TestFn)(BugDriver &, Module *, Module *, std::string &);
Chris Lattnerfa761832004-01-14 03:38:37 +0000180 public:
Chris Lattnerb15825b2004-04-05 21:37:38 +0000181 ReduceMiscompilingFunctions(BugDriver &bd,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000182 bool (*F)(BugDriver &, Module *, Module *,
183 std::string &))
Chris Lattnerb15825b2004-04-05 21:37:38 +0000184 : BD(bd), TestFn(F) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000185
Chris Lattnerfa761832004-01-14 03:38:37 +0000186 virtual TestResult doTest(std::vector<Function*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000187 std::vector<Function*> &Suffix,
188 std::string &Error) {
189 if (!Suffix.empty()) {
190 bool Ret = TestFuncs(Suffix, Error);
191 if (!Error.empty())
192 return InternalError;
193 if (Ret)
194 return KeepSuffix;
195 }
196 if (!Prefix.empty()) {
197 bool Ret = TestFuncs(Prefix, Error);
198 if (!Error.empty())
199 return InternalError;
200 if (Ret)
201 return KeepPrefix;
202 }
Chris Lattnerfa761832004-01-14 03:38:37 +0000203 return NoFailure;
204 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000205
Rafael Espindola84ae2062010-07-26 00:07:51 +0000206 bool TestFuncs(const std::vector<Function*> &Prefix, std::string &Error);
Chris Lattnerfa761832004-01-14 03:38:37 +0000207 };
208}
Chris Lattner640f22e2003-04-24 17:02:17 +0000209
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000210/// TestMergedProgram - Given two modules, link them together and run the
Rafael Espindola13793262010-07-31 14:34:49 +0000211/// program, checking to see if the program matches the diff. If there is
212/// an error, return NULL. If not, return the merged module. The Broken argument
213/// will be set to true if the output is different. If the DeleteInputs
214/// argument is set to true then this function deletes both input
215/// modules before it returns.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000216///
Rafael Espindola13793262010-07-31 14:34:49 +0000217static Module *TestMergedProgram(const BugDriver &BD, Module *M1, Module *M2,
218 bool DeleteInputs, std::string &Error,
219 bool &Broken) {
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000220 // Link the two portions of the program back to together.
221 std::string ErrorMsg;
Chris Lattner90c18c52004-11-16 06:31:38 +0000222 if (!DeleteInputs) {
223 M1 = CloneModule(M1);
224 M2 = CloneModule(M2);
225 }
Tanya Lattnerf1f1a4f2011-10-11 00:24:54 +0000226 if (Linker::LinkModules(M1, M2, Linker::DestroySource, &ErrorMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000227 errs() << BD.getToolName() << ": Error linking modules together:"
228 << ErrorMsg << '\n';
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000229 exit(1);
230 }
Chris Lattner90c18c52004-11-16 06:31:38 +0000231 delete M2; // We are done with this module.
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000232
Rafael Espindola13793262010-07-31 14:34:49 +0000233 // Execute the program.
234 Broken = BD.diffProgram(M1, "", "", false, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000235 if (!Error.empty()) {
Rafael Espindola10757dd2010-07-30 14:19:00 +0000236 // Delete the linked module
237 delete M1;
Rafael Espindola13793262010-07-31 14:34:49 +0000238 return NULL;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000239 }
Rafael Espindola13793262010-07-31 14:34:49 +0000240 return M1;
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000241}
242
Misha Brukman8c194ea2004-04-21 18:36:43 +0000243/// TestFuncs - split functions in a Module into two groups: those that are
244/// under consideration for miscompilation vs. those that are not, and test
245/// accordingly. Each group of functions becomes a separate Module.
246///
Rafael Espindola84ae2062010-07-26 00:07:51 +0000247bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
248 std::string &Error) {
Chris Lattner640f22e2003-04-24 17:02:17 +0000249 // Test to see if the function is misoptimized if we ONLY run it on the
250 // functions listed in Funcs.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000251 outs() << "Checking to see if the program is misoptimized when "
252 << (Funcs.size()==1 ? "this function is" : "these functions are")
253 << " run through the pass"
254 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000255 PrintFunctionList(Funcs);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000256 outs() << '\n';
Chris Lattner640f22e2003-04-24 17:02:17 +0000257
Rafael Espindola84ae2062010-07-26 00:07:51 +0000258 // Create a clone for two reasons:
259 // * If the optimization passes delete any function, the deleted function
260 // will be in the clone and Funcs will still point to valid memory
261 // * If the optimization passes use interprocedural information to break
262 // a function, we want to continue with the original function. Otherwise
263 // we can conclude that a function triggers the bug when in fact one
264 // needs a larger set of original functions to do so.
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000265 ValueToValueMapTy VMap;
Rafael Espindola84ae2062010-07-26 00:07:51 +0000266 Module *Clone = CloneModule(BD.getProgram(), VMap);
267 Module *Orig = BD.swapProgramIn(Clone);
268
269 std::vector<Function*> FuncsOnClone;
270 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
271 Function *F = cast<Function>(VMap[Funcs[i]]);
272 FuncsOnClone.push_back(F);
273 }
274
275 // Split the module into the two halves of the program we want.
276 VMap.clear();
Devang Patele9916a32010-06-24 00:33:28 +0000277 Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap);
Rafael Espindola84ae2062010-07-26 00:07:51 +0000278 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, FuncsOnClone,
Devang Patele9916a32010-06-24 00:33:28 +0000279 VMap);
Chris Lattner640f22e2003-04-24 17:02:17 +0000280
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000281 // Run the predicate, note that the predicate will delete both input modules.
Rafael Espindola84ae2062010-07-26 00:07:51 +0000282 bool Broken = TestFn(BD, ToOptimize, ToNotOptimize, Error);
283
284 delete BD.swapProgramIn(Orig);
285
286 return Broken;
Chris Lattner640f22e2003-04-24 17:02:17 +0000287}
288
Chris Lattner8abfb8a2010-01-16 21:34:01 +0000289/// DisambiguateGlobalSymbols - Give anonymous global values names.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000290///
Chris Lattner36ee07f2004-04-11 23:52:35 +0000291static void DisambiguateGlobalSymbols(Module *M) {
Chris Lattner67ef9e42006-05-04 23:35:31 +0000292 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
Chris Lattner1ffb33d2010-01-16 21:34:51 +0000293 I != E; ++I)
Chris Lattner8abfb8a2010-01-16 21:34:01 +0000294 if (!I->hasName())
295 I->setName("anon_global");
Chris Lattner1ffb33d2010-01-16 21:34:51 +0000296 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner8abfb8a2010-01-16 21:34:01 +0000297 if (!I->hasName())
298 I->setName("anon_fn");
Chris Lattner36ee07f2004-04-11 23:52:35 +0000299}
300
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000301/// ExtractLoops - Given a reduced list of functions that still exposed the bug,
302/// check to see if we can extract the loops in the region without obscuring the
303/// bug. If so, it reduces the amount of code identified.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000304///
Chris Lattnerb15825b2004-04-05 21:37:38 +0000305static bool ExtractLoops(BugDriver &BD,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000306 bool (*TestFn)(BugDriver &, Module *, Module *,
307 std::string &),
308 std::vector<Function*> &MiscompiledFunctions,
309 std::string &Error) {
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000310 bool MadeChange = false;
311 while (1) {
Chris Lattneraed98fa2005-08-02 23:25:56 +0000312 if (BugpointIsInterrupted) return MadeChange;
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000313
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000314 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +0000315 Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap);
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000316 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Dan Gohmand50330c2009-04-22 15:57:18 +0000317 MiscompiledFunctions,
Devang Patele9916a32010-06-24 00:33:28 +0000318 VMap);
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000319 Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
320 if (!ToOptimizeLoopExtracted) {
321 // If the loop extractor crashed or if there were no extractible loops,
322 // then this chapter of our odyssey is over with.
323 delete ToNotOptimize;
324 delete ToOptimize;
325 return MadeChange;
326 }
327
Dan Gohman65f57c22009-07-15 16:35:29 +0000328 errs() << "Extracted a loop from the breaking portion of the program.\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000329
330 // Bugpoint is intentionally not very trusting of LLVM transformations. In
331 // particular, we're not going to assume that the loop extractor works, so
332 // we're going to test the newly loop extracted program to make sure nothing
333 // has broken. If something broke, then we'll inform the user and stop
334 // extraction.
Dan Gohman70ef4492008-12-08 04:02:47 +0000335 AbstractInterpreter *AI = BD.switchToSafeInterpreter();
Rafael Espindola13793262010-07-31 14:34:49 +0000336 bool Failure;
337 Module *New = TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize,
338 false, Error, Failure);
339 if (!New)
Nick Lewycky22ff7482010-04-12 05:08:25 +0000340 return false;
Rafael Espindola13793262010-07-31 14:34:49 +0000341 // Delete the original and set the new program.
342 delete BD.swapProgramIn(New);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000343 if (Failure) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000344 BD.switchToInterpreter(AI);
345
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000346 // Merged program doesn't work anymore!
Dan Gohman65f57c22009-07-15 16:35:29 +0000347 errs() << " *** ERROR: Loop extraction broke the program. :("
348 << " Please report a bug!\n";
349 errs() << " Continuing on with un-loop-extracted version.\n";
Chris Lattner56c41862005-05-08 21:54:56 +0000350
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000351 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc",
352 ToNotOptimize);
353 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc",
354 ToOptimize);
355 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc",
Chris Lattner56c41862005-05-08 21:54:56 +0000356 ToOptimizeLoopExtracted);
357
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000358 errs() << "Please submit the "
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000359 << OutputPrefix << "-loop-extract-fail-*.bc files.\n";
Chris Lattner56c41862005-05-08 21:54:56 +0000360 delete ToOptimize;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000361 delete ToNotOptimize;
362 delete ToOptimizeLoopExtracted;
363 return MadeChange;
364 }
Chris Lattner56c41862005-05-08 21:54:56 +0000365 delete ToOptimize;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000366 BD.switchToInterpreter(AI);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000367
Dan Gohmanac95cc72009-07-16 15:30:09 +0000368 outs() << " Testing after loop extraction:\n";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000369 // Clone modules, the tester function will free them.
370 Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
371 Module *TNOBackup = CloneModule(ToNotOptimize);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000372 Failure = TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize, Error);
373 if (!Error.empty())
374 return false;
375 if (!Failure) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000376 outs() << "*** Loop extraction masked the problem. Undoing.\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000377 // If the program is not still broken, then loop extraction did something
378 // that masked the error. Stop loop extraction now.
Chris Lattnerb15825b2004-04-05 21:37:38 +0000379 delete TOLEBackup;
380 delete TNOBackup;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000381 return MadeChange;
382 }
Chris Lattnerb15825b2004-04-05 21:37:38 +0000383 ToOptimizeLoopExtracted = TOLEBackup;
384 ToNotOptimize = TNOBackup;
385
Dan Gohmanac95cc72009-07-16 15:30:09 +0000386 outs() << "*** Loop extraction successful!\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000387
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000388 std::vector<std::pair<std::string, FunctionType*> > MisCompFunctions;
Chris Lattner90c18c52004-11-16 06:31:38 +0000389 for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
390 E = ToOptimizeLoopExtracted->end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000391 if (!I->isDeclaration())
Chris Lattnerfa1af132004-11-19 07:09:40 +0000392 MisCompFunctions.push_back(std::make_pair(I->getName(),
393 I->getFunctionType()));
Chris Lattner90c18c52004-11-16 06:31:38 +0000394
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000395 // Okay, great! Now we know that we extracted a loop and that loop
396 // extraction both didn't break the program, and didn't mask the problem.
397 // Replace the current program with the loop extracted version, and try to
398 // extract another loop.
399 std::string ErrorMsg;
Tanya Lattnerf1f1a4f2011-10-11 00:24:54 +0000400 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted,
401 Linker::DestroySource, &ErrorMsg)){
Dan Gohman65f57c22009-07-15 16:35:29 +0000402 errs() << BD.getToolName() << ": Error linking modules together:"
403 << ErrorMsg << '\n';
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000404 exit(1);
405 }
Chris Lattner90c18c52004-11-16 06:31:38 +0000406 delete ToOptimizeLoopExtracted;
Chris Lattnerd3a533d2004-03-17 17:42:09 +0000407
408 // All of the Function*'s in the MiscompiledFunctions list are in the old
Chris Lattner5313f232004-04-02 06:32:17 +0000409 // module. Update this list to include all of the functions in the
410 // optimized and loop extracted module.
411 MiscompiledFunctions.clear();
Chris Lattner90c18c52004-11-16 06:31:38 +0000412 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000413 Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000414
Chris Lattner90c18c52004-11-16 06:31:38 +0000415 assert(NewF && "Function not found??");
416 MiscompiledFunctions.push_back(NewF);
Chris Lattnerd3a533d2004-03-17 17:42:09 +0000417 }
418
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000419 BD.setNewProgram(ToNotOptimize);
420 MadeChange = true;
421 }
422}
423
Chris Lattner5e783ab2004-05-11 21:54:13 +0000424namespace {
425 class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
426 BugDriver &BD;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000427 bool (*TestFn)(BugDriver &, Module *, Module *, std::string &);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000428 std::vector<Function*> FunctionsBeingTested;
429 public:
430 ReduceMiscompiledBlocks(BugDriver &bd,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000431 bool (*F)(BugDriver &, Module *, Module *,
432 std::string &),
Chris Lattner5e783ab2004-05-11 21:54:13 +0000433 const std::vector<Function*> &Fns)
434 : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000435
Chris Lattner5e783ab2004-05-11 21:54:13 +0000436 virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000437 std::vector<BasicBlock*> &Suffix,
438 std::string &Error) {
439 if (!Suffix.empty()) {
440 bool Ret = TestFuncs(Suffix, Error);
441 if (!Error.empty())
442 return InternalError;
443 if (Ret)
444 return KeepSuffix;
445 }
446 if (!Prefix.empty()) {
447 bool Ret = TestFuncs(Prefix, Error);
448 if (!Error.empty())
449 return InternalError;
450 if (Ret)
451 return KeepPrefix;
452 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000453 return NoFailure;
454 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000455
Nick Lewycky22ff7482010-04-12 05:08:25 +0000456 bool TestFuncs(const std::vector<BasicBlock*> &BBs, std::string &Error);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000457 };
458}
459
460/// TestFuncs - Extract all blocks for the miscompiled functions except for the
461/// specified blocks. If the problem still exists, return true.
462///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000463bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs,
464 std::string &Error) {
Chris Lattner5e783ab2004-05-11 21:54:13 +0000465 // Test to see if the function is misoptimized if we ONLY run it on the
466 // functions listed in Funcs.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000467 outs() << "Checking to see if the program is misoptimized when all ";
Chris Lattner68bee932004-05-12 16:08:01 +0000468 if (!BBs.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000469 outs() << "but these " << BBs.size() << " blocks are extracted: ";
Chris Lattner68bee932004-05-12 16:08:01 +0000470 for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
Dan Gohmanac95cc72009-07-16 15:30:09 +0000471 outs() << BBs[i]->getName() << " ";
472 if (BBs.size() > 10) outs() << "...";
Chris Lattner68bee932004-05-12 16:08:01 +0000473 } else {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000474 outs() << "blocks are extracted.";
Chris Lattner68bee932004-05-12 16:08:01 +0000475 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000476 outs() << '\n';
Chris Lattner5e783ab2004-05-11 21:54:13 +0000477
478 // Split the module into the two halves of the program we want.
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000479 ValueToValueMapTy VMap;
Rafael Espindola115a9322010-07-29 14:20:59 +0000480 Module *Clone = CloneModule(BD.getProgram(), VMap);
481 Module *Orig = BD.swapProgramIn(Clone);
482 std::vector<Function*> FuncsOnClone;
483 std::vector<BasicBlock*> BBsOnClone;
484 for (unsigned i = 0, e = FunctionsBeingTested.size(); i != e; ++i) {
485 Function *F = cast<Function>(VMap[FunctionsBeingTested[i]]);
486 FuncsOnClone.push_back(F);
487 }
488 for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
489 BasicBlock *BB = cast<BasicBlock>(VMap[BBs[i]]);
490 BBsOnClone.push_back(BB);
491 }
492 VMap.clear();
493
Devang Patele9916a32010-06-24 00:33:28 +0000494 Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000495 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Rafael Espindola115a9322010-07-29 14:20:59 +0000496 FuncsOnClone,
Devang Patele9916a32010-06-24 00:33:28 +0000497 VMap);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000498
499 // Try the extraction. If it doesn't work, then the block extractor crashed
500 // or something, in which case bugpoint can't chase down this possibility.
Rafael Espindola115a9322010-07-29 14:20:59 +0000501 if (Module *New = BD.ExtractMappedBlocksFromModule(BBsOnClone, ToOptimize)) {
Chris Lattner5e783ab2004-05-11 21:54:13 +0000502 delete ToOptimize;
Rafael Espindola115a9322010-07-29 14:20:59 +0000503 // Run the predicate,
504 // note that the predicate will delete both input modules.
505 bool Ret = TestFn(BD, New, ToNotOptimize, Error);
506 delete BD.swapProgramIn(Orig);
507 return Ret;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000508 }
Rafael Espindola115a9322010-07-29 14:20:59 +0000509 delete BD.swapProgramIn(Orig);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000510 delete ToOptimize;
511 delete ToNotOptimize;
512 return false;
513}
514
515
516/// ExtractBlocks - Given a reduced list of functions that still expose the bug,
517/// extract as many basic blocks from the region as possible without obscuring
518/// the bug.
519///
520static bool ExtractBlocks(BugDriver &BD,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000521 bool (*TestFn)(BugDriver &, Module *, Module *,
522 std::string &),
523 std::vector<Function*> &MiscompiledFunctions,
524 std::string &Error) {
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000525 if (BugpointIsInterrupted) return false;
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000526
Chris Lattner5e783ab2004-05-11 21:54:13 +0000527 std::vector<BasicBlock*> Blocks;
528 for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
529 for (Function::iterator I = MiscompiledFunctions[i]->begin(),
530 E = MiscompiledFunctions[i]->end(); I != E; ++I)
531 Blocks.push_back(I);
532
533 // Use the list reducer to identify blocks that can be extracted without
534 // obscuring the bug. The Blocks list will end up containing blocks that must
535 // be retained from the original program.
536 unsigned OldSize = Blocks.size();
Chris Lattner68bee932004-05-12 16:08:01 +0000537
538 // Check to see if all blocks are extractible first.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000539 bool Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions)
540 .TestFuncs(std::vector<BasicBlock*>(), Error);
541 if (!Error.empty())
542 return false;
543 if (Ret) {
Chris Lattner68bee932004-05-12 16:08:01 +0000544 Blocks.clear();
545 } else {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000546 ReduceMiscompiledBlocks(BD, TestFn,
547 MiscompiledFunctions).reduceList(Blocks, Error);
548 if (!Error.empty())
549 return false;
Chris Lattner68bee932004-05-12 16:08:01 +0000550 if (Blocks.size() == OldSize)
551 return false;
552 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000553
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000554 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +0000555 Module *ProgClone = CloneModule(BD.getProgram(), VMap);
Chris Lattner2290e752004-05-12 02:43:24 +0000556 Module *ToExtract = SplitFunctionsOutOfModule(ProgClone,
Dan Gohmand50330c2009-04-22 15:57:18 +0000557 MiscompiledFunctions,
Devang Patele9916a32010-06-24 00:33:28 +0000558 VMap);
Chris Lattner2290e752004-05-12 02:43:24 +0000559 Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract);
560 if (Extracted == 0) {
Chris Lattnerda895d62005-02-27 06:18:25 +0000561 // Weird, extraction should have worked.
Dan Gohman65f57c22009-07-15 16:35:29 +0000562 errs() << "Nondeterministic problem extracting blocks??\n";
Chris Lattner2290e752004-05-12 02:43:24 +0000563 delete ProgClone;
564 delete ToExtract;
565 return false;
566 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000567
Chris Lattner2290e752004-05-12 02:43:24 +0000568 // Otherwise, block extraction succeeded. Link the two program fragments back
569 // together.
570 delete ToExtract;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000571
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000572 std::vector<std::pair<std::string, FunctionType*> > MisCompFunctions;
Chris Lattner90c18c52004-11-16 06:31:38 +0000573 for (Module::iterator I = Extracted->begin(), E = Extracted->end();
574 I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000575 if (!I->isDeclaration())
Chris Lattnerfa1af132004-11-19 07:09:40 +0000576 MisCompFunctions.push_back(std::make_pair(I->getName(),
577 I->getFunctionType()));
Chris Lattner90c18c52004-11-16 06:31:38 +0000578
Chris Lattner2290e752004-05-12 02:43:24 +0000579 std::string ErrorMsg;
Tanya Lattnerf1f1a4f2011-10-11 00:24:54 +0000580 if (Linker::LinkModules(ProgClone, Extracted, Linker::DestroySource,
581 &ErrorMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000582 errs() << BD.getToolName() << ": Error linking modules together:"
583 << ErrorMsg << '\n';
Chris Lattner2290e752004-05-12 02:43:24 +0000584 exit(1);
585 }
Chris Lattner90c18c52004-11-16 06:31:38 +0000586 delete Extracted;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000587
Chris Lattner2290e752004-05-12 02:43:24 +0000588 // Set the new program and delete the old one.
589 BD.setNewProgram(ProgClone);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000590
Chris Lattner2290e752004-05-12 02:43:24 +0000591 // Update the list of miscompiled functions.
592 MiscompiledFunctions.clear();
Chris Lattner5e783ab2004-05-11 21:54:13 +0000593
Chris Lattner90c18c52004-11-16 06:31:38 +0000594 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000595 Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first);
Chris Lattner90c18c52004-11-16 06:31:38 +0000596 assert(NewF && "Function not found??");
597 MiscompiledFunctions.push_back(NewF);
598 }
Chris Lattner2290e752004-05-12 02:43:24 +0000599
600 return true;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000601}
602
603
Chris Lattnerb15825b2004-04-05 21:37:38 +0000604/// DebugAMiscompilation - This is a generic driver to narrow down
605/// miscompilations, either in an optimization or a code generator.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000606///
Chris Lattnerb15825b2004-04-05 21:37:38 +0000607static std::vector<Function*>
608DebugAMiscompilation(BugDriver &BD,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000609 bool (*TestFn)(BugDriver &, Module *, Module *,
610 std::string &),
611 std::string &Error) {
Chris Lattnerb15825b2004-04-05 21:37:38 +0000612 // Okay, now that we have reduced the list of passes which are causing the
613 // failure, see if we can pin down which functions are being
614 // miscompiled... first build a list of all of the non-external functions in
615 // the program.
616 std::vector<Function*> MiscompiledFunctions;
617 Module *Prog = BD.getProgram();
618 for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000619 if (!I->isDeclaration())
Chris Lattnerb15825b2004-04-05 21:37:38 +0000620 MiscompiledFunctions.push_back(I);
621
622 // Do the reduction...
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000623 if (!BugpointIsInterrupted)
Nick Lewycky22ff7482010-04-12 05:08:25 +0000624 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
625 Error);
Andrew Trick7c863eb2011-05-11 16:31:24 +0000626 if (!Error.empty()) {
627 errs() << "\n***Cannot reduce functions: ";
Nick Lewycky22ff7482010-04-12 05:08:25 +0000628 return MiscompiledFunctions;
Andrew Trick7c863eb2011-05-11 16:31:24 +0000629 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000630 outs() << "\n*** The following function"
631 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
632 << " being miscompiled: ";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000633 PrintFunctionList(MiscompiledFunctions);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000634 outs() << '\n';
Chris Lattnerb15825b2004-04-05 21:37:38 +0000635
636 // See if we can rip any loops out of the miscompiled functions and still
637 // trigger the problem.
Reid Spencerdc31a8a2006-11-11 19:05:02 +0000638
Nick Lewycky22ff7482010-04-12 05:08:25 +0000639 if (!BugpointIsInterrupted && !DisableLoopExtraction) {
640 bool Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions, Error);
641 if (!Error.empty())
642 return MiscompiledFunctions;
643 if (Ret) {
644 // Okay, we extracted some loops and the problem still appears. See if
645 // we can eliminate some of the created functions from being candidates.
646 DisambiguateGlobalSymbols(BD.getProgram());
Chris Lattner36ee07f2004-04-11 23:52:35 +0000647
Nick Lewycky22ff7482010-04-12 05:08:25 +0000648 // Do the reduction...
649 if (!BugpointIsInterrupted)
650 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
651 Error);
652 if (!Error.empty())
653 return MiscompiledFunctions;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000654
Nick Lewycky22ff7482010-04-12 05:08:25 +0000655 outs() << "\n*** The following function"
656 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
657 << " being miscompiled: ";
658 PrintFunctionList(MiscompiledFunctions);
659 outs() << '\n';
660 }
Chris Lattnerb15825b2004-04-05 21:37:38 +0000661 }
662
Nick Lewycky22ff7482010-04-12 05:08:25 +0000663 if (!BugpointIsInterrupted && !DisableBlockExtraction) {
664 bool Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions, Error);
665 if (!Error.empty())
666 return MiscompiledFunctions;
667 if (Ret) {
668 // Okay, we extracted some blocks and the problem still appears. See if
669 // we can eliminate some of the created functions from being candidates.
670 DisambiguateGlobalSymbols(BD.getProgram());
Chris Lattner5e783ab2004-05-11 21:54:13 +0000671
Nick Lewycky22ff7482010-04-12 05:08:25 +0000672 // Do the reduction...
673 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
674 Error);
675 if (!Error.empty())
676 return MiscompiledFunctions;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000677
Nick Lewycky22ff7482010-04-12 05:08:25 +0000678 outs() << "\n*** The following function"
679 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
680 << " being miscompiled: ";
681 PrintFunctionList(MiscompiledFunctions);
682 outs() << '\n';
683 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000684 }
685
Chris Lattnerb15825b2004-04-05 21:37:38 +0000686 return MiscompiledFunctions;
687}
688
Chris Lattnera57d86b2004-04-05 22:58:16 +0000689/// TestOptimizer - This is the predicate function used to check to see if the
690/// "Test" portion of the program is misoptimized. If so, return true. In any
691/// case, both module arguments are deleted.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000692///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000693static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe,
694 std::string &Error) {
Chris Lattnerb15825b2004-04-05 21:37:38 +0000695 // Run the optimization passes on ToOptimize, producing a transformed version
696 // of the functions being tested.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000697 outs() << " Optimizing functions being tested: ";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000698 Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
699 /*AutoDebugCrashes*/true);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000700 outs() << "done.\n";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000701 delete Test;
702
Dan Gohmanac95cc72009-07-16 15:30:09 +0000703 outs() << " Checking to see if the merged program executes correctly: ";
Rafael Espindola13793262010-07-31 14:34:49 +0000704 bool Broken;
705 Module *New = TestMergedProgram(BD, Optimized, Safe, true, Error, Broken);
706 if (New) {
707 outs() << (Broken ? " nope.\n" : " yup.\n");
708 // Delete the original and set the new program.
709 delete BD.swapProgramIn(New);
710 }
Chris Lattnerb15825b2004-04-05 21:37:38 +0000711 return Broken;
712}
713
714
Chris Lattner4a106452002-12-23 23:50:16 +0000715/// debugMiscompilation - This method is used when the passes selected are not
716/// crashing, but the generated output is semantically different from the
717/// input.
718///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000719void BugDriver::debugMiscompilation(std::string *Error) {
Chris Lattner4a106452002-12-23 23:50:16 +0000720 // Make sure something was miscompiled...
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000721 if (!BugpointIsInterrupted)
Nick Lewycky22ff7482010-04-12 05:08:25 +0000722 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun, *Error)) {
723 if (Error->empty())
724 errs() << "*** Optimized program matches reference output! No problem"
725 << " detected...\nbugpoint can't help you with your problem!\n";
726 return;
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000727 }
Chris Lattner4a106452002-12-23 23:50:16 +0000728
Dan Gohmanac95cc72009-07-16 15:30:09 +0000729 outs() << "\n*** Found miscompiling pass"
730 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
731 << getPassesString(getPassesToRun()) << '\n';
Rafael Espindolabae1b712010-07-28 18:12:30 +0000732 EmitProgressBitcode(Program, "passinput");
Chris Lattner4a106452002-12-23 23:50:16 +0000733
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000734 std::vector<Function *> MiscompiledFunctions =
Nick Lewycky22ff7482010-04-12 05:08:25 +0000735 DebugAMiscompilation(*this, TestOptimizer, *Error);
736 if (!Error->empty())
737 return;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000738
Gabor Greif8ff70c22007-07-04 21:55:50 +0000739 // Output a bunch of bitcode files for the user...
Dan Gohmanac95cc72009-07-16 15:30:09 +0000740 outs() << "Outputting reduced bitcode files which expose the problem:\n";
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000741 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +0000742 Module *ToNotOptimize = CloneModule(getProgram(), VMap);
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000743 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Dan Gohmand50330c2009-04-22 15:57:18 +0000744 MiscompiledFunctions,
Devang Patele9916a32010-06-24 00:33:28 +0000745 VMap);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000746
Dan Gohmanac95cc72009-07-16 15:30:09 +0000747 outs() << " Non-optimized portion: ";
Rafael Espindolabae1b712010-07-28 18:12:30 +0000748 EmitProgressBitcode(ToNotOptimize, "tonotoptimize", true);
749 delete ToNotOptimize; // Delete hacked module.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000750
Dan Gohmanac95cc72009-07-16 15:30:09 +0000751 outs() << " Portion that is input to optimizer: ";
Rafael Espindolabae1b712010-07-28 18:12:30 +0000752 EmitProgressBitcode(ToOptimize, "tooptimize");
753 delete ToOptimize; // Delete hacked module.
Chris Lattner4a106452002-12-23 23:50:16 +0000754
Nick Lewycky22ff7482010-04-12 05:08:25 +0000755 return;
Chris Lattner4a106452002-12-23 23:50:16 +0000756}
Brian Gaeked0fde302003-11-11 22:41:34 +0000757
Chris Lattnera57d86b2004-04-05 22:58:16 +0000758/// CleanupAndPrepareModules - Get the specified modules ready for code
759/// generator testing.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000760///
Chris Lattnera57d86b2004-04-05 22:58:16 +0000761static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
762 Module *Safe) {
763 // Clean up the modules, removing extra cruft that we don't need anymore...
764 Test = BD.performFinalCleanups(Test);
765
766 // If we are executing the JIT, we have several nasty issues to take care of.
767 if (!BD.isExecutingJIT()) return;
768
769 // First, if the main function is in the Safe module, we must add a stub to
770 // the Test module to call into it. Thus, we create a new function `main'
771 // which just calls the old one.
Reid Spencer688b0492007-02-05 21:19:13 +0000772 if (Function *oldMain = Safe->getFunction("main"))
Reid Spencer5cbf9852007-01-30 20:08:39 +0000773 if (!oldMain->isDeclaration()) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000774 // Rename it
775 oldMain->setName("llvm_bugpoint_old_main");
776 // Create a NEW `main' function with same type in the test module.
Gabor Greif051a9502008-04-06 20:25:17 +0000777 Function *newMain = Function::Create(oldMain->getFunctionType(),
778 GlobalValue::ExternalLinkage,
779 "main", Test);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000780 // Create an `oldmain' prototype in the test module, which will
781 // corresponds to the real main function in the same module.
Gabor Greif051a9502008-04-06 20:25:17 +0000782 Function *oldMainProto = Function::Create(oldMain->getFunctionType(),
783 GlobalValue::ExternalLinkage,
784 oldMain->getName(), Test);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000785 // Set up and remember the argument list for the main function.
786 std::vector<Value*> args;
Alkis Evlogimenos5a1c58d2005-03-15 07:02:26 +0000787 for (Function::arg_iterator
788 I = newMain->arg_begin(), E = newMain->arg_end(),
789 OI = oldMain->arg_begin(); I != E; ++I, ++OI) {
Owen Anderson6bc41e82008-04-14 17:38:21 +0000790 I->setName(OI->getName()); // Copy argument names from oldMain
Chris Lattnera57d86b2004-04-05 22:58:16 +0000791 args.push_back(I);
792 }
793
794 // Call the old main function and return its result
Owen Anderson1d0be152009-08-13 21:58:54 +0000795 BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain);
Jay Foada3efbb12011-07-15 08:37:34 +0000796 CallInst *call = CallInst::Create(oldMainProto, args, "", BB);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000797
Chris Lattnera57d86b2004-04-05 22:58:16 +0000798 // If the type of old function wasn't void, return value of call
Owen Anderson1d0be152009-08-13 21:58:54 +0000799 ReturnInst::Create(Safe->getContext(), call, BB);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000800 }
801
802 // The second nasty issue we must deal with in the JIT is that the Safe
803 // module cannot directly reference any functions defined in the test
804 // module. Instead, we use a JIT API call to dynamically resolve the
805 // symbol.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000806
Chris Lattnera57d86b2004-04-05 22:58:16 +0000807 // Add the resolver to the Safe module.
808 // Prototype: void *getPointerToNamedFunction(const char* Name)
Chris Lattner2db43c82007-01-07 08:13:39 +0000809 Constant *resolverFunc =
Chris Lattnera57d86b2004-04-05 22:58:16 +0000810 Safe->getOrInsertFunction("getPointerToNamedFunction",
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000811 Type::getInt8PtrTy(Safe->getContext()),
812 Type::getInt8PtrTy(Safe->getContext()),
Owen Anderson1d0be152009-08-13 21:58:54 +0000813 (Type *)0);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000814
Chris Lattnera57d86b2004-04-05 22:58:16 +0000815 // Use the function we just added to get addresses of functions we need.
Misha Brukmandc7fef82004-04-19 01:12:01 +0000816 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000817 if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc &&
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000818 !F->isIntrinsic() /* ignore intrinsics */) {
Reid Spencer688b0492007-02-05 21:19:13 +0000819 Function *TestFn = Test->getFunction(F->getName());
Chris Lattnera57d86b2004-04-05 22:58:16 +0000820
821 // Don't forward functions which are external in the test module too.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000822 if (TestFn && !TestFn->isDeclaration()) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000823 // 1. Add a string constant with its name to the global file
Chris Lattner18c7f802012-02-05 02:29:43 +0000824 Constant *InitArray =
825 ConstantDataArray::getString(F->getContext(), F->getName());
Chris Lattnera57d86b2004-04-05 22:58:16 +0000826 GlobalVariable *funcName =
Owen Andersone9b11b42009-07-08 19:03:57 +0000827 new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
Misha Brukman3da94ae2005-04-22 00:00:37 +0000828 GlobalValue::InternalLinkage, InitArray,
Owen Andersone9b11b42009-07-08 19:03:57 +0000829 F->getName() + "_name");
Chris Lattnera57d86b2004-04-05 22:58:16 +0000830
831 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
832 // sbyte* so it matches the signature of the resolver function.
833
834 // GetElementPtr *funcName, ulong 0, ulong 0
Owen Anderson1d0be152009-08-13 21:58:54 +0000835 std::vector<Constant*> GEPargs(2,
836 Constant::getNullValue(Type::getInt32Ty(F->getContext())));
Jay Foaddab3d292011-07-21 14:31:17 +0000837 Value *GEP = ConstantExpr::getGetElementPtr(funcName, GEPargs);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000838 std::vector<Value*> ResolverArgs;
839 ResolverArgs.push_back(GEP);
840
Misha Brukmande4803d2004-04-19 03:36:47 +0000841 // Rewrite uses of F in global initializers, etc. to uses of a wrapper
842 // function that dynamically resolves the calls to F via our JIT API
Chris Lattnera3efca12005-07-12 01:00:32 +0000843 if (!F->use_empty()) {
844 // Create a new global to hold the cached function pointer.
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000845 Constant *NullPtr = ConstantPointerNull::get(F->getType());
Chris Lattnera3efca12005-07-12 01:00:32 +0000846 GlobalVariable *Cache =
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000847 new GlobalVariable(*F->getParent(), F->getType(),
Owen Andersone9b11b42009-07-08 19:03:57 +0000848 false, GlobalValue::InternalLinkage,
849 NullPtr,F->getName()+".fpcache");
Jeff Cohen00b168892005-07-27 06:12:32 +0000850
Misha Brukmande4803d2004-04-19 03:36:47 +0000851 // Construct a new stub function that will re-route calls to F
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000852 FunctionType *FuncTy = F->getFunctionType();
Gabor Greif051a9502008-04-06 20:25:17 +0000853 Function *FuncWrapper = Function::Create(FuncTy,
854 GlobalValue::InternalLinkage,
855 F->getName() + "_wrapper",
856 F->getParent());
Owen Anderson1d0be152009-08-13 21:58:54 +0000857 BasicBlock *EntryBB = BasicBlock::Create(F->getContext(),
858 "entry", FuncWrapper);
859 BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(),
860 "usecache", FuncWrapper);
861 BasicBlock *LookupBB = BasicBlock::Create(F->getContext(),
862 "lookupfp", FuncWrapper);
Jeff Cohen00b168892005-07-27 06:12:32 +0000863
Chris Lattnera3efca12005-07-12 01:00:32 +0000864 // Check to see if we already looked up the value.
865 Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB);
Owen Anderson333c4002009-07-09 23:48:35 +0000866 Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal,
867 NullPtr, "isNull");
Gabor Greif051a9502008-04-06 20:25:17 +0000868 BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000869
Misha Brukmande4803d2004-04-19 03:36:47 +0000870 // Resolve the call to function F via the JIT API:
871 //
872 // call resolver(GetElementPtr...)
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000873 CallInst *Resolver =
Jay Foada3efbb12011-07-15 08:37:34 +0000874 CallInst::Create(resolverFunc, ResolverArgs, "resolver", LookupBB);
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000875
876 // Cast the result from the resolver to correctly-typed function.
877 CastInst *CastedResolver =
878 new BitCastInst(Resolver,
Owen Andersondebcb012009-07-29 22:17:13 +0000879 PointerType::getUnqual(F->getFunctionType()),
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000880 "resolverCast", LookupBB);
Reid Spencer3da59db2006-11-27 01:05:10 +0000881
Chris Lattnera3efca12005-07-12 01:00:32 +0000882 // Save the value in our cache.
883 new StoreInst(CastedResolver, Cache, LookupBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000884 BranchInst::Create(DoCallBB, LookupBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000885
Jay Foad3ecfc862011-03-30 11:28:46 +0000886 PHINode *FuncPtr = PHINode::Create(NullPtr->getType(), 2,
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000887 "fp", DoCallBB);
Chris Lattnera3efca12005-07-12 01:00:32 +0000888 FuncPtr->addIncoming(CastedResolver, LookupBB);
889 FuncPtr->addIncoming(CachedVal, EntryBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000890
Chris Lattnera3efca12005-07-12 01:00:32 +0000891 // Save the argument list.
Misha Brukmandc7fef82004-04-19 01:12:01 +0000892 std::vector<Value*> Args;
Alkis Evlogimenos5a1c58d2005-03-15 07:02:26 +0000893 for (Function::arg_iterator i = FuncWrapper->arg_begin(),
894 e = FuncWrapper->arg_end(); i != e; ++i)
Misha Brukmandc7fef82004-04-19 01:12:01 +0000895 Args.push_back(i);
896
897 // Pass on the arguments to the real function, return its result
Dan Gohmane49a13e2010-06-07 20:19:26 +0000898 if (F->getReturnType()->isVoidTy()) {
Jay Foada3efbb12011-07-15 08:37:34 +0000899 CallInst::Create(FuncPtr, Args, "", DoCallBB);
Owen Anderson1d0be152009-08-13 21:58:54 +0000900 ReturnInst::Create(F->getContext(), DoCallBB);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000901 } else {
Jay Foada3efbb12011-07-15 08:37:34 +0000902 CallInst *Call = CallInst::Create(FuncPtr, Args,
Gabor Greif051a9502008-04-06 20:25:17 +0000903 "retval", DoCallBB);
Owen Anderson1d0be152009-08-13 21:58:54 +0000904 ReturnInst::Create(F->getContext(),Call, DoCallBB);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000905 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000906
Misha Brukmande4803d2004-04-19 03:36:47 +0000907 // Use the wrapper function instead of the old function
908 F->replaceAllUsesWith(FuncWrapper);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000909 }
Chris Lattnera57d86b2004-04-05 22:58:16 +0000910 }
911 }
912 }
913
914 if (verifyModule(*Test) || verifyModule(*Safe)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000915 errs() << "Bugpoint has a bug, which corrupted a module!!\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000916 abort();
917 }
918}
919
920
921
922/// TestCodeGenerator - This is the predicate function used to check to see if
923/// the "Test" portion of the program is miscompiled by the code generator under
924/// test. If so, return true. In any case, both module arguments are deleted.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000925///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000926static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe,
927 std::string &Error) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000928 CleanupAndPrepareModules(BD, Test, Safe);
929
Reid Spencer97182982004-12-15 01:53:08 +0000930 sys::Path TestModuleBC("bugpoint.test.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000931 std::string ErrMsg;
932 if (TestModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000933 errs() << BD.getToolName() << "Error making unique filename: "
934 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000935 exit(1);
936 }
Chris Lattner74382b72009-08-23 22:45:37 +0000937 if (BD.writeProgramToFile(TestModuleBC.str(), Test)) {
938 errs() << "Error writing bitcode to `" << TestModuleBC.str()
939 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000940 exit(1);
941 }
942 delete Test;
943
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000944 FileRemover TestModuleBCRemover(TestModuleBC.str(), !SaveTemps);
Rafael Espindolabc2ed592010-06-21 02:17:36 +0000945
Chris Lattnera57d86b2004-04-05 22:58:16 +0000946 // Make the shared library
Reid Spencer97182982004-12-15 01:53:08 +0000947 sys::Path SafeModuleBC("bugpoint.safe.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000948 if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000949 errs() << BD.getToolName() << "Error making unique filename: "
950 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000951 exit(1);
952 }
Chris Lattnera57d86b2004-04-05 22:58:16 +0000953
Chris Lattner74382b72009-08-23 22:45:37 +0000954 if (BD.writeProgramToFile(SafeModuleBC.str(), Safe)) {
955 errs() << "Error writing bitcode to `" << SafeModuleBC.str()
956 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000957 exit(1);
958 }
Rafael Espindolabc2ed592010-06-21 02:17:36 +0000959
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000960 FileRemover SafeModuleBCRemover(SafeModuleBC.str(), !SaveTemps);
Rafael Espindolabc2ed592010-06-21 02:17:36 +0000961
Nick Lewycky22ff7482010-04-12 05:08:25 +0000962 std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str(), Error);
963 if (!Error.empty())
Benjamin Kramer27063872010-04-12 12:22:19 +0000964 return false;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000965 delete Safe;
966
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000967 FileRemover SharedObjectRemover(SharedObject, !SaveTemps);
Rafael Espindolabc2ed592010-06-21 02:17:36 +0000968
Chris Lattnera57d86b2004-04-05 22:58:16 +0000969 // Run the code generator on the `Test' code, loading the shared library.
970 // The function returns whether or not the new output differs from reference.
Rafael Espindola10757dd2010-07-30 14:19:00 +0000971 bool Result = BD.diffProgram(BD.getProgram(), TestModuleBC.str(),
972 SharedObject, false, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000973 if (!Error.empty())
974 return false;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000975
976 if (Result)
Dan Gohman65f57c22009-07-15 16:35:29 +0000977 errs() << ": still failing!\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000978 else
Dan Gohman65f57c22009-07-15 16:35:29 +0000979 errs() << ": didn't fail.\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000980
981 return Result;
982}
983
984
Misha Brukman8c194ea2004-04-21 18:36:43 +0000985/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
986///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000987bool BugDriver::debugCodeGenerator(std::string *Error) {
Dan Gohman70ef4492008-12-08 04:02:47 +0000988 if ((void*)SafeInterpreter == (void*)Interpreter) {
Rafael Espindola10757dd2010-07-30 14:19:00 +0000989 std::string Result = executeProgramSafely(Program, "bugpoint.safe.out",
990 Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000991 if (Error->empty()) {
992 outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match "
993 << "the reference diff. This may be due to a\n front-end "
994 << "bug or a bug in the original program, but this can also "
995 << "happen if bugpoint isn't running the program with the "
996 << "right flags or input.\n I left the result of executing "
997 << "the program with the \"safe\" backend in this file for "
998 << "you: '"
999 << Result << "'.\n";
1000 }
Chris Lattnera57d86b2004-04-05 22:58:16 +00001001 return true;
1002 }
1003
1004 DisambiguateGlobalSymbols(Program);
1005
Nick Lewycky22ff7482010-04-12 05:08:25 +00001006 std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator,
1007 *Error);
1008 if (!Error->empty())
1009 return true;
Chris Lattnera57d86b2004-04-05 22:58:16 +00001010
1011 // Split the module into the two halves of the program we want.
Rafael Espindola1ed219a2010-10-13 01:36:30 +00001012 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +00001013 Module *ToNotCodeGen = CloneModule(getProgram(), VMap);
1014 Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs, VMap);
Chris Lattnera57d86b2004-04-05 22:58:16 +00001015
1016 // Condition the modules
1017 CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
1018
Reid Spencer97182982004-12-15 01:53:08 +00001019 sys::Path TestModuleBC("bugpoint.test.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +00001020 std::string ErrMsg;
1021 if (TestModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +00001022 errs() << getToolName() << "Error making unique filename: "
1023 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +00001024 exit(1);
1025 }
Reid Spencer97182982004-12-15 01:53:08 +00001026
Chris Lattner74382b72009-08-23 22:45:37 +00001027 if (writeProgramToFile(TestModuleBC.str(), ToCodeGen)) {
1028 errs() << "Error writing bitcode to `" << TestModuleBC.str()
1029 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001030 exit(1);
1031 }
1032 delete ToCodeGen;
1033
1034 // Make the shared library
Reid Spencer97182982004-12-15 01:53:08 +00001035 sys::Path SafeModuleBC("bugpoint.safe.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +00001036 if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +00001037 errs() << getToolName() << "Error making unique filename: "
1038 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +00001039 exit(1);
1040 }
Reid Spencer97182982004-12-15 01:53:08 +00001041
Chris Lattner74382b72009-08-23 22:45:37 +00001042 if (writeProgramToFile(SafeModuleBC.str(), ToNotCodeGen)) {
1043 errs() << "Error writing bitcode to `" << SafeModuleBC.str()
1044 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001045 exit(1);
1046 }
Nick Lewycky22ff7482010-04-12 05:08:25 +00001047 std::string SharedObject = compileSharedObject(SafeModuleBC.str(), *Error);
1048 if (!Error->empty())
1049 return true;
Chris Lattnera57d86b2004-04-05 22:58:16 +00001050 delete ToNotCodeGen;
1051
Dan Gohmanac95cc72009-07-16 15:30:09 +00001052 outs() << "You can reproduce the problem with the command line: \n";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001053 if (isExecutingJIT()) {
Chris Lattner74382b72009-08-23 22:45:37 +00001054 outs() << " lli -load " << SharedObject << " " << TestModuleBC.str();
Chris Lattnera57d86b2004-04-05 22:58:16 +00001055 } else {
Nick Lewycky8d0e1bc2010-04-29 23:37:44 +00001056 outs() << " llc " << TestModuleBC.str() << " -o " << TestModuleBC.str()
Chris Lattner74382b72009-08-23 22:45:37 +00001057 << ".s\n";
1058 outs() << " gcc " << SharedObject << " " << TestModuleBC.str()
1059 << ".s -o " << TestModuleBC.str() << ".exe";
Chris Lattner59615f02005-01-15 00:07:19 +00001060#if defined (HAVE_LINK_R)
Dan Gohmanac95cc72009-07-16 15:30:09 +00001061 outs() << " -Wl,-R.";
Chris Lattner59615f02005-01-15 00:07:19 +00001062#endif
Dan Gohmanac95cc72009-07-16 15:30:09 +00001063 outs() << "\n";
Chris Lattner74382b72009-08-23 22:45:37 +00001064 outs() << " " << TestModuleBC.str() << ".exe";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001065 }
Nick Lewycky22ff7482010-04-12 05:08:25 +00001066 for (unsigned i = 0, e = InputArgv.size(); i != e; ++i)
Dan Gohmanac95cc72009-07-16 15:30:09 +00001067 outs() << " " << InputArgv[i];
1068 outs() << '\n';
1069 outs() << "The shared object was created with:\n llc -march=c "
Chris Lattner74382b72009-08-23 22:45:37 +00001070 << SafeModuleBC.str() << " -o temporary.c\n"
Daniel Dunbarca740962009-08-18 03:35:57 +00001071 << " gcc -xc temporary.c -O2 -o " << SharedObject;
1072 if (TargetTriple.getArch() == Triple::sparc)
1073 outs() << " -G"; // Compile a shared library, `-G' for Sparc
1074 else
1075 outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others
1076
1077 outs() << " -fno-strict-aliasing\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001078
1079 return false;
1080}