blob: 82a3a862a2d873dd8755e15ac7099e58ab84a053 [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"
Chris Lattnera57d86b2004-04-05 22:58:16 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Instructions.h"
Reid Spencer605b9e22004-11-14 23:00:08 +000021#include "llvm/Linker.h"
Chris Lattner4a106452002-12-23 23:50:16 +000022#include "llvm/Module.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000023#include "llvm/Pass.h"
Chris Lattnera57d86b2004-04-05 22:58:16 +000024#include "llvm/Analysis/Verifier.h"
Chris Lattner640f22e2003-04-24 17:02:17 +000025#include "llvm/Transforms/Utils/Cloning.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/FileUtilities.h"
Chris Lattner59615f02005-01-15 00:07:19 +000028#include "llvm/Config/config.h" // for HAVE_LINK_R
Chris Lattnerfa761832004-01-14 03:38:37 +000029using namespace llvm;
Chris Lattner4a106452002-12-23 23:50:16 +000030
Chris Lattnera57d86b2004-04-05 22:58:16 +000031namespace llvm {
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +000032 extern cl::opt<std::string> OutputPrefix;
Chris Lattnera57d86b2004-04-05 22:58:16 +000033 extern cl::list<std::string> InputArgv;
34}
35
Chris Lattnerefdc0b52004-03-14 20:50:42 +000036namespace {
Michael J. Spencer56584fc2011-03-31 13:06:39 +000037 static llvm::cl::opt<bool>
38 DisableLoopExtraction("disable-loop-extraction",
Reid Spencerdc31a8a2006-11-11 19:05:02 +000039 cl::desc("Don't extract loops when searching for miscompilations"),
40 cl::init(false));
Michael J. Spencer56584fc2011-03-31 13:06:39 +000041 static llvm::cl::opt<bool>
42 DisableBlockExtraction("disable-block-extraction",
David Goodwin265d82e2009-07-28 23:08:36 +000043 cl::desc("Don't extract blocks when searching for miscompilations"),
44 cl::init(false));
Reid Spencerdc31a8a2006-11-11 19:05:02 +000045
Rafael Espindola8261dfe2010-08-08 03:55:08 +000046 class ReduceMiscompilingPasses : public ListReducer<std::string> {
Chris Lattnerfa761832004-01-14 03:38:37 +000047 BugDriver &BD;
48 public:
49 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000050
Rafael Espindola8261dfe2010-08-08 03:55:08 +000051 virtual TestResult doTest(std::vector<std::string> &Prefix,
52 std::vector<std::string> &Suffix,
Nick Lewycky22ff7482010-04-12 05:08:25 +000053 std::string &Error);
Chris Lattnerfa761832004-01-14 03:38:37 +000054 };
55}
Chris Lattner640f22e2003-04-24 17:02:17 +000056
Misha Brukman8c194ea2004-04-21 18:36:43 +000057/// TestResult - After passes have been split into a test group and a control
58/// group, see if they still break the program.
59///
Chris Lattner640f22e2003-04-24 17:02:17 +000060ReduceMiscompilingPasses::TestResult
Rafael Espindola8261dfe2010-08-08 03:55:08 +000061ReduceMiscompilingPasses::doTest(std::vector<std::string> &Prefix,
62 std::vector<std::string> &Suffix,
Nick Lewycky22ff7482010-04-12 05:08:25 +000063 std::string &Error) {
Chris Lattner06943ad2003-04-25 03:16:05 +000064 // First, run the program with just the Suffix passes. If it is still broken
Chris Lattner640f22e2003-04-24 17:02:17 +000065 // with JUST the kept passes, discard the prefix passes.
Dan Gohmanac95cc72009-07-16 15:30:09 +000066 outs() << "Checking to see if '" << getPassesString(Suffix)
67 << "' compiles correctly: ";
Chris Lattner640f22e2003-04-24 17:02:17 +000068
Gabor Greif8ff70c22007-07-04 21:55:50 +000069 std::string BitcodeResult;
Rafael Espindolaca356af2010-08-05 00:29:04 +000070 if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false/*delete*/,
71 true/*quiet*/)) {
Dan Gohman65f57c22009-07-15 16:35:29 +000072 errs() << " Error running this sequence of passes"
73 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +000074 BD.setPassesToRun(Suffix);
Rafael Espindolabae1b712010-07-28 18:12:30 +000075 BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +000076 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000077 }
Michael J. Spencer56584fc2011-03-31 13:06:39 +000078
Chris Lattner640f22e2003-04-24 17:02:17 +000079 // Check to see if the finished program matches the reference output...
Rafael Espindola10757dd2010-07-30 14:19:00 +000080 bool Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "",
81 true /*delete bitcode*/, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +000082 if (!Error.empty())
83 return InternalError;
84 if (Diff) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000085 outs() << " nope.\n";
Chris Lattner59615f02005-01-15 00:07:19 +000086 if (Suffix.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +000087 errs() << BD.getToolName() << ": I'm confused: the test fails when "
88 << "no passes are run, nondeterministic program?\n";
Chris Lattner59615f02005-01-15 00:07:19 +000089 exit(1);
90 }
Misha Brukman123f8fe2004-04-22 20:02:09 +000091 return KeepSuffix; // Miscompilation detected!
Chris Lattner640f22e2003-04-24 17:02:17 +000092 }
Dan Gohmanac95cc72009-07-16 15:30:09 +000093 outs() << " yup.\n"; // No miscompilation!
Chris Lattner640f22e2003-04-24 17:02:17 +000094
95 if (Prefix.empty()) return NoFailure;
96
Chris Lattner06943ad2003-04-25 03:16:05 +000097 // Next, see if the program is broken if we run the "prefix" passes first,
Misha Brukmanbc0e9982003-07-14 17:20:40 +000098 // then separately run the "kept" passes.
Dan Gohmanac95cc72009-07-16 15:30:09 +000099 outs() << "Checking to see if '" << getPassesString(Prefix)
100 << "' compiles correctly: ";
Chris Lattner640f22e2003-04-24 17:02:17 +0000101
102 // If it is not broken with the kept passes, it's possible that the prefix
103 // passes must be run before the kept passes to break it. If the program
104 // WORKS after the prefix passes, but then fails if running the prefix AND
Gabor Greif8ff70c22007-07-04 21:55:50 +0000105 // kept passes, we can update our bitcode file to include the result of the
Chris Lattner640f22e2003-04-24 17:02:17 +0000106 // prefix passes, then discard the prefix passes.
107 //
Rafael Espindolaca356af2010-08-05 00:29:04 +0000108 if (BD.runPasses(BD.getProgram(), Prefix, BitcodeResult, false/*delete*/,
109 true/*quiet*/)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000110 errs() << " Error running this sequence of passes"
111 << " on the input program!\n";
Chris Lattner9c6cfe12003-10-17 23:03:16 +0000112 BD.setPassesToRun(Prefix);
Rafael Espindolabae1b712010-07-28 18:12:30 +0000113 BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000114 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000115 }
116
117 // If the prefix maintains the predicate by itself, only keep the prefix!
Rafael Espindola10757dd2010-07-30 14:19:00 +0000118 Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", false, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000119 if (!Error.empty())
120 return InternalError;
121 if (Diff) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000122 outs() << " nope.\n";
Gabor Greif8ff70c22007-07-04 21:55:50 +0000123 sys::Path(BitcodeResult).eraseFromDisk();
Chris Lattner640f22e2003-04-24 17:02:17 +0000124 return KeepPrefix;
125 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000126 outs() << " yup.\n"; // No miscompilation!
Chris Lattner640f22e2003-04-24 17:02:17 +0000127
128 // Ok, so now we know that the prefix passes work, try running the suffix
129 // passes on the result of the prefix passes.
130 //
Jeffrey Yasskin6865f292010-05-11 23:25:16 +0000131 OwningPtr<Module> PrefixOutput(ParseInputFile(BitcodeResult,
132 BD.getContext()));
Chris Lattner640f22e2003-04-24 17:02:17 +0000133 if (PrefixOutput == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000134 errs() << BD.getToolName() << ": Error reading bitcode file '"
135 << BitcodeResult << "'!\n";
Chris Lattner640f22e2003-04-24 17:02:17 +0000136 exit(1);
137 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000138 sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk
Chris Lattnerf4789e62004-04-23 20:36:51 +0000139
140 // Don't check if there are no passes in the suffix.
141 if (Suffix.empty())
142 return NoFailure;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000143
Dan Gohmanac95cc72009-07-16 15:30:09 +0000144 outs() << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +0000145 << "' passes compile correctly after the '"
146 << getPassesString(Prefix) << "' passes: ";
147
Jeffrey Yasskin6865f292010-05-11 23:25:16 +0000148 OwningPtr<Module> OriginalInput(BD.swapProgramIn(PrefixOutput.take()));
Rafael Espindolaca356af2010-08-05 00:29:04 +0000149 if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false/*delete*/,
150 true/*quiet*/)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000151 errs() << " Error running this sequence of passes"
152 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000153 BD.setPassesToRun(Suffix);
Rafael Espindolabae1b712010-07-28 18:12:30 +0000154 BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000155 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000156 }
157
158 // Run the result...
Rafael Espindola10757dd2010-07-30 14:19:00 +0000159 Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "",
160 true /*delete bitcode*/, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000161 if (!Error.empty())
162 return InternalError;
163 if (Diff) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000164 outs() << " nope.\n";
Chris Lattner06943ad2003-04-25 03:16:05 +0000165 return KeepSuffix;
Chris Lattner640f22e2003-04-24 17:02:17 +0000166 }
167
168 // Otherwise, we must not be running the bad pass anymore.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000169 outs() << " yup.\n"; // No miscompilation!
Jeffrey Yasskin6865f292010-05-11 23:25:16 +0000170 // Restore orig program & free test.
171 delete BD.swapProgramIn(OriginalInput.take());
Chris Lattner640f22e2003-04-24 17:02:17 +0000172 return NoFailure;
173}
174
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000175namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +0000176 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
177 BugDriver &BD;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000178 bool (*TestFn)(BugDriver &, Module *, Module *, std::string &);
Chris Lattnerfa761832004-01-14 03:38:37 +0000179 public:
Chris Lattnerb15825b2004-04-05 21:37:38 +0000180 ReduceMiscompilingFunctions(BugDriver &bd,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000181 bool (*F)(BugDriver &, Module *, Module *,
182 std::string &))
Chris Lattnerb15825b2004-04-05 21:37:38 +0000183 : BD(bd), TestFn(F) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000184
Chris Lattnerfa761832004-01-14 03:38:37 +0000185 virtual TestResult doTest(std::vector<Function*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000186 std::vector<Function*> &Suffix,
187 std::string &Error) {
188 if (!Suffix.empty()) {
189 bool Ret = TestFuncs(Suffix, Error);
190 if (!Error.empty())
191 return InternalError;
192 if (Ret)
193 return KeepSuffix;
194 }
195 if (!Prefix.empty()) {
196 bool Ret = TestFuncs(Prefix, Error);
197 if (!Error.empty())
198 return InternalError;
199 if (Ret)
200 return KeepPrefix;
201 }
Chris Lattnerfa761832004-01-14 03:38:37 +0000202 return NoFailure;
203 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000204
Rafael Espindola84ae2062010-07-26 00:07:51 +0000205 bool TestFuncs(const std::vector<Function*> &Prefix, std::string &Error);
Chris Lattnerfa761832004-01-14 03:38:37 +0000206 };
207}
Chris Lattner640f22e2003-04-24 17:02:17 +0000208
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000209/// TestMergedProgram - Given two modules, link them together and run the
Rafael Espindola13793262010-07-31 14:34:49 +0000210/// program, checking to see if the program matches the diff. If there is
211/// an error, return NULL. If not, return the merged module. The Broken argument
212/// will be set to true if the output is different. If the DeleteInputs
213/// argument is set to true then this function deletes both input
214/// modules before it returns.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000215///
Rafael Espindola13793262010-07-31 14:34:49 +0000216static Module *TestMergedProgram(const BugDriver &BD, Module *M1, Module *M2,
217 bool DeleteInputs, std::string &Error,
218 bool &Broken) {
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000219 // Link the two portions of the program back to together.
220 std::string ErrorMsg;
Chris Lattner90c18c52004-11-16 06:31:38 +0000221 if (!DeleteInputs) {
222 M1 = CloneModule(M1);
223 M2 = CloneModule(M2);
224 }
Tanya Lattnerf1f1a4f2011-10-11 00:24:54 +0000225 if (Linker::LinkModules(M1, M2, Linker::DestroySource, &ErrorMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000226 errs() << BD.getToolName() << ": Error linking modules together:"
227 << ErrorMsg << '\n';
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000228 exit(1);
229 }
Chris Lattner90c18c52004-11-16 06:31:38 +0000230 delete M2; // We are done with this module.
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000231
Rafael Espindola13793262010-07-31 14:34:49 +0000232 // Execute the program.
233 Broken = BD.diffProgram(M1, "", "", false, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000234 if (!Error.empty()) {
Rafael Espindola10757dd2010-07-30 14:19:00 +0000235 // Delete the linked module
236 delete M1;
Rafael Espindola13793262010-07-31 14:34:49 +0000237 return NULL;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000238 }
Rafael Espindola13793262010-07-31 14:34:49 +0000239 return M1;
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000240}
241
Misha Brukman8c194ea2004-04-21 18:36:43 +0000242/// TestFuncs - split functions in a Module into two groups: those that are
243/// under consideration for miscompilation vs. those that are not, and test
244/// accordingly. Each group of functions becomes a separate Module.
245///
Rafael Espindola84ae2062010-07-26 00:07:51 +0000246bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
247 std::string &Error) {
Chris Lattner640f22e2003-04-24 17:02:17 +0000248 // Test to see if the function is misoptimized if we ONLY run it on the
249 // functions listed in Funcs.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000250 outs() << "Checking to see if the program is misoptimized when "
251 << (Funcs.size()==1 ? "this function is" : "these functions are")
252 << " run through the pass"
253 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000254 PrintFunctionList(Funcs);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000255 outs() << '\n';
Chris Lattner640f22e2003-04-24 17:02:17 +0000256
Rafael Espindola84ae2062010-07-26 00:07:51 +0000257 // Create a clone for two reasons:
258 // * If the optimization passes delete any function, the deleted function
259 // will be in the clone and Funcs will still point to valid memory
260 // * If the optimization passes use interprocedural information to break
261 // a function, we want to continue with the original function. Otherwise
262 // we can conclude that a function triggers the bug when in fact one
263 // needs a larger set of original functions to do so.
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000264 ValueToValueMapTy VMap;
Rafael Espindola84ae2062010-07-26 00:07:51 +0000265 Module *Clone = CloneModule(BD.getProgram(), VMap);
266 Module *Orig = BD.swapProgramIn(Clone);
267
268 std::vector<Function*> FuncsOnClone;
269 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
270 Function *F = cast<Function>(VMap[Funcs[i]]);
271 FuncsOnClone.push_back(F);
272 }
273
274 // Split the module into the two halves of the program we want.
275 VMap.clear();
Devang Patele9916a32010-06-24 00:33:28 +0000276 Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap);
Rafael Espindola84ae2062010-07-26 00:07:51 +0000277 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, FuncsOnClone,
Devang Patele9916a32010-06-24 00:33:28 +0000278 VMap);
Chris Lattner640f22e2003-04-24 17:02:17 +0000279
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000280 // Run the predicate, note that the predicate will delete both input modules.
Rafael Espindola84ae2062010-07-26 00:07:51 +0000281 bool Broken = TestFn(BD, ToOptimize, ToNotOptimize, Error);
282
283 delete BD.swapProgramIn(Orig);
284
285 return Broken;
Chris Lattner640f22e2003-04-24 17:02:17 +0000286}
287
Chris Lattner8abfb8a2010-01-16 21:34:01 +0000288/// DisambiguateGlobalSymbols - Give anonymous global values names.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000289///
Chris Lattner36ee07f2004-04-11 23:52:35 +0000290static void DisambiguateGlobalSymbols(Module *M) {
Chris Lattner67ef9e42006-05-04 23:35:31 +0000291 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
Chris Lattner1ffb33d2010-01-16 21:34:51 +0000292 I != E; ++I)
Chris Lattner8abfb8a2010-01-16 21:34:01 +0000293 if (!I->hasName())
294 I->setName("anon_global");
Chris Lattner1ffb33d2010-01-16 21:34:51 +0000295 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner8abfb8a2010-01-16 21:34:01 +0000296 if (!I->hasName())
297 I->setName("anon_fn");
Chris Lattner36ee07f2004-04-11 23:52:35 +0000298}
299
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000300/// ExtractLoops - Given a reduced list of functions that still exposed the bug,
301/// check to see if we can extract the loops in the region without obscuring the
302/// bug. If so, it reduces the amount of code identified.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000303///
Chris Lattnerb15825b2004-04-05 21:37:38 +0000304static bool ExtractLoops(BugDriver &BD,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000305 bool (*TestFn)(BugDriver &, Module *, Module *,
306 std::string &),
307 std::vector<Function*> &MiscompiledFunctions,
308 std::string &Error) {
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000309 bool MadeChange = false;
310 while (1) {
Chris Lattneraed98fa2005-08-02 23:25:56 +0000311 if (BugpointIsInterrupted) return MadeChange;
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000312
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000313 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +0000314 Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap);
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000315 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Dan Gohmand50330c2009-04-22 15:57:18 +0000316 MiscompiledFunctions,
Devang Patele9916a32010-06-24 00:33:28 +0000317 VMap);
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000318 Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
319 if (!ToOptimizeLoopExtracted) {
320 // If the loop extractor crashed or if there were no extractible loops,
321 // then this chapter of our odyssey is over with.
322 delete ToNotOptimize;
323 delete ToOptimize;
324 return MadeChange;
325 }
326
Dan Gohman65f57c22009-07-15 16:35:29 +0000327 errs() << "Extracted a loop from the breaking portion of the program.\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000328
329 // Bugpoint is intentionally not very trusting of LLVM transformations. In
330 // particular, we're not going to assume that the loop extractor works, so
331 // we're going to test the newly loop extracted program to make sure nothing
332 // has broken. If something broke, then we'll inform the user and stop
333 // extraction.
Dan Gohman70ef4492008-12-08 04:02:47 +0000334 AbstractInterpreter *AI = BD.switchToSafeInterpreter();
Rafael Espindola13793262010-07-31 14:34:49 +0000335 bool Failure;
336 Module *New = TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize,
337 false, Error, Failure);
338 if (!New)
Nick Lewycky22ff7482010-04-12 05:08:25 +0000339 return false;
Rafael Espindola13793262010-07-31 14:34:49 +0000340 // Delete the original and set the new program.
341 delete BD.swapProgramIn(New);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000342 if (Failure) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000343 BD.switchToInterpreter(AI);
344
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000345 // Merged program doesn't work anymore!
Dan Gohman65f57c22009-07-15 16:35:29 +0000346 errs() << " *** ERROR: Loop extraction broke the program. :("
347 << " Please report a bug!\n";
348 errs() << " Continuing on with un-loop-extracted version.\n";
Chris Lattner56c41862005-05-08 21:54:56 +0000349
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000350 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc",
351 ToNotOptimize);
352 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc",
353 ToOptimize);
354 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc",
Chris Lattner56c41862005-05-08 21:54:56 +0000355 ToOptimizeLoopExtracted);
356
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000357 errs() << "Please submit the "
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000358 << OutputPrefix << "-loop-extract-fail-*.bc files.\n";
Chris Lattner56c41862005-05-08 21:54:56 +0000359 delete ToOptimize;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000360 delete ToNotOptimize;
361 delete ToOptimizeLoopExtracted;
362 return MadeChange;
363 }
Chris Lattner56c41862005-05-08 21:54:56 +0000364 delete ToOptimize;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000365 BD.switchToInterpreter(AI);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000366
Dan Gohmanac95cc72009-07-16 15:30:09 +0000367 outs() << " Testing after loop extraction:\n";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000368 // Clone modules, the tester function will free them.
369 Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
370 Module *TNOBackup = CloneModule(ToNotOptimize);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000371 Failure = TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize, Error);
372 if (!Error.empty())
373 return false;
374 if (!Failure) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000375 outs() << "*** Loop extraction masked the problem. Undoing.\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000376 // If the program is not still broken, then loop extraction did something
377 // that masked the error. Stop loop extraction now.
Chris Lattnerb15825b2004-04-05 21:37:38 +0000378 delete TOLEBackup;
379 delete TNOBackup;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000380 return MadeChange;
381 }
Chris Lattnerb15825b2004-04-05 21:37:38 +0000382 ToOptimizeLoopExtracted = TOLEBackup;
383 ToNotOptimize = TNOBackup;
384
Dan Gohmanac95cc72009-07-16 15:30:09 +0000385 outs() << "*** Loop extraction successful!\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000386
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000387 std::vector<std::pair<std::string, FunctionType*> > MisCompFunctions;
Chris Lattner90c18c52004-11-16 06:31:38 +0000388 for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
389 E = ToOptimizeLoopExtracted->end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000390 if (!I->isDeclaration())
Chris Lattnerfa1af132004-11-19 07:09:40 +0000391 MisCompFunctions.push_back(std::make_pair(I->getName(),
392 I->getFunctionType()));
Chris Lattner90c18c52004-11-16 06:31:38 +0000393
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000394 // Okay, great! Now we know that we extracted a loop and that loop
395 // extraction both didn't break the program, and didn't mask the problem.
396 // Replace the current program with the loop extracted version, and try to
397 // extract another loop.
398 std::string ErrorMsg;
Tanya Lattnerf1f1a4f2011-10-11 00:24:54 +0000399 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted,
400 Linker::DestroySource, &ErrorMsg)){
Dan Gohman65f57c22009-07-15 16:35:29 +0000401 errs() << BD.getToolName() << ": Error linking modules together:"
402 << ErrorMsg << '\n';
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000403 exit(1);
404 }
Chris Lattner90c18c52004-11-16 06:31:38 +0000405 delete ToOptimizeLoopExtracted;
Chris Lattnerd3a533d2004-03-17 17:42:09 +0000406
407 // All of the Function*'s in the MiscompiledFunctions list are in the old
Chris Lattner5313f232004-04-02 06:32:17 +0000408 // module. Update this list to include all of the functions in the
409 // optimized and loop extracted module.
410 MiscompiledFunctions.clear();
Chris Lattner90c18c52004-11-16 06:31:38 +0000411 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000412 Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000413
Chris Lattner90c18c52004-11-16 06:31:38 +0000414 assert(NewF && "Function not found??");
415 MiscompiledFunctions.push_back(NewF);
Chris Lattnerd3a533d2004-03-17 17:42:09 +0000416 }
417
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000418 BD.setNewProgram(ToNotOptimize);
419 MadeChange = true;
420 }
421}
422
Chris Lattner5e783ab2004-05-11 21:54:13 +0000423namespace {
424 class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
425 BugDriver &BD;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000426 bool (*TestFn)(BugDriver &, Module *, Module *, std::string &);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000427 std::vector<Function*> FunctionsBeingTested;
428 public:
429 ReduceMiscompiledBlocks(BugDriver &bd,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000430 bool (*F)(BugDriver &, Module *, Module *,
431 std::string &),
Chris Lattner5e783ab2004-05-11 21:54:13 +0000432 const std::vector<Function*> &Fns)
433 : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000434
Chris Lattner5e783ab2004-05-11 21:54:13 +0000435 virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000436 std::vector<BasicBlock*> &Suffix,
437 std::string &Error) {
438 if (!Suffix.empty()) {
439 bool Ret = TestFuncs(Suffix, Error);
440 if (!Error.empty())
441 return InternalError;
442 if (Ret)
443 return KeepSuffix;
444 }
445 if (!Prefix.empty()) {
446 bool Ret = TestFuncs(Prefix, Error);
447 if (!Error.empty())
448 return InternalError;
449 if (Ret)
450 return KeepPrefix;
451 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000452 return NoFailure;
453 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000454
Nick Lewycky22ff7482010-04-12 05:08:25 +0000455 bool TestFuncs(const std::vector<BasicBlock*> &BBs, std::string &Error);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000456 };
457}
458
459/// TestFuncs - Extract all blocks for the miscompiled functions except for the
460/// specified blocks. If the problem still exists, return true.
461///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000462bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs,
463 std::string &Error) {
Chris Lattner5e783ab2004-05-11 21:54:13 +0000464 // Test to see if the function is misoptimized if we ONLY run it on the
465 // functions listed in Funcs.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000466 outs() << "Checking to see if the program is misoptimized when all ";
Chris Lattner68bee932004-05-12 16:08:01 +0000467 if (!BBs.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000468 outs() << "but these " << BBs.size() << " blocks are extracted: ";
Chris Lattner68bee932004-05-12 16:08:01 +0000469 for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
Dan Gohmanac95cc72009-07-16 15:30:09 +0000470 outs() << BBs[i]->getName() << " ";
471 if (BBs.size() > 10) outs() << "...";
Chris Lattner68bee932004-05-12 16:08:01 +0000472 } else {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000473 outs() << "blocks are extracted.";
Chris Lattner68bee932004-05-12 16:08:01 +0000474 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000475 outs() << '\n';
Chris Lattner5e783ab2004-05-11 21:54:13 +0000476
477 // Split the module into the two halves of the program we want.
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000478 ValueToValueMapTy VMap;
Rafael Espindola115a9322010-07-29 14:20:59 +0000479 Module *Clone = CloneModule(BD.getProgram(), VMap);
480 Module *Orig = BD.swapProgramIn(Clone);
481 std::vector<Function*> FuncsOnClone;
482 std::vector<BasicBlock*> BBsOnClone;
483 for (unsigned i = 0, e = FunctionsBeingTested.size(); i != e; ++i) {
484 Function *F = cast<Function>(VMap[FunctionsBeingTested[i]]);
485 FuncsOnClone.push_back(F);
486 }
487 for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
488 BasicBlock *BB = cast<BasicBlock>(VMap[BBs[i]]);
489 BBsOnClone.push_back(BB);
490 }
491 VMap.clear();
492
Devang Patele9916a32010-06-24 00:33:28 +0000493 Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000494 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Rafael Espindola115a9322010-07-29 14:20:59 +0000495 FuncsOnClone,
Devang Patele9916a32010-06-24 00:33:28 +0000496 VMap);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000497
498 // Try the extraction. If it doesn't work, then the block extractor crashed
499 // or something, in which case bugpoint can't chase down this possibility.
Rafael Espindola115a9322010-07-29 14:20:59 +0000500 if (Module *New = BD.ExtractMappedBlocksFromModule(BBsOnClone, ToOptimize)) {
Chris Lattner5e783ab2004-05-11 21:54:13 +0000501 delete ToOptimize;
Rafael Espindola115a9322010-07-29 14:20:59 +0000502 // Run the predicate,
503 // note that the predicate will delete both input modules.
504 bool Ret = TestFn(BD, New, ToNotOptimize, Error);
505 delete BD.swapProgramIn(Orig);
506 return Ret;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000507 }
Rafael Espindola115a9322010-07-29 14:20:59 +0000508 delete BD.swapProgramIn(Orig);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000509 delete ToOptimize;
510 delete ToNotOptimize;
511 return false;
512}
513
514
515/// ExtractBlocks - Given a reduced list of functions that still expose the bug,
516/// extract as many basic blocks from the region as possible without obscuring
517/// the bug.
518///
519static bool ExtractBlocks(BugDriver &BD,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000520 bool (*TestFn)(BugDriver &, Module *, Module *,
521 std::string &),
522 std::vector<Function*> &MiscompiledFunctions,
523 std::string &Error) {
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000524 if (BugpointIsInterrupted) return false;
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000525
Chris Lattner5e783ab2004-05-11 21:54:13 +0000526 std::vector<BasicBlock*> Blocks;
527 for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
528 for (Function::iterator I = MiscompiledFunctions[i]->begin(),
529 E = MiscompiledFunctions[i]->end(); I != E; ++I)
530 Blocks.push_back(I);
531
532 // Use the list reducer to identify blocks that can be extracted without
533 // obscuring the bug. The Blocks list will end up containing blocks that must
534 // be retained from the original program.
535 unsigned OldSize = Blocks.size();
Chris Lattner68bee932004-05-12 16:08:01 +0000536
537 // Check to see if all blocks are extractible first.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000538 bool Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions)
539 .TestFuncs(std::vector<BasicBlock*>(), Error);
540 if (!Error.empty())
541 return false;
542 if (Ret) {
Chris Lattner68bee932004-05-12 16:08:01 +0000543 Blocks.clear();
544 } else {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000545 ReduceMiscompiledBlocks(BD, TestFn,
546 MiscompiledFunctions).reduceList(Blocks, Error);
547 if (!Error.empty())
548 return false;
Chris Lattner68bee932004-05-12 16:08:01 +0000549 if (Blocks.size() == OldSize)
550 return false;
551 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000552
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000553 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +0000554 Module *ProgClone = CloneModule(BD.getProgram(), VMap);
Chris Lattner2290e752004-05-12 02:43:24 +0000555 Module *ToExtract = SplitFunctionsOutOfModule(ProgClone,
Dan Gohmand50330c2009-04-22 15:57:18 +0000556 MiscompiledFunctions,
Devang Patele9916a32010-06-24 00:33:28 +0000557 VMap);
Chris Lattner2290e752004-05-12 02:43:24 +0000558 Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract);
559 if (Extracted == 0) {
Chris Lattnerda895d62005-02-27 06:18:25 +0000560 // Weird, extraction should have worked.
Dan Gohman65f57c22009-07-15 16:35:29 +0000561 errs() << "Nondeterministic problem extracting blocks??\n";
Chris Lattner2290e752004-05-12 02:43:24 +0000562 delete ProgClone;
563 delete ToExtract;
564 return false;
565 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000566
Chris Lattner2290e752004-05-12 02:43:24 +0000567 // Otherwise, block extraction succeeded. Link the two program fragments back
568 // together.
569 delete ToExtract;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000570
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000571 std::vector<std::pair<std::string, FunctionType*> > MisCompFunctions;
Chris Lattner90c18c52004-11-16 06:31:38 +0000572 for (Module::iterator I = Extracted->begin(), E = Extracted->end();
573 I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000574 if (!I->isDeclaration())
Chris Lattnerfa1af132004-11-19 07:09:40 +0000575 MisCompFunctions.push_back(std::make_pair(I->getName(),
576 I->getFunctionType()));
Chris Lattner90c18c52004-11-16 06:31:38 +0000577
Chris Lattner2290e752004-05-12 02:43:24 +0000578 std::string ErrorMsg;
Tanya Lattnerf1f1a4f2011-10-11 00:24:54 +0000579 if (Linker::LinkModules(ProgClone, Extracted, Linker::DestroySource,
580 &ErrorMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000581 errs() << BD.getToolName() << ": Error linking modules together:"
582 << ErrorMsg << '\n';
Chris Lattner2290e752004-05-12 02:43:24 +0000583 exit(1);
584 }
Chris Lattner90c18c52004-11-16 06:31:38 +0000585 delete Extracted;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000586
Chris Lattner2290e752004-05-12 02:43:24 +0000587 // Set the new program and delete the old one.
588 BD.setNewProgram(ProgClone);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000589
Chris Lattner2290e752004-05-12 02:43:24 +0000590 // Update the list of miscompiled functions.
591 MiscompiledFunctions.clear();
Chris Lattner5e783ab2004-05-11 21:54:13 +0000592
Chris Lattner90c18c52004-11-16 06:31:38 +0000593 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000594 Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first);
Chris Lattner90c18c52004-11-16 06:31:38 +0000595 assert(NewF && "Function not found??");
596 MiscompiledFunctions.push_back(NewF);
597 }
Chris Lattner2290e752004-05-12 02:43:24 +0000598
599 return true;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000600}
601
602
Chris Lattnerb15825b2004-04-05 21:37:38 +0000603/// DebugAMiscompilation - This is a generic driver to narrow down
604/// miscompilations, either in an optimization or a code generator.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000605///
Chris Lattnerb15825b2004-04-05 21:37:38 +0000606static std::vector<Function*>
607DebugAMiscompilation(BugDriver &BD,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000608 bool (*TestFn)(BugDriver &, Module *, Module *,
609 std::string &),
610 std::string &Error) {
Chris Lattnerb15825b2004-04-05 21:37:38 +0000611 // Okay, now that we have reduced the list of passes which are causing the
612 // failure, see if we can pin down which functions are being
613 // miscompiled... first build a list of all of the non-external functions in
614 // the program.
615 std::vector<Function*> MiscompiledFunctions;
616 Module *Prog = BD.getProgram();
617 for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000618 if (!I->isDeclaration())
Chris Lattnerb15825b2004-04-05 21:37:38 +0000619 MiscompiledFunctions.push_back(I);
620
621 // Do the reduction...
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000622 if (!BugpointIsInterrupted)
Nick Lewycky22ff7482010-04-12 05:08:25 +0000623 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
624 Error);
Andrew Trick7c863eb2011-05-11 16:31:24 +0000625 if (!Error.empty()) {
626 errs() << "\n***Cannot reduce functions: ";
Nick Lewycky22ff7482010-04-12 05:08:25 +0000627 return MiscompiledFunctions;
Andrew Trick7c863eb2011-05-11 16:31:24 +0000628 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000629 outs() << "\n*** The following function"
630 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
631 << " being miscompiled: ";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000632 PrintFunctionList(MiscompiledFunctions);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000633 outs() << '\n';
Chris Lattnerb15825b2004-04-05 21:37:38 +0000634
635 // See if we can rip any loops out of the miscompiled functions and still
636 // trigger the problem.
Reid Spencerdc31a8a2006-11-11 19:05:02 +0000637
Nick Lewycky22ff7482010-04-12 05:08:25 +0000638 if (!BugpointIsInterrupted && !DisableLoopExtraction) {
639 bool Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions, Error);
640 if (!Error.empty())
641 return MiscompiledFunctions;
642 if (Ret) {
643 // Okay, we extracted some loops and the problem still appears. See if
644 // we can eliminate some of the created functions from being candidates.
645 DisambiguateGlobalSymbols(BD.getProgram());
Chris Lattner36ee07f2004-04-11 23:52:35 +0000646
Nick Lewycky22ff7482010-04-12 05:08:25 +0000647 // Do the reduction...
648 if (!BugpointIsInterrupted)
649 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
650 Error);
651 if (!Error.empty())
652 return MiscompiledFunctions;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000653
Nick Lewycky22ff7482010-04-12 05:08:25 +0000654 outs() << "\n*** The following function"
655 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
656 << " being miscompiled: ";
657 PrintFunctionList(MiscompiledFunctions);
658 outs() << '\n';
659 }
Chris Lattnerb15825b2004-04-05 21:37:38 +0000660 }
661
Nick Lewycky22ff7482010-04-12 05:08:25 +0000662 if (!BugpointIsInterrupted && !DisableBlockExtraction) {
663 bool Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions, Error);
664 if (!Error.empty())
665 return MiscompiledFunctions;
666 if (Ret) {
667 // Okay, we extracted some blocks and the problem still appears. See if
668 // we can eliminate some of the created functions from being candidates.
669 DisambiguateGlobalSymbols(BD.getProgram());
Chris Lattner5e783ab2004-05-11 21:54:13 +0000670
Nick Lewycky22ff7482010-04-12 05:08:25 +0000671 // Do the reduction...
672 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
673 Error);
674 if (!Error.empty())
675 return MiscompiledFunctions;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000676
Nick Lewycky22ff7482010-04-12 05:08:25 +0000677 outs() << "\n*** The following function"
678 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
679 << " being miscompiled: ";
680 PrintFunctionList(MiscompiledFunctions);
681 outs() << '\n';
682 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000683 }
684
Chris Lattnerb15825b2004-04-05 21:37:38 +0000685 return MiscompiledFunctions;
686}
687
Chris Lattnera57d86b2004-04-05 22:58:16 +0000688/// TestOptimizer - This is the predicate function used to check to see if the
689/// "Test" portion of the program is misoptimized. If so, return true. In any
690/// case, both module arguments are deleted.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000691///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000692static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe,
693 std::string &Error) {
Chris Lattnerb15825b2004-04-05 21:37:38 +0000694 // Run the optimization passes on ToOptimize, producing a transformed version
695 // of the functions being tested.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000696 outs() << " Optimizing functions being tested: ";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000697 Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
698 /*AutoDebugCrashes*/true);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000699 outs() << "done.\n";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000700 delete Test;
701
Dan Gohmanac95cc72009-07-16 15:30:09 +0000702 outs() << " Checking to see if the merged program executes correctly: ";
Rafael Espindola13793262010-07-31 14:34:49 +0000703 bool Broken;
704 Module *New = TestMergedProgram(BD, Optimized, Safe, true, Error, Broken);
705 if (New) {
706 outs() << (Broken ? " nope.\n" : " yup.\n");
707 // Delete the original and set the new program.
708 delete BD.swapProgramIn(New);
709 }
Chris Lattnerb15825b2004-04-05 21:37:38 +0000710 return Broken;
711}
712
713
Chris Lattner4a106452002-12-23 23:50:16 +0000714/// debugMiscompilation - This method is used when the passes selected are not
715/// crashing, but the generated output is semantically different from the
716/// input.
717///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000718void BugDriver::debugMiscompilation(std::string *Error) {
Chris Lattner4a106452002-12-23 23:50:16 +0000719 // Make sure something was miscompiled...
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000720 if (!BugpointIsInterrupted)
Nick Lewycky22ff7482010-04-12 05:08:25 +0000721 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun, *Error)) {
722 if (Error->empty())
723 errs() << "*** Optimized program matches reference output! No problem"
724 << " detected...\nbugpoint can't help you with your problem!\n";
725 return;
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000726 }
Chris Lattner4a106452002-12-23 23:50:16 +0000727
Dan Gohmanac95cc72009-07-16 15:30:09 +0000728 outs() << "\n*** Found miscompiling pass"
729 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
730 << getPassesString(getPassesToRun()) << '\n';
Rafael Espindolabae1b712010-07-28 18:12:30 +0000731 EmitProgressBitcode(Program, "passinput");
Chris Lattner4a106452002-12-23 23:50:16 +0000732
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000733 std::vector<Function *> MiscompiledFunctions =
Nick Lewycky22ff7482010-04-12 05:08:25 +0000734 DebugAMiscompilation(*this, TestOptimizer, *Error);
735 if (!Error->empty())
736 return;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000737
Gabor Greif8ff70c22007-07-04 21:55:50 +0000738 // Output a bunch of bitcode files for the user...
Dan Gohmanac95cc72009-07-16 15:30:09 +0000739 outs() << "Outputting reduced bitcode files which expose the problem:\n";
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000740 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +0000741 Module *ToNotOptimize = CloneModule(getProgram(), VMap);
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000742 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Dan Gohmand50330c2009-04-22 15:57:18 +0000743 MiscompiledFunctions,
Devang Patele9916a32010-06-24 00:33:28 +0000744 VMap);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000745
Dan Gohmanac95cc72009-07-16 15:30:09 +0000746 outs() << " Non-optimized portion: ";
Rafael Espindolabae1b712010-07-28 18:12:30 +0000747 EmitProgressBitcode(ToNotOptimize, "tonotoptimize", true);
748 delete ToNotOptimize; // Delete hacked module.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000749
Dan Gohmanac95cc72009-07-16 15:30:09 +0000750 outs() << " Portion that is input to optimizer: ";
Rafael Espindolabae1b712010-07-28 18:12:30 +0000751 EmitProgressBitcode(ToOptimize, "tooptimize");
752 delete ToOptimize; // Delete hacked module.
Chris Lattner4a106452002-12-23 23:50:16 +0000753
Nick Lewycky22ff7482010-04-12 05:08:25 +0000754 return;
Chris Lattner4a106452002-12-23 23:50:16 +0000755}
Brian Gaeked0fde302003-11-11 22:41:34 +0000756
Chris Lattnera57d86b2004-04-05 22:58:16 +0000757/// CleanupAndPrepareModules - Get the specified modules ready for code
758/// generator testing.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000759///
Chris Lattnera57d86b2004-04-05 22:58:16 +0000760static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
761 Module *Safe) {
762 // Clean up the modules, removing extra cruft that we don't need anymore...
763 Test = BD.performFinalCleanups(Test);
764
765 // If we are executing the JIT, we have several nasty issues to take care of.
766 if (!BD.isExecutingJIT()) return;
767
768 // First, if the main function is in the Safe module, we must add a stub to
769 // the Test module to call into it. Thus, we create a new function `main'
770 // which just calls the old one.
Reid Spencer688b0492007-02-05 21:19:13 +0000771 if (Function *oldMain = Safe->getFunction("main"))
Reid Spencer5cbf9852007-01-30 20:08:39 +0000772 if (!oldMain->isDeclaration()) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000773 // Rename it
774 oldMain->setName("llvm_bugpoint_old_main");
775 // Create a NEW `main' function with same type in the test module.
Gabor Greif051a9502008-04-06 20:25:17 +0000776 Function *newMain = Function::Create(oldMain->getFunctionType(),
777 GlobalValue::ExternalLinkage,
778 "main", Test);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000779 // Create an `oldmain' prototype in the test module, which will
780 // corresponds to the real main function in the same module.
Gabor Greif051a9502008-04-06 20:25:17 +0000781 Function *oldMainProto = Function::Create(oldMain->getFunctionType(),
782 GlobalValue::ExternalLinkage,
783 oldMain->getName(), Test);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000784 // Set up and remember the argument list for the main function.
785 std::vector<Value*> args;
Alkis Evlogimenos5a1c58d2005-03-15 07:02:26 +0000786 for (Function::arg_iterator
787 I = newMain->arg_begin(), E = newMain->arg_end(),
788 OI = oldMain->arg_begin(); I != E; ++I, ++OI) {
Owen Anderson6bc41e82008-04-14 17:38:21 +0000789 I->setName(OI->getName()); // Copy argument names from oldMain
Chris Lattnera57d86b2004-04-05 22:58:16 +0000790 args.push_back(I);
791 }
792
793 // Call the old main function and return its result
Owen Anderson1d0be152009-08-13 21:58:54 +0000794 BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain);
Jay Foada3efbb12011-07-15 08:37:34 +0000795 CallInst *call = CallInst::Create(oldMainProto, args, "", BB);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000796
Chris Lattnera57d86b2004-04-05 22:58:16 +0000797 // If the type of old function wasn't void, return value of call
Owen Anderson1d0be152009-08-13 21:58:54 +0000798 ReturnInst::Create(Safe->getContext(), call, BB);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000799 }
800
801 // The second nasty issue we must deal with in the JIT is that the Safe
802 // module cannot directly reference any functions defined in the test
803 // module. Instead, we use a JIT API call to dynamically resolve the
804 // symbol.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000805
Chris Lattnera57d86b2004-04-05 22:58:16 +0000806 // Add the resolver to the Safe module.
807 // Prototype: void *getPointerToNamedFunction(const char* Name)
Chris Lattner2db43c82007-01-07 08:13:39 +0000808 Constant *resolverFunc =
Chris Lattnera57d86b2004-04-05 22:58:16 +0000809 Safe->getOrInsertFunction("getPointerToNamedFunction",
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000810 Type::getInt8PtrTy(Safe->getContext()),
811 Type::getInt8PtrTy(Safe->getContext()),
Owen Anderson1d0be152009-08-13 21:58:54 +0000812 (Type *)0);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000813
Chris Lattnera57d86b2004-04-05 22:58:16 +0000814 // Use the function we just added to get addresses of functions we need.
Misha Brukmandc7fef82004-04-19 01:12:01 +0000815 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000816 if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc &&
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000817 !F->isIntrinsic() /* ignore intrinsics */) {
Reid Spencer688b0492007-02-05 21:19:13 +0000818 Function *TestFn = Test->getFunction(F->getName());
Chris Lattnera57d86b2004-04-05 22:58:16 +0000819
820 // Don't forward functions which are external in the test module too.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000821 if (TestFn && !TestFn->isDeclaration()) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000822 // 1. Add a string constant with its name to the global file
Chris Lattner18c7f802012-02-05 02:29:43 +0000823 Constant *InitArray =
824 ConstantDataArray::getString(F->getContext(), F->getName());
Chris Lattnera57d86b2004-04-05 22:58:16 +0000825 GlobalVariable *funcName =
Owen Andersone9b11b42009-07-08 19:03:57 +0000826 new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
Misha Brukman3da94ae2005-04-22 00:00:37 +0000827 GlobalValue::InternalLinkage, InitArray,
Owen Andersone9b11b42009-07-08 19:03:57 +0000828 F->getName() + "_name");
Chris Lattnera57d86b2004-04-05 22:58:16 +0000829
830 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
831 // sbyte* so it matches the signature of the resolver function.
832
833 // GetElementPtr *funcName, ulong 0, ulong 0
Owen Anderson1d0be152009-08-13 21:58:54 +0000834 std::vector<Constant*> GEPargs(2,
835 Constant::getNullValue(Type::getInt32Ty(F->getContext())));
Jay Foaddab3d292011-07-21 14:31:17 +0000836 Value *GEP = ConstantExpr::getGetElementPtr(funcName, GEPargs);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000837 std::vector<Value*> ResolverArgs;
838 ResolverArgs.push_back(GEP);
839
Misha Brukmande4803d2004-04-19 03:36:47 +0000840 // Rewrite uses of F in global initializers, etc. to uses of a wrapper
841 // function that dynamically resolves the calls to F via our JIT API
Chris Lattnera3efca12005-07-12 01:00:32 +0000842 if (!F->use_empty()) {
843 // Create a new global to hold the cached function pointer.
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000844 Constant *NullPtr = ConstantPointerNull::get(F->getType());
Chris Lattnera3efca12005-07-12 01:00:32 +0000845 GlobalVariable *Cache =
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000846 new GlobalVariable(*F->getParent(), F->getType(),
Owen Andersone9b11b42009-07-08 19:03:57 +0000847 false, GlobalValue::InternalLinkage,
848 NullPtr,F->getName()+".fpcache");
Jeff Cohen00b168892005-07-27 06:12:32 +0000849
Misha Brukmande4803d2004-04-19 03:36:47 +0000850 // Construct a new stub function that will re-route calls to F
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000851 FunctionType *FuncTy = F->getFunctionType();
Gabor Greif051a9502008-04-06 20:25:17 +0000852 Function *FuncWrapper = Function::Create(FuncTy,
853 GlobalValue::InternalLinkage,
854 F->getName() + "_wrapper",
855 F->getParent());
Owen Anderson1d0be152009-08-13 21:58:54 +0000856 BasicBlock *EntryBB = BasicBlock::Create(F->getContext(),
857 "entry", FuncWrapper);
858 BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(),
859 "usecache", FuncWrapper);
860 BasicBlock *LookupBB = BasicBlock::Create(F->getContext(),
861 "lookupfp", FuncWrapper);
Jeff Cohen00b168892005-07-27 06:12:32 +0000862
Chris Lattnera3efca12005-07-12 01:00:32 +0000863 // Check to see if we already looked up the value.
864 Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB);
Owen Anderson333c4002009-07-09 23:48:35 +0000865 Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal,
866 NullPtr, "isNull");
Gabor Greif051a9502008-04-06 20:25:17 +0000867 BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000868
Misha Brukmande4803d2004-04-19 03:36:47 +0000869 // Resolve the call to function F via the JIT API:
870 //
871 // call resolver(GetElementPtr...)
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000872 CallInst *Resolver =
Jay Foada3efbb12011-07-15 08:37:34 +0000873 CallInst::Create(resolverFunc, ResolverArgs, "resolver", LookupBB);
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000874
875 // Cast the result from the resolver to correctly-typed function.
876 CastInst *CastedResolver =
877 new BitCastInst(Resolver,
Owen Andersondebcb012009-07-29 22:17:13 +0000878 PointerType::getUnqual(F->getFunctionType()),
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000879 "resolverCast", LookupBB);
Reid Spencer3da59db2006-11-27 01:05:10 +0000880
Chris Lattnera3efca12005-07-12 01:00:32 +0000881 // Save the value in our cache.
882 new StoreInst(CastedResolver, Cache, LookupBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000883 BranchInst::Create(DoCallBB, LookupBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000884
Jay Foad3ecfc862011-03-30 11:28:46 +0000885 PHINode *FuncPtr = PHINode::Create(NullPtr->getType(), 2,
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000886 "fp", DoCallBB);
Chris Lattnera3efca12005-07-12 01:00:32 +0000887 FuncPtr->addIncoming(CastedResolver, LookupBB);
888 FuncPtr->addIncoming(CachedVal, EntryBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000889
Chris Lattnera3efca12005-07-12 01:00:32 +0000890 // Save the argument list.
Misha Brukmandc7fef82004-04-19 01:12:01 +0000891 std::vector<Value*> Args;
Alkis Evlogimenos5a1c58d2005-03-15 07:02:26 +0000892 for (Function::arg_iterator i = FuncWrapper->arg_begin(),
893 e = FuncWrapper->arg_end(); i != e; ++i)
Misha Brukmandc7fef82004-04-19 01:12:01 +0000894 Args.push_back(i);
895
896 // Pass on the arguments to the real function, return its result
Dan Gohmane49a13e2010-06-07 20:19:26 +0000897 if (F->getReturnType()->isVoidTy()) {
Jay Foada3efbb12011-07-15 08:37:34 +0000898 CallInst::Create(FuncPtr, Args, "", DoCallBB);
Owen Anderson1d0be152009-08-13 21:58:54 +0000899 ReturnInst::Create(F->getContext(), DoCallBB);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000900 } else {
Jay Foada3efbb12011-07-15 08:37:34 +0000901 CallInst *Call = CallInst::Create(FuncPtr, Args,
Gabor Greif051a9502008-04-06 20:25:17 +0000902 "retval", DoCallBB);
Owen Anderson1d0be152009-08-13 21:58:54 +0000903 ReturnInst::Create(F->getContext(),Call, DoCallBB);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000904 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000905
Misha Brukmande4803d2004-04-19 03:36:47 +0000906 // Use the wrapper function instead of the old function
907 F->replaceAllUsesWith(FuncWrapper);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000908 }
Chris Lattnera57d86b2004-04-05 22:58:16 +0000909 }
910 }
911 }
912
913 if (verifyModule(*Test) || verifyModule(*Safe)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000914 errs() << "Bugpoint has a bug, which corrupted a module!!\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000915 abort();
916 }
917}
918
919
920
921/// TestCodeGenerator - This is the predicate function used to check to see if
922/// the "Test" portion of the program is miscompiled by the code generator under
923/// test. If so, return true. In any case, both module arguments are deleted.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000924///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000925static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe,
926 std::string &Error) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000927 CleanupAndPrepareModules(BD, Test, Safe);
928
Reid Spencer97182982004-12-15 01:53:08 +0000929 sys::Path TestModuleBC("bugpoint.test.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000930 std::string ErrMsg;
931 if (TestModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000932 errs() << BD.getToolName() << "Error making unique filename: "
933 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000934 exit(1);
935 }
Chris Lattner74382b72009-08-23 22:45:37 +0000936 if (BD.writeProgramToFile(TestModuleBC.str(), Test)) {
937 errs() << "Error writing bitcode to `" << TestModuleBC.str()
938 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000939 exit(1);
940 }
941 delete Test;
942
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000943 FileRemover TestModuleBCRemover(TestModuleBC.str(), !SaveTemps);
Rafael Espindolabc2ed592010-06-21 02:17:36 +0000944
Chris Lattnera57d86b2004-04-05 22:58:16 +0000945 // Make the shared library
Reid Spencer97182982004-12-15 01:53:08 +0000946 sys::Path SafeModuleBC("bugpoint.safe.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000947 if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000948 errs() << BD.getToolName() << "Error making unique filename: "
949 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000950 exit(1);
951 }
Chris Lattnera57d86b2004-04-05 22:58:16 +0000952
Chris Lattner74382b72009-08-23 22:45:37 +0000953 if (BD.writeProgramToFile(SafeModuleBC.str(), Safe)) {
954 errs() << "Error writing bitcode to `" << SafeModuleBC.str()
955 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000956 exit(1);
957 }
Rafael Espindolabc2ed592010-06-21 02:17:36 +0000958
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000959 FileRemover SafeModuleBCRemover(SafeModuleBC.str(), !SaveTemps);
Rafael Espindolabc2ed592010-06-21 02:17:36 +0000960
Nick Lewycky22ff7482010-04-12 05:08:25 +0000961 std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str(), Error);
962 if (!Error.empty())
Benjamin Kramer27063872010-04-12 12:22:19 +0000963 return false;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000964 delete Safe;
965
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000966 FileRemover SharedObjectRemover(SharedObject, !SaveTemps);
Rafael Espindolabc2ed592010-06-21 02:17:36 +0000967
Chris Lattnera57d86b2004-04-05 22:58:16 +0000968 // Run the code generator on the `Test' code, loading the shared library.
969 // The function returns whether or not the new output differs from reference.
Rafael Espindola10757dd2010-07-30 14:19:00 +0000970 bool Result = BD.diffProgram(BD.getProgram(), TestModuleBC.str(),
971 SharedObject, false, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000972 if (!Error.empty())
973 return false;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000974
975 if (Result)
Dan Gohman65f57c22009-07-15 16:35:29 +0000976 errs() << ": still failing!\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000977 else
Dan Gohman65f57c22009-07-15 16:35:29 +0000978 errs() << ": didn't fail.\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000979
980 return Result;
981}
982
983
Misha Brukman8c194ea2004-04-21 18:36:43 +0000984/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
985///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000986bool BugDriver::debugCodeGenerator(std::string *Error) {
Dan Gohman70ef4492008-12-08 04:02:47 +0000987 if ((void*)SafeInterpreter == (void*)Interpreter) {
Rafael Espindola10757dd2010-07-30 14:19:00 +0000988 std::string Result = executeProgramSafely(Program, "bugpoint.safe.out",
989 Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000990 if (Error->empty()) {
991 outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match "
992 << "the reference diff. This may be due to a\n front-end "
993 << "bug or a bug in the original program, but this can also "
994 << "happen if bugpoint isn't running the program with the "
995 << "right flags or input.\n I left the result of executing "
996 << "the program with the \"safe\" backend in this file for "
997 << "you: '"
998 << Result << "'.\n";
999 }
Chris Lattnera57d86b2004-04-05 22:58:16 +00001000 return true;
1001 }
1002
1003 DisambiguateGlobalSymbols(Program);
1004
Nick Lewycky22ff7482010-04-12 05:08:25 +00001005 std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator,
1006 *Error);
1007 if (!Error->empty())
1008 return true;
Chris Lattnera57d86b2004-04-05 22:58:16 +00001009
1010 // Split the module into the two halves of the program we want.
Rafael Espindola1ed219a2010-10-13 01:36:30 +00001011 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +00001012 Module *ToNotCodeGen = CloneModule(getProgram(), VMap);
1013 Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs, VMap);
Chris Lattnera57d86b2004-04-05 22:58:16 +00001014
1015 // Condition the modules
1016 CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
1017
Reid Spencer97182982004-12-15 01:53:08 +00001018 sys::Path TestModuleBC("bugpoint.test.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +00001019 std::string ErrMsg;
1020 if (TestModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +00001021 errs() << getToolName() << "Error making unique filename: "
1022 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +00001023 exit(1);
1024 }
Reid Spencer97182982004-12-15 01:53:08 +00001025
Chris Lattner74382b72009-08-23 22:45:37 +00001026 if (writeProgramToFile(TestModuleBC.str(), ToCodeGen)) {
1027 errs() << "Error writing bitcode to `" << TestModuleBC.str()
1028 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001029 exit(1);
1030 }
1031 delete ToCodeGen;
1032
1033 // Make the shared library
Reid Spencer97182982004-12-15 01:53:08 +00001034 sys::Path SafeModuleBC("bugpoint.safe.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +00001035 if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +00001036 errs() << getToolName() << "Error making unique filename: "
1037 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +00001038 exit(1);
1039 }
Reid Spencer97182982004-12-15 01:53:08 +00001040
Chris Lattner74382b72009-08-23 22:45:37 +00001041 if (writeProgramToFile(SafeModuleBC.str(), ToNotCodeGen)) {
1042 errs() << "Error writing bitcode to `" << SafeModuleBC.str()
1043 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001044 exit(1);
1045 }
Nick Lewycky22ff7482010-04-12 05:08:25 +00001046 std::string SharedObject = compileSharedObject(SafeModuleBC.str(), *Error);
1047 if (!Error->empty())
1048 return true;
Chris Lattnera57d86b2004-04-05 22:58:16 +00001049 delete ToNotCodeGen;
1050
Dan Gohmanac95cc72009-07-16 15:30:09 +00001051 outs() << "You can reproduce the problem with the command line: \n";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001052 if (isExecutingJIT()) {
Chris Lattner74382b72009-08-23 22:45:37 +00001053 outs() << " lli -load " << SharedObject << " " << TestModuleBC.str();
Chris Lattnera57d86b2004-04-05 22:58:16 +00001054 } else {
Nick Lewycky8d0e1bc2010-04-29 23:37:44 +00001055 outs() << " llc " << TestModuleBC.str() << " -o " << TestModuleBC.str()
Chris Lattner74382b72009-08-23 22:45:37 +00001056 << ".s\n";
1057 outs() << " gcc " << SharedObject << " " << TestModuleBC.str()
1058 << ".s -o " << TestModuleBC.str() << ".exe";
Chris Lattner59615f02005-01-15 00:07:19 +00001059#if defined (HAVE_LINK_R)
Dan Gohmanac95cc72009-07-16 15:30:09 +00001060 outs() << " -Wl,-R.";
Chris Lattner59615f02005-01-15 00:07:19 +00001061#endif
Dan Gohmanac95cc72009-07-16 15:30:09 +00001062 outs() << "\n";
Chris Lattner74382b72009-08-23 22:45:37 +00001063 outs() << " " << TestModuleBC.str() << ".exe";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001064 }
Nick Lewycky22ff7482010-04-12 05:08:25 +00001065 for (unsigned i = 0, e = InputArgv.size(); i != e; ++i)
Dan Gohmanac95cc72009-07-16 15:30:09 +00001066 outs() << " " << InputArgv[i];
1067 outs() << '\n';
1068 outs() << "The shared object was created with:\n llc -march=c "
Chris Lattner74382b72009-08-23 22:45:37 +00001069 << SafeModuleBC.str() << " -o temporary.c\n"
Daniel Dunbarca740962009-08-18 03:35:57 +00001070 << " gcc -xc temporary.c -O2 -o " << SharedObject;
1071 if (TargetTriple.getArch() == Triple::sparc)
1072 outs() << " -G"; // Compile a shared library, `-G' for Sparc
1073 else
1074 outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others
1075
1076 outs() << " -fno-strict-aliasing\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001077
1078 return false;
1079}