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