blob: 9c14147295694fbdea23bb0ff601234ef04c0697 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Miscompilation.cpp - Debug program miscompilations -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements optimizer and code generation miscompilation debugging
11// support.
12//
13//===----------------------------------------------------------------------===//
14
15#include "BugDriver.h"
16#include "ListReducer.h"
Daniel Dunbard8590af2009-08-18 03:35:57 +000017#include "ToolRunner.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Instructions.h"
21#include "llvm/Linker.h"
22#include "llvm/Module.h"
23#include "llvm/Pass.h"
24#include "llvm/Analysis/Verifier.h"
25#include "llvm/Support/Mangler.h"
26#include "llvm/Transforms/Utils/Cloning.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/FileUtilities.h"
29#include "llvm/Config/config.h" // for HAVE_LINK_R
30using namespace llvm;
31
32namespace llvm {
33 extern cl::list<std::string> InputArgv;
34}
35
36namespace {
37 static llvm::cl::opt<bool>
38 DisableLoopExtraction("disable-loop-extraction",
39 cl::desc("Don't extract loops when searching for miscompilations"),
40 cl::init(false));
David Goodwin25c07052009-07-28 23:08:36 +000041 static llvm::cl::opt<bool>
42 DisableBlockExtraction("disable-block-extraction",
43 cl::desc("Don't extract blocks when searching for miscompilations"),
44 cl::init(false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045
46 class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
47 BugDriver &BD;
48 public:
49 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
50
51 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
52 std::vector<const PassInfo*> &Suffix);
53 };
54}
55
56/// TestResult - After passes have been split into a test group and a control
57/// group, see if they still break the program.
58///
59ReduceMiscompilingPasses::TestResult
60ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
61 std::vector<const PassInfo*> &Suffix) {
62 // First, run the program with just the Suffix passes. If it is still broken
63 // with JUST the kept passes, discard the prefix passes.
Dan Gohmanb714fab2009-07-16 15:30:09 +000064 outs() << "Checking to see if '" << getPassesString(Suffix)
65 << "' compiles correctly: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066
67 std::string BitcodeResult;
68 if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000069 errs() << " Error running this sequence of passes"
70 << " on the input program!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 BD.setPassesToRun(Suffix);
72 BD.EmitProgressBitcode("pass-error", false);
73 exit(BD.debugOptimizerCrash());
74 }
Owen Anderson9f5b2aa2009-07-14 23:09:55 +000075
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 // Check to see if the finished program matches the reference output...
77 if (BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/)) {
Dan Gohmanb714fab2009-07-16 15:30:09 +000078 outs() << " nope.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 if (Suffix.empty()) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000080 errs() << BD.getToolName() << ": I'm confused: the test fails when "
81 << "no passes are run, nondeterministic program?\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 exit(1);
83 }
84 return KeepSuffix; // Miscompilation detected!
85 }
Dan Gohmanb714fab2009-07-16 15:30:09 +000086 outs() << " yup.\n"; // No miscompilation!
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
88 if (Prefix.empty()) return NoFailure;
89
90 // Next, see if the program is broken if we run the "prefix" passes first,
91 // then separately run the "kept" passes.
Dan Gohmanb714fab2009-07-16 15:30:09 +000092 outs() << "Checking to see if '" << getPassesString(Prefix)
93 << "' compiles correctly: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094
95 // If it is not broken with the kept passes, it's possible that the prefix
96 // passes must be run before the kept passes to break it. If the program
97 // WORKS after the prefix passes, but then fails if running the prefix AND
98 // kept passes, we can update our bitcode file to include the result of the
99 // prefix passes, then discard the prefix passes.
100 //
101 if (BD.runPasses(Prefix, BitcodeResult, false/*delete*/, true/*quiet*/)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000102 errs() << " Error running this sequence of passes"
103 << " on the input program!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 BD.setPassesToRun(Prefix);
105 BD.EmitProgressBitcode("pass-error", false);
106 exit(BD.debugOptimizerCrash());
107 }
108
109 // If the prefix maintains the predicate by itself, only keep the prefix!
110 if (BD.diffProgram(BitcodeResult)) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000111 outs() << " nope.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 sys::Path(BitcodeResult).eraseFromDisk();
113 return KeepPrefix;
114 }
Dan Gohmanb714fab2009-07-16 15:30:09 +0000115 outs() << " yup.\n"; // No miscompilation!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116
117 // Ok, so now we know that the prefix passes work, try running the suffix
118 // passes on the result of the prefix passes.
119 //
Owen Anderson25209b42009-07-01 16:58:40 +0000120 Module *PrefixOutput = ParseInputFile(BitcodeResult, BD.getContext());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 if (PrefixOutput == 0) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000122 errs() << BD.getToolName() << ": Error reading bitcode file '"
123 << BitcodeResult << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 exit(1);
125 }
126 sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk
127
128 // Don't check if there are no passes in the suffix.
129 if (Suffix.empty())
130 return NoFailure;
131
Dan Gohmanb714fab2009-07-16 15:30:09 +0000132 outs() << "Checking to see if '" << getPassesString(Suffix)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 << "' passes compile correctly after the '"
134 << getPassesString(Prefix) << "' passes: ";
135
136 Module *OriginalInput = BD.swapProgramIn(PrefixOutput);
137 if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000138 errs() << " Error running this sequence of passes"
139 << " on the input program!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 BD.setPassesToRun(Suffix);
141 BD.EmitProgressBitcode("pass-error", false);
142 exit(BD.debugOptimizerCrash());
143 }
144
145 // Run the result...
146 if (BD.diffProgram(BitcodeResult, "", true/*delete bitcode*/)) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000147 outs() << " nope.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 delete OriginalInput; // We pruned down the original input...
149 return KeepSuffix;
150 }
151
152 // Otherwise, we must not be running the bad pass anymore.
Dan Gohmanb714fab2009-07-16 15:30:09 +0000153 outs() << " yup.\n"; // No miscompilation!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test
155 return NoFailure;
156}
157
158namespace {
159 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
160 BugDriver &BD;
161 bool (*TestFn)(BugDriver &, Module *, Module *);
162 public:
163 ReduceMiscompilingFunctions(BugDriver &bd,
164 bool (*F)(BugDriver &, Module *, Module *))
165 : BD(bd), TestFn(F) {}
166
167 virtual TestResult doTest(std::vector<Function*> &Prefix,
168 std::vector<Function*> &Suffix) {
169 if (!Suffix.empty() && TestFuncs(Suffix))
170 return KeepSuffix;
171 if (!Prefix.empty() && TestFuncs(Prefix))
172 return KeepPrefix;
173 return NoFailure;
174 }
175
176 bool TestFuncs(const std::vector<Function*> &Prefix);
177 };
178}
179
180/// TestMergedProgram - Given two modules, link them together and run the
181/// program, checking to see if the program matches the diff. If the diff
182/// matches, return false, otherwise return true. If the DeleteInputs argument
183/// is set to true then this function deletes both input modules before it
184/// returns.
185///
186static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
187 bool DeleteInputs) {
188 // Link the two portions of the program back to together.
189 std::string ErrorMsg;
190 if (!DeleteInputs) {
191 M1 = CloneModule(M1);
192 M2 = CloneModule(M2);
193 }
194 if (Linker::LinkModules(M1, M2, &ErrorMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000195 errs() << BD.getToolName() << ": Error linking modules together:"
196 << ErrorMsg << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 exit(1);
198 }
199 delete M2; // We are done with this module.
200
201 Module *OldProgram = BD.swapProgramIn(M1);
202
203 // Execute the program. If it does not match the expected output, we must
204 // return true.
205 bool Broken = BD.diffProgram();
206
207 // Delete the linked module & restore the original
208 BD.swapProgramIn(OldProgram);
209 delete M1;
210 return Broken;
211}
212
213/// TestFuncs - split functions in a Module into two groups: those that are
214/// under consideration for miscompilation vs. those that are not, and test
215/// accordingly. Each group of functions becomes a separate Module.
216///
217bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
218 // Test to see if the function is misoptimized if we ONLY run it on the
219 // functions listed in Funcs.
Dan Gohmanb714fab2009-07-16 15:30:09 +0000220 outs() << "Checking to see if the program is misoptimized when "
221 << (Funcs.size()==1 ? "this function is" : "these functions are")
222 << " run through the pass"
223 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 PrintFunctionList(Funcs);
Dan Gohmanb714fab2009-07-16 15:30:09 +0000225 outs() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226
227 // Split the module into the two halves of the program we want.
Dan Gohman819b9562009-04-22 15:57:18 +0000228 DenseMap<const Value*, Value*> ValueMap;
229 Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap);
230 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs,
231 ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232
Nick Lewycky43e736d2007-11-14 06:47:06 +0000233 // Run the predicate, note that the predicate will delete both input modules.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 return TestFn(BD, ToOptimize, ToNotOptimize);
235}
236
237/// DisambiguateGlobalSymbols - Mangle symbols to guarantee uniqueness by
238/// modifying predominantly internal symbols rather than external ones.
239///
240static void DisambiguateGlobalSymbols(Module *M) {
241 // Try not to cause collisions by minimizing chances of renaming an
242 // already-external symbol, so take in external globals and functions as-is.
243 // The code should work correctly without disambiguation (assuming the same
244 // mangler is used by the two code generators), but having symbols with the
245 // same name causes warnings to be emitted by the code generator.
246 Mangler Mang(*M);
247 // Agree with the CBE on symbol naming
248 Mang.markCharUnacceptable('.');
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
Chris Lattner98a5ca12009-07-15 04:50:47 +0000250 I != E; ++I) {
251 // Don't mangle asm names.
252 if (!I->hasName() || I->getName()[0] != 1)
253 I->setName(Mang.getMangledName(I));
254 }
255 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattner6baff2f2009-07-19 20:19:04 +0000256 // Don't mangle asm names or intrinsics.
Chris Lattnerb053ed42009-07-19 20:19:25 +0000257 if ((!I->hasName() || I->getName()[0] != 1) &&
258 I->getIntrinsicID() == 0)
Chris Lattner98a5ca12009-07-15 04:50:47 +0000259 I->setName(Mang.getMangledName(I));
260 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261}
262
263/// ExtractLoops - Given a reduced list of functions that still exposed the bug,
264/// check to see if we can extract the loops in the region without obscuring the
265/// bug. If so, it reduces the amount of code identified.
266///
267static bool ExtractLoops(BugDriver &BD,
268 bool (*TestFn)(BugDriver &, Module *, Module *),
269 std::vector<Function*> &MiscompiledFunctions) {
270 bool MadeChange = false;
271 while (1) {
272 if (BugpointIsInterrupted) return MadeChange;
273
Dan Gohman819b9562009-04-22 15:57:18 +0000274 DenseMap<const Value*, Value*> ValueMap;
275 Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Dan Gohman819b9562009-04-22 15:57:18 +0000277 MiscompiledFunctions,
278 ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
280 if (!ToOptimizeLoopExtracted) {
281 // If the loop extractor crashed or if there were no extractible loops,
282 // then this chapter of our odyssey is over with.
283 delete ToNotOptimize;
284 delete ToOptimize;
285 return MadeChange;
286 }
287
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000288 errs() << "Extracted a loop from the breaking portion of the program.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289
290 // Bugpoint is intentionally not very trusting of LLVM transformations. In
291 // particular, we're not going to assume that the loop extractor works, so
292 // we're going to test the newly loop extracted program to make sure nothing
293 // has broken. If something broke, then we'll inform the user and stop
294 // extraction.
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000295 AbstractInterpreter *AI = BD.switchToSafeInterpreter();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) {
297 BD.switchToInterpreter(AI);
298
299 // Merged program doesn't work anymore!
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000300 errs() << " *** ERROR: Loop extraction broke the program. :("
301 << " Please report a bug!\n";
302 errs() << " Continuing on with un-loop-extracted version.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303
304 BD.writeProgramToFile("bugpoint-loop-extract-fail-tno.bc", ToNotOptimize);
305 BD.writeProgramToFile("bugpoint-loop-extract-fail-to.bc", ToOptimize);
306 BD.writeProgramToFile("bugpoint-loop-extract-fail-to-le.bc",
307 ToOptimizeLoopExtracted);
308
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000309 errs() << "Please submit the bugpoint-loop-extract-fail-*.bc files.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 delete ToOptimize;
311 delete ToNotOptimize;
312 delete ToOptimizeLoopExtracted;
313 return MadeChange;
314 }
315 delete ToOptimize;
316 BD.switchToInterpreter(AI);
317
Dan Gohmanb714fab2009-07-16 15:30:09 +0000318 outs() << " Testing after loop extraction:\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 // Clone modules, the tester function will free them.
320 Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
321 Module *TNOBackup = CloneModule(ToNotOptimize);
322 if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000323 outs() << "*** Loop extraction masked the problem. Undoing.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 // If the program is not still broken, then loop extraction did something
325 // that masked the error. Stop loop extraction now.
326 delete TOLEBackup;
327 delete TNOBackup;
328 return MadeChange;
329 }
330 ToOptimizeLoopExtracted = TOLEBackup;
331 ToNotOptimize = TNOBackup;
332
Dan Gohmanb714fab2009-07-16 15:30:09 +0000333 outs() << "*** Loop extraction successful!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334
335 std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions;
336 for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
337 E = ToOptimizeLoopExtracted->end(); I != E; ++I)
338 if (!I->isDeclaration())
339 MisCompFunctions.push_back(std::make_pair(I->getName(),
340 I->getFunctionType()));
341
342 // Okay, great! Now we know that we extracted a loop and that loop
343 // extraction both didn't break the program, and didn't mask the problem.
344 // Replace the current program with the loop extracted version, and try to
345 // extract another loop.
346 std::string ErrorMsg;
347 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)){
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000348 errs() << BD.getToolName() << ": Error linking modules together:"
349 << ErrorMsg << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 exit(1);
351 }
352 delete ToOptimizeLoopExtracted;
353
354 // All of the Function*'s in the MiscompiledFunctions list are in the old
355 // module. Update this list to include all of the functions in the
356 // optimized and loop extracted module.
357 MiscompiledFunctions.clear();
358 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
359 Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);
360
361 assert(NewF && "Function not found??");
362 assert(NewF->getFunctionType() == MisCompFunctions[i].second &&
363 "found wrong function type?");
364 MiscompiledFunctions.push_back(NewF);
365 }
366
367 BD.setNewProgram(ToNotOptimize);
368 MadeChange = true;
369 }
370}
371
372namespace {
373 class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
374 BugDriver &BD;
375 bool (*TestFn)(BugDriver &, Module *, Module *);
376 std::vector<Function*> FunctionsBeingTested;
377 public:
378 ReduceMiscompiledBlocks(BugDriver &bd,
379 bool (*F)(BugDriver &, Module *, Module *),
380 const std::vector<Function*> &Fns)
381 : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
382
383 virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
384 std::vector<BasicBlock*> &Suffix) {
385 if (!Suffix.empty() && TestFuncs(Suffix))
386 return KeepSuffix;
387 if (TestFuncs(Prefix))
388 return KeepPrefix;
389 return NoFailure;
390 }
391
392 bool TestFuncs(const std::vector<BasicBlock*> &Prefix);
393 };
394}
395
396/// TestFuncs - Extract all blocks for the miscompiled functions except for the
397/// specified blocks. If the problem still exists, return true.
398///
399bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs) {
400 // Test to see if the function is misoptimized if we ONLY run it on the
401 // functions listed in Funcs.
Dan Gohmanb714fab2009-07-16 15:30:09 +0000402 outs() << "Checking to see if the program is misoptimized when all ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 if (!BBs.empty()) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000404 outs() << "but these " << BBs.size() << " blocks are extracted: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
Dan Gohmanb714fab2009-07-16 15:30:09 +0000406 outs() << BBs[i]->getName() << " ";
407 if (BBs.size() > 10) outs() << "...";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 } else {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000409 outs() << "blocks are extracted.";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 }
Dan Gohmanb714fab2009-07-16 15:30:09 +0000411 outs() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412
413 // Split the module into the two halves of the program we want.
Dan Gohman819b9562009-04-22 15:57:18 +0000414 DenseMap<const Value*, Value*> ValueMap;
415 Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Dan Gohman819b9562009-04-22 15:57:18 +0000417 FunctionsBeingTested,
418 ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419
420 // Try the extraction. If it doesn't work, then the block extractor crashed
421 // or something, in which case bugpoint can't chase down this possibility.
422 if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) {
423 delete ToOptimize;
424 // Run the predicate, not that the predicate will delete both input modules.
425 return TestFn(BD, New, ToNotOptimize);
426 }
427 delete ToOptimize;
428 delete ToNotOptimize;
429 return false;
430}
431
432
433/// ExtractBlocks - Given a reduced list of functions that still expose the bug,
434/// extract as many basic blocks from the region as possible without obscuring
435/// the bug.
436///
437static bool ExtractBlocks(BugDriver &BD,
438 bool (*TestFn)(BugDriver &, Module *, Module *),
439 std::vector<Function*> &MiscompiledFunctions) {
440 if (BugpointIsInterrupted) return false;
441
442 std::vector<BasicBlock*> Blocks;
443 for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
444 for (Function::iterator I = MiscompiledFunctions[i]->begin(),
445 E = MiscompiledFunctions[i]->end(); I != E; ++I)
446 Blocks.push_back(I);
447
448 // Use the list reducer to identify blocks that can be extracted without
449 // obscuring the bug. The Blocks list will end up containing blocks that must
450 // be retained from the original program.
451 unsigned OldSize = Blocks.size();
452
453 // Check to see if all blocks are extractible first.
454 if (ReduceMiscompiledBlocks(BD, TestFn,
455 MiscompiledFunctions).TestFuncs(std::vector<BasicBlock*>())) {
456 Blocks.clear();
457 } else {
458 ReduceMiscompiledBlocks(BD, TestFn,MiscompiledFunctions).reduceList(Blocks);
459 if (Blocks.size() == OldSize)
460 return false;
461 }
462
Dan Gohman819b9562009-04-22 15:57:18 +0000463 DenseMap<const Value*, Value*> ValueMap;
464 Module *ProgClone = CloneModule(BD.getProgram(), ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 Module *ToExtract = SplitFunctionsOutOfModule(ProgClone,
Dan Gohman819b9562009-04-22 15:57:18 +0000466 MiscompiledFunctions,
467 ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract);
469 if (Extracted == 0) {
470 // Weird, extraction should have worked.
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000471 errs() << "Nondeterministic problem extracting blocks??\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 delete ProgClone;
473 delete ToExtract;
474 return false;
475 }
476
477 // Otherwise, block extraction succeeded. Link the two program fragments back
478 // together.
479 delete ToExtract;
480
481 std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions;
482 for (Module::iterator I = Extracted->begin(), E = Extracted->end();
483 I != E; ++I)
484 if (!I->isDeclaration())
485 MisCompFunctions.push_back(std::make_pair(I->getName(),
486 I->getFunctionType()));
487
488 std::string ErrorMsg;
489 if (Linker::LinkModules(ProgClone, Extracted, &ErrorMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000490 errs() << BD.getToolName() << ": Error linking modules together:"
491 << ErrorMsg << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 exit(1);
493 }
494 delete Extracted;
495
496 // Set the new program and delete the old one.
497 BD.setNewProgram(ProgClone);
498
499 // Update the list of miscompiled functions.
500 MiscompiledFunctions.clear();
501
502 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
503 Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first);
504 assert(NewF && "Function not found??");
505 assert(NewF->getFunctionType() == MisCompFunctions[i].second &&
506 "Function has wrong type??");
507 MiscompiledFunctions.push_back(NewF);
508 }
509
510 return true;
511}
512
513
514/// DebugAMiscompilation - This is a generic driver to narrow down
515/// miscompilations, either in an optimization or a code generator.
516///
517static std::vector<Function*>
518DebugAMiscompilation(BugDriver &BD,
519 bool (*TestFn)(BugDriver &, Module *, Module *)) {
520 // Okay, now that we have reduced the list of passes which are causing the
521 // failure, see if we can pin down which functions are being
522 // miscompiled... first build a list of all of the non-external functions in
523 // the program.
524 std::vector<Function*> MiscompiledFunctions;
525 Module *Prog = BD.getProgram();
526 for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
527 if (!I->isDeclaration())
528 MiscompiledFunctions.push_back(I);
529
530 // Do the reduction...
531 if (!BugpointIsInterrupted)
532 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
533
Dan Gohmanb714fab2009-07-16 15:30:09 +0000534 outs() << "\n*** The following function"
535 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
536 << " being miscompiled: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 PrintFunctionList(MiscompiledFunctions);
Dan Gohmanb714fab2009-07-16 15:30:09 +0000538 outs() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539
540 // See if we can rip any loops out of the miscompiled functions and still
541 // trigger the problem.
542
543 if (!BugpointIsInterrupted && !DisableLoopExtraction &&
544 ExtractLoops(BD, TestFn, MiscompiledFunctions)) {
545 // Okay, we extracted some loops and the problem still appears. See if we
546 // can eliminate some of the created functions from being candidates.
547
548 // Loop extraction can introduce functions with the same name (foo_code).
549 // Make sure to disambiguate the symbols so that when the program is split
550 // apart that we can link it back together again.
551 DisambiguateGlobalSymbols(BD.getProgram());
552
553 // Do the reduction...
554 if (!BugpointIsInterrupted)
555 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
556
Dan Gohmanb714fab2009-07-16 15:30:09 +0000557 outs() << "\n*** The following function"
558 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
559 << " being miscompiled: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 PrintFunctionList(MiscompiledFunctions);
Dan Gohmanb714fab2009-07-16 15:30:09 +0000561 outs() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 }
563
David Goodwin25c07052009-07-28 23:08:36 +0000564 if (!BugpointIsInterrupted && !DisableBlockExtraction &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 ExtractBlocks(BD, TestFn, MiscompiledFunctions)) {
566 // Okay, we extracted some blocks and the problem still appears. See if we
567 // can eliminate some of the created functions from being candidates.
568
569 // Block extraction can introduce functions with the same name (foo_code).
570 // Make sure to disambiguate the symbols so that when the program is split
571 // apart that we can link it back together again.
572 DisambiguateGlobalSymbols(BD.getProgram());
573
574 // Do the reduction...
575 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
576
Dan Gohmanb714fab2009-07-16 15:30:09 +0000577 outs() << "\n*** The following function"
578 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
579 << " being miscompiled: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 PrintFunctionList(MiscompiledFunctions);
Dan Gohmanb714fab2009-07-16 15:30:09 +0000581 outs() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 }
583
584 return MiscompiledFunctions;
585}
586
587/// TestOptimizer - This is the predicate function used to check to see if the
588/// "Test" portion of the program is misoptimized. If so, return true. In any
589/// case, both module arguments are deleted.
590///
591static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
592 // Run the optimization passes on ToOptimize, producing a transformed version
593 // of the functions being tested.
Dan Gohmanb714fab2009-07-16 15:30:09 +0000594 outs() << " Optimizing functions being tested: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
596 /*AutoDebugCrashes*/true);
Dan Gohmanb714fab2009-07-16 15:30:09 +0000597 outs() << "done.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 delete Test;
599
Dan Gohmanb714fab2009-07-16 15:30:09 +0000600 outs() << " Checking to see if the merged program executes correctly: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 bool Broken = TestMergedProgram(BD, Optimized, Safe, true);
Dan Gohmanb714fab2009-07-16 15:30:09 +0000602 outs() << (Broken ? " nope.\n" : " yup.\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 return Broken;
604}
605
606
607/// debugMiscompilation - This method is used when the passes selected are not
608/// crashing, but the generated output is semantically different from the
609/// input.
610///
611bool BugDriver::debugMiscompilation() {
612 // Make sure something was miscompiled...
613 if (!BugpointIsInterrupted)
614 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000615 errs() << "*** Optimized program matches reference output! No problem"
616 << " detected...\nbugpoint can't help you with your problem!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 return false;
618 }
619
Dan Gohmanb714fab2009-07-16 15:30:09 +0000620 outs() << "\n*** Found miscompiling pass"
621 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
622 << getPassesString(getPassesToRun()) << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 EmitProgressBitcode("passinput");
624
625 std::vector<Function*> MiscompiledFunctions =
626 DebugAMiscompilation(*this, TestOptimizer);
627
628 // Output a bunch of bitcode files for the user...
Dan Gohmanb714fab2009-07-16 15:30:09 +0000629 outs() << "Outputting reduced bitcode files which expose the problem:\n";
Dan Gohman819b9562009-04-22 15:57:18 +0000630 DenseMap<const Value*, Value*> ValueMap;
631 Module *ToNotOptimize = CloneModule(getProgram(), ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Dan Gohman819b9562009-04-22 15:57:18 +0000633 MiscompiledFunctions,
634 ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635
Dan Gohmanb714fab2009-07-16 15:30:09 +0000636 outs() << " Non-optimized portion: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 ToNotOptimize = swapProgramIn(ToNotOptimize);
638 EmitProgressBitcode("tonotoptimize", true);
639 setNewProgram(ToNotOptimize); // Delete hacked module.
640
Dan Gohmanb714fab2009-07-16 15:30:09 +0000641 outs() << " Portion that is input to optimizer: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 ToOptimize = swapProgramIn(ToOptimize);
643 EmitProgressBitcode("tooptimize");
644 setNewProgram(ToOptimize); // Delete hacked module.
645
646 return false;
647}
648
649/// CleanupAndPrepareModules - Get the specified modules ready for code
650/// generator testing.
651///
652static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
653 Module *Safe) {
654 // Clean up the modules, removing extra cruft that we don't need anymore...
655 Test = BD.performFinalCleanups(Test);
656
657 // If we are executing the JIT, we have several nasty issues to take care of.
658 if (!BD.isExecutingJIT()) return;
659
660 // First, if the main function is in the Safe module, we must add a stub to
661 // the Test module to call into it. Thus, we create a new function `main'
662 // which just calls the old one.
663 if (Function *oldMain = Safe->getFunction("main"))
664 if (!oldMain->isDeclaration()) {
665 // Rename it
666 oldMain->setName("llvm_bugpoint_old_main");
667 // Create a NEW `main' function with same type in the test module.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000668 Function *newMain = Function::Create(oldMain->getFunctionType(),
669 GlobalValue::ExternalLinkage,
670 "main", Test);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 // Create an `oldmain' prototype in the test module, which will
672 // corresponds to the real main function in the same module.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000673 Function *oldMainProto = Function::Create(oldMain->getFunctionType(),
674 GlobalValue::ExternalLinkage,
675 oldMain->getName(), Test);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 // Set up and remember the argument list for the main function.
677 std::vector<Value*> args;
678 for (Function::arg_iterator
679 I = newMain->arg_begin(), E = newMain->arg_end(),
680 OI = oldMain->arg_begin(); I != E; ++I, ++OI) {
Owen Andersonab567f82008-04-14 17:38:21 +0000681 I->setName(OI->getName()); // Copy argument names from oldMain
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 args.push_back(I);
683 }
684
685 // Call the old main function and return its result
Owen Anderson35b47072009-08-13 21:58:54 +0000686 BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000687 CallInst *call = CallInst::Create(oldMainProto, args.begin(), args.end(),
688 "", BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689
690 // If the type of old function wasn't void, return value of call
Owen Anderson35b47072009-08-13 21:58:54 +0000691 ReturnInst::Create(Safe->getContext(), call, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 }
693
694 // The second nasty issue we must deal with in the JIT is that the Safe
695 // module cannot directly reference any functions defined in the test
696 // module. Instead, we use a JIT API call to dynamically resolve the
697 // symbol.
698
699 // Add the resolver to the Safe module.
700 // Prototype: void *getPointerToNamedFunction(const char* Name)
701 Constant *resolverFunc =
702 Safe->getOrInsertFunction("getPointerToNamedFunction",
Owen Anderson35b47072009-08-13 21:58:54 +0000703 PointerType::getUnqual(Type::getInt8Ty(Safe->getContext())),
704 PointerType::getUnqual(Type::getInt8Ty(Safe->getContext())),
705 (Type *)0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706
707 // Use the function we just added to get addresses of functions we need.
708 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
709 if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc &&
Duncan Sands79d28872007-12-03 20:06:50 +0000710 !F->isIntrinsic() /* ignore intrinsics */) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 Function *TestFn = Test->getFunction(F->getName());
712
713 // Don't forward functions which are external in the test module too.
714 if (TestFn && !TestFn->isDeclaration()) {
715 // 1. Add a string constant with its name to the global file
Owen Anderson35b47072009-08-13 21:58:54 +0000716 Constant *InitArray = ConstantArray::get(F->getContext(), F->getName());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 GlobalVariable *funcName =
Owen Andersone17fc1d2009-07-08 19:03:57 +0000718 new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 GlobalValue::InternalLinkage, InitArray,
Owen Andersone17fc1d2009-07-08 19:03:57 +0000720 F->getName() + "_name");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721
722 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
723 // sbyte* so it matches the signature of the resolver function.
724
725 // GetElementPtr *funcName, ulong 0, ulong 0
Owen Anderson35b47072009-08-13 21:58:54 +0000726 std::vector<Constant*> GEPargs(2,
727 Constant::getNullValue(Type::getInt32Ty(F->getContext())));
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000728 Value *GEP =
Owen Anderson02b48c32009-07-29 18:55:55 +0000729 ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 std::vector<Value*> ResolverArgs;
731 ResolverArgs.push_back(GEP);
732
733 // Rewrite uses of F in global initializers, etc. to uses of a wrapper
734 // function that dynamically resolves the calls to F via our JIT API
735 if (!F->use_empty()) {
736 // Create a new global to hold the cached function pointer.
Owen Andersonb99ecca2009-07-30 23:03:37 +0000737 Constant *NullPtr = ConstantPointerNull::get(F->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 GlobalVariable *Cache =
Owen Andersone17fc1d2009-07-08 19:03:57 +0000739 new GlobalVariable(*F->getParent(), F->getType(),
740 false, GlobalValue::InternalLinkage,
741 NullPtr,F->getName()+".fpcache");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742
743 // Construct a new stub function that will re-route calls to F
744 const FunctionType *FuncTy = F->getFunctionType();
Gabor Greifd6da1d02008-04-06 20:25:17 +0000745 Function *FuncWrapper = Function::Create(FuncTy,
746 GlobalValue::InternalLinkage,
747 F->getName() + "_wrapper",
748 F->getParent());
Owen Anderson35b47072009-08-13 21:58:54 +0000749 BasicBlock *EntryBB = BasicBlock::Create(F->getContext(),
750 "entry", FuncWrapper);
751 BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(),
752 "usecache", FuncWrapper);
753 BasicBlock *LookupBB = BasicBlock::Create(F->getContext(),
754 "lookupfp", FuncWrapper);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755
756 // Check to see if we already looked up the value.
757 Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB);
Owen Anderson6601fcd2009-07-09 23:48:35 +0000758 Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal,
759 NullPtr, "isNull");
Gabor Greifd6da1d02008-04-06 20:25:17 +0000760 BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761
762 // Resolve the call to function F via the JIT API:
763 //
764 // call resolver(GetElementPtr...)
Gabor Greifb91ea9d2008-05-15 10:04:30 +0000765 CallInst *Resolver =
766 CallInst::Create(resolverFunc, ResolverArgs.begin(),
767 ResolverArgs.end(), "resolver", LookupBB);
768
769 // Cast the result from the resolver to correctly-typed function.
770 CastInst *CastedResolver =
771 new BitCastInst(Resolver,
Owen Anderson6b6e2d92009-07-29 22:17:13 +0000772 PointerType::getUnqual(F->getFunctionType()),
Gabor Greifb91ea9d2008-05-15 10:04:30 +0000773 "resolverCast", LookupBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774
775 // Save the value in our cache.
776 new StoreInst(CastedResolver, Cache, LookupBB);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000777 BranchInst::Create(DoCallBB, LookupBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778
Gabor Greifb91ea9d2008-05-15 10:04:30 +0000779 PHINode *FuncPtr = PHINode::Create(NullPtr->getType(),
780 "fp", DoCallBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781 FuncPtr->addIncoming(CastedResolver, LookupBB);
782 FuncPtr->addIncoming(CachedVal, EntryBB);
783
784 // Save the argument list.
785 std::vector<Value*> Args;
786 for (Function::arg_iterator i = FuncWrapper->arg_begin(),
787 e = FuncWrapper->arg_end(); i != e; ++i)
788 Args.push_back(i);
789
790 // Pass on the arguments to the real function, return its result
Owen Anderson35b47072009-08-13 21:58:54 +0000791 if (F->getReturnType() == Type::getVoidTy(F->getContext())) {
Gabor Greifd6da1d02008-04-06 20:25:17 +0000792 CallInst::Create(FuncPtr, Args.begin(), Args.end(), "", DoCallBB);
Owen Anderson35b47072009-08-13 21:58:54 +0000793 ReturnInst::Create(F->getContext(), DoCallBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 } else {
Gabor Greifd6da1d02008-04-06 20:25:17 +0000795 CallInst *Call = CallInst::Create(FuncPtr, Args.begin(), Args.end(),
796 "retval", DoCallBB);
Owen Anderson35b47072009-08-13 21:58:54 +0000797 ReturnInst::Create(F->getContext(),Call, DoCallBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 }
799
800 // Use the wrapper function instead of the old function
801 F->replaceAllUsesWith(FuncWrapper);
802 }
803 }
804 }
805 }
806
807 if (verifyModule(*Test) || verifyModule(*Safe)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000808 errs() << "Bugpoint has a bug, which corrupted a module!!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 abort();
810 }
811}
812
813
814
815/// TestCodeGenerator - This is the predicate function used to check to see if
816/// the "Test" portion of the program is miscompiled by the code generator under
817/// test. If so, return true. In any case, both module arguments are deleted.
818///
819static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) {
820 CleanupAndPrepareModules(BD, Test, Safe);
821
822 sys::Path TestModuleBC("bugpoint.test.bc");
823 std::string ErrMsg;
824 if (TestModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000825 errs() << BD.getToolName() << "Error making unique filename: "
826 << ErrMsg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 exit(1);
828 }
829 if (BD.writeProgramToFile(TestModuleBC.toString(), Test)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000830 errs() << "Error writing bitcode to `" << TestModuleBC << "'\nExiting.";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 exit(1);
832 }
833 delete Test;
834
835 // Make the shared library
836 sys::Path SafeModuleBC("bugpoint.safe.bc");
837 if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000838 errs() << BD.getToolName() << "Error making unique filename: "
839 << ErrMsg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 exit(1);
841 }
842
843 if (BD.writeProgramToFile(SafeModuleBC.toString(), Safe)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000844 errs() << "Error writing bitcode to `" << SafeModuleBC << "'\nExiting.";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 exit(1);
846 }
847 std::string SharedObject = BD.compileSharedObject(SafeModuleBC.toString());
848 delete Safe;
849
850 // Run the code generator on the `Test' code, loading the shared library.
851 // The function returns whether or not the new output differs from reference.
852 int Result = BD.diffProgram(TestModuleBC.toString(), SharedObject, false);
853
854 if (Result)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000855 errs() << ": still failing!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 else
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000857 errs() << ": didn't fail.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 TestModuleBC.eraseFromDisk();
859 SafeModuleBC.eraseFromDisk();
860 sys::Path(SharedObject).eraseFromDisk();
861
862 return Result;
863}
864
865
866/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
867///
868bool BugDriver::debugCodeGenerator() {
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000869 if ((void*)SafeInterpreter == (void*)Interpreter) {
870 std::string Result = executeProgramSafely("bugpoint.safe.out");
Dan Gohmanb714fab2009-07-16 15:30:09 +0000871 outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match "
872 << "the reference diff. This may be due to a\n front-end "
873 << "bug or a bug in the original program, but this can also "
874 << "happen if bugpoint isn't running the program with the "
875 << "right flags or input.\n I left the result of executing "
876 << "the program with the \"safe\" backend in this file for "
877 << "you: '"
878 << Result << "'.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 return true;
880 }
881
882 DisambiguateGlobalSymbols(Program);
883
884 std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator);
885
886 // Split the module into the two halves of the program we want.
Dan Gohman819b9562009-04-22 15:57:18 +0000887 DenseMap<const Value*, Value*> ValueMap;
888 Module *ToNotCodeGen = CloneModule(getProgram(), ValueMap);
889 Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs, ValueMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890
891 // Condition the modules
892 CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
893
894 sys::Path TestModuleBC("bugpoint.test.bc");
895 std::string ErrMsg;
896 if (TestModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000897 errs() << getToolName() << "Error making unique filename: "
898 << ErrMsg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899 exit(1);
900 }
901
902 if (writeProgramToFile(TestModuleBC.toString(), ToCodeGen)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000903 errs() << "Error writing bitcode to `" << TestModuleBC << "'\nExiting.";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 exit(1);
905 }
906 delete ToCodeGen;
907
908 // Make the shared library
909 sys::Path SafeModuleBC("bugpoint.safe.bc");
910 if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000911 errs() << getToolName() << "Error making unique filename: "
912 << ErrMsg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 exit(1);
914 }
915
916 if (writeProgramToFile(SafeModuleBC.toString(), ToNotCodeGen)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000917 errs() << "Error writing bitcode to `" << SafeModuleBC << "'\nExiting.";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 exit(1);
919 }
920 std::string SharedObject = compileSharedObject(SafeModuleBC.toString());
921 delete ToNotCodeGen;
922
Dan Gohmanb714fab2009-07-16 15:30:09 +0000923 outs() << "You can reproduce the problem with the command line: \n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 if (isExecutingJIT()) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000925 outs() << " lli -load " << SharedObject << " " << TestModuleBC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926 } else {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000927 outs() << " llc -f " << TestModuleBC << " -o " << TestModuleBC<< ".s\n";
928 outs() << " gcc " << SharedObject << " " << TestModuleBC
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 << ".s -o " << TestModuleBC << ".exe";
930#if defined (HAVE_LINK_R)
Dan Gohmanb714fab2009-07-16 15:30:09 +0000931 outs() << " -Wl,-R.";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932#endif
Dan Gohmanb714fab2009-07-16 15:30:09 +0000933 outs() << "\n";
934 outs() << " " << TestModuleBC << ".exe";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 }
936 for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
Dan Gohmanb714fab2009-07-16 15:30:09 +0000937 outs() << " " << InputArgv[i];
938 outs() << '\n';
939 outs() << "The shared object was created with:\n llc -march=c "
940 << SafeModuleBC << " -o temporary.c\n"
Daniel Dunbard8590af2009-08-18 03:35:57 +0000941 << " gcc -xc temporary.c -O2 -o " << SharedObject;
942 if (TargetTriple.getArch() == Triple::sparc)
943 outs() << " -G"; // Compile a shared library, `-G' for Sparc
944 else
945 outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others
946
947 outs() << " -fno-strict-aliasing\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948
949 return false;
950}