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