1 //===- Miscompilation.cpp - Debug program miscompilations -----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements program miscompilation debugging support.
12 //===----------------------------------------------------------------------===//
14 #include "BugDriver.h"
15 #include "ListReducer.h"
16 #include "llvm/Module.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Transforms/Utils/Cloning.h"
19 #include "llvm/Transforms/Utils/Linker.h"
20 #include "Support/FileUtilities.h"
24 class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
27 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
29 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
30 std::vector<const PassInfo*> &Suffix);
33 ReduceMiscompilingPasses::TestResult
34 ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
35 std::vector<const PassInfo*> &Suffix) {
36 // First, run the program with just the Suffix passes. If it is still broken
37 // with JUST the kept passes, discard the prefix passes.
38 std::cout << "Checking to see if '" << getPassesString(Suffix)
39 << "' compile correctly: ";
41 std::string BytecodeResult;
42 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
43 std::cerr << " Error running this sequence of passes"
44 << " on the input program!\n";
45 BD.setPassesToRun(Suffix);
46 BD.EmitProgressBytecode("pass-error", false);
47 exit(BD.debugCrash());
50 // Check to see if the finished program matches the reference output...
51 if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) {
52 std::cout << "nope.\n";
53 return KeepSuffix; // Miscompilation detected!
55 std::cout << "yup.\n"; // No miscompilation!
57 if (Prefix.empty()) return NoFailure;
59 // Next, see if the program is broken if we run the "prefix" passes first,
60 // then separately run the "kept" passes.
61 std::cout << "Checking to see if '" << getPassesString(Prefix)
62 << "' compile correctly: ";
64 // If it is not broken with the kept passes, it's possible that the prefix
65 // passes must be run before the kept passes to break it. If the program
66 // WORKS after the prefix passes, but then fails if running the prefix AND
67 // kept passes, we can update our bytecode file to include the result of the
68 // prefix passes, then discard the prefix passes.
70 if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
71 std::cerr << " Error running this sequence of passes"
72 << " on the input program!\n";
73 BD.setPassesToRun(Prefix);
74 BD.EmitProgressBytecode("pass-error", false);
75 exit(BD.debugCrash());
78 // If the prefix maintains the predicate by itself, only keep the prefix!
79 if (BD.diffProgram(BytecodeResult)) {
80 std::cout << "nope.\n";
81 removeFile(BytecodeResult);
84 std::cout << "yup.\n"; // No miscompilation!
86 // Ok, so now we know that the prefix passes work, try running the suffix
87 // passes on the result of the prefix passes.
89 Module *PrefixOutput = BD.ParseInputFile(BytecodeResult);
90 if (PrefixOutput == 0) {
91 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
92 << BytecodeResult << "'!\n";
95 removeFile(BytecodeResult); // No longer need the file on disk
97 std::cout << "Checking to see if '" << getPassesString(Suffix)
98 << "' passes compile correctly after the '"
99 << getPassesString(Prefix) << "' passes: ";
101 Module *OriginalInput = BD.Program;
102 BD.Program = PrefixOutput;
103 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
104 std::cerr << " Error running this sequence of passes"
105 << " on the input program!\n";
106 BD.setPassesToRun(Suffix);
107 BD.EmitProgressBytecode("pass-error", false);
108 exit(BD.debugCrash());
112 if (BD.diffProgram(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 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
128 ReduceMiscompilingFunctions(BugDriver &bd) : BD(bd) {}
130 virtual TestResult doTest(std::vector<Function*> &Prefix,
131 std::vector<Function*> &Suffix) {
132 if (!Suffix.empty() && TestFuncs(Suffix, false))
134 if (!Prefix.empty() && TestFuncs(Prefix, false))
139 bool TestFuncs(const std::vector<Function*> &Prefix, bool EmitBytecode);
142 bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
144 // Test to see if the function is misoptimized if we ONLY run it on the
145 // functions listed in Funcs.
147 std::cout << "Checking to see if the program is misoptimized when these "
148 << "functions are run\nthrough the passes: ";
149 BD.PrintFunctionList(Funcs);
152 std::cout <<"Outputting reduced bytecode files which expose the problem:\n";
155 // First step: clone the module for the two halves of the program we want.
156 Module *ToOptimize = CloneModule(BD.Program);
158 // Second step: Make sure functions & globals are all external so that linkage
159 // between the two modules will work.
160 for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
161 I->setLinkage(GlobalValue::ExternalLinkage);
162 for (Module::giterator I = ToOptimize->gbegin(), E = ToOptimize->gend();
164 I->setLinkage(GlobalValue::ExternalLinkage);
166 // Third step: make a clone of the externalized program for the non-optimized
168 Module *ToNotOptimize = CloneModule(ToOptimize);
170 // Fourth step: Remove the test functions from the ToNotOptimize module, and
171 // all of the global variables.
172 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
173 Function *TNOF = ToNotOptimize->getFunction(Funcs[i]->getName(),
174 Funcs[i]->getFunctionType());
175 assert(TNOF && "Function doesn't exist in module!");
176 DeleteFunctionBody(TNOF); // Function is now external in this module!
178 for (Module::giterator I = ToNotOptimize->gbegin(), E = ToNotOptimize->gend();
180 I->setInitializer(0); // Delete the initializer to make it external
183 std::cout << " Non-optimized portion: ";
184 std::swap(BD.Program, ToNotOptimize);
185 BD.EmitProgressBytecode("tonotoptimize", true);
186 std::swap(BD.Program, ToNotOptimize);
189 // Fifth step: Remove all functions from the ToOptimize module EXCEPT for the
190 // ones specified in Funcs. We know which ones these are because they are
191 // non-external in ToOptimize, but external in ToNotOptimize.
193 for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
194 if (!I->isExternal()) {
195 Function *TNOF = ToNotOptimize->getFunction(I->getName(),
196 I->getFunctionType());
197 assert(TNOF && "Function doesn't exist in ToNotOptimize module??");
198 if (!TNOF->isExternal())
199 DeleteFunctionBody(I);
203 std::cout << " Portion that is input to optimizer: ";
204 std::swap(BD.Program, ToOptimize);
205 BD.EmitProgressBytecode("tooptimize");
206 std::swap(BD.Program, ToOptimize);
209 // Sixth step: Run the optimization passes on ToOptimize, producing a
210 // transformed version of the functions being tested.
211 Module *OldProgram = BD.Program;
212 BD.Program = ToOptimize;
215 std::cout << " Optimizing functions being tested: ";
216 std::string BytecodeResult;
217 if (BD.runPasses(BD.PassesToRun, BytecodeResult, false/*delete*/,
219 std::cerr << " Error running this sequence of passes"
220 << " on the input program!\n";
221 BD.EmitProgressBytecode("pass-error", false);
222 exit(BD.debugCrash());
226 std::cout << "done.\n";
228 delete BD.Program; // Delete the old "ToOptimize" module
229 BD.Program = BD.ParseInputFile(BytecodeResult);
232 std::cout << " 'tooptimize' after being optimized: ";
233 BD.EmitProgressBytecode("optimized", true);
236 if (BD.Program == 0) {
237 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
238 << BytecodeResult << "'!\n";
241 removeFile(BytecodeResult); // No longer need the file on disk
243 // Seventh step: Link the optimized part of the program back to the
244 // unoptimized part of the program.
246 if (LinkModules(BD.Program, ToNotOptimize, &BytecodeResult)) {
247 std::cerr << BD.getToolName() << ": Error linking modules together:"
248 << BytecodeResult << "\n";
251 delete ToNotOptimize; // We are done with this module...
254 std::cout << " Program as tested: ";
255 BD.EmitProgressBytecode("linked", true);
257 BD.Program = OldProgram;
258 return false; // We don't need to actually execute the program here.
261 std::cout << " Checking to see if the merged program executes correctly: ";
263 // Eighth step: Execute the program. If it does not match the expected
264 // output, then 'Funcs' are being misoptimized!
265 bool Broken = BD.diffProgram();
267 delete BD.Program; // Delete the hacked up program
268 BD.Program = OldProgram; // Restore the original
270 std::cout << (Broken ? "nope.\n" : "yup.\n");
275 /// debugMiscompilation - This method is used when the passes selected are not
276 /// crashing, but the generated output is semantically different from the
279 bool BugDriver::debugMiscompilation() {
280 // Make sure something was miscompiled...
281 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
282 std::cerr << "*** Optimized program matches reference output! No problem "
283 << "detected...\nbugpoint can't help you with your problem!\n";
287 std::cout << "\n*** Found miscompiling pass"
288 << (PassesToRun.size() == 1 ? "" : "es") << ": "
289 << getPassesString(PassesToRun) << "\n";
290 EmitProgressBytecode("passinput");
292 // Okay, now that we have reduced the list of passes which are causing the
293 // failure, see if we can pin down which functions are being
294 // miscompiled... first build a list of all of the non-external functions in
296 std::vector<Function*> MiscompiledFunctions;
297 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
298 if (!I->isExternal())
299 MiscompiledFunctions.push_back(I);
301 // Do the reduction...
302 ReduceMiscompilingFunctions(*this).reduceList(MiscompiledFunctions);
304 std::cout << "\n*** The following functions are being miscompiled: ";
305 PrintFunctionList(MiscompiledFunctions);
308 // Output a bunch of bytecode files for the user...
309 ReduceMiscompilingFunctions(*this).TestFuncs(MiscompiledFunctions, true);
314 } // End llvm namespace