blob: 5ec8b1543e36ac8cec8974c3c72990bc58b468ea [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//
10// This file implements program miscompilation debugging support.
11//
12//===----------------------------------------------------------------------===//
13
14#include "BugDriver.h"
Chris Lattner126840f2003-04-24 20:16:29 +000015#include "ListReducer.h"
Chris Lattner4a106452002-12-23 23:50:16 +000016#include "llvm/Module.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000017#include "llvm/Pass.h"
Chris Lattner640f22e2003-04-24 17:02:17 +000018#include "llvm/Transforms/Utils/Cloning.h"
19#include "llvm/Transforms/Utils/Linker.h"
Misha Brukman3d9cafa2003-08-07 21:42:28 +000020#include "Support/FileUtilities.h"
Chris Lattner4a106452002-12-23 23:50:16 +000021
Chris Lattner640f22e2003-04-24 17:02:17 +000022class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
23 BugDriver &BD;
24public:
25 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
26
Chris Lattner39aebca2003-04-24 22:24:22 +000027 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
Misha Brukman50733362003-07-24 18:17:43 +000028 std::vector<const PassInfo*> &Suffix);
Chris Lattner640f22e2003-04-24 17:02:17 +000029};
30
31ReduceMiscompilingPasses::TestResult
Chris Lattner39aebca2003-04-24 22:24:22 +000032ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
Chris Lattner06943ad2003-04-25 03:16:05 +000033 std::vector<const PassInfo*> &Suffix) {
34 // First, run the program with just the Suffix passes. If it is still broken
Chris Lattner640f22e2003-04-24 17:02:17 +000035 // with JUST the kept passes, discard the prefix passes.
Chris Lattner06943ad2003-04-25 03:16:05 +000036 std::cout << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +000037 << "' compile correctly: ";
38
39 std::string BytecodeResult;
Chris Lattner06943ad2003-04-25 03:16:05 +000040 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +000041 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000042 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +000043 BD.setPassesToRun(Suffix);
44 BD.EmitProgressBytecode("pass-error", false);
Chris Lattnera12c06a2003-10-18 19:27:48 +000045 exit(BD.debugCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000046 }
47
48 // Check to see if the finished program matches the reference output...
Misha Brukman50733362003-07-24 18:17:43 +000049 if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) {
Chris Lattner640f22e2003-04-24 17:02:17 +000050 std::cout << "nope.\n";
51 return KeepSuffix; // Miscompilation detected!
52 }
53 std::cout << "yup.\n"; // No miscompilation!
54
55 if (Prefix.empty()) return NoFailure;
56
Chris Lattner06943ad2003-04-25 03:16:05 +000057 // Next, see if the program is broken if we run the "prefix" passes first,
Misha Brukmanbc0e9982003-07-14 17:20:40 +000058 // then separately run the "kept" passes.
Chris Lattner640f22e2003-04-24 17:02:17 +000059 std::cout << "Checking to see if '" << getPassesString(Prefix)
60 << "' compile correctly: ";
61
62 // If it is not broken with the kept passes, it's possible that the prefix
63 // passes must be run before the kept passes to break it. If the program
64 // WORKS after the prefix passes, but then fails if running the prefix AND
65 // kept passes, we can update our bytecode file to include the result of the
66 // prefix passes, then discard the prefix passes.
67 //
68 if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +000069 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000070 << " on the input program!\n";
Chris Lattner9c6cfe12003-10-17 23:03:16 +000071 BD.setPassesToRun(Prefix);
72 BD.EmitProgressBytecode("pass-error", false);
Chris Lattnera12c06a2003-10-18 19:27:48 +000073 exit(BD.debugCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000074 }
75
76 // If the prefix maintains the predicate by itself, only keep the prefix!
Misha Brukman50733362003-07-24 18:17:43 +000077 if (BD.diffProgram(BytecodeResult)) {
Chris Lattner640f22e2003-04-24 17:02:17 +000078 std::cout << "nope.\n";
79 removeFile(BytecodeResult);
80 return KeepPrefix;
81 }
82 std::cout << "yup.\n"; // No miscompilation!
83
84 // Ok, so now we know that the prefix passes work, try running the suffix
85 // passes on the result of the prefix passes.
86 //
87 Module *PrefixOutput = BD.ParseInputFile(BytecodeResult);
88 if (PrefixOutput == 0) {
89 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
90 << BytecodeResult << "'!\n";
91 exit(1);
92 }
93 removeFile(BytecodeResult); // No longer need the file on disk
94
Chris Lattner06943ad2003-04-25 03:16:05 +000095 std::cout << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +000096 << "' passes compile correctly after the '"
97 << getPassesString(Prefix) << "' passes: ";
98
99 Module *OriginalInput = BD.Program;
100 BD.Program = PrefixOutput;
Chris Lattner06943ad2003-04-25 03:16:05 +0000101 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +0000102 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +0000103 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000104 BD.setPassesToRun(Suffix);
Chris Lattner9c6cfe12003-10-17 23:03:16 +0000105 BD.EmitProgressBytecode("pass-error", false);
Chris Lattnera12c06a2003-10-18 19:27:48 +0000106 exit(BD.debugCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000107 }
108
109 // Run the result...
Misha Brukman50733362003-07-24 18:17:43 +0000110 if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) {
Chris Lattner640f22e2003-04-24 17:02:17 +0000111 std::cout << "nope.\n";
112 delete OriginalInput; // We pruned down the original input...
Chris Lattner06943ad2003-04-25 03:16:05 +0000113 return KeepSuffix;
Chris Lattner640f22e2003-04-24 17:02:17 +0000114 }
115
116 // Otherwise, we must not be running the bad pass anymore.
117 std::cout << "yup.\n"; // No miscompilation!
118 BD.Program = OriginalInput; // Restore original program
119 delete PrefixOutput; // Free experiment
120 return NoFailure;
121}
122
Chris Lattner640f22e2003-04-24 17:02:17 +0000123class ReduceMiscompilingFunctions : public ListReducer<Function*> {
124 BugDriver &BD;
125public:
126 ReduceMiscompilingFunctions(BugDriver &bd) : BD(bd) {}
127
Chris Lattner39aebca2003-04-24 22:24:22 +0000128 virtual TestResult doTest(std::vector<Function*> &Prefix,
Chris Lattner06943ad2003-04-25 03:16:05 +0000129 std::vector<Function*> &Suffix) {
Misha Brukman5d3f1f02003-08-04 19:03:42 +0000130 if (!Suffix.empty() && TestFuncs(Suffix, false))
Chris Lattner640f22e2003-04-24 17:02:17 +0000131 return KeepSuffix;
Chris Lattnera148ccb2003-04-24 19:32:42 +0000132 if (!Prefix.empty() && TestFuncs(Prefix, false))
Chris Lattner640f22e2003-04-24 17:02:17 +0000133 return KeepPrefix;
134 return NoFailure;
135 }
136
137 bool TestFuncs(const std::vector<Function*> &Prefix, bool EmitBytecode);
138};
139
Chris Lattner640f22e2003-04-24 17:02:17 +0000140bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
141 bool EmitBytecode) {
142 // Test to see if the function is misoptimized if we ONLY run it on the
143 // functions listed in Funcs.
144 if (!EmitBytecode) {
145 std::cout << "Checking to see if the program is misoptimized when these "
146 << "functions are run\nthrough the passes: ";
Misha Brukman50733362003-07-24 18:17:43 +0000147 BD.PrintFunctionList(Funcs);
Chris Lattner640f22e2003-04-24 17:02:17 +0000148 std::cout << "\n";
149 } else {
150 std::cout <<"Outputting reduced bytecode files which expose the problem:\n";
151 }
152
153 // First step: clone the module for the two halves of the program we want.
154 Module *ToOptimize = CloneModule(BD.Program);
155
156 // Second step: Make sure functions & globals are all external so that linkage
157 // between the two modules will work.
158 for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
159 I->setLinkage(GlobalValue::ExternalLinkage);
160 for (Module::giterator I = ToOptimize->gbegin(), E = ToOptimize->gend();
161 I != E; ++I)
162 I->setLinkage(GlobalValue::ExternalLinkage);
163
164 // Third step: make a clone of the externalized program for the non-optimized
165 // part.
166 Module *ToNotOptimize = CloneModule(ToOptimize);
167
168 // Fourth step: Remove the test functions from the ToNotOptimize module, and
169 // all of the global variables.
170 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
171 Function *TNOF = ToNotOptimize->getFunction(Funcs[i]->getName(),
172 Funcs[i]->getFunctionType());
173 assert(TNOF && "Function doesn't exist in module!");
174 DeleteFunctionBody(TNOF); // Function is now external in this module!
175 }
176 for (Module::giterator I = ToNotOptimize->gbegin(), E = ToNotOptimize->gend();
177 I != E; ++I)
178 I->setInitializer(0); // Delete the initializer to make it external
179
180 if (EmitBytecode) {
181 std::cout << " Non-optimized portion: ";
182 std::swap(BD.Program, ToNotOptimize);
183 BD.EmitProgressBytecode("tonotoptimize", true);
184 std::swap(BD.Program, ToNotOptimize);
185 }
186
187 // Fifth step: Remove all functions from the ToOptimize module EXCEPT for the
188 // ones specified in Funcs. We know which ones these are because they are
189 // non-external in ToOptimize, but external in ToNotOptimize.
190 //
191 for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
192 if (!I->isExternal()) {
193 Function *TNOF = ToNotOptimize->getFunction(I->getName(),
194 I->getFunctionType());
195 assert(TNOF && "Function doesn't exist in ToNotOptimize module??");
196 if (!TNOF->isExternal())
197 DeleteFunctionBody(I);
198 }
199
200 if (EmitBytecode) {
201 std::cout << " Portion that is input to optimizer: ";
202 std::swap(BD.Program, ToOptimize);
203 BD.EmitProgressBytecode("tooptimize");
204 std::swap(BD.Program, ToOptimize);
205 }
206
207 // Sixth step: Run the optimization passes on ToOptimize, producing a
208 // transformed version of the functions being tested.
209 Module *OldProgram = BD.Program;
210 BD.Program = ToOptimize;
211
212 if (!EmitBytecode)
213 std::cout << " Optimizing functions being tested: ";
214 std::string BytecodeResult;
215 if (BD.runPasses(BD.PassesToRun, BytecodeResult, false/*delete*/,
216 true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +0000217 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +0000218 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000219 BD.EmitProgressBytecode("pass-error", false);
Chris Lattnera12c06a2003-10-18 19:27:48 +0000220 exit(BD.debugCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000221 }
222
223 if (!EmitBytecode)
224 std::cout << "done.\n";
225
226 delete BD.Program; // Delete the old "ToOptimize" module
227 BD.Program = BD.ParseInputFile(BytecodeResult);
228
229 if (EmitBytecode) {
230 std::cout << " 'tooptimize' after being optimized: ";
231 BD.EmitProgressBytecode("optimized", true);
232 }
233
234 if (BD.Program == 0) {
235 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
236 << BytecodeResult << "'!\n";
237 exit(1);
238 }
239 removeFile(BytecodeResult); // No longer need the file on disk
240
241 // Seventh step: Link the optimized part of the program back to the
242 // unoptimized part of the program.
243 //
244 if (LinkModules(BD.Program, ToNotOptimize, &BytecodeResult)) {
245 std::cerr << BD.getToolName() << ": Error linking modules together:"
246 << BytecodeResult << "\n";
247 exit(1);
248 }
249 delete ToNotOptimize; // We are done with this module...
250
251 if (EmitBytecode) {
252 std::cout << " Program as tested: ";
253 BD.EmitProgressBytecode("linked", true);
254 delete BD.Program;
255 BD.Program = OldProgram;
256 return false; // We don't need to actually execute the program here.
257 }
258
259 std::cout << " Checking to see if the merged program executes correctly: ";
260
261 // Eighth step: Execute the program. If it does not match the expected
262 // output, then 'Funcs' are being misoptimized!
Misha Brukman50733362003-07-24 18:17:43 +0000263 bool Broken = BD.diffProgram();
Chris Lattner640f22e2003-04-24 17:02:17 +0000264
265 delete BD.Program; // Delete the hacked up program
266 BD.Program = OldProgram; // Restore the original
267
268 std::cout << (Broken ? "nope.\n" : "yup.\n");
269 return Broken;
270}
271
272
Chris Lattner4a106452002-12-23 23:50:16 +0000273/// debugMiscompilation - This method is used when the passes selected are not
274/// crashing, but the generated output is semantically different from the
275/// input.
276///
277bool BugDriver::debugMiscompilation() {
Chris Lattner4a106452002-12-23 23:50:16 +0000278 // Make sure something was miscompiled...
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000279 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
Chris Lattner4a106452002-12-23 23:50:16 +0000280 std::cerr << "*** Optimized program matches reference output! No problem "
281 << "detected...\nbugpoint can't help you with your problem!\n";
282 return false;
283 }
284
Chris Lattner640f22e2003-04-24 17:02:17 +0000285 std::cout << "\n*** Found miscompiling pass"
286 << (PassesToRun.size() == 1 ? "" : "es") << ": "
287 << getPassesString(PassesToRun) << "\n";
288 EmitProgressBytecode("passinput");
Chris Lattner4a106452002-12-23 23:50:16 +0000289
Chris Lattner640f22e2003-04-24 17:02:17 +0000290 // Okay, now that we have reduced the list of passes which are causing the
291 // failure, see if we can pin down which functions are being
292 // miscompiled... first build a list of all of the non-external functions in
293 // the program.
294 std::vector<Function*> MiscompiledFunctions;
295 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
296 if (!I->isExternal())
297 MiscompiledFunctions.push_back(I);
298
299 // Do the reduction...
300 ReduceMiscompilingFunctions(*this).reduceList(MiscompiledFunctions);
301
302 std::cout << "\n*** The following functions are being miscompiled: ";
303 PrintFunctionList(MiscompiledFunctions);
304 std::cout << "\n";
305
306 // Output a bunch of bytecode files for the user...
307 ReduceMiscompilingFunctions(*this).TestFuncs(MiscompiledFunctions, true);
Chris Lattner4a106452002-12-23 23:50:16 +0000308
Chris Lattner4a106452002-12-23 23:50:16 +0000309 return false;
310}