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