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