1 //===- Miscompilation.cpp - Debug program miscompilations -----------------===//
3 // This file implements program miscompilation debugging support.
5 //===----------------------------------------------------------------------===//
8 #include "SystemUtils.h"
9 #include "ListReducer.h"
10 #include "llvm/Pass.h"
11 #include "llvm/Module.h"
12 #include "llvm/Transforms/Utils/Cloning.h"
13 #include "llvm/Transforms/Utils/Linker.h"
14 #include "Support/CommandLine.h"
16 // Anonymous namespace to define command line options for miscompilation
20 // Output - The user can specify a file containing the expected output of the
21 // program. If this filename is set, it is used as the reference diff source,
22 // otherwise the raw input run through an interpreter is used as the reference
26 Output("output", cl::desc("Specify a reference program output "
27 "(for miscompilation detection)"));
30 class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
33 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
35 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
36 std::vector<const PassInfo*> &Kept);
39 ReduceMiscompilingPasses::TestResult
40 ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
41 std::vector<const PassInfo*> &Suffix) {
42 // First, run the program with just the Suffix passes. If it is still broken
43 // with JUST the kept passes, discard the prefix passes.
44 std::cout << "Checking to see if '" << getPassesString(Suffix)
45 << "' compile correctly: ";
47 std::string BytecodeResult;
48 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
49 std::cerr << BD.getToolName() << ": Error running this sequence of passes"
50 << " on the input program!\n";
54 // Check to see if the finished program matches the reference output...
55 if (BD.diffProgram(Output, BytecodeResult, true /*delete bytecode*/)) {
56 std::cout << "nope.\n";
57 return KeepSuffix; // Miscompilation detected!
59 std::cout << "yup.\n"; // No miscompilation!
61 if (Prefix.empty()) return NoFailure;
63 // Next, see if the program is broken if we run the "prefix" passes first,
64 // then seperately run the "kept" passes.
65 std::cout << "Checking to see if '" << getPassesString(Prefix)
66 << "' compile correctly: ";
68 // If it is not broken with the kept passes, it's possible that the prefix
69 // passes must be run before the kept passes to break it. If the program
70 // WORKS after the prefix passes, but then fails if running the prefix AND
71 // kept passes, we can update our bytecode file to include the result of the
72 // prefix passes, then discard the prefix passes.
74 if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
75 std::cerr << BD.getToolName() << ": Error running this sequence of passes"
76 << " on the input program!\n";
80 // If the prefix maintains the predicate by itself, only keep the prefix!
81 if (BD.diffProgram(Output, BytecodeResult)) {
82 std::cout << "nope.\n";
83 removeFile(BytecodeResult);
86 std::cout << "yup.\n"; // No miscompilation!
88 // Ok, so now we know that the prefix passes work, try running the suffix
89 // passes on the result of the prefix passes.
91 Module *PrefixOutput = BD.ParseInputFile(BytecodeResult);
92 if (PrefixOutput == 0) {
93 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
94 << BytecodeResult << "'!\n";
97 removeFile(BytecodeResult); // No longer need the file on disk
99 std::cout << "Checking to see if '" << getPassesString(Suffix)
100 << "' passes compile correctly after the '"
101 << getPassesString(Prefix) << "' passes: ";
103 Module *OriginalInput = BD.Program;
104 BD.Program = PrefixOutput;
105 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
106 std::cerr << BD.getToolName() << ": Error running this sequence of passes"
107 << " on the input program!\n";
112 if (BD.diffProgram(Output, BytecodeResult, true/*delete bytecode*/)) {
113 std::cout << "nope.\n";
114 delete OriginalInput; // We pruned down the original input...
118 // Otherwise, we must not be running the bad pass anymore.
119 std::cout << "yup.\n"; // No miscompilation!
120 BD.Program = OriginalInput; // Restore original program
121 delete PrefixOutput; // Free experiment
125 static void PrintFunctionList(const std::vector<Function*> &Funcs) {
126 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
127 if (i) std::cout << ", ";
128 std::cout << Funcs[i]->getName();
133 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
136 ReduceMiscompilingFunctions(BugDriver &bd) : BD(bd) {}
138 virtual TestResult doTest(std::vector<Function*> &Prefix,
139 std::vector<Function*> &Suffix) {
140 if (TestFuncs(Suffix, false))
142 if (!Prefix.empty() && TestFuncs(Prefix, false))
147 bool TestFuncs(const std::vector<Function*> &Prefix, bool EmitBytecode);
150 bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
152 // Test to see if the function is misoptimized if we ONLY run it on the
153 // functions listed in Funcs.
155 std::cout << "Checking to see if the program is misoptimized when these "
156 << "functions are run\nthrough the passes: ";
157 PrintFunctionList(Funcs);
160 std::cout <<"Outputting reduced bytecode files which expose the problem:\n";
163 // First step: clone the module for the two halves of the program we want.
164 Module *ToOptimize = CloneModule(BD.Program);
166 // Second step: Make sure functions & globals are all external so that linkage
167 // between the two modules will work.
168 for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
169 I->setLinkage(GlobalValue::ExternalLinkage);
170 for (Module::giterator I = ToOptimize->gbegin(), E = ToOptimize->gend();
172 I->setLinkage(GlobalValue::ExternalLinkage);
174 // Third step: make a clone of the externalized program for the non-optimized
176 Module *ToNotOptimize = CloneModule(ToOptimize);
178 // Fourth step: Remove the test functions from the ToNotOptimize module, and
179 // all of the global variables.
180 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
181 Function *TNOF = ToNotOptimize->getFunction(Funcs[i]->getName(),
182 Funcs[i]->getFunctionType());
183 assert(TNOF && "Function doesn't exist in module!");
184 DeleteFunctionBody(TNOF); // Function is now external in this module!
186 for (Module::giterator I = ToNotOptimize->gbegin(), E = ToNotOptimize->gend();
188 I->setInitializer(0); // Delete the initializer to make it external
191 std::cout << " Non-optimized portion: ";
192 std::swap(BD.Program, ToNotOptimize);
193 BD.EmitProgressBytecode("tonotoptimize", true);
194 std::swap(BD.Program, ToNotOptimize);
197 // Fifth step: Remove all functions from the ToOptimize module EXCEPT for the
198 // ones specified in Funcs. We know which ones these are because they are
199 // non-external in ToOptimize, but external in ToNotOptimize.
201 for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
202 if (!I->isExternal()) {
203 Function *TNOF = ToNotOptimize->getFunction(I->getName(),
204 I->getFunctionType());
205 assert(TNOF && "Function doesn't exist in ToNotOptimize module??");
206 if (!TNOF->isExternal())
207 DeleteFunctionBody(I);
211 std::cout << " Portion that is input to optimizer: ";
212 std::swap(BD.Program, ToOptimize);
213 BD.EmitProgressBytecode("tooptimize");
214 std::swap(BD.Program, ToOptimize);
217 // Sixth step: Run the optimization passes on ToOptimize, producing a
218 // transformed version of the functions being tested.
219 Module *OldProgram = BD.Program;
220 BD.Program = ToOptimize;
223 std::cout << " Optimizing functions being tested: ";
224 std::string BytecodeResult;
225 if (BD.runPasses(BD.PassesToRun, BytecodeResult, false/*delete*/,
227 std::cerr << BD.getToolName() << ": Error running this sequence of passes"
228 << " on the input program!\n";
233 std::cout << "done.\n";
235 delete BD.Program; // Delete the old "ToOptimize" module
236 BD.Program = BD.ParseInputFile(BytecodeResult);
239 std::cout << " 'tooptimize' after being optimized: ";
240 BD.EmitProgressBytecode("optimized", true);
243 if (BD.Program == 0) {
244 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
245 << BytecodeResult << "'!\n";
248 removeFile(BytecodeResult); // No longer need the file on disk
250 // Seventh step: Link the optimized part of the program back to the
251 // unoptimized part of the program.
253 if (LinkModules(BD.Program, ToNotOptimize, &BytecodeResult)) {
254 std::cerr << BD.getToolName() << ": Error linking modules together:"
255 << BytecodeResult << "\n";
258 delete ToNotOptimize; // We are done with this module...
261 std::cout << " Program as tested: ";
262 BD.EmitProgressBytecode("linked", true);
264 BD.Program = OldProgram;
265 return false; // We don't need to actually execute the program here.
268 std::cout << " Checking to see if the merged program executes correctly: ";
270 // Eighth step: Execute the program. If it does not match the expected
271 // output, then 'Funcs' are being misoptimized!
272 bool Broken = BD.diffProgram(Output);
274 delete BD.Program; // Delete the hacked up program
275 BD.Program = OldProgram; // Restore the original
277 std::cout << (Broken ? "nope.\n" : "yup.\n");
282 /// debugMiscompilation - This method is used when the passes selected are not
283 /// crashing, but the generated output is semantically different from the
286 bool BugDriver::debugMiscompilation() {
287 std::cout << "*** Debugging miscompilation!\n";
289 // Set up the execution environment, selecting a method to run LLVM bytecode.
290 if (initializeExecutionEnvironment()) return true;
292 // Run the raw input to see where we are coming from. If a reference output
293 // was specified, make sure that the raw output matches it. If not, it's a
294 // problem in the front-end or whatever produced the input code.
296 bool CreatedOutput = false;
297 if (Output.empty()) {
298 std::cout << "Generating reference output from raw program...";
299 Output = executeProgram("bugpoint.reference.out");
300 CreatedOutput = true;
301 std::cout << " done! Reference output is: " << Output << "\n";
302 } else if (diffProgram(Output)) {
303 std::cout << "\n*** Input program does not match reference diff!\n"
304 << " Must be problem with input source!\n";
305 return false; // Problem found
308 // Figure out which transformations miscompile the input program.
309 unsigned OldSize = PassesToRun.size();
310 ReduceMiscompilingPasses(*this).reduceList(PassesToRun);
312 // Make sure something was miscompiled...
313 if (PassesToRun.size() == OldSize) {
314 std::cerr << "*** Optimized program matches reference output! No problem "
315 << "detected...\nbugpoint can't help you with your problem!\n";
319 std::cout << "\n*** Found miscompiling pass"
320 << (PassesToRun.size() == 1 ? "" : "es") << ": "
321 << getPassesString(PassesToRun) << "\n";
322 EmitProgressBytecode("passinput");
325 // Okay, now that we have reduced the list of passes which are causing the
326 // failure, see if we can pin down which functions are being
327 // miscompiled... first build a list of all of the non-external functions in
329 std::vector<Function*> MiscompiledFunctions;
330 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
331 if (!I->isExternal())
332 MiscompiledFunctions.push_back(I);
334 // Do the reduction...
335 ReduceMiscompilingFunctions(*this).reduceList(MiscompiledFunctions);
337 std::cout << "\n*** The following functions are being miscompiled: ";
338 PrintFunctionList(MiscompiledFunctions);
341 // Output a bunch of bytecode files for the user...
342 ReduceMiscompilingFunctions(*this).TestFuncs(MiscompiledFunctions, true);
344 if (CreatedOutput) removeFile(Output);