1 //===- Miscompilation.cpp - Debug program miscompilations -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Module.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Analysis/Verifier.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/Transforms/Utils/Cloning.h"
25 #include "llvm/Transforms/Utils/Linker.h"
26 #include "Support/CommandLine.h"
27 #include "Support/FileUtilities.h"
31 extern cl::list<std::string> InputArgv;
35 class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
38 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
40 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
41 std::vector<const PassInfo*> &Suffix);
45 /// TestResult - After passes have been split into a test group and a control
46 /// group, see if they still break the program.
48 ReduceMiscompilingPasses::TestResult
49 ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
50 std::vector<const PassInfo*> &Suffix) {
51 // First, run the program with just the Suffix passes. If it is still broken
52 // with JUST the kept passes, discard the prefix passes.
53 std::cout << "Checking to see if '" << getPassesString(Suffix)
54 << "' compile correctly: ";
56 std::string BytecodeResult;
57 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
58 std::cerr << " Error running this sequence of passes"
59 << " on the input program!\n";
60 BD.setPassesToRun(Suffix);
61 BD.EmitProgressBytecode("pass-error", false);
62 exit(BD.debugOptimizerCrash());
65 // Check to see if the finished program matches the reference output...
66 if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) {
67 std::cout << " nope.\n";
68 return KeepSuffix; // Miscompilation detected!
70 std::cout << " yup.\n"; // No miscompilation!
72 if (Prefix.empty()) return NoFailure;
74 // Next, see if the program is broken if we run the "prefix" passes first,
75 // then separately run the "kept" passes.
76 std::cout << "Checking to see if '" << getPassesString(Prefix)
77 << "' compile correctly: ";
79 // If it is not broken with the kept passes, it's possible that the prefix
80 // passes must be run before the kept passes to break it. If the program
81 // WORKS after the prefix passes, but then fails if running the prefix AND
82 // kept passes, we can update our bytecode file to include the result of the
83 // prefix passes, then discard the prefix passes.
85 if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
86 std::cerr << " Error running this sequence of passes"
87 << " on the input program!\n";
88 BD.setPassesToRun(Prefix);
89 BD.EmitProgressBytecode("pass-error", false);
90 exit(BD.debugOptimizerCrash());
93 // If the prefix maintains the predicate by itself, only keep the prefix!
94 if (BD.diffProgram(BytecodeResult)) {
95 std::cout << " nope.\n";
96 removeFile(BytecodeResult);
99 std::cout << " yup.\n"; // No miscompilation!
101 // Ok, so now we know that the prefix passes work, try running the suffix
102 // passes on the result of the prefix passes.
104 Module *PrefixOutput = ParseInputFile(BytecodeResult);
105 if (PrefixOutput == 0) {
106 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
107 << BytecodeResult << "'!\n";
110 removeFile(BytecodeResult); // No longer need the file on disk
112 // Don't check if there are no passes in the suffix.
116 std::cout << "Checking to see if '" << getPassesString(Suffix)
117 << "' passes compile correctly after the '"
118 << getPassesString(Prefix) << "' passes: ";
120 Module *OriginalInput = BD.swapProgramIn(PrefixOutput);
121 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
122 std::cerr << " Error running this sequence of passes"
123 << " on the input program!\n";
124 BD.setPassesToRun(Suffix);
125 BD.EmitProgressBytecode("pass-error", false);
126 exit(BD.debugOptimizerCrash());
130 if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) {
131 std::cout << " nope.\n";
132 delete OriginalInput; // We pruned down the original input...
136 // Otherwise, we must not be running the bad pass anymore.
137 std::cout << " yup.\n"; // No miscompilation!
138 delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test
143 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
145 bool (*TestFn)(BugDriver &, Module *, Module *);
147 ReduceMiscompilingFunctions(BugDriver &bd,
148 bool (*F)(BugDriver &, Module *, Module *))
149 : BD(bd), TestFn(F) {}
151 virtual TestResult doTest(std::vector<Function*> &Prefix,
152 std::vector<Function*> &Suffix) {
153 if (!Suffix.empty() && TestFuncs(Suffix))
155 if (!Prefix.empty() && TestFuncs(Prefix))
160 bool TestFuncs(const std::vector<Function*> &Prefix);
164 /// TestMergedProgram - Given two modules, link them together and run the
165 /// program, checking to see if the program matches the diff. If the diff
166 /// matches, return false, otherwise return true. If the DeleteInputs argument
167 /// is set to true then this function deletes both input modules before it
170 static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
172 // Link the two portions of the program back to together.
173 std::string ErrorMsg;
174 if (!DeleteInputs) M1 = CloneModule(M1);
175 if (LinkModules(M1, M2, &ErrorMsg)) {
176 std::cerr << BD.getToolName() << ": Error linking modules together:"
180 if (DeleteInputs) delete M2; // We are done with this module...
182 Module *OldProgram = BD.swapProgramIn(M1);
184 // Execute the program. If it does not match the expected output, we must
186 bool Broken = BD.diffProgram();
188 // Delete the linked module & restore the original
189 BD.swapProgramIn(OldProgram);
194 /// TestFuncs - split functions in a Module into two groups: those that are
195 /// under consideration for miscompilation vs. those that are not, and test
196 /// accordingly. Each group of functions becomes a separate Module.
198 bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
199 // Test to see if the function is misoptimized if we ONLY run it on the
200 // functions listed in Funcs.
201 std::cout << "Checking to see if the program is misoptimized when "
202 << (Funcs.size()==1 ? "this function is" : "these functions are")
203 << " run through the pass"
204 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
205 PrintFunctionList(Funcs);
208 // Split the module into the two halves of the program we want.
209 Module *ToNotOptimize = CloneModule(BD.getProgram());
210 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs);
212 // Run the predicate, not that the predicate will delete both input modules.
213 return TestFn(BD, ToOptimize, ToNotOptimize);
216 /// DisambiguateGlobalSymbols - Mangle symbols to guarantee uniqueness by
217 /// modifying predominantly internal symbols rather than external ones.
219 static void DisambiguateGlobalSymbols(Module *M) {
220 // Try not to cause collisions by minimizing chances of renaming an
221 // already-external symbol, so take in external globals and functions as-is.
222 // The code should work correctly without disambiguation (assuming the same
223 // mangler is used by the two code generators), but having symbols with the
224 // same name causes warnings to be emitted by the code generator.
226 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
227 I->setName(Mang.getValueName(I));
228 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
229 I->setName(Mang.getValueName(I));
232 /// ExtractLoops - Given a reduced list of functions that still exposed the bug,
233 /// check to see if we can extract the loops in the region without obscuring the
234 /// bug. If so, it reduces the amount of code identified.
236 static bool ExtractLoops(BugDriver &BD,
237 bool (*TestFn)(BugDriver &, Module *, Module *),
238 std::vector<Function*> &MiscompiledFunctions) {
239 bool MadeChange = false;
241 Module *ToNotOptimize = CloneModule(BD.getProgram());
242 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
243 MiscompiledFunctions);
244 Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
245 if (!ToOptimizeLoopExtracted) {
246 // If the loop extractor crashed or if there were no extractible loops,
247 // then this chapter of our odyssey is over with.
248 delete ToNotOptimize;
253 std::cerr << "Extracted a loop from the breaking portion of the program.\n";
256 // Bugpoint is intentionally not very trusting of LLVM transformations. In
257 // particular, we're not going to assume that the loop extractor works, so
258 // we're going to test the newly loop extracted program to make sure nothing
259 // has broken. If something broke, then we'll inform the user and stop
261 AbstractInterpreter *AI = BD.switchToCBE();
262 if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) {
263 BD.switchToInterpreter(AI);
265 // Merged program doesn't work anymore!
266 std::cerr << " *** ERROR: Loop extraction broke the program. :("
267 << " Please report a bug!\n";
268 std::cerr << " Continuing on with un-loop-extracted version.\n";
269 delete ToNotOptimize;
270 delete ToOptimizeLoopExtracted;
273 BD.switchToInterpreter(AI);
275 std::cout << " Testing after loop extraction:\n";
276 // Clone modules, the tester function will free them.
277 Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
278 Module *TNOBackup = CloneModule(ToNotOptimize);
279 if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) {
280 std::cout << "*** Loop extraction masked the problem. Undoing.\n";
281 // If the program is not still broken, then loop extraction did something
282 // that masked the error. Stop loop extraction now.
287 ToOptimizeLoopExtracted = TOLEBackup;
288 ToNotOptimize = TNOBackup;
290 std::cout << "*** Loop extraction successful!\n";
292 // Okay, great! Now we know that we extracted a loop and that loop
293 // extraction both didn't break the program, and didn't mask the problem.
294 // Replace the current program with the loop extracted version, and try to
295 // extract another loop.
296 std::string ErrorMsg;
297 if (LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)) {
298 std::cerr << BD.getToolName() << ": Error linking modules together:"
303 // All of the Function*'s in the MiscompiledFunctions list are in the old
304 // module. Update this list to include all of the functions in the
305 // optimized and loop extracted module.
306 MiscompiledFunctions.clear();
307 for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
308 E = ToOptimizeLoopExtracted->end(); I != E; ++I) {
309 if (!I->isExternal()) {
310 Function *NewF = ToNotOptimize->getFunction(I->getName(),
311 I->getFunctionType());
312 assert(NewF && "Function not found??");
313 MiscompiledFunctions.push_back(NewF);
316 delete ToOptimizeLoopExtracted;
318 BD.setNewProgram(ToNotOptimize);
324 class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
326 bool (*TestFn)(BugDriver &, Module *, Module *);
327 std::vector<Function*> FunctionsBeingTested;
329 ReduceMiscompiledBlocks(BugDriver &bd,
330 bool (*F)(BugDriver &, Module *, Module *),
331 const std::vector<Function*> &Fns)
332 : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
334 virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
335 std::vector<BasicBlock*> &Suffix) {
336 if (!Suffix.empty() && TestFuncs(Suffix))
338 if (TestFuncs(Prefix))
343 bool TestFuncs(const std::vector<BasicBlock*> &Prefix);
347 /// TestFuncs - Extract all blocks for the miscompiled functions except for the
348 /// specified blocks. If the problem still exists, return true.
350 bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs) {
351 // Test to see if the function is misoptimized if we ONLY run it on the
352 // functions listed in Funcs.
353 std::cout << "Checking to see if the program is misoptimized when all ";
355 std::cout << "but these " << BBs.size() << " blocks are extracted: ";
356 for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
357 std::cout << BBs[i]->getName() << " ";
358 if (BBs.size() > 10) std::cout << "...";
360 std::cout << "blocks are extracted.";
364 // Split the module into the two halves of the program we want.
365 Module *ToNotOptimize = CloneModule(BD.getProgram());
366 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
367 FunctionsBeingTested);
369 // Try the extraction. If it doesn't work, then the block extractor crashed
370 // or something, in which case bugpoint can't chase down this possibility.
371 if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) {
373 // Run the predicate, not that the predicate will delete both input modules.
374 return TestFn(BD, New, ToNotOptimize);
377 delete ToNotOptimize;
382 /// ExtractBlocks - Given a reduced list of functions that still expose the bug,
383 /// extract as many basic blocks from the region as possible without obscuring
386 static bool ExtractBlocks(BugDriver &BD,
387 bool (*TestFn)(BugDriver &, Module *, Module *),
388 std::vector<Function*> &MiscompiledFunctions) {
389 std::vector<BasicBlock*> Blocks;
390 for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
391 for (Function::iterator I = MiscompiledFunctions[i]->begin(),
392 E = MiscompiledFunctions[i]->end(); I != E; ++I)
395 // Use the list reducer to identify blocks that can be extracted without
396 // obscuring the bug. The Blocks list will end up containing blocks that must
397 // be retained from the original program.
398 unsigned OldSize = Blocks.size();
400 // Check to see if all blocks are extractible first.
401 if (ReduceMiscompiledBlocks(BD, TestFn,
402 MiscompiledFunctions).TestFuncs(std::vector<BasicBlock*>())) {
405 ReduceMiscompiledBlocks(BD, TestFn,MiscompiledFunctions).reduceList(Blocks);
406 if (Blocks.size() == OldSize)
410 Module *ProgClone = CloneModule(BD.getProgram());
411 Module *ToExtract = SplitFunctionsOutOfModule(ProgClone,
412 MiscompiledFunctions);
413 Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract);
414 if (Extracted == 0) {
415 // Wierd, extraction should have worked.
416 std::cerr << "Nondeterministic problem extracting blocks??\n";
422 // Otherwise, block extraction succeeded. Link the two program fragments back
426 std::string ErrorMsg;
427 if (LinkModules(ProgClone, Extracted, &ErrorMsg)) {
428 std::cerr << BD.getToolName() << ": Error linking modules together:"
433 // Set the new program and delete the old one.
434 BD.setNewProgram(ProgClone);
436 // Update the list of miscompiled functions.
437 MiscompiledFunctions.clear();
439 for (Module::iterator I = Extracted->begin(), E = Extracted->end(); I != E;
441 if (!I->isExternal()) {
442 Function *NF = ProgClone->getFunction(I->getName(), I->getFunctionType());
443 assert(NF && "Mapped function not found!");
444 MiscompiledFunctions.push_back(NF);
453 /// DebugAMiscompilation - This is a generic driver to narrow down
454 /// miscompilations, either in an optimization or a code generator.
456 static std::vector<Function*>
457 DebugAMiscompilation(BugDriver &BD,
458 bool (*TestFn)(BugDriver &, Module *, Module *)) {
459 // Okay, now that we have reduced the list of passes which are causing the
460 // failure, see if we can pin down which functions are being
461 // miscompiled... first build a list of all of the non-external functions in
463 std::vector<Function*> MiscompiledFunctions;
464 Module *Prog = BD.getProgram();
465 for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
466 if (!I->isExternal())
467 MiscompiledFunctions.push_back(I);
469 // Do the reduction...
470 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
472 std::cout << "\n*** The following function"
473 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
474 << " being miscompiled: ";
475 PrintFunctionList(MiscompiledFunctions);
478 // See if we can rip any loops out of the miscompiled functions and still
479 // trigger the problem.
480 if (ExtractLoops(BD, TestFn, MiscompiledFunctions)) {
481 // Okay, we extracted some loops and the problem still appears. See if we
482 // can eliminate some of the created functions from being candidates.
484 // Loop extraction can introduce functions with the same name (foo_code).
485 // Make sure to disambiguate the symbols so that when the program is split
486 // apart that we can link it back together again.
487 DisambiguateGlobalSymbols(BD.getProgram());
489 // Do the reduction...
490 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
492 std::cout << "\n*** The following function"
493 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
494 << " being miscompiled: ";
495 PrintFunctionList(MiscompiledFunctions);
499 if (ExtractBlocks(BD, TestFn, MiscompiledFunctions)) {
500 // Okay, we extracted some blocks and the problem still appears. See if we
501 // can eliminate some of the created functions from being candidates.
503 // Block extraction can introduce functions with the same name (foo_code).
504 // Make sure to disambiguate the symbols so that when the program is split
505 // apart that we can link it back together again.
506 DisambiguateGlobalSymbols(BD.getProgram());
508 // Do the reduction...
509 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
511 std::cout << "\n*** The following function"
512 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
513 << " being miscompiled: ";
514 PrintFunctionList(MiscompiledFunctions);
518 return MiscompiledFunctions;
521 /// TestOptimizer - This is the predicate function used to check to see if the
522 /// "Test" portion of the program is misoptimized. If so, return true. In any
523 /// case, both module arguments are deleted.
525 static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
526 // Run the optimization passes on ToOptimize, producing a transformed version
527 // of the functions being tested.
528 std::cout << " Optimizing functions being tested: ";
529 Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
530 /*AutoDebugCrashes*/true);
531 std::cout << "done.\n";
534 std::cout << " Checking to see if the merged program executes correctly: ";
535 bool Broken = TestMergedProgram(BD, Optimized, Safe, true);
536 std::cout << (Broken ? " nope.\n" : " yup.\n");
541 /// debugMiscompilation - This method is used when the passes selected are not
542 /// crashing, but the generated output is semantically different from the
545 bool BugDriver::debugMiscompilation() {
546 // Make sure something was miscompiled...
547 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
548 std::cerr << "*** Optimized program matches reference output! No problem "
549 << "detected...\nbugpoint can't help you with your problem!\n";
553 std::cout << "\n*** Found miscompiling pass"
554 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
555 << getPassesString(getPassesToRun()) << "\n";
556 EmitProgressBytecode("passinput");
558 std::vector<Function*> MiscompiledFunctions =
559 DebugAMiscompilation(*this, TestOptimizer);
561 // Output a bunch of bytecode files for the user...
562 std::cout << "Outputting reduced bytecode files which expose the problem:\n";
563 Module *ToNotOptimize = CloneModule(getProgram());
564 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
565 MiscompiledFunctions);
567 std::cout << " Non-optimized portion: ";
568 ToNotOptimize = swapProgramIn(ToNotOptimize);
569 EmitProgressBytecode("tonotoptimize", true);
570 setNewProgram(ToNotOptimize); // Delete hacked module.
572 std::cout << " Portion that is input to optimizer: ";
573 ToOptimize = swapProgramIn(ToOptimize);
574 EmitProgressBytecode("tooptimize");
575 setNewProgram(ToOptimize); // Delete hacked module.
580 /// CleanupAndPrepareModules - Get the specified modules ready for code
581 /// generator testing.
583 static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
585 // Clean up the modules, removing extra cruft that we don't need anymore...
586 Test = BD.performFinalCleanups(Test);
588 // If we are executing the JIT, we have several nasty issues to take care of.
589 if (!BD.isExecutingJIT()) return;
591 // First, if the main function is in the Safe module, we must add a stub to
592 // the Test module to call into it. Thus, we create a new function `main'
593 // which just calls the old one.
594 if (Function *oldMain = Safe->getNamedFunction("main"))
595 if (!oldMain->isExternal()) {
597 oldMain->setName("llvm_bugpoint_old_main");
598 // Create a NEW `main' function with same type in the test module.
599 Function *newMain = new Function(oldMain->getFunctionType(),
600 GlobalValue::ExternalLinkage,
602 // Create an `oldmain' prototype in the test module, which will
603 // corresponds to the real main function in the same module.
604 Function *oldMainProto = new Function(oldMain->getFunctionType(),
605 GlobalValue::ExternalLinkage,
606 oldMain->getName(), Test);
607 // Set up and remember the argument list for the main function.
608 std::vector<Value*> args;
609 for (Function::aiterator I = newMain->abegin(), E = newMain->aend(),
610 OI = oldMain->abegin(); I != E; ++I, ++OI) {
611 I->setName(OI->getName()); // Copy argument names from oldMain
615 // Call the old main function and return its result
616 BasicBlock *BB = new BasicBlock("entry", newMain);
617 CallInst *call = new CallInst(oldMainProto, args);
618 BB->getInstList().push_back(call);
620 // If the type of old function wasn't void, return value of call
621 new ReturnInst(oldMain->getReturnType() != Type::VoidTy ? call : 0, BB);
624 // The second nasty issue we must deal with in the JIT is that the Safe
625 // module cannot directly reference any functions defined in the test
626 // module. Instead, we use a JIT API call to dynamically resolve the
629 // Add the resolver to the Safe module.
630 // Prototype: void *getPointerToNamedFunction(const char* Name)
631 Function *resolverFunc =
632 Safe->getOrInsertFunction("getPointerToNamedFunction",
633 PointerType::get(Type::SByteTy),
634 PointerType::get(Type::SByteTy), 0);
636 // Use the function we just added to get addresses of functions we need.
637 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
638 if (F->isExternal() && !F->use_empty() && &*F != resolverFunc &&
639 F->getIntrinsicID() == 0 /* ignore intrinsics */) {
640 Function *TestFn = Test->getFunction(F->getName(), F->getFunctionType());
642 // Don't forward functions which are external in the test module too.
643 if (TestFn && !TestFn->isExternal()) {
644 // 1. Add a string constant with its name to the global file
645 Constant *InitArray = ConstantArray::get(F->getName());
646 GlobalVariable *funcName =
647 new GlobalVariable(InitArray->getType(), true /*isConstant*/,
648 GlobalValue::InternalLinkage, InitArray,
649 F->getName() + "_name", Safe);
651 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
652 // sbyte* so it matches the signature of the resolver function.
654 // GetElementPtr *funcName, ulong 0, ulong 0
655 std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::IntTy));
657 ConstantExpr::getGetElementPtr(ConstantPointerRef::get(funcName),
659 std::vector<Value*> ResolverArgs;
660 ResolverArgs.push_back(GEP);
662 // Rewrite uses of F in global initializers, etc. to uses of a wrapper
663 // function that dynamically resolves the calls to F via our JIT API
664 if (F->use_begin() != F->use_end()) {
665 // Construct a new stub function that will re-route calls to F
666 const FunctionType *FuncTy = F->getFunctionType();
667 Function *FuncWrapper = new Function(FuncTy,
668 GlobalValue::InternalLinkage,
669 F->getName() + "_wrapper",
671 BasicBlock *Header = new BasicBlock("header", FuncWrapper);
673 // Resolve the call to function F via the JIT API:
675 // call resolver(GetElementPtr...)
676 CallInst *resolve = new CallInst(resolverFunc, ResolverArgs,
678 Header->getInstList().push_back(resolve);
679 // cast the result from the resolver to correctly-typed function
680 CastInst *castResolver =
681 new CastInst(resolve, PointerType::get(F->getFunctionType()),
683 Header->getInstList().push_back(castResolver);
685 // Save the argument list
686 std::vector<Value*> Args;
687 for (Function::aiterator i = FuncWrapper->abegin(),
688 e = FuncWrapper->aend(); i != e; ++i)
691 // Pass on the arguments to the real function, return its result
692 if (F->getReturnType() == Type::VoidTy) {
693 CallInst *Call = new CallInst(castResolver, Args);
694 Header->getInstList().push_back(Call);
695 ReturnInst *Ret = new ReturnInst();
696 Header->getInstList().push_back(Ret);
698 CallInst *Call = new CallInst(castResolver, Args, "redir");
699 Header->getInstList().push_back(Call);
700 ReturnInst *Ret = new ReturnInst(Call);
701 Header->getInstList().push_back(Ret);
704 // Use the wrapper function instead of the old function
705 F->replaceAllUsesWith(FuncWrapper);
711 if (verifyModule(*Test) || verifyModule(*Safe)) {
712 std::cerr << "Bugpoint has a bug, which corrupted a module!!\n";
719 /// TestCodeGenerator - This is the predicate function used to check to see if
720 /// the "Test" portion of the program is miscompiled by the code generator under
721 /// test. If so, return true. In any case, both module arguments are deleted.
723 static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) {
724 CleanupAndPrepareModules(BD, Test, Safe);
726 std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
727 if (BD.writeProgramToFile(TestModuleBC, Test)) {
728 std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
733 // Make the shared library
734 std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
736 if (BD.writeProgramToFile(SafeModuleBC, Safe)) {
737 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
740 std::string SharedObject = BD.compileSharedObject(SafeModuleBC);
743 // Run the code generator on the `Test' code, loading the shared library.
744 // The function returns whether or not the new output differs from reference.
745 int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
748 std::cerr << ": still failing!\n";
750 std::cerr << ": didn't fail.\n";
751 removeFile(TestModuleBC);
752 removeFile(SafeModuleBC);
753 removeFile(SharedObject);
759 /// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
761 bool BugDriver::debugCodeGenerator() {
762 if ((void*)cbe == (void*)Interpreter) {
763 std::string Result = executeProgramWithCBE("bugpoint.cbe.out");
764 std::cout << "\n*** The C backend cannot match the reference diff, but it "
765 << "is used as the 'known good'\n code generator, so I can't"
766 << " debug it. Perhaps you have a front-end problem?\n As a"
767 << " sanity check, I left the result of executing the program "
768 << "with the C backend\n in this file for you: '"
773 DisambiguateGlobalSymbols(Program);
775 std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator);
777 // Split the module into the two halves of the program we want.
778 Module *ToNotCodeGen = CloneModule(getProgram());
779 Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs);
781 // Condition the modules
782 CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
784 std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
785 if (writeProgramToFile(TestModuleBC, ToCodeGen)) {
786 std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
791 // Make the shared library
792 std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
793 if (writeProgramToFile(SafeModuleBC, ToNotCodeGen)) {
794 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
797 std::string SharedObject = compileSharedObject(SafeModuleBC);
800 std::cout << "You can reproduce the problem with the command line: \n";
801 if (isExecutingJIT()) {
802 std::cout << " lli -load " << SharedObject << " " << TestModuleBC;
804 std::cout << " llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
805 std::cout << " gcc " << SharedObject << " " << TestModuleBC
806 << ".s -o " << TestModuleBC << ".exe -Wl,-R.\n";
807 std::cout << " " << TestModuleBC << ".exe";
809 for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
810 std::cout << " " << InputArgv[i];
812 std::cout << "The shared object was created with:\n llc -march=c "
813 << SafeModuleBC << " -o temporary.c\n"
814 << " gcc -xc temporary.c -O2 -o " << SharedObject
815 #if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
816 << " -G" // Compile a shared library, `-G' for Sparc
818 << " -shared" // `-shared' for Linux/X86, maybe others
820 << " -fno-strict-aliasing\n";