blob: 830f26f51466a454fb50a180494a9431c12061d2 [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
Chris Lattnerefdc0b52004-03-14 20:50:42 +000023namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +000024 class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
25 BugDriver &BD;
26 public:
27 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
28
29 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
30 std::vector<const PassInfo*> &Suffix);
31 };
32}
Chris Lattner640f22e2003-04-24 17:02:17 +000033
34ReduceMiscompilingPasses::TestResult
Chris Lattner39aebca2003-04-24 22:24:22 +000035ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
Chris Lattner06943ad2003-04-25 03:16:05 +000036 std::vector<const PassInfo*> &Suffix) {
37 // First, run the program with just the Suffix passes. If it is still broken
Chris Lattner640f22e2003-04-24 17:02:17 +000038 // with JUST the kept passes, discard the prefix passes.
Chris Lattner06943ad2003-04-25 03:16:05 +000039 std::cout << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +000040 << "' compile correctly: ";
41
42 std::string BytecodeResult;
Chris Lattner06943ad2003-04-25 03:16:05 +000043 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +000044 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000045 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +000046 BD.setPassesToRun(Suffix);
47 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +000048 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000049 }
50
51 // Check to see if the finished program matches the reference output...
Misha Brukman50733362003-07-24 18:17:43 +000052 if (BD.diffProgram(BytecodeResult, "", true /*delete bytecode*/)) {
Chris Lattner640f22e2003-04-24 17:02:17 +000053 std::cout << "nope.\n";
54 return KeepSuffix; // Miscompilation detected!
55 }
56 std::cout << "yup.\n"; // No miscompilation!
57
58 if (Prefix.empty()) return NoFailure;
59
Chris Lattner06943ad2003-04-25 03:16:05 +000060 // Next, see if the program is broken if we run the "prefix" passes first,
Misha Brukmanbc0e9982003-07-14 17:20:40 +000061 // then separately run the "kept" passes.
Chris Lattner640f22e2003-04-24 17:02:17 +000062 std::cout << "Checking to see if '" << getPassesString(Prefix)
63 << "' compile correctly: ";
64
65 // If it is not broken with the kept passes, it's possible that the prefix
66 // passes must be run before the kept passes to break it. If the program
67 // WORKS after the prefix passes, but then fails if running the prefix AND
68 // kept passes, we can update our bytecode file to include the result of the
69 // prefix passes, then discard the prefix passes.
70 //
71 if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +000072 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +000073 << " on the input program!\n";
Chris Lattner9c6cfe12003-10-17 23:03:16 +000074 BD.setPassesToRun(Prefix);
75 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +000076 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000077 }
78
79 // If the prefix maintains the predicate by itself, only keep the prefix!
Misha Brukman50733362003-07-24 18:17:43 +000080 if (BD.diffProgram(BytecodeResult)) {
Chris Lattner640f22e2003-04-24 17:02:17 +000081 std::cout << "nope.\n";
82 removeFile(BytecodeResult);
83 return KeepPrefix;
84 }
85 std::cout << "yup.\n"; // No miscompilation!
86
87 // Ok, so now we know that the prefix passes work, try running the suffix
88 // passes on the result of the prefix passes.
89 //
Chris Lattnerefdc0b52004-03-14 20:50:42 +000090 Module *PrefixOutput = ParseInputFile(BytecodeResult);
Chris Lattner640f22e2003-04-24 17:02:17 +000091 if (PrefixOutput == 0) {
92 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
93 << BytecodeResult << "'!\n";
94 exit(1);
95 }
96 removeFile(BytecodeResult); // No longer need the file on disk
97
Chris Lattner06943ad2003-04-25 03:16:05 +000098 std::cout << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +000099 << "' passes compile correctly after the '"
100 << getPassesString(Prefix) << "' passes: ";
101
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000102 Module *OriginalInput = BD.swapProgramIn(PrefixOutput);
Chris Lattner06943ad2003-04-25 03:16:05 +0000103 if (BD.runPasses(Suffix, BytecodeResult, false/*delete*/, true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +0000104 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +0000105 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000106 BD.setPassesToRun(Suffix);
Chris Lattner9c6cfe12003-10-17 23:03:16 +0000107 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000108 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000109 }
110
111 // Run the result...
Misha Brukman50733362003-07-24 18:17:43 +0000112 if (BD.diffProgram(BytecodeResult, "", true/*delete bytecode*/)) {
Chris Lattner640f22e2003-04-24 17:02:17 +0000113 std::cout << "nope.\n";
114 delete OriginalInput; // We pruned down the original input...
Chris Lattner06943ad2003-04-25 03:16:05 +0000115 return KeepSuffix;
Chris Lattner640f22e2003-04-24 17:02:17 +0000116 }
117
118 // Otherwise, we must not be running the bad pass anymore.
119 std::cout << "yup.\n"; // No miscompilation!
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000120 delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test
Chris Lattner640f22e2003-04-24 17:02:17 +0000121 return NoFailure;
122}
123
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000124namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +0000125 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
126 BugDriver &BD;
127 public:
128 ReduceMiscompilingFunctions(BugDriver &bd) : BD(bd) {}
129
130 virtual TestResult doTest(std::vector<Function*> &Prefix,
131 std::vector<Function*> &Suffix) {
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000132 if (!Suffix.empty() && TestFuncs(Suffix))
Chris Lattnerfa761832004-01-14 03:38:37 +0000133 return KeepSuffix;
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000134 if (!Prefix.empty() && TestFuncs(Prefix))
Chris Lattnerfa761832004-01-14 03:38:37 +0000135 return KeepPrefix;
136 return NoFailure;
137 }
138
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000139 bool TestFuncs(const std::vector<Function*> &Prefix);
Chris Lattnerfa761832004-01-14 03:38:37 +0000140 };
141}
Chris Lattner640f22e2003-04-24 17:02:17 +0000142
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000143/// TestMergedProgram - Given two modules, link them together and run the
144/// program, checking to see if the program matches the diff. If the diff
145/// matches, return false, otherwise return true. In either case, we delete
146/// both input modules before we return.
147static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2) {
148 // Link the two portions of the program back to together.
149 std::string ErrorMsg;
150 if (LinkModules(M1, M2, &ErrorMsg)) {
151 std::cerr << BD.getToolName() << ": Error linking modules together:"
152 << ErrorMsg << "\n";
153 exit(1);
154 }
155 delete M2; // We are done with this module...
156
157 Module *OldProgram = BD.swapProgramIn(M1);
158
159 // Execute the program. If it does not match the expected output, we must
160 // return true.
161 bool Broken = BD.diffProgram();
162
163 // Delete the linked module & restore the original
164 delete BD.swapProgramIn(OldProgram);
165 return Broken;
166}
167
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000168bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
Chris Lattner640f22e2003-04-24 17:02:17 +0000169 // Test to see if the function is misoptimized if we ONLY run it on the
170 // functions listed in Funcs.
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000171 std::cout << "Checking to see if the program is misoptimized when "
172 << (Funcs.size()==1 ? "this function is" : "these functions are")
173 << " run through the pass"
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000174 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ": ";
175 PrintFunctionList(Funcs);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000176 std::cout << "\n";
Chris Lattner640f22e2003-04-24 17:02:17 +0000177
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000178 // Split the module into the two halves of the program we want.
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000179 Module *ToNotOptimize = CloneModule(BD.getProgram());
180 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs);
Chris Lattner640f22e2003-04-24 17:02:17 +0000181
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000182 // Run the optimization passes on ToOptimize, producing a transformed version
183 // of the functions being tested.
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000184 Module *OldProgram = BD.swapProgramIn(ToOptimize);
Chris Lattner640f22e2003-04-24 17:02:17 +0000185
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000186 std::cout << " Optimizing functions being tested: ";
Chris Lattner640f22e2003-04-24 17:02:17 +0000187 std::string BytecodeResult;
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000188 if (BD.runPasses(BD.getPassesToRun(), BytecodeResult, false/*delete*/,
Chris Lattner640f22e2003-04-24 17:02:17 +0000189 true/*quiet*/)) {
Chris Lattner9f71e792003-10-18 00:05:05 +0000190 std::cerr << " Error running this sequence of passes"
Chris Lattner640f22e2003-04-24 17:02:17 +0000191 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000192 BD.EmitProgressBytecode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000193 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000194 }
195
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000196 std::cout << "done.\n";
Chris Lattner640f22e2003-04-24 17:02:17 +0000197
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000198 // Delete the old "ToOptimize" module
199 delete BD.swapProgramIn(OldProgram);
200 Module *Optimized = ParseInputFile(BytecodeResult);
201 if (Optimized == 0) {
Chris Lattner640f22e2003-04-24 17:02:17 +0000202 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
203 << BytecodeResult << "'!\n";
204 exit(1);
205 }
206 removeFile(BytecodeResult); // No longer need the file on disk
207
Chris Lattner640f22e2003-04-24 17:02:17 +0000208 std::cout << " Checking to see if the merged program executes correctly: ";
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000209 bool Broken = TestMergedProgram(BD, Optimized, ToNotOptimize);
Chris Lattnerde9750d2003-12-07 02:43:09 +0000210 std::cout << (Broken ? " nope.\n" : " yup.\n");
Chris Lattner640f22e2003-04-24 17:02:17 +0000211 return Broken;
212}
213
Chris Lattner4a106452002-12-23 23:50:16 +0000214/// debugMiscompilation - This method is used when the passes selected are not
215/// crashing, but the generated output is semantically different from the
216/// input.
217///
218bool BugDriver::debugMiscompilation() {
Chris Lattner4a106452002-12-23 23:50:16 +0000219 // Make sure something was miscompiled...
Misha Brukmanbe6bf562003-07-30 20:15:56 +0000220 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) {
Chris Lattner4a106452002-12-23 23:50:16 +0000221 std::cerr << "*** Optimized program matches reference output! No problem "
222 << "detected...\nbugpoint can't help you with your problem!\n";
223 return false;
224 }
225
Chris Lattner640f22e2003-04-24 17:02:17 +0000226 std::cout << "\n*** Found miscompiling pass"
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000227 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
228 << getPassesString(getPassesToRun()) << "\n";
Chris Lattner640f22e2003-04-24 17:02:17 +0000229 EmitProgressBytecode("passinput");
Chris Lattner4a106452002-12-23 23:50:16 +0000230
Chris Lattner640f22e2003-04-24 17:02:17 +0000231 // Okay, now that we have reduced the list of passes which are causing the
232 // failure, see if we can pin down which functions are being
233 // miscompiled... first build a list of all of the non-external functions in
234 // the program.
235 std::vector<Function*> MiscompiledFunctions;
236 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
237 if (!I->isExternal())
238 MiscompiledFunctions.push_back(I);
239
240 // Do the reduction...
241 ReduceMiscompilingFunctions(*this).reduceList(MiscompiledFunctions);
242
Chris Lattnerde9750d2003-12-07 02:43:09 +0000243 std::cout << "\n*** The following function"
244 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
245 << " being miscompiled: ";
Chris Lattner640f22e2003-04-24 17:02:17 +0000246 PrintFunctionList(MiscompiledFunctions);
247 std::cout << "\n";
248
249 // Output a bunch of bytecode files for the user...
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000250 std::cout << "Outputting reduced bytecode files which expose the problem:\n";
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000251 Module *ToNotOptimize = CloneModule(getProgram());
252 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
253 MiscompiledFunctions);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000254
255 std::cout << " Non-optimized portion: ";
256 std::swap(Program, ToNotOptimize);
257 EmitProgressBytecode("tonotoptimize", true);
258 setNewProgram(ToNotOptimize); // Delete hacked module.
259
260 std::cout << " Portion that is input to optimizer: ";
261 std::swap(Program, ToOptimize);
262 EmitProgressBytecode("tooptimize");
263 setNewProgram(ToOptimize); // Delete hacked module.
Chris Lattner4a106452002-12-23 23:50:16 +0000264
Chris Lattner4a106452002-12-23 23:50:16 +0000265 return false;
266}
Brian Gaeked0fde302003-11-11 22:41:34 +0000267