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