Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 1 | //===- Miscompilation.cpp - Debug program miscompilations -----------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 10 | // This file implements optimizer and code generation miscompilation debugging |
| 11 | // support. |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "BugDriver.h" |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 16 | #include "ListReducer.h" |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 17 | #include "ToolRunner.h" |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Instructions.h" |
Reid Spencer | 605b9e2 | 2004-11-14 23:00:08 +0000 | [diff] [blame] | 21 | #include "llvm/Linker.h" |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 23 | #include "llvm/Pass.h" |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/Cloning.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | 59615f0 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 28 | #include "llvm/Config/config.h" // for HAVE_LINK_R |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 30 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 31 | namespace llvm { |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 32 | extern cl::opt<std::string> OutputPrefix; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 33 | extern cl::list<std::string> InputArgv; |
| 34 | } |
| 35 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 36 | namespace { |
Reid Spencer | dc31a8a | 2006-11-11 19:05:02 +0000 | [diff] [blame] | 37 | static llvm::cl::opt<bool> |
| 38 | DisableLoopExtraction("disable-loop-extraction", |
| 39 | cl::desc("Don't extract loops when searching for miscompilations"), |
| 40 | cl::init(false)); |
David Goodwin | 265d82e | 2009-07-28 23:08:36 +0000 | [diff] [blame] | 41 | static llvm::cl::opt<bool> |
| 42 | DisableBlockExtraction("disable-block-extraction", |
| 43 | cl::desc("Don't extract blocks when searching for miscompilations"), |
| 44 | cl::init(false)); |
Reid Spencer | dc31a8a | 2006-11-11 19:05:02 +0000 | [diff] [blame] | 45 | |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 46 | class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> { |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 47 | BugDriver &BD; |
| 48 | public: |
| 49 | ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 50 | |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 51 | virtual TestResult doTest(std::vector<const PassInfo*> &Prefix, |
| 52 | std::vector<const PassInfo*> &Suffix, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 53 | std::string &Error); |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 54 | }; |
| 55 | } |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 56 | |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 57 | /// TestResult - After passes have been split into a test group and a control |
| 58 | /// group, see if they still break the program. |
| 59 | /// |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 60 | ReduceMiscompilingPasses::TestResult |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 61 | ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix, |
| 62 | std::vector<const PassInfo*> &Suffix, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 63 | std::string &Error) { |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 64 | // First, run the program with just the Suffix passes. If it is still broken |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 65 | // with JUST the kept passes, discard the prefix passes. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 66 | outs() << "Checking to see if '" << getPassesString(Suffix) |
| 67 | << "' compiles correctly: "; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 68 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 69 | std::string BitcodeResult; |
| 70 | if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 71 | errs() << " Error running this sequence of passes" |
| 72 | << " on the input program!\n"; |
Chris Lattner | 5ef681c | 2003-10-17 23:07:47 +0000 | [diff] [blame] | 73 | BD.setPassesToRun(Suffix); |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 74 | BD.EmitProgressBitcode("pass-error", false); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 75 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 76 | } |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 78 | // Check to see if the finished program matches the reference output... |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 79 | bool Diff = BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/, |
| 80 | &Error); |
| 81 | if (!Error.empty()) |
| 82 | return InternalError; |
| 83 | if (Diff) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 84 | outs() << " nope.\n"; |
Chris Lattner | 59615f0 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 85 | if (Suffix.empty()) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 86 | errs() << BD.getToolName() << ": I'm confused: the test fails when " |
| 87 | << "no passes are run, nondeterministic program?\n"; |
Chris Lattner | 59615f0 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 88 | exit(1); |
| 89 | } |
Misha Brukman | 123f8fe | 2004-04-22 20:02:09 +0000 | [diff] [blame] | 90 | return KeepSuffix; // Miscompilation detected! |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 91 | } |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 92 | outs() << " yup.\n"; // No miscompilation! |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 93 | |
| 94 | if (Prefix.empty()) return NoFailure; |
| 95 | |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 96 | // Next, see if the program is broken if we run the "prefix" passes first, |
Misha Brukman | bc0e998 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 97 | // then separately run the "kept" passes. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 98 | outs() << "Checking to see if '" << getPassesString(Prefix) |
| 99 | << "' compiles correctly: "; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 100 | |
| 101 | // If it is not broken with the kept passes, it's possible that the prefix |
| 102 | // passes must be run before the kept passes to break it. If the program |
| 103 | // WORKS after the prefix passes, but then fails if running the prefix AND |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 104 | // kept passes, we can update our bitcode file to include the result of the |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 105 | // prefix passes, then discard the prefix passes. |
| 106 | // |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 107 | if (BD.runPasses(Prefix, BitcodeResult, false/*delete*/, true/*quiet*/)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 108 | errs() << " Error running this sequence of passes" |
| 109 | << " on the input program!\n"; |
Chris Lattner | 9c6cfe1 | 2003-10-17 23:03:16 +0000 | [diff] [blame] | 110 | BD.setPassesToRun(Prefix); |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 111 | BD.EmitProgressBitcode("pass-error", false); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 112 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // If the prefix maintains the predicate by itself, only keep the prefix! |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 116 | Diff = BD.diffProgram(BitcodeResult, "", false, &Error); |
| 117 | if (!Error.empty()) |
| 118 | return InternalError; |
| 119 | if (Diff) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 120 | outs() << " nope.\n"; |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 121 | sys::Path(BitcodeResult).eraseFromDisk(); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 122 | return KeepPrefix; |
| 123 | } |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 124 | outs() << " yup.\n"; // No miscompilation! |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 125 | |
| 126 | // Ok, so now we know that the prefix passes work, try running the suffix |
| 127 | // passes on the result of the prefix passes. |
| 128 | // |
Jeffrey Yasskin | 6865f29 | 2010-05-11 23:25:16 +0000 | [diff] [blame] | 129 | OwningPtr<Module> PrefixOutput(ParseInputFile(BitcodeResult, |
| 130 | BD.getContext())); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 131 | if (PrefixOutput == 0) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 132 | errs() << BD.getToolName() << ": Error reading bitcode file '" |
| 133 | << BitcodeResult << "'!\n"; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 134 | exit(1); |
| 135 | } |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 136 | sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk |
Chris Lattner | f4789e6 | 2004-04-23 20:36:51 +0000 | [diff] [blame] | 137 | |
| 138 | // Don't check if there are no passes in the suffix. |
| 139 | if (Suffix.empty()) |
| 140 | return NoFailure; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 141 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 142 | outs() << "Checking to see if '" << getPassesString(Suffix) |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 143 | << "' passes compile correctly after the '" |
| 144 | << getPassesString(Prefix) << "' passes: "; |
| 145 | |
Jeffrey Yasskin | 6865f29 | 2010-05-11 23:25:16 +0000 | [diff] [blame] | 146 | OwningPtr<Module> OriginalInput(BD.swapProgramIn(PrefixOutput.take())); |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 147 | if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 148 | errs() << " Error running this sequence of passes" |
| 149 | << " on the input program!\n"; |
Chris Lattner | 5ef681c | 2003-10-17 23:07:47 +0000 | [diff] [blame] | 150 | BD.setPassesToRun(Suffix); |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 151 | BD.EmitProgressBitcode("pass-error", false); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 152 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Run the result... |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 156 | Diff = BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/, &Error); |
| 157 | if (!Error.empty()) |
| 158 | return InternalError; |
| 159 | if (Diff) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 160 | outs() << " nope.\n"; |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 161 | return KeepSuffix; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Otherwise, we must not be running the bad pass anymore. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 165 | outs() << " yup.\n"; // No miscompilation! |
Jeffrey Yasskin | 6865f29 | 2010-05-11 23:25:16 +0000 | [diff] [blame] | 166 | // Restore orig program & free test. |
| 167 | delete BD.swapProgramIn(OriginalInput.take()); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 168 | return NoFailure; |
| 169 | } |
| 170 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 171 | namespace { |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 172 | class ReduceMiscompilingFunctions : public ListReducer<Function*> { |
| 173 | BugDriver &BD; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 174 | bool (*TestFn)(BugDriver &, Module *, Module *, std::string &); |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 175 | public: |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 176 | ReduceMiscompilingFunctions(BugDriver &bd, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 177 | bool (*F)(BugDriver &, Module *, Module *, |
| 178 | std::string &)) |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 179 | : BD(bd), TestFn(F) {} |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 180 | |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 181 | virtual TestResult doTest(std::vector<Function*> &Prefix, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 182 | std::vector<Function*> &Suffix, |
| 183 | std::string &Error) { |
| 184 | if (!Suffix.empty()) { |
| 185 | bool Ret = TestFuncs(Suffix, Error); |
| 186 | if (!Error.empty()) |
| 187 | return InternalError; |
| 188 | if (Ret) |
| 189 | return KeepSuffix; |
| 190 | } |
| 191 | if (!Prefix.empty()) { |
| 192 | bool Ret = TestFuncs(Prefix, Error); |
| 193 | if (!Error.empty()) |
| 194 | return InternalError; |
| 195 | if (Ret) |
| 196 | return KeepPrefix; |
| 197 | } |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 198 | return NoFailure; |
| 199 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 200 | |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame^] | 201 | bool TestFuncs(const std::vector<Function*> &Prefix, std::string &Error); |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 202 | }; |
| 203 | } |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 204 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 205 | /// TestMergedProgram - Given two modules, link them together and run the |
| 206 | /// program, checking to see if the program matches the diff. If the diff |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 207 | /// matches, return false, otherwise return true. If the DeleteInputs argument |
| 208 | /// is set to true then this function deletes both input modules before it |
| 209 | /// returns. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 210 | /// |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 211 | static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 212 | bool DeleteInputs, std::string &Error) { |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 213 | // Link the two portions of the program back to together. |
| 214 | std::string ErrorMsg; |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 215 | if (!DeleteInputs) { |
| 216 | M1 = CloneModule(M1); |
| 217 | M2 = CloneModule(M2); |
| 218 | } |
Reid Spencer | e487402 | 2004-12-13 03:01:03 +0000 | [diff] [blame] | 219 | if (Linker::LinkModules(M1, M2, &ErrorMsg)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 220 | errs() << BD.getToolName() << ": Error linking modules together:" |
| 221 | << ErrorMsg << '\n'; |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 222 | exit(1); |
| 223 | } |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 224 | delete M2; // We are done with this module. |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 225 | |
Jeffrey Yasskin | 6865f29 | 2010-05-11 23:25:16 +0000 | [diff] [blame] | 226 | OwningPtr<Module> OldProgram(BD.swapProgramIn(M1)); |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 227 | |
| 228 | // Execute the program. If it does not match the expected output, we must |
| 229 | // return true. |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 230 | bool Broken = BD.diffProgram("", "", false, &Error); |
| 231 | if (!Error.empty()) { |
| 232 | // Delete the linked module & restore the original |
Jeffrey Yasskin | 6865f29 | 2010-05-11 23:25:16 +0000 | [diff] [blame] | 233 | delete BD.swapProgramIn(OldProgram.take()); |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 234 | } |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 235 | return Broken; |
| 236 | } |
| 237 | |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 238 | /// TestFuncs - split functions in a Module into two groups: those that are |
| 239 | /// under consideration for miscompilation vs. those that are not, and test |
| 240 | /// accordingly. Each group of functions becomes a separate Module. |
| 241 | /// |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame^] | 242 | bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs, |
| 243 | std::string &Error) { |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 244 | // Test to see if the function is misoptimized if we ONLY run it on the |
| 245 | // functions listed in Funcs. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 246 | outs() << "Checking to see if the program is misoptimized when " |
| 247 | << (Funcs.size()==1 ? "this function is" : "these functions are") |
| 248 | << " run through the pass" |
| 249 | << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":"; |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 250 | PrintFunctionList(Funcs); |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 251 | outs() << '\n'; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 252 | |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame^] | 253 | // Create a clone for two reasons: |
| 254 | // * If the optimization passes delete any function, the deleted function |
| 255 | // will be in the clone and Funcs will still point to valid memory |
| 256 | // * If the optimization passes use interprocedural information to break |
| 257 | // a function, we want to continue with the original function. Otherwise |
| 258 | // we can conclude that a function triggers the bug when in fact one |
| 259 | // needs a larger set of original functions to do so. |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 260 | ValueMap<const Value*, Value*> VMap; |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame^] | 261 | Module *Clone = CloneModule(BD.getProgram(), VMap); |
| 262 | Module *Orig = BD.swapProgramIn(Clone); |
| 263 | |
| 264 | std::vector<Function*> FuncsOnClone; |
| 265 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
| 266 | Function *F = cast<Function>(VMap[Funcs[i]]); |
| 267 | FuncsOnClone.push_back(F); |
| 268 | } |
| 269 | |
| 270 | // Split the module into the two halves of the program we want. |
| 271 | VMap.clear(); |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 272 | Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame^] | 273 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, FuncsOnClone, |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 274 | VMap); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 275 | |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 276 | // Run the predicate, note that the predicate will delete both input modules. |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame^] | 277 | bool Broken = TestFn(BD, ToOptimize, ToNotOptimize, Error); |
| 278 | |
| 279 | delete BD.swapProgramIn(Orig); |
| 280 | |
| 281 | return Broken; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Chris Lattner | 8abfb8a | 2010-01-16 21:34:01 +0000 | [diff] [blame] | 284 | /// DisambiguateGlobalSymbols - Give anonymous global values names. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 285 | /// |
Chris Lattner | 36ee07f | 2004-04-11 23:52:35 +0000 | [diff] [blame] | 286 | static void DisambiguateGlobalSymbols(Module *M) { |
Chris Lattner | 67ef9e4 | 2006-05-04 23:35:31 +0000 | [diff] [blame] | 287 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
Chris Lattner | 1ffb33d | 2010-01-16 21:34:51 +0000 | [diff] [blame] | 288 | I != E; ++I) |
Chris Lattner | 8abfb8a | 2010-01-16 21:34:01 +0000 | [diff] [blame] | 289 | if (!I->hasName()) |
| 290 | I->setName("anon_global"); |
Chris Lattner | 1ffb33d | 2010-01-16 21:34:51 +0000 | [diff] [blame] | 291 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
Chris Lattner | 8abfb8a | 2010-01-16 21:34:01 +0000 | [diff] [blame] | 292 | if (!I->hasName()) |
| 293 | I->setName("anon_fn"); |
Chris Lattner | 36ee07f | 2004-04-11 23:52:35 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 296 | /// ExtractLoops - Given a reduced list of functions that still exposed the bug, |
| 297 | /// check to see if we can extract the loops in the region without obscuring the |
| 298 | /// bug. If so, it reduces the amount of code identified. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 299 | /// |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 300 | static bool ExtractLoops(BugDriver &BD, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 301 | bool (*TestFn)(BugDriver &, Module *, Module *, |
| 302 | std::string &), |
| 303 | std::vector<Function*> &MiscompiledFunctions, |
| 304 | std::string &Error) { |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 305 | bool MadeChange = false; |
| 306 | while (1) { |
Chris Lattner | aed98fa | 2005-08-02 23:25:56 +0000 | [diff] [blame] | 307 | if (BugpointIsInterrupted) return MadeChange; |
| 308 | |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 309 | ValueMap<const Value*, Value*> VMap; |
| 310 | Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 311 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 312 | MiscompiledFunctions, |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 313 | VMap); |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 314 | Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize); |
| 315 | if (!ToOptimizeLoopExtracted) { |
| 316 | // If the loop extractor crashed or if there were no extractible loops, |
| 317 | // then this chapter of our odyssey is over with. |
| 318 | delete ToNotOptimize; |
| 319 | delete ToOptimize; |
| 320 | return MadeChange; |
| 321 | } |
| 322 | |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 323 | errs() << "Extracted a loop from the breaking portion of the program.\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 324 | |
| 325 | // Bugpoint is intentionally not very trusting of LLVM transformations. In |
| 326 | // particular, we're not going to assume that the loop extractor works, so |
| 327 | // we're going to test the newly loop extracted program to make sure nothing |
| 328 | // has broken. If something broke, then we'll inform the user and stop |
| 329 | // extraction. |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 330 | AbstractInterpreter *AI = BD.switchToSafeInterpreter(); |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 331 | bool Failure = TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, |
| 332 | false, Error); |
| 333 | if (!Error.empty()) |
| 334 | return false; |
| 335 | if (Failure) { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 336 | BD.switchToInterpreter(AI); |
| 337 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 338 | // Merged program doesn't work anymore! |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 339 | errs() << " *** ERROR: Loop extraction broke the program. :(" |
| 340 | << " Please report a bug!\n"; |
| 341 | errs() << " Continuing on with un-loop-extracted version.\n"; |
Chris Lattner | 56c4186 | 2005-05-08 21:54:56 +0000 | [diff] [blame] | 342 | |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 343 | BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc", |
| 344 | ToNotOptimize); |
| 345 | BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc", |
| 346 | ToOptimize); |
| 347 | BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc", |
Chris Lattner | 56c4186 | 2005-05-08 21:54:56 +0000 | [diff] [blame] | 348 | ToOptimizeLoopExtracted); |
| 349 | |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 350 | errs() << "Please submit the " |
| 351 | << OutputPrefix << "-loop-extract-fail-*.bc files.\n"; |
Chris Lattner | 56c4186 | 2005-05-08 21:54:56 +0000 | [diff] [blame] | 352 | delete ToOptimize; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 353 | delete ToNotOptimize; |
| 354 | delete ToOptimizeLoopExtracted; |
| 355 | return MadeChange; |
| 356 | } |
Chris Lattner | 56c4186 | 2005-05-08 21:54:56 +0000 | [diff] [blame] | 357 | delete ToOptimize; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 358 | BD.switchToInterpreter(AI); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 359 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 360 | outs() << " Testing after loop extraction:\n"; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 361 | // Clone modules, the tester function will free them. |
| 362 | Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted); |
| 363 | Module *TNOBackup = CloneModule(ToNotOptimize); |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 364 | Failure = TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize, Error); |
| 365 | if (!Error.empty()) |
| 366 | return false; |
| 367 | if (!Failure) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 368 | outs() << "*** Loop extraction masked the problem. Undoing.\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 369 | // If the program is not still broken, then loop extraction did something |
| 370 | // that masked the error. Stop loop extraction now. |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 371 | delete TOLEBackup; |
| 372 | delete TNOBackup; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 373 | return MadeChange; |
| 374 | } |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 375 | ToOptimizeLoopExtracted = TOLEBackup; |
| 376 | ToNotOptimize = TNOBackup; |
| 377 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 378 | outs() << "*** Loop extraction successful!\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 380 | std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions; |
| 381 | for (Module::iterator I = ToOptimizeLoopExtracted->begin(), |
| 382 | E = ToOptimizeLoopExtracted->end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 383 | if (!I->isDeclaration()) |
Chris Lattner | fa1af13 | 2004-11-19 07:09:40 +0000 | [diff] [blame] | 384 | MisCompFunctions.push_back(std::make_pair(I->getName(), |
| 385 | I->getFunctionType())); |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 386 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 387 | // Okay, great! Now we know that we extracted a loop and that loop |
| 388 | // extraction both didn't break the program, and didn't mask the problem. |
| 389 | // Replace the current program with the loop extracted version, and try to |
| 390 | // extract another loop. |
| 391 | std::string ErrorMsg; |
Reid Spencer | e487402 | 2004-12-13 03:01:03 +0000 | [diff] [blame] | 392 | if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)){ |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 393 | errs() << BD.getToolName() << ": Error linking modules together:" |
| 394 | << ErrorMsg << '\n'; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 395 | exit(1); |
| 396 | } |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 397 | delete ToOptimizeLoopExtracted; |
Chris Lattner | d3a533d | 2004-03-17 17:42:09 +0000 | [diff] [blame] | 398 | |
| 399 | // All of the Function*'s in the MiscompiledFunctions list are in the old |
Chris Lattner | 5313f23 | 2004-04-02 06:32:17 +0000 | [diff] [blame] | 400 | // module. Update this list to include all of the functions in the |
| 401 | // optimized and loop extracted module. |
| 402 | MiscompiledFunctions.clear(); |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 403 | for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 404 | Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first); |
| 405 | |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 406 | assert(NewF && "Function not found??"); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 407 | assert(NewF->getFunctionType() == MisCompFunctions[i].second && |
| 408 | "found wrong function type?"); |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 409 | MiscompiledFunctions.push_back(NewF); |
Chris Lattner | d3a533d | 2004-03-17 17:42:09 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 412 | BD.setNewProgram(ToNotOptimize); |
| 413 | MadeChange = true; |
| 414 | } |
| 415 | } |
| 416 | |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 417 | namespace { |
| 418 | class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> { |
| 419 | BugDriver &BD; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 420 | bool (*TestFn)(BugDriver &, Module *, Module *, std::string &); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 421 | std::vector<Function*> FunctionsBeingTested; |
| 422 | public: |
| 423 | ReduceMiscompiledBlocks(BugDriver &bd, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 424 | bool (*F)(BugDriver &, Module *, Module *, |
| 425 | std::string &), |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 426 | const std::vector<Function*> &Fns) |
| 427 | : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {} |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 429 | virtual TestResult doTest(std::vector<BasicBlock*> &Prefix, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 430 | std::vector<BasicBlock*> &Suffix, |
| 431 | std::string &Error) { |
| 432 | if (!Suffix.empty()) { |
| 433 | bool Ret = TestFuncs(Suffix, Error); |
| 434 | if (!Error.empty()) |
| 435 | return InternalError; |
| 436 | if (Ret) |
| 437 | return KeepSuffix; |
| 438 | } |
| 439 | if (!Prefix.empty()) { |
| 440 | bool Ret = TestFuncs(Prefix, Error); |
| 441 | if (!Error.empty()) |
| 442 | return InternalError; |
| 443 | if (Ret) |
| 444 | return KeepPrefix; |
| 445 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 446 | return NoFailure; |
| 447 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 448 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 449 | bool TestFuncs(const std::vector<BasicBlock*> &BBs, std::string &Error); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 450 | }; |
| 451 | } |
| 452 | |
| 453 | /// TestFuncs - Extract all blocks for the miscompiled functions except for the |
| 454 | /// specified blocks. If the problem still exists, return true. |
| 455 | /// |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 456 | bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs, |
| 457 | std::string &Error) { |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 458 | // Test to see if the function is misoptimized if we ONLY run it on the |
| 459 | // functions listed in Funcs. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 460 | outs() << "Checking to see if the program is misoptimized when all "; |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 461 | if (!BBs.empty()) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 462 | outs() << "but these " << BBs.size() << " blocks are extracted: "; |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 463 | for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 464 | outs() << BBs[i]->getName() << " "; |
| 465 | if (BBs.size() > 10) outs() << "..."; |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 466 | } else { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 467 | outs() << "blocks are extracted."; |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 468 | } |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 469 | outs() << '\n'; |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 470 | |
| 471 | // Split the module into the two halves of the program we want. |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 472 | ValueMap<const Value*, Value*> VMap; |
| 473 | Module *ToNotOptimize = CloneModule(BD.getProgram(), VMap); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 474 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 475 | FunctionsBeingTested, |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 476 | VMap); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 477 | |
| 478 | // Try the extraction. If it doesn't work, then the block extractor crashed |
| 479 | // or something, in which case bugpoint can't chase down this possibility. |
| 480 | if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) { |
| 481 | delete ToOptimize; |
| 482 | // Run the predicate, not that the predicate will delete both input modules. |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 483 | return TestFn(BD, New, ToNotOptimize, Error); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 484 | } |
| 485 | delete ToOptimize; |
| 486 | delete ToNotOptimize; |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | /// ExtractBlocks - Given a reduced list of functions that still expose the bug, |
| 492 | /// extract as many basic blocks from the region as possible without obscuring |
| 493 | /// the bug. |
| 494 | /// |
| 495 | static bool ExtractBlocks(BugDriver &BD, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 496 | bool (*TestFn)(BugDriver &, Module *, Module *, |
| 497 | std::string &), |
| 498 | std::vector<Function*> &MiscompiledFunctions, |
| 499 | std::string &Error) { |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 500 | if (BugpointIsInterrupted) return false; |
| 501 | |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 502 | std::vector<BasicBlock*> Blocks; |
| 503 | for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) |
| 504 | for (Function::iterator I = MiscompiledFunctions[i]->begin(), |
| 505 | E = MiscompiledFunctions[i]->end(); I != E; ++I) |
| 506 | Blocks.push_back(I); |
| 507 | |
| 508 | // Use the list reducer to identify blocks that can be extracted without |
| 509 | // obscuring the bug. The Blocks list will end up containing blocks that must |
| 510 | // be retained from the original program. |
| 511 | unsigned OldSize = Blocks.size(); |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 512 | |
| 513 | // Check to see if all blocks are extractible first. |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 514 | bool Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions) |
| 515 | .TestFuncs(std::vector<BasicBlock*>(), Error); |
| 516 | if (!Error.empty()) |
| 517 | return false; |
| 518 | if (Ret) { |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 519 | Blocks.clear(); |
| 520 | } else { |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 521 | ReduceMiscompiledBlocks(BD, TestFn, |
| 522 | MiscompiledFunctions).reduceList(Blocks, Error); |
| 523 | if (!Error.empty()) |
| 524 | return false; |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 525 | if (Blocks.size() == OldSize) |
| 526 | return false; |
| 527 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 528 | |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 529 | ValueMap<const Value*, Value*> VMap; |
| 530 | Module *ProgClone = CloneModule(BD.getProgram(), VMap); |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 531 | Module *ToExtract = SplitFunctionsOutOfModule(ProgClone, |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 532 | MiscompiledFunctions, |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 533 | VMap); |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 534 | Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract); |
| 535 | if (Extracted == 0) { |
Chris Lattner | da895d6 | 2005-02-27 06:18:25 +0000 | [diff] [blame] | 536 | // Weird, extraction should have worked. |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 537 | errs() << "Nondeterministic problem extracting blocks??\n"; |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 538 | delete ProgClone; |
| 539 | delete ToExtract; |
| 540 | return false; |
| 541 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 542 | |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 543 | // Otherwise, block extraction succeeded. Link the two program fragments back |
| 544 | // together. |
| 545 | delete ToExtract; |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 546 | |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 547 | std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions; |
| 548 | for (Module::iterator I = Extracted->begin(), E = Extracted->end(); |
| 549 | I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 550 | if (!I->isDeclaration()) |
Chris Lattner | fa1af13 | 2004-11-19 07:09:40 +0000 | [diff] [blame] | 551 | MisCompFunctions.push_back(std::make_pair(I->getName(), |
| 552 | I->getFunctionType())); |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 554 | std::string ErrorMsg; |
Reid Spencer | e487402 | 2004-12-13 03:01:03 +0000 | [diff] [blame] | 555 | if (Linker::LinkModules(ProgClone, Extracted, &ErrorMsg)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 556 | errs() << BD.getToolName() << ": Error linking modules together:" |
| 557 | << ErrorMsg << '\n'; |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 558 | exit(1); |
| 559 | } |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 560 | delete Extracted; |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 561 | |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 562 | // Set the new program and delete the old one. |
| 563 | BD.setNewProgram(ProgClone); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 564 | |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 565 | // Update the list of miscompiled functions. |
| 566 | MiscompiledFunctions.clear(); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 568 | for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 569 | Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first); |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 570 | assert(NewF && "Function not found??"); |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 571 | assert(NewF->getFunctionType() == MisCompFunctions[i].second && |
| 572 | "Function has wrong type??"); |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 573 | MiscompiledFunctions.push_back(NewF); |
| 574 | } |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 575 | |
| 576 | return true; |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 580 | /// DebugAMiscompilation - This is a generic driver to narrow down |
| 581 | /// miscompilations, either in an optimization or a code generator. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 582 | /// |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 583 | static std::vector<Function*> |
| 584 | DebugAMiscompilation(BugDriver &BD, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 585 | bool (*TestFn)(BugDriver &, Module *, Module *, |
| 586 | std::string &), |
| 587 | std::string &Error) { |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 588 | // Okay, now that we have reduced the list of passes which are causing the |
| 589 | // failure, see if we can pin down which functions are being |
| 590 | // miscompiled... first build a list of all of the non-external functions in |
| 591 | // the program. |
| 592 | std::vector<Function*> MiscompiledFunctions; |
| 593 | Module *Prog = BD.getProgram(); |
| 594 | for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 595 | if (!I->isDeclaration()) |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 596 | MiscompiledFunctions.push_back(I); |
| 597 | |
| 598 | // Do the reduction... |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 599 | if (!BugpointIsInterrupted) |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 600 | ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, |
| 601 | Error); |
| 602 | if (!Error.empty()) |
| 603 | return MiscompiledFunctions; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 604 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 605 | outs() << "\n*** The following function" |
| 606 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 607 | << " being miscompiled: "; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 608 | PrintFunctionList(MiscompiledFunctions); |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 609 | outs() << '\n'; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 610 | |
| 611 | // See if we can rip any loops out of the miscompiled functions and still |
| 612 | // trigger the problem. |
Reid Spencer | dc31a8a | 2006-11-11 19:05:02 +0000 | [diff] [blame] | 613 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 614 | if (!BugpointIsInterrupted && !DisableLoopExtraction) { |
| 615 | bool Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions, Error); |
| 616 | if (!Error.empty()) |
| 617 | return MiscompiledFunctions; |
| 618 | if (Ret) { |
| 619 | // Okay, we extracted some loops and the problem still appears. See if |
| 620 | // we can eliminate some of the created functions from being candidates. |
| 621 | DisambiguateGlobalSymbols(BD.getProgram()); |
Chris Lattner | 36ee07f | 2004-04-11 23:52:35 +0000 | [diff] [blame] | 622 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 623 | // Do the reduction... |
| 624 | if (!BugpointIsInterrupted) |
| 625 | ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, |
| 626 | Error); |
| 627 | if (!Error.empty()) |
| 628 | return MiscompiledFunctions; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 629 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 630 | outs() << "\n*** The following function" |
| 631 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 632 | << " being miscompiled: "; |
| 633 | PrintFunctionList(MiscompiledFunctions); |
| 634 | outs() << '\n'; |
| 635 | } |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 638 | if (!BugpointIsInterrupted && !DisableBlockExtraction) { |
| 639 | bool Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions, Error); |
| 640 | if (!Error.empty()) |
| 641 | return MiscompiledFunctions; |
| 642 | if (Ret) { |
| 643 | // Okay, we extracted some blocks and the problem still appears. See if |
| 644 | // we can eliminate some of the created functions from being candidates. |
| 645 | DisambiguateGlobalSymbols(BD.getProgram()); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 646 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 647 | // Do the reduction... |
| 648 | ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, |
| 649 | Error); |
| 650 | if (!Error.empty()) |
| 651 | return MiscompiledFunctions; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 652 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 653 | outs() << "\n*** The following function" |
| 654 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 655 | << " being miscompiled: "; |
| 656 | PrintFunctionList(MiscompiledFunctions); |
| 657 | outs() << '\n'; |
| 658 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 661 | return MiscompiledFunctions; |
| 662 | } |
| 663 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 664 | /// TestOptimizer - This is the predicate function used to check to see if the |
| 665 | /// "Test" portion of the program is misoptimized. If so, return true. In any |
| 666 | /// case, both module arguments are deleted. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 667 | /// |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 668 | static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe, |
| 669 | std::string &Error) { |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 670 | // Run the optimization passes on ToOptimize, producing a transformed version |
| 671 | // of the functions being tested. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 672 | outs() << " Optimizing functions being tested: "; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 673 | Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(), |
| 674 | /*AutoDebugCrashes*/true); |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 675 | outs() << "done.\n"; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 676 | delete Test; |
| 677 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 678 | outs() << " Checking to see if the merged program executes correctly: "; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 679 | bool Broken = TestMergedProgram(BD, Optimized, Safe, true, Error); |
| 680 | if (Error.empty()) outs() << (Broken ? " nope.\n" : " yup.\n"); |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 681 | return Broken; |
| 682 | } |
| 683 | |
| 684 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 685 | /// debugMiscompilation - This method is used when the passes selected are not |
| 686 | /// crashing, but the generated output is semantically different from the |
| 687 | /// input. |
| 688 | /// |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 689 | void BugDriver::debugMiscompilation(std::string *Error) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 690 | // Make sure something was miscompiled... |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 691 | if (!BugpointIsInterrupted) |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 692 | if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun, *Error)) { |
| 693 | if (Error->empty()) |
| 694 | errs() << "*** Optimized program matches reference output! No problem" |
| 695 | << " detected...\nbugpoint can't help you with your problem!\n"; |
| 696 | return; |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 697 | } |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 698 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 699 | outs() << "\n*** Found miscompiling pass" |
| 700 | << (getPassesToRun().size() == 1 ? "" : "es") << ": " |
| 701 | << getPassesString(getPassesToRun()) << '\n'; |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 702 | EmitProgressBitcode("passinput"); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 703 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 704 | std::vector<Function *> MiscompiledFunctions = |
| 705 | DebugAMiscompilation(*this, TestOptimizer, *Error); |
| 706 | if (!Error->empty()) |
| 707 | return; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 708 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 709 | // Output a bunch of bitcode files for the user... |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 710 | outs() << "Outputting reduced bitcode files which expose the problem:\n"; |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 711 | ValueMap<const Value*, Value*> VMap; |
| 712 | Module *ToNotOptimize = CloneModule(getProgram(), VMap); |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 713 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 714 | MiscompiledFunctions, |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 715 | VMap); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 716 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 717 | outs() << " Non-optimized portion: "; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 718 | ToNotOptimize = swapProgramIn(ToNotOptimize); |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 719 | EmitProgressBitcode("tonotoptimize", true); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 720 | setNewProgram(ToNotOptimize); // Delete hacked module. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 721 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 722 | outs() << " Portion that is input to optimizer: "; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 723 | ToOptimize = swapProgramIn(ToOptimize); |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 724 | EmitProgressBitcode("tooptimize"); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 725 | setNewProgram(ToOptimize); // Delete hacked module. |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 726 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 727 | return; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 728 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 729 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 730 | /// CleanupAndPrepareModules - Get the specified modules ready for code |
| 731 | /// generator testing. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 732 | /// |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 733 | static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test, |
| 734 | Module *Safe) { |
| 735 | // Clean up the modules, removing extra cruft that we don't need anymore... |
| 736 | Test = BD.performFinalCleanups(Test); |
| 737 | |
| 738 | // If we are executing the JIT, we have several nasty issues to take care of. |
| 739 | if (!BD.isExecutingJIT()) return; |
| 740 | |
| 741 | // First, if the main function is in the Safe module, we must add a stub to |
| 742 | // the Test module to call into it. Thus, we create a new function `main' |
| 743 | // which just calls the old one. |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 744 | if (Function *oldMain = Safe->getFunction("main")) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 745 | if (!oldMain->isDeclaration()) { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 746 | // Rename it |
| 747 | oldMain->setName("llvm_bugpoint_old_main"); |
| 748 | // Create a NEW `main' function with same type in the test module. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 749 | Function *newMain = Function::Create(oldMain->getFunctionType(), |
| 750 | GlobalValue::ExternalLinkage, |
| 751 | "main", Test); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 752 | // Create an `oldmain' prototype in the test module, which will |
| 753 | // corresponds to the real main function in the same module. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 754 | Function *oldMainProto = Function::Create(oldMain->getFunctionType(), |
| 755 | GlobalValue::ExternalLinkage, |
| 756 | oldMain->getName(), Test); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 757 | // Set up and remember the argument list for the main function. |
| 758 | std::vector<Value*> args; |
Alkis Evlogimenos | 5a1c58d | 2005-03-15 07:02:26 +0000 | [diff] [blame] | 759 | for (Function::arg_iterator |
| 760 | I = newMain->arg_begin(), E = newMain->arg_end(), |
| 761 | OI = oldMain->arg_begin(); I != E; ++I, ++OI) { |
Owen Anderson | 6bc41e8 | 2008-04-14 17:38:21 +0000 | [diff] [blame] | 762 | I->setName(OI->getName()); // Copy argument names from oldMain |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 763 | args.push_back(I); |
| 764 | } |
| 765 | |
| 766 | // Call the old main function and return its result |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 767 | BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 768 | CallInst *call = CallInst::Create(oldMainProto, args.begin(), args.end(), |
| 769 | "", BB); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 770 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 771 | // If the type of old function wasn't void, return value of call |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 772 | ReturnInst::Create(Safe->getContext(), call, BB); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // The second nasty issue we must deal with in the JIT is that the Safe |
| 776 | // module cannot directly reference any functions defined in the test |
| 777 | // module. Instead, we use a JIT API call to dynamically resolve the |
| 778 | // symbol. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 779 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 780 | // Add the resolver to the Safe module. |
| 781 | // Prototype: void *getPointerToNamedFunction(const char* Name) |
Chris Lattner | 2db43c8 | 2007-01-07 08:13:39 +0000 | [diff] [blame] | 782 | Constant *resolverFunc = |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 783 | Safe->getOrInsertFunction("getPointerToNamedFunction", |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 784 | Type::getInt8PtrTy(Safe->getContext()), |
| 785 | Type::getInt8PtrTy(Safe->getContext()), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 786 | (Type *)0); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 787 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 788 | // Use the function we just added to get addresses of functions we need. |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 789 | for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 790 | if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc && |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 791 | !F->isIntrinsic() /* ignore intrinsics */) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 792 | Function *TestFn = Test->getFunction(F->getName()); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 793 | |
| 794 | // Don't forward functions which are external in the test module too. |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 795 | if (TestFn && !TestFn->isDeclaration()) { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 796 | // 1. Add a string constant with its name to the global file |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 797 | Constant *InitArray = ConstantArray::get(F->getContext(), F->getName()); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 798 | GlobalVariable *funcName = |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 799 | new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/, |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 800 | GlobalValue::InternalLinkage, InitArray, |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 801 | F->getName() + "_name"); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 802 | |
| 803 | // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an |
| 804 | // sbyte* so it matches the signature of the resolver function. |
| 805 | |
| 806 | // GetElementPtr *funcName, ulong 0, ulong 0 |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 807 | std::vector<Constant*> GEPargs(2, |
| 808 | Constant::getNullValue(Type::getInt32Ty(F->getContext()))); |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 809 | Value *GEP = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 810 | ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 811 | std::vector<Value*> ResolverArgs; |
| 812 | ResolverArgs.push_back(GEP); |
| 813 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 814 | // Rewrite uses of F in global initializers, etc. to uses of a wrapper |
| 815 | // function that dynamically resolves the calls to F via our JIT API |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 816 | if (!F->use_empty()) { |
| 817 | // Create a new global to hold the cached function pointer. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 818 | Constant *NullPtr = ConstantPointerNull::get(F->getType()); |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 819 | GlobalVariable *Cache = |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 820 | new GlobalVariable(*F->getParent(), F->getType(), |
| 821 | false, GlobalValue::InternalLinkage, |
| 822 | NullPtr,F->getName()+".fpcache"); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 823 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 824 | // Construct a new stub function that will re-route calls to F |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 825 | const FunctionType *FuncTy = F->getFunctionType(); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 826 | Function *FuncWrapper = Function::Create(FuncTy, |
| 827 | GlobalValue::InternalLinkage, |
| 828 | F->getName() + "_wrapper", |
| 829 | F->getParent()); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 830 | BasicBlock *EntryBB = BasicBlock::Create(F->getContext(), |
| 831 | "entry", FuncWrapper); |
| 832 | BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(), |
| 833 | "usecache", FuncWrapper); |
| 834 | BasicBlock *LookupBB = BasicBlock::Create(F->getContext(), |
| 835 | "lookupfp", FuncWrapper); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 836 | |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 837 | // Check to see if we already looked up the value. |
| 838 | Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 839 | Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal, |
| 840 | NullPtr, "isNull"); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 841 | BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 842 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 843 | // Resolve the call to function F via the JIT API: |
| 844 | // |
| 845 | // call resolver(GetElementPtr...) |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 846 | CallInst *Resolver = |
| 847 | CallInst::Create(resolverFunc, ResolverArgs.begin(), |
| 848 | ResolverArgs.end(), "resolver", LookupBB); |
| 849 | |
| 850 | // Cast the result from the resolver to correctly-typed function. |
| 851 | CastInst *CastedResolver = |
| 852 | new BitCastInst(Resolver, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 853 | PointerType::getUnqual(F->getFunctionType()), |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 854 | "resolverCast", LookupBB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 855 | |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 856 | // Save the value in our cache. |
| 857 | new StoreInst(CastedResolver, Cache, LookupBB); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 858 | BranchInst::Create(DoCallBB, LookupBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 859 | |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 860 | PHINode *FuncPtr = PHINode::Create(NullPtr->getType(), |
| 861 | "fp", DoCallBB); |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 862 | FuncPtr->addIncoming(CastedResolver, LookupBB); |
| 863 | FuncPtr->addIncoming(CachedVal, EntryBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 864 | |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 865 | // Save the argument list. |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 866 | std::vector<Value*> Args; |
Alkis Evlogimenos | 5a1c58d | 2005-03-15 07:02:26 +0000 | [diff] [blame] | 867 | for (Function::arg_iterator i = FuncWrapper->arg_begin(), |
| 868 | e = FuncWrapper->arg_end(); i != e; ++i) |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 869 | Args.push_back(i); |
| 870 | |
| 871 | // Pass on the arguments to the real function, return its result |
Dan Gohman | e49a13e | 2010-06-07 20:19:26 +0000 | [diff] [blame] | 872 | if (F->getReturnType()->isVoidTy()) { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 873 | CallInst::Create(FuncPtr, Args.begin(), Args.end(), "", DoCallBB); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 874 | ReturnInst::Create(F->getContext(), DoCallBB); |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 875 | } else { |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 876 | CallInst *Call = CallInst::Create(FuncPtr, Args.begin(), Args.end(), |
| 877 | "retval", DoCallBB); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 878 | ReturnInst::Create(F->getContext(),Call, DoCallBB); |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 879 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 880 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 881 | // Use the wrapper function instead of the old function |
| 882 | F->replaceAllUsesWith(FuncWrapper); |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 883 | } |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | if (verifyModule(*Test) || verifyModule(*Safe)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 889 | errs() << "Bugpoint has a bug, which corrupted a module!!\n"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 890 | abort(); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | |
| 895 | |
| 896 | /// TestCodeGenerator - This is the predicate function used to check to see if |
| 897 | /// the "Test" portion of the program is miscompiled by the code generator under |
| 898 | /// test. If so, return true. In any case, both module arguments are deleted. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 899 | /// |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 900 | static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe, |
| 901 | std::string &Error) { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 902 | CleanupAndPrepareModules(BD, Test, Safe); |
| 903 | |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 904 | sys::Path TestModuleBC("bugpoint.test.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 905 | std::string ErrMsg; |
| 906 | if (TestModuleBC.makeUnique(true, &ErrMsg)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 907 | errs() << BD.getToolName() << "Error making unique filename: " |
| 908 | << ErrMsg << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 909 | exit(1); |
| 910 | } |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 911 | if (BD.writeProgramToFile(TestModuleBC.str(), Test)) { |
| 912 | errs() << "Error writing bitcode to `" << TestModuleBC.str() |
| 913 | << "'\nExiting."; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 914 | exit(1); |
| 915 | } |
| 916 | delete Test; |
| 917 | |
Rafael Espindola | bc2ed59 | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 918 | FileRemover TestModuleBCRemover(TestModuleBC, !SaveTemps); |
| 919 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 920 | // Make the shared library |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 921 | sys::Path SafeModuleBC("bugpoint.safe.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 922 | if (SafeModuleBC.makeUnique(true, &ErrMsg)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 923 | errs() << BD.getToolName() << "Error making unique filename: " |
| 924 | << ErrMsg << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 925 | exit(1); |
| 926 | } |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 928 | if (BD.writeProgramToFile(SafeModuleBC.str(), Safe)) { |
| 929 | errs() << "Error writing bitcode to `" << SafeModuleBC.str() |
| 930 | << "'\nExiting."; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 931 | exit(1); |
| 932 | } |
Rafael Espindola | bc2ed59 | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 933 | |
| 934 | FileRemover SafeModuleBCRemover(SafeModuleBC, !SaveTemps); |
| 935 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 936 | std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str(), Error); |
| 937 | if (!Error.empty()) |
Benjamin Kramer | 2706387 | 2010-04-12 12:22:19 +0000 | [diff] [blame] | 938 | return false; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 939 | delete Safe; |
| 940 | |
Rafael Espindola | bc2ed59 | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 941 | FileRemover SharedObjectRemover(sys::Path(SharedObject), !SaveTemps); |
| 942 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 943 | // Run the code generator on the `Test' code, loading the shared library. |
| 944 | // The function returns whether or not the new output differs from reference. |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 945 | bool Result = BD.diffProgram(TestModuleBC.str(), SharedObject, false, &Error); |
| 946 | if (!Error.empty()) |
| 947 | return false; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 948 | |
| 949 | if (Result) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 950 | errs() << ": still failing!\n"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 951 | else |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 952 | errs() << ": didn't fail.\n"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 953 | |
| 954 | return Result; |
| 955 | } |
| 956 | |
| 957 | |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 958 | /// debugCodeGenerator - debug errors in LLC, LLI, or CBE. |
| 959 | /// |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 960 | bool BugDriver::debugCodeGenerator(std::string *Error) { |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 961 | if ((void*)SafeInterpreter == (void*)Interpreter) { |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 962 | std::string Result = executeProgramSafely("bugpoint.safe.out", Error); |
| 963 | if (Error->empty()) { |
| 964 | outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match " |
| 965 | << "the reference diff. This may be due to a\n front-end " |
| 966 | << "bug or a bug in the original program, but this can also " |
| 967 | << "happen if bugpoint isn't running the program with the " |
| 968 | << "right flags or input.\n I left the result of executing " |
| 969 | << "the program with the \"safe\" backend in this file for " |
| 970 | << "you: '" |
| 971 | << Result << "'.\n"; |
| 972 | } |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | |
| 976 | DisambiguateGlobalSymbols(Program); |
| 977 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 978 | std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator, |
| 979 | *Error); |
| 980 | if (!Error->empty()) |
| 981 | return true; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 982 | |
| 983 | // Split the module into the two halves of the program we want. |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 984 | ValueMap<const Value*, Value*> VMap; |
| 985 | Module *ToNotCodeGen = CloneModule(getProgram(), VMap); |
| 986 | Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs, VMap); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 987 | |
| 988 | // Condition the modules |
| 989 | CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen); |
| 990 | |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 991 | sys::Path TestModuleBC("bugpoint.test.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 992 | std::string ErrMsg; |
| 993 | if (TestModuleBC.makeUnique(true, &ErrMsg)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 994 | errs() << getToolName() << "Error making unique filename: " |
| 995 | << ErrMsg << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 996 | exit(1); |
| 997 | } |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 998 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 999 | if (writeProgramToFile(TestModuleBC.str(), ToCodeGen)) { |
| 1000 | errs() << "Error writing bitcode to `" << TestModuleBC.str() |
| 1001 | << "'\nExiting."; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1002 | exit(1); |
| 1003 | } |
| 1004 | delete ToCodeGen; |
| 1005 | |
| 1006 | // Make the shared library |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 1007 | sys::Path SafeModuleBC("bugpoint.safe.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 1008 | if (SafeModuleBC.makeUnique(true, &ErrMsg)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 1009 | errs() << getToolName() << "Error making unique filename: " |
| 1010 | << ErrMsg << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 1011 | exit(1); |
| 1012 | } |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1014 | if (writeProgramToFile(SafeModuleBC.str(), ToNotCodeGen)) { |
| 1015 | errs() << "Error writing bitcode to `" << SafeModuleBC.str() |
| 1016 | << "'\nExiting."; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1017 | exit(1); |
| 1018 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1019 | std::string SharedObject = compileSharedObject(SafeModuleBC.str(), *Error); |
| 1020 | if (!Error->empty()) |
| 1021 | return true; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1022 | delete ToNotCodeGen; |
| 1023 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1024 | outs() << "You can reproduce the problem with the command line: \n"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1025 | if (isExecutingJIT()) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1026 | outs() << " lli -load " << SharedObject << " " << TestModuleBC.str(); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1027 | } else { |
Nick Lewycky | 8d0e1bc | 2010-04-29 23:37:44 +0000 | [diff] [blame] | 1028 | outs() << " llc " << TestModuleBC.str() << " -o " << TestModuleBC.str() |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1029 | << ".s\n"; |
| 1030 | outs() << " gcc " << SharedObject << " " << TestModuleBC.str() |
| 1031 | << ".s -o " << TestModuleBC.str() << ".exe"; |
Chris Lattner | 59615f0 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 1032 | #if defined (HAVE_LINK_R) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1033 | outs() << " -Wl,-R."; |
Chris Lattner | 59615f0 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 1034 | #endif |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1035 | outs() << "\n"; |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1036 | outs() << " " << TestModuleBC.str() << ".exe"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1037 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1038 | for (unsigned i = 0, e = InputArgv.size(); i != e; ++i) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1039 | outs() << " " << InputArgv[i]; |
| 1040 | outs() << '\n'; |
| 1041 | outs() << "The shared object was created with:\n llc -march=c " |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1042 | << SafeModuleBC.str() << " -o temporary.c\n" |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 1043 | << " gcc -xc temporary.c -O2 -o " << SharedObject; |
| 1044 | if (TargetTriple.getArch() == Triple::sparc) |
| 1045 | outs() << " -G"; // Compile a shared library, `-G' for Sparc |
| 1046 | else |
| 1047 | outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others |
| 1048 | |
| 1049 | outs() << " -fno-strict-aliasing\n"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1050 | |
| 1051 | return false; |
| 1052 | } |