Miscompilation.cpp revision 6fa98b13206583e6eb90b8304758b35548914944
1//===- Miscompilation.cpp - Debug program miscompilations -----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 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. 7// 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" 17#include "llvm/Constants.h" 18#include "llvm/DerivedTypes.h" 19#include "llvm/Instructions.h" 20#include "llvm/Linker.h" 21#include "llvm/Module.h" 22#include "llvm/Pass.h" 23#include "llvm/Analysis/Verifier.h" 24#include "llvm/Support/Mangler.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 29using namespace llvm; 30 31namespace llvm { 32 extern cl::list<std::string> InputArgv; 33} 34 35namespace { 36 static llvm::cl::opt<bool> 37 DisableLoopExtraction("disable-loop-extraction", 38 cl::desc("Don't extract loops when searching for miscompilations"), 39 cl::init(false)); 40 41 class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> { 42 BugDriver &BD; 43 public: 44 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} 45 46 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix, 47 std::vector<const PassInfo*> &Suffix); 48 }; 49} 50 51/// TestResult - After passes have been split into a test group and a control 52/// group, see if they still break the program. 53/// 54ReduceMiscompilingPasses::TestResult 55ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix, 56 std::vector<const PassInfo*> &Suffix) { 57 // First, run the program with just the Suffix passes. If it is still broken 58 // with JUST the kept passes, discard the prefix passes. 59 std::cout << "Checking to see if '" << getPassesString(Suffix) 60 << "' compile correctly: "; 61 62 std::string BitcodeResult; 63 if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) { 64 std::cerr << " Error running this sequence of passes" 65 << " on the input program!\n"; 66 BD.setPassesToRun(Suffix); 67 BD.EmitProgressBitcode("pass-error", false); 68 exit(BD.debugOptimizerCrash()); 69 } 70 71 // Check to see if the finished program matches the reference output... 72 if (BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/)) { 73 std::cout << " nope.\n"; 74 if (Suffix.empty()) { 75 std::cerr << BD.getToolName() << ": I'm confused: the test fails when " 76 << "no passes are run, nondeterministic program?\n"; 77 exit(1); 78 } 79 return KeepSuffix; // Miscompilation detected! 80 } 81 std::cout << " yup.\n"; // No miscompilation! 82 83 if (Prefix.empty()) return NoFailure; 84 85 // Next, see if the program is broken if we run the "prefix" passes first, 86 // then separately run the "kept" passes. 87 std::cout << "Checking to see if '" << getPassesString(Prefix) 88 << "' compile correctly: "; 89 90 // If it is not broken with the kept passes, it's possible that the prefix 91 // passes must be run before the kept passes to break it. If the program 92 // WORKS after the prefix passes, but then fails if running the prefix AND 93 // kept passes, we can update our bitcode file to include the result of the 94 // prefix passes, then discard the prefix passes. 95 // 96 if (BD.runPasses(Prefix, BitcodeResult, false/*delete*/, true/*quiet*/)) { 97 std::cerr << " Error running this sequence of passes" 98 << " on the input program!\n"; 99 BD.setPassesToRun(Prefix); 100 BD.EmitProgressBitcode("pass-error", false); 101 exit(BD.debugOptimizerCrash()); 102 } 103 104 // If the prefix maintains the predicate by itself, only keep the prefix! 105 if (BD.diffProgram(BitcodeResult)) { 106 std::cout << " nope.\n"; 107 sys::Path(BitcodeResult).eraseFromDisk(); 108 return KeepPrefix; 109 } 110 std::cout << " yup.\n"; // No miscompilation! 111 112 // Ok, so now we know that the prefix passes work, try running the suffix 113 // passes on the result of the prefix passes. 114 // 115 Module *PrefixOutput = ParseInputFile(BitcodeResult); 116 if (PrefixOutput == 0) { 117 std::cerr << BD.getToolName() << ": Error reading bitcode file '" 118 << BitcodeResult << "'!\n"; 119 exit(1); 120 } 121 sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk 122 123 // Don't check if there are no passes in the suffix. 124 if (Suffix.empty()) 125 return NoFailure; 126 127 std::cout << "Checking to see if '" << getPassesString(Suffix) 128 << "' passes compile correctly after the '" 129 << getPassesString(Prefix) << "' passes: "; 130 131 Module *OriginalInput = BD.swapProgramIn(PrefixOutput); 132 if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) { 133 std::cerr << " Error running this sequence of passes" 134 << " on the input program!\n"; 135 BD.setPassesToRun(Suffix); 136 BD.EmitProgressBitcode("pass-error", false); 137 exit(BD.debugOptimizerCrash()); 138 } 139 140 // Run the result... 141 if (BD.diffProgram(BitcodeResult, "", true/*delete bitcode*/)) { 142 std::cout << " nope.\n"; 143 delete OriginalInput; // We pruned down the original input... 144 return KeepSuffix; 145 } 146 147 // Otherwise, we must not be running the bad pass anymore. 148 std::cout << " yup.\n"; // No miscompilation! 149 delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test 150 return NoFailure; 151} 152 153namespace { 154 class ReduceMiscompilingFunctions : public ListReducer<Function*> { 155 BugDriver &BD; 156 bool (*TestFn)(BugDriver &, Module *, Module *); 157 public: 158 ReduceMiscompilingFunctions(BugDriver &bd, 159 bool (*F)(BugDriver &, Module *, Module *)) 160 : BD(bd), TestFn(F) {} 161 162 virtual TestResult doTest(std::vector<Function*> &Prefix, 163 std::vector<Function*> &Suffix) { 164 if (!Suffix.empty() && TestFuncs(Suffix)) 165 return KeepSuffix; 166 if (!Prefix.empty() && TestFuncs(Prefix)) 167 return KeepPrefix; 168 return NoFailure; 169 } 170 171 bool TestFuncs(const std::vector<Function*> &Prefix); 172 }; 173} 174 175/// TestMergedProgram - Given two modules, link them together and run the 176/// program, checking to see if the program matches the diff. If the diff 177/// matches, return false, otherwise return true. If the DeleteInputs argument 178/// is set to true then this function deletes both input modules before it 179/// returns. 180/// 181static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2, 182 bool DeleteInputs) { 183 // Link the two portions of the program back to together. 184 std::string ErrorMsg; 185 if (!DeleteInputs) { 186 M1 = CloneModule(M1); 187 M2 = CloneModule(M2); 188 } 189 if (Linker::LinkModules(M1, M2, &ErrorMsg)) { 190 std::cerr << BD.getToolName() << ": Error linking modules together:" 191 << ErrorMsg << '\n'; 192 exit(1); 193 } 194 delete M2; // We are done with this module. 195 196 Module *OldProgram = BD.swapProgramIn(M1); 197 198 // Execute the program. If it does not match the expected output, we must 199 // return true. 200 bool Broken = BD.diffProgram(); 201 202 // Delete the linked module & restore the original 203 BD.swapProgramIn(OldProgram); 204 delete M1; 205 return Broken; 206} 207 208/// TestFuncs - split functions in a Module into two groups: those that are 209/// under consideration for miscompilation vs. those that are not, and test 210/// accordingly. Each group of functions becomes a separate Module. 211/// 212bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){ 213 // Test to see if the function is misoptimized if we ONLY run it on the 214 // functions listed in Funcs. 215 std::cout << "Checking to see if the program is misoptimized when " 216 << (Funcs.size()==1 ? "this function is" : "these functions are") 217 << " run through the pass" 218 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":"; 219 PrintFunctionList(Funcs); 220 std::cout << '\n'; 221 222 // Split the module into the two halves of the program we want. 223 Module *ToNotOptimize = CloneModule(BD.getProgram()); 224 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs); 225 226 // Run the predicate, note that the predicate will delete both input modules. 227 return TestFn(BD, ToOptimize, ToNotOptimize); 228} 229 230/// DisambiguateGlobalSymbols - Mangle symbols to guarantee uniqueness by 231/// modifying predominantly internal symbols rather than external ones. 232/// 233static void DisambiguateGlobalSymbols(Module *M) { 234 // Try not to cause collisions by minimizing chances of renaming an 235 // already-external symbol, so take in external globals and functions as-is. 236 // The code should work correctly without disambiguation (assuming the same 237 // mangler is used by the two code generators), but having symbols with the 238 // same name causes warnings to be emitted by the code generator. 239 Mangler Mang(*M); 240 // Agree with the CBE on symbol naming 241 Mang.markCharUnacceptable('.'); 242 Mang.setPreserveAsmNames(true); 243 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 244 I != E; ++I) 245 I->setName(Mang.getValueName(I)); 246 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 247 I->setName(Mang.getValueName(I)); 248} 249 250/// ExtractLoops - Given a reduced list of functions that still exposed the bug, 251/// check to see if we can extract the loops in the region without obscuring the 252/// bug. If so, it reduces the amount of code identified. 253/// 254static bool ExtractLoops(BugDriver &BD, 255 bool (*TestFn)(BugDriver &, Module *, Module *), 256 std::vector<Function*> &MiscompiledFunctions) { 257 bool MadeChange = false; 258 while (1) { 259 if (BugpointIsInterrupted) return MadeChange; 260 261 Module *ToNotOptimize = CloneModule(BD.getProgram()); 262 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, 263 MiscompiledFunctions); 264 Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize); 265 if (!ToOptimizeLoopExtracted) { 266 // If the loop extractor crashed or if there were no extractible loops, 267 // then this chapter of our odyssey is over with. 268 delete ToNotOptimize; 269 delete ToOptimize; 270 return MadeChange; 271 } 272 273 std::cerr << "Extracted a loop from the breaking portion of the program.\n"; 274 275 // Bugpoint is intentionally not very trusting of LLVM transformations. In 276 // particular, we're not going to assume that the loop extractor works, so 277 // we're going to test the newly loop extracted program to make sure nothing 278 // has broken. If something broke, then we'll inform the user and stop 279 // extraction. 280 AbstractInterpreter *AI = BD.switchToCBE(); 281 if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) { 282 BD.switchToInterpreter(AI); 283 284 // Merged program doesn't work anymore! 285 std::cerr << " *** ERROR: Loop extraction broke the program. :(" 286 << " Please report a bug!\n"; 287 std::cerr << " Continuing on with un-loop-extracted version.\n"; 288 289 BD.writeProgramToFile("bugpoint-loop-extract-fail-tno.bc", ToNotOptimize); 290 BD.writeProgramToFile("bugpoint-loop-extract-fail-to.bc", ToOptimize); 291 BD.writeProgramToFile("bugpoint-loop-extract-fail-to-le.bc", 292 ToOptimizeLoopExtracted); 293 294 std::cerr << "Please submit the bugpoint-loop-extract-fail-*.bc files.\n"; 295 delete ToOptimize; 296 delete ToNotOptimize; 297 delete ToOptimizeLoopExtracted; 298 return MadeChange; 299 } 300 delete ToOptimize; 301 BD.switchToInterpreter(AI); 302 303 std::cout << " Testing after loop extraction:\n"; 304 // Clone modules, the tester function will free them. 305 Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted); 306 Module *TNOBackup = CloneModule(ToNotOptimize); 307 if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) { 308 std::cout << "*** Loop extraction masked the problem. Undoing.\n"; 309 // If the program is not still broken, then loop extraction did something 310 // that masked the error. Stop loop extraction now. 311 delete TOLEBackup; 312 delete TNOBackup; 313 return MadeChange; 314 } 315 ToOptimizeLoopExtracted = TOLEBackup; 316 ToNotOptimize = TNOBackup; 317 318 std::cout << "*** Loop extraction successful!\n"; 319 320 std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions; 321 for (Module::iterator I = ToOptimizeLoopExtracted->begin(), 322 E = ToOptimizeLoopExtracted->end(); I != E; ++I) 323 if (!I->isDeclaration()) 324 MisCompFunctions.push_back(std::make_pair(I->getName(), 325 I->getFunctionType())); 326 327 // Okay, great! Now we know that we extracted a loop and that loop 328 // extraction both didn't break the program, and didn't mask the problem. 329 // Replace the current program with the loop extracted version, and try to 330 // extract another loop. 331 std::string ErrorMsg; 332 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)){ 333 std::cerr << BD.getToolName() << ": Error linking modules together:" 334 << ErrorMsg << '\n'; 335 exit(1); 336 } 337 delete ToOptimizeLoopExtracted; 338 339 // All of the Function*'s in the MiscompiledFunctions list are in the old 340 // module. Update this list to include all of the functions in the 341 // optimized and loop extracted module. 342 MiscompiledFunctions.clear(); 343 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { 344 Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first); 345 346 assert(NewF && "Function not found??"); 347 assert(NewF->getFunctionType() == MisCompFunctions[i].second && 348 "found wrong function type?"); 349 MiscompiledFunctions.push_back(NewF); 350 } 351 352 BD.setNewProgram(ToNotOptimize); 353 MadeChange = true; 354 } 355} 356 357namespace { 358 class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> { 359 BugDriver &BD; 360 bool (*TestFn)(BugDriver &, Module *, Module *); 361 std::vector<Function*> FunctionsBeingTested; 362 public: 363 ReduceMiscompiledBlocks(BugDriver &bd, 364 bool (*F)(BugDriver &, Module *, Module *), 365 const std::vector<Function*> &Fns) 366 : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {} 367 368 virtual TestResult doTest(std::vector<BasicBlock*> &Prefix, 369 std::vector<BasicBlock*> &Suffix) { 370 if (!Suffix.empty() && TestFuncs(Suffix)) 371 return KeepSuffix; 372 if (TestFuncs(Prefix)) 373 return KeepPrefix; 374 return NoFailure; 375 } 376 377 bool TestFuncs(const std::vector<BasicBlock*> &Prefix); 378 }; 379} 380 381/// TestFuncs - Extract all blocks for the miscompiled functions except for the 382/// specified blocks. If the problem still exists, return true. 383/// 384bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs) { 385 // Test to see if the function is misoptimized if we ONLY run it on the 386 // functions listed in Funcs. 387 std::cout << "Checking to see if the program is misoptimized when all "; 388 if (!BBs.empty()) { 389 std::cout << "but these " << BBs.size() << " blocks are extracted: "; 390 for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i) 391 std::cout << BBs[i]->getName() << " "; 392 if (BBs.size() > 10) std::cout << "..."; 393 } else { 394 std::cout << "blocks are extracted."; 395 } 396 std::cout << '\n'; 397 398 // Split the module into the two halves of the program we want. 399 Module *ToNotOptimize = CloneModule(BD.getProgram()); 400 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, 401 FunctionsBeingTested); 402 403 // Try the extraction. If it doesn't work, then the block extractor crashed 404 // or something, in which case bugpoint can't chase down this possibility. 405 if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) { 406 delete ToOptimize; 407 // Run the predicate, not that the predicate will delete both input modules. 408 return TestFn(BD, New, ToNotOptimize); 409 } 410 delete ToOptimize; 411 delete ToNotOptimize; 412 return false; 413} 414 415 416/// ExtractBlocks - Given a reduced list of functions that still expose the bug, 417/// extract as many basic blocks from the region as possible without obscuring 418/// the bug. 419/// 420static bool ExtractBlocks(BugDriver &BD, 421 bool (*TestFn)(BugDriver &, Module *, Module *), 422 std::vector<Function*> &MiscompiledFunctions) { 423 if (BugpointIsInterrupted) return false; 424 425 std::vector<BasicBlock*> Blocks; 426 for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) 427 for (Function::iterator I = MiscompiledFunctions[i]->begin(), 428 E = MiscompiledFunctions[i]->end(); I != E; ++I) 429 Blocks.push_back(I); 430 431 // Use the list reducer to identify blocks that can be extracted without 432 // obscuring the bug. The Blocks list will end up containing blocks that must 433 // be retained from the original program. 434 unsigned OldSize = Blocks.size(); 435 436 // Check to see if all blocks are extractible first. 437 if (ReduceMiscompiledBlocks(BD, TestFn, 438 MiscompiledFunctions).TestFuncs(std::vector<BasicBlock*>())) { 439 Blocks.clear(); 440 } else { 441 ReduceMiscompiledBlocks(BD, TestFn,MiscompiledFunctions).reduceList(Blocks); 442 if (Blocks.size() == OldSize) 443 return false; 444 } 445 446 Module *ProgClone = CloneModule(BD.getProgram()); 447 Module *ToExtract = SplitFunctionsOutOfModule(ProgClone, 448 MiscompiledFunctions); 449 Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract); 450 if (Extracted == 0) { 451 // Weird, extraction should have worked. 452 std::cerr << "Nondeterministic problem extracting blocks??\n"; 453 delete ProgClone; 454 delete ToExtract; 455 return false; 456 } 457 458 // Otherwise, block extraction succeeded. Link the two program fragments back 459 // together. 460 delete ToExtract; 461 462 std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions; 463 for (Module::iterator I = Extracted->begin(), E = Extracted->end(); 464 I != E; ++I) 465 if (!I->isDeclaration()) 466 MisCompFunctions.push_back(std::make_pair(I->getName(), 467 I->getFunctionType())); 468 469 std::string ErrorMsg; 470 if (Linker::LinkModules(ProgClone, Extracted, &ErrorMsg)) { 471 std::cerr << BD.getToolName() << ": Error linking modules together:" 472 << ErrorMsg << '\n'; 473 exit(1); 474 } 475 delete Extracted; 476 477 // Set the new program and delete the old one. 478 BD.setNewProgram(ProgClone); 479 480 // Update the list of miscompiled functions. 481 MiscompiledFunctions.clear(); 482 483 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { 484 Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first); 485 assert(NewF && "Function not found??"); 486 assert(NewF->getFunctionType() == MisCompFunctions[i].second && 487 "Function has wrong type??"); 488 MiscompiledFunctions.push_back(NewF); 489 } 490 491 return true; 492} 493 494 495/// DebugAMiscompilation - This is a generic driver to narrow down 496/// miscompilations, either in an optimization or a code generator. 497/// 498static std::vector<Function*> 499DebugAMiscompilation(BugDriver &BD, 500 bool (*TestFn)(BugDriver &, Module *, Module *)) { 501 // Okay, now that we have reduced the list of passes which are causing the 502 // failure, see if we can pin down which functions are being 503 // miscompiled... first build a list of all of the non-external functions in 504 // the program. 505 std::vector<Function*> MiscompiledFunctions; 506 Module *Prog = BD.getProgram(); 507 for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I) 508 if (!I->isDeclaration()) 509 MiscompiledFunctions.push_back(I); 510 511 // Do the reduction... 512 if (!BugpointIsInterrupted) 513 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); 514 515 std::cout << "\n*** The following function" 516 << (MiscompiledFunctions.size() == 1 ? " is" : "s are") 517 << " being miscompiled: "; 518 PrintFunctionList(MiscompiledFunctions); 519 std::cout << '\n'; 520 521 // See if we can rip any loops out of the miscompiled functions and still 522 // trigger the problem. 523 524 if (!BugpointIsInterrupted && !DisableLoopExtraction && 525 ExtractLoops(BD, TestFn, MiscompiledFunctions)) { 526 // Okay, we extracted some loops and the problem still appears. See if we 527 // can eliminate some of the created functions from being candidates. 528 529 // Loop extraction can introduce functions with the same name (foo_code). 530 // Make sure to disambiguate the symbols so that when the program is split 531 // apart that we can link it back together again. 532 DisambiguateGlobalSymbols(BD.getProgram()); 533 534 // Do the reduction... 535 if (!BugpointIsInterrupted) 536 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); 537 538 std::cout << "\n*** The following function" 539 << (MiscompiledFunctions.size() == 1 ? " is" : "s are") 540 << " being miscompiled: "; 541 PrintFunctionList(MiscompiledFunctions); 542 std::cout << '\n'; 543 } 544 545 if (!BugpointIsInterrupted && 546 ExtractBlocks(BD, TestFn, MiscompiledFunctions)) { 547 // Okay, we extracted some blocks and the problem still appears. See if we 548 // can eliminate some of the created functions from being candidates. 549 550 // Block extraction can introduce functions with the same name (foo_code). 551 // Make sure to disambiguate the symbols so that when the program is split 552 // apart that we can link it back together again. 553 DisambiguateGlobalSymbols(BD.getProgram()); 554 555 // Do the reduction... 556 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); 557 558 std::cout << "\n*** The following function" 559 << (MiscompiledFunctions.size() == 1 ? " is" : "s are") 560 << " being miscompiled: "; 561 PrintFunctionList(MiscompiledFunctions); 562 std::cout << '\n'; 563 } 564 565 return MiscompiledFunctions; 566} 567 568/// TestOptimizer - This is the predicate function used to check to see if the 569/// "Test" portion of the program is misoptimized. If so, return true. In any 570/// case, both module arguments are deleted. 571/// 572static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) { 573 // Run the optimization passes on ToOptimize, producing a transformed version 574 // of the functions being tested. 575 std::cout << " Optimizing functions being tested: "; 576 Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(), 577 /*AutoDebugCrashes*/true); 578 std::cout << "done.\n"; 579 delete Test; 580 581 std::cout << " Checking to see if the merged program executes correctly: "; 582 bool Broken = TestMergedProgram(BD, Optimized, Safe, true); 583 std::cout << (Broken ? " nope.\n" : " yup.\n"); 584 return Broken; 585} 586 587 588/// debugMiscompilation - This method is used when the passes selected are not 589/// crashing, but the generated output is semantically different from the 590/// input. 591/// 592bool BugDriver::debugMiscompilation() { 593 // Make sure something was miscompiled... 594 if (!BugpointIsInterrupted) 595 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) { 596 std::cerr << "*** Optimized program matches reference output! No problem" 597 << " detected...\nbugpoint can't help you with your problem!\n"; 598 return false; 599 } 600 601 std::cout << "\n*** Found miscompiling pass" 602 << (getPassesToRun().size() == 1 ? "" : "es") << ": " 603 << getPassesString(getPassesToRun()) << '\n'; 604 EmitProgressBitcode("passinput"); 605 606 std::vector<Function*> MiscompiledFunctions = 607 DebugAMiscompilation(*this, TestOptimizer); 608 609 // Output a bunch of bitcode files for the user... 610 std::cout << "Outputting reduced bitcode files which expose the problem:\n"; 611 Module *ToNotOptimize = CloneModule(getProgram()); 612 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, 613 MiscompiledFunctions); 614 615 std::cout << " Non-optimized portion: "; 616 ToNotOptimize = swapProgramIn(ToNotOptimize); 617 EmitProgressBitcode("tonotoptimize", true); 618 setNewProgram(ToNotOptimize); // Delete hacked module. 619 620 std::cout << " Portion that is input to optimizer: "; 621 ToOptimize = swapProgramIn(ToOptimize); 622 EmitProgressBitcode("tooptimize"); 623 setNewProgram(ToOptimize); // Delete hacked module. 624 625 return false; 626} 627 628/// CleanupAndPrepareModules - Get the specified modules ready for code 629/// generator testing. 630/// 631static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test, 632 Module *Safe) { 633 // Clean up the modules, removing extra cruft that we don't need anymore... 634 Test = BD.performFinalCleanups(Test); 635 636 // If we are executing the JIT, we have several nasty issues to take care of. 637 if (!BD.isExecutingJIT()) return; 638 639 // First, if the main function is in the Safe module, we must add a stub to 640 // the Test module to call into it. Thus, we create a new function `main' 641 // which just calls the old one. 642 if (Function *oldMain = Safe->getFunction("main")) 643 if (!oldMain->isDeclaration()) { 644 // Rename it 645 oldMain->setName("llvm_bugpoint_old_main"); 646 // Create a NEW `main' function with same type in the test module. 647 Function *newMain = new Function(oldMain->getFunctionType(), 648 GlobalValue::ExternalLinkage, 649 "main", Test); 650 // Create an `oldmain' prototype in the test module, which will 651 // corresponds to the real main function in the same module. 652 Function *oldMainProto = new Function(oldMain->getFunctionType(), 653 GlobalValue::ExternalLinkage, 654 oldMain->getName(), Test); 655 // Set up and remember the argument list for the main function. 656 std::vector<Value*> args; 657 for (Function::arg_iterator 658 I = newMain->arg_begin(), E = newMain->arg_end(), 659 OI = oldMain->arg_begin(); I != E; ++I, ++OI) { 660 I->setName(OI->getName()); // Copy argument names from oldMain 661 args.push_back(I); 662 } 663 664 // Call the old main function and return its result 665 BasicBlock *BB = new BasicBlock("entry", newMain); 666 CallInst *call = new CallInst(oldMainProto, args.begin(), args.end(), 667 "", BB); 668 669 // If the type of old function wasn't void, return value of call 670 new ReturnInst(call, BB); 671 } 672 673 // The second nasty issue we must deal with in the JIT is that the Safe 674 // module cannot directly reference any functions defined in the test 675 // module. Instead, we use a JIT API call to dynamically resolve the 676 // symbol. 677 678 // Add the resolver to the Safe module. 679 // Prototype: void *getPointerToNamedFunction(const char* Name) 680 Constant *resolverFunc = 681 Safe->getOrInsertFunction("getPointerToNamedFunction", 682 PointerType::get(Type::Int8Ty), 683 PointerType::get(Type::Int8Ty), (Type *)0); 684 685 // Use the function we just added to get addresses of functions we need. 686 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) { 687 if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc && 688 F->getIntrinsicID() == 0 /* ignore intrinsics */) { 689 Function *TestFn = Test->getFunction(F->getName()); 690 691 // Don't forward functions which are external in the test module too. 692 if (TestFn && !TestFn->isDeclaration()) { 693 // 1. Add a string constant with its name to the global file 694 Constant *InitArray = ConstantArray::get(F->getName()); 695 GlobalVariable *funcName = 696 new GlobalVariable(InitArray->getType(), true /*isConstant*/, 697 GlobalValue::InternalLinkage, InitArray, 698 F->getName() + "_name", Safe); 699 700 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an 701 // sbyte* so it matches the signature of the resolver function. 702 703 // GetElementPtr *funcName, ulong 0, ulong 0 704 std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::Int32Ty)); 705 Value *GEP = ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2); 706 std::vector<Value*> ResolverArgs; 707 ResolverArgs.push_back(GEP); 708 709 // Rewrite uses of F in global initializers, etc. to uses of a wrapper 710 // function that dynamically resolves the calls to F via our JIT API 711 if (!F->use_empty()) { 712 // Create a new global to hold the cached function pointer. 713 Constant *NullPtr = ConstantPointerNull::get(F->getType()); 714 GlobalVariable *Cache = 715 new GlobalVariable(F->getType(), false,GlobalValue::InternalLinkage, 716 NullPtr,F->getName()+".fpcache", F->getParent()); 717 718 // Construct a new stub function that will re-route calls to F 719 const FunctionType *FuncTy = F->getFunctionType(); 720 Function *FuncWrapper = new Function(FuncTy, 721 GlobalValue::InternalLinkage, 722 F->getName() + "_wrapper", 723 F->getParent()); 724 BasicBlock *EntryBB = new BasicBlock("entry", FuncWrapper); 725 BasicBlock *DoCallBB = new BasicBlock("usecache", FuncWrapper); 726 BasicBlock *LookupBB = new BasicBlock("lookupfp", FuncWrapper); 727 728 // Check to see if we already looked up the value. 729 Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB); 730 Value *IsNull = new ICmpInst(ICmpInst::ICMP_EQ, CachedVal, 731 NullPtr, "isNull", EntryBB); 732 new BranchInst(LookupBB, DoCallBB, IsNull, EntryBB); 733 734 // Resolve the call to function F via the JIT API: 735 // 736 // call resolver(GetElementPtr...) 737 CallInst *Resolver = new CallInst(resolverFunc, ResolverArgs.begin(), 738 ResolverArgs.end(), 739 "resolver", LookupBB); 740 // cast the result from the resolver to correctly-typed function 741 CastInst *CastedResolver = new BitCastInst(Resolver, 742 PointerType::get(F->getFunctionType()), "resolverCast", LookupBB); 743 744 // Save the value in our cache. 745 new StoreInst(CastedResolver, Cache, LookupBB); 746 new BranchInst(DoCallBB, LookupBB); 747 748 PHINode *FuncPtr = new PHINode(NullPtr->getType(), "fp", DoCallBB); 749 FuncPtr->addIncoming(CastedResolver, LookupBB); 750 FuncPtr->addIncoming(CachedVal, EntryBB); 751 752 // Save the argument list. 753 std::vector<Value*> Args; 754 for (Function::arg_iterator i = FuncWrapper->arg_begin(), 755 e = FuncWrapper->arg_end(); i != e; ++i) 756 Args.push_back(i); 757 758 // Pass on the arguments to the real function, return its result 759 if (F->getReturnType() == Type::VoidTy) { 760 new CallInst(FuncPtr, Args.begin(), Args.end(), "", DoCallBB); 761 new ReturnInst(DoCallBB); 762 } else { 763 CallInst *Call = new CallInst(FuncPtr, Args.begin(), Args.end(), 764 "retval", DoCallBB); 765 new ReturnInst(Call, DoCallBB); 766 } 767 768 // Use the wrapper function instead of the old function 769 F->replaceAllUsesWith(FuncWrapper); 770 } 771 } 772 } 773 } 774 775 if (verifyModule(*Test) || verifyModule(*Safe)) { 776 std::cerr << "Bugpoint has a bug, which corrupted a module!!\n"; 777 abort(); 778 } 779} 780 781 782 783/// TestCodeGenerator - This is the predicate function used to check to see if 784/// the "Test" portion of the program is miscompiled by the code generator under 785/// test. If so, return true. In any case, both module arguments are deleted. 786/// 787static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) { 788 CleanupAndPrepareModules(BD, Test, Safe); 789 790 sys::Path TestModuleBC("bugpoint.test.bc"); 791 std::string ErrMsg; 792 if (TestModuleBC.makeUnique(true, &ErrMsg)) { 793 std::cerr << BD.getToolName() << "Error making unique filename: " 794 << ErrMsg << "\n"; 795 exit(1); 796 } 797 if (BD.writeProgramToFile(TestModuleBC.toString(), Test)) { 798 std::cerr << "Error writing bitcode to `" << TestModuleBC << "'\nExiting."; 799 exit(1); 800 } 801 delete Test; 802 803 // Make the shared library 804 sys::Path SafeModuleBC("bugpoint.safe.bc"); 805 if (SafeModuleBC.makeUnique(true, &ErrMsg)) { 806 std::cerr << BD.getToolName() << "Error making unique filename: " 807 << ErrMsg << "\n"; 808 exit(1); 809 } 810 811 if (BD.writeProgramToFile(SafeModuleBC.toString(), Safe)) { 812 std::cerr << "Error writing bitcode to `" << SafeModuleBC << "'\nExiting."; 813 exit(1); 814 } 815 std::string SharedObject = BD.compileSharedObject(SafeModuleBC.toString()); 816 delete Safe; 817 818 // Run the code generator on the `Test' code, loading the shared library. 819 // The function returns whether or not the new output differs from reference. 820 int Result = BD.diffProgram(TestModuleBC.toString(), SharedObject, false); 821 822 if (Result) 823 std::cerr << ": still failing!\n"; 824 else 825 std::cerr << ": didn't fail.\n"; 826 TestModuleBC.eraseFromDisk(); 827 SafeModuleBC.eraseFromDisk(); 828 sys::Path(SharedObject).eraseFromDisk(); 829 830 return Result; 831} 832 833 834/// debugCodeGenerator - debug errors in LLC, LLI, or CBE. 835/// 836bool BugDriver::debugCodeGenerator() { 837 if ((void*)cbe == (void*)Interpreter) { 838 std::string Result = executeProgramWithCBE("bugpoint.cbe.out"); 839 std::cout << "\n*** The C backend cannot match the reference diff, but it " 840 << "is used as the 'known good'\n code generator, so I can't" 841 << " debug it. Perhaps you have a front-end problem?\n As a" 842 << " sanity check, I left the result of executing the program " 843 << "with the C backend\n in this file for you: '" 844 << Result << "'.\n"; 845 return true; 846 } 847 848 DisambiguateGlobalSymbols(Program); 849 850 std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator); 851 852 // Split the module into the two halves of the program we want. 853 Module *ToNotCodeGen = CloneModule(getProgram()); 854 Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs); 855 856 // Condition the modules 857 CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen); 858 859 sys::Path TestModuleBC("bugpoint.test.bc"); 860 std::string ErrMsg; 861 if (TestModuleBC.makeUnique(true, &ErrMsg)) { 862 std::cerr << getToolName() << "Error making unique filename: " 863 << ErrMsg << "\n"; 864 exit(1); 865 } 866 867 if (writeProgramToFile(TestModuleBC.toString(), ToCodeGen)) { 868 std::cerr << "Error writing bitcode to `" << TestModuleBC << "'\nExiting."; 869 exit(1); 870 } 871 delete ToCodeGen; 872 873 // Make the shared library 874 sys::Path SafeModuleBC("bugpoint.safe.bc"); 875 if (SafeModuleBC.makeUnique(true, &ErrMsg)) { 876 std::cerr << getToolName() << "Error making unique filename: " 877 << ErrMsg << "\n"; 878 exit(1); 879 } 880 881 if (writeProgramToFile(SafeModuleBC.toString(), ToNotCodeGen)) { 882 std::cerr << "Error writing bitcode to `" << SafeModuleBC << "'\nExiting."; 883 exit(1); 884 } 885 std::string SharedObject = compileSharedObject(SafeModuleBC.toString()); 886 delete ToNotCodeGen; 887 888 std::cout << "You can reproduce the problem with the command line: \n"; 889 if (isExecutingJIT()) { 890 std::cout << " lli -load " << SharedObject << " " << TestModuleBC; 891 } else { 892 std::cout << " llc -f " << TestModuleBC << " -o " << TestModuleBC<< ".s\n"; 893 std::cout << " gcc " << SharedObject << " " << TestModuleBC 894 << ".s -o " << TestModuleBC << ".exe"; 895#if defined (HAVE_LINK_R) 896 std::cout << " -Wl,-R."; 897#endif 898 std::cout << "\n"; 899 std::cout << " " << TestModuleBC << ".exe"; 900 } 901 for (unsigned i=0, e = InputArgv.size(); i != e; ++i) 902 std::cout << " " << InputArgv[i]; 903 std::cout << '\n'; 904 std::cout << "The shared object was created with:\n llc -march=c " 905 << SafeModuleBC << " -o temporary.c\n" 906 << " gcc -xc temporary.c -O2 -o " << SharedObject 907#if defined(sparc) || defined(__sparc__) || defined(__sparcv9) 908 << " -G" // Compile a shared library, `-G' for Sparc 909#else 910 << " -shared" // `-shared' for Linux/X86, maybe others 911#endif 912 << " -fno-strict-aliasing\n"; 913 914 return false; 915} 916