1 //===- Miscompilation.cpp - Debug program miscompilations -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements optimizer and code generation miscompilation debugging
13 //===----------------------------------------------------------------------===//
15 #include "BugDriver.h"
16 #include "ListReducer.h"
17 #include "ToolRunner.h"
18 #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/Transforms/Utils/Cloning.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/FileUtilities.h"
28 #include "llvm/Config/config.h" // for HAVE_LINK_R
32 extern cl::opt<std::string> OutputPrefix;
33 extern cl::list<std::string> InputArgv;
37 static llvm::cl::opt<bool>
38 DisableLoopExtraction("disable-loop-extraction",
39 cl::desc("Don't extract loops when searching for miscompilations"),
41 static llvm::cl::opt<bool>
42 DisableBlockExtraction("disable-block-extraction",
43 cl::desc("Don't extract blocks when searching for miscompilations"),
46 class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
49 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
51 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
52 std::vector<const PassInfo*> &Suffix,
57 /// TestResult - After passes have been split into a test group and a control
58 /// group, see if they still break the program.
60 ReduceMiscompilingPasses::TestResult
61 ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
62 std::vector<const PassInfo*> &Suffix,
64 // First, run the program with just the Suffix passes. If it is still broken
65 // with JUST the kept passes, discard the prefix passes.
66 outs() << "Checking to see if '" << getPassesString(Suffix)
67 << "' compiles correctly: ";
69 std::string BitcodeResult;
70 if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) {
71 errs() << " Error running this sequence of passes"
72 << " on the input program!\n";
73 BD.setPassesToRun(Suffix);
74 BD.EmitProgressBitcode("pass-error", false);
75 exit(BD.debugOptimizerCrash());
78 // Check to see if the finished program matches the reference output...
79 bool Diff = BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/,
86 errs() << BD.getToolName() << ": I'm confused: the test fails when "
87 << "no passes are run, nondeterministic program?\n";
90 return KeepSuffix; // Miscompilation detected!
92 outs() << " yup.\n"; // No miscompilation!
94 if (Prefix.empty()) return NoFailure;
96 // Next, see if the program is broken if we run the "prefix" passes first,
97 // then separately run the "kept" passes.
98 outs() << "Checking to see if '" << getPassesString(Prefix)
99 << "' compiles correctly: ";
101 // If it is not broken with the kept passes, it's possible that the prefix
102 // passes must be run before the kept passes to break it. If the program
103 // WORKS after the prefix passes, but then fails if running the prefix AND
104 // kept passes, we can update our bitcode file to include the result of the
105 // prefix passes, then discard the prefix passes.
107 if (BD.runPasses(Prefix, BitcodeResult, false/*delete*/, true/*quiet*/)) {
108 errs() << " Error running this sequence of passes"
109 << " on the input program!\n";
110 BD.setPassesToRun(Prefix);
111 BD.EmitProgressBitcode("pass-error", false);
112 exit(BD.debugOptimizerCrash());
115 // If the prefix maintains the predicate by itself, only keep the prefix!
116 Diff = BD.diffProgram(BitcodeResult, "", false, &Error);
118 return InternalError;
120 outs() << " nope.\n";
121 sys::Path(BitcodeResult).eraseFromDisk();
124 outs() << " yup.\n"; // No miscompilation!
126 // Ok, so now we know that the prefix passes work, try running the suffix
127 // passes on the result of the prefix passes.
129 Module *PrefixOutput = ParseInputFile(BitcodeResult, BD.getContext());
130 if (PrefixOutput == 0) {
131 errs() << BD.getToolName() << ": Error reading bitcode file '"
132 << BitcodeResult << "'!\n";
135 sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk
137 // Don't check if there are no passes in the suffix.
141 outs() << "Checking to see if '" << getPassesString(Suffix)
142 << "' passes compile correctly after the '"
143 << getPassesString(Prefix) << "' passes: ";
145 Module *OriginalInput = BD.swapProgramIn(PrefixOutput);
146 if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) {
147 errs() << " Error running this sequence of passes"
148 << " on the input program!\n";
149 BD.setPassesToRun(Suffix);
150 BD.EmitProgressBitcode("pass-error", false);
151 exit(BD.debugOptimizerCrash());
155 Diff = BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/, &Error);
157 return InternalError;
159 outs() << " nope.\n";
160 delete OriginalInput; // We pruned down the original input...
164 // Otherwise, we must not be running the bad pass anymore.
165 outs() << " yup.\n"; // No miscompilation!
166 delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test
171 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
173 bool (*TestFn)(BugDriver &, Module *, Module *, std::string &);
175 ReduceMiscompilingFunctions(BugDriver &bd,
176 bool (*F)(BugDriver &, Module *, Module *,
178 : BD(bd), TestFn(F) {}
180 virtual TestResult doTest(std::vector<Function*> &Prefix,
181 std::vector<Function*> &Suffix,
182 std::string &Error) {
183 if (!Suffix.empty()) {
184 bool Ret = TestFuncs(Suffix, Error);
186 return InternalError;
190 if (!Prefix.empty()) {
191 bool Ret = TestFuncs(Prefix, Error);
193 return InternalError;
200 int TestFuncs(const std::vector<Function*> &Prefix, std::string &Error);
204 /// TestMergedProgram - Given two modules, link them together and run the
205 /// program, checking to see if the program matches the diff. If the diff
206 /// matches, return false, otherwise return true. If the DeleteInputs argument
207 /// is set to true then this function deletes both input modules before it
210 static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
211 bool DeleteInputs, std::string &Error) {
212 // Link the two portions of the program back to together.
213 std::string ErrorMsg;
215 M1 = CloneModule(M1);
216 M2 = CloneModule(M2);
218 if (Linker::LinkModules(M1, M2, &ErrorMsg)) {
219 errs() << BD.getToolName() << ": Error linking modules together:"
223 delete M2; // We are done with this module.
225 Module *OldProgram = BD.swapProgramIn(M1);
227 // Execute the program. If it does not match the expected output, we must
229 bool Broken = BD.diffProgram("", "", false, &Error);
230 if (!Error.empty()) {
231 // Delete the linked module & restore the original
232 BD.swapProgramIn(OldProgram);
238 /// TestFuncs - split functions in a Module into two groups: those that are
239 /// under consideration for miscompilation vs. those that are not, and test
240 /// accordingly. Each group of functions becomes a separate Module.
242 int ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
243 std::string &Error) {
244 // Test to see if the function is misoptimized if we ONLY run it on the
245 // functions listed in Funcs.
246 outs() << "Checking to see if the program is misoptimized when "
247 << (Funcs.size()==1 ? "this function is" : "these functions are")
248 << " run through the pass"
249 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
250 PrintFunctionList(Funcs);
253 // Split the module into the two halves of the program we want.
254 DenseMap<const Value*, Value*> ValueMap;
255 Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap);
256 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs,
259 // Run the predicate, note that the predicate will delete both input modules.
260 return TestFn(BD, ToOptimize, ToNotOptimize, Error);
263 /// DisambiguateGlobalSymbols - Give anonymous global values names.
265 static void DisambiguateGlobalSymbols(Module *M) {
266 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
269 I->setName("anon_global");
270 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
272 I->setName("anon_fn");
275 /// ExtractLoops - Given a reduced list of functions that still exposed the bug,
276 /// check to see if we can extract the loops in the region without obscuring the
277 /// bug. If so, it reduces the amount of code identified.
279 static bool ExtractLoops(BugDriver &BD,
280 bool (*TestFn)(BugDriver &, Module *, Module *,
282 std::vector<Function*> &MiscompiledFunctions,
283 std::string &Error) {
284 bool MadeChange = false;
286 if (BugpointIsInterrupted) return MadeChange;
288 DenseMap<const Value*, Value*> ValueMap;
289 Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap);
290 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
291 MiscompiledFunctions,
293 Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
294 if (!ToOptimizeLoopExtracted) {
295 // If the loop extractor crashed or if there were no extractible loops,
296 // then this chapter of our odyssey is over with.
297 delete ToNotOptimize;
302 errs() << "Extracted a loop from the breaking portion of the program.\n";
304 // Bugpoint is intentionally not very trusting of LLVM transformations. In
305 // particular, we're not going to assume that the loop extractor works, so
306 // we're going to test the newly loop extracted program to make sure nothing
307 // has broken. If something broke, then we'll inform the user and stop
309 AbstractInterpreter *AI = BD.switchToSafeInterpreter();
310 bool Failure = TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize,
315 BD.switchToInterpreter(AI);
317 // Merged program doesn't work anymore!
318 errs() << " *** ERROR: Loop extraction broke the program. :("
319 << " Please report a bug!\n";
320 errs() << " Continuing on with un-loop-extracted version.\n";
322 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc",
324 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc",
326 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc",
327 ToOptimizeLoopExtracted);
329 errs() << "Please submit the "
330 << OutputPrefix << "-loop-extract-fail-*.bc files.\n";
332 delete ToNotOptimize;
333 delete ToOptimizeLoopExtracted;
337 BD.switchToInterpreter(AI);
339 outs() << " Testing after loop extraction:\n";
340 // Clone modules, the tester function will free them.
341 Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
342 Module *TNOBackup = CloneModule(ToNotOptimize);
343 Failure = TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize, Error);
347 outs() << "*** Loop extraction masked the problem. Undoing.\n";
348 // If the program is not still broken, then loop extraction did something
349 // that masked the error. Stop loop extraction now.
354 ToOptimizeLoopExtracted = TOLEBackup;
355 ToNotOptimize = TNOBackup;
357 outs() << "*** Loop extraction successful!\n";
359 std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions;
360 for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
361 E = ToOptimizeLoopExtracted->end(); I != E; ++I)
362 if (!I->isDeclaration())
363 MisCompFunctions.push_back(std::make_pair(I->getName(),
364 I->getFunctionType()));
366 // Okay, great! Now we know that we extracted a loop and that loop
367 // extraction both didn't break the program, and didn't mask the problem.
368 // Replace the current program with the loop extracted version, and try to
369 // extract another loop.
370 std::string ErrorMsg;
371 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)){
372 errs() << BD.getToolName() << ": Error linking modules together:"
376 delete ToOptimizeLoopExtracted;
378 // All of the Function*'s in the MiscompiledFunctions list are in the old
379 // module. Update this list to include all of the functions in the
380 // optimized and loop extracted module.
381 MiscompiledFunctions.clear();
382 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
383 Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);
385 assert(NewF && "Function not found??");
386 assert(NewF->getFunctionType() == MisCompFunctions[i].second &&
387 "found wrong function type?");
388 MiscompiledFunctions.push_back(NewF);
391 BD.setNewProgram(ToNotOptimize);
397 class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
399 bool (*TestFn)(BugDriver &, Module *, Module *, std::string &);
400 std::vector<Function*> FunctionsBeingTested;
402 ReduceMiscompiledBlocks(BugDriver &bd,
403 bool (*F)(BugDriver &, Module *, Module *,
405 const std::vector<Function*> &Fns)
406 : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
408 virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
409 std::vector<BasicBlock*> &Suffix,
410 std::string &Error) {
411 if (!Suffix.empty()) {
412 bool Ret = TestFuncs(Suffix, Error);
414 return InternalError;
418 if (!Prefix.empty()) {
419 bool Ret = TestFuncs(Prefix, Error);
421 return InternalError;
428 bool TestFuncs(const std::vector<BasicBlock*> &BBs, std::string &Error);
432 /// TestFuncs - Extract all blocks for the miscompiled functions except for the
433 /// specified blocks. If the problem still exists, return true.
435 bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs,
436 std::string &Error) {
437 // Test to see if the function is misoptimized if we ONLY run it on the
438 // functions listed in Funcs.
439 outs() << "Checking to see if the program is misoptimized when all ";
441 outs() << "but these " << BBs.size() << " blocks are extracted: ";
442 for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
443 outs() << BBs[i]->getName() << " ";
444 if (BBs.size() > 10) outs() << "...";
446 outs() << "blocks are extracted.";
450 // Split the module into the two halves of the program we want.
451 DenseMap<const Value*, Value*> ValueMap;
452 Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap);
453 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
454 FunctionsBeingTested,
457 // Try the extraction. If it doesn't work, then the block extractor crashed
458 // or something, in which case bugpoint can't chase down this possibility.
459 if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) {
461 // Run the predicate, not that the predicate will delete both input modules.
462 return TestFn(BD, New, ToNotOptimize, Error);
465 delete ToNotOptimize;
470 /// ExtractBlocks - Given a reduced list of functions that still expose the bug,
471 /// extract as many basic blocks from the region as possible without obscuring
474 static bool ExtractBlocks(BugDriver &BD,
475 bool (*TestFn)(BugDriver &, Module *, Module *,
477 std::vector<Function*> &MiscompiledFunctions,
478 std::string &Error) {
479 if (BugpointIsInterrupted) return false;
481 std::vector<BasicBlock*> Blocks;
482 for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
483 for (Function::iterator I = MiscompiledFunctions[i]->begin(),
484 E = MiscompiledFunctions[i]->end(); I != E; ++I)
487 // Use the list reducer to identify blocks that can be extracted without
488 // obscuring the bug. The Blocks list will end up containing blocks that must
489 // be retained from the original program.
490 unsigned OldSize = Blocks.size();
492 // Check to see if all blocks are extractible first.
493 bool Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions)
494 .TestFuncs(std::vector<BasicBlock*>(), Error);
500 ReduceMiscompiledBlocks(BD, TestFn,
501 MiscompiledFunctions).reduceList(Blocks, Error);
504 if (Blocks.size() == OldSize)
508 DenseMap<const Value*, Value*> ValueMap;
509 Module *ProgClone = CloneModule(BD.getProgram(), ValueMap);
510 Module *ToExtract = SplitFunctionsOutOfModule(ProgClone,
511 MiscompiledFunctions,
513 Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract);
514 if (Extracted == 0) {
515 // Weird, extraction should have worked.
516 errs() << "Nondeterministic problem extracting blocks??\n";
522 // Otherwise, block extraction succeeded. Link the two program fragments back
526 std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions;
527 for (Module::iterator I = Extracted->begin(), E = Extracted->end();
529 if (!I->isDeclaration())
530 MisCompFunctions.push_back(std::make_pair(I->getName(),
531 I->getFunctionType()));
533 std::string ErrorMsg;
534 if (Linker::LinkModules(ProgClone, Extracted, &ErrorMsg)) {
535 errs() << BD.getToolName() << ": Error linking modules together:"
541 // Set the new program and delete the old one.
542 BD.setNewProgram(ProgClone);
544 // Update the list of miscompiled functions.
545 MiscompiledFunctions.clear();
547 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
548 Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first);
549 assert(NewF && "Function not found??");
550 assert(NewF->getFunctionType() == MisCompFunctions[i].second &&
551 "Function has wrong type??");
552 MiscompiledFunctions.push_back(NewF);
559 /// DebugAMiscompilation - This is a generic driver to narrow down
560 /// miscompilations, either in an optimization or a code generator.
562 static std::vector<Function*>
563 DebugAMiscompilation(BugDriver &BD,
564 bool (*TestFn)(BugDriver &, Module *, Module *,
566 std::string &Error) {
567 // Okay, now that we have reduced the list of passes which are causing the
568 // failure, see if we can pin down which functions are being
569 // miscompiled... first build a list of all of the non-external functions in
571 std::vector<Function*> MiscompiledFunctions;
572 Module *Prog = BD.getProgram();
573 for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
574 if (!I->isDeclaration())
575 MiscompiledFunctions.push_back(I);
577 // Do the reduction...
578 if (!BugpointIsInterrupted)
579 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
582 return MiscompiledFunctions;
584 outs() << "\n*** The following function"
585 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
586 << " being miscompiled: ";
587 PrintFunctionList(MiscompiledFunctions);
590 // See if we can rip any loops out of the miscompiled functions and still
591 // trigger the problem.
593 if (!BugpointIsInterrupted && !DisableLoopExtraction) {
594 bool Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions, Error);
596 return MiscompiledFunctions;
598 // Okay, we extracted some loops and the problem still appears. See if
599 // we can eliminate some of the created functions from being candidates.
600 DisambiguateGlobalSymbols(BD.getProgram());
602 // Do the reduction...
603 if (!BugpointIsInterrupted)
604 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
607 return MiscompiledFunctions;
609 outs() << "\n*** The following function"
610 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
611 << " being miscompiled: ";
612 PrintFunctionList(MiscompiledFunctions);
617 if (!BugpointIsInterrupted && !DisableBlockExtraction) {
618 bool Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions, Error);
620 return MiscompiledFunctions;
622 // Okay, we extracted some blocks and the problem still appears. See if
623 // we can eliminate some of the created functions from being candidates.
624 DisambiguateGlobalSymbols(BD.getProgram());
626 // Do the reduction...
627 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
630 return MiscompiledFunctions;
632 outs() << "\n*** The following function"
633 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
634 << " being miscompiled: ";
635 PrintFunctionList(MiscompiledFunctions);
640 return MiscompiledFunctions;
643 /// TestOptimizer - This is the predicate function used to check to see if the
644 /// "Test" portion of the program is misoptimized. If so, return true. In any
645 /// case, both module arguments are deleted.
647 static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe,
648 std::string &Error) {
649 // Run the optimization passes on ToOptimize, producing a transformed version
650 // of the functions being tested.
651 outs() << " Optimizing functions being tested: ";
652 Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
653 /*AutoDebugCrashes*/true);
657 outs() << " Checking to see if the merged program executes correctly: ";
658 bool Broken = TestMergedProgram(BD, Optimized, Safe, true, Error);
659 if (Error.empty()) outs() << (Broken ? " nope.\n" : " yup.\n");
664 /// debugMiscompilation - This method is used when the passes selected are not
665 /// crashing, but the generated output is semantically different from the
668 void BugDriver::debugMiscompilation(std::string *Error) {
669 // Make sure something was miscompiled...
670 if (!BugpointIsInterrupted)
671 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun, *Error)) {
673 errs() << "*** Optimized program matches reference output! No problem"
674 << " detected...\nbugpoint can't help you with your problem!\n";
678 outs() << "\n*** Found miscompiling pass"
679 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
680 << getPassesString(getPassesToRun()) << '\n';
681 EmitProgressBitcode("passinput");
683 std::vector<Function *> MiscompiledFunctions =
684 DebugAMiscompilation(*this, TestOptimizer, *Error);
688 // Output a bunch of bitcode files for the user...
689 outs() << "Outputting reduced bitcode files which expose the problem:\n";
690 DenseMap<const Value*, Value*> ValueMap;
691 Module *ToNotOptimize = CloneModule(getProgram(), ValueMap);
692 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
693 MiscompiledFunctions,
696 outs() << " Non-optimized portion: ";
697 ToNotOptimize = swapProgramIn(ToNotOptimize);
698 EmitProgressBitcode("tonotoptimize", true);
699 setNewProgram(ToNotOptimize); // Delete hacked module.
701 outs() << " Portion that is input to optimizer: ";
702 ToOptimize = swapProgramIn(ToOptimize);
703 EmitProgressBitcode("tooptimize");
704 setNewProgram(ToOptimize); // Delete hacked module.
709 /// CleanupAndPrepareModules - Get the specified modules ready for code
710 /// generator testing.
712 static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
714 // Clean up the modules, removing extra cruft that we don't need anymore...
715 Test = BD.performFinalCleanups(Test);
717 // If we are executing the JIT, we have several nasty issues to take care of.
718 if (!BD.isExecutingJIT()) return;
720 // First, if the main function is in the Safe module, we must add a stub to
721 // the Test module to call into it. Thus, we create a new function `main'
722 // which just calls the old one.
723 if (Function *oldMain = Safe->getFunction("main"))
724 if (!oldMain->isDeclaration()) {
726 oldMain->setName("llvm_bugpoint_old_main");
727 // Create a NEW `main' function with same type in the test module.
728 Function *newMain = Function::Create(oldMain->getFunctionType(),
729 GlobalValue::ExternalLinkage,
731 // Create an `oldmain' prototype in the test module, which will
732 // corresponds to the real main function in the same module.
733 Function *oldMainProto = Function::Create(oldMain->getFunctionType(),
734 GlobalValue::ExternalLinkage,
735 oldMain->getName(), Test);
736 // Set up and remember the argument list for the main function.
737 std::vector<Value*> args;
738 for (Function::arg_iterator
739 I = newMain->arg_begin(), E = newMain->arg_end(),
740 OI = oldMain->arg_begin(); I != E; ++I, ++OI) {
741 I->setName(OI->getName()); // Copy argument names from oldMain
745 // Call the old main function and return its result
746 BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain);
747 CallInst *call = CallInst::Create(oldMainProto, args.begin(), args.end(),
750 // If the type of old function wasn't void, return value of call
751 ReturnInst::Create(Safe->getContext(), call, BB);
754 // The second nasty issue we must deal with in the JIT is that the Safe
755 // module cannot directly reference any functions defined in the test
756 // module. Instead, we use a JIT API call to dynamically resolve the
759 // Add the resolver to the Safe module.
760 // Prototype: void *getPointerToNamedFunction(const char* Name)
761 Constant *resolverFunc =
762 Safe->getOrInsertFunction("getPointerToNamedFunction",
763 Type::getInt8PtrTy(Safe->getContext()),
764 Type::getInt8PtrTy(Safe->getContext()),
767 // Use the function we just added to get addresses of functions we need.
768 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
769 if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc &&
770 !F->isIntrinsic() /* ignore intrinsics */) {
771 Function *TestFn = Test->getFunction(F->getName());
773 // Don't forward functions which are external in the test module too.
774 if (TestFn && !TestFn->isDeclaration()) {
775 // 1. Add a string constant with its name to the global file
776 Constant *InitArray = ConstantArray::get(F->getContext(), F->getName());
777 GlobalVariable *funcName =
778 new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
779 GlobalValue::InternalLinkage, InitArray,
780 F->getName() + "_name");
782 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
783 // sbyte* so it matches the signature of the resolver function.
785 // GetElementPtr *funcName, ulong 0, ulong 0
786 std::vector<Constant*> GEPargs(2,
787 Constant::getNullValue(Type::getInt32Ty(F->getContext())));
789 ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2);
790 std::vector<Value*> ResolverArgs;
791 ResolverArgs.push_back(GEP);
793 // Rewrite uses of F in global initializers, etc. to uses of a wrapper
794 // function that dynamically resolves the calls to F via our JIT API
795 if (!F->use_empty()) {
796 // Create a new global to hold the cached function pointer.
797 Constant *NullPtr = ConstantPointerNull::get(F->getType());
798 GlobalVariable *Cache =
799 new GlobalVariable(*F->getParent(), F->getType(),
800 false, GlobalValue::InternalLinkage,
801 NullPtr,F->getName()+".fpcache");
803 // Construct a new stub function that will re-route calls to F
804 const FunctionType *FuncTy = F->getFunctionType();
805 Function *FuncWrapper = Function::Create(FuncTy,
806 GlobalValue::InternalLinkage,
807 F->getName() + "_wrapper",
809 BasicBlock *EntryBB = BasicBlock::Create(F->getContext(),
810 "entry", FuncWrapper);
811 BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(),
812 "usecache", FuncWrapper);
813 BasicBlock *LookupBB = BasicBlock::Create(F->getContext(),
814 "lookupfp", FuncWrapper);
816 // Check to see if we already looked up the value.
817 Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB);
818 Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal,
820 BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB);
822 // Resolve the call to function F via the JIT API:
824 // call resolver(GetElementPtr...)
826 CallInst::Create(resolverFunc, ResolverArgs.begin(),
827 ResolverArgs.end(), "resolver", LookupBB);
829 // Cast the result from the resolver to correctly-typed function.
830 CastInst *CastedResolver =
831 new BitCastInst(Resolver,
832 PointerType::getUnqual(F->getFunctionType()),
833 "resolverCast", LookupBB);
835 // Save the value in our cache.
836 new StoreInst(CastedResolver, Cache, LookupBB);
837 BranchInst::Create(DoCallBB, LookupBB);
839 PHINode *FuncPtr = PHINode::Create(NullPtr->getType(),
841 FuncPtr->addIncoming(CastedResolver, LookupBB);
842 FuncPtr->addIncoming(CachedVal, EntryBB);
844 // Save the argument list.
845 std::vector<Value*> Args;
846 for (Function::arg_iterator i = FuncWrapper->arg_begin(),
847 e = FuncWrapper->arg_end(); i != e; ++i)
850 // Pass on the arguments to the real function, return its result
851 if (F->getReturnType() == Type::getVoidTy(F->getContext())) {
852 CallInst::Create(FuncPtr, Args.begin(), Args.end(), "", DoCallBB);
853 ReturnInst::Create(F->getContext(), DoCallBB);
855 CallInst *Call = CallInst::Create(FuncPtr, Args.begin(), Args.end(),
857 ReturnInst::Create(F->getContext(),Call, DoCallBB);
860 // Use the wrapper function instead of the old function
861 F->replaceAllUsesWith(FuncWrapper);
867 if (verifyModule(*Test) || verifyModule(*Safe)) {
868 errs() << "Bugpoint has a bug, which corrupted a module!!\n";
875 /// TestCodeGenerator - This is the predicate function used to check to see if
876 /// the "Test" portion of the program is miscompiled by the code generator under
877 /// test. If so, return true. In any case, both module arguments are deleted.
879 static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe,
880 std::string &Error) {
881 CleanupAndPrepareModules(BD, Test, Safe);
883 sys::Path TestModuleBC("bugpoint.test.bc");
885 if (TestModuleBC.makeUnique(true, &ErrMsg)) {
886 errs() << BD.getToolName() << "Error making unique filename: "
890 if (BD.writeProgramToFile(TestModuleBC.str(), Test)) {
891 errs() << "Error writing bitcode to `" << TestModuleBC.str()
897 // Make the shared library
898 sys::Path SafeModuleBC("bugpoint.safe.bc");
899 if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
900 errs() << BD.getToolName() << "Error making unique filename: "
905 if (BD.writeProgramToFile(SafeModuleBC.str(), Safe)) {
906 errs() << "Error writing bitcode to `" << SafeModuleBC.str()
910 std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str(), Error);
915 // Run the code generator on the `Test' code, loading the shared library.
916 // The function returns whether or not the new output differs from reference.
917 bool Result = BD.diffProgram(TestModuleBC.str(), SharedObject, false, &Error);
922 errs() << ": still failing!\n";
924 errs() << ": didn't fail.\n";
925 TestModuleBC.eraseFromDisk();
926 SafeModuleBC.eraseFromDisk();
927 sys::Path(SharedObject).eraseFromDisk();
933 /// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
935 bool BugDriver::debugCodeGenerator(std::string *Error) {
936 if ((void*)SafeInterpreter == (void*)Interpreter) {
937 std::string Result = executeProgramSafely("bugpoint.safe.out", Error);
938 if (Error->empty()) {
939 outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match "
940 << "the reference diff. This may be due to a\n front-end "
941 << "bug or a bug in the original program, but this can also "
942 << "happen if bugpoint isn't running the program with the "
943 << "right flags or input.\n I left the result of executing "
944 << "the program with the \"safe\" backend in this file for "
951 DisambiguateGlobalSymbols(Program);
953 std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator,
958 // Split the module into the two halves of the program we want.
959 DenseMap<const Value*, Value*> ValueMap;
960 Module *ToNotCodeGen = CloneModule(getProgram(), ValueMap);
961 Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs, ValueMap);
963 // Condition the modules
964 CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
966 sys::Path TestModuleBC("bugpoint.test.bc");
968 if (TestModuleBC.makeUnique(true, &ErrMsg)) {
969 errs() << getToolName() << "Error making unique filename: "
974 if (writeProgramToFile(TestModuleBC.str(), ToCodeGen)) {
975 errs() << "Error writing bitcode to `" << TestModuleBC.str()
981 // Make the shared library
982 sys::Path SafeModuleBC("bugpoint.safe.bc");
983 if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
984 errs() << getToolName() << "Error making unique filename: "
989 if (writeProgramToFile(SafeModuleBC.str(), ToNotCodeGen)) {
990 errs() << "Error writing bitcode to `" << SafeModuleBC.str()
994 std::string SharedObject = compileSharedObject(SafeModuleBC.str(), *Error);
999 outs() << "You can reproduce the problem with the command line: \n";
1000 if (isExecutingJIT()) {
1001 outs() << " lli -load " << SharedObject << " " << TestModuleBC.str();
1003 outs() << " llc " << TestModuleBC.str() << " -o " << TestModuleBC.str()
1005 outs() << " gcc " << SharedObject << " " << TestModuleBC.str()
1006 << ".s -o " << TestModuleBC.str() << ".exe";
1007 #if defined (HAVE_LINK_R)
1008 outs() << " -Wl,-R.";
1011 outs() << " " << TestModuleBC.str() << ".exe";
1013 for (unsigned i = 0, e = InputArgv.size(); i != e; ++i)
1014 outs() << " " << InputArgv[i];
1016 outs() << "The shared object was created with:\n llc -march=c "
1017 << SafeModuleBC.str() << " -o temporary.c\n"
1018 << " gcc -xc temporary.c -O2 -o " << SharedObject;
1019 if (TargetTriple.getArch() == Triple::sparc)
1020 outs() << " -G"; // Compile a shared library, `-G' for Sparc
1022 outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others
1024 outs() << " -fno-strict-aliasing\n";