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