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