Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 1 | //===- Miscompilation.cpp - Debug program miscompilations -----------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 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 | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 10 | // This file implements optimizer and code generation miscompilation debugging |
| 11 | // support. |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "BugDriver.h" |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 16 | #include "ListReducer.h" |
Daniel Dunbar | 8575a60 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 17 | #include "ToolRunner.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 18 | #include "llvm/Config/config.h" // for HAVE_LINK_R |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/DerivedTypes.h" |
Rafael Espindola | f49a38f | 2015-12-04 22:08:53 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DiagnosticPrinter.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Instructions.h" |
| 23 | #include "llvm/IR/Module.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 6cc07df | 2014-03-06 03:42:23 +0000 | [diff] [blame] | 25 | #include "llvm/Linker/Linker.h" |
Misha Brukman | 0c2305b | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/FileUtilities.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 31 | |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 32 | namespace llvm { |
Daniel Dunbar | a53337f | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 33 | extern cl::opt<std::string> OutputPrefix; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 34 | extern cl::list<std::string> InputArgv; |
| 35 | } |
| 36 | |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 37 | namespace { |
Michael J. Spencer | 3df5c04 | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 38 | static llvm::cl::opt<bool> |
| 39 | DisableLoopExtraction("disable-loop-extraction", |
Reid Spencer | 471bcb7 | 2006-11-11 19:05:02 +0000 | [diff] [blame] | 40 | cl::desc("Don't extract loops when searching for miscompilations"), |
| 41 | cl::init(false)); |
Michael J. Spencer | 3df5c04 | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 42 | static llvm::cl::opt<bool> |
| 43 | DisableBlockExtraction("disable-block-extraction", |
David Goodwin | 2c04ff6 | 2009-07-28 23:08:36 +0000 | [diff] [blame] | 44 | cl::desc("Don't extract blocks when searching for miscompilations"), |
| 45 | cl::init(false)); |
Reid Spencer | 471bcb7 | 2006-11-11 19:05:02 +0000 | [diff] [blame] | 46 | |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 47 | class ReduceMiscompilingPasses : public ListReducer<std::string> { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 48 | BugDriver &BD; |
| 49 | public: |
| 50 | ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 51 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 52 | TestResult doTest(std::vector<std::string> &Prefix, |
| 53 | std::vector<std::string> &Suffix, |
| 54 | std::string &Error) override; |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 55 | }; |
| 56 | } |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 57 | |
Misha Brukman | 0784a60 | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 58 | /// TestResult - After passes have been split into a test group and a control |
| 59 | /// group, see if they still break the program. |
| 60 | /// |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 61 | ReduceMiscompilingPasses::TestResult |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 62 | ReduceMiscompilingPasses::doTest(std::vector<std::string> &Prefix, |
| 63 | std::vector<std::string> &Suffix, |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 64 | std::string &Error) { |
Chris Lattner | 4a1a9d2 | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 65 | // First, run the program with just the Suffix passes. If it is still broken |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 66 | // with JUST the kept passes, discard the prefix passes. |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 67 | outs() << "Checking to see if '" << getPassesString(Suffix) |
| 68 | << "' compiles correctly: "; |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 69 | |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 70 | std::string BitcodeResult; |
Rafael Espindola | 315190b | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 71 | if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false/*delete*/, |
| 72 | true/*quiet*/)) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 73 | errs() << " Error running this sequence of passes" |
| 74 | << " on the input program!\n"; |
Chris Lattner | 92a9f7a | 2003-10-17 23:07:47 +0000 | [diff] [blame] | 75 | BD.setPassesToRun(Suffix); |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 76 | BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 77 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 78 | } |
Michael J. Spencer | 3df5c04 | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 80 | // Check to see if the finished program matches the reference output... |
Rafael Espindola | c89b1ef | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 81 | bool Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", |
| 82 | true /*delete bitcode*/, &Error); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 83 | if (!Error.empty()) |
| 84 | return InternalError; |
| 85 | if (Diff) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 86 | outs() << " nope.\n"; |
Chris Lattner | ea2fa46 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 87 | if (Suffix.empty()) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 88 | errs() << BD.getToolName() << ": I'm confused: the test fails when " |
| 89 | << "no passes are run, nondeterministic program?\n"; |
Chris Lattner | ea2fa46 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 90 | exit(1); |
| 91 | } |
Misha Brukman | 324e9e0 | 2004-04-22 20:02:09 +0000 | [diff] [blame] | 92 | return KeepSuffix; // Miscompilation detected! |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 93 | } |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 94 | outs() << " yup.\n"; // No miscompilation! |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 95 | |
| 96 | if (Prefix.empty()) return NoFailure; |
| 97 | |
Chris Lattner | 4a1a9d2 | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 98 | // Next, see if the program is broken if we run the "prefix" passes first, |
Misha Brukman | 7fdaab4 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 99 | // then separately run the "kept" passes. |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 100 | outs() << "Checking to see if '" << getPassesString(Prefix) |
| 101 | << "' compiles correctly: "; |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 102 | |
| 103 | // If it is not broken with the kept passes, it's possible that the prefix |
| 104 | // passes must be run before the kept passes to break it. If the program |
| 105 | // WORKS after the prefix passes, but then fails if running the prefix AND |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 106 | // kept passes, we can update our bitcode file to include the result of the |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 107 | // prefix passes, then discard the prefix passes. |
| 108 | // |
Rafael Espindola | 315190b | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 109 | if (BD.runPasses(BD.getProgram(), Prefix, BitcodeResult, false/*delete*/, |
| 110 | true/*quiet*/)) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 111 | errs() << " Error running this sequence of passes" |
| 112 | << " on the input program!\n"; |
Chris Lattner | 7d147ae | 2003-10-17 23:03:16 +0000 | [diff] [blame] | 113 | BD.setPassesToRun(Prefix); |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 114 | BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 115 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // If the prefix maintains the predicate by itself, only keep the prefix! |
Rafael Espindola | c89b1ef | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 119 | Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", false, &Error); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 120 | if (!Error.empty()) |
| 121 | return InternalError; |
| 122 | if (Diff) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 123 | outs() << " nope.\n"; |
Rafael Espindola | 1807958 | 2013-06-17 21:50:28 +0000 | [diff] [blame] | 124 | sys::fs::remove(BitcodeResult); |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 125 | return KeepPrefix; |
| 126 | } |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 127 | outs() << " yup.\n"; // No miscompilation! |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 128 | |
| 129 | // Ok, so now we know that the prefix passes work, try running the suffix |
| 130 | // passes on the result of the prefix passes. |
| 131 | // |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 132 | std::unique_ptr<Module> PrefixOutput = |
| 133 | parseInputFile(BitcodeResult, BD.getContext()); |
David Blaikie | 041f1aa | 2013-05-15 07:36:59 +0000 | [diff] [blame] | 134 | if (!PrefixOutput) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 135 | errs() << BD.getToolName() << ": Error reading bitcode file '" |
| 136 | << BitcodeResult << "'!\n"; |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 137 | exit(1); |
| 138 | } |
Rafael Espindola | 1807958 | 2013-06-17 21:50:28 +0000 | [diff] [blame] | 139 | sys::fs::remove(BitcodeResult); |
Chris Lattner | 02cea4a | 2004-04-23 20:36:51 +0000 | [diff] [blame] | 140 | |
| 141 | // Don't check if there are no passes in the suffix. |
| 142 | if (Suffix.empty()) |
| 143 | return NoFailure; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 144 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 145 | outs() << "Checking to see if '" << getPassesString(Suffix) |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 146 | << "' passes compile correctly after the '" |
| 147 | << getPassesString(Prefix) << "' passes: "; |
| 148 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 149 | std::unique_ptr<Module> OriginalInput( |
| 150 | BD.swapProgramIn(PrefixOutput.release())); |
Rafael Espindola | 315190b | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 151 | if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false/*delete*/, |
| 152 | true/*quiet*/)) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 153 | errs() << " Error running this sequence of passes" |
| 154 | << " on the input program!\n"; |
Chris Lattner | 92a9f7a | 2003-10-17 23:07:47 +0000 | [diff] [blame] | 155 | BD.setPassesToRun(Suffix); |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 156 | BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 157 | exit(BD.debugOptimizerCrash()); |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Run the result... |
Rafael Espindola | c89b1ef | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 161 | Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", |
| 162 | true /*delete bitcode*/, &Error); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 163 | if (!Error.empty()) |
| 164 | return InternalError; |
| 165 | if (Diff) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 166 | outs() << " nope.\n"; |
Chris Lattner | 4a1a9d2 | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 167 | return KeepSuffix; |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Otherwise, we must not be running the bad pass anymore. |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 171 | outs() << " yup.\n"; // No miscompilation! |
Jeffrey Yasskin | e476fa0 | 2010-05-11 23:25:16 +0000 | [diff] [blame] | 172 | // Restore orig program & free test. |
Ahmed Charles | 96c9d95 | 2014-03-05 10:19:29 +0000 | [diff] [blame] | 173 | delete BD.swapProgramIn(OriginalInput.release()); |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 174 | return NoFailure; |
| 175 | } |
| 176 | |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 177 | namespace { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 178 | class ReduceMiscompilingFunctions : public ListReducer<Function*> { |
| 179 | BugDriver &BD; |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 180 | bool (*TestFn)(BugDriver &, std::unique_ptr<Module>, |
| 181 | std::unique_ptr<Module>, std::string &); |
| 182 | |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 183 | public: |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 184 | ReduceMiscompilingFunctions(BugDriver &bd, |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 185 | bool (*F)(BugDriver &, std::unique_ptr<Module>, |
| 186 | std::unique_ptr<Module>, |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 187 | std::string &)) |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 188 | : BD(bd), TestFn(F) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 189 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 190 | TestResult doTest(std::vector<Function*> &Prefix, |
| 191 | std::vector<Function*> &Suffix, |
| 192 | std::string &Error) override { |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 193 | if (!Suffix.empty()) { |
| 194 | bool Ret = TestFuncs(Suffix, Error); |
| 195 | if (!Error.empty()) |
| 196 | return InternalError; |
| 197 | if (Ret) |
| 198 | return KeepSuffix; |
| 199 | } |
| 200 | if (!Prefix.empty()) { |
| 201 | bool Ret = TestFuncs(Prefix, Error); |
| 202 | if (!Error.empty()) |
| 203 | return InternalError; |
| 204 | if (Ret) |
| 205 | return KeepPrefix; |
| 206 | } |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 207 | return NoFailure; |
| 208 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 209 | |
Rafael Espindola | 235457c | 2010-07-26 00:07:51 +0000 | [diff] [blame] | 210 | bool TestFuncs(const std::vector<Function*> &Prefix, std::string &Error); |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 211 | }; |
| 212 | } |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 213 | |
Rafael Espindola | f49a38f | 2015-12-04 22:08:53 +0000 | [diff] [blame] | 214 | static void diagnosticHandler(const DiagnosticInfo &DI) { |
| 215 | DiagnosticPrinterRawOStream DP(errs()); |
| 216 | DI.print(DP); |
| 217 | errs() << '\n'; |
| 218 | if (DI.getSeverity() == DS_Error) |
| 219 | exit(1); |
| 220 | } |
| 221 | |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 222 | /// TestMergedProgram - Given two modules, link them together and run the |
Rafael Espindola | e490460 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 223 | /// program, checking to see if the program matches the diff. If there is |
| 224 | /// an error, return NULL. If not, return the merged module. The Broken argument |
| 225 | /// will be set to true if the output is different. If the DeleteInputs |
| 226 | /// argument is set to true then this function deletes both input |
| 227 | /// modules before it returns. |
Misha Brukman | 0784a60 | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 228 | /// |
Rafael Espindola | e490460 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 229 | static Module *TestMergedProgram(const BugDriver &BD, Module *M1, Module *M2, |
| 230 | bool DeleteInputs, std::string &Error, |
| 231 | bool &Broken) { |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 232 | // Link the two portions of the program back to together. |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 233 | if (!DeleteInputs) { |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 234 | M1 = CloneModule(M1).release(); |
| 235 | M2 = CloneModule(M2).release(); |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 236 | } |
Rafael Espindola | f49a38f | 2015-12-04 22:08:53 +0000 | [diff] [blame] | 237 | if (Linker::linkModules(*M1, *M2, diagnosticHandler)) |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 238 | exit(1); |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 239 | delete M2; // We are done with this module. |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 240 | |
Rafael Espindola | e490460 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 241 | // Execute the program. |
| 242 | Broken = BD.diffProgram(M1, "", "", false, &Error); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 243 | if (!Error.empty()) { |
Rafael Espindola | c89b1ef | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 244 | // Delete the linked module |
| 245 | delete M1; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 246 | return nullptr; |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 247 | } |
Rafael Espindola | e490460 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 248 | return M1; |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Misha Brukman | 0784a60 | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 251 | /// TestFuncs - split functions in a Module into two groups: those that are |
| 252 | /// under consideration for miscompilation vs. those that are not, and test |
| 253 | /// accordingly. Each group of functions becomes a separate Module. |
| 254 | /// |
Rafael Espindola | 235457c | 2010-07-26 00:07:51 +0000 | [diff] [blame] | 255 | bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs, |
| 256 | std::string &Error) { |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 257 | // Test to see if the function is misoptimized if we ONLY run it on the |
| 258 | // functions listed in Funcs. |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 259 | outs() << "Checking to see if the program is misoptimized when " |
| 260 | << (Funcs.size()==1 ? "this function is" : "these functions are") |
| 261 | << " run through the pass" |
| 262 | << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":"; |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 263 | PrintFunctionList(Funcs); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 264 | outs() << '\n'; |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 265 | |
Rafael Espindola | 235457c | 2010-07-26 00:07:51 +0000 | [diff] [blame] | 266 | // Create a clone for two reasons: |
| 267 | // * If the optimization passes delete any function, the deleted function |
| 268 | // will be in the clone and Funcs will still point to valid memory |
| 269 | // * If the optimization passes use interprocedural information to break |
| 270 | // a function, we want to continue with the original function. Otherwise |
| 271 | // we can conclude that a function triggers the bug when in fact one |
| 272 | // needs a larger set of original functions to do so. |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 273 | ValueToValueMapTy VMap; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 274 | Module *Clone = CloneModule(BD.getProgram(), VMap).release(); |
Rafael Espindola | 235457c | 2010-07-26 00:07:51 +0000 | [diff] [blame] | 275 | Module *Orig = BD.swapProgramIn(Clone); |
| 276 | |
| 277 | std::vector<Function*> FuncsOnClone; |
| 278 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
| 279 | Function *F = cast<Function>(VMap[Funcs[i]]); |
| 280 | FuncsOnClone.push_back(F); |
| 281 | } |
| 282 | |
| 283 | // Split the module into the two halves of the program we want. |
| 284 | VMap.clear(); |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 285 | std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap); |
| 286 | std::unique_ptr<Module> ToOptimize = |
| 287 | SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap); |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 288 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 289 | bool Broken = |
| 290 | TestFn(BD, std::move(ToOptimize), std::move(ToNotOptimize), Error); |
Rafael Espindola | 235457c | 2010-07-26 00:07:51 +0000 | [diff] [blame] | 291 | |
| 292 | delete BD.swapProgramIn(Orig); |
| 293 | |
| 294 | return Broken; |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Chris Lattner | dc2cb5a | 2010-01-16 21:34:01 +0000 | [diff] [blame] | 297 | /// DisambiguateGlobalSymbols - Give anonymous global values names. |
Misha Brukman | 0784a60 | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 298 | /// |
Chris Lattner | dfbb1d1 | 2004-04-11 23:52:35 +0000 | [diff] [blame] | 299 | static void DisambiguateGlobalSymbols(Module *M) { |
Chris Lattner | 34d26c3 | 2006-05-04 23:35:31 +0000 | [diff] [blame] | 300 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
Chris Lattner | 6a941f0 | 2010-01-16 21:34:51 +0000 | [diff] [blame] | 301 | I != E; ++I) |
Chris Lattner | dc2cb5a | 2010-01-16 21:34:01 +0000 | [diff] [blame] | 302 | if (!I->hasName()) |
| 303 | I->setName("anon_global"); |
Chris Lattner | 6a941f0 | 2010-01-16 21:34:51 +0000 | [diff] [blame] | 304 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
Chris Lattner | dc2cb5a | 2010-01-16 21:34:01 +0000 | [diff] [blame] | 305 | if (!I->hasName()) |
| 306 | I->setName("anon_fn"); |
Chris Lattner | dfbb1d1 | 2004-04-11 23:52:35 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 309 | /// Given a reduced list of functions that still exposed the bug, check to see |
| 310 | /// if we can extract the loops in the region without obscuring the bug. If so, |
| 311 | /// it reduces the amount of code identified. |
Misha Brukman | 0784a60 | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 312 | /// |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 313 | static bool ExtractLoops(BugDriver &BD, |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 314 | bool (*TestFn)(BugDriver &, std::unique_ptr<Module>, |
| 315 | std::unique_ptr<Module>, std::string &), |
| 316 | std::vector<Function *> &MiscompiledFunctions, |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 317 | std::string &Error) { |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 318 | bool MadeChange = false; |
| 319 | while (1) { |
Chris Lattner | bd615f5 | 2005-08-02 23:25:56 +0000 | [diff] [blame] | 320 | if (BugpointIsInterrupted) return MadeChange; |
Michael J. Spencer | 3df5c04 | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 321 | |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 322 | ValueToValueMapTy VMap; |
Rafael Espindola | 000bf49 | 2015-12-09 00:18:41 +0000 | [diff] [blame] | 323 | std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap); |
| 324 | Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize.get(), |
Rafael Espindola | 50102c2 | 2015-12-09 00:34:10 +0000 | [diff] [blame] | 325 | MiscompiledFunctions, VMap) |
| 326 | .release(); |
Rafael Espindola | 88a8f72 | 2015-12-09 00:08:22 +0000 | [diff] [blame] | 327 | std::unique_ptr<Module> ToOptimizeLoopExtracted = |
| 328 | BD.extractLoop(ToOptimize); |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 329 | if (!ToOptimizeLoopExtracted) { |
| 330 | // If the loop extractor crashed or if there were no extractible loops, |
| 331 | // then this chapter of our odyssey is over with. |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 332 | delete ToOptimize; |
| 333 | return MadeChange; |
| 334 | } |
| 335 | |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 336 | errs() << "Extracted a loop from the breaking portion of the program.\n"; |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 337 | |
| 338 | // Bugpoint is intentionally not very trusting of LLVM transformations. In |
| 339 | // particular, we're not going to assume that the loop extractor works, so |
| 340 | // we're going to test the newly loop extracted program to make sure nothing |
| 341 | // has broken. If something broke, then we'll inform the user and stop |
| 342 | // extraction. |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 343 | AbstractInterpreter *AI = BD.switchToSafeInterpreter(); |
Rafael Espindola | e490460 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 344 | bool Failure; |
Rafael Espindola | 88a8f72 | 2015-12-09 00:08:22 +0000 | [diff] [blame] | 345 | Module *New = TestMergedProgram(BD, ToOptimizeLoopExtracted.get(), |
Rafael Espindola | 000bf49 | 2015-12-09 00:18:41 +0000 | [diff] [blame] | 346 | ToNotOptimize.get(), false, Error, Failure); |
Rafael Espindola | e490460 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 347 | if (!New) |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 348 | return false; |
Hal Finkel | e9efbf1 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 349 | |
Rafael Espindola | e490460 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 350 | // Delete the original and set the new program. |
Hal Finkel | e9efbf1 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 351 | Module *Old = BD.swapProgramIn(New); |
| 352 | for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) |
| 353 | MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]); |
| 354 | delete Old; |
| 355 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 356 | if (Failure) { |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 357 | BD.switchToInterpreter(AI); |
| 358 | |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 359 | // Merged program doesn't work anymore! |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 360 | errs() << " *** ERROR: Loop extraction broke the program. :(" |
| 361 | << " Please report a bug!\n"; |
| 362 | errs() << " Continuing on with un-loop-extracted version.\n"; |
Chris Lattner | 257008b | 2005-05-08 21:54:56 +0000 | [diff] [blame] | 363 | |
Daniel Dunbar | a53337f | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 364 | BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc", |
Rafael Espindola | 000bf49 | 2015-12-09 00:18:41 +0000 | [diff] [blame] | 365 | ToNotOptimize.get()); |
Daniel Dunbar | a53337f | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 366 | BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc", |
| 367 | ToOptimize); |
| 368 | BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc", |
Rafael Espindola | 88a8f72 | 2015-12-09 00:08:22 +0000 | [diff] [blame] | 369 | ToOptimizeLoopExtracted.get()); |
Chris Lattner | 257008b | 2005-05-08 21:54:56 +0000 | [diff] [blame] | 370 | |
Michael J. Spencer | 3df5c04 | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 371 | errs() << "Please submit the " |
Daniel Dunbar | a53337f | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 372 | << OutputPrefix << "-loop-extract-fail-*.bc files.\n"; |
Chris Lattner | 257008b | 2005-05-08 21:54:56 +0000 | [diff] [blame] | 373 | delete ToOptimize; |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 374 | return MadeChange; |
| 375 | } |
Chris Lattner | 257008b | 2005-05-08 21:54:56 +0000 | [diff] [blame] | 376 | delete ToOptimize; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 377 | BD.switchToInterpreter(AI); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 378 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 379 | outs() << " Testing after loop extraction:\n"; |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 380 | // Clone modules, the tester function will free them. |
Rafael Espindola | 88a8f72 | 2015-12-09 00:08:22 +0000 | [diff] [blame] | 381 | std::unique_ptr<Module> TOLEBackup = |
| 382 | CloneModule(ToOptimizeLoopExtracted.get(), VMap); |
Rafael Espindola | 000bf49 | 2015-12-09 00:18:41 +0000 | [diff] [blame] | 383 | std::unique_ptr<Module> TNOBackup = CloneModule(ToNotOptimize.get(), VMap); |
Hal Finkel | e9efbf1 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 384 | |
| 385 | for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) |
| 386 | MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]); |
| 387 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 388 | Failure = TestFn(BD, std::move(ToOptimizeLoopExtracted), |
| 389 | std::move(ToNotOptimize), Error); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 390 | if (!Error.empty()) |
| 391 | return false; |
Hal Finkel | e9efbf1 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 392 | |
Rafael Espindola | 88a8f72 | 2015-12-09 00:08:22 +0000 | [diff] [blame] | 393 | ToOptimizeLoopExtracted = std::move(TOLEBackup); |
Rafael Espindola | 000bf49 | 2015-12-09 00:18:41 +0000 | [diff] [blame] | 394 | ToNotOptimize = std::move(TNOBackup); |
Hal Finkel | e9efbf1 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 395 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 396 | if (!Failure) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 397 | outs() << "*** Loop extraction masked the problem. Undoing.\n"; |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 398 | // If the program is not still broken, then loop extraction did something |
| 399 | // that masked the error. Stop loop extraction now. |
Hal Finkel | e9efbf1 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 400 | |
| 401 | std::vector<std::pair<std::string, FunctionType*> > MisCompFunctions; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 402 | for (Function *F : MiscompiledFunctions) { |
| 403 | MisCompFunctions.emplace_back(F->getName(), F->getFunctionType()); |
Hal Finkel | e9efbf1 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Rafael Espindola | f49a38f | 2015-12-04 22:08:53 +0000 | [diff] [blame] | 406 | if (Linker::linkModules(*ToNotOptimize, *ToOptimizeLoopExtracted, |
| 407 | diagnosticHandler)) |
Hal Finkel | e9efbf1 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 408 | exit(1); |
Hal Finkel | e9efbf1 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 409 | |
| 410 | MiscompiledFunctions.clear(); |
| 411 | for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { |
| 412 | Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first); |
| 413 | |
| 414 | assert(NewF && "Function not found??"); |
| 415 | MiscompiledFunctions.push_back(NewF); |
| 416 | } |
| 417 | |
Rafael Espindola | 000bf49 | 2015-12-09 00:18:41 +0000 | [diff] [blame] | 418 | BD.setNewProgram(ToNotOptimize.release()); |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 419 | return MadeChange; |
| 420 | } |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 421 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 422 | outs() << "*** Loop extraction successful!\n"; |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 424 | std::vector<std::pair<std::string, FunctionType*> > MisCompFunctions; |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 425 | for (Module::iterator I = ToOptimizeLoopExtracted->begin(), |
| 426 | E = ToOptimizeLoopExtracted->end(); I != E; ++I) |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 427 | if (!I->isDeclaration()) |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 428 | MisCompFunctions.emplace_back(I->getName(), I->getFunctionType()); |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 429 | |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 430 | // Okay, great! Now we know that we extracted a loop and that loop |
| 431 | // extraction both didn't break the program, and didn't mask the problem. |
| 432 | // Replace the current program with the loop extracted version, and try to |
| 433 | // extract another loop. |
Rafael Espindola | f49a38f | 2015-12-04 22:08:53 +0000 | [diff] [blame] | 434 | if (Linker::linkModules(*ToNotOptimize, *ToOptimizeLoopExtracted, |
| 435 | diagnosticHandler)) |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 436 | exit(1); |
Rafael Espindola | d12b4a3 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 437 | |
Chris Lattner | b38cc9c | 2004-03-17 17:42:09 +0000 | [diff] [blame] | 438 | // All of the Function*'s in the MiscompiledFunctions list are in the old |
Chris Lattner | a413b08 | 2004-04-02 06:32:17 +0000 | [diff] [blame] | 439 | // module. Update this list to include all of the functions in the |
| 440 | // optimized and loop extracted module. |
| 441 | MiscompiledFunctions.clear(); |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 442 | for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 443 | Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first); |
Michael J. Spencer | 3df5c04 | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 444 | |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 445 | assert(NewF && "Function not found??"); |
| 446 | MiscompiledFunctions.push_back(NewF); |
Chris Lattner | b38cc9c | 2004-03-17 17:42:09 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Rafael Espindola | 000bf49 | 2015-12-09 00:18:41 +0000 | [diff] [blame] | 449 | BD.setNewProgram(ToNotOptimize.release()); |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 450 | MadeChange = true; |
| 451 | } |
| 452 | } |
| 453 | |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 454 | namespace { |
| 455 | class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> { |
| 456 | BugDriver &BD; |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 457 | bool (*TestFn)(BugDriver &, std::unique_ptr<Module>, |
| 458 | std::unique_ptr<Module>, std::string &); |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 459 | std::vector<Function*> FunctionsBeingTested; |
| 460 | public: |
| 461 | ReduceMiscompiledBlocks(BugDriver &bd, |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 462 | bool (*F)(BugDriver &, std::unique_ptr<Module>, |
| 463 | std::unique_ptr<Module>, std::string &), |
| 464 | const std::vector<Function *> &Fns) |
| 465 | : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 466 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 467 | TestResult doTest(std::vector<BasicBlock*> &Prefix, |
| 468 | std::vector<BasicBlock*> &Suffix, |
| 469 | std::string &Error) override { |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 470 | if (!Suffix.empty()) { |
| 471 | bool Ret = TestFuncs(Suffix, Error); |
| 472 | if (!Error.empty()) |
| 473 | return InternalError; |
| 474 | if (Ret) |
| 475 | return KeepSuffix; |
| 476 | } |
| 477 | if (!Prefix.empty()) { |
| 478 | bool Ret = TestFuncs(Prefix, Error); |
| 479 | if (!Error.empty()) |
| 480 | return InternalError; |
| 481 | if (Ret) |
| 482 | return KeepPrefix; |
| 483 | } |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 484 | return NoFailure; |
| 485 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 486 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 487 | bool TestFuncs(const std::vector<BasicBlock*> &BBs, std::string &Error); |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 488 | }; |
| 489 | } |
| 490 | |
| 491 | /// TestFuncs - Extract all blocks for the miscompiled functions except for the |
| 492 | /// specified blocks. If the problem still exists, return true. |
| 493 | /// |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 494 | bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs, |
| 495 | std::string &Error) { |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 496 | // Test to see if the function is misoptimized if we ONLY run it on the |
| 497 | // functions listed in Funcs. |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 498 | outs() << "Checking to see if the program is misoptimized when all "; |
Chris Lattner | 6c0c313 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 499 | if (!BBs.empty()) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 500 | outs() << "but these " << BBs.size() << " blocks are extracted: "; |
Chris Lattner | 6c0c313 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 501 | for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 502 | outs() << BBs[i]->getName() << " "; |
| 503 | if (BBs.size() > 10) outs() << "..."; |
Chris Lattner | 6c0c313 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 504 | } else { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 505 | outs() << "blocks are extracted."; |
Chris Lattner | 6c0c313 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 506 | } |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 507 | outs() << '\n'; |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 508 | |
| 509 | // Split the module into the two halves of the program we want. |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 510 | ValueToValueMapTy VMap; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 511 | Module *Clone = CloneModule(BD.getProgram(), VMap).release(); |
Rafael Espindola | 07035e6 | 2010-07-29 14:20:59 +0000 | [diff] [blame] | 512 | Module *Orig = BD.swapProgramIn(Clone); |
| 513 | std::vector<Function*> FuncsOnClone; |
| 514 | std::vector<BasicBlock*> BBsOnClone; |
| 515 | for (unsigned i = 0, e = FunctionsBeingTested.size(); i != e; ++i) { |
| 516 | Function *F = cast<Function>(VMap[FunctionsBeingTested[i]]); |
| 517 | FuncsOnClone.push_back(F); |
| 518 | } |
| 519 | for (unsigned i = 0, e = BBs.size(); i != e; ++i) { |
| 520 | BasicBlock *BB = cast<BasicBlock>(VMap[BBs[i]]); |
| 521 | BBsOnClone.push_back(BB); |
| 522 | } |
| 523 | VMap.clear(); |
| 524 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 525 | std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap); |
| 526 | std::unique_ptr<Module> ToOptimize = |
| 527 | SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap); |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 528 | |
| 529 | // Try the extraction. If it doesn't work, then the block extractor crashed |
| 530 | // or something, in which case bugpoint can't chase down this possibility. |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 531 | if (std::unique_ptr<Module> New = |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 532 | BD.extractMappedBlocksFromModule(BBsOnClone, ToOptimize.get())) { |
| 533 | bool Ret = TestFn(BD, std::move(New), std::move(ToNotOptimize), Error); |
Rafael Espindola | 07035e6 | 2010-07-29 14:20:59 +0000 | [diff] [blame] | 534 | delete BD.swapProgramIn(Orig); |
| 535 | return Ret; |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 536 | } |
Rafael Espindola | 07035e6 | 2010-07-29 14:20:59 +0000 | [diff] [blame] | 537 | delete BD.swapProgramIn(Orig); |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 541 | /// Given a reduced list of functions that still expose the bug, extract as many |
| 542 | /// basic blocks from the region as possible without obscuring the bug. |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 543 | /// |
| 544 | static bool ExtractBlocks(BugDriver &BD, |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 545 | bool (*TestFn)(BugDriver &, std::unique_ptr<Module>, |
| 546 | std::unique_ptr<Module>, |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 547 | std::string &), |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 548 | std::vector<Function *> &MiscompiledFunctions, |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 549 | std::string &Error) { |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 550 | if (BugpointIsInterrupted) return false; |
Michael J. Spencer | 3df5c04 | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 551 | |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 552 | std::vector<BasicBlock*> Blocks; |
| 553 | for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 554 | for (BasicBlock &BB : *MiscompiledFunctions[i]) |
| 555 | Blocks.push_back(&BB); |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 556 | |
| 557 | // Use the list reducer to identify blocks that can be extracted without |
| 558 | // obscuring the bug. The Blocks list will end up containing blocks that must |
| 559 | // be retained from the original program. |
| 560 | unsigned OldSize = Blocks.size(); |
Chris Lattner | 6c0c313 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 561 | |
| 562 | // Check to see if all blocks are extractible first. |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 563 | bool Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions) |
| 564 | .TestFuncs(std::vector<BasicBlock*>(), Error); |
| 565 | if (!Error.empty()) |
| 566 | return false; |
| 567 | if (Ret) { |
Chris Lattner | 6c0c313 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 568 | Blocks.clear(); |
| 569 | } else { |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 570 | ReduceMiscompiledBlocks(BD, TestFn, |
| 571 | MiscompiledFunctions).reduceList(Blocks, Error); |
| 572 | if (!Error.empty()) |
| 573 | return false; |
Chris Lattner | 6c0c313 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 574 | if (Blocks.size() == OldSize) |
| 575 | return false; |
| 576 | } |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 577 | |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 578 | ValueToValueMapTy VMap; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 579 | Module *ProgClone = CloneModule(BD.getProgram(), VMap).release(); |
Rafael Espindola | 50102c2 | 2015-12-09 00:34:10 +0000 | [diff] [blame] | 580 | Module *ToExtract = |
| 581 | SplitFunctionsOutOfModule(ProgClone, MiscompiledFunctions, VMap) |
| 582 | .release(); |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 583 | std::unique_ptr<Module> Extracted = |
| 584 | BD.extractMappedBlocksFromModule(Blocks, ToExtract); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 585 | if (!Extracted) { |
Chris Lattner | 0ce80cd | 2005-02-27 06:18:25 +0000 | [diff] [blame] | 586 | // Weird, extraction should have worked. |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 587 | errs() << "Nondeterministic problem extracting blocks??\n"; |
Chris Lattner | 49e4a33 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 588 | delete ProgClone; |
| 589 | delete ToExtract; |
| 590 | return false; |
| 591 | } |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 49e4a33 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 593 | // Otherwise, block extraction succeeded. Link the two program fragments back |
| 594 | // together. |
| 595 | delete ToExtract; |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 597 | std::vector<std::pair<std::string, FunctionType*> > MisCompFunctions; |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 598 | for (Module::iterator I = Extracted->begin(), E = Extracted->end(); |
| 599 | I != E; ++I) |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 600 | if (!I->isDeclaration()) |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 601 | MisCompFunctions.emplace_back(I->getName(), I->getFunctionType()); |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 602 | |
Rafael Espindola | f49a38f | 2015-12-04 22:08:53 +0000 | [diff] [blame] | 603 | if (Linker::linkModules(*ProgClone, *Extracted, diagnosticHandler)) |
Chris Lattner | 49e4a33 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 604 | exit(1); |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 49e4a33 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 606 | // Set the new program and delete the old one. |
| 607 | BD.setNewProgram(ProgClone); |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 49e4a33 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 609 | // Update the list of miscompiled functions. |
| 610 | MiscompiledFunctions.clear(); |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 611 | |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 612 | for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 613 | Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first); |
Chris Lattner | d308797 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 614 | assert(NewF && "Function not found??"); |
| 615 | MiscompiledFunctions.push_back(NewF); |
| 616 | } |
Chris Lattner | 49e4a33 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 617 | |
| 618 | return true; |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 621 | /// This is a generic driver to narrow down miscompilations, either in an |
| 622 | /// optimization or a code generator. |
Misha Brukman | 0784a60 | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 623 | /// |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 624 | static std::vector<Function *> |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 625 | DebugAMiscompilation(BugDriver &BD, |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 626 | bool (*TestFn)(BugDriver &, std::unique_ptr<Module>, |
| 627 | std::unique_ptr<Module>, std::string &), |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 628 | std::string &Error) { |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 629 | // Okay, now that we have reduced the list of passes which are causing the |
| 630 | // failure, see if we can pin down which functions are being |
| 631 | // miscompiled... first build a list of all of the non-external functions in |
| 632 | // the program. |
| 633 | std::vector<Function*> MiscompiledFunctions; |
| 634 | Module *Prog = BD.getProgram(); |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 635 | for (Function &F : *Prog) |
| 636 | if (!F.isDeclaration()) |
| 637 | MiscompiledFunctions.push_back(&F); |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 638 | |
| 639 | // Do the reduction... |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 640 | if (!BugpointIsInterrupted) |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 641 | ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, |
| 642 | Error); |
Andrew Trick | 55aeb55 | 2011-05-11 16:31:24 +0000 | [diff] [blame] | 643 | if (!Error.empty()) { |
| 644 | errs() << "\n***Cannot reduce functions: "; |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 645 | return MiscompiledFunctions; |
Andrew Trick | 55aeb55 | 2011-05-11 16:31:24 +0000 | [diff] [blame] | 646 | } |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 647 | outs() << "\n*** The following function" |
| 648 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 649 | << " being miscompiled: "; |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 650 | PrintFunctionList(MiscompiledFunctions); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 651 | outs() << '\n'; |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 652 | |
| 653 | // See if we can rip any loops out of the miscompiled functions and still |
| 654 | // trigger the problem. |
Reid Spencer | 471bcb7 | 2006-11-11 19:05:02 +0000 | [diff] [blame] | 655 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 656 | if (!BugpointIsInterrupted && !DisableLoopExtraction) { |
| 657 | bool Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions, Error); |
| 658 | if (!Error.empty()) |
| 659 | return MiscompiledFunctions; |
| 660 | if (Ret) { |
| 661 | // Okay, we extracted some loops and the problem still appears. See if |
| 662 | // we can eliminate some of the created functions from being candidates. |
| 663 | DisambiguateGlobalSymbols(BD.getProgram()); |
Chris Lattner | dfbb1d1 | 2004-04-11 23:52:35 +0000 | [diff] [blame] | 664 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 665 | // Do the reduction... |
| 666 | if (!BugpointIsInterrupted) |
| 667 | ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, |
| 668 | Error); |
| 669 | if (!Error.empty()) |
| 670 | return MiscompiledFunctions; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 671 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 672 | outs() << "\n*** The following function" |
| 673 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 674 | << " being miscompiled: "; |
| 675 | PrintFunctionList(MiscompiledFunctions); |
| 676 | outs() << '\n'; |
| 677 | } |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 680 | if (!BugpointIsInterrupted && !DisableBlockExtraction) { |
| 681 | bool Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions, Error); |
| 682 | if (!Error.empty()) |
| 683 | return MiscompiledFunctions; |
| 684 | if (Ret) { |
| 685 | // Okay, we extracted some blocks and the problem still appears. See if |
| 686 | // we can eliminate some of the created functions from being candidates. |
| 687 | DisambiguateGlobalSymbols(BD.getProgram()); |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 688 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 689 | // Do the reduction... |
| 690 | ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions, |
| 691 | Error); |
| 692 | if (!Error.empty()) |
| 693 | return MiscompiledFunctions; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 694 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 695 | outs() << "\n*** The following function" |
| 696 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 697 | << " being miscompiled: "; |
| 698 | PrintFunctionList(MiscompiledFunctions); |
| 699 | outs() << '\n'; |
| 700 | } |
Chris Lattner | a060b10 | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 703 | return MiscompiledFunctions; |
| 704 | } |
| 705 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 706 | /// This is the predicate function used to check to see if the "Test" portion of |
| 707 | /// the program is misoptimized. If so, return true. In any case, both module |
| 708 | /// arguments are deleted. |
Misha Brukman | 0784a60 | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 709 | /// |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 710 | static bool TestOptimizer(BugDriver &BD, std::unique_ptr<Module> Test, |
| 711 | std::unique_ptr<Module> Safe, std::string &Error) { |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 712 | // Run the optimization passes on ToOptimize, producing a transformed version |
| 713 | // of the functions being tested. |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 714 | outs() << " Optimizing functions being tested: "; |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 715 | std::unique_ptr<Module> Optimized = |
| 716 | BD.runPassesOn(Test.get(), BD.getPassesToRun(), |
| 717 | /*AutoDebugCrashes*/ true); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 718 | outs() << "done.\n"; |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 719 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 720 | outs() << " Checking to see if the merged program executes correctly: "; |
Rafael Espindola | e490460 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 721 | bool Broken; |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 722 | Module *New = |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 723 | TestMergedProgram(BD, Optimized.get(), Safe.get(), true, Error, Broken); |
Rafael Espindola | e490460 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 724 | if (New) { |
| 725 | outs() << (Broken ? " nope.\n" : " yup.\n"); |
| 726 | // Delete the original and set the new program. |
| 727 | delete BD.swapProgramIn(New); |
| 728 | } |
Chris Lattner | 0434ba3 | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 729 | return Broken; |
| 730 | } |
| 731 | |
| 732 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 733 | /// debugMiscompilation - This method is used when the passes selected are not |
| 734 | /// crashing, but the generated output is semantically different from the |
| 735 | /// input. |
| 736 | /// |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 737 | void BugDriver::debugMiscompilation(std::string *Error) { |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 738 | // Make sure something was miscompiled... |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 739 | if (!BugpointIsInterrupted) |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 740 | if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun, *Error)) { |
| 741 | if (Error->empty()) |
| 742 | errs() << "*** Optimized program matches reference output! No problem" |
| 743 | << " detected...\nbugpoint can't help you with your problem!\n"; |
| 744 | return; |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 745 | } |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 746 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 747 | outs() << "\n*** Found miscompiling pass" |
| 748 | << (getPassesToRun().size() == 1 ? "" : "es") << ": " |
| 749 | << getPassesString(getPassesToRun()) << '\n'; |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 750 | EmitProgressBitcode(Program, "passinput"); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 751 | |
Michael J. Spencer | 3df5c04 | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 752 | std::vector<Function *> MiscompiledFunctions = |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 753 | DebugAMiscompilation(*this, TestOptimizer, *Error); |
| 754 | if (!Error->empty()) |
| 755 | return; |
Chris Lattner | bcec787 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 756 | |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 757 | // Output a bunch of bitcode files for the user... |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 758 | outs() << "Outputting reduced bitcode files which expose the problem:\n"; |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 759 | ValueToValueMapTy VMap; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 760 | Module *ToNotOptimize = CloneModule(getProgram(), VMap).release(); |
Rafael Espindola | 50102c2 | 2015-12-09 00:34:10 +0000 | [diff] [blame] | 761 | Module *ToOptimize = |
| 762 | SplitFunctionsOutOfModule(ToNotOptimize, MiscompiledFunctions, VMap) |
| 763 | .release(); |
Chris Lattner | 567543f | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 764 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 765 | outs() << " Non-optimized portion: "; |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 766 | EmitProgressBitcode(ToNotOptimize, "tonotoptimize", true); |
| 767 | delete ToNotOptimize; // Delete hacked module. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 768 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 769 | outs() << " Portion that is input to optimizer: "; |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 770 | EmitProgressBitcode(ToOptimize, "tooptimize"); |
| 771 | delete ToOptimize; // Delete hacked module. |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 772 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 773 | return; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 774 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 775 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 776 | /// Get the specified modules ready for code generator testing. |
Misha Brukman | 0784a60 | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 777 | /// |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 778 | static void CleanupAndPrepareModules(BugDriver &BD, |
| 779 | std::unique_ptr<Module> &Test, |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 780 | Module *Safe) { |
| 781 | // Clean up the modules, removing extra cruft that we don't need anymore... |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 782 | Test = BD.performFinalCleanups(Test.get()); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 783 | |
| 784 | // If we are executing the JIT, we have several nasty issues to take care of. |
| 785 | if (!BD.isExecutingJIT()) return; |
| 786 | |
| 787 | // First, if the main function is in the Safe module, we must add a stub to |
| 788 | // the Test module to call into it. Thus, we create a new function `main' |
| 789 | // which just calls the old one. |
Reid Spencer | 1241d6d | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 790 | if (Function *oldMain = Safe->getFunction("main")) |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 791 | if (!oldMain->isDeclaration()) { |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 792 | // Rename it |
| 793 | oldMain->setName("llvm_bugpoint_old_main"); |
| 794 | // Create a NEW `main' function with same type in the test module. |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 795 | Function *newMain = |
| 796 | Function::Create(oldMain->getFunctionType(), |
| 797 | GlobalValue::ExternalLinkage, "main", Test.get()); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 798 | // Create an `oldmain' prototype in the test module, which will |
| 799 | // corresponds to the real main function in the same module. |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 800 | Function *oldMainProto = Function::Create(oldMain->getFunctionType(), |
| 801 | GlobalValue::ExternalLinkage, |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 802 | oldMain->getName(), Test.get()); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 803 | // Set up and remember the argument list for the main function. |
| 804 | std::vector<Value*> args; |
Alkis Evlogimenos | a67bafa | 2005-03-15 07:02:26 +0000 | [diff] [blame] | 805 | for (Function::arg_iterator |
| 806 | I = newMain->arg_begin(), E = newMain->arg_end(), |
| 807 | OI = oldMain->arg_begin(); I != E; ++I, ++OI) { |
Owen Anderson | 7629b71 | 2008-04-14 17:38:21 +0000 | [diff] [blame] | 808 | I->setName(OI->getName()); // Copy argument names from oldMain |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 809 | args.push_back(&*I); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // Call the old main function and return its result |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 813 | BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 814 | CallInst *call = CallInst::Create(oldMainProto, args, "", BB); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 815 | |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 816 | // If the type of old function wasn't void, return value of call |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 817 | ReturnInst::Create(Safe->getContext(), call, BB); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | // The second nasty issue we must deal with in the JIT is that the Safe |
| 821 | // module cannot directly reference any functions defined in the test |
| 822 | // module. Instead, we use a JIT API call to dynamically resolve the |
| 823 | // symbol. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 824 | |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 825 | // Add the resolver to the Safe module. |
| 826 | // Prototype: void *getPointerToNamedFunction(const char* Name) |
Chris Lattner | 7b864c0 | 2007-01-07 08:13:39 +0000 | [diff] [blame] | 827 | Constant *resolverFunc = |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 828 | Safe->getOrInsertFunction("getPointerToNamedFunction", |
Duncan Sands | 9ed7b16 | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 829 | Type::getInt8PtrTy(Safe->getContext()), |
| 830 | Type::getInt8PtrTy(Safe->getContext()), |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 831 | (Type *)nullptr); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 832 | |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 833 | // Use the function we just added to get addresses of functions we need. |
Misha Brukman | 8301864 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 834 | for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 835 | if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc && |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 836 | !F->isIntrinsic() /* ignore intrinsics */) { |
Reid Spencer | 1241d6d | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 837 | Function *TestFn = Test->getFunction(F->getName()); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 838 | |
| 839 | // Don't forward functions which are external in the test module too. |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 840 | if (TestFn && !TestFn->isDeclaration()) { |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 841 | // 1. Add a string constant with its name to the global file |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 842 | Constant *InitArray = |
| 843 | ConstantDataArray::getString(F->getContext(), F->getName()); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 844 | GlobalVariable *funcName = |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 845 | new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/, |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 846 | GlobalValue::InternalLinkage, InitArray, |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 847 | F->getName() + "_name"); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 848 | |
| 849 | // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an |
| 850 | // sbyte* so it matches the signature of the resolver function. |
| 851 | |
| 852 | // GetElementPtr *funcName, ulong 0, ulong 0 |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 853 | std::vector<Constant*> GEPargs(2, |
| 854 | Constant::getNullValue(Type::getInt32Ty(F->getContext()))); |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 855 | Value *GEP = ConstantExpr::getGetElementPtr(InitArray->getType(), |
| 856 | funcName, GEPargs); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 857 | std::vector<Value*> ResolverArgs; |
| 858 | ResolverArgs.push_back(GEP); |
| 859 | |
Misha Brukman | ce89b39 | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 860 | // Rewrite uses of F in global initializers, etc. to uses of a wrapper |
| 861 | // function that dynamically resolves the calls to F via our JIT API |
Chris Lattner | 986675c | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 862 | if (!F->use_empty()) { |
| 863 | // Create a new global to hold the cached function pointer. |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 864 | Constant *NullPtr = ConstantPointerNull::get(F->getType()); |
Chris Lattner | 986675c | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 865 | GlobalVariable *Cache = |
Michael J. Spencer | 3df5c04 | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 866 | new GlobalVariable(*F->getParent(), F->getType(), |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 867 | false, GlobalValue::InternalLinkage, |
| 868 | NullPtr,F->getName()+".fpcache"); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 869 | |
Misha Brukman | ce89b39 | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 870 | // Construct a new stub function that will re-route calls to F |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 871 | FunctionType *FuncTy = F->getFunctionType(); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 872 | Function *FuncWrapper = Function::Create(FuncTy, |
| 873 | GlobalValue::InternalLinkage, |
| 874 | F->getName() + "_wrapper", |
| 875 | F->getParent()); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 876 | BasicBlock *EntryBB = BasicBlock::Create(F->getContext(), |
| 877 | "entry", FuncWrapper); |
| 878 | BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(), |
| 879 | "usecache", FuncWrapper); |
| 880 | BasicBlock *LookupBB = BasicBlock::Create(F->getContext(), |
| 881 | "lookupfp", FuncWrapper); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 882 | |
Chris Lattner | 986675c | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 883 | // Check to see if we already looked up the value. |
| 884 | Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 885 | Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal, |
| 886 | NullPtr, "isNull"); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 887 | BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 888 | |
Misha Brukman | ce89b39 | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 889 | // Resolve the call to function F via the JIT API: |
| 890 | // |
| 891 | // call resolver(GetElementPtr...) |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 892 | CallInst *Resolver = |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 893 | CallInst::Create(resolverFunc, ResolverArgs, "resolver", LookupBB); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 894 | |
| 895 | // Cast the result from the resolver to correctly-typed function. |
| 896 | CastInst *CastedResolver = |
| 897 | new BitCastInst(Resolver, |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 898 | PointerType::getUnqual(F->getFunctionType()), |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 899 | "resolverCast", LookupBB); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 900 | |
Chris Lattner | 986675c | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 901 | // Save the value in our cache. |
| 902 | new StoreInst(CastedResolver, Cache, LookupBB); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 903 | BranchInst::Create(DoCallBB, LookupBB); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 904 | |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 905 | PHINode *FuncPtr = PHINode::Create(NullPtr->getType(), 2, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 906 | "fp", DoCallBB); |
Chris Lattner | 986675c | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 907 | FuncPtr->addIncoming(CastedResolver, LookupBB); |
| 908 | FuncPtr->addIncoming(CachedVal, EntryBB); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 909 | |
Chris Lattner | 986675c | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 910 | // Save the argument list. |
Misha Brukman | 8301864 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 911 | std::vector<Value*> Args; |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 912 | for (Argument &A : FuncWrapper->args()) |
| 913 | Args.push_back(&A); |
Misha Brukman | 8301864 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 914 | |
| 915 | // Pass on the arguments to the real function, return its result |
Dan Gohman | 34a2249 | 2010-06-07 20:19:26 +0000 | [diff] [blame] | 916 | if (F->getReturnType()->isVoidTy()) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 917 | CallInst::Create(FuncPtr, Args, "", DoCallBB); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 918 | ReturnInst::Create(F->getContext(), DoCallBB); |
Misha Brukman | 8301864 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 919 | } else { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 920 | CallInst *Call = CallInst::Create(FuncPtr, Args, |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 921 | "retval", DoCallBB); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 922 | ReturnInst::Create(F->getContext(),Call, DoCallBB); |
Misha Brukman | 8301864 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 923 | } |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 924 | |
Misha Brukman | ce89b39 | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 925 | // Use the wrapper function instead of the old function |
| 926 | F->replaceAllUsesWith(FuncWrapper); |
Misha Brukman | 8301864 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 927 | } |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | if (verifyModule(*Test) || verifyModule(*Safe)) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 933 | errs() << "Bugpoint has a bug, which corrupted a module!!\n"; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 934 | abort(); |
| 935 | } |
| 936 | } |
| 937 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 938 | /// This is the predicate function used to check to see if the "Test" portion of |
| 939 | /// the program is miscompiled by the code generator under test. If so, return |
| 940 | /// true. In any case, both module arguments are deleted. |
Misha Brukman | 0784a60 | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 941 | /// |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 942 | static bool TestCodeGenerator(BugDriver &BD, std::unique_ptr<Module> Test, |
| 943 | std::unique_ptr<Module> Safe, |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 944 | std::string &Error) { |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 945 | CleanupAndPrepareModules(BD, Test, Safe.get()); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 946 | |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 947 | SmallString<128> TestModuleBC; |
| 948 | int TestModuleFD; |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 949 | std::error_code EC = sys::fs::createTemporaryFile("bugpoint.test", "bc", |
| 950 | TestModuleFD, TestModuleBC); |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 951 | if (EC) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 952 | errs() << BD.getToolName() << "Error making unique filename: " |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 953 | << EC.message() << "\n"; |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 954 | exit(1); |
| 955 | } |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 956 | if (BD.writeProgramToFile(TestModuleBC.str(), TestModuleFD, Test.get())) { |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 957 | errs() << "Error writing bitcode to `" << TestModuleBC.str() |
| 958 | << "'\nExiting."; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 959 | exit(1); |
| 960 | } |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 961 | |
Michael J. Spencer | c60223e | 2011-03-31 13:04:19 +0000 | [diff] [blame] | 962 | FileRemover TestModuleBCRemover(TestModuleBC.str(), !SaveTemps); |
Rafael Espindola | 720f49e | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 963 | |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 964 | // Make the shared library |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 965 | SmallString<128> SafeModuleBC; |
| 966 | int SafeModuleFD; |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 967 | EC = sys::fs::createTemporaryFile("bugpoint.safe", "bc", SafeModuleFD, |
| 968 | SafeModuleBC); |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 969 | if (EC) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 970 | errs() << BD.getToolName() << "Error making unique filename: " |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 971 | << EC.message() << "\n"; |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 972 | exit(1); |
| 973 | } |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 974 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 975 | if (BD.writeProgramToFile(SafeModuleBC.str(), SafeModuleFD, Safe.get())) { |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 976 | errs() << "Error writing bitcode to `" << SafeModuleBC |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 977 | << "'\nExiting."; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 978 | exit(1); |
| 979 | } |
Rafael Espindola | 720f49e | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 980 | |
Michael J. Spencer | c60223e | 2011-03-31 13:04:19 +0000 | [diff] [blame] | 981 | FileRemover SafeModuleBCRemover(SafeModuleBC.str(), !SaveTemps); |
Rafael Espindola | 720f49e | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 982 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 983 | std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str(), Error); |
| 984 | if (!Error.empty()) |
Benjamin Kramer | 1f336da | 2010-04-12 12:22:19 +0000 | [diff] [blame] | 985 | return false; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 986 | |
Michael J. Spencer | c60223e | 2011-03-31 13:04:19 +0000 | [diff] [blame] | 987 | FileRemover SharedObjectRemover(SharedObject, !SaveTemps); |
Rafael Espindola | 720f49e | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 988 | |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 989 | // Run the code generator on the `Test' code, loading the shared library. |
| 990 | // The function returns whether or not the new output differs from reference. |
Rafael Espindola | c89b1ef | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 991 | bool Result = BD.diffProgram(BD.getProgram(), TestModuleBC.str(), |
| 992 | SharedObject, false, &Error); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 993 | if (!Error.empty()) |
| 994 | return false; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 995 | |
| 996 | if (Result) |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 997 | errs() << ": still failing!\n"; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 998 | else |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 999 | errs() << ": didn't fail.\n"; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1000 | |
| 1001 | return Result; |
| 1002 | } |
| 1003 | |
| 1004 | |
Misha Brukman | 0784a60 | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 1005 | /// debugCodeGenerator - debug errors in LLC, LLI, or CBE. |
| 1006 | /// |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1007 | bool BugDriver::debugCodeGenerator(std::string *Error) { |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 1008 | if ((void*)SafeInterpreter == (void*)Interpreter) { |
Rafael Espindola | c89b1ef | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 1009 | std::string Result = executeProgramSafely(Program, "bugpoint.safe.out", |
| 1010 | Error); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1011 | if (Error->empty()) { |
| 1012 | outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match " |
| 1013 | << "the reference diff. This may be due to a\n front-end " |
| 1014 | << "bug or a bug in the original program, but this can also " |
| 1015 | << "happen if bugpoint isn't running the program with the " |
| 1016 | << "right flags or input.\n I left the result of executing " |
| 1017 | << "the program with the \"safe\" backend in this file for " |
| 1018 | << "you: '" |
| 1019 | << Result << "'.\n"; |
| 1020 | } |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1021 | return true; |
| 1022 | } |
| 1023 | |
| 1024 | DisambiguateGlobalSymbols(Program); |
| 1025 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1026 | std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator, |
| 1027 | *Error); |
| 1028 | if (!Error->empty()) |
| 1029 | return true; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1030 | |
| 1031 | // Split the module into the two halves of the program we want. |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 1032 | ValueToValueMapTy VMap; |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 1033 | std::unique_ptr<Module> ToNotCodeGen = CloneModule(getProgram(), VMap); |
| 1034 | std::unique_ptr<Module> ToCodeGen = |
| 1035 | SplitFunctionsOutOfModule(ToNotCodeGen.get(), Funcs, VMap); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1036 | |
| 1037 | // Condition the modules |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 1038 | CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen.get()); |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1039 | |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 1040 | SmallString<128> TestModuleBC; |
| 1041 | int TestModuleFD; |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 1042 | std::error_code EC = sys::fs::createTemporaryFile("bugpoint.test", "bc", |
| 1043 | TestModuleFD, TestModuleBC); |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 1044 | if (EC) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 1045 | errs() << getToolName() << "Error making unique filename: " |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 1046 | << EC.message() << "\n"; |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 1047 | exit(1); |
| 1048 | } |
Reid Spencer | 30067f1 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 1049 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 1050 | if (writeProgramToFile(TestModuleBC.str(), TestModuleFD, ToCodeGen.get())) { |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1051 | errs() << "Error writing bitcode to `" << TestModuleBC |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1052 | << "'\nExiting."; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1053 | exit(1); |
| 1054 | } |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Make the shared library |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 1057 | SmallString<128> SafeModuleBC; |
| 1058 | int SafeModuleFD; |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 1059 | EC = sys::fs::createTemporaryFile("bugpoint.safe", "bc", SafeModuleFD, |
| 1060 | SafeModuleBC); |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 1061 | if (EC) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 1062 | errs() << getToolName() << "Error making unique filename: " |
Rafael Espindola | 302c0da | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 1063 | << EC.message() << "\n"; |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 1064 | exit(1); |
| 1065 | } |
Reid Spencer | 30067f1 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 1066 | |
Rafael Espindola | bc12cbc | 2015-12-09 00:51:06 +0000 | [diff] [blame^] | 1067 | if (writeProgramToFile(SafeModuleBC.str(), SafeModuleFD, |
| 1068 | ToNotCodeGen.get())) { |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1069 | errs() << "Error writing bitcode to `" << SafeModuleBC |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1070 | << "'\nExiting."; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1071 | exit(1); |
| 1072 | } |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1073 | std::string SharedObject = compileSharedObject(SafeModuleBC.str(), *Error); |
| 1074 | if (!Error->empty()) |
| 1075 | return true; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1076 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1077 | outs() << "You can reproduce the problem with the command line: \n"; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1078 | if (isExecutingJIT()) { |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1079 | outs() << " lli -load " << SharedObject << " " << TestModuleBC; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1080 | } else { |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1081 | outs() << " llc " << TestModuleBC << " -o " << TestModuleBC |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1082 | << ".s\n"; |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 1083 | outs() << " cc " << SharedObject << " " << TestModuleBC.str() |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1084 | << ".s -o " << TestModuleBC << ".exe"; |
Chris Lattner | ea2fa46 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 1085 | #if defined (HAVE_LINK_R) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1086 | outs() << " -Wl,-R."; |
Chris Lattner | ea2fa46 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 1087 | #endif |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1088 | outs() << "\n"; |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1089 | outs() << " " << TestModuleBC << ".exe"; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1090 | } |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1091 | for (unsigned i = 0, e = InputArgv.size(); i != e; ++i) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1092 | outs() << " " << InputArgv[i]; |
| 1093 | outs() << '\n'; |
| 1094 | outs() << "The shared object was created with:\n llc -march=c " |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1095 | << SafeModuleBC.str() << " -o temporary.c\n" |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 1096 | << " cc -xc temporary.c -O2 -o " << SharedObject; |
Daniel Dunbar | 8575a60 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 1097 | if (TargetTriple.getArch() == Triple::sparc) |
| 1098 | outs() << " -G"; // Compile a shared library, `-G' for Sparc |
| 1099 | else |
| 1100 | outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others |
| 1101 | |
| 1102 | outs() << " -fno-strict-aliasing\n"; |
Chris Lattner | bf79161 | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1103 | |
| 1104 | return false; |
| 1105 | } |