blob: 98e299d76d82ddce5d5677e024ddf90ddf2ab21d [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 Lattnerfa761832004-01-14 03:38:37 +000021using namespace llvm;
Chris Lattner4a106452002-12-23 23:50:16 +000022
Brian Gaeked0fde302003-11-11 22:41:34 +000023namespace llvm {
24
Chris Lattnerfa761832004-01-14 03:38:37 +000025 class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
26 BugDriver &BD;
27 public:
28 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
29
30 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
31 std::vector<const PassInfo*> &Suffix);
32 };
33}
Chris Lattner640f22e2003-04-24 17:02:17 +000034
35ReduceMiscompilingPasses::TestResult
Chris Lattner39aebca2003-04-24 22:24:22 +000036ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
Chris Lattner06943ad2003-04-25 03:16:05 +000037 std::vector<const PassInfo*> &Suffix) {
38 // First, run the program with just the Suffix passes. If it is still broken
Chris Lattner640f22e2003-04-24 17:02:17 +000039 // with JUST the kept passes, discard the prefix passes.
Chris Lattner06943ad2003-04-25 03:16:05 +000040 std::cout << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +000041 << "' compile correctly: ";
42
43 std::string BytecodeResult;
Chris Lattner06943ad2003-04-25 03:16:05 +000044 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +000045 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000046 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +000047 BD.setPassesToRun(Suffix);
48 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +000049 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000050 }
51
52 // Check to see if the finished program matches the reference output...
Misha Brukman50733362003-07-24 18:17:43 +000053 if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) {
Chris Lattner640f22e2003-04-24 17:02:17 +000054 std::cout << "nope.\n";
55 return KeepSuffix; // Miscompilation detected!
56 }
57 std::cout << "yup.\n"; // No miscompilation!
58
59 if (Prefix.empty()) return NoFailure;
60
Chris Lattner06943ad2003-04-25 03:16:05 +000061 // Next, see if the program is broken if we run the "prefix" passes first,
Misha Brukmanbc0e9982003-07-14 17:20:40 +000062 // then separately run the "kept" passes.
Chris Lattner640f22e2003-04-24 17:02:17 +000063 std::cout << "Checking to see if '" << getPassesString(Prefix)
64 << "' compile correctly: ";
65
66 // If it is not broken with the kept passes, it's possible that the prefix
67 // passes must be run before the kept passes to break it. If the program
68 // WORKS after the prefix passes, but then fails if running the prefix AND
69 // kept passes, we can update our bytecode file to include the result of the
70 // prefix passes, then discard the prefix passes.
71 //
72 if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +000073 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000074 << " on the input program!\n";
Chris Lattner9c6cfe12003-10-17 23:03:16 +000075 BD.setPassesToRun(Prefix);
76 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +000077 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000078 }
79
80 // If the prefix maintains the predicate by itself, only keep the prefix!
Misha Brukman50733362003-07-24 18:17:43 +000081 if (BD.diffProgram(BytecodeResult)) {
Chris Lattner640f22e2003-04-24 17:02:17 +000082 std::cout << "nope.\n";
83 removeFile(BytecodeResult);
84 return KeepPrefix;
85 }
86 std::cout << "yup.\n"; // No miscompilation!
87
88 // Ok, so now we know that the prefix passes work, try running the suffix
89 // passes on the result of the prefix passes.
90 //
91 Module *PrefixOutput = BD.ParseInputFile(BytecodeResult);
92 if (PrefixOutput == 0) {
93 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
94 << BytecodeResult << "'!\n";
95 exit(1);
96 }
97 removeFile(BytecodeResult); // No longer need the file on disk
98
Chris Lattner06943ad2003-04-25 03:16:05 +000099 std::cout << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +0000100 << "' passes compile correctly after the '"
101 << getPassesString(Prefix) << "' passes: ";
102
103 Module *OriginalInput = BD.Program;
104 BD.Program = PrefixOutput;
Chris Lattner06943ad2003-04-25 03:16:05 +0000105 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +0000106 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +0000107 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000108 BD.setPassesToRun(Suffix);
Chris Lattner9c6cfe12003-10-17 23:03:16 +0000109 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000110 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000111 }
112
113 // Run the result...
Misha Brukman50733362003-07-24 18:17:43 +0000114 if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) {
Chris Lattner640f22e2003-04-24 17:02:17 +0000115 std::cout << "nope.\n";
116 delete OriginalInput; // We pruned down the original input...
Chris Lattner06943ad2003-04-25 03:16:05 +0000117 return KeepSuffix;
Chris Lattner640f22e2003-04-24 17:02:17 +0000118 }
119
120 // Otherwise, we must not be running the bad pass anymore.
121 std::cout << "yup.\n"; // No miscompilation!
122 BD.Program = OriginalInput; // Restore original program
123 delete PrefixOutput; // Free experiment
124 return NoFailure;
125}
126
Chris Lattnerfa761832004-01-14 03:38:37 +0000127namespace llvm {
128 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
129 BugDriver &BD;
130 public:
131 ReduceMiscompilingFunctions(BugDriver &bd) : BD(bd) {}
132
133 virtual TestResult doTest(std::vector<Function*> &Prefix,
134 std::vector<Function*> &Suffix) {
135 if (!Suffix.empty() && TestFuncs(Suffix, false))
136 return KeepSuffix;
137 if (!Prefix.empty() && TestFuncs(Prefix, false))
138 return KeepPrefix;
139 return NoFailure;
140 }
141
142 bool TestFuncs(const std::vector<Function*> &Prefix, bool EmitBytecode);
143 };
144}
Chris Lattner640f22e2003-04-24 17:02:17 +0000145
Chris Lattner640f22e2003-04-24 17:02:17 +0000146bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
147 bool EmitBytecode) {
148 // Test to see if the function is misoptimized if we ONLY run it on the
149 // functions listed in Funcs.
150 if (!EmitBytecode) {
Chris Lattnerde9750d2003-12-07 02:43:09 +0000151 std::cout << "Checking to see if the program is misoptimized when "
152 << (Funcs.size()==1 ? "this function is" : "these functions are")
153 << " run through the pass"
154 << (BD.PassesToRun.size() == 1 ? "" : "es") << ": ";
Misha Brukman50733362003-07-24 18:17:43 +0000155 BD.PrintFunctionList(Funcs);
Chris Lattner640f22e2003-04-24 17:02:17 +0000156 std::cout << "\n";
157 } else {
158 std::cout <<"Outputting reduced bytecode files which expose the problem:\n";
159 }
160
161 // First step: clone the module for the two halves of the program we want.
Chris Lattnerf913f402004-02-18 21:29:46 +0000162 Module *ToOptimize = CloneModule(BD.getProgram());
Chris Lattner640f22e2003-04-24 17:02:17 +0000163
164 // Second step: Make sure functions & globals are all external so that linkage
165 // between the two modules will work.
166 for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
167 I->setLinkage(GlobalValue::ExternalLinkage);
168 for (Module::giterator I = ToOptimize->gbegin(), E = ToOptimize->gend();
169 I != E; ++I)
170 I->setLinkage(GlobalValue::ExternalLinkage);
171
172 // Third step: make a clone of the externalized program for the non-optimized
173 // part.
174 Module *ToNotOptimize = CloneModule(ToOptimize);
175
176 // Fourth step: Remove the test functions from the ToNotOptimize module, and
177 // all of the global variables.
178 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
179 Function *TNOF = ToNotOptimize->getFunction(Funcs[i]->getName(),
180 Funcs[i]->getFunctionType());
181 assert(TNOF && "Function doesn't exist in module!");
182 DeleteFunctionBody(TNOF); // Function is now external in this module!
183 }
184 for (Module::giterator I = ToNotOptimize->gbegin(), E = ToNotOptimize->gend();
185 I != E; ++I)
186 I->setInitializer(0); // Delete the initializer to make it external
187
188 if (EmitBytecode) {
189 std::cout << " Non-optimized portion: ";
190 std::swap(BD.Program, ToNotOptimize);
191 BD.EmitProgressBytecode("tonotoptimize", true);
192 std::swap(BD.Program, ToNotOptimize);
193 }
194
195 // Fifth step: Remove all functions from the ToOptimize module EXCEPT for the
196 // ones specified in Funcs. We know which ones these are because they are
197 // non-external in ToOptimize, but external in ToNotOptimize.
198 //
199 for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
200 if (!I->isExternal()) {
201 Function *TNOF = ToNotOptimize->getFunction(I->getName(),
202 I->getFunctionType());
203 assert(TNOF && "Function doesn't exist in ToNotOptimize module??");
204 if (!TNOF->isExternal())
205 DeleteFunctionBody(I);
206 }
207
208 if (EmitBytecode) {
209 std::cout << " Portion that is input to optimizer: ";
210 std::swap(BD.Program, ToOptimize);
211 BD.EmitProgressBytecode("tooptimize");
212 std::swap(BD.Program, ToOptimize);
213 }
214
215 // Sixth step: Run the optimization passes on ToOptimize, producing a
216 // transformed version of the functions being tested.
217 Module *OldProgram = BD.Program;
218 BD.Program = ToOptimize;
219
220 if (!EmitBytecode)
221 std::cout << " Optimizing functions being tested: ";
222 std::string BytecodeResult;
223 if (BD.runPasses(BD.PassesToRun, BytecodeResult, false/*delete*/,
224 true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +0000225 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +0000226 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000227 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000228 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000229 }
230
231 if (!EmitBytecode)
232 std::cout << "done.\n";
233
Chris Lattnerf913f402004-02-18 21:29:46 +0000234 delete BD.getProgram(); // Delete the old "ToOptimize" module
Chris Lattner640f22e2003-04-24 17:02:17 +0000235 BD.Program = BD.ParseInputFile(BytecodeResult);
236
237 if (EmitBytecode) {
238 std::cout << " 'tooptimize' after being optimized: ";
239 BD.EmitProgressBytecode("optimized", true);
240 }
241
242 if (BD.Program == 0) {
243 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
244 << BytecodeResult << "'!\n";
245 exit(1);
246 }
247 removeFile(BytecodeResult); // No longer need the file on disk
248
249 // Seventh step: Link the optimized part of the program back to the
250 // unoptimized part of the program.
251 //
252 if (LinkModules(BD.Program, ToNotOptimize, &BytecodeResult)) {
253 std::cerr << BD.getToolName() << ": Error linking modules together:"
254 << BytecodeResult << "\n";
255 exit(1);
256 }
257 delete ToNotOptimize; // We are done with this module...
258
259 if (EmitBytecode) {
260 std::cout << " Program as tested: ";
261 BD.EmitProgressBytecode("linked", true);
262 delete BD.Program;
263 BD.Program = OldProgram;
264 return false; // We don't need to actually execute the program here.
265 }
266
267 std::cout << " Checking to see if the merged program executes correctly: ";
268
269 // Eighth step: Execute the program. If it does not match the expected
270 // output, then 'Funcs' are being misoptimized!
Misha Brukman50733362003-07-24 18:17:43 +0000271 bool Broken = BD.diffProgram();
Chris Lattner640f22e2003-04-24 17:02:17 +0000272
Chris Lattnerf913f402004-02-18 21:29:46 +0000273 delete BD.Program; // Delete the hacked up program
Chris Lattner640f22e2003-04-24 17:02:17 +0000274 BD.Program = OldProgram; // Restore the original
275
Chris Lattnerde9750d2003-12-07 02:43:09 +0000276 std::cout << (Broken ? " nope.\n" : " yup.\n");
Chris Lattner640f22e2003-04-24 17:02:17 +0000277 return Broken;
278}
279
280
Chris Lattner4a106452002-12-23 23:50:16 +0000281/// debugMiscompilation - This method is used when the passes selected are not
282/// crashing, but the generated output is semantically different from the
283/// input.
284///
285bool BugDriver::debugMiscompilation() {
Chris Lattner4a106452002-12-23 23:50:16 +0000286 // Make sure something was miscompiled...
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000287 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
Chris Lattner4a106452002-12-23 23:50:16 +0000288 std::cerr << "*** Optimized program matches reference output! No problem "
289 << "detected...\nbugpoint can't help you with your problem!\n";
290 return false;
291 }
292
Chris Lattner640f22e2003-04-24 17:02:17 +0000293 std::cout << "\n*** Found miscompiling pass"
294 << (PassesToRun.size() == 1 ? "" : "es") << ": "
295 << getPassesString(PassesToRun) << "\n";
296 EmitProgressBytecode("passinput");
Chris Lattner4a106452002-12-23 23:50:16 +0000297
Chris Lattner640f22e2003-04-24 17:02:17 +0000298 // Okay, now that we have reduced the list of passes which are causing the
299 // failure, see if we can pin down which functions are being
300 // miscompiled... first build a list of all of the non-external functions in
301 // the program.
302 std::vector<Function*> MiscompiledFunctions;
303 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
304 if (!I->isExternal())
305 MiscompiledFunctions.push_back(I);
306
307 // Do the reduction...
308 ReduceMiscompilingFunctions(*this).reduceList(MiscompiledFunctions);
309
Chris Lattnerde9750d2003-12-07 02:43:09 +0000310 std::cout << "\n*** The following function"
311 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
312 << " being miscompiled: ";
Chris Lattner640f22e2003-04-24 17:02:17 +0000313 PrintFunctionList(MiscompiledFunctions);
314 std::cout << "\n";
315
316 // Output a bunch of bytecode files for the user...
317 ReduceMiscompilingFunctions(*this).TestFuncs(MiscompiledFunctions, true);
Chris Lattner4a106452002-12-23 23:50:16 +0000318
Chris Lattner4a106452002-12-23 23:50:16 +0000319 return false;
320}
Brian Gaeked0fde302003-11-11 22:41:34 +0000321