blob: 3e532226b951301eb2d62a2651e8b5a9d8332141 [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- Miscompilation.cpp - Debug program miscompilations -----------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner4a106452002-12-23 23:50:16 +00009//
Chris Lattnera57d86b2004-04-05 22:58:16 +000010// This file implements optimizer and code generation miscompilation debugging
11// support.
Chris Lattner4a106452002-12-23 23:50:16 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "BugDriver.h"
Chris Lattner126840f2003-04-24 20:16:29 +000016#include "ListReducer.h"
Chris Lattnera57d86b2004-04-05 22:58:16 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Instructions.h"
Chris Lattner4a106452002-12-23 23:50:16 +000020#include "llvm/Module.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000021#include "llvm/Pass.h"
Chris Lattnera57d86b2004-04-05 22:58:16 +000022#include "llvm/Analysis/Verifier.h"
23#include "llvm/Support/Mangler.h"
Chris Lattner640f22e2003-04-24 17:02:17 +000024#include "llvm/Transforms/Utils/Cloning.h"
25#include "llvm/Transforms/Utils/Linker.h"
Chris Lattnera57d86b2004-04-05 22:58:16 +000026#include "Support/CommandLine.h"
Misha Brukman3d9cafa2003-08-07 21:42:28 +000027#include "Support/FileUtilities.h"
Chris Lattnerfa761832004-01-14 03:38:37 +000028using namespace llvm;
Chris Lattner4a106452002-12-23 23:50:16 +000029
Chris Lattnera57d86b2004-04-05 22:58:16 +000030namespace llvm {
31 extern cl::list<std::string> InputArgv;
32}
33
Chris Lattnerefdc0b52004-03-14 20:50:42 +000034namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +000035 class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
36 BugDriver &BD;
37 public:
38 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
39
40 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
41 std::vector<const PassInfo*> &Suffix);
42 };
43}
Chris Lattner640f22e2003-04-24 17:02:17 +000044
Misha Brukman8c194ea2004-04-21 18:36:43 +000045/// TestResult - After passes have been split into a test group and a control
46/// group, see if they still break the program.
47///
Chris Lattner640f22e2003-04-24 17:02:17 +000048ReduceMiscompilingPasses::TestResult
Chris Lattner39aebca2003-04-24 22:24:22 +000049ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
Chris Lattner06943ad2003-04-25 03:16:05 +000050 std::vector<const PassInfo*> &Suffix) {
51 // First, run the program with just the Suffix passes. If it is still broken
Chris Lattner640f22e2003-04-24 17:02:17 +000052 // with JUST the kept passes, discard the prefix passes.
Chris Lattner06943ad2003-04-25 03:16:05 +000053 std::cout << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +000054 << "' compile correctly: ";
55
56 std::string BytecodeResult;
Chris Lattner06943ad2003-04-25 03:16:05 +000057 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +000058 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000059 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +000060 BD.setPassesToRun(Suffix);
61 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +000062 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000063 }
64
65 // Check to see if the finished program matches the reference output...
Misha Brukman50733362003-07-24 18:17:43 +000066 if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) {
Misha Brukman123f8fe2004-04-22 20:02:09 +000067 std::cout << " nope.\n";
68 return KeepSuffix; // Miscompilation detected!
Chris Lattner640f22e2003-04-24 17:02:17 +000069 }
Misha Brukman123f8fe2004-04-22 20:02:09 +000070 std::cout << " yup.\n"; // No miscompilation!
Chris Lattner640f22e2003-04-24 17:02:17 +000071
72 if (Prefix.empty()) return NoFailure;
73
Chris Lattner06943ad2003-04-25 03:16:05 +000074 // Next, see if the program is broken if we run the "prefix" passes first,
Misha Brukmanbc0e9982003-07-14 17:20:40 +000075 // then separately run the "kept" passes.
Chris Lattner640f22e2003-04-24 17:02:17 +000076 std::cout << "Checking to see if '" << getPassesString(Prefix)
77 << "' compile correctly: ";
78
79 // If it is not broken with the kept passes, it's possible that the prefix
80 // passes must be run before the kept passes to break it. If the program
81 // WORKS after the prefix passes, but then fails if running the prefix AND
82 // kept passes, we can update our bytecode file to include the result of the
83 // prefix passes, then discard the prefix passes.
84 //
85 if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +000086 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000087 << " on the input program!\n";
Chris Lattner9c6cfe12003-10-17 23:03:16 +000088 BD.setPassesToRun(Prefix);
89 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +000090 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000091 }
92
93 // If the prefix maintains the predicate by itself, only keep the prefix!
Misha Brukman50733362003-07-24 18:17:43 +000094 if (BD.diffProgram(BytecodeResult)) {
Misha Brukman123f8fe2004-04-22 20:02:09 +000095 std::cout << " nope.\n";
Chris Lattner640f22e2003-04-24 17:02:17 +000096 removeFile(BytecodeResult);
97 return KeepPrefix;
98 }
Misha Brukman123f8fe2004-04-22 20:02:09 +000099 std::cout << " yup.\n"; // No miscompilation!
Chris Lattner640f22e2003-04-24 17:02:17 +0000100
101 // Ok, so now we know that the prefix passes work, try running the suffix
102 // passes on the result of the prefix passes.
103 //
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000104 Module *PrefixOutput = ParseInputFile(BytecodeResult);
Chris Lattner640f22e2003-04-24 17:02:17 +0000105 if (PrefixOutput == 0) {
106 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
107 << BytecodeResult << "'!\n";
108 exit(1);
109 }
110 removeFile(BytecodeResult); // No longer need the file on disk
Chris Lattnerf4789e62004-04-23 20:36:51 +0000111
112 // Don't check if there are no passes in the suffix.
113 if (Suffix.empty())
114 return NoFailure;
115
Chris Lattner06943ad2003-04-25 03:16:05 +0000116 std::cout << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +0000117 << "' passes compile correctly after the '"
118 << getPassesString(Prefix) << "' passes: ";
119
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000120 Module *OriginalInput = BD.swapProgramIn(PrefixOutput);
Chris Lattner06943ad2003-04-25 03:16:05 +0000121 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +0000122 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +0000123 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000124 BD.setPassesToRun(Suffix);
Chris Lattner9c6cfe12003-10-17 23:03:16 +0000125 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000126 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000127 }
128
129 // Run the result...
Misha Brukman50733362003-07-24 18:17:43 +0000130 if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) {
Misha Brukman123f8fe2004-04-22 20:02:09 +0000131 std::cout << " nope.\n";
Chris Lattner640f22e2003-04-24 17:02:17 +0000132 delete OriginalInput; // We pruned down the original input...
Chris Lattner06943ad2003-04-25 03:16:05 +0000133 return KeepSuffix;
Chris Lattner640f22e2003-04-24 17:02:17 +0000134 }
135
136 // Otherwise, we must not be running the bad pass anymore.
Misha Brukman123f8fe2004-04-22 20:02:09 +0000137 std::cout << " yup.\n"; // No miscompilation!
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000138 delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test
Chris Lattner640f22e2003-04-24 17:02:17 +0000139 return NoFailure;
140}
141
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000142namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +0000143 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
144 BugDriver &BD;
Chris Lattnerb15825b2004-04-05 21:37:38 +0000145 bool (*TestFn)(BugDriver &, Module *, Module *);
Chris Lattnerfa761832004-01-14 03:38:37 +0000146 public:
Chris Lattnerb15825b2004-04-05 21:37:38 +0000147 ReduceMiscompilingFunctions(BugDriver &bd,
148 bool (*F)(BugDriver &, Module *, Module *))
149 : BD(bd), TestFn(F) {}
Chris Lattnerfa761832004-01-14 03:38:37 +0000150
151 virtual TestResult doTest(std::vector<Function*> &Prefix,
152 std::vector<Function*> &Suffix) {
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000153 if (!Suffix.empty() && TestFuncs(Suffix))
Chris Lattnerfa761832004-01-14 03:38:37 +0000154 return KeepSuffix;
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000155 if (!Prefix.empty() && TestFuncs(Prefix))
Chris Lattnerfa761832004-01-14 03:38:37 +0000156 return KeepPrefix;
157 return NoFailure;
158 }
159
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000160 bool TestFuncs(const std::vector<Function*> &Prefix);
Chris Lattnerfa761832004-01-14 03:38:37 +0000161 };
162}
Chris Lattner640f22e2003-04-24 17:02:17 +0000163
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000164/// TestMergedProgram - Given two modules, link them together and run the
165/// program, checking to see if the program matches the diff. If the diff
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000166/// matches, return false, otherwise return true. If the DeleteInputs argument
167/// is set to true then this function deletes both input modules before it
168/// returns.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000169///
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000170static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
171 bool DeleteInputs) {
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000172 // Link the two portions of the program back to together.
173 std::string ErrorMsg;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000174 if (!DeleteInputs) M1 = CloneModule(M1);
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000175 if (LinkModules(M1, M2, &ErrorMsg)) {
176 std::cerr << BD.getToolName() << ": Error linking modules together:"
177 << ErrorMsg << "\n";
178 exit(1);
179 }
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000180 if (DeleteInputs) delete M2; // We are done with this module...
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000181
182 Module *OldProgram = BD.swapProgramIn(M1);
183
184 // Execute the program. If it does not match the expected output, we must
185 // return true.
186 bool Broken = BD.diffProgram();
187
188 // Delete the linked module & restore the original
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000189 BD.swapProgramIn(OldProgram);
Chris Lattner5313f232004-04-02 06:32:17 +0000190 delete M1;
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000191 return Broken;
192}
193
Misha Brukman8c194ea2004-04-21 18:36:43 +0000194/// TestFuncs - split functions in a Module into two groups: those that are
195/// under consideration for miscompilation vs. those that are not, and test
196/// accordingly. Each group of functions becomes a separate Module.
197///
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000198bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
Chris Lattner640f22e2003-04-24 17:02:17 +0000199 // Test to see if the function is misoptimized if we ONLY run it on the
200 // functions listed in Funcs.
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000201 std::cout << "Checking to see if the program is misoptimized when "
202 << (Funcs.size()==1 ? "this function is" : "these functions are")
203 << " run through the pass"
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000204 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000205 PrintFunctionList(Funcs);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000206 std::cout << "\n";
Chris Lattner640f22e2003-04-24 17:02:17 +0000207
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000208 // Split the module into the two halves of the program we want.
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000209 Module *ToNotOptimize = CloneModule(BD.getProgram());
210 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs);
Chris Lattner640f22e2003-04-24 17:02:17 +0000211
Chris Lattnerb15825b2004-04-05 21:37:38 +0000212 // Run the predicate, not that the predicate will delete both input modules.
213 return TestFn(BD, ToOptimize, ToNotOptimize);
Chris Lattner640f22e2003-04-24 17:02:17 +0000214}
215
Misha Brukman8c194ea2004-04-21 18:36:43 +0000216/// DisambiguateGlobalSymbols - Mangle symbols to guarantee uniqueness by
217/// modifying predominantly internal symbols rather than external ones.
218///
Chris Lattner36ee07f2004-04-11 23:52:35 +0000219static void DisambiguateGlobalSymbols(Module *M) {
220 // Try not to cause collisions by minimizing chances of renaming an
221 // already-external symbol, so take in external globals and functions as-is.
222 // The code should work correctly without disambiguation (assuming the same
223 // mangler is used by the two code generators), but having symbols with the
224 // same name causes warnings to be emitted by the code generator.
225 Mangler Mang(*M);
226 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
227 I->setName(Mang.getValueName(I));
228 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
229 I->setName(Mang.getValueName(I));
230}
231
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000232/// ExtractLoops - Given a reduced list of functions that still exposed the bug,
233/// check to see if we can extract the loops in the region without obscuring the
234/// bug. If so, it reduces the amount of code identified.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000235///
Chris Lattnerb15825b2004-04-05 21:37:38 +0000236static bool ExtractLoops(BugDriver &BD,
237 bool (*TestFn)(BugDriver &, Module *, Module *),
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000238 std::vector<Function*> &MiscompiledFunctions) {
239 bool MadeChange = false;
240 while (1) {
241 Module *ToNotOptimize = CloneModule(BD.getProgram());
242 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
243 MiscompiledFunctions);
244 Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
245 if (!ToOptimizeLoopExtracted) {
246 // If the loop extractor crashed or if there were no extractible loops,
247 // then this chapter of our odyssey is over with.
248 delete ToNotOptimize;
249 delete ToOptimize;
250 return MadeChange;
251 }
252
253 std::cerr << "Extracted a loop from the breaking portion of the program.\n";
254 delete ToOptimize;
255
256 // Bugpoint is intentionally not very trusting of LLVM transformations. In
257 // particular, we're not going to assume that the loop extractor works, so
258 // we're going to test the newly loop extracted program to make sure nothing
259 // has broken. If something broke, then we'll inform the user and stop
260 // extraction.
Chris Lattnera57d86b2004-04-05 22:58:16 +0000261 AbstractInterpreter *AI = BD.switchToCBE();
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000262 if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000263 BD.switchToInterpreter(AI);
264
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000265 // Merged program doesn't work anymore!
266 std::cerr << " *** ERROR: Loop extraction broke the program. :("
267 << " Please report a bug!\n";
268 std::cerr << " Continuing on with un-loop-extracted version.\n";
269 delete ToNotOptimize;
270 delete ToOptimizeLoopExtracted;
271 return MadeChange;
272 }
Chris Lattnera57d86b2004-04-05 22:58:16 +0000273 BD.switchToInterpreter(AI);
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000274
Chris Lattnerb15825b2004-04-05 21:37:38 +0000275 std::cout << " Testing after loop extraction:\n";
276 // Clone modules, the tester function will free them.
277 Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
278 Module *TNOBackup = CloneModule(ToNotOptimize);
279 if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) {
280 std::cout << "*** Loop extraction masked the problem. Undoing.\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000281 // If the program is not still broken, then loop extraction did something
282 // that masked the error. Stop loop extraction now.
Chris Lattnerb15825b2004-04-05 21:37:38 +0000283 delete TOLEBackup;
284 delete TNOBackup;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000285 return MadeChange;
286 }
Chris Lattnerb15825b2004-04-05 21:37:38 +0000287 ToOptimizeLoopExtracted = TOLEBackup;
288 ToNotOptimize = TNOBackup;
289
290 std::cout << "*** Loop extraction successful!\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000291
292 // Okay, great! Now we know that we extracted a loop and that loop
293 // extraction both didn't break the program, and didn't mask the problem.
294 // Replace the current program with the loop extracted version, and try to
295 // extract another loop.
296 std::string ErrorMsg;
297 if (LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)) {
298 std::cerr << BD.getToolName() << ": Error linking modules together:"
299 << ErrorMsg << "\n";
300 exit(1);
301 }
Chris Lattnerd3a533d2004-03-17 17:42:09 +0000302
303 // All of the Function*'s in the MiscompiledFunctions list are in the old
Chris Lattner5313f232004-04-02 06:32:17 +0000304 // module. Update this list to include all of the functions in the
305 // optimized and loop extracted module.
306 MiscompiledFunctions.clear();
307 for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
308 E = ToOptimizeLoopExtracted->end(); I != E; ++I) {
309 if (!I->isExternal()) {
Chris Lattner02bb4812004-04-02 06:32:45 +0000310 Function *NewF = ToNotOptimize->getFunction(I->getName(),
311 I->getFunctionType());
Chris Lattner5313f232004-04-02 06:32:17 +0000312 assert(NewF && "Function not found??");
313 MiscompiledFunctions.push_back(NewF);
314 }
Chris Lattnerd3a533d2004-03-17 17:42:09 +0000315 }
Chris Lattner5313f232004-04-02 06:32:17 +0000316 delete ToOptimizeLoopExtracted;
Chris Lattnerd3a533d2004-03-17 17:42:09 +0000317
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000318 BD.setNewProgram(ToNotOptimize);
319 MadeChange = true;
320 }
321}
322
Chris Lattnerb15825b2004-04-05 21:37:38 +0000323/// DebugAMiscompilation - This is a generic driver to narrow down
324/// miscompilations, either in an optimization or a code generator.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000325///
Chris Lattnerb15825b2004-04-05 21:37:38 +0000326static std::vector<Function*>
327DebugAMiscompilation(BugDriver &BD,
328 bool (*TestFn)(BugDriver &, Module *, Module *)) {
329 // Okay, now that we have reduced the list of passes which are causing the
330 // failure, see if we can pin down which functions are being
331 // miscompiled... first build a list of all of the non-external functions in
332 // the program.
333 std::vector<Function*> MiscompiledFunctions;
334 Module *Prog = BD.getProgram();
335 for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
336 if (!I->isExternal())
337 MiscompiledFunctions.push_back(I);
338
339 // Do the reduction...
340 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
341
342 std::cout << "\n*** The following function"
343 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
344 << " being miscompiled: ";
345 PrintFunctionList(MiscompiledFunctions);
346 std::cout << "\n";
347
348 // See if we can rip any loops out of the miscompiled functions and still
349 // trigger the problem.
350 if (ExtractLoops(BD, TestFn, MiscompiledFunctions)) {
351 // Okay, we extracted some loops and the problem still appears. See if we
352 // can eliminate some of the created functions from being candidates.
353
Chris Lattner36ee07f2004-04-11 23:52:35 +0000354 // Loop extraction can introduce functions with the same name (foo_code).
355 // Make sure to disambiguate the symbols so that when the program is split
356 // apart that we can link it back together again.
357 DisambiguateGlobalSymbols(BD.getProgram());
358
Chris Lattnerb15825b2004-04-05 21:37:38 +0000359 // Do the reduction...
360 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
361
362 std::cout << "\n*** The following function"
363 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
364 << " being miscompiled: ";
365 PrintFunctionList(MiscompiledFunctions);
366 std::cout << "\n";
367 }
368
369 return MiscompiledFunctions;
370}
371
Chris Lattnera57d86b2004-04-05 22:58:16 +0000372/// TestOptimizer - This is the predicate function used to check to see if the
373/// "Test" portion of the program is misoptimized. If so, return true. In any
374/// case, both module arguments are deleted.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000375///
Chris Lattnerb15825b2004-04-05 21:37:38 +0000376static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
377 // Run the optimization passes on ToOptimize, producing a transformed version
378 // of the functions being tested.
379 std::cout << " Optimizing functions being tested: ";
380 Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
381 /*AutoDebugCrashes*/true);
382 std::cout << "done.\n";
383 delete Test;
384
385 std::cout << " Checking to see if the merged program executes correctly: ";
Chris Lattner2423db02004-04-09 22:28:33 +0000386 bool Broken = TestMergedProgram(BD, Optimized, Safe, true);
Chris Lattnerb15825b2004-04-05 21:37:38 +0000387 std::cout << (Broken ? " nope.\n" : " yup.\n");
388 return Broken;
389}
390
391
Chris Lattner4a106452002-12-23 23:50:16 +0000392/// debugMiscompilation - This method is used when the passes selected are not
393/// crashing, but the generated output is semantically different from the
394/// input.
395///
396bool BugDriver::debugMiscompilation() {
Chris Lattner4a106452002-12-23 23:50:16 +0000397 // Make sure something was miscompiled...
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000398 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
Chris Lattner4a106452002-12-23 23:50:16 +0000399 std::cerr << "*** Optimized program matches reference output! No problem "
400 << "detected...\nbugpoint can't help you with your problem!\n";
401 return false;
402 }
403
Chris Lattner640f22e2003-04-24 17:02:17 +0000404 std::cout << "\n*** Found miscompiling pass"
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000405 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
406 << getPassesString(getPassesToRun()) << "\n";
Chris Lattner640f22e2003-04-24 17:02:17 +0000407 EmitProgressBytecode("passinput");
Chris Lattner4a106452002-12-23 23:50:16 +0000408
Chris Lattnerb15825b2004-04-05 21:37:38 +0000409 std::vector<Function*> MiscompiledFunctions =
410 DebugAMiscompilation(*this, TestOptimizer);
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000411
Chris Lattner640f22e2003-04-24 17:02:17 +0000412 // Output a bunch of bytecode files for the user...
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000413 std::cout << "Outputting reduced bytecode files which expose the problem:\n";
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000414 Module *ToNotOptimize = CloneModule(getProgram());
415 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
416 MiscompiledFunctions);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000417
418 std::cout << " Non-optimized portion: ";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000419 ToNotOptimize = swapProgramIn(ToNotOptimize);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000420 EmitProgressBytecode("tonotoptimize", true);
421 setNewProgram(ToNotOptimize); // Delete hacked module.
422
423 std::cout << " Portion that is input to optimizer: ";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000424 ToOptimize = swapProgramIn(ToOptimize);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000425 EmitProgressBytecode("tooptimize");
426 setNewProgram(ToOptimize); // Delete hacked module.
Chris Lattner4a106452002-12-23 23:50:16 +0000427
Chris Lattner4a106452002-12-23 23:50:16 +0000428 return false;
429}
Brian Gaeked0fde302003-11-11 22:41:34 +0000430
Chris Lattnera57d86b2004-04-05 22:58:16 +0000431/// CleanupAndPrepareModules - Get the specified modules ready for code
432/// generator testing.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000433///
Chris Lattnera57d86b2004-04-05 22:58:16 +0000434static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
435 Module *Safe) {
436 // Clean up the modules, removing extra cruft that we don't need anymore...
437 Test = BD.performFinalCleanups(Test);
438
439 // If we are executing the JIT, we have several nasty issues to take care of.
440 if (!BD.isExecutingJIT()) return;
441
442 // First, if the main function is in the Safe module, we must add a stub to
443 // the Test module to call into it. Thus, we create a new function `main'
444 // which just calls the old one.
445 if (Function *oldMain = Safe->getNamedFunction("main"))
446 if (!oldMain->isExternal()) {
447 // Rename it
448 oldMain->setName("llvm_bugpoint_old_main");
449 // Create a NEW `main' function with same type in the test module.
450 Function *newMain = new Function(oldMain->getFunctionType(),
451 GlobalValue::ExternalLinkage,
452 "main", Test);
453 // Create an `oldmain' prototype in the test module, which will
454 // corresponds to the real main function in the same module.
455 Function *oldMainProto = new Function(oldMain->getFunctionType(),
456 GlobalValue::ExternalLinkage,
457 oldMain->getName(), Test);
458 // Set up and remember the argument list for the main function.
459 std::vector<Value*> args;
460 for (Function::aiterator I = newMain->abegin(), E = newMain->aend(),
461 OI = oldMain->abegin(); I != E; ++I, ++OI) {
462 I->setName(OI->getName()); // Copy argument names from oldMain
463 args.push_back(I);
464 }
465
466 // Call the old main function and return its result
467 BasicBlock *BB = new BasicBlock("entry", newMain);
468 CallInst *call = new CallInst(oldMainProto, args);
469 BB->getInstList().push_back(call);
470
471 // If the type of old function wasn't void, return value of call
472 new ReturnInst(oldMain->getReturnType() != Type::VoidTy ? call : 0, BB);
473 }
474
475 // The second nasty issue we must deal with in the JIT is that the Safe
476 // module cannot directly reference any functions defined in the test
477 // module. Instead, we use a JIT API call to dynamically resolve the
478 // symbol.
479
480 // Add the resolver to the Safe module.
481 // Prototype: void *getPointerToNamedFunction(const char* Name)
482 Function *resolverFunc =
483 Safe->getOrInsertFunction("getPointerToNamedFunction",
484 PointerType::get(Type::SByteTy),
485 PointerType::get(Type::SByteTy), 0);
486
487 // Use the function we just added to get addresses of functions we need.
Misha Brukmandc7fef82004-04-19 01:12:01 +0000488 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000489 if (F->isExternal() && !F->use_empty() && &*F != resolverFunc &&
490 F->getIntrinsicID() == 0 /* ignore intrinsics */) {
Misha Brukmandc7fef82004-04-19 01:12:01 +0000491 Function *TestFn = Test->getFunction(F->getName(), F->getFunctionType());
Chris Lattnera57d86b2004-04-05 22:58:16 +0000492
493 // Don't forward functions which are external in the test module too.
494 if (TestFn && !TestFn->isExternal()) {
495 // 1. Add a string constant with its name to the global file
496 Constant *InitArray = ConstantArray::get(F->getName());
497 GlobalVariable *funcName =
498 new GlobalVariable(InitArray->getType(), true /*isConstant*/,
499 GlobalValue::InternalLinkage, InitArray,
500 F->getName() + "_name", Safe);
501
502 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
503 // sbyte* so it matches the signature of the resolver function.
504
505 // GetElementPtr *funcName, ulong 0, ulong 0
506 std::vector<Constant*> GEPargs(2,Constant::getNullValue(Type::IntTy));
507 Value *GEP =
508 ConstantExpr::getGetElementPtr(ConstantPointerRef::get(funcName),
509 GEPargs);
510 std::vector<Value*> ResolverArgs;
511 ResolverArgs.push_back(GEP);
512
Misha Brukmande4803d2004-04-19 03:36:47 +0000513 // Rewrite uses of F in global initializers, etc. to uses of a wrapper
514 // function that dynamically resolves the calls to F via our JIT API
515 if (F->use_begin() != F->use_end()) {
516 // Construct a new stub function that will re-route calls to F
Misha Brukmandc7fef82004-04-19 01:12:01 +0000517 const FunctionType *FuncTy = F->getFunctionType();
Misha Brukmande4803d2004-04-19 03:36:47 +0000518 Function *FuncWrapper = new Function(FuncTy,
519 GlobalValue::InternalLinkage,
Misha Brukmandc7fef82004-04-19 01:12:01 +0000520 F->getName() + "_wrapper",
521 F->getParent());
522 BasicBlock *Header = new BasicBlock("header", FuncWrapper);
523
Misha Brukmande4803d2004-04-19 03:36:47 +0000524 // Resolve the call to function F via the JIT API:
525 //
526 // call resolver(GetElementPtr...)
527 CallInst *resolve = new CallInst(resolverFunc, ResolverArgs,
528 "resolver");
529 Header->getInstList().push_back(resolve);
530 // cast the result from the resolver to correctly-typed function
531 CastInst *castResolver =
532 new CastInst(resolve, PointerType::get(F->getFunctionType()),
533 "resolverCast");
534 Header->getInstList().push_back(castResolver);
535
Misha Brukmandc7fef82004-04-19 01:12:01 +0000536 // Save the argument list
537 std::vector<Value*> Args;
538 for (Function::aiterator i = FuncWrapper->abegin(),
539 e = FuncWrapper->aend(); i != e; ++i)
540 Args.push_back(i);
541
542 // Pass on the arguments to the real function, return its result
543 if (F->getReturnType() == Type::VoidTy) {
Misha Brukmande4803d2004-04-19 03:36:47 +0000544 CallInst *Call = new CallInst(castResolver, Args);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000545 Header->getInstList().push_back(Call);
546 ReturnInst *Ret = new ReturnInst();
547 Header->getInstList().push_back(Ret);
548 } else {
Misha Brukmande4803d2004-04-19 03:36:47 +0000549 CallInst *Call = new CallInst(castResolver, Args, "redir");
Misha Brukmandc7fef82004-04-19 01:12:01 +0000550 Header->getInstList().push_back(Call);
551 ReturnInst *Ret = new ReturnInst(Call);
552 Header->getInstList().push_back(Ret);
553 }
554
Misha Brukmande4803d2004-04-19 03:36:47 +0000555 // Use the wrapper function instead of the old function
556 F->replaceAllUsesWith(FuncWrapper);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000557 }
Chris Lattnera57d86b2004-04-05 22:58:16 +0000558 }
559 }
560 }
561
562 if (verifyModule(*Test) || verifyModule(*Safe)) {
563 std::cerr << "Bugpoint has a bug, which corrupted a module!!\n";
564 abort();
565 }
566}
567
568
569
570/// TestCodeGenerator - This is the predicate function used to check to see if
571/// the "Test" portion of the program is miscompiled by the code generator under
572/// test. If so, return true. In any case, both module arguments are deleted.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000573///
Chris Lattnera57d86b2004-04-05 22:58:16 +0000574static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) {
575 CleanupAndPrepareModules(BD, Test, Safe);
576
577 std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
578 if (BD.writeProgramToFile(TestModuleBC, Test)) {
579 std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
580 exit(1);
581 }
582 delete Test;
583
584 // Make the shared library
585 std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
586
587 if (BD.writeProgramToFile(SafeModuleBC, Safe)) {
588 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
589 exit(1);
590 }
591 std::string SharedObject = BD.compileSharedObject(SafeModuleBC);
592 delete Safe;
593
594 // Run the code generator on the `Test' code, loading the shared library.
595 // The function returns whether or not the new output differs from reference.
596 int Result = BD.diffProgram(TestModuleBC, SharedObject, false);
597
598 if (Result)
599 std::cerr << ": still failing!\n";
600 else
601 std::cerr << ": didn't fail.\n";
602 removeFile(TestModuleBC);
603 removeFile(SafeModuleBC);
604 removeFile(SharedObject);
605
606 return Result;
607}
608
609
Misha Brukman8c194ea2004-04-21 18:36:43 +0000610/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
611///
Chris Lattnera57d86b2004-04-05 22:58:16 +0000612bool BugDriver::debugCodeGenerator() {
613 if ((void*)cbe == (void*)Interpreter) {
614 std::string Result = executeProgramWithCBE("bugpoint.cbe.out");
615 std::cout << "\n*** The C backend cannot match the reference diff, but it "
616 << "is used as the 'known good'\n code generator, so I can't"
617 << " debug it. Perhaps you have a front-end problem?\n As a"
618 << " sanity check, I left the result of executing the program "
619 << "with the C backend\n in this file for you: '"
620 << Result << "'.\n";
621 return true;
622 }
623
624 DisambiguateGlobalSymbols(Program);
625
626 std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator);
627
628 // Split the module into the two halves of the program we want.
629 Module *ToNotCodeGen = CloneModule(getProgram());
630 Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs);
631
632 // Condition the modules
633 CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
634
635 std::string TestModuleBC = getUniqueFilename("bugpoint.test.bc");
636 if (writeProgramToFile(TestModuleBC, ToCodeGen)) {
637 std::cerr << "Error writing bytecode to `" << TestModuleBC << "'\nExiting.";
638 exit(1);
639 }
640 delete ToCodeGen;
641
642 // Make the shared library
643 std::string SafeModuleBC = getUniqueFilename("bugpoint.safe.bc");
644 if (writeProgramToFile(SafeModuleBC, ToNotCodeGen)) {
645 std::cerr << "Error writing bytecode to `" << SafeModuleBC << "'\nExiting.";
646 exit(1);
647 }
648 std::string SharedObject = compileSharedObject(SafeModuleBC);
649 delete ToNotCodeGen;
650
651 std::cout << "You can reproduce the problem with the command line: \n";
652 if (isExecutingJIT()) {
653 std::cout << " lli -load " << SharedObject << " " << TestModuleBC;
654 } else {
655 std::cout << " llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
656 std::cout << " gcc " << SharedObject << " " << TestModuleBC
657 << ".s -o " << TestModuleBC << ".exe -Wl,-R.\n";
658 std::cout << " " << TestModuleBC << ".exe";
659 }
660 for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
661 std::cout << " " << InputArgv[i];
662 std::cout << "\n";
663 std::cout << "The shared object was created with:\n llc -march=c "
664 << SafeModuleBC << " -o temporary.c\n"
665 << " gcc -xc temporary.c -O2 -o " << SharedObject
666#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
667 << " -G" // Compile a shared library, `-G' for Sparc
668#else
669 << " -shared" // `-shared' for Linux/X86, maybe others
670#endif
671 << " -fno-strict-aliasing\n";
672
673 return false;
674}