blob: 95fd7d6538d205a934378a6c2d6ba646e7708485 [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- Miscompilation.cpp - Debug program miscompilations -----------------===//
2//
3// This file implements program miscompilation debugging support.
4//
5//===----------------------------------------------------------------------===//
6
7#include "BugDriver.h"
Chris Lattner126840f2003-04-24 20:16:29 +00008#include "ListReducer.h"
Chris Lattner4a106452002-12-23 23:50:16 +00009#include "llvm/Module.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000010#include "llvm/Pass.h"
Chris Lattner640f22e2003-04-24 17:02:17 +000011#include "llvm/Transforms/Utils/Cloning.h"
12#include "llvm/Transforms/Utils/Linker.h"
Misha Brukman3d9cafa2003-08-07 21:42:28 +000013#include "Support/FileUtilities.h"
Chris Lattner4a106452002-12-23 23:50:16 +000014
Chris Lattner640f22e2003-04-24 17:02:17 +000015class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
16 BugDriver &BD;
17public:
18 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
19
Chris Lattner39aebca2003-04-24 22:24:22 +000020 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
Misha Brukman50733362003-07-24 18:17:43 +000021 std::vector<const PassInfo*> &Suffix);
Chris Lattner640f22e2003-04-24 17:02:17 +000022};
23
24ReduceMiscompilingPasses::TestResult
Chris Lattner39aebca2003-04-24 22:24:22 +000025ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
Chris Lattner06943ad2003-04-25 03:16:05 +000026 std::vector<const PassInfo*> &Suffix) {
27 // First, run the program with just the Suffix passes. If it is still broken
Chris Lattner640f22e2003-04-24 17:02:17 +000028 // with JUST the kept passes, discard the prefix passes.
Chris Lattner06943ad2003-04-25 03:16:05 +000029 std::cout << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +000030 << "' compile correctly: ";
31
32 std::string BytecodeResult;
Chris Lattner06943ad2003-04-25 03:16:05 +000033 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner5ef681c2003-10-17 23:07:47 +000034 std::cerr << ": Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000035 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +000036 BD.setPassesToRun(Suffix);
37 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner640f22e2003-04-24 17:02:17 +000038 exit(1);
39 }
40
41 // Check to see if the finished program matches the reference output...
Misha Brukman50733362003-07-24 18:17:43 +000042 if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) {
Chris Lattner640f22e2003-04-24 17:02:17 +000043 std::cout << "nope.\n";
44 return KeepSuffix; // Miscompilation detected!
45 }
46 std::cout << "yup.\n"; // No miscompilation!
47
48 if (Prefix.empty()) return NoFailure;
49
Chris Lattner06943ad2003-04-25 03:16:05 +000050 // Next, see if the program is broken if we run the "prefix" passes first,
Misha Brukmanbc0e9982003-07-14 17:20:40 +000051 // then separately run the "kept" passes.
Chris Lattner640f22e2003-04-24 17:02:17 +000052 std::cout << "Checking to see if '" << getPassesString(Prefix)
53 << "' compile correctly: ";
54
55 // If it is not broken with the kept passes, it's possible that the prefix
56 // passes must be run before the kept passes to break it. If the program
57 // WORKS after the prefix passes, but then fails if running the prefix AND
58 // kept passes, we can update our bytecode file to include the result of the
59 // prefix passes, then discard the prefix passes.
60 //
61 if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9c6cfe12003-10-17 23:03:16 +000062 std::cerr << ": Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000063 << " on the input program!\n";
Chris Lattner9c6cfe12003-10-17 23:03:16 +000064 BD.setPassesToRun(Prefix);
65 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner640f22e2003-04-24 17:02:17 +000066 exit(1);
67 }
68
69 // If the prefix maintains the predicate by itself, only keep the prefix!
Misha Brukman50733362003-07-24 18:17:43 +000070 if (BD.diffProgram(BytecodeResult)) {
Chris Lattner640f22e2003-04-24 17:02:17 +000071 std::cout << "nope.\n";
72 removeFile(BytecodeResult);
73 return KeepPrefix;
74 }
75 std::cout << "yup.\n"; // No miscompilation!
76
77 // Ok, so now we know that the prefix passes work, try running the suffix
78 // passes on the result of the prefix passes.
79 //
80 Module *PrefixOutput = BD.ParseInputFile(BytecodeResult);
81 if (PrefixOutput == 0) {
82 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
83 << BytecodeResult << "'!\n";
84 exit(1);
85 }
86 removeFile(BytecodeResult); // No longer need the file on disk
87
Chris Lattner06943ad2003-04-25 03:16:05 +000088 std::cout << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +000089 << "' passes compile correctly after the '"
90 << getPassesString(Prefix) << "' passes: ";
91
92 Module *OriginalInput = BD.Program;
93 BD.Program = PrefixOutput;
Chris Lattner06943ad2003-04-25 03:16:05 +000094 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9c6cfe12003-10-17 23:03:16 +000095 std::cerr << ": Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000096 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +000097 BD.setPassesToRun(Suffix);
Chris Lattner9c6cfe12003-10-17 23:03:16 +000098 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner640f22e2003-04-24 17:02:17 +000099 exit(1);
100 }
101
102 // Run the result...
Misha Brukman50733362003-07-24 18:17:43 +0000103 if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) {
Chris Lattner640f22e2003-04-24 17:02:17 +0000104 std::cout << "nope.\n";
105 delete OriginalInput; // We pruned down the original input...
Chris Lattner06943ad2003-04-25 03:16:05 +0000106 return KeepSuffix;
Chris Lattner640f22e2003-04-24 17:02:17 +0000107 }
108
109 // Otherwise, we must not be running the bad pass anymore.
110 std::cout << "yup.\n"; // No miscompilation!
111 BD.Program = OriginalInput; // Restore original program
112 delete PrefixOutput; // Free experiment
113 return NoFailure;
114}
115
Chris Lattner640f22e2003-04-24 17:02:17 +0000116class ReduceMiscompilingFunctions : public ListReducer<Function*> {
117 BugDriver &BD;
118public:
119 ReduceMiscompilingFunctions(BugDriver &bd) : BD(bd) {}
120
Chris Lattner39aebca2003-04-24 22:24:22 +0000121 virtual TestResult doTest(std::vector<Function*> &Prefix,
Chris Lattner06943ad2003-04-25 03:16:05 +0000122 std::vector<Function*> &Suffix) {
Misha Brukman5d3f1f02003-08-04 19:03:42 +0000123 if (!Suffix.empty() && TestFuncs(Suffix, false))
Chris Lattner640f22e2003-04-24 17:02:17 +0000124 return KeepSuffix;
Chris Lattnera148ccb2003-04-24 19:32:42 +0000125 if (!Prefix.empty() && TestFuncs(Prefix, false))
Chris Lattner640f22e2003-04-24 17:02:17 +0000126 return KeepPrefix;
127 return NoFailure;
128 }
129
130 bool TestFuncs(const std::vector<Function*> &Prefix, bool EmitBytecode);
131};
132
Chris Lattner640f22e2003-04-24 17:02:17 +0000133bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
134 bool EmitBytecode) {
135 // Test to see if the function is misoptimized if we ONLY run it on the
136 // functions listed in Funcs.
137 if (!EmitBytecode) {
138 std::cout << "Checking to see if the program is misoptimized when these "
139 << "functions are run\nthrough the passes: ";
Misha Brukman50733362003-07-24 18:17:43 +0000140 BD.PrintFunctionList(Funcs);
Chris Lattner640f22e2003-04-24 17:02:17 +0000141 std::cout << "\n";
142 } else {
143 std::cout <<"Outputting reduced bytecode files which expose the problem:\n";
144 }
145
146 // First step: clone the module for the two halves of the program we want.
147 Module *ToOptimize = CloneModule(BD.Program);
148
149 // Second step: Make sure functions & globals are all external so that linkage
150 // between the two modules will work.
151 for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
152 I->setLinkage(GlobalValue::ExternalLinkage);
153 for (Module::giterator I = ToOptimize->gbegin(), E = ToOptimize->gend();
154 I != E; ++I)
155 I->setLinkage(GlobalValue::ExternalLinkage);
156
157 // Third step: make a clone of the externalized program for the non-optimized
158 // part.
159 Module *ToNotOptimize = CloneModule(ToOptimize);
160
161 // Fourth step: Remove the test functions from the ToNotOptimize module, and
162 // all of the global variables.
163 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
164 Function *TNOF = ToNotOptimize->getFunction(Funcs[i]->getName(),
165 Funcs[i]->getFunctionType());
166 assert(TNOF && "Function doesn't exist in module!");
167 DeleteFunctionBody(TNOF); // Function is now external in this module!
168 }
169 for (Module::giterator I = ToNotOptimize->gbegin(), E = ToNotOptimize->gend();
170 I != E; ++I)
171 I->setInitializer(0); // Delete the initializer to make it external
172
173 if (EmitBytecode) {
174 std::cout << " Non-optimized portion: ";
175 std::swap(BD.Program, ToNotOptimize);
176 BD.EmitProgressBytecode("tonotoptimize", true);
177 std::swap(BD.Program, ToNotOptimize);
178 }
179
180 // Fifth step: Remove all functions from the ToOptimize module EXCEPT for the
181 // ones specified in Funcs. We know which ones these are because they are
182 // non-external in ToOptimize, but external in ToNotOptimize.
183 //
184 for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I)
185 if (!I->isExternal()) {
186 Function *TNOF = ToNotOptimize->getFunction(I->getName(),
187 I->getFunctionType());
188 assert(TNOF && "Function doesn't exist in ToNotOptimize module??");
189 if (!TNOF->isExternal())
190 DeleteFunctionBody(I);
191 }
192
193 if (EmitBytecode) {
194 std::cout << " Portion that is input to optimizer: ";
195 std::swap(BD.Program, ToOptimize);
196 BD.EmitProgressBytecode("tooptimize");
197 std::swap(BD.Program, ToOptimize);
198 }
199
200 // Sixth step: Run the optimization passes on ToOptimize, producing a
201 // transformed version of the functions being tested.
202 Module *OldProgram = BD.Program;
203 BD.Program = ToOptimize;
204
205 if (!EmitBytecode)
206 std::cout << " Optimizing functions being tested: ";
207 std::string BytecodeResult;
208 if (BD.runPasses(BD.PassesToRun, BytecodeResult, false/*delete*/,
209 true/*quiet*/)) {
Chris Lattner5ef681c2003-10-17 23:07:47 +0000210 std::cerr << ": Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +0000211 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000212 BD.setPassesToRun(Suffix);
213 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner640f22e2003-04-24 17:02:17 +0000214 exit(1);
215 }
216
217 if (!EmitBytecode)
218 std::cout << "done.\n";
219
220 delete BD.Program; // Delete the old "ToOptimize" module
221 BD.Program = BD.ParseInputFile(BytecodeResult);
222
223 if (EmitBytecode) {
224 std::cout << " 'tooptimize' after being optimized: ";
225 BD.EmitProgressBytecode("optimized", true);
226 }
227
228 if (BD.Program == 0) {
229 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
230 << BytecodeResult << "'!\n";
231 exit(1);
232 }
233 removeFile(BytecodeResult); // No longer need the file on disk
234
235 // Seventh step: Link the optimized part of the program back to the
236 // unoptimized part of the program.
237 //
238 if (LinkModules(BD.Program, ToNotOptimize, &BytecodeResult)) {
239 std::cerr << BD.getToolName() << ": Error linking modules together:"
240 << BytecodeResult << "\n";
241 exit(1);
242 }
243 delete ToNotOptimize; // We are done with this module...
244
245 if (EmitBytecode) {
246 std::cout << " Program as tested: ";
247 BD.EmitProgressBytecode("linked", true);
248 delete BD.Program;
249 BD.Program = OldProgram;
250 return false; // We don't need to actually execute the program here.
251 }
252
253 std::cout << " Checking to see if the merged program executes correctly: ";
254
255 // Eighth step: Execute the program. If it does not match the expected
256 // output, then 'Funcs' are being misoptimized!
Misha Brukman50733362003-07-24 18:17:43 +0000257 bool Broken = BD.diffProgram();
Chris Lattner640f22e2003-04-24 17:02:17 +0000258
259 delete BD.Program; // Delete the hacked up program
260 BD.Program = OldProgram; // Restore the original
261
262 std::cout << (Broken ? "nope.\n" : "yup.\n");
263 return Broken;
264}
265
266
Chris Lattner4a106452002-12-23 23:50:16 +0000267/// debugMiscompilation - This method is used when the passes selected are not
268/// crashing, but the generated output is semantically different from the
269/// input.
270///
271bool BugDriver::debugMiscompilation() {
Chris Lattner4a106452002-12-23 23:50:16 +0000272 // Make sure something was miscompiled...
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000273 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
Chris Lattner4a106452002-12-23 23:50:16 +0000274 std::cerr << "*** Optimized program matches reference output! No problem "
275 << "detected...\nbugpoint can't help you with your problem!\n";
276 return false;
277 }
278
Chris Lattner640f22e2003-04-24 17:02:17 +0000279 std::cout << "\n*** Found miscompiling pass"
280 << (PassesToRun.size() == 1 ? "" : "es") << ": "
281 << getPassesString(PassesToRun) << "\n";
282 EmitProgressBytecode("passinput");
Chris Lattner4a106452002-12-23 23:50:16 +0000283
Chris Lattner640f22e2003-04-24 17:02:17 +0000284 // Okay, now that we have reduced the list of passes which are causing the
285 // failure, see if we can pin down which functions are being
286 // miscompiled... first build a list of all of the non-external functions in
287 // the program.
288 std::vector<Function*> MiscompiledFunctions;
289 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
290 if (!I->isExternal())
291 MiscompiledFunctions.push_back(I);
292
293 // Do the reduction...
294 ReduceMiscompilingFunctions(*this).reduceList(MiscompiledFunctions);
295
296 std::cout << "\n*** The following functions are being miscompiled: ";
297 PrintFunctionList(MiscompiledFunctions);
298 std::cout << "\n";
299
300 // Output a bunch of bytecode files for the user...
301 ReduceMiscompilingFunctions(*this).TestFuncs(MiscompiledFunctions, true);
Chris Lattner4a106452002-12-23 23:50:16 +0000302
Chris Lattner4a106452002-12-23 23:50:16 +0000303 return false;
304}