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