Miscompilation.cpp revision 198090
1193323Sed//===- Miscompilation.cpp - Debug program miscompilations -----------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file implements optimizer and code generation miscompilation debugging 11193323Sed// support. 12193323Sed// 13193323Sed//===----------------------------------------------------------------------===// 14193323Sed 15193323Sed#include "BugDriver.h" 16193323Sed#include "ListReducer.h" 17198090Srdivacky#include "ToolRunner.h" 18193323Sed#include "llvm/Constants.h" 19193323Sed#include "llvm/DerivedTypes.h" 20193323Sed#include "llvm/Instructions.h" 21193323Sed#include "llvm/Linker.h" 22193323Sed#include "llvm/Module.h" 23193323Sed#include "llvm/Pass.h" 24193323Sed#include "llvm/Analysis/Verifier.h" 25193323Sed#include "llvm/Support/Mangler.h" 26193323Sed#include "llvm/Transforms/Utils/Cloning.h" 27193323Sed#include "llvm/Support/CommandLine.h" 28193323Sed#include "llvm/Support/FileUtilities.h" 29193323Sed#include "llvm/Config/config.h" // for HAVE_LINK_R 30193323Sedusing namespace llvm; 31193323Sed 32193323Sednamespace llvm { 33198090Srdivacky extern cl::opt<std::string> OutputPrefix; 34193323Sed extern cl::list<std::string> InputArgv; 35193323Sed} 36193323Sed 37193323Sednamespace { 38193323Sed static llvm::cl::opt<bool> 39193323Sed DisableLoopExtraction("disable-loop-extraction", 40193323Sed cl::desc("Don't extract loops when searching for miscompilations"), 41193323Sed cl::init(false)); 42198090Srdivacky static llvm::cl::opt<bool> 43198090Srdivacky DisableBlockExtraction("disable-block-extraction", 44198090Srdivacky cl::desc("Don't extract blocks when searching for miscompilations"), 45198090Srdivacky cl::init(false)); 46193323Sed 47193323Sed class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> { 48193323Sed BugDriver &BD; 49193323Sed public: 50193323Sed ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} 51193323Sed 52193323Sed virtual TestResult doTest(std::vector<const PassInfo*> &Prefix, 53193323Sed std::vector<const PassInfo*> &Suffix); 54193323Sed }; 55193323Sed} 56193323Sed 57193323Sed/// TestResult - After passes have been split into a test group and a control 58193323Sed/// group, see if they still break the program. 59193323Sed/// 60193323SedReduceMiscompilingPasses::TestResult 61193323SedReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix, 62193323Sed std::vector<const PassInfo*> &Suffix) { 63193323Sed // First, run the program with just the Suffix passes. If it is still broken 64193323Sed // with JUST the kept passes, discard the prefix passes. 65198090Srdivacky outs() << "Checking to see if '" << getPassesString(Suffix) 66198090Srdivacky << "' compiles correctly: "; 67193323Sed 68193323Sed std::string BitcodeResult; 69193323Sed if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) { 70198090Srdivacky errs() << " Error running this sequence of passes" 71198090Srdivacky << " on the input program!\n"; 72193323Sed BD.setPassesToRun(Suffix); 73193323Sed BD.EmitProgressBitcode("pass-error", false); 74193323Sed exit(BD.debugOptimizerCrash()); 75193323Sed } 76198090Srdivacky 77193323Sed // Check to see if the finished program matches the reference output... 78193323Sed if (BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/)) { 79198090Srdivacky outs() << " nope.\n"; 80193323Sed if (Suffix.empty()) { 81198090Srdivacky errs() << BD.getToolName() << ": I'm confused: the test fails when " 82198090Srdivacky << "no passes are run, nondeterministic program?\n"; 83193323Sed exit(1); 84193323Sed } 85193323Sed return KeepSuffix; // Miscompilation detected! 86193323Sed } 87198090Srdivacky outs() << " yup.\n"; // No miscompilation! 88193323Sed 89193323Sed if (Prefix.empty()) return NoFailure; 90193323Sed 91193323Sed // Next, see if the program is broken if we run the "prefix" passes first, 92193323Sed // then separately run the "kept" passes. 93198090Srdivacky outs() << "Checking to see if '" << getPassesString(Prefix) 94198090Srdivacky << "' compiles correctly: "; 95193323Sed 96193323Sed // If it is not broken with the kept passes, it's possible that the prefix 97193323Sed // passes must be run before the kept passes to break it. If the program 98193323Sed // WORKS after the prefix passes, but then fails if running the prefix AND 99193323Sed // kept passes, we can update our bitcode file to include the result of the 100193323Sed // prefix passes, then discard the prefix passes. 101193323Sed // 102193323Sed if (BD.runPasses(Prefix, BitcodeResult, false/*delete*/, true/*quiet*/)) { 103198090Srdivacky errs() << " Error running this sequence of passes" 104198090Srdivacky << " on the input program!\n"; 105193323Sed BD.setPassesToRun(Prefix); 106193323Sed BD.EmitProgressBitcode("pass-error", false); 107193323Sed exit(BD.debugOptimizerCrash()); 108193323Sed } 109193323Sed 110193323Sed // If the prefix maintains the predicate by itself, only keep the prefix! 111193323Sed if (BD.diffProgram(BitcodeResult)) { 112198090Srdivacky outs() << " nope.\n"; 113193323Sed sys::Path(BitcodeResult).eraseFromDisk(); 114193323Sed return KeepPrefix; 115193323Sed } 116198090Srdivacky outs() << " yup.\n"; // No miscompilation! 117193323Sed 118193323Sed // Ok, so now we know that the prefix passes work, try running the suffix 119193323Sed // passes on the result of the prefix passes. 120193323Sed // 121195340Sed Module *PrefixOutput = ParseInputFile(BitcodeResult, BD.getContext()); 122193323Sed if (PrefixOutput == 0) { 123198090Srdivacky errs() << BD.getToolName() << ": Error reading bitcode file '" 124198090Srdivacky << BitcodeResult << "'!\n"; 125193323Sed exit(1); 126193323Sed } 127193323Sed sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk 128193323Sed 129193323Sed // Don't check if there are no passes in the suffix. 130193323Sed if (Suffix.empty()) 131193323Sed return NoFailure; 132193323Sed 133198090Srdivacky outs() << "Checking to see if '" << getPassesString(Suffix) 134193323Sed << "' passes compile correctly after the '" 135193323Sed << getPassesString(Prefix) << "' passes: "; 136193323Sed 137193323Sed Module *OriginalInput = BD.swapProgramIn(PrefixOutput); 138193323Sed if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) { 139198090Srdivacky errs() << " Error running this sequence of passes" 140198090Srdivacky << " on the input program!\n"; 141193323Sed BD.setPassesToRun(Suffix); 142193323Sed BD.EmitProgressBitcode("pass-error", false); 143193323Sed exit(BD.debugOptimizerCrash()); 144193323Sed } 145193323Sed 146193323Sed // Run the result... 147193323Sed if (BD.diffProgram(BitcodeResult, "", true/*delete bitcode*/)) { 148198090Srdivacky outs() << " nope.\n"; 149193323Sed delete OriginalInput; // We pruned down the original input... 150193323Sed return KeepSuffix; 151193323Sed } 152193323Sed 153193323Sed // Otherwise, we must not be running the bad pass anymore. 154198090Srdivacky outs() << " yup.\n"; // No miscompilation! 155193323Sed delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test 156193323Sed return NoFailure; 157193323Sed} 158193323Sed 159193323Sednamespace { 160193323Sed class ReduceMiscompilingFunctions : public ListReducer<Function*> { 161193323Sed BugDriver &BD; 162193323Sed bool (*TestFn)(BugDriver &, Module *, Module *); 163193323Sed public: 164193323Sed ReduceMiscompilingFunctions(BugDriver &bd, 165193323Sed bool (*F)(BugDriver &, Module *, Module *)) 166193323Sed : BD(bd), TestFn(F) {} 167193323Sed 168193323Sed virtual TestResult doTest(std::vector<Function*> &Prefix, 169193323Sed std::vector<Function*> &Suffix) { 170193323Sed if (!Suffix.empty() && TestFuncs(Suffix)) 171193323Sed return KeepSuffix; 172193323Sed if (!Prefix.empty() && TestFuncs(Prefix)) 173193323Sed return KeepPrefix; 174193323Sed return NoFailure; 175193323Sed } 176193323Sed 177193323Sed bool TestFuncs(const std::vector<Function*> &Prefix); 178193323Sed }; 179193323Sed} 180193323Sed 181193323Sed/// TestMergedProgram - Given two modules, link them together and run the 182193323Sed/// program, checking to see if the program matches the diff. If the diff 183193323Sed/// matches, return false, otherwise return true. If the DeleteInputs argument 184193323Sed/// is set to true then this function deletes both input modules before it 185193323Sed/// returns. 186193323Sed/// 187193323Sedstatic bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2, 188193323Sed bool DeleteInputs) { 189193323Sed // Link the two portions of the program back to together. 190193323Sed std::string ErrorMsg; 191193323Sed if (!DeleteInputs) { 192193323Sed M1 = CloneModule(M1); 193193323Sed M2 = CloneModule(M2); 194193323Sed } 195193323Sed if (Linker::LinkModules(M1, M2, &ErrorMsg)) { 196198090Srdivacky errs() << BD.getToolName() << ": Error linking modules together:" 197198090Srdivacky << ErrorMsg << '\n'; 198193323Sed exit(1); 199193323Sed } 200193323Sed delete M2; // We are done with this module. 201193323Sed 202193323Sed Module *OldProgram = BD.swapProgramIn(M1); 203193323Sed 204193323Sed // Execute the program. If it does not match the expected output, we must 205193323Sed // return true. 206193323Sed bool Broken = BD.diffProgram(); 207193323Sed 208193323Sed // Delete the linked module & restore the original 209193323Sed BD.swapProgramIn(OldProgram); 210193323Sed delete M1; 211193323Sed return Broken; 212193323Sed} 213193323Sed 214193323Sed/// TestFuncs - split functions in a Module into two groups: those that are 215193323Sed/// under consideration for miscompilation vs. those that are not, and test 216193323Sed/// accordingly. Each group of functions becomes a separate Module. 217193323Sed/// 218193323Sedbool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){ 219193323Sed // Test to see if the function is misoptimized if we ONLY run it on the 220193323Sed // functions listed in Funcs. 221198090Srdivacky outs() << "Checking to see if the program is misoptimized when " 222198090Srdivacky << (Funcs.size()==1 ? "this function is" : "these functions are") 223198090Srdivacky << " run through the pass" 224198090Srdivacky << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":"; 225193323Sed PrintFunctionList(Funcs); 226198090Srdivacky outs() << '\n'; 227193323Sed 228193323Sed // Split the module into the two halves of the program we want. 229193323Sed DenseMap<const Value*, Value*> ValueMap; 230193323Sed Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap); 231193323Sed Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs, 232193323Sed ValueMap); 233193323Sed 234193323Sed // Run the predicate, note that the predicate will delete both input modules. 235193323Sed return TestFn(BD, ToOptimize, ToNotOptimize); 236193323Sed} 237193323Sed 238193323Sed/// DisambiguateGlobalSymbols - Mangle symbols to guarantee uniqueness by 239193323Sed/// modifying predominantly internal symbols rather than external ones. 240193323Sed/// 241193323Sedstatic void DisambiguateGlobalSymbols(Module *M) { 242193323Sed // Try not to cause collisions by minimizing chances of renaming an 243193323Sed // already-external symbol, so take in external globals and functions as-is. 244193323Sed // The code should work correctly without disambiguation (assuming the same 245193323Sed // mangler is used by the two code generators), but having symbols with the 246193323Sed // same name causes warnings to be emitted by the code generator. 247193323Sed Mangler Mang(*M); 248193323Sed // Agree with the CBE on symbol naming 249193323Sed Mang.markCharUnacceptable('.'); 250193323Sed for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 251198090Srdivacky I != E; ++I) { 252198090Srdivacky // Don't mangle asm names. 253198090Srdivacky if (!I->hasName() || I->getName()[0] != 1) 254198090Srdivacky I->setName(Mang.getMangledName(I)); 255198090Srdivacky } 256198090Srdivacky for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { 257198090Srdivacky // Don't mangle asm names or intrinsics. 258198090Srdivacky if ((!I->hasName() || I->getName()[0] != 1) && 259198090Srdivacky I->getIntrinsicID() == 0) 260198090Srdivacky I->setName(Mang.getMangledName(I)); 261198090Srdivacky } 262193323Sed} 263193323Sed 264193323Sed/// ExtractLoops - Given a reduced list of functions that still exposed the bug, 265193323Sed/// check to see if we can extract the loops in the region without obscuring the 266193323Sed/// bug. If so, it reduces the amount of code identified. 267193323Sed/// 268193323Sedstatic bool ExtractLoops(BugDriver &BD, 269193323Sed bool (*TestFn)(BugDriver &, Module *, Module *), 270193323Sed std::vector<Function*> &MiscompiledFunctions) { 271193323Sed bool MadeChange = false; 272193323Sed while (1) { 273193323Sed if (BugpointIsInterrupted) return MadeChange; 274193323Sed 275193323Sed DenseMap<const Value*, Value*> ValueMap; 276193323Sed Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap); 277193323Sed Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, 278193323Sed MiscompiledFunctions, 279193323Sed ValueMap); 280193323Sed Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize); 281193323Sed if (!ToOptimizeLoopExtracted) { 282193323Sed // If the loop extractor crashed or if there were no extractible loops, 283193323Sed // then this chapter of our odyssey is over with. 284193323Sed delete ToNotOptimize; 285193323Sed delete ToOptimize; 286193323Sed return MadeChange; 287193323Sed } 288193323Sed 289198090Srdivacky errs() << "Extracted a loop from the breaking portion of the program.\n"; 290193323Sed 291193323Sed // Bugpoint is intentionally not very trusting of LLVM transformations. In 292193323Sed // particular, we're not going to assume that the loop extractor works, so 293193323Sed // we're going to test the newly loop extracted program to make sure nothing 294193323Sed // has broken. If something broke, then we'll inform the user and stop 295193323Sed // extraction. 296193323Sed AbstractInterpreter *AI = BD.switchToSafeInterpreter(); 297193323Sed if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) { 298193323Sed BD.switchToInterpreter(AI); 299193323Sed 300193323Sed // Merged program doesn't work anymore! 301198090Srdivacky errs() << " *** ERROR: Loop extraction broke the program. :(" 302198090Srdivacky << " Please report a bug!\n"; 303198090Srdivacky errs() << " Continuing on with un-loop-extracted version.\n"; 304193323Sed 305198090Srdivacky BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc", 306198090Srdivacky ToNotOptimize); 307198090Srdivacky BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc", 308198090Srdivacky ToOptimize); 309198090Srdivacky BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc", 310193323Sed ToOptimizeLoopExtracted); 311193323Sed 312198090Srdivacky errs() << "Please submit the " 313198090Srdivacky << OutputPrefix << "-loop-extract-fail-*.bc files.\n"; 314193323Sed delete ToOptimize; 315193323Sed delete ToNotOptimize; 316193323Sed delete ToOptimizeLoopExtracted; 317193323Sed return MadeChange; 318193323Sed } 319193323Sed delete ToOptimize; 320193323Sed BD.switchToInterpreter(AI); 321193323Sed 322198090Srdivacky outs() << " Testing after loop extraction:\n"; 323193323Sed // Clone modules, the tester function will free them. 324193323Sed Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted); 325193323Sed Module *TNOBackup = CloneModule(ToNotOptimize); 326193323Sed if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) { 327198090Srdivacky outs() << "*** Loop extraction masked the problem. Undoing.\n"; 328193323Sed // If the program is not still broken, then loop extraction did something 329193323Sed // that masked the error. Stop loop extraction now. 330193323Sed delete TOLEBackup; 331193323Sed delete TNOBackup; 332193323Sed return MadeChange; 333193323Sed } 334193323Sed ToOptimizeLoopExtracted = TOLEBackup; 335193323Sed ToNotOptimize = TNOBackup; 336193323Sed 337198090Srdivacky outs() << "*** Loop extraction successful!\n"; 338193323Sed 339193323Sed std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions; 340193323Sed for (Module::iterator I = ToOptimizeLoopExtracted->begin(), 341193323Sed E = ToOptimizeLoopExtracted->end(); I != E; ++I) 342193323Sed if (!I->isDeclaration()) 343193323Sed MisCompFunctions.push_back(std::make_pair(I->getName(), 344193323Sed I->getFunctionType())); 345193323Sed 346193323Sed // Okay, great! Now we know that we extracted a loop and that loop 347193323Sed // extraction both didn't break the program, and didn't mask the problem. 348193323Sed // Replace the current program with the loop extracted version, and try to 349193323Sed // extract another loop. 350193323Sed std::string ErrorMsg; 351193323Sed if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)){ 352198090Srdivacky errs() << BD.getToolName() << ": Error linking modules together:" 353198090Srdivacky << ErrorMsg << '\n'; 354193323Sed exit(1); 355193323Sed } 356193323Sed delete ToOptimizeLoopExtracted; 357193323Sed 358193323Sed // All of the Function*'s in the MiscompiledFunctions list are in the old 359193323Sed // module. Update this list to include all of the functions in the 360193323Sed // optimized and loop extracted module. 361193323Sed MiscompiledFunctions.clear(); 362193323Sed for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { 363193323Sed Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first); 364193323Sed 365193323Sed assert(NewF && "Function not found??"); 366193323Sed assert(NewF->getFunctionType() == MisCompFunctions[i].second && 367193323Sed "found wrong function type?"); 368193323Sed MiscompiledFunctions.push_back(NewF); 369193323Sed } 370193323Sed 371193323Sed BD.setNewProgram(ToNotOptimize); 372193323Sed MadeChange = true; 373193323Sed } 374193323Sed} 375193323Sed 376193323Sednamespace { 377193323Sed class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> { 378193323Sed BugDriver &BD; 379193323Sed bool (*TestFn)(BugDriver &, Module *, Module *); 380193323Sed std::vector<Function*> FunctionsBeingTested; 381193323Sed public: 382193323Sed ReduceMiscompiledBlocks(BugDriver &bd, 383193323Sed bool (*F)(BugDriver &, Module *, Module *), 384193323Sed const std::vector<Function*> &Fns) 385193323Sed : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {} 386193323Sed 387193323Sed virtual TestResult doTest(std::vector<BasicBlock*> &Prefix, 388193323Sed std::vector<BasicBlock*> &Suffix) { 389193323Sed if (!Suffix.empty() && TestFuncs(Suffix)) 390193323Sed return KeepSuffix; 391193323Sed if (TestFuncs(Prefix)) 392193323Sed return KeepPrefix; 393193323Sed return NoFailure; 394193323Sed } 395193323Sed 396193323Sed bool TestFuncs(const std::vector<BasicBlock*> &Prefix); 397193323Sed }; 398193323Sed} 399193323Sed 400193323Sed/// TestFuncs - Extract all blocks for the miscompiled functions except for the 401193323Sed/// specified blocks. If the problem still exists, return true. 402193323Sed/// 403193323Sedbool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs) { 404193323Sed // Test to see if the function is misoptimized if we ONLY run it on the 405193323Sed // functions listed in Funcs. 406198090Srdivacky outs() << "Checking to see if the program is misoptimized when all "; 407193323Sed if (!BBs.empty()) { 408198090Srdivacky outs() << "but these " << BBs.size() << " blocks are extracted: "; 409193323Sed for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i) 410198090Srdivacky outs() << BBs[i]->getName() << " "; 411198090Srdivacky if (BBs.size() > 10) outs() << "..."; 412193323Sed } else { 413198090Srdivacky outs() << "blocks are extracted."; 414193323Sed } 415198090Srdivacky outs() << '\n'; 416193323Sed 417193323Sed // Split the module into the two halves of the program we want. 418193323Sed DenseMap<const Value*, Value*> ValueMap; 419193323Sed Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap); 420193323Sed Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, 421193323Sed FunctionsBeingTested, 422193323Sed ValueMap); 423193323Sed 424193323Sed // Try the extraction. If it doesn't work, then the block extractor crashed 425193323Sed // or something, in which case bugpoint can't chase down this possibility. 426193323Sed if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) { 427193323Sed delete ToOptimize; 428193323Sed // Run the predicate, not that the predicate will delete both input modules. 429193323Sed return TestFn(BD, New, ToNotOptimize); 430193323Sed } 431193323Sed delete ToOptimize; 432193323Sed delete ToNotOptimize; 433193323Sed return false; 434193323Sed} 435193323Sed 436193323Sed 437193323Sed/// ExtractBlocks - Given a reduced list of functions that still expose the bug, 438193323Sed/// extract as many basic blocks from the region as possible without obscuring 439193323Sed/// the bug. 440193323Sed/// 441193323Sedstatic bool ExtractBlocks(BugDriver &BD, 442193323Sed bool (*TestFn)(BugDriver &, Module *, Module *), 443193323Sed std::vector<Function*> &MiscompiledFunctions) { 444193323Sed if (BugpointIsInterrupted) return false; 445193323Sed 446193323Sed std::vector<BasicBlock*> Blocks; 447193323Sed for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) 448193323Sed for (Function::iterator I = MiscompiledFunctions[i]->begin(), 449193323Sed E = MiscompiledFunctions[i]->end(); I != E; ++I) 450193323Sed Blocks.push_back(I); 451193323Sed 452193323Sed // Use the list reducer to identify blocks that can be extracted without 453193323Sed // obscuring the bug. The Blocks list will end up containing blocks that must 454193323Sed // be retained from the original program. 455193323Sed unsigned OldSize = Blocks.size(); 456193323Sed 457193323Sed // Check to see if all blocks are extractible first. 458193323Sed if (ReduceMiscompiledBlocks(BD, TestFn, 459193323Sed MiscompiledFunctions).TestFuncs(std::vector<BasicBlock*>())) { 460193323Sed Blocks.clear(); 461193323Sed } else { 462193323Sed ReduceMiscompiledBlocks(BD, TestFn,MiscompiledFunctions).reduceList(Blocks); 463193323Sed if (Blocks.size() == OldSize) 464193323Sed return false; 465193323Sed } 466193323Sed 467193323Sed DenseMap<const Value*, Value*> ValueMap; 468193323Sed Module *ProgClone = CloneModule(BD.getProgram(), ValueMap); 469193323Sed Module *ToExtract = SplitFunctionsOutOfModule(ProgClone, 470193323Sed MiscompiledFunctions, 471193323Sed ValueMap); 472193323Sed Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract); 473193323Sed if (Extracted == 0) { 474193323Sed // Weird, extraction should have worked. 475198090Srdivacky errs() << "Nondeterministic problem extracting blocks??\n"; 476193323Sed delete ProgClone; 477193323Sed delete ToExtract; 478193323Sed return false; 479193323Sed } 480193323Sed 481193323Sed // Otherwise, block extraction succeeded. Link the two program fragments back 482193323Sed // together. 483193323Sed delete ToExtract; 484193323Sed 485193323Sed std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions; 486193323Sed for (Module::iterator I = Extracted->begin(), E = Extracted->end(); 487193323Sed I != E; ++I) 488193323Sed if (!I->isDeclaration()) 489193323Sed MisCompFunctions.push_back(std::make_pair(I->getName(), 490193323Sed I->getFunctionType())); 491193323Sed 492193323Sed std::string ErrorMsg; 493193323Sed if (Linker::LinkModules(ProgClone, Extracted, &ErrorMsg)) { 494198090Srdivacky errs() << BD.getToolName() << ": Error linking modules together:" 495198090Srdivacky << ErrorMsg << '\n'; 496193323Sed exit(1); 497193323Sed } 498193323Sed delete Extracted; 499193323Sed 500193323Sed // Set the new program and delete the old one. 501193323Sed BD.setNewProgram(ProgClone); 502193323Sed 503193323Sed // Update the list of miscompiled functions. 504193323Sed MiscompiledFunctions.clear(); 505193323Sed 506193323Sed for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { 507193323Sed Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first); 508193323Sed assert(NewF && "Function not found??"); 509193323Sed assert(NewF->getFunctionType() == MisCompFunctions[i].second && 510193323Sed "Function has wrong type??"); 511193323Sed MiscompiledFunctions.push_back(NewF); 512193323Sed } 513193323Sed 514193323Sed return true; 515193323Sed} 516193323Sed 517193323Sed 518193323Sed/// DebugAMiscompilation - This is a generic driver to narrow down 519193323Sed/// miscompilations, either in an optimization or a code generator. 520193323Sed/// 521193323Sedstatic std::vector<Function*> 522193323SedDebugAMiscompilation(BugDriver &BD, 523193323Sed bool (*TestFn)(BugDriver &, Module *, Module *)) { 524193323Sed // Okay, now that we have reduced the list of passes which are causing the 525193323Sed // failure, see if we can pin down which functions are being 526193323Sed // miscompiled... first build a list of all of the non-external functions in 527193323Sed // the program. 528193323Sed std::vector<Function*> MiscompiledFunctions; 529193323Sed Module *Prog = BD.getProgram(); 530193323Sed for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I) 531193323Sed if (!I->isDeclaration()) 532193323Sed MiscompiledFunctions.push_back(I); 533193323Sed 534193323Sed // Do the reduction... 535193323Sed if (!BugpointIsInterrupted) 536193323Sed ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); 537193323Sed 538198090Srdivacky outs() << "\n*** The following function" 539198090Srdivacky << (MiscompiledFunctions.size() == 1 ? " is" : "s are") 540198090Srdivacky << " being miscompiled: "; 541193323Sed PrintFunctionList(MiscompiledFunctions); 542198090Srdivacky outs() << '\n'; 543193323Sed 544193323Sed // See if we can rip any loops out of the miscompiled functions and still 545193323Sed // trigger the problem. 546193323Sed 547193323Sed if (!BugpointIsInterrupted && !DisableLoopExtraction && 548193323Sed ExtractLoops(BD, TestFn, MiscompiledFunctions)) { 549193323Sed // Okay, we extracted some loops and the problem still appears. See if we 550193323Sed // can eliminate some of the created functions from being candidates. 551193323Sed 552193323Sed // Loop extraction can introduce functions with the same name (foo_code). 553193323Sed // Make sure to disambiguate the symbols so that when the program is split 554193323Sed // apart that we can link it back together again. 555193323Sed DisambiguateGlobalSymbols(BD.getProgram()); 556193323Sed 557193323Sed // Do the reduction... 558193323Sed if (!BugpointIsInterrupted) 559193323Sed ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); 560193323Sed 561198090Srdivacky outs() << "\n*** The following function" 562198090Srdivacky << (MiscompiledFunctions.size() == 1 ? " is" : "s are") 563198090Srdivacky << " being miscompiled: "; 564193323Sed PrintFunctionList(MiscompiledFunctions); 565198090Srdivacky outs() << '\n'; 566193323Sed } 567193323Sed 568198090Srdivacky if (!BugpointIsInterrupted && !DisableBlockExtraction && 569193323Sed ExtractBlocks(BD, TestFn, MiscompiledFunctions)) { 570193323Sed // Okay, we extracted some blocks and the problem still appears. See if we 571193323Sed // can eliminate some of the created functions from being candidates. 572193323Sed 573193323Sed // Block extraction can introduce functions with the same name (foo_code). 574193323Sed // Make sure to disambiguate the symbols so that when the program is split 575193323Sed // apart that we can link it back together again. 576193323Sed DisambiguateGlobalSymbols(BD.getProgram()); 577193323Sed 578193323Sed // Do the reduction... 579193323Sed ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); 580193323Sed 581198090Srdivacky outs() << "\n*** The following function" 582198090Srdivacky << (MiscompiledFunctions.size() == 1 ? " is" : "s are") 583198090Srdivacky << " being miscompiled: "; 584193323Sed PrintFunctionList(MiscompiledFunctions); 585198090Srdivacky outs() << '\n'; 586193323Sed } 587193323Sed 588193323Sed return MiscompiledFunctions; 589193323Sed} 590193323Sed 591193323Sed/// TestOptimizer - This is the predicate function used to check to see if the 592193323Sed/// "Test" portion of the program is misoptimized. If so, return true. In any 593193323Sed/// case, both module arguments are deleted. 594193323Sed/// 595193323Sedstatic bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) { 596193323Sed // Run the optimization passes on ToOptimize, producing a transformed version 597193323Sed // of the functions being tested. 598198090Srdivacky outs() << " Optimizing functions being tested: "; 599193323Sed Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(), 600193323Sed /*AutoDebugCrashes*/true); 601198090Srdivacky outs() << "done.\n"; 602193323Sed delete Test; 603193323Sed 604198090Srdivacky outs() << " Checking to see if the merged program executes correctly: "; 605193323Sed bool Broken = TestMergedProgram(BD, Optimized, Safe, true); 606198090Srdivacky outs() << (Broken ? " nope.\n" : " yup.\n"); 607193323Sed return Broken; 608193323Sed} 609193323Sed 610193323Sed 611193323Sed/// debugMiscompilation - This method is used when the passes selected are not 612193323Sed/// crashing, but the generated output is semantically different from the 613193323Sed/// input. 614193323Sed/// 615193323Sedbool BugDriver::debugMiscompilation() { 616193323Sed // Make sure something was miscompiled... 617193323Sed if (!BugpointIsInterrupted) 618193323Sed if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) { 619198090Srdivacky errs() << "*** Optimized program matches reference output! No problem" 620198090Srdivacky << " detected...\nbugpoint can't help you with your problem!\n"; 621193323Sed return false; 622193323Sed } 623193323Sed 624198090Srdivacky outs() << "\n*** Found miscompiling pass" 625198090Srdivacky << (getPassesToRun().size() == 1 ? "" : "es") << ": " 626198090Srdivacky << getPassesString(getPassesToRun()) << '\n'; 627193323Sed EmitProgressBitcode("passinput"); 628193323Sed 629193323Sed std::vector<Function*> MiscompiledFunctions = 630193323Sed DebugAMiscompilation(*this, TestOptimizer); 631193323Sed 632193323Sed // Output a bunch of bitcode files for the user... 633198090Srdivacky outs() << "Outputting reduced bitcode files which expose the problem:\n"; 634193323Sed DenseMap<const Value*, Value*> ValueMap; 635193323Sed Module *ToNotOptimize = CloneModule(getProgram(), ValueMap); 636193323Sed Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, 637193323Sed MiscompiledFunctions, 638193323Sed ValueMap); 639193323Sed 640198090Srdivacky outs() << " Non-optimized portion: "; 641193323Sed ToNotOptimize = swapProgramIn(ToNotOptimize); 642193323Sed EmitProgressBitcode("tonotoptimize", true); 643193323Sed setNewProgram(ToNotOptimize); // Delete hacked module. 644193323Sed 645198090Srdivacky outs() << " Portion that is input to optimizer: "; 646193323Sed ToOptimize = swapProgramIn(ToOptimize); 647193323Sed EmitProgressBitcode("tooptimize"); 648193323Sed setNewProgram(ToOptimize); // Delete hacked module. 649193323Sed 650193323Sed return false; 651193323Sed} 652193323Sed 653193323Sed/// CleanupAndPrepareModules - Get the specified modules ready for code 654193323Sed/// generator testing. 655193323Sed/// 656193323Sedstatic void CleanupAndPrepareModules(BugDriver &BD, Module *&Test, 657193323Sed Module *Safe) { 658193323Sed // Clean up the modules, removing extra cruft that we don't need anymore... 659193323Sed Test = BD.performFinalCleanups(Test); 660193323Sed 661193323Sed // If we are executing the JIT, we have several nasty issues to take care of. 662193323Sed if (!BD.isExecutingJIT()) return; 663193323Sed 664193323Sed // First, if the main function is in the Safe module, we must add a stub to 665193323Sed // the Test module to call into it. Thus, we create a new function `main' 666193323Sed // which just calls the old one. 667193323Sed if (Function *oldMain = Safe->getFunction("main")) 668193323Sed if (!oldMain->isDeclaration()) { 669193323Sed // Rename it 670193323Sed oldMain->setName("llvm_bugpoint_old_main"); 671193323Sed // Create a NEW `main' function with same type in the test module. 672193323Sed Function *newMain = Function::Create(oldMain->getFunctionType(), 673193323Sed GlobalValue::ExternalLinkage, 674193323Sed "main", Test); 675193323Sed // Create an `oldmain' prototype in the test module, which will 676193323Sed // corresponds to the real main function in the same module. 677193323Sed Function *oldMainProto = Function::Create(oldMain->getFunctionType(), 678193323Sed GlobalValue::ExternalLinkage, 679193323Sed oldMain->getName(), Test); 680193323Sed // Set up and remember the argument list for the main function. 681193323Sed std::vector<Value*> args; 682193323Sed for (Function::arg_iterator 683193323Sed I = newMain->arg_begin(), E = newMain->arg_end(), 684193323Sed OI = oldMain->arg_begin(); I != E; ++I, ++OI) { 685193323Sed I->setName(OI->getName()); // Copy argument names from oldMain 686193323Sed args.push_back(I); 687193323Sed } 688193323Sed 689193323Sed // Call the old main function and return its result 690198090Srdivacky BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain); 691193323Sed CallInst *call = CallInst::Create(oldMainProto, args.begin(), args.end(), 692193323Sed "", BB); 693193323Sed 694193323Sed // If the type of old function wasn't void, return value of call 695198090Srdivacky ReturnInst::Create(Safe->getContext(), call, BB); 696193323Sed } 697193323Sed 698193323Sed // The second nasty issue we must deal with in the JIT is that the Safe 699193323Sed // module cannot directly reference any functions defined in the test 700193323Sed // module. Instead, we use a JIT API call to dynamically resolve the 701193323Sed // symbol. 702193323Sed 703193323Sed // Add the resolver to the Safe module. 704193323Sed // Prototype: void *getPointerToNamedFunction(const char* Name) 705193323Sed Constant *resolverFunc = 706193323Sed Safe->getOrInsertFunction("getPointerToNamedFunction", 707198090Srdivacky Type::getInt8PtrTy(Safe->getContext()), 708198090Srdivacky Type::getInt8PtrTy(Safe->getContext()), 709198090Srdivacky (Type *)0); 710193323Sed 711193323Sed // Use the function we just added to get addresses of functions we need. 712193323Sed for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) { 713193323Sed if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc && 714193323Sed !F->isIntrinsic() /* ignore intrinsics */) { 715193323Sed Function *TestFn = Test->getFunction(F->getName()); 716193323Sed 717193323Sed // Don't forward functions which are external in the test module too. 718193323Sed if (TestFn && !TestFn->isDeclaration()) { 719193323Sed // 1. Add a string constant with its name to the global file 720198090Srdivacky Constant *InitArray = ConstantArray::get(F->getContext(), F->getName()); 721193323Sed GlobalVariable *funcName = 722198090Srdivacky new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/, 723193323Sed GlobalValue::InternalLinkage, InitArray, 724198090Srdivacky F->getName() + "_name"); 725193323Sed 726193323Sed // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an 727193323Sed // sbyte* so it matches the signature of the resolver function. 728193323Sed 729193323Sed // GetElementPtr *funcName, ulong 0, ulong 0 730198090Srdivacky std::vector<Constant*> GEPargs(2, 731198090Srdivacky Constant::getNullValue(Type::getInt32Ty(F->getContext()))); 732198090Srdivacky Value *GEP = 733198090Srdivacky ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2); 734193323Sed std::vector<Value*> ResolverArgs; 735193323Sed ResolverArgs.push_back(GEP); 736193323Sed 737193323Sed // Rewrite uses of F in global initializers, etc. to uses of a wrapper 738193323Sed // function that dynamically resolves the calls to F via our JIT API 739193323Sed if (!F->use_empty()) { 740193323Sed // Create a new global to hold the cached function pointer. 741193323Sed Constant *NullPtr = ConstantPointerNull::get(F->getType()); 742193323Sed GlobalVariable *Cache = 743198090Srdivacky new GlobalVariable(*F->getParent(), F->getType(), 744198090Srdivacky false, GlobalValue::InternalLinkage, 745198090Srdivacky NullPtr,F->getName()+".fpcache"); 746193323Sed 747193323Sed // Construct a new stub function that will re-route calls to F 748193323Sed const FunctionType *FuncTy = F->getFunctionType(); 749193323Sed Function *FuncWrapper = Function::Create(FuncTy, 750193323Sed GlobalValue::InternalLinkage, 751193323Sed F->getName() + "_wrapper", 752193323Sed F->getParent()); 753198090Srdivacky BasicBlock *EntryBB = BasicBlock::Create(F->getContext(), 754198090Srdivacky "entry", FuncWrapper); 755198090Srdivacky BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(), 756198090Srdivacky "usecache", FuncWrapper); 757198090Srdivacky BasicBlock *LookupBB = BasicBlock::Create(F->getContext(), 758198090Srdivacky "lookupfp", FuncWrapper); 759193323Sed 760193323Sed // Check to see if we already looked up the value. 761193323Sed Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB); 762198090Srdivacky Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal, 763198090Srdivacky NullPtr, "isNull"); 764193323Sed BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB); 765193323Sed 766193323Sed // Resolve the call to function F via the JIT API: 767193323Sed // 768193323Sed // call resolver(GetElementPtr...) 769193323Sed CallInst *Resolver = 770193323Sed CallInst::Create(resolverFunc, ResolverArgs.begin(), 771193323Sed ResolverArgs.end(), "resolver", LookupBB); 772193323Sed 773193323Sed // Cast the result from the resolver to correctly-typed function. 774193323Sed CastInst *CastedResolver = 775193323Sed new BitCastInst(Resolver, 776193323Sed PointerType::getUnqual(F->getFunctionType()), 777193323Sed "resolverCast", LookupBB); 778193323Sed 779193323Sed // Save the value in our cache. 780193323Sed new StoreInst(CastedResolver, Cache, LookupBB); 781193323Sed BranchInst::Create(DoCallBB, LookupBB); 782193323Sed 783193323Sed PHINode *FuncPtr = PHINode::Create(NullPtr->getType(), 784193323Sed "fp", DoCallBB); 785193323Sed FuncPtr->addIncoming(CastedResolver, LookupBB); 786193323Sed FuncPtr->addIncoming(CachedVal, EntryBB); 787193323Sed 788193323Sed // Save the argument list. 789193323Sed std::vector<Value*> Args; 790193323Sed for (Function::arg_iterator i = FuncWrapper->arg_begin(), 791193323Sed e = FuncWrapper->arg_end(); i != e; ++i) 792193323Sed Args.push_back(i); 793193323Sed 794193323Sed // Pass on the arguments to the real function, return its result 795198090Srdivacky if (F->getReturnType() == Type::getVoidTy(F->getContext())) { 796193323Sed CallInst::Create(FuncPtr, Args.begin(), Args.end(), "", DoCallBB); 797198090Srdivacky ReturnInst::Create(F->getContext(), DoCallBB); 798193323Sed } else { 799193323Sed CallInst *Call = CallInst::Create(FuncPtr, Args.begin(), Args.end(), 800193323Sed "retval", DoCallBB); 801198090Srdivacky ReturnInst::Create(F->getContext(),Call, DoCallBB); 802193323Sed } 803193323Sed 804193323Sed // Use the wrapper function instead of the old function 805193323Sed F->replaceAllUsesWith(FuncWrapper); 806193323Sed } 807193323Sed } 808193323Sed } 809193323Sed } 810193323Sed 811193323Sed if (verifyModule(*Test) || verifyModule(*Safe)) { 812198090Srdivacky errs() << "Bugpoint has a bug, which corrupted a module!!\n"; 813193323Sed abort(); 814193323Sed } 815193323Sed} 816193323Sed 817193323Sed 818193323Sed 819193323Sed/// TestCodeGenerator - This is the predicate function used to check to see if 820193323Sed/// the "Test" portion of the program is miscompiled by the code generator under 821193323Sed/// test. If so, return true. In any case, both module arguments are deleted. 822193323Sed/// 823193323Sedstatic bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) { 824193323Sed CleanupAndPrepareModules(BD, Test, Safe); 825193323Sed 826193323Sed sys::Path TestModuleBC("bugpoint.test.bc"); 827193323Sed std::string ErrMsg; 828193323Sed if (TestModuleBC.makeUnique(true, &ErrMsg)) { 829198090Srdivacky errs() << BD.getToolName() << "Error making unique filename: " 830198090Srdivacky << ErrMsg << "\n"; 831193323Sed exit(1); 832193323Sed } 833198090Srdivacky if (BD.writeProgramToFile(TestModuleBC.str(), Test)) { 834198090Srdivacky errs() << "Error writing bitcode to `" << TestModuleBC.str() 835198090Srdivacky << "'\nExiting."; 836193323Sed exit(1); 837193323Sed } 838193323Sed delete Test; 839193323Sed 840193323Sed // Make the shared library 841193323Sed sys::Path SafeModuleBC("bugpoint.safe.bc"); 842193323Sed if (SafeModuleBC.makeUnique(true, &ErrMsg)) { 843198090Srdivacky errs() << BD.getToolName() << "Error making unique filename: " 844198090Srdivacky << ErrMsg << "\n"; 845193323Sed exit(1); 846193323Sed } 847193323Sed 848198090Srdivacky if (BD.writeProgramToFile(SafeModuleBC.str(), Safe)) { 849198090Srdivacky errs() << "Error writing bitcode to `" << SafeModuleBC.str() 850198090Srdivacky << "'\nExiting."; 851193323Sed exit(1); 852193323Sed } 853198090Srdivacky std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str()); 854193323Sed delete Safe; 855193323Sed 856193323Sed // Run the code generator on the `Test' code, loading the shared library. 857193323Sed // The function returns whether or not the new output differs from reference. 858198090Srdivacky int Result = BD.diffProgram(TestModuleBC.str(), SharedObject, false); 859193323Sed 860193323Sed if (Result) 861198090Srdivacky errs() << ": still failing!\n"; 862193323Sed else 863198090Srdivacky errs() << ": didn't fail.\n"; 864193323Sed TestModuleBC.eraseFromDisk(); 865193323Sed SafeModuleBC.eraseFromDisk(); 866193323Sed sys::Path(SharedObject).eraseFromDisk(); 867193323Sed 868193323Sed return Result; 869193323Sed} 870193323Sed 871193323Sed 872193323Sed/// debugCodeGenerator - debug errors in LLC, LLI, or CBE. 873193323Sed/// 874193323Sedbool BugDriver::debugCodeGenerator() { 875193323Sed if ((void*)SafeInterpreter == (void*)Interpreter) { 876193323Sed std::string Result = executeProgramSafely("bugpoint.safe.out"); 877198090Srdivacky outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match " 878198090Srdivacky << "the reference diff. This may be due to a\n front-end " 879198090Srdivacky << "bug or a bug in the original program, but this can also " 880198090Srdivacky << "happen if bugpoint isn't running the program with the " 881198090Srdivacky << "right flags or input.\n I left the result of executing " 882198090Srdivacky << "the program with the \"safe\" backend in this file for " 883198090Srdivacky << "you: '" 884198090Srdivacky << Result << "'.\n"; 885193323Sed return true; 886193323Sed } 887193323Sed 888193323Sed DisambiguateGlobalSymbols(Program); 889193323Sed 890193323Sed std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator); 891193323Sed 892193323Sed // Split the module into the two halves of the program we want. 893193323Sed DenseMap<const Value*, Value*> ValueMap; 894193323Sed Module *ToNotCodeGen = CloneModule(getProgram(), ValueMap); 895193323Sed Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs, ValueMap); 896193323Sed 897193323Sed // Condition the modules 898193323Sed CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen); 899193323Sed 900193323Sed sys::Path TestModuleBC("bugpoint.test.bc"); 901193323Sed std::string ErrMsg; 902193323Sed if (TestModuleBC.makeUnique(true, &ErrMsg)) { 903198090Srdivacky errs() << getToolName() << "Error making unique filename: " 904198090Srdivacky << ErrMsg << "\n"; 905193323Sed exit(1); 906193323Sed } 907193323Sed 908198090Srdivacky if (writeProgramToFile(TestModuleBC.str(), ToCodeGen)) { 909198090Srdivacky errs() << "Error writing bitcode to `" << TestModuleBC.str() 910198090Srdivacky << "'\nExiting."; 911193323Sed exit(1); 912193323Sed } 913193323Sed delete ToCodeGen; 914193323Sed 915193323Sed // Make the shared library 916193323Sed sys::Path SafeModuleBC("bugpoint.safe.bc"); 917193323Sed if (SafeModuleBC.makeUnique(true, &ErrMsg)) { 918198090Srdivacky errs() << getToolName() << "Error making unique filename: " 919198090Srdivacky << ErrMsg << "\n"; 920193323Sed exit(1); 921193323Sed } 922193323Sed 923198090Srdivacky if (writeProgramToFile(SafeModuleBC.str(), ToNotCodeGen)) { 924198090Srdivacky errs() << "Error writing bitcode to `" << SafeModuleBC.str() 925198090Srdivacky << "'\nExiting."; 926193323Sed exit(1); 927193323Sed } 928198090Srdivacky std::string SharedObject = compileSharedObject(SafeModuleBC.str()); 929193323Sed delete ToNotCodeGen; 930193323Sed 931198090Srdivacky outs() << "You can reproduce the problem with the command line: \n"; 932193323Sed if (isExecutingJIT()) { 933198090Srdivacky outs() << " lli -load " << SharedObject << " " << TestModuleBC.str(); 934193323Sed } else { 935198090Srdivacky outs() << " llc -f " << TestModuleBC.str() << " -o " << TestModuleBC.str() 936198090Srdivacky << ".s\n"; 937198090Srdivacky outs() << " gcc " << SharedObject << " " << TestModuleBC.str() 938198090Srdivacky << ".s -o " << TestModuleBC.str() << ".exe"; 939193323Sed#if defined (HAVE_LINK_R) 940198090Srdivacky outs() << " -Wl,-R."; 941193323Sed#endif 942198090Srdivacky outs() << "\n"; 943198090Srdivacky outs() << " " << TestModuleBC.str() << ".exe"; 944193323Sed } 945193323Sed for (unsigned i=0, e = InputArgv.size(); i != e; ++i) 946198090Srdivacky outs() << " " << InputArgv[i]; 947198090Srdivacky outs() << '\n'; 948198090Srdivacky outs() << "The shared object was created with:\n llc -march=c " 949198090Srdivacky << SafeModuleBC.str() << " -o temporary.c\n" 950198090Srdivacky << " gcc -xc temporary.c -O2 -o " << SharedObject; 951198090Srdivacky if (TargetTriple.getArch() == Triple::sparc) 952198090Srdivacky outs() << " -G"; // Compile a shared library, `-G' for Sparc 953198090Srdivacky else 954198090Srdivacky outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others 955193323Sed 956198090Srdivacky outs() << " -fno-strict-aliasing\n"; 957198090Srdivacky 958193323Sed return false; 959193323Sed} 960