Miscompilation.cpp revision 13793264e7cbf58e3b7b0cff3baac8e0b7a11a9d
14a10645c70199c8d8567fbc46312158c419720abChris Lattner//===- Miscompilation.cpp - Debug program miscompilations -----------------===// 23da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman// 37c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell// The LLVM Compiler Infrastructure 47c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell// 521c62da287237d39d0d95004881ea4baae3be6daChris Lattner// This file is distributed under the University of Illinois Open Source 621c62da287237d39d0d95004881ea4baae3be6daChris Lattner// License. See LICENSE.TXT for details. 73da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman// 87c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//===----------------------------------------------------------------------===// 94a10645c70199c8d8567fbc46312158c419720abChris Lattner// 10a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner// This file implements optimizer and code generation miscompilation debugging 11a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner// support. 124a10645c70199c8d8567fbc46312158c419720abChris Lattner// 134a10645c70199c8d8567fbc46312158c419720abChris Lattner//===----------------------------------------------------------------------===// 144a10645c70199c8d8567fbc46312158c419720abChris Lattner 154a10645c70199c8d8567fbc46312158c419720abChris Lattner#include "BugDriver.h" 16126840f49e8d49156a342e836d4b2adca46dc3baChris Lattner#include "ListReducer.h" 17ca7409664273fed4b473127295af3af0836b3077Daniel Dunbar#include "ToolRunner.h" 18a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/Constants.h" 19a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/DerivedTypes.h" 20a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/Instructions.h" 21605b9e2c5bd1b0c151a0b15d01e6df3aba93d52fReid Spencer#include "llvm/Linker.h" 224a10645c70199c8d8567fbc46312158c419720abChris Lattner#include "llvm/Module.h" 23e49603d79d220a795bd50684c8b1f503ee40f97fMisha Brukman#include "llvm/Pass.h" 24a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner#include "llvm/Analysis/Verifier.h" 25640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner#include "llvm/Transforms/Utils/Cloning.h" 26551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h" 27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/FileUtilities.h" 2859615f0f85e2ac99e012cb81934d002faebd405aChris Lattner#include "llvm/Config/config.h" // for HAVE_LINK_R 29fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattnerusing namespace llvm; 304a10645c70199c8d8567fbc46312158c419720abChris Lattner 31a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnernamespace llvm { 3268ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar extern cl::opt<std::string> OutputPrefix; 33a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner extern cl::list<std::string> InputArgv; 34a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner} 35a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 36efdc0b505712d1ca4460def27e51c430f033d58dChris Lattnernamespace { 37dc31a8a70cab3b4c180ac1a482855e31d3fe8e6bReid Spencer static llvm::cl::opt<bool> 38dc31a8a70cab3b4c180ac1a482855e31d3fe8e6bReid Spencer DisableLoopExtraction("disable-loop-extraction", 39dc31a8a70cab3b4c180ac1a482855e31d3fe8e6bReid Spencer cl::desc("Don't extract loops when searching for miscompilations"), 40dc31a8a70cab3b4c180ac1a482855e31d3fe8e6bReid Spencer cl::init(false)); 41265d82e4c68fa30b7ff1cb650456249f7068bdd6David Goodwin static llvm::cl::opt<bool> 42265d82e4c68fa30b7ff1cb650456249f7068bdd6David Goodwin DisableBlockExtraction("disable-block-extraction", 43265d82e4c68fa30b7ff1cb650456249f7068bdd6David Goodwin cl::desc("Don't extract blocks when searching for miscompilations"), 44265d82e4c68fa30b7ff1cb650456249f7068bdd6David Goodwin cl::init(false)); 45dc31a8a70cab3b4c180ac1a482855e31d3fe8e6bReid Spencer 468be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> { 47fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner BugDriver &BD; 48fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner public: 49fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} 503da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 518be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson virtual TestResult doTest(std::vector<const PassInfo*> &Prefix, 528be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson std::vector<const PassInfo*> &Suffix, 5322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &Error); 54fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner }; 55fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner} 56640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 578c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// TestResult - After passes have been split into a test group and a control 588c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// group, see if they still break the program. 598c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// 60640f22e66d90439857a97a83896ee68c4f7128c9Chris LattnerReduceMiscompilingPasses::TestResult 618be3291f5942e3ae4a5d66c480e7aabe2f771031Owen AndersonReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix, 628be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson std::vector<const PassInfo*> &Suffix, 6322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &Error) { 6406943add8b2b764e131979cca064eda9f28826c9Chris Lattner // First, run the program with just the Suffix passes. If it is still broken 65640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // with JUST the kept passes, discard the prefix passes. 66ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "Checking to see if '" << getPassesString(Suffix) 67ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman << "' compiles correctly: "; 68640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 698ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif std::string BitcodeResult; 708ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) { 7165f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << " Error running this sequence of passes" 7265f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << " on the input program!\n"; 735ef681c19de9c675a265211f8fb0ae49cc3a3a66Chris Lattner BD.setPassesToRun(Suffix); 74bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); 75025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner exit(BD.debugOptimizerCrash()); 76640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner } 779adc0abad3c3ed40a268ccbcee0c74cb9e1359feOwen Anderson 78640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // Check to see if the finished program matches the reference output... 7910757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola bool Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", 8010757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola true /*delete bitcode*/, &Error); 8122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 8222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return InternalError; 8322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Diff) { 84ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " nope.\n"; 8559615f0f85e2ac99e012cb81934d002faebd405aChris Lattner if (Suffix.empty()) { 8665f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << BD.getToolName() << ": I'm confused: the test fails when " 8765f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << "no passes are run, nondeterministic program?\n"; 8859615f0f85e2ac99e012cb81934d002faebd405aChris Lattner exit(1); 8959615f0f85e2ac99e012cb81934d002faebd405aChris Lattner } 90123f8fec94d1f22d876382897231868c62f8eabbMisha Brukman return KeepSuffix; // Miscompilation detected! 91640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner } 92ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " yup.\n"; // No miscompilation! 93640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 94640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner if (Prefix.empty()) return NoFailure; 95640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 9606943add8b2b764e131979cca064eda9f28826c9Chris Lattner // Next, see if the program is broken if we run the "prefix" passes first, 97bc0e998c497446f5448425b3cbd7f8f19a458764Misha Brukman // then separately run the "kept" passes. 98ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "Checking to see if '" << getPassesString(Prefix) 99ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman << "' compiles correctly: "; 100640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 101640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // If it is not broken with the kept passes, it's possible that the prefix 102640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // passes must be run before the kept passes to break it. If the program 103640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // WORKS after the prefix passes, but then fails if running the prefix AND 1048ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif // kept passes, we can update our bitcode file to include the result of the 105640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // prefix passes, then discard the prefix passes. 106640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // 1078ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif if (BD.runPasses(Prefix, BitcodeResult, false/*delete*/, true/*quiet*/)) { 10865f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << " Error running this sequence of passes" 10965f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << " on the input program!\n"; 1109c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner BD.setPassesToRun(Prefix); 111bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); 112025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner exit(BD.debugOptimizerCrash()); 113640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner } 114640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 115640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // If the prefix maintains the predicate by itself, only keep the prefix! 11610757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", false, &Error); 11722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 11822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return InternalError; 11922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Diff) { 120ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " nope.\n"; 1218ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif sys::Path(BitcodeResult).eraseFromDisk(); 122640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner return KeepPrefix; 123640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner } 124ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " yup.\n"; // No miscompilation! 125640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 126640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // Ok, so now we know that the prefix passes work, try running the suffix 127640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // passes on the result of the prefix passes. 128640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // 1296865f29fe71559a18d7f2ff0bc4f67c5fc1d000eJeffrey Yasskin OwningPtr<Module> PrefixOutput(ParseInputFile(BitcodeResult, 1306865f29fe71559a18d7f2ff0bc4f67c5fc1d000eJeffrey Yasskin BD.getContext())); 131640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner if (PrefixOutput == 0) { 13265f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << BD.getToolName() << ": Error reading bitcode file '" 13365f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << BitcodeResult << "'!\n"; 134640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner exit(1); 135640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner } 1368ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk 137f4789e6d04c1fddb40092a1193c4a5eb67387accChris Lattner 138f4789e6d04c1fddb40092a1193c4a5eb67387accChris Lattner // Don't check if there are no passes in the suffix. 139f4789e6d04c1fddb40092a1193c4a5eb67387accChris Lattner if (Suffix.empty()) 140f4789e6d04c1fddb40092a1193c4a5eb67387accChris Lattner return NoFailure; 1413da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 142ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "Checking to see if '" << getPassesString(Suffix) 143640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner << "' passes compile correctly after the '" 144640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner << getPassesString(Prefix) << "' passes: "; 145640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 1466865f29fe71559a18d7f2ff0bc4f67c5fc1d000eJeffrey Yasskin OwningPtr<Module> OriginalInput(BD.swapProgramIn(PrefixOutput.take())); 1478ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) { 14865f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << " Error running this sequence of passes" 14965f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << " on the input program!\n"; 1505ef681c19de9c675a265211f8fb0ae49cc3a3a66Chris Lattner BD.setPassesToRun(Suffix); 151bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); 152025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner exit(BD.debugOptimizerCrash()); 153640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner } 154640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 155640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // Run the result... 15610757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", 15710757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola true /*delete bitcode*/, &Error); 15822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 15922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return InternalError; 16022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Diff) { 161ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " nope.\n"; 16206943add8b2b764e131979cca064eda9f28826c9Chris Lattner return KeepSuffix; 163640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner } 164640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 165640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // Otherwise, we must not be running the bad pass anymore. 166ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " yup.\n"; // No miscompilation! 1676865f29fe71559a18d7f2ff0bc4f67c5fc1d000eJeffrey Yasskin // Restore orig program & free test. 1686865f29fe71559a18d7f2ff0bc4f67c5fc1d000eJeffrey Yasskin delete BD.swapProgramIn(OriginalInput.take()); 169640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner return NoFailure; 170640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner} 171640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 172efdc0b505712d1ca4460def27e51c430f033d58dChris Lattnernamespace { 173fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner class ReduceMiscompilingFunctions : public ListReducer<Function*> { 174fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner BugDriver &BD; 17522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool (*TestFn)(BugDriver &, Module *, Module *, std::string &); 176fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner public: 177b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner ReduceMiscompilingFunctions(BugDriver &bd, 17822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool (*F)(BugDriver &, Module *, Module *, 17922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &)) 180b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner : BD(bd), TestFn(F) {} 1813da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 182fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner virtual TestResult doTest(std::vector<Function*> &Prefix, 18322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::vector<Function*> &Suffix, 18422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &Error) { 18522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Suffix.empty()) { 18622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool Ret = TestFuncs(Suffix, Error); 18722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 18822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return InternalError; 18922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Ret) 19022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return KeepSuffix; 19122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky } 19222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Prefix.empty()) { 19322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool Ret = TestFuncs(Prefix, Error); 19422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 19522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return InternalError; 19622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Ret) 19722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return KeepPrefix; 19822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky } 199fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner return NoFailure; 200fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner } 2013da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 20284ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola bool TestFuncs(const std::vector<Function*> &Prefix, std::string &Error); 203fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner }; 204fa76183e8e28985dfd17b1d6291c939dab4cbe1dChris Lattner} 205640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 206efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// TestMergedProgram - Given two modules, link them together and run the 20713793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola/// program, checking to see if the program matches the diff. If there is 20813793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola/// an error, return NULL. If not, return the merged module. The Broken argument 20913793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola/// will be set to true if the output is different. If the DeleteInputs 21013793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola/// argument is set to true then this function deletes both input 21113793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola/// modules before it returns. 2128c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// 21313793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindolastatic Module *TestMergedProgram(const BugDriver &BD, Module *M1, Module *M2, 21413793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola bool DeleteInputs, std::string &Error, 21513793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola bool &Broken) { 216efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner // Link the two portions of the program back to together. 217efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner std::string ErrorMsg; 21890c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner if (!DeleteInputs) { 21990c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner M1 = CloneModule(M1); 22090c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner M2 = CloneModule(M2); 22190c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner } 222e4874029c37c4b14d0646289f18e5f2a1b03fdc2Reid Spencer if (Linker::LinkModules(M1, M2, &ErrorMsg)) { 22365f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << BD.getToolName() << ": Error linking modules together:" 22465f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << ErrorMsg << '\n'; 225efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner exit(1); 226efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner } 22790c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner delete M2; // We are done with this module. 228efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner 22913793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola // Execute the program. 23013793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola Broken = BD.diffProgram(M1, "", "", false, &Error); 23122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) { 23210757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola // Delete the linked module 23310757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola delete M1; 23413793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola return NULL; 23522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky } 23613793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola return M1; 237efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner} 238efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner 2398c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// TestFuncs - split functions in a Module into two groups: those that are 2408c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// under consideration for miscompilation vs. those that are not, and test 2418c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// accordingly. Each group of functions becomes a separate Module. 2428c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// 24384ae206c976c76761e307e5c45f8170d0b61015fRafael Espindolabool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs, 24484ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola std::string &Error) { 245640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // Test to see if the function is misoptimized if we ONLY run it on the 246640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // functions listed in Funcs. 247ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "Checking to see if the program is misoptimized when " 248ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman << (Funcs.size()==1 ? "this function is" : "these functions are") 249ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman << " run through the pass" 250ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":"; 251efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner PrintFunctionList(Funcs); 252ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << '\n'; 253640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 25484ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola // Create a clone for two reasons: 25584ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola // * If the optimization passes delete any function, the deleted function 25684ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola // will be in the clone and Funcs will still point to valid memory 25784ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola // * If the optimization passes use interprocedural information to break 25884ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola // a function, we want to continue with the original function. Otherwise 25984ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola // we can conclude that a function triggers the bug when in fact one 26084ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola // needs a larger set of original functions to do so. 261e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel ValueMap<const Value*, Value*> VMap; 26284ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola Module *Clone = CloneModule(BD.getProgram(), VMap); 26384ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola Module *Orig = BD.swapProgramIn(Clone); 26484ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola 26584ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola std::vector<Function*> FuncsOnClone; 26684ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { 26784ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola Function *F = cast<Function>(VMap[Funcs[i]]); 26884ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola FuncsOnClone.push_back(F); 26984ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola } 27084ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola 27184ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola // Split the module into the two halves of the program we want. 27284ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola VMap.clear(); 273e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); 27484ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, FuncsOnClone, 275e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel VMap); 276640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 2776fa98b13206583e6eb90b8304758b35548914944Nick Lewycky // Run the predicate, note that the predicate will delete both input modules. 27884ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola bool Broken = TestFn(BD, ToOptimize, ToNotOptimize, Error); 27984ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola 28084ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola delete BD.swapProgramIn(Orig); 28184ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola 28284ae206c976c76761e307e5c45f8170d0b61015fRafael Espindola return Broken; 283640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner} 284640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 2858abfb8adb2f383cab46a5e8b9fca4301effd8140Chris Lattner/// DisambiguateGlobalSymbols - Give anonymous global values names. 2868c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// 28736ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattnerstatic void DisambiguateGlobalSymbols(Module *M) { 28867ef9e43049c28c8fe2c9f70d2ad163045ee5876Chris Lattner for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 2891ffb33d033d3593ded0adb08b05eb455cce59ea8Chris Lattner I != E; ++I) 2908abfb8adb2f383cab46a5e8b9fca4301effd8140Chris Lattner if (!I->hasName()) 2918abfb8adb2f383cab46a5e8b9fca4301effd8140Chris Lattner I->setName("anon_global"); 2921ffb33d033d3593ded0adb08b05eb455cce59ea8Chris Lattner for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 2938abfb8adb2f383cab46a5e8b9fca4301effd8140Chris Lattner if (!I->hasName()) 2948abfb8adb2f383cab46a5e8b9fca4301effd8140Chris Lattner I->setName("anon_fn"); 29536ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner} 29636ee07ff9d26a2c6ebf9faf9ba90923644db29c5Chris Lattner 297a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// ExtractLoops - Given a reduced list of functions that still exposed the bug, 298a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// check to see if we can extract the loops in the region without obscuring the 299a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner/// bug. If so, it reduces the amount of code identified. 3008c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// 301b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic bool ExtractLoops(BugDriver &BD, 30222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool (*TestFn)(BugDriver &, Module *, Module *, 30322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &), 30422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::vector<Function*> &MiscompiledFunctions, 30522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &Error) { 306a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner bool MadeChange = false; 307a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner while (1) { 308aed98fa8861a28e5f7ba7c0659e106f2a441e9ffChris Lattner if (BugpointIsInterrupted) return MadeChange; 309aed98fa8861a28e5f7ba7c0659e106f2a441e9ffChris Lattner 310e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel ValueMap<const Value*, Value*> VMap; 311e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); 312a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, 313d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman MiscompiledFunctions, 314e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel VMap); 315a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize); 316a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner if (!ToOptimizeLoopExtracted) { 317a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // If the loop extractor crashed or if there were no extractible loops, 318a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // then this chapter of our odyssey is over with. 319a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner delete ToNotOptimize; 320a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner delete ToOptimize; 321a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner return MadeChange; 322a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner } 323a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner 32465f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << "Extracted a loop from the breaking portion of the program.\n"; 325a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner 326a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // Bugpoint is intentionally not very trusting of LLVM transformations. In 327a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // particular, we're not going to assume that the loop extractor works, so 328a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // we're going to test the newly loop extracted program to make sure nothing 329a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // has broken. If something broke, then we'll inform the user and stop 330a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // extraction. 33170ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman AbstractInterpreter *AI = BD.switchToSafeInterpreter(); 33213793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola bool Failure; 33313793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola Module *New = TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, 33413793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola false, Error, Failure); 33513793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola if (!New) 33622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return false; 33713793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola // Delete the original and set the new program. 33813793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola delete BD.swapProgramIn(New); 33922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Failure) { 340a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner BD.switchToInterpreter(AI); 341a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 342a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // Merged program doesn't work anymore! 34365f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << " *** ERROR: Loop extraction broke the program. :(" 34465f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << " Please report a bug!\n"; 34565f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << " Continuing on with un-loop-extracted version.\n"; 34656c418676a308034e5eecf10d3f96ced2d1fab24Chris Lattner 34768ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc", 34868ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar ToNotOptimize); 34968ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc", 35068ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar ToOptimize); 35168ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc", 35256c418676a308034e5eecf10d3f96ced2d1fab24Chris Lattner ToOptimizeLoopExtracted); 35356c418676a308034e5eecf10d3f96ced2d1fab24Chris Lattner 35468ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar errs() << "Please submit the " 35568ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar << OutputPrefix << "-loop-extract-fail-*.bc files.\n"; 35656c418676a308034e5eecf10d3f96ced2d1fab24Chris Lattner delete ToOptimize; 357a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner delete ToNotOptimize; 358a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner delete ToOptimizeLoopExtracted; 359a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner return MadeChange; 360a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner } 36156c418676a308034e5eecf10d3f96ced2d1fab24Chris Lattner delete ToOptimize; 362a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner BD.switchToInterpreter(AI); 3633da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 364ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " Testing after loop extraction:\n"; 365b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner // Clone modules, the tester function will free them. 366b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted); 367b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner Module *TNOBackup = CloneModule(ToNotOptimize); 36822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky Failure = TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize, Error); 36922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 37022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return false; 37122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Failure) { 372ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "*** Loop extraction masked the problem. Undoing.\n"; 373a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // If the program is not still broken, then loop extraction did something 374a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // that masked the error. Stop loop extraction now. 375b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner delete TOLEBackup; 376b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner delete TNOBackup; 377a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner return MadeChange; 378a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner } 379b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner ToOptimizeLoopExtracted = TOLEBackup; 380b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner ToNotOptimize = TNOBackup; 381b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner 382ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "*** Loop extraction successful!\n"; 383a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner 38490c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions; 38590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner for (Module::iterator I = ToOptimizeLoopExtracted->begin(), 38690c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner E = ToOptimizeLoopExtracted->end(); I != E; ++I) 3875cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer if (!I->isDeclaration()) 388fa1af1344910ee975f50ffdddf605c26f80ef016Chris Lattner MisCompFunctions.push_back(std::make_pair(I->getName(), 389fa1af1344910ee975f50ffdddf605c26f80ef016Chris Lattner I->getFunctionType())); 39090c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner 391a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // Okay, great! Now we know that we extracted a loop and that loop 392a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // extraction both didn't break the program, and didn't mask the problem. 393a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // Replace the current program with the loop extracted version, and try to 394a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // extract another loop. 395a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner std::string ErrorMsg; 396e4874029c37c4b14d0646289f18e5f2a1b03fdc2Reid Spencer if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)){ 39765f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << BD.getToolName() << ": Error linking modules together:" 39865f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << ErrorMsg << '\n'; 399a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner exit(1); 400a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner } 40190c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner delete ToOptimizeLoopExtracted; 402d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner 403d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner // All of the Function*'s in the MiscompiledFunctions list are in the old 4045313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner // module. Update this list to include all of the functions in the 4055313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner // optimized and loop extracted module. 4065313f23b8c3d22a2028beb731c60fc1a25beb149Chris Lattner MiscompiledFunctions.clear(); 40790c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { 408ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first); 409ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer 41090c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner assert(NewF && "Function not found??"); 411ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer assert(NewF->getFunctionType() == MisCompFunctions[i].second && 412ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer "found wrong function type?"); 41390c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner MiscompiledFunctions.push_back(NewF); 414d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner } 415d3a533d94dae1e57194001af08763eb3ba199c8fChris Lattner 416a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner BD.setNewProgram(ToNotOptimize); 417a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner MadeChange = true; 418a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner } 419a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner} 420a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner 4215e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnernamespace { 4225e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> { 4235e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner BugDriver &BD; 42422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool (*TestFn)(BugDriver &, Module *, Module *, std::string &); 4255e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner std::vector<Function*> FunctionsBeingTested; 4265e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner public: 4275e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner ReduceMiscompiledBlocks(BugDriver &bd, 42822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool (*F)(BugDriver &, Module *, Module *, 42922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &), 4305e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner const std::vector<Function*> &Fns) 4315e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {} 4323da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 4335e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner virtual TestResult doTest(std::vector<BasicBlock*> &Prefix, 43422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::vector<BasicBlock*> &Suffix, 43522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &Error) { 43622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Suffix.empty()) { 43722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool Ret = TestFuncs(Suffix, Error); 43822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 43922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return InternalError; 44022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Ret) 44122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return KeepSuffix; 44222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky } 44322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Prefix.empty()) { 44422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool Ret = TestFuncs(Prefix, Error); 44522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 44622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return InternalError; 44722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Ret) 44822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return KeepPrefix; 44922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky } 4505e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner return NoFailure; 4515e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner } 4523da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 45322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool TestFuncs(const std::vector<BasicBlock*> &BBs, std::string &Error); 4545e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner }; 4555e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner} 4565e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 4575e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// TestFuncs - Extract all blocks for the miscompiled functions except for the 4585e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// specified blocks. If the problem still exists, return true. 4595e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// 46022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewyckybool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs, 46122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &Error) { 4625e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner // Test to see if the function is misoptimized if we ONLY run it on the 4635e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner // functions listed in Funcs. 464ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "Checking to see if the program is misoptimized when all "; 46568bee938e539d884ee89ce4dfebbad777896960eChris Lattner if (!BBs.empty()) { 466ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "but these " << BBs.size() << " blocks are extracted: "; 46768bee938e539d884ee89ce4dfebbad777896960eChris Lattner for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i) 468ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << BBs[i]->getName() << " "; 469ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman if (BBs.size() > 10) outs() << "..."; 47068bee938e539d884ee89ce4dfebbad777896960eChris Lattner } else { 471ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "blocks are extracted."; 47268bee938e539d884ee89ce4dfebbad777896960eChris Lattner } 473ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << '\n'; 4745e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 4755e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner // Split the module into the two halves of the program we want. 476e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel ValueMap<const Value*, Value*> VMap; 477115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola Module *Clone = CloneModule(BD.getProgram(), VMap); 478115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola Module *Orig = BD.swapProgramIn(Clone); 479115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola std::vector<Function*> FuncsOnClone; 480115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola std::vector<BasicBlock*> BBsOnClone; 481115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola for (unsigned i = 0, e = FunctionsBeingTested.size(); i != e; ++i) { 482115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola Function *F = cast<Function>(VMap[FunctionsBeingTested[i]]); 483115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola FuncsOnClone.push_back(F); 484115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola } 485115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola for (unsigned i = 0, e = BBs.size(); i != e; ++i) { 486115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola BasicBlock *BB = cast<BasicBlock>(VMap[BBs[i]]); 487115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola BBsOnClone.push_back(BB); 488115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola } 489115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola VMap.clear(); 490115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola 491e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); 4925e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, 493115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola FuncsOnClone, 494e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel VMap); 4955e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 4965e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner // Try the extraction. If it doesn't work, then the block extractor crashed 4975e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner // or something, in which case bugpoint can't chase down this possibility. 498115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola if (Module *New = BD.ExtractMappedBlocksFromModule(BBsOnClone, ToOptimize)) { 4995e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner delete ToOptimize; 500115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola // Run the predicate, 501115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola // note that the predicate will delete both input modules. 502115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola bool Ret = TestFn(BD, New, ToNotOptimize, Error); 503115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola delete BD.swapProgramIn(Orig); 504115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola return Ret; 5055e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner } 506115a932eb9e66efd424664dbd532dbed76faa072Rafael Espindola delete BD.swapProgramIn(Orig); 5075e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner delete ToOptimize; 5085e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner delete ToNotOptimize; 5095e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner return false; 5105e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner} 5115e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 5125e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 5135e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// ExtractBlocks - Given a reduced list of functions that still expose the bug, 5145e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// extract as many basic blocks from the region as possible without obscuring 5155e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// the bug. 5165e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// 5175e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnerstatic bool ExtractBlocks(BugDriver &BD, 51822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool (*TestFn)(BugDriver &, Module *, Module *, 51922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &), 52022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::vector<Function*> &MiscompiledFunctions, 52122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &Error) { 522f9aaae06cd2109082cda2b09ef3f23e0e1cff47bChris Lattner if (BugpointIsInterrupted) return false; 523f9aaae06cd2109082cda2b09ef3f23e0e1cff47bChris Lattner 5245e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner std::vector<BasicBlock*> Blocks; 5255e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) 5265e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner for (Function::iterator I = MiscompiledFunctions[i]->begin(), 5275e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner E = MiscompiledFunctions[i]->end(); I != E; ++I) 5285e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner Blocks.push_back(I); 5295e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 5305e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner // Use the list reducer to identify blocks that can be extracted without 5315e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner // obscuring the bug. The Blocks list will end up containing blocks that must 5325e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner // be retained from the original program. 5335e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner unsigned OldSize = Blocks.size(); 53468bee938e539d884ee89ce4dfebbad777896960eChris Lattner 53568bee938e539d884ee89ce4dfebbad777896960eChris Lattner // Check to see if all blocks are extractible first. 53622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions) 53722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky .TestFuncs(std::vector<BasicBlock*>(), Error); 53822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 53922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return false; 54022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Ret) { 54168bee938e539d884ee89ce4dfebbad777896960eChris Lattner Blocks.clear(); 54268bee938e539d884ee89ce4dfebbad777896960eChris Lattner } else { 54322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky ReduceMiscompiledBlocks(BD, TestFn, 54422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky MiscompiledFunctions).reduceList(Blocks, Error); 54522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 54622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return false; 54768bee938e539d884ee89ce4dfebbad777896960eChris Lattner if (Blocks.size() == OldSize) 54868bee938e539d884ee89ce4dfebbad777896960eChris Lattner return false; 54968bee938e539d884ee89ce4dfebbad777896960eChris Lattner } 5505e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 551e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel ValueMap<const Value*, Value*> VMap; 552e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel Module *ProgClone = CloneModule(BD.getProgram(), VMap); 5532290e754061f1393bb96b1808ac33dc03399c939Chris Lattner Module *ToExtract = SplitFunctionsOutOfModule(ProgClone, 554d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman MiscompiledFunctions, 555e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel VMap); 5562290e754061f1393bb96b1808ac33dc03399c939Chris Lattner Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract); 5572290e754061f1393bb96b1808ac33dc03399c939Chris Lattner if (Extracted == 0) { 558da895d63377b421dc50117befb2bec80d2973526Chris Lattner // Weird, extraction should have worked. 55965f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << "Nondeterministic problem extracting blocks??\n"; 5602290e754061f1393bb96b1808ac33dc03399c939Chris Lattner delete ProgClone; 5612290e754061f1393bb96b1808ac33dc03399c939Chris Lattner delete ToExtract; 5622290e754061f1393bb96b1808ac33dc03399c939Chris Lattner return false; 5632290e754061f1393bb96b1808ac33dc03399c939Chris Lattner } 5645e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 5652290e754061f1393bb96b1808ac33dc03399c939Chris Lattner // Otherwise, block extraction succeeded. Link the two program fragments back 5662290e754061f1393bb96b1808ac33dc03399c939Chris Lattner // together. 5672290e754061f1393bb96b1808ac33dc03399c939Chris Lattner delete ToExtract; 5685e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 56990c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions; 57090c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner for (Module::iterator I = Extracted->begin(), E = Extracted->end(); 57190c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner I != E; ++I) 5725cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer if (!I->isDeclaration()) 573fa1af1344910ee975f50ffdddf605c26f80ef016Chris Lattner MisCompFunctions.push_back(std::make_pair(I->getName(), 574fa1af1344910ee975f50ffdddf605c26f80ef016Chris Lattner I->getFunctionType())); 57590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner 5762290e754061f1393bb96b1808ac33dc03399c939Chris Lattner std::string ErrorMsg; 577e4874029c37c4b14d0646289f18e5f2a1b03fdc2Reid Spencer if (Linker::LinkModules(ProgClone, Extracted, &ErrorMsg)) { 57865f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << BD.getToolName() << ": Error linking modules together:" 57965f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << ErrorMsg << '\n'; 5802290e754061f1393bb96b1808ac33dc03399c939Chris Lattner exit(1); 5812290e754061f1393bb96b1808ac33dc03399c939Chris Lattner } 58290c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner delete Extracted; 5835e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 5842290e754061f1393bb96b1808ac33dc03399c939Chris Lattner // Set the new program and delete the old one. 5852290e754061f1393bb96b1808ac33dc03399c939Chris Lattner BD.setNewProgram(ProgClone); 5865e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 5872290e754061f1393bb96b1808ac33dc03399c939Chris Lattner // Update the list of miscompiled functions. 5882290e754061f1393bb96b1808ac33dc03399c939Chris Lattner MiscompiledFunctions.clear(); 5895e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 59090c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { 591ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first); 59290c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner assert(NewF && "Function not found??"); 593ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer assert(NewF->getFunctionType() == MisCompFunctions[i].second && 594ef9b9a793949469cdaa4ab6d0173136229dcab7bReid Spencer "Function has wrong type??"); 59590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner MiscompiledFunctions.push_back(NewF); 59690c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner } 5972290e754061f1393bb96b1808ac33dc03399c939Chris Lattner 5982290e754061f1393bb96b1808ac33dc03399c939Chris Lattner return true; 5995e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner} 6005e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 6015e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 602b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// DebugAMiscompilation - This is a generic driver to narrow down 603b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// miscompilations, either in an optimization or a code generator. 6048c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// 605b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattnerstatic std::vector<Function*> 606b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris LattnerDebugAMiscompilation(BugDriver &BD, 60722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool (*TestFn)(BugDriver &, Module *, Module *, 60822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &), 60922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &Error) { 610640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // Okay, now that we have reduced the list of passes which are causing the 611640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // failure, see if we can pin down which functions are being 612640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // miscompiled... first build a list of all of the non-external functions in 613640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // the program. 614640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner std::vector<Function*> MiscompiledFunctions; 615b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner Module *Prog = BD.getProgram(); 616b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I) 6175cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer if (!I->isDeclaration()) 618640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner MiscompiledFunctions.push_back(I); 6194a10645c70199c8d8567fbc46312158c419720abChris Lattner 620640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner // Do the reduction... 621f9aaae06cd2109082cda2b09ef3f23e0e1cff47bChris Lattner if (!BugpointIsInterrupted) 62222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, 62322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky Error); 62422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 62522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return MiscompiledFunctions; 626640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner 627ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "\n*** The following function" 628ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman << (MiscompiledFunctions.size() == 1 ? " is" : "s are") 629ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman << " being miscompiled: "; 630640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner PrintFunctionList(MiscompiledFunctions); 631ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << '\n'; 6324a10645c70199c8d8567fbc46312158c419720abChris Lattner 633a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // See if we can rip any loops out of the miscompiled functions and still 634a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner // trigger the problem. 635dc31a8a70cab3b4c180ac1a482855e31d3fe8e6bReid Spencer 63622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!BugpointIsInterrupted && !DisableLoopExtraction) { 63722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions, Error); 63822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 63922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return MiscompiledFunctions; 64022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Ret) { 64122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky // Okay, we extracted some loops and the problem still appears. See if 64222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky // we can eliminate some of the created functions from being candidates. 64322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky DisambiguateGlobalSymbols(BD.getProgram()); 64422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky 64522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky // Do the reduction... 64622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!BugpointIsInterrupted) 64722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, 64822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky Error); 64922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 65022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return MiscompiledFunctions; 65122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky 65222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky outs() << "\n*** The following function" 65322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << (MiscompiledFunctions.size() == 1 ? " is" : "s are") 65422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << " being miscompiled: "; 65522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky PrintFunctionList(MiscompiledFunctions); 65622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky outs() << '\n'; 65722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky } 6585e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner } 6595e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 66022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!BugpointIsInterrupted && !DisableBlockExtraction) { 66122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky bool Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions, Error); 66222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 66322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return MiscompiledFunctions; 66422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Ret) { 66522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky // Okay, we extracted some blocks and the problem still appears. See if 66622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky // we can eliminate some of the created functions from being candidates. 66722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky DisambiguateGlobalSymbols(BD.getProgram()); 66822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky 66922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky // Do the reduction... 67022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, 67122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky Error); 67222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 67322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return MiscompiledFunctions; 67422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky 67522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky outs() << "\n*** The following function" 67622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << (MiscompiledFunctions.size() == 1 ? " is" : "s are") 67722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << " being miscompiled: "; 67822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky PrintFunctionList(MiscompiledFunctions); 67922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky outs() << '\n'; 68022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky } 681a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner } 682a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner 683b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner return MiscompiledFunctions; 684b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner} 685b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner 686a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// TestOptimizer - This is the predicate function used to check to see if the 687a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// "Test" portion of the program is misoptimized. If so, return true. In any 688a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// case, both module arguments are deleted. 6898c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// 69022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewyckystatic bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe, 69122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &Error) { 692b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner // Run the optimization passes on ToOptimize, producing a transformed version 693b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner // of the functions being tested. 694ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " Optimizing functions being tested: "; 695b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(), 696b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner /*AutoDebugCrashes*/true); 697ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "done.\n"; 698b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner delete Test; 699b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner 700ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " Checking to see if the merged program executes correctly: "; 70113793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola bool Broken; 70213793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola Module *New = TestMergedProgram(BD, Optimized, Safe, true, Error, Broken); 70313793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola if (New) { 70413793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola outs() << (Broken ? " nope.\n" : " yup.\n"); 70513793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola // Delete the original and set the new program. 70613793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola delete BD.swapProgramIn(New); 70713793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola } 708b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner return Broken; 709b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner} 710b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner 711b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner 712b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// debugMiscompilation - This method is used when the passes selected are not 713b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// crashing, but the generated output is semantically different from the 714b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// input. 715b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner/// 71622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewyckyvoid BugDriver::debugMiscompilation(std::string *Error) { 717b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner // Make sure something was miscompiled... 718f9aaae06cd2109082cda2b09ef3f23e0e1cff47bChris Lattner if (!BugpointIsInterrupted) 71922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun, *Error)) { 72022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Error->empty()) 72122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky errs() << "*** Optimized program matches reference output! No problem" 72222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << " detected...\nbugpoint can't help you with your problem!\n"; 72322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return; 724f9aaae06cd2109082cda2b09ef3f23e0e1cff47bChris Lattner } 725b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner 726ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "\n*** Found miscompiling pass" 727ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman << (getPassesToRun().size() == 1 ? "" : "es") << ": " 728ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman << getPassesString(getPassesToRun()) << '\n'; 729bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola EmitProgressBitcode(Program, "passinput"); 730b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner 73122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::vector<Function *> MiscompiledFunctions = 73222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky DebugAMiscompilation(*this, TestOptimizer, *Error); 73322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error->empty()) 73422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return; 735b15825b0a29e527b361b63a6e41aff5fdb8fdd5aChris Lattner 7368ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif // Output a bunch of bitcode files for the user... 737ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "Outputting reduced bitcode files which expose the problem:\n"; 738e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel ValueMap<const Value*, Value*> VMap; 739e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel Module *ToNotOptimize = CloneModule(getProgram(), VMap); 740efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, 741d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman MiscompiledFunctions, 742e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel VMap); 743be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner 744ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " Non-optimized portion: "; 745bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola EmitProgressBitcode(ToNotOptimize, "tonotoptimize", true); 746bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola delete ToNotOptimize; // Delete hacked module. 7473da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 748ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " Portion that is input to optimizer: "; 749bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola EmitProgressBitcode(ToOptimize, "tooptimize"); 750bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola delete ToOptimize; // Delete hacked module. 7514a10645c70199c8d8567fbc46312158c419720abChris Lattner 75222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return; 7534a10645c70199c8d8567fbc46312158c419720abChris Lattner} 754d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 755a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// CleanupAndPrepareModules - Get the specified modules ready for code 756a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// generator testing. 7578c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// 758a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattnerstatic void CleanupAndPrepareModules(BugDriver &BD, Module *&Test, 759a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner Module *Safe) { 760a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Clean up the modules, removing extra cruft that we don't need anymore... 761a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner Test = BD.performFinalCleanups(Test); 762a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 763a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // If we are executing the JIT, we have several nasty issues to take care of. 764a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner if (!BD.isExecutingJIT()) return; 765a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 766a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // First, if the main function is in the Safe module, we must add a stub to 767a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // the Test module to call into it. Thus, we create a new function `main' 768a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // which just calls the old one. 769688b0490e22eb67623f5aaa24406209be74efcb2Reid Spencer if (Function *oldMain = Safe->getFunction("main")) 7705cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer if (!oldMain->isDeclaration()) { 771a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Rename it 772a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner oldMain->setName("llvm_bugpoint_old_main"); 773a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Create a NEW `main' function with same type in the test module. 774051a950000e21935165db56695e35bade668193bGabor Greif Function *newMain = Function::Create(oldMain->getFunctionType(), 775051a950000e21935165db56695e35bade668193bGabor Greif GlobalValue::ExternalLinkage, 776051a950000e21935165db56695e35bade668193bGabor Greif "main", Test); 777a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Create an `oldmain' prototype in the test module, which will 778a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // corresponds to the real main function in the same module. 779051a950000e21935165db56695e35bade668193bGabor Greif Function *oldMainProto = Function::Create(oldMain->getFunctionType(), 780051a950000e21935165db56695e35bade668193bGabor Greif GlobalValue::ExternalLinkage, 781051a950000e21935165db56695e35bade668193bGabor Greif oldMain->getName(), Test); 782a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Set up and remember the argument list for the main function. 783a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner std::vector<Value*> args; 7845a1c58d0094ff16dcd103f3752046d426ad5dd2cAlkis Evlogimenos for (Function::arg_iterator 7855a1c58d0094ff16dcd103f3752046d426ad5dd2cAlkis Evlogimenos I = newMain->arg_begin(), E = newMain->arg_end(), 7865a1c58d0094ff16dcd103f3752046d426ad5dd2cAlkis Evlogimenos OI = oldMain->arg_begin(); I != E; ++I, ++OI) { 7876bc41e8a74d1756da0003641bfebd02a3d6d9586Owen Anderson I->setName(OI->getName()); // Copy argument names from oldMain 788a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner args.push_back(I); 789a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 790a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 791a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Call the old main function and return its result 7921d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain); 793051a950000e21935165db56695e35bade668193bGabor Greif CallInst *call = CallInst::Create(oldMainProto, args.begin(), args.end(), 794051a950000e21935165db56695e35bade668193bGabor Greif "", BB); 7953da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 796a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // If the type of old function wasn't void, return value of call 7971d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson ReturnInst::Create(Safe->getContext(), call, BB); 798a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 799a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 800a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // The second nasty issue we must deal with in the JIT is that the Safe 801a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // module cannot directly reference any functions defined in the test 802a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // module. Instead, we use a JIT API call to dynamically resolve the 803a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // symbol. 8043da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 805a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Add the resolver to the Safe module. 806a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Prototype: void *getPointerToNamedFunction(const char* Name) 8072db43c864e8372823d961d961ca520ed20edca82Chris Lattner Constant *resolverFunc = 808a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner Safe->getOrInsertFunction("getPointerToNamedFunction", 809ac53a0b272452013124bfc70480aea5e41b60f40Duncan Sands Type::getInt8PtrTy(Safe->getContext()), 810ac53a0b272452013124bfc70480aea5e41b60f40Duncan Sands Type::getInt8PtrTy(Safe->getContext()), 8111d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson (Type *)0); 8123da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 813a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Use the function we just added to get addresses of functions we need. 814dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) { 8155cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc && 816a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands !F->isIntrinsic() /* ignore intrinsics */) { 817688b0490e22eb67623f5aaa24406209be74efcb2Reid Spencer Function *TestFn = Test->getFunction(F->getName()); 818a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 819a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Don't forward functions which are external in the test module too. 8205cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer if (TestFn && !TestFn->isDeclaration()) { 821a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // 1. Add a string constant with its name to the global file 8221d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson Constant *InitArray = ConstantArray::get(F->getContext(), F->getName()); 823a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner GlobalVariable *funcName = 824e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/, 8253da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman GlobalValue::InternalLinkage, InitArray, 826e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson F->getName() + "_name"); 827a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 828a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an 829a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // sbyte* so it matches the signature of the resolver function. 830a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 831a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // GetElementPtr *funcName, ulong 0, ulong 0 8321d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson std::vector<Constant*> GEPargs(2, 8331d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson Constant::getNullValue(Type::getInt32Ty(F->getContext()))); 8349adc0abad3c3ed40a268ccbcee0c74cb9e1359feOwen Anderson Value *GEP = 835baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2); 836a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner std::vector<Value*> ResolverArgs; 837a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner ResolverArgs.push_back(GEP); 838a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 839de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman // Rewrite uses of F in global initializers, etc. to uses of a wrapper 840de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman // function that dynamically resolves the calls to F via our JIT API 841a3efca16f2688981672deeb718909cf6acbe474eChris Lattner if (!F->use_empty()) { 842a3efca16f2688981672deeb718909cf6acbe474eChris Lattner // Create a new global to hold the cached function pointer. 8439e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson Constant *NullPtr = ConstantPointerNull::get(F->getType()); 844a3efca16f2688981672deeb718909cf6acbe474eChris Lattner GlobalVariable *Cache = 845e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson new GlobalVariable(*F->getParent(), F->getType(), 846e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson false, GlobalValue::InternalLinkage, 847e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson NullPtr,F->getName()+".fpcache"); 84800b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 849de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman // Construct a new stub function that will re-route calls to F 850dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman const FunctionType *FuncTy = F->getFunctionType(); 851051a950000e21935165db56695e35bade668193bGabor Greif Function *FuncWrapper = Function::Create(FuncTy, 852051a950000e21935165db56695e35bade668193bGabor Greif GlobalValue::InternalLinkage, 853051a950000e21935165db56695e35bade668193bGabor Greif F->getName() + "_wrapper", 854051a950000e21935165db56695e35bade668193bGabor Greif F->getParent()); 8551d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson BasicBlock *EntryBB = BasicBlock::Create(F->getContext(), 8561d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson "entry", FuncWrapper); 8571d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(), 8581d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson "usecache", FuncWrapper); 8591d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson BasicBlock *LookupBB = BasicBlock::Create(F->getContext(), 8601d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson "lookupfp", FuncWrapper); 86100b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 862a3efca16f2688981672deeb718909cf6acbe474eChris Lattner // Check to see if we already looked up the value. 863a3efca16f2688981672deeb718909cf6acbe474eChris Lattner Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB); 864333c40096561218bc3597cf153c0a3895274414cOwen Anderson Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal, 865333c40096561218bc3597cf153c0a3895274414cOwen Anderson NullPtr, "isNull"); 866051a950000e21935165db56695e35bade668193bGabor Greif BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB); 86700b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 868de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman // Resolve the call to function F via the JIT API: 869de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman // 870de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman // call resolver(GetElementPtr...) 871b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif CallInst *Resolver = 872b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif CallInst::Create(resolverFunc, ResolverArgs.begin(), 873b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif ResolverArgs.end(), "resolver", LookupBB); 874b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif 875b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif // Cast the result from the resolver to correctly-typed function. 876b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif CastInst *CastedResolver = 877b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif new BitCastInst(Resolver, 878debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson PointerType::getUnqual(F->getFunctionType()), 879b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif "resolverCast", LookupBB); 8803da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer 881a3efca16f2688981672deeb718909cf6acbe474eChris Lattner // Save the value in our cache. 882a3efca16f2688981672deeb718909cf6acbe474eChris Lattner new StoreInst(CastedResolver, Cache, LookupBB); 883051a950000e21935165db56695e35bade668193bGabor Greif BranchInst::Create(DoCallBB, LookupBB); 88400b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 885b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif PHINode *FuncPtr = PHINode::Create(NullPtr->getType(), 886b1dbcd886a4b5597a839f299054b78b33fb2d6dfGabor Greif "fp", DoCallBB); 887a3efca16f2688981672deeb718909cf6acbe474eChris Lattner FuncPtr->addIncoming(CastedResolver, LookupBB); 888a3efca16f2688981672deeb718909cf6acbe474eChris Lattner FuncPtr->addIncoming(CachedVal, EntryBB); 88900b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 890a3efca16f2688981672deeb718909cf6acbe474eChris Lattner // Save the argument list. 891dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman std::vector<Value*> Args; 8925a1c58d0094ff16dcd103f3752046d426ad5dd2cAlkis Evlogimenos for (Function::arg_iterator i = FuncWrapper->arg_begin(), 8935a1c58d0094ff16dcd103f3752046d426ad5dd2cAlkis Evlogimenos e = FuncWrapper->arg_end(); i != e; ++i) 894dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman Args.push_back(i); 895dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman 896dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman // Pass on the arguments to the real function, return its result 897e49a13e7260b83ce56d01446f2a165cc9f35da7fDan Gohman if (F->getReturnType()->isVoidTy()) { 898051a950000e21935165db56695e35bade668193bGabor Greif CallInst::Create(FuncPtr, Args.begin(), Args.end(), "", DoCallBB); 8991d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson ReturnInst::Create(F->getContext(), DoCallBB); 900dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman } else { 901051a950000e21935165db56695e35bade668193bGabor Greif CallInst *Call = CallInst::Create(FuncPtr, Args.begin(), Args.end(), 902051a950000e21935165db56695e35bade668193bGabor Greif "retval", DoCallBB); 9031d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson ReturnInst::Create(F->getContext(),Call, DoCallBB); 904dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman } 90500b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen 906de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman // Use the wrapper function instead of the old function 907de4803d0af6824a2d5da41fa09b512084c73ce34Misha Brukman F->replaceAllUsesWith(FuncWrapper); 908dc7fef83dcab053f86119d00478e6b008166fcf5Misha Brukman } 909a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 910a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 911a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 912a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 913a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner if (verifyModule(*Test) || verifyModule(*Safe)) { 91465f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << "Bugpoint has a bug, which corrupted a module!!\n"; 915a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner abort(); 916a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 917a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner} 918a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 919a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 920a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 921a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// TestCodeGenerator - This is the predicate function used to check to see if 922a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// the "Test" portion of the program is miscompiled by the code generator under 923a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner/// test. If so, return true. In any case, both module arguments are deleted. 9248c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// 92522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewyckystatic bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe, 92622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string &Error) { 927a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner CleanupAndPrepareModules(BD, Test, Safe); 928a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 92997182985d530dbef488696c95a39c14fe56c995bReid Spencer sys::Path TestModuleBC("bugpoint.test.bc"); 93051c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer std::string ErrMsg; 93151c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer if (TestModuleBC.makeUnique(true, &ErrMsg)) { 93265f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << BD.getToolName() << "Error making unique filename: " 93365f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << ErrMsg << "\n"; 93451c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer exit(1); 93551c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer } 93674382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner if (BD.writeProgramToFile(TestModuleBC.str(), Test)) { 93774382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner errs() << "Error writing bitcode to `" << TestModuleBC.str() 93874382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner << "'\nExiting."; 939a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner exit(1); 940a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 941a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner delete Test; 942a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 943bc2ed599e877b9d76bd548546019f98ae256fe9bRafael Espindola FileRemover TestModuleBCRemover(TestModuleBC, !SaveTemps); 944bc2ed599e877b9d76bd548546019f98ae256fe9bRafael Espindola 945a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Make the shared library 94697182985d530dbef488696c95a39c14fe56c995bReid Spencer sys::Path SafeModuleBC("bugpoint.safe.bc"); 94751c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer if (SafeModuleBC.makeUnique(true, &ErrMsg)) { 94865f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << BD.getToolName() << "Error making unique filename: " 94965f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << ErrMsg << "\n"; 95051c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer exit(1); 95151c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer } 952a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 95374382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner if (BD.writeProgramToFile(SafeModuleBC.str(), Safe)) { 95474382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner errs() << "Error writing bitcode to `" << SafeModuleBC.str() 95574382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner << "'\nExiting."; 956a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner exit(1); 957a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 958bc2ed599e877b9d76bd548546019f98ae256fe9bRafael Espindola 959bc2ed599e877b9d76bd548546019f98ae256fe9bRafael Espindola FileRemover SafeModuleBCRemover(SafeModuleBC, !SaveTemps); 960bc2ed599e877b9d76bd548546019f98ae256fe9bRafael Espindola 96122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str(), Error); 96222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 9632706387d37b30fc191c5b74987dc139e1835c52dBenjamin Kramer return false; 964a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner delete Safe; 965a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 966bc2ed599e877b9d76bd548546019f98ae256fe9bRafael Espindola FileRemover SharedObjectRemover(sys::Path(SharedObject), !SaveTemps); 967bc2ed599e877b9d76bd548546019f98ae256fe9bRafael Espindola 968a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Run the code generator on the `Test' code, loading the shared library. 969a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // The function returns whether or not the new output differs from reference. 97010757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola bool Result = BD.diffProgram(BD.getProgram(), TestModuleBC.str(), 97110757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola SharedObject, false, &Error); 97222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error.empty()) 97322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return false; 974a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 975a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner if (Result) 97665f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << ": still failing!\n"; 977a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner else 97865f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << ": didn't fail.\n"; 979a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 980a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner return Result; 981a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner} 982a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 983a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 9848c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// debugCodeGenerator - debug errors in LLC, LLI, or CBE. 9858c194eaa0577a207bb1ea91bf2c2a5e664fce9eeMisha Brukman/// 98622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewyckybool BugDriver::debugCodeGenerator(std::string *Error) { 98770ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman if ((void*)SafeInterpreter == (void*)Interpreter) { 98810757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola std::string Result = executeProgramSafely(Program, "bugpoint.safe.out", 98910757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola Error); 99022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (Error->empty()) { 99122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match " 99222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << "the reference diff. This may be due to a\n front-end " 99322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << "bug or a bug in the original program, but this can also " 99422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << "happen if bugpoint isn't running the program with the " 99522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << "right flags or input.\n I left the result of executing " 99622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << "the program with the \"safe\" backend in this file for " 99722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << "you: '" 99822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky << Result << "'.\n"; 99922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky } 1000a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner return true; 1001a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 1002a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 1003a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner DisambiguateGlobalSymbols(Program); 1004a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 100522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator, 100622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky *Error); 100722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error->empty()) 100822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return true; 1009a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 1010a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Split the module into the two halves of the program we want. 1011e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel ValueMap<const Value*, Value*> VMap; 1012e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel Module *ToNotCodeGen = CloneModule(getProgram(), VMap); 1013e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs, VMap); 1014a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 1015a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Condition the modules 1016a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen); 1017a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 101897182985d530dbef488696c95a39c14fe56c995bReid Spencer sys::Path TestModuleBC("bugpoint.test.bc"); 101951c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer std::string ErrMsg; 102051c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer if (TestModuleBC.makeUnique(true, &ErrMsg)) { 102165f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << getToolName() << "Error making unique filename: " 102265f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << ErrMsg << "\n"; 102351c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer exit(1); 102451c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer } 102597182985d530dbef488696c95a39c14fe56c995bReid Spencer 102674382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner if (writeProgramToFile(TestModuleBC.str(), ToCodeGen)) { 102774382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner errs() << "Error writing bitcode to `" << TestModuleBC.str() 102874382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner << "'\nExiting."; 1029a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner exit(1); 1030a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 1031a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner delete ToCodeGen; 1032a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 1033a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner // Make the shared library 103497182985d530dbef488696c95a39c14fe56c995bReid Spencer sys::Path SafeModuleBC("bugpoint.safe.bc"); 103551c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer if (SafeModuleBC.makeUnique(true, &ErrMsg)) { 103665f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << getToolName() << "Error making unique filename: " 103765f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << ErrMsg << "\n"; 103851c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer exit(1); 103951c5a286bae5ad27ddc49602f44b7ea7253a4cc9Reid Spencer } 104097182985d530dbef488696c95a39c14fe56c995bReid Spencer 104174382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner if (writeProgramToFile(SafeModuleBC.str(), ToNotCodeGen)) { 104274382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner errs() << "Error writing bitcode to `" << SafeModuleBC.str() 104374382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner << "'\nExiting."; 1044a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner exit(1); 1045a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 104622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky std::string SharedObject = compileSharedObject(SafeModuleBC.str(), *Error); 104722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky if (!Error->empty()) 104822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky return true; 1049a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner delete ToNotCodeGen; 1050a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 1051ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "You can reproduce the problem with the command line: \n"; 1052a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner if (isExecutingJIT()) { 105374382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner outs() << " lli -load " << SharedObject << " " << TestModuleBC.str(); 1054a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } else { 10558d0e1bcc921c942e979b0925051c663590bd618fNick Lewycky outs() << " llc " << TestModuleBC.str() << " -o " << TestModuleBC.str() 105674382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner << ".s\n"; 105774382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner outs() << " gcc " << SharedObject << " " << TestModuleBC.str() 105874382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner << ".s -o " << TestModuleBC.str() << ".exe"; 105959615f0f85e2ac99e012cb81934d002faebd405aChris Lattner#if defined (HAVE_LINK_R) 1060ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " -Wl,-R."; 106159615f0f85e2ac99e012cb81934d002faebd405aChris Lattner#endif 1062ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "\n"; 106374382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner outs() << " " << TestModuleBC.str() << ".exe"; 1064a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner } 106522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky for (unsigned i = 0, e = InputArgv.size(); i != e; ++i) 1066ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << " " << InputArgv[i]; 1067ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << '\n'; 1068ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "The shared object was created with:\n llc -march=c " 106974382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner << SafeModuleBC.str() << " -o temporary.c\n" 1070ca7409664273fed4b473127295af3af0836b3077Daniel Dunbar << " gcc -xc temporary.c -O2 -o " << SharedObject; 1071ca7409664273fed4b473127295af3af0836b3077Daniel Dunbar if (TargetTriple.getArch() == Triple::sparc) 1072ca7409664273fed4b473127295af3af0836b3077Daniel Dunbar outs() << " -G"; // Compile a shared library, `-G' for Sparc 1073ca7409664273fed4b473127295af3af0836b3077Daniel Dunbar else 1074ca7409664273fed4b473127295af3af0836b3077Daniel Dunbar outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others 1075ca7409664273fed4b473127295af3af0836b3077Daniel Dunbar 1076ca7409664273fed4b473127295af3af0836b3077Daniel Dunbar outs() << " -fno-strict-aliasing\n"; 1077a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner 1078a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner return false; 1079a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner} 1080