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