Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 1 | //===- Miscompilation.cpp - Debug program miscompilations -----------------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 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" |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Verifier.h" |
| 23 | #include "llvm/Support/Mangler.h" |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/Cloning.h" |
| 25 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 26 | #include "Support/CommandLine.h" |
Misha Brukman | 3d9cafa | 2003-08-07 21:42:28 +0000 | [diff] [blame] | 27 | #include "Support/FileUtilities.h" |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 29 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 30 | namespace llvm { |
| 31 | extern cl::list<std::string> InputArgv; |
| 32 | } |
| 33 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 34 | namespace { |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 35 | class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> { |
| 36 | BugDriver &BD; |
| 37 | public: |
| 38 | ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} |
| 39 | |
| 40 | virtual TestResult doTest(std::vector<const PassInfo*> &Prefix, |
| 41 | std::vector<const PassInfo*> &Suffix); |
| 42 | }; |
| 43 | } |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 44 | |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame^] | 45 | /// TestResult - After passes have been split into a test group and a control |
| 46 | /// group, see if they still break the program. |
| 47 | /// |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 48 | ReduceMiscompilingPasses::TestResult |
Chris Lattner | 39aebca | 2003-04-24 22:24:22 +0000 | [diff] [blame] | 49 | ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix, |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 50 | std::vector<const PassInfo*> &Suffix) { |
| 51 | // 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] | 52 | // with JUST the kept passes, discard the prefix passes. |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 53 | std::cout << "Checking to see if '" << getPassesString(Suffix) |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 54 | << "' compile correctly: "; |
| 55 | |
| 56 | std::string BytecodeResult; |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 57 | if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) { |
Chris Lattner | 9f71e79 | 2003-10-18 00:05:05 +0000 | [diff] [blame] | 58 | std::cerr << " Error running this sequence of passes" |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 59 | << " on the input program!\n"; |
Chris Lattner | 5ef681c | 2003-10-17 23:07:47 +0000 | [diff] [blame] | 60 | BD.setPassesToRun(Suffix); |
| 61 | BD.EmitProgressBytecode("pass-error", false); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 62 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Check to see if the finished program matches the reference output... |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 66 | if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) { |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 67 | std::cout << "nope.\n"; |
| 68 | return KeepSuffix; // Miscompilation detected! |
| 69 | } |
| 70 | std::cout << "yup.\n"; // No miscompilation! |
| 71 | |
| 72 | if (Prefix.empty()) return NoFailure; |
| 73 | |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 74 | // 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] | 75 | // then separately run the "kept" passes. |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 76 | std::cout << "Checking to see if '" << getPassesString(Prefix) |
| 77 | << "' compile correctly: "; |
| 78 | |
| 79 | // If it is not broken with the kept passes, it's possible that the prefix |
| 80 | // passes must be run before the kept passes to break it. If the program |
| 81 | // WORKS after the prefix passes, but then fails if running the prefix AND |
| 82 | // kept passes, we can update our bytecode file to include the result of the |
| 83 | // prefix passes, then discard the prefix passes. |
| 84 | // |
| 85 | if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) { |
Chris Lattner | 9f71e79 | 2003-10-18 00:05:05 +0000 | [diff] [blame] | 86 | std::cerr << " Error running this sequence of passes" |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 87 | << " on the input program!\n"; |
Chris Lattner | 9c6cfe1 | 2003-10-17 23:03:16 +0000 | [diff] [blame] | 88 | BD.setPassesToRun(Prefix); |
| 89 | BD.EmitProgressBytecode("pass-error", false); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 90 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // If the prefix maintains the predicate by itself, only keep the prefix! |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 94 | if (BD.diffProgram(BytecodeResult)) { |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 95 | std::cout << "nope.\n"; |
| 96 | removeFile(BytecodeResult); |
| 97 | return KeepPrefix; |
| 98 | } |
| 99 | std::cout << "yup.\n"; // No miscompilation! |
| 100 | |
| 101 | // Ok, so now we know that the prefix passes work, try running the suffix |
| 102 | // passes on the result of the prefix passes. |
| 103 | // |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 104 | Module *PrefixOutput = ParseInputFile(BytecodeResult); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 105 | if (PrefixOutput == 0) { |
| 106 | std::cerr << BD.getToolName() << ": Error reading bytecode file '" |
| 107 | << BytecodeResult << "'!\n"; |
| 108 | exit(1); |
| 109 | } |
| 110 | removeFile(BytecodeResult); // No longer need the file on disk |
| 111 | |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 112 | std::cout << "Checking to see if '" << getPassesString(Suffix) |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 113 | << "' passes compile correctly after the '" |
| 114 | << getPassesString(Prefix) << "' passes: "; |
| 115 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 116 | Module *OriginalInput = BD.swapProgramIn(PrefixOutput); |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 117 | if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) { |
Chris Lattner | 9f71e79 | 2003-10-18 00:05:05 +0000 | [diff] [blame] | 118 | std::cerr << " Error running this sequence of passes" |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 119 | << " on the input program!\n"; |
Chris Lattner | 5ef681c | 2003-10-17 23:07:47 +0000 | [diff] [blame] | 120 | BD.setPassesToRun(Suffix); |
Chris Lattner | 9c6cfe1 | 2003-10-17 23:03:16 +0000 | [diff] [blame] | 121 | BD.EmitProgressBytecode("pass-error", false); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 122 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Run the result... |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 126 | if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) { |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 127 | std::cout << "nope.\n"; |
| 128 | delete OriginalInput; // We pruned down the original input... |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 129 | return KeepSuffix; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Otherwise, we must not be running the bad pass anymore. |
| 133 | std::cout << "yup.\n"; // No miscompilation! |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 134 | delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 135 | return NoFailure; |
| 136 | } |
| 137 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 138 | namespace { |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 139 | class ReduceMiscompilingFunctions : public ListReducer<Function*> { |
| 140 | BugDriver &BD; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 141 | bool (*TestFn)(BugDriver &, Module *, Module *); |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 142 | public: |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 143 | ReduceMiscompilingFunctions(BugDriver &bd, |
| 144 | bool (*F)(BugDriver &, Module *, Module *)) |
| 145 | : BD(bd), TestFn(F) {} |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 146 | |
| 147 | virtual TestResult doTest(std::vector<Function*> &Prefix, |
| 148 | std::vector<Function*> &Suffix) { |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 149 | if (!Suffix.empty() && TestFuncs(Suffix)) |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 150 | return KeepSuffix; |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 151 | if (!Prefix.empty() && TestFuncs(Prefix)) |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 152 | return KeepPrefix; |
| 153 | return NoFailure; |
| 154 | } |
| 155 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 156 | bool TestFuncs(const std::vector<Function*> &Prefix); |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 157 | }; |
| 158 | } |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 159 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 160 | /// TestMergedProgram - Given two modules, link them together and run the |
| 161 | /// 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] | 162 | /// matches, return false, otherwise return true. If the DeleteInputs argument |
| 163 | /// is set to true then this function deletes both input modules before it |
| 164 | /// returns. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame^] | 165 | /// |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 166 | static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2, |
| 167 | bool DeleteInputs) { |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 168 | // Link the two portions of the program back to together. |
| 169 | std::string ErrorMsg; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 170 | if (!DeleteInputs) M1 = CloneModule(M1); |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 171 | if (LinkModules(M1, M2, &ErrorMsg)) { |
| 172 | std::cerr << BD.getToolName() << ": Error linking modules together:" |
| 173 | << ErrorMsg << "\n"; |
| 174 | exit(1); |
| 175 | } |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 176 | if (DeleteInputs) delete M2; // We are done with this module... |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 177 | |
| 178 | Module *OldProgram = BD.swapProgramIn(M1); |
| 179 | |
| 180 | // Execute the program. If it does not match the expected output, we must |
| 181 | // return true. |
| 182 | bool Broken = BD.diffProgram(); |
| 183 | |
| 184 | // Delete the linked module & restore the original |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 185 | BD.swapProgramIn(OldProgram); |
Chris Lattner | 5313f23 | 2004-04-02 06:32:17 +0000 | [diff] [blame] | 186 | delete M1; |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 187 | return Broken; |
| 188 | } |
| 189 | |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame^] | 190 | /// TestFuncs - split functions in a Module into two groups: those that are |
| 191 | /// under consideration for miscompilation vs. those that are not, and test |
| 192 | /// accordingly. Each group of functions becomes a separate Module. |
| 193 | /// |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 194 | bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){ |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 195 | // Test to see if the function is misoptimized if we ONLY run it on the |
| 196 | // functions listed in Funcs. |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 197 | std::cout << "Checking to see if the program is misoptimized when " |
| 198 | << (Funcs.size()==1 ? "this function is" : "these functions are") |
| 199 | << " run through the pass" |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 200 | << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":"; |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 201 | PrintFunctionList(Funcs); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 202 | std::cout << "\n"; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 203 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 204 | // Split the module into the two halves of the program we want. |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 205 | Module *ToNotOptimize = CloneModule(BD.getProgram()); |
| 206 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 207 | |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 208 | // Run the predicate, not that the predicate will delete both input modules. |
| 209 | return TestFn(BD, ToOptimize, ToNotOptimize); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame^] | 212 | /// DisambiguateGlobalSymbols - Mangle symbols to guarantee uniqueness by |
| 213 | /// modifying predominantly internal symbols rather than external ones. |
| 214 | /// |
Chris Lattner | 36ee07f | 2004-04-11 23:52:35 +0000 | [diff] [blame] | 215 | static void DisambiguateGlobalSymbols(Module *M) { |
| 216 | // Try not to cause collisions by minimizing chances of renaming an |
| 217 | // already-external symbol, so take in external globals and functions as-is. |
| 218 | // The code should work correctly without disambiguation (assuming the same |
| 219 | // mangler is used by the two code generators), but having symbols with the |
| 220 | // same name causes warnings to be emitted by the code generator. |
| 221 | Mangler Mang(*M); |
| 222 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 223 | I->setName(Mang.getValueName(I)); |
| 224 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 225 | I->setName(Mang.getValueName(I)); |
| 226 | } |
| 227 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 228 | /// ExtractLoops - Given a reduced list of functions that still exposed the bug, |
| 229 | /// check to see if we can extract the loops in the region without obscuring the |
| 230 | /// bug. If so, it reduces the amount of code identified. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame^] | 231 | /// |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 232 | static bool ExtractLoops(BugDriver &BD, |
| 233 | bool (*TestFn)(BugDriver &, Module *, Module *), |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 234 | std::vector<Function*> &MiscompiledFunctions) { |
| 235 | bool MadeChange = false; |
| 236 | while (1) { |
| 237 | Module *ToNotOptimize = CloneModule(BD.getProgram()); |
| 238 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, |
| 239 | MiscompiledFunctions); |
| 240 | Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize); |
| 241 | if (!ToOptimizeLoopExtracted) { |
| 242 | // If the loop extractor crashed or if there were no extractible loops, |
| 243 | // then this chapter of our odyssey is over with. |
| 244 | delete ToNotOptimize; |
| 245 | delete ToOptimize; |
| 246 | return MadeChange; |
| 247 | } |
| 248 | |
| 249 | std::cerr << "Extracted a loop from the breaking portion of the program.\n"; |
| 250 | delete ToOptimize; |
| 251 | |
| 252 | // Bugpoint is intentionally not very trusting of LLVM transformations. In |
| 253 | // particular, we're not going to assume that the loop extractor works, so |
| 254 | // we're going to test the newly loop extracted program to make sure nothing |
| 255 | // has broken. If something broke, then we'll inform the user and stop |
| 256 | // extraction. |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 257 | AbstractInterpreter *AI = BD.switchToCBE(); |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 258 | if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 259 | BD.switchToInterpreter(AI); |
| 260 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 261 | // Merged program doesn't work anymore! |
| 262 | std::cerr << " *** ERROR: Loop extraction broke the program. :(" |
| 263 | << " Please report a bug!\n"; |
| 264 | std::cerr << " Continuing on with un-loop-extracted version.\n"; |
| 265 | delete ToNotOptimize; |
| 266 | delete ToOptimizeLoopExtracted; |
| 267 | return MadeChange; |
| 268 | } |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 269 | BD.switchToInterpreter(AI); |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 270 | |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 271 | std::cout << " Testing after loop extraction:\n"; |
| 272 | // Clone modules, the tester function will free them. |
| 273 | Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted); |
| 274 | Module *TNOBackup = CloneModule(ToNotOptimize); |
| 275 | if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) { |
| 276 | std::cout << "*** Loop extraction masked the problem. Undoing.\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 277 | // If the program is not still broken, then loop extraction did something |
| 278 | // that masked the error. Stop loop extraction now. |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 279 | delete TOLEBackup; |
| 280 | delete TNOBackup; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 281 | return MadeChange; |
| 282 | } |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 283 | ToOptimizeLoopExtracted = TOLEBackup; |
| 284 | ToNotOptimize = TNOBackup; |
| 285 | |
| 286 | std::cout << "*** Loop extraction successful!\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 287 | |
| 288 | // Okay, great! Now we know that we extracted a loop and that loop |
| 289 | // extraction both didn't break the program, and didn't mask the problem. |
| 290 | // Replace the current program with the loop extracted version, and try to |
| 291 | // extract another loop. |
| 292 | std::string ErrorMsg; |
| 293 | if (LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)) { |
| 294 | std::cerr << BD.getToolName() << ": Error linking modules together:" |
| 295 | << ErrorMsg << "\n"; |
| 296 | exit(1); |
| 297 | } |
Chris Lattner | d3a533d | 2004-03-17 17:42:09 +0000 | [diff] [blame] | 298 | |
| 299 | // 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] | 300 | // module. Update this list to include all of the functions in the |
| 301 | // optimized and loop extracted module. |
| 302 | MiscompiledFunctions.clear(); |
| 303 | for (Module::iterator I = ToOptimizeLoopExtracted->begin(), |
| 304 | E = ToOptimizeLoopExtracted->end(); I != E; ++I) { |
| 305 | if (!I->isExternal()) { |
Chris Lattner | 02bb481 | 2004-04-02 06:32:45 +0000 | [diff] [blame] | 306 | Function *NewF = ToNotOptimize->getFunction(I->getName(), |
| 307 | I->getFunctionType()); |
Chris Lattner | 5313f23 | 2004-04-02 06:32:17 +0000 | [diff] [blame] | 308 | assert(NewF && "Function not found??"); |
| 309 | MiscompiledFunctions.push_back(NewF); |
| 310 | } |
Chris Lattner | d3a533d | 2004-03-17 17:42:09 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 5313f23 | 2004-04-02 06:32:17 +0000 | [diff] [blame] | 312 | delete ToOptimizeLoopExtracted; |
Chris Lattner | d3a533d | 2004-03-17 17:42:09 +0000 | [diff] [blame] | 313 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 314 | BD.setNewProgram(ToNotOptimize); |
| 315 | MadeChange = true; |
| 316 | } |
| 317 | } |
| 318 | |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 319 | /// DebugAMiscompilation - This is a generic driver to narrow down |
| 320 | /// miscompilations, either in an optimization or a code generator. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame^] | 321 | /// |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 322 | static std::vector<Function*> |
| 323 | DebugAMiscompilation(BugDriver &BD, |
| 324 | bool (*TestFn)(BugDriver &, Module *, Module *)) { |
| 325 | // Okay, now that we have reduced the list of passes which are causing the |
| 326 | // failure, see if we can pin down which functions are being |
| 327 | // miscompiled... first build a list of all of the non-external functions in |
| 328 | // the program. |
| 329 | std::vector<Function*> MiscompiledFunctions; |
| 330 | Module *Prog = BD.getProgram(); |
| 331 | for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I) |
| 332 | if (!I->isExternal()) |
| 333 | MiscompiledFunctions.push_back(I); |
| 334 | |
| 335 | // Do the reduction... |
| 336 | ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); |
| 337 | |
| 338 | std::cout << "\n*** The following function" |
| 339 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 340 | << " being miscompiled: "; |
| 341 | PrintFunctionList(MiscompiledFunctions); |
| 342 | std::cout << "\n"; |
| 343 | |
| 344 | // See if we can rip any loops out of the miscompiled functions and still |
| 345 | // trigger the problem. |
| 346 | if (ExtractLoops(BD, TestFn, MiscompiledFunctions)) { |
| 347 | // Okay, we extracted some loops and the problem still appears. See if we |
| 348 | // can eliminate some of the created functions from being candidates. |
| 349 | |
Chris Lattner | 36ee07f | 2004-04-11 23:52:35 +0000 | [diff] [blame] | 350 | // Loop extraction can introduce functions with the same name (foo_code). |
| 351 | // Make sure to disambiguate the symbols so that when the program is split |
| 352 | // apart that we can link it back together again. |
| 353 | DisambiguateGlobalSymbols(BD.getProgram()); |
| 354 | |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 355 | // Do the reduction... |
| 356 | ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); |
| 357 | |
| 358 | std::cout << "\n*** The following function" |
| 359 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 360 | << " being miscompiled: "; |
| 361 | PrintFunctionList(MiscompiledFunctions); |
| 362 | std::cout << "\n"; |
| 363 | } |
| 364 | |
| 365 | return MiscompiledFunctions; |
| 366 | } |
| 367 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 368 | /// TestOptimizer - This is the predicate function used to check to see if the |
| 369 | /// "Test" portion of the program is misoptimized. If so, return true. In any |
| 370 | /// case, both module arguments are deleted. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame^] | 371 | /// |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 372 | static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) { |
| 373 | // Run the optimization passes on ToOptimize, producing a transformed version |
| 374 | // of the functions being tested. |
| 375 | std::cout << " Optimizing functions being tested: "; |
| 376 | Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(), |
| 377 | /*AutoDebugCrashes*/true); |
| 378 | std::cout << "done.\n"; |
| 379 | delete Test; |
| 380 | |
| 381 | std::cout << " Checking to see if the merged program executes correctly: "; |
Chris Lattner | 2423db0 | 2004-04-09 22:28:33 +0000 | [diff] [blame] | 382 | bool Broken = TestMergedProgram(BD, Optimized, Safe, true); |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 383 | std::cout << (Broken ? " nope.\n" : " yup.\n"); |
| 384 | return Broken; |
| 385 | } |
| 386 | |
| 387 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 388 | /// debugMiscompilation - This method is used when the passes selected are not |
| 389 | /// crashing, but the generated output is semantically different from the |
| 390 | /// input. |
| 391 | /// |
| 392 | bool BugDriver::debugMiscompilation() { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 393 | // Make sure something was miscompiled... |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 394 | if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 395 | std::cerr << "*** Optimized program matches reference output! No problem " |
| 396 | << "detected...\nbugpoint can't help you with your problem!\n"; |
| 397 | return false; |
| 398 | } |
| 399 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 400 | std::cout << "\n*** Found miscompiling pass" |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 401 | << (getPassesToRun().size() == 1 ? "" : "es") << ": " |
| 402 | << getPassesString(getPassesToRun()) << "\n"; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 403 | EmitProgressBytecode("passinput"); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 404 | |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 405 | std::vector<Function*> MiscompiledFunctions = |
| 406 | DebugAMiscompilation(*this, TestOptimizer); |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 408 | // Output a bunch of bytecode files for the user... |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 409 | std::cout << "Outputting reduced bytecode files which expose the problem:\n"; |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 410 | Module *ToNotOptimize = CloneModule(getProgram()); |
| 411 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, |
| 412 | MiscompiledFunctions); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 413 | |
| 414 | std::cout << " Non-optimized portion: "; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 415 | ToNotOptimize = swapProgramIn(ToNotOptimize); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 416 | EmitProgressBytecode("tonotoptimize", true); |
| 417 | setNewProgram(ToNotOptimize); // Delete hacked module. |
| 418 | |
| 419 | std::cout << " Portion that is input to optimizer: "; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 420 | ToOptimize = swapProgramIn(ToOptimize); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 421 | EmitProgressBytecode("tooptimize"); |
| 422 | setNewProgram(ToOptimize); // Delete hacked module. |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 424 | return false; |
| 425 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 426 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 427 | /// CleanupAndPrepareModules - Get the specified modules ready for code |
| 428 | /// generator testing. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame^] | 429 | /// |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 430 | static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test, |
| 431 | Module *Safe) { |
| 432 | // Clean up the modules, removing extra cruft that we don't need anymore... |
| 433 | Test = BD.performFinalCleanups(Test); |
| 434 | |
| 435 | // If we are executing the JIT, we have several nasty issues to take care of. |
| 436 | if (!BD.isExecutingJIT()) return; |
| 437 | |
| 438 | // First, if the main function is in the Safe module, we must add a stub to |
| 439 | // the Test module to call into it. Thus, we create a new function `main' |
| 440 | // which just calls the old one. |
| 441 | if (Function *oldMain = Safe->getNamedFunction("main")) |
| 442 | if (!oldMain->isExternal()) { |
| 443 | // Rename it |
| 444 | oldMain->setName("llvm_bugpoint_old_main"); |
| 445 | // Create a NEW `main' function with same type in the test module. |
| 446 | Function *newMain = new Function(oldMain->getFunctionType(), |
| 447 | GlobalValue::ExternalLinkage, |
| 448 | "main", Test); |
| 449 | // Create an `oldmain' prototype in the test module, which will |
| 450 | // corresponds to the real main function in the same module. |
| 451 | Function *oldMainProto = new Function(oldMain->getFunctionType(), |
| 452 | GlobalValue::ExternalLinkage, |
| 453 | oldMain->getName(), Test); |
| 454 | // Set up and remember the argument list for the main function. |
| 455 | std::vector<Value*> args; |
| 456 | for (Function::aiterator I = newMain->abegin(), E = newMain->aend(), |
| 457 | OI = oldMain->abegin(); I != E; ++I, ++OI) { |
| 458 | I->setName(OI->getName()); // Copy argument names from oldMain |
| 459 | args.push_back(I); |
| 460 | } |
| 461 | |
| 462 | // Call the old main function and return its result |
| 463 | BasicBlock *BB = new BasicBlock("entry", newMain); |
| 464 | CallInst *call = new CallInst(oldMainProto, args); |
| 465 | BB->getInstList().push_back(call); |
| 466 | |
| 467 | // If the type of old function wasn't void, return value of call |
| 468 | new ReturnInst(oldMain->getReturnType() != Type::VoidTy ? call : 0, BB); |
| 469 | } |
| 470 | |
| 471 | // The second nasty issue we must deal with in the JIT is that the Safe |
| 472 | // module cannot directly reference any functions defined in the test |
| 473 | // module. Instead, we use a JIT API call to dynamically resolve the |
| 474 | // symbol. |
| 475 | |
| 476 | // Add the resolver to the Safe module. |
| 477 | // Prototype: void *getPointerToNamedFunction(const char* Name) |
| 478 | Function *resolverFunc = |
| 479 | Safe->getOrInsertFunction("getPointerToNamedFunction", |
| 480 | PointerType::get(Type::SByteTy), |
| 481 | PointerType::get(Type::SByteTy), 0); |
| 482 | |
| 483 | // 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] | 484 | for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 485 | if (F->isExternal() && !F->use_empty() && &*F != resolverFunc && |
| 486 | F->getIntrinsicID() == 0 /* ignore intrinsics */) { |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 487 | Function *TestFn = Test->getFunction(F->getName(), F->getFunctionType()); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 488 | |
| 489 | // Don't forward functions which are external in the test module too. |
| 490 | if (TestFn && !TestFn->isExternal()) { |
| 491 | // 1. Add a string constant with its name to the global file |
| 492 | Constant *InitArray = ConstantArray::get(F->getName()); |
| 493 | GlobalVariable *funcName = |
| 494 | new GlobalVariable(InitArray->getType(), true /*isConstant*/, |
| 495 | GlobalValue::InternalLinkage, InitArray, |
| 496 | F->getName() + "_name", Safe); |
| 497 | |
| 498 | // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an |
| 499 | // sbyte* so it matches the signature of the resolver function. |
| 500 | |
| 501 | // GetElementPtr *funcName, ulong 0, ulong 0 |
| 502 | std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::IntTy)); |
| 503 | Value *GEP = |
| 504 | ConstantExpr::getGetElementPtr(ConstantPointerRef::get(funcName), |
| 505 | GEPargs); |
| 506 | std::vector<Value*> ResolverArgs; |
| 507 | ResolverArgs.push_back(GEP); |
| 508 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 509 | // Rewrite uses of F in global initializers, etc. to uses of a wrapper |
| 510 | // function that dynamically resolves the calls to F via our JIT API |
| 511 | if (F->use_begin() != F->use_end()) { |
| 512 | // Construct a new stub function that will re-route calls to F |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 513 | const FunctionType *FuncTy = F->getFunctionType(); |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 514 | Function *FuncWrapper = new Function(FuncTy, |
| 515 | GlobalValue::InternalLinkage, |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 516 | F->getName() + "_wrapper", |
| 517 | F->getParent()); |
| 518 | BasicBlock *Header = new BasicBlock("header", FuncWrapper); |
| 519 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 520 | // Resolve the call to function F via the JIT API: |
| 521 | // |
| 522 | // call resolver(GetElementPtr...) |
| 523 | CallInst *resolve = new CallInst(resolverFunc, ResolverArgs, |
| 524 | "resolver"); |
| 525 | Header->getInstList().push_back(resolve); |
| 526 | // cast the result from the resolver to correctly-typed function |
| 527 | CastInst *castResolver = |
| 528 | new CastInst(resolve, PointerType::get(F->getFunctionType()), |
| 529 | "resolverCast"); |
| 530 | Header->getInstList().push_back(castResolver); |
| 531 | |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 532 | // Save the argument list |
| 533 | std::vector<Value*> Args; |
| 534 | for (Function::aiterator i = FuncWrapper->abegin(), |
| 535 | e = FuncWrapper->aend(); i != e; ++i) |
| 536 | Args.push_back(i); |
| 537 | |
| 538 | // Pass on the arguments to the real function, return its result |
| 539 | if (F->getReturnType() == Type::VoidTy) { |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 540 | CallInst *Call = new CallInst(castResolver, Args); |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 541 | Header->getInstList().push_back(Call); |
| 542 | ReturnInst *Ret = new ReturnInst(); |
| 543 | Header->getInstList().push_back(Ret); |
| 544 | } else { |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 545 | CallInst *Call = new CallInst(castResolver, Args, "redir"); |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 546 | Header->getInstList().push_back(Call); |
| 547 | ReturnInst *Ret = new ReturnInst(Call); |
| 548 | Header->getInstList().push_back(Ret); |
| 549 | } |
| 550 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 551 | // Use the wrapper function instead of the old function |
| 552 | F->replaceAllUsesWith(FuncWrapper); |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 553 | } |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if (verifyModule(*Test) || verifyModule(*Safe)) { |
| 559 | std::cerr << "Bugpoint has a bug, which corrupted a module!!\n"; |
| 560 | abort(); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | |
| 565 | |
| 566 | /// TestCodeGenerator - This is the predicate function used to check to see if |
| 567 | /// the "Test" portion of the program is miscompiled by the code generator under |
| 568 | /// 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^] | 569 | /// |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 570 | static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) { |
| 571 | CleanupAndPrepareModules(BD, Test, Safe); |
| 572 | |
| 573 | std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc"); |
| 574 | if (BD.writeProgramToFile(TestModuleBC, Test)) { |
| 575 | std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting."; |
| 576 | exit(1); |
| 577 | } |
| 578 | delete Test; |
| 579 | |
| 580 | // Make the shared library |
| 581 | std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc"); |
| 582 | |
| 583 | if (BD.writeProgramToFile(SafeModuleBC, Safe)) { |
| 584 | std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting."; |
| 585 | exit(1); |
| 586 | } |
| 587 | std::string SharedObject = BD.compileSharedObject(SafeModuleBC); |
| 588 | delete Safe; |
| 589 | |
| 590 | // Run the code generator on the `Test' code, loading the shared library. |
| 591 | // The function returns whether or not the new output differs from reference. |
| 592 | int Result = BD.diffProgram(TestModuleBC, SharedObject, false); |
| 593 | |
| 594 | if (Result) |
| 595 | std::cerr << ": still failing!\n"; |
| 596 | else |
| 597 | std::cerr << ": didn't fail.\n"; |
| 598 | removeFile(TestModuleBC); |
| 599 | removeFile(SafeModuleBC); |
| 600 | removeFile(SharedObject); |
| 601 | |
| 602 | return Result; |
| 603 | } |
| 604 | |
| 605 | |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame^] | 606 | /// debugCodeGenerator - debug errors in LLC, LLI, or CBE. |
| 607 | /// |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 608 | bool BugDriver::debugCodeGenerator() { |
| 609 | if ((void*)cbe == (void*)Interpreter) { |
| 610 | std::string Result = executeProgramWithCBE("bugpoint.cbe.out"); |
| 611 | std::cout << "\n*** The C backend cannot match the reference diff, but it " |
| 612 | << "is used as the 'known good'\n code generator, so I can't" |
| 613 | << " debug it. Perhaps you have a front-end problem?\n As a" |
| 614 | << " sanity check, I left the result of executing the program " |
| 615 | << "with the C backend\n in this file for you: '" |
| 616 | << Result << "'.\n"; |
| 617 | return true; |
| 618 | } |
| 619 | |
| 620 | DisambiguateGlobalSymbols(Program); |
| 621 | |
| 622 | std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator); |
| 623 | |
| 624 | // Split the module into the two halves of the program we want. |
| 625 | Module *ToNotCodeGen = CloneModule(getProgram()); |
| 626 | Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs); |
| 627 | |
| 628 | // Condition the modules |
| 629 | CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen); |
| 630 | |
| 631 | std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc"); |
| 632 | if (writeProgramToFile(TestModuleBC, ToCodeGen)) { |
| 633 | std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting."; |
| 634 | exit(1); |
| 635 | } |
| 636 | delete ToCodeGen; |
| 637 | |
| 638 | // Make the shared library |
| 639 | std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc"); |
| 640 | if (writeProgramToFile(SafeModuleBC, ToNotCodeGen)) { |
| 641 | std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting."; |
| 642 | exit(1); |
| 643 | } |
| 644 | std::string SharedObject = compileSharedObject(SafeModuleBC); |
| 645 | delete ToNotCodeGen; |
| 646 | |
| 647 | std::cout << "You can reproduce the problem with the command line: \n"; |
| 648 | if (isExecutingJIT()) { |
| 649 | std::cout << " lli -load " << SharedObject << " " << TestModuleBC; |
| 650 | } else { |
| 651 | std::cout << " llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n"; |
| 652 | std::cout << " gcc " << SharedObject << " " << TestModuleBC |
| 653 | << ".s -o " << TestModuleBC << ".exe -Wl,-R.\n"; |
| 654 | std::cout << " " << TestModuleBC << ".exe"; |
| 655 | } |
| 656 | for (unsigned i=0, e = InputArgv.size(); i != e; ++i) |
| 657 | std::cout << " " << InputArgv[i]; |
| 658 | std::cout << "\n"; |
| 659 | std::cout << "The shared object was created with:\n llc -march=c " |
| 660 | << SafeModuleBC << " -o temporary.c\n" |
| 661 | << " gcc -xc temporary.c -O2 -o " << SharedObject |
| 662 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 663 | << " -G" // Compile a shared library, `-G' for Sparc |
| 664 | #else |
| 665 | << " -shared" // `-shared' for Linux/X86, maybe others |
| 666 | #endif |
| 667 | << " -fno-strict-aliasing\n"; |
| 668 | |
| 669 | return false; |
| 670 | } |