blob: 6af1f6a57eeb3dc6c1d37ec600a00866f06752fe [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- Miscompilation.cpp - Debug program miscompilations -----------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a106452002-12-23 23:50:16 +00009//
Chris Lattnera57d86b2004-04-05 22:58:16 +000010// This file implements optimizer and code generation miscompilation debugging
11// support.
Chris Lattner4a106452002-12-23 23:50:16 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "BugDriver.h"
Chris Lattner126840f2003-04-24 20:16:29 +000016#include "ListReducer.h"
Daniel Dunbarca740962009-08-18 03:35:57 +000017#include "ToolRunner.h"
Chris Lattnera57d86b2004-04-05 22:58:16 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Instructions.h"
Reid Spencer605b9e22004-11-14 23:00:08 +000021#include "llvm/Linker.h"
Chris Lattner4a106452002-12-23 23:50:16 +000022#include "llvm/Module.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000023#include "llvm/Pass.h"
Chris Lattnera57d86b2004-04-05 22:58:16 +000024#include "llvm/Analysis/Verifier.h"
Chris Lattner640f22e2003-04-24 17:02:17 +000025#include "llvm/Transforms/Utils/Cloning.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/FileUtilities.h"
Chris Lattner59615f02005-01-15 00:07:19 +000028#include "llvm/Config/config.h" // for HAVE_LINK_R
Chris Lattnerfa761832004-01-14 03:38:37 +000029using namespace llvm;
Chris Lattner4a106452002-12-23 23:50:16 +000030
Chris Lattnera57d86b2004-04-05 22:58:16 +000031namespace llvm {
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +000032 extern cl::opt<std::string> OutputPrefix;
Chris Lattnera57d86b2004-04-05 22:58:16 +000033 extern cl::list<std::string> InputArgv;
34}
35
Chris Lattnerefdc0b52004-03-14 20:50:42 +000036namespace {
Reid Spencerdc31a8a2006-11-11 19:05:02 +000037 static llvm::cl::opt<bool>
38 DisableLoopExtraction("disable-loop-extraction",
39 cl::desc("Don't extract loops when searching for miscompilations"),
40 cl::init(false));
David Goodwin265d82e2009-07-28 23:08:36 +000041 static llvm::cl::opt<bool>
42 DisableBlockExtraction("disable-block-extraction",
43 cl::desc("Don't extract blocks when searching for miscompilations"),
44 cl::init(false));
Reid Spencerdc31a8a2006-11-11 19:05:02 +000045
Chris Lattnerfa761832004-01-14 03:38:37 +000046 class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> {
47 BugDriver &BD;
48 public:
49 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000050
Chris Lattnerfa761832004-01-14 03:38:37 +000051 virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +000052 std::vector<const PassInfo*> &Suffix,
53 std::string &Error);
Chris Lattnerfa761832004-01-14 03:38:37 +000054 };
55}
Chris Lattner640f22e2003-04-24 17:02:17 +000056
Misha Brukman8c194ea2004-04-21 18:36:43 +000057/// TestResult - After passes have been split into a test group and a control
58/// group, see if they still break the program.
59///
Chris Lattner640f22e2003-04-24 17:02:17 +000060ReduceMiscompilingPasses::TestResult
Chris Lattner39aebca2003-04-24 22:24:22 +000061ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +000062 std::vector<const PassInfo*> &Suffix,
63 std::string &Error) {
Chris Lattner06943ad2003-04-25 03:16:05 +000064 // First, run the program with just the Suffix passes. If it is still broken
Chris Lattner640f22e2003-04-24 17:02:17 +000065 // with JUST the kept passes, discard the prefix passes.
Dan Gohmanac95cc72009-07-16 15:30:09 +000066 outs() << "Checking to see if '" << getPassesString(Suffix)
67 << "' compiles correctly: ";
Chris Lattner640f22e2003-04-24 17:02:17 +000068
Gabor Greif8ff70c22007-07-04 21:55:50 +000069 std::string BitcodeResult;
70 if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) {
Dan Gohman65f57c22009-07-15 16:35:29 +000071 errs() << " Error running this sequence of passes"
72 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +000073 BD.setPassesToRun(Suffix);
Gabor Greif8ff70c22007-07-04 21:55:50 +000074 BD.EmitProgressBitcode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +000075 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +000076 }
Owen Anderson9adc0ab2009-07-14 23:09:55 +000077
Chris Lattner640f22e2003-04-24 17:02:17 +000078 // Check to see if the finished program matches the reference output...
Nick Lewycky22ff7482010-04-12 05:08:25 +000079 bool Diff = BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/,
80 &Error);
81 if (!Error.empty())
82 return InternalError;
83 if (Diff) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000084 outs() << " nope.\n";
Chris Lattner59615f02005-01-15 00:07:19 +000085 if (Suffix.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +000086 errs() << BD.getToolName() << ": I'm confused: the test fails when "
87 << "no passes are run, nondeterministic program?\n";
Chris Lattner59615f02005-01-15 00:07:19 +000088 exit(1);
89 }
Misha Brukman123f8fe2004-04-22 20:02:09 +000090 return KeepSuffix; // Miscompilation detected!
Chris Lattner640f22e2003-04-24 17:02:17 +000091 }
Dan Gohmanac95cc72009-07-16 15:30:09 +000092 outs() << " yup.\n"; // No miscompilation!
Chris Lattner640f22e2003-04-24 17:02:17 +000093
94 if (Prefix.empty()) return NoFailure;
95
Chris Lattner06943ad2003-04-25 03:16:05 +000096 // Next, see if the program is broken if we run the "prefix" passes first,
Misha Brukmanbc0e9982003-07-14 17:20:40 +000097 // then separately run the "kept" passes.
Dan Gohmanac95cc72009-07-16 15:30:09 +000098 outs() << "Checking to see if '" << getPassesString(Prefix)
99 << "' compiles correctly: ";
Chris Lattner640f22e2003-04-24 17:02:17 +0000100
101 // If it is not broken with the kept passes, it's possible that the prefix
102 // passes must be run before the kept passes to break it. If the program
103 // WORKS after the prefix passes, but then fails if running the prefix AND
Gabor Greif8ff70c22007-07-04 21:55:50 +0000104 // kept passes, we can update our bitcode file to include the result of the
Chris Lattner640f22e2003-04-24 17:02:17 +0000105 // prefix passes, then discard the prefix passes.
106 //
Gabor Greif8ff70c22007-07-04 21:55:50 +0000107 if (BD.runPasses(Prefix, BitcodeResult, false/*delete*/, true/*quiet*/)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000108 errs() << " Error running this sequence of passes"
109 << " on the input program!\n";
Chris Lattner9c6cfe12003-10-17 23:03:16 +0000110 BD.setPassesToRun(Prefix);
Gabor Greif8ff70c22007-07-04 21:55:50 +0000111 BD.EmitProgressBitcode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000112 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000113 }
114
115 // If the prefix maintains the predicate by itself, only keep the prefix!
Nick Lewycky22ff7482010-04-12 05:08:25 +0000116 Diff = BD.diffProgram(BitcodeResult, "", false, &Error);
117 if (!Error.empty())
118 return InternalError;
119 if (Diff) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000120 outs() << " nope.\n";
Gabor Greif8ff70c22007-07-04 21:55:50 +0000121 sys::Path(BitcodeResult).eraseFromDisk();
Chris Lattner640f22e2003-04-24 17:02:17 +0000122 return KeepPrefix;
123 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000124 outs() << " yup.\n"; // No miscompilation!
Chris Lattner640f22e2003-04-24 17:02:17 +0000125
126 // Ok, so now we know that the prefix passes work, try running the suffix
127 // passes on the result of the prefix passes.
128 //
Owen Anderson8b477ed2009-07-01 16:58:40 +0000129 Module *PrefixOutput = ParseInputFile(BitcodeResult, BD.getContext());
Chris Lattner640f22e2003-04-24 17:02:17 +0000130 if (PrefixOutput == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000131 errs() << BD.getToolName() << ": Error reading bitcode file '"
132 << BitcodeResult << "'!\n";
Chris Lattner640f22e2003-04-24 17:02:17 +0000133 exit(1);
134 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000135 sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk
Chris Lattnerf4789e62004-04-23 20:36:51 +0000136
137 // Don't check if there are no passes in the suffix.
138 if (Suffix.empty())
139 return NoFailure;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000140
Dan Gohmanac95cc72009-07-16 15:30:09 +0000141 outs() << "Checking to see if '" << getPassesString(Suffix)
Chris Lattner640f22e2003-04-24 17:02:17 +0000142 << "' passes compile correctly after the '"
143 << getPassesString(Prefix) << "' passes: ";
144
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000145 Module *OriginalInput = BD.swapProgramIn(PrefixOutput);
Gabor Greif8ff70c22007-07-04 21:55:50 +0000146 if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000147 errs() << " Error running this sequence of passes"
148 << " on the input program!\n";
Chris Lattner5ef681c2003-10-17 23:07:47 +0000149 BD.setPassesToRun(Suffix);
Gabor Greif8ff70c22007-07-04 21:55:50 +0000150 BD.EmitProgressBitcode("pass-error", false);
Chris Lattner02526262004-02-18 21:02:04 +0000151 exit(BD.debugOptimizerCrash());
Chris Lattner640f22e2003-04-24 17:02:17 +0000152 }
153
154 // Run the result...
Nick Lewycky22ff7482010-04-12 05:08:25 +0000155 Diff = BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/, &Error);
156 if (!Error.empty())
157 return InternalError;
158 if (Diff) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000159 outs() << " nope.\n";
Chris Lattner640f22e2003-04-24 17:02:17 +0000160 delete OriginalInput; // We pruned down the original input...
Chris Lattner06943ad2003-04-25 03:16:05 +0000161 return KeepSuffix;
Chris Lattner640f22e2003-04-24 17:02:17 +0000162 }
163
164 // Otherwise, we must not be running the bad pass anymore.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000165 outs() << " yup.\n"; // No miscompilation!
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000166 delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test
Chris Lattner640f22e2003-04-24 17:02:17 +0000167 return NoFailure;
168}
169
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000170namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +0000171 class ReduceMiscompilingFunctions : public ListReducer<Function*> {
172 BugDriver &BD;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000173 bool (*TestFn)(BugDriver &, Module *, Module *, std::string &);
Chris Lattnerfa761832004-01-14 03:38:37 +0000174 public:
Chris Lattnerb15825b2004-04-05 21:37:38 +0000175 ReduceMiscompilingFunctions(BugDriver &bd,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000176 bool (*F)(BugDriver &, Module *, Module *,
177 std::string &))
Chris Lattnerb15825b2004-04-05 21:37:38 +0000178 : BD(bd), TestFn(F) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000179
Chris Lattnerfa761832004-01-14 03:38:37 +0000180 virtual TestResult doTest(std::vector<Function*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000181 std::vector<Function*> &Suffix,
182 std::string &Error) {
183 if (!Suffix.empty()) {
184 bool Ret = TestFuncs(Suffix, Error);
185 if (!Error.empty())
186 return InternalError;
187 if (Ret)
188 return KeepSuffix;
189 }
190 if (!Prefix.empty()) {
191 bool Ret = TestFuncs(Prefix, Error);
192 if (!Error.empty())
193 return InternalError;
194 if (Ret)
195 return KeepPrefix;
196 }
Chris Lattnerfa761832004-01-14 03:38:37 +0000197 return NoFailure;
198 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000199
Nick Lewycky22ff7482010-04-12 05:08:25 +0000200 int TestFuncs(const std::vector<Function*> &Prefix, std::string &Error);
Chris Lattnerfa761832004-01-14 03:38:37 +0000201 };
202}
Chris Lattner640f22e2003-04-24 17:02:17 +0000203
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000204/// TestMergedProgram - Given two modules, link them together and run the
205/// program, checking to see if the program matches the diff. If the diff
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000206/// matches, return false, otherwise return true. If the DeleteInputs argument
207/// is set to true then this function deletes both input modules before it
208/// returns.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000209///
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000210static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000211 bool DeleteInputs, std::string &Error) {
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000212 // Link the two portions of the program back to together.
213 std::string ErrorMsg;
Chris Lattner90c18c52004-11-16 06:31:38 +0000214 if (!DeleteInputs) {
215 M1 = CloneModule(M1);
216 M2 = CloneModule(M2);
217 }
Reid Spencere4874022004-12-13 03:01:03 +0000218 if (Linker::LinkModules(M1, M2, &ErrorMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000219 errs() << BD.getToolName() << ": Error linking modules together:"
220 << ErrorMsg << '\n';
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000221 exit(1);
222 }
Chris Lattner90c18c52004-11-16 06:31:38 +0000223 delete M2; // We are done with this module.
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000224
225 Module *OldProgram = BD.swapProgramIn(M1);
226
227 // Execute the program. If it does not match the expected output, we must
228 // return true.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000229 bool Broken = BD.diffProgram("", "", false, &Error);
230 if (!Error.empty()) {
231 // Delete the linked module & restore the original
232 BD.swapProgramIn(OldProgram);
233 delete M1;
234 }
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000235 return Broken;
236}
237
Misha Brukman8c194ea2004-04-21 18:36:43 +0000238/// TestFuncs - split functions in a Module into two groups: those that are
239/// under consideration for miscompilation vs. those that are not, and test
240/// accordingly. Each group of functions becomes a separate Module.
241///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000242int ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
243 std::string &Error) {
Chris Lattner640f22e2003-04-24 17:02:17 +0000244 // Test to see if the function is misoptimized if we ONLY run it on the
245 // functions listed in Funcs.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000246 outs() << "Checking to see if the program is misoptimized when "
247 << (Funcs.size()==1 ? "this function is" : "these functions are")
248 << " run through the pass"
249 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000250 PrintFunctionList(Funcs);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000251 outs() << '\n';
Chris Lattner640f22e2003-04-24 17:02:17 +0000252
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000253 // Split the module into the two halves of the program we want.
Dan Gohmand50330c2009-04-22 15:57:18 +0000254 DenseMap<const Value*, Value*> ValueMap;
255 Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap);
256 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize, Funcs,
257 ValueMap);
Chris Lattner640f22e2003-04-24 17:02:17 +0000258
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000259 // Run the predicate, note that the predicate will delete both input modules.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000260 return TestFn(BD, ToOptimize, ToNotOptimize, Error);
Chris Lattner640f22e2003-04-24 17:02:17 +0000261}
262
Chris Lattner8abfb8a2010-01-16 21:34:01 +0000263/// DisambiguateGlobalSymbols - Give anonymous global values names.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000264///
Chris Lattner36ee07f2004-04-11 23:52:35 +0000265static void DisambiguateGlobalSymbols(Module *M) {
Chris Lattner67ef9e42006-05-04 23:35:31 +0000266 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
Chris Lattner1ffb33d2010-01-16 21:34:51 +0000267 I != E; ++I)
Chris Lattner8abfb8a2010-01-16 21:34:01 +0000268 if (!I->hasName())
269 I->setName("anon_global");
Chris Lattner1ffb33d2010-01-16 21:34:51 +0000270 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner8abfb8a2010-01-16 21:34:01 +0000271 if (!I->hasName())
272 I->setName("anon_fn");
Chris Lattner36ee07f2004-04-11 23:52:35 +0000273}
274
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000275/// ExtractLoops - Given a reduced list of functions that still exposed the bug,
276/// check to see if we can extract the loops in the region without obscuring the
277/// bug. If so, it reduces the amount of code identified.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000278///
Chris Lattnerb15825b2004-04-05 21:37:38 +0000279static bool ExtractLoops(BugDriver &BD,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000280 bool (*TestFn)(BugDriver &, Module *, Module *,
281 std::string &),
282 std::vector<Function*> &MiscompiledFunctions,
283 std::string &Error) {
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000284 bool MadeChange = false;
285 while (1) {
Chris Lattneraed98fa2005-08-02 23:25:56 +0000286 if (BugpointIsInterrupted) return MadeChange;
287
Dan Gohmand50330c2009-04-22 15:57:18 +0000288 DenseMap<const Value*, Value*> ValueMap;
289 Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap);
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000290 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Dan Gohmand50330c2009-04-22 15:57:18 +0000291 MiscompiledFunctions,
292 ValueMap);
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000293 Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
294 if (!ToOptimizeLoopExtracted) {
295 // If the loop extractor crashed or if there were no extractible loops,
296 // then this chapter of our odyssey is over with.
297 delete ToNotOptimize;
298 delete ToOptimize;
299 return MadeChange;
300 }
301
Dan Gohman65f57c22009-07-15 16:35:29 +0000302 errs() << "Extracted a loop from the breaking portion of the program.\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000303
304 // Bugpoint is intentionally not very trusting of LLVM transformations. In
305 // particular, we're not going to assume that the loop extractor works, so
306 // we're going to test the newly loop extracted program to make sure nothing
307 // has broken. If something broke, then we'll inform the user and stop
308 // extraction.
Dan Gohman70ef4492008-12-08 04:02:47 +0000309 AbstractInterpreter *AI = BD.switchToSafeInterpreter();
Nick Lewycky22ff7482010-04-12 05:08:25 +0000310 bool Failure = TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize,
311 false, Error);
312 if (!Error.empty())
313 return false;
314 if (Failure) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000315 BD.switchToInterpreter(AI);
316
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000317 // Merged program doesn't work anymore!
Dan Gohman65f57c22009-07-15 16:35:29 +0000318 errs() << " *** ERROR: Loop extraction broke the program. :("
319 << " Please report a bug!\n";
320 errs() << " Continuing on with un-loop-extracted version.\n";
Chris Lattner56c41862005-05-08 21:54:56 +0000321
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000322 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc",
323 ToNotOptimize);
324 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc",
325 ToOptimize);
326 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc",
Chris Lattner56c41862005-05-08 21:54:56 +0000327 ToOptimizeLoopExtracted);
328
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000329 errs() << "Please submit the "
330 << OutputPrefix << "-loop-extract-fail-*.bc files.\n";
Chris Lattner56c41862005-05-08 21:54:56 +0000331 delete ToOptimize;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000332 delete ToNotOptimize;
333 delete ToOptimizeLoopExtracted;
334 return MadeChange;
335 }
Chris Lattner56c41862005-05-08 21:54:56 +0000336 delete ToOptimize;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000337 BD.switchToInterpreter(AI);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000338
Dan Gohmanac95cc72009-07-16 15:30:09 +0000339 outs() << " Testing after loop extraction:\n";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000340 // Clone modules, the tester function will free them.
341 Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
342 Module *TNOBackup = CloneModule(ToNotOptimize);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000343 Failure = TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize, Error);
344 if (!Error.empty())
345 return false;
346 if (!Failure) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000347 outs() << "*** Loop extraction masked the problem. Undoing.\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000348 // If the program is not still broken, then loop extraction did something
349 // that masked the error. Stop loop extraction now.
Chris Lattnerb15825b2004-04-05 21:37:38 +0000350 delete TOLEBackup;
351 delete TNOBackup;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000352 return MadeChange;
353 }
Chris Lattnerb15825b2004-04-05 21:37:38 +0000354 ToOptimizeLoopExtracted = TOLEBackup;
355 ToNotOptimize = TNOBackup;
356
Dan Gohmanac95cc72009-07-16 15:30:09 +0000357 outs() << "*** Loop extraction successful!\n";
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000358
Chris Lattner90c18c52004-11-16 06:31:38 +0000359 std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions;
360 for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
361 E = ToOptimizeLoopExtracted->end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000362 if (!I->isDeclaration())
Chris Lattnerfa1af132004-11-19 07:09:40 +0000363 MisCompFunctions.push_back(std::make_pair(I->getName(),
364 I->getFunctionType()));
Chris Lattner90c18c52004-11-16 06:31:38 +0000365
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000366 // Okay, great! Now we know that we extracted a loop and that loop
367 // extraction both didn't break the program, and didn't mask the problem.
368 // Replace the current program with the loop extracted version, and try to
369 // extract another loop.
370 std::string ErrorMsg;
Reid Spencere4874022004-12-13 03:01:03 +0000371 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)){
Dan Gohman65f57c22009-07-15 16:35:29 +0000372 errs() << BD.getToolName() << ": Error linking modules together:"
373 << ErrorMsg << '\n';
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000374 exit(1);
375 }
Chris Lattner90c18c52004-11-16 06:31:38 +0000376 delete ToOptimizeLoopExtracted;
Chris Lattnerd3a533d2004-03-17 17:42:09 +0000377
378 // All of the Function*'s in the MiscompiledFunctions list are in the old
Chris Lattner5313f232004-04-02 06:32:17 +0000379 // module. Update this list to include all of the functions in the
380 // optimized and loop extracted module.
381 MiscompiledFunctions.clear();
Chris Lattner90c18c52004-11-16 06:31:38 +0000382 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000383 Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);
384
Chris Lattner90c18c52004-11-16 06:31:38 +0000385 assert(NewF && "Function not found??");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000386 assert(NewF->getFunctionType() == MisCompFunctions[i].second &&
387 "found wrong function type?");
Chris Lattner90c18c52004-11-16 06:31:38 +0000388 MiscompiledFunctions.push_back(NewF);
Chris Lattnerd3a533d2004-03-17 17:42:09 +0000389 }
390
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000391 BD.setNewProgram(ToNotOptimize);
392 MadeChange = true;
393 }
394}
395
Chris Lattner5e783ab2004-05-11 21:54:13 +0000396namespace {
397 class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
398 BugDriver &BD;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000399 bool (*TestFn)(BugDriver &, Module *, Module *, std::string &);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000400 std::vector<Function*> FunctionsBeingTested;
401 public:
402 ReduceMiscompiledBlocks(BugDriver &bd,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000403 bool (*F)(BugDriver &, Module *, Module *,
404 std::string &),
Chris Lattner5e783ab2004-05-11 21:54:13 +0000405 const std::vector<Function*> &Fns)
406 : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000407
Chris Lattner5e783ab2004-05-11 21:54:13 +0000408 virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000409 std::vector<BasicBlock*> &Suffix,
410 std::string &Error) {
411 if (!Suffix.empty()) {
412 bool Ret = TestFuncs(Suffix, Error);
413 if (!Error.empty())
414 return InternalError;
415 if (Ret)
416 return KeepSuffix;
417 }
418 if (!Prefix.empty()) {
419 bool Ret = TestFuncs(Prefix, Error);
420 if (!Error.empty())
421 return InternalError;
422 if (Ret)
423 return KeepPrefix;
424 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000425 return NoFailure;
426 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000427
Nick Lewycky22ff7482010-04-12 05:08:25 +0000428 bool TestFuncs(const std::vector<BasicBlock*> &BBs, std::string &Error);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000429 };
430}
431
432/// TestFuncs - Extract all blocks for the miscompiled functions except for the
433/// specified blocks. If the problem still exists, return true.
434///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000435bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs,
436 std::string &Error) {
Chris Lattner5e783ab2004-05-11 21:54:13 +0000437 // Test to see if the function is misoptimized if we ONLY run it on the
438 // functions listed in Funcs.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000439 outs() << "Checking to see if the program is misoptimized when all ";
Chris Lattner68bee932004-05-12 16:08:01 +0000440 if (!BBs.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000441 outs() << "but these " << BBs.size() << " blocks are extracted: ";
Chris Lattner68bee932004-05-12 16:08:01 +0000442 for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
Dan Gohmanac95cc72009-07-16 15:30:09 +0000443 outs() << BBs[i]->getName() << " ";
444 if (BBs.size() > 10) outs() << "...";
Chris Lattner68bee932004-05-12 16:08:01 +0000445 } else {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000446 outs() << "blocks are extracted.";
Chris Lattner68bee932004-05-12 16:08:01 +0000447 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000448 outs() << '\n';
Chris Lattner5e783ab2004-05-11 21:54:13 +0000449
450 // Split the module into the two halves of the program we want.
Dan Gohmand50330c2009-04-22 15:57:18 +0000451 DenseMap<const Value*, Value*> ValueMap;
452 Module *ToNotOptimize = CloneModule(BD.getProgram(), ValueMap);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000453 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Dan Gohmand50330c2009-04-22 15:57:18 +0000454 FunctionsBeingTested,
455 ValueMap);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000456
457 // Try the extraction. If it doesn't work, then the block extractor crashed
458 // or something, in which case bugpoint can't chase down this possibility.
459 if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) {
460 delete ToOptimize;
461 // Run the predicate, not that the predicate will delete both input modules.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000462 return TestFn(BD, New, ToNotOptimize, Error);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000463 }
464 delete ToOptimize;
465 delete ToNotOptimize;
466 return false;
467}
468
469
470/// ExtractBlocks - Given a reduced list of functions that still expose the bug,
471/// extract as many basic blocks from the region as possible without obscuring
472/// the bug.
473///
474static bool ExtractBlocks(BugDriver &BD,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000475 bool (*TestFn)(BugDriver &, Module *, Module *,
476 std::string &),
477 std::vector<Function*> &MiscompiledFunctions,
478 std::string &Error) {
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000479 if (BugpointIsInterrupted) return false;
480
Chris Lattner5e783ab2004-05-11 21:54:13 +0000481 std::vector<BasicBlock*> Blocks;
482 for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
483 for (Function::iterator I = MiscompiledFunctions[i]->begin(),
484 E = MiscompiledFunctions[i]->end(); I != E; ++I)
485 Blocks.push_back(I);
486
487 // Use the list reducer to identify blocks that can be extracted without
488 // obscuring the bug. The Blocks list will end up containing blocks that must
489 // be retained from the original program.
490 unsigned OldSize = Blocks.size();
Chris Lattner68bee932004-05-12 16:08:01 +0000491
492 // Check to see if all blocks are extractible first.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000493 bool Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions)
494 .TestFuncs(std::vector<BasicBlock*>(), Error);
495 if (!Error.empty())
496 return false;
497 if (Ret) {
Chris Lattner68bee932004-05-12 16:08:01 +0000498 Blocks.clear();
499 } else {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000500 ReduceMiscompiledBlocks(BD, TestFn,
501 MiscompiledFunctions).reduceList(Blocks, Error);
502 if (!Error.empty())
503 return false;
Chris Lattner68bee932004-05-12 16:08:01 +0000504 if (Blocks.size() == OldSize)
505 return false;
506 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000507
Dan Gohmand50330c2009-04-22 15:57:18 +0000508 DenseMap<const Value*, Value*> ValueMap;
509 Module *ProgClone = CloneModule(BD.getProgram(), ValueMap);
Chris Lattner2290e752004-05-12 02:43:24 +0000510 Module *ToExtract = SplitFunctionsOutOfModule(ProgClone,
Dan Gohmand50330c2009-04-22 15:57:18 +0000511 MiscompiledFunctions,
512 ValueMap);
Chris Lattner2290e752004-05-12 02:43:24 +0000513 Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract);
514 if (Extracted == 0) {
Chris Lattnerda895d62005-02-27 06:18:25 +0000515 // Weird, extraction should have worked.
Dan Gohman65f57c22009-07-15 16:35:29 +0000516 errs() << "Nondeterministic problem extracting blocks??\n";
Chris Lattner2290e752004-05-12 02:43:24 +0000517 delete ProgClone;
518 delete ToExtract;
519 return false;
520 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000521
Chris Lattner2290e752004-05-12 02:43:24 +0000522 // Otherwise, block extraction succeeded. Link the two program fragments back
523 // together.
524 delete ToExtract;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000525
Chris Lattner90c18c52004-11-16 06:31:38 +0000526 std::vector<std::pair<std::string, const FunctionType*> > MisCompFunctions;
527 for (Module::iterator I = Extracted->begin(), E = Extracted->end();
528 I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000529 if (!I->isDeclaration())
Chris Lattnerfa1af132004-11-19 07:09:40 +0000530 MisCompFunctions.push_back(std::make_pair(I->getName(),
531 I->getFunctionType()));
Chris Lattner90c18c52004-11-16 06:31:38 +0000532
Chris Lattner2290e752004-05-12 02:43:24 +0000533 std::string ErrorMsg;
Reid Spencere4874022004-12-13 03:01:03 +0000534 if (Linker::LinkModules(ProgClone, Extracted, &ErrorMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000535 errs() << BD.getToolName() << ": Error linking modules together:"
536 << ErrorMsg << '\n';
Chris Lattner2290e752004-05-12 02:43:24 +0000537 exit(1);
538 }
Chris Lattner90c18c52004-11-16 06:31:38 +0000539 delete Extracted;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000540
Chris Lattner2290e752004-05-12 02:43:24 +0000541 // Set the new program and delete the old one.
542 BD.setNewProgram(ProgClone);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000543
Chris Lattner2290e752004-05-12 02:43:24 +0000544 // Update the list of miscompiled functions.
545 MiscompiledFunctions.clear();
Chris Lattner5e783ab2004-05-11 21:54:13 +0000546
Chris Lattner90c18c52004-11-16 06:31:38 +0000547 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000548 Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first);
Chris Lattner90c18c52004-11-16 06:31:38 +0000549 assert(NewF && "Function not found??");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000550 assert(NewF->getFunctionType() == MisCompFunctions[i].second &&
551 "Function has wrong type??");
Chris Lattner90c18c52004-11-16 06:31:38 +0000552 MiscompiledFunctions.push_back(NewF);
553 }
Chris Lattner2290e752004-05-12 02:43:24 +0000554
555 return true;
Chris Lattner5e783ab2004-05-11 21:54:13 +0000556}
557
558
Chris Lattnerb15825b2004-04-05 21:37:38 +0000559/// DebugAMiscompilation - This is a generic driver to narrow down
560/// miscompilations, either in an optimization or a code generator.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000561///
Chris Lattnerb15825b2004-04-05 21:37:38 +0000562static std::vector<Function*>
563DebugAMiscompilation(BugDriver &BD,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000564 bool (*TestFn)(BugDriver &, Module *, Module *,
565 std::string &),
566 std::string &Error) {
Chris Lattnerb15825b2004-04-05 21:37:38 +0000567 // Okay, now that we have reduced the list of passes which are causing the
568 // failure, see if we can pin down which functions are being
569 // miscompiled... first build a list of all of the non-external functions in
570 // the program.
571 std::vector<Function*> MiscompiledFunctions;
572 Module *Prog = BD.getProgram();
573 for (Module::iterator I = Prog->begin(), E = Prog->end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000574 if (!I->isDeclaration())
Chris Lattnerb15825b2004-04-05 21:37:38 +0000575 MiscompiledFunctions.push_back(I);
576
577 // Do the reduction...
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000578 if (!BugpointIsInterrupted)
Nick Lewycky22ff7482010-04-12 05:08:25 +0000579 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
580 Error);
581 if (!Error.empty())
582 return MiscompiledFunctions;
Chris Lattnerb15825b2004-04-05 21:37:38 +0000583
Dan Gohmanac95cc72009-07-16 15:30:09 +0000584 outs() << "\n*** The following function"
585 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
586 << " being miscompiled: ";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000587 PrintFunctionList(MiscompiledFunctions);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000588 outs() << '\n';
Chris Lattnerb15825b2004-04-05 21:37:38 +0000589
590 // See if we can rip any loops out of the miscompiled functions and still
591 // trigger the problem.
Reid Spencerdc31a8a2006-11-11 19:05:02 +0000592
Nick Lewycky22ff7482010-04-12 05:08:25 +0000593 if (!BugpointIsInterrupted && !DisableLoopExtraction) {
594 bool Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions, Error);
595 if (!Error.empty())
596 return MiscompiledFunctions;
597 if (Ret) {
598 // Okay, we extracted some loops and the problem still appears. See if
599 // we can eliminate some of the created functions from being candidates.
600 DisambiguateGlobalSymbols(BD.getProgram());
Chris Lattner36ee07f2004-04-11 23:52:35 +0000601
Nick Lewycky22ff7482010-04-12 05:08:25 +0000602 // Do the reduction...
603 if (!BugpointIsInterrupted)
604 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
605 Error);
606 if (!Error.empty())
607 return MiscompiledFunctions;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000608
Nick Lewycky22ff7482010-04-12 05:08:25 +0000609 outs() << "\n*** The following function"
610 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
611 << " being miscompiled: ";
612 PrintFunctionList(MiscompiledFunctions);
613 outs() << '\n';
614 }
Chris Lattnerb15825b2004-04-05 21:37:38 +0000615 }
616
Nick Lewycky22ff7482010-04-12 05:08:25 +0000617 if (!BugpointIsInterrupted && !DisableBlockExtraction) {
618 bool Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions, Error);
619 if (!Error.empty())
620 return MiscompiledFunctions;
621 if (Ret) {
622 // Okay, we extracted some blocks and the problem still appears. See if
623 // we can eliminate some of the created functions from being candidates.
624 DisambiguateGlobalSymbols(BD.getProgram());
Chris Lattner5e783ab2004-05-11 21:54:13 +0000625
Nick Lewycky22ff7482010-04-12 05:08:25 +0000626 // Do the reduction...
627 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
628 Error);
629 if (!Error.empty())
630 return MiscompiledFunctions;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000631
Nick Lewycky22ff7482010-04-12 05:08:25 +0000632 outs() << "\n*** The following function"
633 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
634 << " being miscompiled: ";
635 PrintFunctionList(MiscompiledFunctions);
636 outs() << '\n';
637 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000638 }
639
Chris Lattnerb15825b2004-04-05 21:37:38 +0000640 return MiscompiledFunctions;
641}
642
Chris Lattnera57d86b2004-04-05 22:58:16 +0000643/// TestOptimizer - This is the predicate function used to check to see if the
644/// "Test" portion of the program is misoptimized. If so, return true. In any
645/// case, both module arguments are deleted.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000646///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000647static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe,
648 std::string &Error) {
Chris Lattnerb15825b2004-04-05 21:37:38 +0000649 // Run the optimization passes on ToOptimize, producing a transformed version
650 // of the functions being tested.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000651 outs() << " Optimizing functions being tested: ";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000652 Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
653 /*AutoDebugCrashes*/true);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000654 outs() << "done.\n";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000655 delete Test;
656
Dan Gohmanac95cc72009-07-16 15:30:09 +0000657 outs() << " Checking to see if the merged program executes correctly: ";
Nick Lewycky22ff7482010-04-12 05:08:25 +0000658 bool Broken = TestMergedProgram(BD, Optimized, Safe, true, Error);
659 if (Error.empty()) outs() << (Broken ? " nope.\n" : " yup.\n");
Chris Lattnerb15825b2004-04-05 21:37:38 +0000660 return Broken;
661}
662
663
Chris Lattner4a106452002-12-23 23:50:16 +0000664/// debugMiscompilation - This method is used when the passes selected are not
665/// crashing, but the generated output is semantically different from the
666/// input.
667///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000668void BugDriver::debugMiscompilation(std::string *Error) {
Chris Lattner4a106452002-12-23 23:50:16 +0000669 // Make sure something was miscompiled...
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000670 if (!BugpointIsInterrupted)
Nick Lewycky22ff7482010-04-12 05:08:25 +0000671 if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun, *Error)) {
672 if (Error->empty())
673 errs() << "*** Optimized program matches reference output! No problem"
674 << " detected...\nbugpoint can't help you with your problem!\n";
675 return;
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000676 }
Chris Lattner4a106452002-12-23 23:50:16 +0000677
Dan Gohmanac95cc72009-07-16 15:30:09 +0000678 outs() << "\n*** Found miscompiling pass"
679 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
680 << getPassesString(getPassesToRun()) << '\n';
Gabor Greif8ff70c22007-07-04 21:55:50 +0000681 EmitProgressBitcode("passinput");
Chris Lattner4a106452002-12-23 23:50:16 +0000682
Nick Lewycky22ff7482010-04-12 05:08:25 +0000683 std::vector<Function *> MiscompiledFunctions =
684 DebugAMiscompilation(*this, TestOptimizer, *Error);
685 if (!Error->empty())
686 return;
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000687
Gabor Greif8ff70c22007-07-04 21:55:50 +0000688 // Output a bunch of bitcode files for the user...
Dan Gohmanac95cc72009-07-16 15:30:09 +0000689 outs() << "Outputting reduced bitcode files which expose the problem:\n";
Dan Gohmand50330c2009-04-22 15:57:18 +0000690 DenseMap<const Value*, Value*> ValueMap;
691 Module *ToNotOptimize = CloneModule(getProgram(), ValueMap);
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000692 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
Dan Gohmand50330c2009-04-22 15:57:18 +0000693 MiscompiledFunctions,
694 ValueMap);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000695
Dan Gohmanac95cc72009-07-16 15:30:09 +0000696 outs() << " Non-optimized portion: ";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000697 ToNotOptimize = swapProgramIn(ToNotOptimize);
Gabor Greif8ff70c22007-07-04 21:55:50 +0000698 EmitProgressBitcode("tonotoptimize", true);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000699 setNewProgram(ToNotOptimize); // Delete hacked module.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000700
Dan Gohmanac95cc72009-07-16 15:30:09 +0000701 outs() << " Portion that is input to optimizer: ";
Chris Lattnerb15825b2004-04-05 21:37:38 +0000702 ToOptimize = swapProgramIn(ToOptimize);
Gabor Greif8ff70c22007-07-04 21:55:50 +0000703 EmitProgressBitcode("tooptimize");
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000704 setNewProgram(ToOptimize); // Delete hacked module.
Chris Lattner4a106452002-12-23 23:50:16 +0000705
Nick Lewycky22ff7482010-04-12 05:08:25 +0000706 return;
Chris Lattner4a106452002-12-23 23:50:16 +0000707}
Brian Gaeked0fde302003-11-11 22:41:34 +0000708
Chris Lattnera57d86b2004-04-05 22:58:16 +0000709/// CleanupAndPrepareModules - Get the specified modules ready for code
710/// generator testing.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000711///
Chris Lattnera57d86b2004-04-05 22:58:16 +0000712static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
713 Module *Safe) {
714 // Clean up the modules, removing extra cruft that we don't need anymore...
715 Test = BD.performFinalCleanups(Test);
716
717 // If we are executing the JIT, we have several nasty issues to take care of.
718 if (!BD.isExecutingJIT()) return;
719
720 // First, if the main function is in the Safe module, we must add a stub to
721 // the Test module to call into it. Thus, we create a new function `main'
722 // which just calls the old one.
Reid Spencer688b0492007-02-05 21:19:13 +0000723 if (Function *oldMain = Safe->getFunction("main"))
Reid Spencer5cbf9852007-01-30 20:08:39 +0000724 if (!oldMain->isDeclaration()) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000725 // Rename it
726 oldMain->setName("llvm_bugpoint_old_main");
727 // Create a NEW `main' function with same type in the test module.
Gabor Greif051a9502008-04-06 20:25:17 +0000728 Function *newMain = Function::Create(oldMain->getFunctionType(),
729 GlobalValue::ExternalLinkage,
730 "main", Test);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000731 // Create an `oldmain' prototype in the test module, which will
732 // corresponds to the real main function in the same module.
Gabor Greif051a9502008-04-06 20:25:17 +0000733 Function *oldMainProto = Function::Create(oldMain->getFunctionType(),
734 GlobalValue::ExternalLinkage,
735 oldMain->getName(), Test);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000736 // Set up and remember the argument list for the main function.
737 std::vector<Value*> args;
Alkis Evlogimenos5a1c58d2005-03-15 07:02:26 +0000738 for (Function::arg_iterator
739 I = newMain->arg_begin(), E = newMain->arg_end(),
740 OI = oldMain->arg_begin(); I != E; ++I, ++OI) {
Owen Anderson6bc41e82008-04-14 17:38:21 +0000741 I->setName(OI->getName()); // Copy argument names from oldMain
Chris Lattnera57d86b2004-04-05 22:58:16 +0000742 args.push_back(I);
743 }
744
745 // Call the old main function and return its result
Owen Anderson1d0be152009-08-13 21:58:54 +0000746 BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain);
Gabor Greif051a9502008-04-06 20:25:17 +0000747 CallInst *call = CallInst::Create(oldMainProto, args.begin(), args.end(),
748 "", BB);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000749
Chris Lattnera57d86b2004-04-05 22:58:16 +0000750 // If the type of old function wasn't void, return value of call
Owen Anderson1d0be152009-08-13 21:58:54 +0000751 ReturnInst::Create(Safe->getContext(), call, BB);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000752 }
753
754 // The second nasty issue we must deal with in the JIT is that the Safe
755 // module cannot directly reference any functions defined in the test
756 // module. Instead, we use a JIT API call to dynamically resolve the
757 // symbol.
Misha Brukman3da94ae2005-04-22 00:00:37 +0000758
Chris Lattnera57d86b2004-04-05 22:58:16 +0000759 // Add the resolver to the Safe module.
760 // Prototype: void *getPointerToNamedFunction(const char* Name)
Chris Lattner2db43c82007-01-07 08:13:39 +0000761 Constant *resolverFunc =
Chris Lattnera57d86b2004-04-05 22:58:16 +0000762 Safe->getOrInsertFunction("getPointerToNamedFunction",
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000763 Type::getInt8PtrTy(Safe->getContext()),
764 Type::getInt8PtrTy(Safe->getContext()),
Owen Anderson1d0be152009-08-13 21:58:54 +0000765 (Type *)0);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000766
Chris Lattnera57d86b2004-04-05 22:58:16 +0000767 // Use the function we just added to get addresses of functions we need.
Misha Brukmandc7fef82004-04-19 01:12:01 +0000768 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000769 if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc &&
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000770 !F->isIntrinsic() /* ignore intrinsics */) {
Reid Spencer688b0492007-02-05 21:19:13 +0000771 Function *TestFn = Test->getFunction(F->getName());
Chris Lattnera57d86b2004-04-05 22:58:16 +0000772
773 // Don't forward functions which are external in the test module too.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000774 if (TestFn && !TestFn->isDeclaration()) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000775 // 1. Add a string constant with its name to the global file
Owen Anderson1d0be152009-08-13 21:58:54 +0000776 Constant *InitArray = ConstantArray::get(F->getContext(), F->getName());
Chris Lattnera57d86b2004-04-05 22:58:16 +0000777 GlobalVariable *funcName =
Owen Andersone9b11b42009-07-08 19:03:57 +0000778 new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
Misha Brukman3da94ae2005-04-22 00:00:37 +0000779 GlobalValue::InternalLinkage, InitArray,
Owen Andersone9b11b42009-07-08 19:03:57 +0000780 F->getName() + "_name");
Chris Lattnera57d86b2004-04-05 22:58:16 +0000781
782 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
783 // sbyte* so it matches the signature of the resolver function.
784
785 // GetElementPtr *funcName, ulong 0, ulong 0
Owen Anderson1d0be152009-08-13 21:58:54 +0000786 std::vector<Constant*> GEPargs(2,
787 Constant::getNullValue(Type::getInt32Ty(F->getContext())));
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000788 Value *GEP =
Owen Andersonbaf3c402009-07-29 18:55:55 +0000789 ConstantExpr::getGetElementPtr(funcName, &GEPargs[0], 2);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000790 std::vector<Value*> ResolverArgs;
791 ResolverArgs.push_back(GEP);
792
Misha Brukmande4803d2004-04-19 03:36:47 +0000793 // Rewrite uses of F in global initializers, etc. to uses of a wrapper
794 // function that dynamically resolves the calls to F via our JIT API
Chris Lattnera3efca12005-07-12 01:00:32 +0000795 if (!F->use_empty()) {
796 // Create a new global to hold the cached function pointer.
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000797 Constant *NullPtr = ConstantPointerNull::get(F->getType());
Chris Lattnera3efca12005-07-12 01:00:32 +0000798 GlobalVariable *Cache =
Owen Andersone9b11b42009-07-08 19:03:57 +0000799 new GlobalVariable(*F->getParent(), F->getType(),
800 false, GlobalValue::InternalLinkage,
801 NullPtr,F->getName()+".fpcache");
Jeff Cohen00b168892005-07-27 06:12:32 +0000802
Misha Brukmande4803d2004-04-19 03:36:47 +0000803 // Construct a new stub function that will re-route calls to F
Misha Brukmandc7fef82004-04-19 01:12:01 +0000804 const FunctionType *FuncTy = F->getFunctionType();
Gabor Greif051a9502008-04-06 20:25:17 +0000805 Function *FuncWrapper = Function::Create(FuncTy,
806 GlobalValue::InternalLinkage,
807 F->getName() + "_wrapper",
808 F->getParent());
Owen Anderson1d0be152009-08-13 21:58:54 +0000809 BasicBlock *EntryBB = BasicBlock::Create(F->getContext(),
810 "entry", FuncWrapper);
811 BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(),
812 "usecache", FuncWrapper);
813 BasicBlock *LookupBB = BasicBlock::Create(F->getContext(),
814 "lookupfp", FuncWrapper);
Jeff Cohen00b168892005-07-27 06:12:32 +0000815
Chris Lattnera3efca12005-07-12 01:00:32 +0000816 // Check to see if we already looked up the value.
817 Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB);
Owen Anderson333c4002009-07-09 23:48:35 +0000818 Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal,
819 NullPtr, "isNull");
Gabor Greif051a9502008-04-06 20:25:17 +0000820 BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000821
Misha Brukmande4803d2004-04-19 03:36:47 +0000822 // Resolve the call to function F via the JIT API:
823 //
824 // call resolver(GetElementPtr...)
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000825 CallInst *Resolver =
826 CallInst::Create(resolverFunc, ResolverArgs.begin(),
827 ResolverArgs.end(), "resolver", LookupBB);
828
829 // Cast the result from the resolver to correctly-typed function.
830 CastInst *CastedResolver =
831 new BitCastInst(Resolver,
Owen Andersondebcb012009-07-29 22:17:13 +0000832 PointerType::getUnqual(F->getFunctionType()),
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000833 "resolverCast", LookupBB);
Reid Spencer3da59db2006-11-27 01:05:10 +0000834
Chris Lattnera3efca12005-07-12 01:00:32 +0000835 // Save the value in our cache.
836 new StoreInst(CastedResolver, Cache, LookupBB);
Gabor Greif051a9502008-04-06 20:25:17 +0000837 BranchInst::Create(DoCallBB, LookupBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000838
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000839 PHINode *FuncPtr = PHINode::Create(NullPtr->getType(),
840 "fp", DoCallBB);
Chris Lattnera3efca12005-07-12 01:00:32 +0000841 FuncPtr->addIncoming(CastedResolver, LookupBB);
842 FuncPtr->addIncoming(CachedVal, EntryBB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000843
Chris Lattnera3efca12005-07-12 01:00:32 +0000844 // Save the argument list.
Misha Brukmandc7fef82004-04-19 01:12:01 +0000845 std::vector<Value*> Args;
Alkis Evlogimenos5a1c58d2005-03-15 07:02:26 +0000846 for (Function::arg_iterator i = FuncWrapper->arg_begin(),
847 e = FuncWrapper->arg_end(); i != e; ++i)
Misha Brukmandc7fef82004-04-19 01:12:01 +0000848 Args.push_back(i);
849
850 // Pass on the arguments to the real function, return its result
Owen Anderson1d0be152009-08-13 21:58:54 +0000851 if (F->getReturnType() == Type::getVoidTy(F->getContext())) {
Gabor Greif051a9502008-04-06 20:25:17 +0000852 CallInst::Create(FuncPtr, Args.begin(), Args.end(), "", DoCallBB);
Owen Anderson1d0be152009-08-13 21:58:54 +0000853 ReturnInst::Create(F->getContext(), DoCallBB);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000854 } else {
Gabor Greif051a9502008-04-06 20:25:17 +0000855 CallInst *Call = CallInst::Create(FuncPtr, Args.begin(), Args.end(),
856 "retval", DoCallBB);
Owen Anderson1d0be152009-08-13 21:58:54 +0000857 ReturnInst::Create(F->getContext(),Call, DoCallBB);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000858 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000859
Misha Brukmande4803d2004-04-19 03:36:47 +0000860 // Use the wrapper function instead of the old function
861 F->replaceAllUsesWith(FuncWrapper);
Misha Brukmandc7fef82004-04-19 01:12:01 +0000862 }
Chris Lattnera57d86b2004-04-05 22:58:16 +0000863 }
864 }
865 }
866
867 if (verifyModule(*Test) || verifyModule(*Safe)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000868 errs() << "Bugpoint has a bug, which corrupted a module!!\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000869 abort();
870 }
871}
872
873
874
875/// TestCodeGenerator - This is the predicate function used to check to see if
876/// the "Test" portion of the program is miscompiled by the code generator under
877/// test. If so, return true. In any case, both module arguments are deleted.
Misha Brukman8c194ea2004-04-21 18:36:43 +0000878///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000879static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe,
880 std::string &Error) {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000881 CleanupAndPrepareModules(BD, Test, Safe);
882
Reid Spencer97182982004-12-15 01:53:08 +0000883 sys::Path TestModuleBC("bugpoint.test.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000884 std::string ErrMsg;
885 if (TestModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000886 errs() << BD.getToolName() << "Error making unique filename: "
887 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000888 exit(1);
889 }
Chris Lattner74382b72009-08-23 22:45:37 +0000890 if (BD.writeProgramToFile(TestModuleBC.str(), Test)) {
891 errs() << "Error writing bitcode to `" << TestModuleBC.str()
892 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000893 exit(1);
894 }
895 delete Test;
896
897 // Make the shared library
Reid Spencer97182982004-12-15 01:53:08 +0000898 sys::Path SafeModuleBC("bugpoint.safe.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000899 if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000900 errs() << BD.getToolName() << "Error making unique filename: "
901 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000902 exit(1);
903 }
Chris Lattnera57d86b2004-04-05 22:58:16 +0000904
Chris Lattner74382b72009-08-23 22:45:37 +0000905 if (BD.writeProgramToFile(SafeModuleBC.str(), Safe)) {
906 errs() << "Error writing bitcode to `" << SafeModuleBC.str()
907 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000908 exit(1);
909 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000910 std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str(), Error);
911 if (!Error.empty())
Benjamin Kramer27063872010-04-12 12:22:19 +0000912 return false;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000913 delete Safe;
914
915 // Run the code generator on the `Test' code, loading the shared library.
916 // The function returns whether or not the new output differs from reference.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000917 bool Result = BD.diffProgram(TestModuleBC.str(), SharedObject, false, &Error);
918 if (!Error.empty())
919 return false;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000920
921 if (Result)
Dan Gohman65f57c22009-07-15 16:35:29 +0000922 errs() << ": still failing!\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000923 else
Dan Gohman65f57c22009-07-15 16:35:29 +0000924 errs() << ": didn't fail.\n";
Reid Spencera229c5c2005-07-08 03:08:58 +0000925 TestModuleBC.eraseFromDisk();
926 SafeModuleBC.eraseFromDisk();
927 sys::Path(SharedObject).eraseFromDisk();
Chris Lattnera57d86b2004-04-05 22:58:16 +0000928
929 return Result;
930}
931
932
Misha Brukman8c194ea2004-04-21 18:36:43 +0000933/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
934///
Nick Lewycky22ff7482010-04-12 05:08:25 +0000935bool BugDriver::debugCodeGenerator(std::string *Error) {
Dan Gohman70ef4492008-12-08 04:02:47 +0000936 if ((void*)SafeInterpreter == (void*)Interpreter) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000937 std::string Result = executeProgramSafely("bugpoint.safe.out", Error);
938 if (Error->empty()) {
939 outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match "
940 << "the reference diff. This may be due to a\n front-end "
941 << "bug or a bug in the original program, but this can also "
942 << "happen if bugpoint isn't running the program with the "
943 << "right flags or input.\n I left the result of executing "
944 << "the program with the \"safe\" backend in this file for "
945 << "you: '"
946 << Result << "'.\n";
947 }
Chris Lattnera57d86b2004-04-05 22:58:16 +0000948 return true;
949 }
950
951 DisambiguateGlobalSymbols(Program);
952
Nick Lewycky22ff7482010-04-12 05:08:25 +0000953 std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator,
954 *Error);
955 if (!Error->empty())
956 return true;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000957
958 // Split the module into the two halves of the program we want.
Dan Gohmand50330c2009-04-22 15:57:18 +0000959 DenseMap<const Value*, Value*> ValueMap;
960 Module *ToNotCodeGen = CloneModule(getProgram(), ValueMap);
961 Module *ToCodeGen = SplitFunctionsOutOfModule(ToNotCodeGen, Funcs, ValueMap);
Chris Lattnera57d86b2004-04-05 22:58:16 +0000962
963 // Condition the modules
964 CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen);
965
Reid Spencer97182982004-12-15 01:53:08 +0000966 sys::Path TestModuleBC("bugpoint.test.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000967 std::string ErrMsg;
968 if (TestModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000969 errs() << getToolName() << "Error making unique filename: "
970 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000971 exit(1);
972 }
Reid Spencer97182982004-12-15 01:53:08 +0000973
Chris Lattner74382b72009-08-23 22:45:37 +0000974 if (writeProgramToFile(TestModuleBC.str(), ToCodeGen)) {
975 errs() << "Error writing bitcode to `" << TestModuleBC.str()
976 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000977 exit(1);
978 }
979 delete ToCodeGen;
980
981 // Make the shared library
Reid Spencer97182982004-12-15 01:53:08 +0000982 sys::Path SafeModuleBC("bugpoint.safe.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000983 if (SafeModuleBC.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000984 errs() << getToolName() << "Error making unique filename: "
985 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000986 exit(1);
987 }
Reid Spencer97182982004-12-15 01:53:08 +0000988
Chris Lattner74382b72009-08-23 22:45:37 +0000989 if (writeProgramToFile(SafeModuleBC.str(), ToNotCodeGen)) {
990 errs() << "Error writing bitcode to `" << SafeModuleBC.str()
991 << "'\nExiting.";
Chris Lattnera57d86b2004-04-05 22:58:16 +0000992 exit(1);
993 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000994 std::string SharedObject = compileSharedObject(SafeModuleBC.str(), *Error);
995 if (!Error->empty())
996 return true;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000997 delete ToNotCodeGen;
998
Dan Gohmanac95cc72009-07-16 15:30:09 +0000999 outs() << "You can reproduce the problem with the command line: \n";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001000 if (isExecutingJIT()) {
Chris Lattner74382b72009-08-23 22:45:37 +00001001 outs() << " lli -load " << SharedObject << " " << TestModuleBC.str();
Chris Lattnera57d86b2004-04-05 22:58:16 +00001002 } else {
Chris Lattner74382b72009-08-23 22:45:37 +00001003 outs() << " llc -f " << TestModuleBC.str() << " -o " << TestModuleBC.str()
1004 << ".s\n";
1005 outs() << " gcc " << SharedObject << " " << TestModuleBC.str()
1006 << ".s -o " << TestModuleBC.str() << ".exe";
Chris Lattner59615f02005-01-15 00:07:19 +00001007#if defined (HAVE_LINK_R)
Dan Gohmanac95cc72009-07-16 15:30:09 +00001008 outs() << " -Wl,-R.";
Chris Lattner59615f02005-01-15 00:07:19 +00001009#endif
Dan Gohmanac95cc72009-07-16 15:30:09 +00001010 outs() << "\n";
Chris Lattner74382b72009-08-23 22:45:37 +00001011 outs() << " " << TestModuleBC.str() << ".exe";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001012 }
Nick Lewycky22ff7482010-04-12 05:08:25 +00001013 for (unsigned i = 0, e = InputArgv.size(); i != e; ++i)
Dan Gohmanac95cc72009-07-16 15:30:09 +00001014 outs() << " " << InputArgv[i];
1015 outs() << '\n';
1016 outs() << "The shared object was created with:\n llc -march=c "
Chris Lattner74382b72009-08-23 22:45:37 +00001017 << SafeModuleBC.str() << " -o temporary.c\n"
Daniel Dunbarca740962009-08-18 03:35:57 +00001018 << " gcc -xc temporary.c -O2 -o " << SharedObject;
1019 if (TargetTriple.getArch() == Triple::sparc)
1020 outs() << " -G"; // Compile a shared library, `-G' for Sparc
1021 else
1022 outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others
1023
1024 outs() << " -fno-strict-aliasing\n";
Chris Lattnera57d86b2004-04-05 22:58:16 +00001025
1026 return false;
1027}