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 | // |
| 10 | // This file implements program miscompilation debugging support. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "BugDriver.h" |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 15 | #include "ListReducer.h" |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Utils/Cloning.h" |
| 19 | #include "llvm/Transforms/Utils/Linker.h" |
Misha Brukman | 3d9cafa | 2003-08-07 21:42:28 +0000 | [diff] [blame] | 20 | #include "Support/FileUtilities.h" |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 22 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 23 | namespace { |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 24 | class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> { |
| 25 | BugDriver &BD; |
| 26 | public: |
| 27 | ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} |
| 28 | |
| 29 | virtual TestResult doTest(std::vector<const PassInfo*> &Prefix, |
| 30 | std::vector<const PassInfo*> &Suffix); |
| 31 | }; |
| 32 | } |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 33 | |
| 34 | ReduceMiscompilingPasses::TestResult |
Chris Lattner | 39aebca | 2003-04-24 22:24:22 +0000 | [diff] [blame] | 35 | ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix, |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 36 | std::vector<const PassInfo*> &Suffix) { |
| 37 | // 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] | 38 | // with JUST the kept passes, discard the prefix passes. |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 39 | std::cout << "Checking to see if '" << getPassesString(Suffix) |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 40 | << "' compile correctly: "; |
| 41 | |
| 42 | std::string BytecodeResult; |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 43 | if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) { |
Chris Lattner | 9f71e79 | 2003-10-18 00:05:05 +0000 | [diff] [blame] | 44 | std::cerr << " Error running this sequence of passes" |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 45 | << " on the input program!\n"; |
Chris Lattner | 5ef681c | 2003-10-17 23:07:47 +0000 | [diff] [blame] | 46 | BD.setPassesToRun(Suffix); |
| 47 | BD.EmitProgressBytecode("pass-error", false); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 48 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Check to see if the finished program matches the reference output... |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 52 | if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) { |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 53 | std::cout << "nope.\n"; |
| 54 | return KeepSuffix; // Miscompilation detected! |
| 55 | } |
| 56 | std::cout << "yup.\n"; // No miscompilation! |
| 57 | |
| 58 | if (Prefix.empty()) return NoFailure; |
| 59 | |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 60 | // 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] | 61 | // then separately run the "kept" passes. |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 62 | std::cout << "Checking to see if '" << getPassesString(Prefix) |
| 63 | << "' compile correctly: "; |
| 64 | |
| 65 | // If it is not broken with the kept passes, it's possible that the prefix |
| 66 | // passes must be run before the kept passes to break it. If the program |
| 67 | // WORKS after the prefix passes, but then fails if running the prefix AND |
| 68 | // kept passes, we can update our bytecode file to include the result of the |
| 69 | // prefix passes, then discard the prefix passes. |
| 70 | // |
| 71 | if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) { |
Chris Lattner | 9f71e79 | 2003-10-18 00:05:05 +0000 | [diff] [blame] | 72 | std::cerr << " Error running this sequence of passes" |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 73 | << " on the input program!\n"; |
Chris Lattner | 9c6cfe1 | 2003-10-17 23:03:16 +0000 | [diff] [blame] | 74 | BD.setPassesToRun(Prefix); |
| 75 | BD.EmitProgressBytecode("pass-error", false); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 76 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // If the prefix maintains the predicate by itself, only keep the prefix! |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 80 | if (BD.diffProgram(BytecodeResult)) { |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 81 | std::cout << "nope.\n"; |
| 82 | removeFile(BytecodeResult); |
| 83 | return KeepPrefix; |
| 84 | } |
| 85 | std::cout << "yup.\n"; // No miscompilation! |
| 86 | |
| 87 | // Ok, so now we know that the prefix passes work, try running the suffix |
| 88 | // passes on the result of the prefix passes. |
| 89 | // |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 90 | Module *PrefixOutput = ParseInputFile(BytecodeResult); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 91 | if (PrefixOutput == 0) { |
| 92 | std::cerr << BD.getToolName() << ": Error reading bytecode file '" |
| 93 | << BytecodeResult << "'!\n"; |
| 94 | exit(1); |
| 95 | } |
| 96 | removeFile(BytecodeResult); // No longer need the file on disk |
| 97 | |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 98 | std::cout << "Checking to see if '" << getPassesString(Suffix) |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 99 | << "' passes compile correctly after the '" |
| 100 | << getPassesString(Prefix) << "' passes: "; |
| 101 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 102 | Module *OriginalInput = BD.swapProgramIn(PrefixOutput); |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 103 | if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) { |
Chris Lattner | 9f71e79 | 2003-10-18 00:05:05 +0000 | [diff] [blame] | 104 | std::cerr << " Error running this sequence of passes" |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 105 | << " on the input program!\n"; |
Chris Lattner | 5ef681c | 2003-10-17 23:07:47 +0000 | [diff] [blame] | 106 | BD.setPassesToRun(Suffix); |
Chris Lattner | 9c6cfe1 | 2003-10-17 23:03:16 +0000 | [diff] [blame] | 107 | BD.EmitProgressBytecode("pass-error", false); |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 108 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Run the result... |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 112 | if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) { |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 113 | std::cout << "nope.\n"; |
| 114 | delete OriginalInput; // We pruned down the original input... |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 115 | return KeepSuffix; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Otherwise, we must not be running the bad pass anymore. |
| 119 | std::cout << "yup.\n"; // No miscompilation! |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 120 | delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 121 | return NoFailure; |
| 122 | } |
| 123 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 124 | namespace { |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 125 | class ReduceMiscompilingFunctions : public ListReducer<Function*> { |
| 126 | BugDriver &BD; |
| 127 | public: |
| 128 | ReduceMiscompilingFunctions(BugDriver &bd) : BD(bd) {} |
| 129 | |
| 130 | virtual TestResult doTest(std::vector<Function*> &Prefix, |
| 131 | std::vector<Function*> &Suffix) { |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 132 | if (!Suffix.empty() && TestFuncs(Suffix)) |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 133 | return KeepSuffix; |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 134 | if (!Prefix.empty() && TestFuncs(Prefix)) |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 135 | return KeepPrefix; |
| 136 | return NoFailure; |
| 137 | } |
| 138 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 139 | bool TestFuncs(const std::vector<Function*> &Prefix); |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 140 | }; |
| 141 | } |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 142 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 143 | /// TestMergedProgram - Given two modules, link them together and run the |
| 144 | /// 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^] | 145 | /// matches, return false, otherwise return true. If the DeleteInputs argument |
| 146 | /// is set to true then this function deletes both input modules before it |
| 147 | /// returns. |
| 148 | static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2, |
| 149 | bool DeleteInputs) { |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 150 | // Link the two portions of the program back to together. |
| 151 | std::string ErrorMsg; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame^] | 152 | if (!DeleteInputs) M1 = CloneModule(M1); |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 153 | if (LinkModules(M1, M2, &ErrorMsg)) { |
| 154 | std::cerr << BD.getToolName() << ": Error linking modules together:" |
| 155 | << ErrorMsg << "\n"; |
| 156 | exit(1); |
| 157 | } |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame^] | 158 | if (DeleteInputs) delete M2; // We are done with this module... |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 159 | |
| 160 | Module *OldProgram = BD.swapProgramIn(M1); |
| 161 | |
| 162 | // Execute the program. If it does not match the expected output, we must |
| 163 | // return true. |
| 164 | bool Broken = BD.diffProgram(); |
| 165 | |
| 166 | // Delete the linked module & restore the original |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame^] | 167 | BD.swapProgramIn(OldProgram); |
| 168 | if (DeleteInputs) delete M1; |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 169 | return Broken; |
| 170 | } |
| 171 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 172 | bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){ |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 173 | // Test to see if the function is misoptimized if we ONLY run it on the |
| 174 | // functions listed in Funcs. |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 175 | std::cout << "Checking to see if the program is misoptimized when " |
| 176 | << (Funcs.size()==1 ? "this function is" : "these functions are") |
| 177 | << " run through the pass" |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame^] | 178 | << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":"; |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 179 | PrintFunctionList(Funcs); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 180 | std::cout << "\n"; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 181 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 182 | // Split the module into the two halves of the program we want. |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 183 | Module *ToNotOptimize = CloneModule(BD.getProgram()); |
| 184 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 185 | |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 186 | // Run the optimization passes on ToOptimize, producing a transformed version |
| 187 | // of the functions being tested. |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 188 | std::cout << " Optimizing functions being tested: "; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame^] | 189 | Module *Optimized = BD.runPassesOn(ToOptimize, BD.getPassesToRun(), |
| 190 | /*AutoDebugCrashes*/true); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 191 | std::cout << "done.\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame^] | 192 | delete ToOptimize; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 195 | std::cout << " Checking to see if the merged program executes correctly: "; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame^] | 196 | bool Broken = TestMergedProgram(BD, Optimized, ToNotOptimize, true); |
Chris Lattner | de9750d | 2003-12-07 02:43:09 +0000 | [diff] [blame] | 197 | std::cout << (Broken ? " nope.\n" : " yup.\n"); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 198 | return Broken; |
| 199 | } |
| 200 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame^] | 201 | /// ExtractLoops - Given a reduced list of functions that still exposed the bug, |
| 202 | /// check to see if we can extract the loops in the region without obscuring the |
| 203 | /// bug. If so, it reduces the amount of code identified. |
| 204 | static bool ExtractLoops(BugDriver &BD, |
| 205 | std::vector<Function*> &MiscompiledFunctions) { |
| 206 | bool MadeChange = false; |
| 207 | while (1) { |
| 208 | Module *ToNotOptimize = CloneModule(BD.getProgram()); |
| 209 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, |
| 210 | MiscompiledFunctions); |
| 211 | Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize); |
| 212 | if (!ToOptimizeLoopExtracted) { |
| 213 | // If the loop extractor crashed or if there were no extractible loops, |
| 214 | // then this chapter of our odyssey is over with. |
| 215 | delete ToNotOptimize; |
| 216 | delete ToOptimize; |
| 217 | return MadeChange; |
| 218 | } |
| 219 | |
| 220 | std::cerr << "Extracted a loop from the breaking portion of the program.\n"; |
| 221 | delete ToOptimize; |
| 222 | |
| 223 | // Bugpoint is intentionally not very trusting of LLVM transformations. In |
| 224 | // particular, we're not going to assume that the loop extractor works, so |
| 225 | // we're going to test the newly loop extracted program to make sure nothing |
| 226 | // has broken. If something broke, then we'll inform the user and stop |
| 227 | // extraction. |
| 228 | if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) { |
| 229 | // Merged program doesn't work anymore! |
| 230 | std::cerr << " *** ERROR: Loop extraction broke the program. :(" |
| 231 | << " Please report a bug!\n"; |
| 232 | std::cerr << " Continuing on with un-loop-extracted version.\n"; |
| 233 | delete ToNotOptimize; |
| 234 | delete ToOptimizeLoopExtracted; |
| 235 | return MadeChange; |
| 236 | } |
| 237 | |
| 238 | // Okay, the loop extractor didn't break the program. Run the series of |
| 239 | // optimizations on the loop extracted portion and see if THEY still break |
| 240 | // the program. If so, it was safe to extract these loops! |
| 241 | std::cout << " Running optimizations on loop extracted portion: "; |
| 242 | Module *Optimized = BD.runPassesOn(ToOptimizeLoopExtracted, |
| 243 | BD.getPassesToRun(), |
| 244 | /*AutoDebugCrashes*/true); |
| 245 | std::cout << "done.\n"; |
| 246 | |
| 247 | std::cout << " Checking to see if the merged program executes correctly: "; |
| 248 | bool Broken = TestMergedProgram(BD, Optimized, ToNotOptimize, true); |
| 249 | delete Optimized; |
| 250 | if (!Broken) { |
| 251 | std::cout << "yup: loop extraction masked the problem. Undoing.\n"; |
| 252 | // If the program is not still broken, then loop extraction did something |
| 253 | // that masked the error. Stop loop extraction now. |
| 254 | delete ToNotOptimize; |
| 255 | delete ToOptimizeLoopExtracted; |
| 256 | return MadeChange; |
| 257 | } |
| 258 | std::cout << "nope: loop extraction successful!\n"; |
| 259 | |
| 260 | // Okay, great! Now we know that we extracted a loop and that loop |
| 261 | // extraction both didn't break the program, and didn't mask the problem. |
| 262 | // Replace the current program with the loop extracted version, and try to |
| 263 | // extract another loop. |
| 264 | std::string ErrorMsg; |
| 265 | if (LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)) { |
| 266 | std::cerr << BD.getToolName() << ": Error linking modules together:" |
| 267 | << ErrorMsg << "\n"; |
| 268 | exit(1); |
| 269 | } |
| 270 | delete ToOptimizeLoopExtracted; |
| 271 | BD.setNewProgram(ToNotOptimize); |
| 272 | MadeChange = true; |
| 273 | } |
| 274 | } |
| 275 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 276 | /// debugMiscompilation - This method is used when the passes selected are not |
| 277 | /// crashing, but the generated output is semantically different from the |
| 278 | /// input. |
| 279 | /// |
| 280 | bool BugDriver::debugMiscompilation() { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 281 | // Make sure something was miscompiled... |
Misha Brukman | be6bf56 | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 282 | if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 283 | std::cerr << "*** Optimized program matches reference output! No problem " |
| 284 | << "detected...\nbugpoint can't help you with your problem!\n"; |
| 285 | return false; |
| 286 | } |
| 287 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 288 | std::cout << "\n*** Found miscompiling pass" |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 289 | << (getPassesToRun().size() == 1 ? "" : "es") << ": " |
| 290 | << getPassesString(getPassesToRun()) << "\n"; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 291 | EmitProgressBytecode("passinput"); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 293 | // Okay, now that we have reduced the list of passes which are causing the |
| 294 | // failure, see if we can pin down which functions are being |
| 295 | // miscompiled... first build a list of all of the non-external functions in |
| 296 | // the program. |
| 297 | std::vector<Function*> MiscompiledFunctions; |
| 298 | for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I) |
| 299 | if (!I->isExternal()) |
| 300 | MiscompiledFunctions.push_back(I); |
| 301 | |
| 302 | // Do the reduction... |
| 303 | ReduceMiscompilingFunctions(*this).reduceList(MiscompiledFunctions); |
| 304 | |
Chris Lattner | de9750d | 2003-12-07 02:43:09 +0000 | [diff] [blame] | 305 | std::cout << "\n*** The following function" |
| 306 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 307 | << " being miscompiled: "; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 308 | PrintFunctionList(MiscompiledFunctions); |
| 309 | std::cout << "\n"; |
| 310 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame^] | 311 | // See if we can rip any loops out of the miscompiled functions and still |
| 312 | // trigger the problem. |
| 313 | if (ExtractLoops(*this, MiscompiledFunctions)) { |
| 314 | // Okay, we extracted some loops and the problem still appears. See if we |
| 315 | // can eliminate some of the created functions from being candidates. |
| 316 | |
| 317 | // Do the reduction... |
| 318 | ReduceMiscompilingFunctions(*this).reduceList(MiscompiledFunctions); |
| 319 | |
| 320 | std::cout << "\n*** The following function" |
| 321 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 322 | << " being miscompiled: "; |
| 323 | PrintFunctionList(MiscompiledFunctions); |
| 324 | std::cout << "\n"; |
| 325 | } |
| 326 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 327 | // Output a bunch of bytecode files for the user... |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 328 | std::cout << "Outputting reduced bytecode files which expose the problem:\n"; |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 329 | Module *ToNotOptimize = CloneModule(getProgram()); |
| 330 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, |
| 331 | MiscompiledFunctions); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 332 | |
| 333 | std::cout << " Non-optimized portion: "; |
| 334 | std::swap(Program, ToNotOptimize); |
| 335 | EmitProgressBytecode("tonotoptimize", true); |
| 336 | setNewProgram(ToNotOptimize); // Delete hacked module. |
| 337 | |
| 338 | std::cout << " Portion that is input to optimizer: "; |
| 339 | std::swap(Program, ToOptimize); |
| 340 | EmitProgressBytecode("tooptimize"); |
| 341 | setNewProgram(ToOptimize); // Delete hacked module. |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 343 | return false; |
| 344 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 345 | |