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