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