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