xref: /aosp_15_r20/external/llvm/tools/bugpoint/Miscompilation.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- Miscompilation.cpp - Debug program miscompilations -----------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements optimizer and code generation miscompilation debugging
11*9880d681SAndroid Build Coastguard Worker // support.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #include "BugDriver.h"
16*9880d681SAndroid Build Coastguard Worker #include "ListReducer.h"
17*9880d681SAndroid Build Coastguard Worker #include "ToolRunner.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Config/config.h"   // for HAVE_LINK_R
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Verifier.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Linker/Linker.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FileUtilities.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/Cloning.h"
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker using namespace llvm;
31*9880d681SAndroid Build Coastguard Worker 
32*9880d681SAndroid Build Coastguard Worker namespace llvm {
33*9880d681SAndroid Build Coastguard Worker   extern cl::opt<std::string> OutputPrefix;
34*9880d681SAndroid Build Coastguard Worker   extern cl::list<std::string> InputArgv;
35*9880d681SAndroid Build Coastguard Worker } // end namespace llvm
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker namespace {
38*9880d681SAndroid Build Coastguard Worker   static llvm::cl::opt<bool>
39*9880d681SAndroid Build Coastguard Worker     DisableLoopExtraction("disable-loop-extraction",
40*9880d681SAndroid Build Coastguard Worker         cl::desc("Don't extract loops when searching for miscompilations"),
41*9880d681SAndroid Build Coastguard Worker         cl::init(false));
42*9880d681SAndroid Build Coastguard Worker   static llvm::cl::opt<bool>
43*9880d681SAndroid Build Coastguard Worker     DisableBlockExtraction("disable-block-extraction",
44*9880d681SAndroid Build Coastguard Worker         cl::desc("Don't extract blocks when searching for miscompilations"),
45*9880d681SAndroid Build Coastguard Worker         cl::init(false));
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker   class ReduceMiscompilingPasses : public ListReducer<std::string> {
48*9880d681SAndroid Build Coastguard Worker     BugDriver &BD;
49*9880d681SAndroid Build Coastguard Worker   public:
ReduceMiscompilingPasses(BugDriver & bd)50*9880d681SAndroid Build Coastguard Worker     ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker     TestResult doTest(std::vector<std::string> &Prefix,
53*9880d681SAndroid Build Coastguard Worker                       std::vector<std::string> &Suffix,
54*9880d681SAndroid Build Coastguard Worker                       std::string &Error) override;
55*9880d681SAndroid Build Coastguard Worker   };
56*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
57*9880d681SAndroid Build Coastguard Worker 
58*9880d681SAndroid Build Coastguard Worker /// TestResult - After passes have been split into a test group and a control
59*9880d681SAndroid Build Coastguard Worker /// group, see if they still break the program.
60*9880d681SAndroid Build Coastguard Worker ///
61*9880d681SAndroid Build Coastguard Worker ReduceMiscompilingPasses::TestResult
doTest(std::vector<std::string> & Prefix,std::vector<std::string> & Suffix,std::string & Error)62*9880d681SAndroid Build Coastguard Worker ReduceMiscompilingPasses::doTest(std::vector<std::string> &Prefix,
63*9880d681SAndroid Build Coastguard Worker                                  std::vector<std::string> &Suffix,
64*9880d681SAndroid Build Coastguard Worker                                  std::string &Error) {
65*9880d681SAndroid Build Coastguard Worker   // First, run the program with just the Suffix passes.  If it is still broken
66*9880d681SAndroid Build Coastguard Worker   // with JUST the kept passes, discard the prefix passes.
67*9880d681SAndroid Build Coastguard Worker   outs() << "Checking to see if '" << getPassesString(Suffix)
68*9880d681SAndroid Build Coastguard Worker          << "' compiles correctly: ";
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker   std::string BitcodeResult;
71*9880d681SAndroid Build Coastguard Worker   if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false/*delete*/,
72*9880d681SAndroid Build Coastguard Worker                    true/*quiet*/)) {
73*9880d681SAndroid Build Coastguard Worker     errs() << " Error running this sequence of passes"
74*9880d681SAndroid Build Coastguard Worker            << " on the input program!\n";
75*9880d681SAndroid Build Coastguard Worker     BD.setPassesToRun(Suffix);
76*9880d681SAndroid Build Coastguard Worker     BD.EmitProgressBitcode(BD.getProgram(), "pass-error",  false);
77*9880d681SAndroid Build Coastguard Worker     exit(BD.debugOptimizerCrash());
78*9880d681SAndroid Build Coastguard Worker   }
79*9880d681SAndroid Build Coastguard Worker 
80*9880d681SAndroid Build Coastguard Worker   // Check to see if the finished program matches the reference output...
81*9880d681SAndroid Build Coastguard Worker   bool Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "",
82*9880d681SAndroid Build Coastguard Worker                              true /*delete bitcode*/, &Error);
83*9880d681SAndroid Build Coastguard Worker   if (!Error.empty())
84*9880d681SAndroid Build Coastguard Worker     return InternalError;
85*9880d681SAndroid Build Coastguard Worker   if (Diff) {
86*9880d681SAndroid Build Coastguard Worker     outs() << " nope.\n";
87*9880d681SAndroid Build Coastguard Worker     if (Suffix.empty()) {
88*9880d681SAndroid Build Coastguard Worker       errs() << BD.getToolName() << ": I'm confused: the test fails when "
89*9880d681SAndroid Build Coastguard Worker              << "no passes are run, nondeterministic program?\n";
90*9880d681SAndroid Build Coastguard Worker       exit(1);
91*9880d681SAndroid Build Coastguard Worker     }
92*9880d681SAndroid Build Coastguard Worker     return KeepSuffix;         // Miscompilation detected!
93*9880d681SAndroid Build Coastguard Worker   }
94*9880d681SAndroid Build Coastguard Worker   outs() << " yup.\n";      // No miscompilation!
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker   if (Prefix.empty()) return NoFailure;
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker   // Next, see if the program is broken if we run the "prefix" passes first,
99*9880d681SAndroid Build Coastguard Worker   // then separately run the "kept" passes.
100*9880d681SAndroid Build Coastguard Worker   outs() << "Checking to see if '" << getPassesString(Prefix)
101*9880d681SAndroid Build Coastguard Worker          << "' compiles correctly: ";
102*9880d681SAndroid Build Coastguard Worker 
103*9880d681SAndroid Build Coastguard Worker   // If it is not broken with the kept passes, it's possible that the prefix
104*9880d681SAndroid Build Coastguard Worker   // passes must be run before the kept passes to break it.  If the program
105*9880d681SAndroid Build Coastguard Worker   // WORKS after the prefix passes, but then fails if running the prefix AND
106*9880d681SAndroid Build Coastguard Worker   // kept passes, we can update our bitcode file to include the result of the
107*9880d681SAndroid Build Coastguard Worker   // prefix passes, then discard the prefix passes.
108*9880d681SAndroid Build Coastguard Worker   //
109*9880d681SAndroid Build Coastguard Worker   if (BD.runPasses(BD.getProgram(), Prefix, BitcodeResult, false/*delete*/,
110*9880d681SAndroid Build Coastguard Worker                    true/*quiet*/)) {
111*9880d681SAndroid Build Coastguard Worker     errs() << " Error running this sequence of passes"
112*9880d681SAndroid Build Coastguard Worker            << " on the input program!\n";
113*9880d681SAndroid Build Coastguard Worker     BD.setPassesToRun(Prefix);
114*9880d681SAndroid Build Coastguard Worker     BD.EmitProgressBitcode(BD.getProgram(), "pass-error",  false);
115*9880d681SAndroid Build Coastguard Worker     exit(BD.debugOptimizerCrash());
116*9880d681SAndroid Build Coastguard Worker   }
117*9880d681SAndroid Build Coastguard Worker 
118*9880d681SAndroid Build Coastguard Worker   // If the prefix maintains the predicate by itself, only keep the prefix!
119*9880d681SAndroid Build Coastguard Worker   Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", false, &Error);
120*9880d681SAndroid Build Coastguard Worker   if (!Error.empty())
121*9880d681SAndroid Build Coastguard Worker     return InternalError;
122*9880d681SAndroid Build Coastguard Worker   if (Diff) {
123*9880d681SAndroid Build Coastguard Worker     outs() << " nope.\n";
124*9880d681SAndroid Build Coastguard Worker     sys::fs::remove(BitcodeResult);
125*9880d681SAndroid Build Coastguard Worker     return KeepPrefix;
126*9880d681SAndroid Build Coastguard Worker   }
127*9880d681SAndroid Build Coastguard Worker   outs() << " yup.\n";      // No miscompilation!
128*9880d681SAndroid Build Coastguard Worker 
129*9880d681SAndroid Build Coastguard Worker   // Ok, so now we know that the prefix passes work, try running the suffix
130*9880d681SAndroid Build Coastguard Worker   // passes on the result of the prefix passes.
131*9880d681SAndroid Build Coastguard Worker   //
132*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> PrefixOutput =
133*9880d681SAndroid Build Coastguard Worker       parseInputFile(BitcodeResult, BD.getContext());
134*9880d681SAndroid Build Coastguard Worker   if (!PrefixOutput) {
135*9880d681SAndroid Build Coastguard Worker     errs() << BD.getToolName() << ": Error reading bitcode file '"
136*9880d681SAndroid Build Coastguard Worker            << BitcodeResult << "'!\n";
137*9880d681SAndroid Build Coastguard Worker     exit(1);
138*9880d681SAndroid Build Coastguard Worker   }
139*9880d681SAndroid Build Coastguard Worker   sys::fs::remove(BitcodeResult);
140*9880d681SAndroid Build Coastguard Worker 
141*9880d681SAndroid Build Coastguard Worker   // Don't check if there are no passes in the suffix.
142*9880d681SAndroid Build Coastguard Worker   if (Suffix.empty())
143*9880d681SAndroid Build Coastguard Worker     return NoFailure;
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker   outs() << "Checking to see if '" << getPassesString(Suffix)
146*9880d681SAndroid Build Coastguard Worker             << "' passes compile correctly after the '"
147*9880d681SAndroid Build Coastguard Worker             << getPassesString(Prefix) << "' passes: ";
148*9880d681SAndroid Build Coastguard Worker 
149*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> OriginalInput(
150*9880d681SAndroid Build Coastguard Worker       BD.swapProgramIn(PrefixOutput.release()));
151*9880d681SAndroid Build Coastguard Worker   if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false/*delete*/,
152*9880d681SAndroid Build Coastguard Worker                    true/*quiet*/)) {
153*9880d681SAndroid Build Coastguard Worker     errs() << " Error running this sequence of passes"
154*9880d681SAndroid Build Coastguard Worker            << " on the input program!\n";
155*9880d681SAndroid Build Coastguard Worker     BD.setPassesToRun(Suffix);
156*9880d681SAndroid Build Coastguard Worker     BD.EmitProgressBitcode(BD.getProgram(), "pass-error",  false);
157*9880d681SAndroid Build Coastguard Worker     exit(BD.debugOptimizerCrash());
158*9880d681SAndroid Build Coastguard Worker   }
159*9880d681SAndroid Build Coastguard Worker 
160*9880d681SAndroid Build Coastguard Worker   // Run the result...
161*9880d681SAndroid Build Coastguard Worker   Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "",
162*9880d681SAndroid Build Coastguard Worker                         true /*delete bitcode*/, &Error);
163*9880d681SAndroid Build Coastguard Worker   if (!Error.empty())
164*9880d681SAndroid Build Coastguard Worker     return InternalError;
165*9880d681SAndroid Build Coastguard Worker   if (Diff) {
166*9880d681SAndroid Build Coastguard Worker     outs() << " nope.\n";
167*9880d681SAndroid Build Coastguard Worker     return KeepSuffix;
168*9880d681SAndroid Build Coastguard Worker   }
169*9880d681SAndroid Build Coastguard Worker 
170*9880d681SAndroid Build Coastguard Worker   // Otherwise, we must not be running the bad pass anymore.
171*9880d681SAndroid Build Coastguard Worker   outs() << " yup.\n";      // No miscompilation!
172*9880d681SAndroid Build Coastguard Worker   // Restore orig program & free test.
173*9880d681SAndroid Build Coastguard Worker   delete BD.swapProgramIn(OriginalInput.release());
174*9880d681SAndroid Build Coastguard Worker   return NoFailure;
175*9880d681SAndroid Build Coastguard Worker }
176*9880d681SAndroid Build Coastguard Worker 
177*9880d681SAndroid Build Coastguard Worker namespace {
178*9880d681SAndroid Build Coastguard Worker   class ReduceMiscompilingFunctions : public ListReducer<Function*> {
179*9880d681SAndroid Build Coastguard Worker     BugDriver &BD;
180*9880d681SAndroid Build Coastguard Worker     bool (*TestFn)(BugDriver &, std::unique_ptr<Module>,
181*9880d681SAndroid Build Coastguard Worker                    std::unique_ptr<Module>, std::string &);
182*9880d681SAndroid Build Coastguard Worker 
183*9880d681SAndroid Build Coastguard Worker   public:
ReduceMiscompilingFunctions(BugDriver & bd,bool (* F)(BugDriver &,std::unique_ptr<Module>,std::unique_ptr<Module>,std::string &))184*9880d681SAndroid Build Coastguard Worker     ReduceMiscompilingFunctions(BugDriver &bd,
185*9880d681SAndroid Build Coastguard Worker                                 bool (*F)(BugDriver &, std::unique_ptr<Module>,
186*9880d681SAndroid Build Coastguard Worker                                           std::unique_ptr<Module>,
187*9880d681SAndroid Build Coastguard Worker                                           std::string &))
188*9880d681SAndroid Build Coastguard Worker         : BD(bd), TestFn(F) {}
189*9880d681SAndroid Build Coastguard Worker 
doTest(std::vector<Function * > & Prefix,std::vector<Function * > & Suffix,std::string & Error)190*9880d681SAndroid Build Coastguard Worker     TestResult doTest(std::vector<Function*> &Prefix,
191*9880d681SAndroid Build Coastguard Worker                       std::vector<Function*> &Suffix,
192*9880d681SAndroid Build Coastguard Worker                       std::string &Error) override {
193*9880d681SAndroid Build Coastguard Worker       if (!Suffix.empty()) {
194*9880d681SAndroid Build Coastguard Worker         bool Ret = TestFuncs(Suffix, Error);
195*9880d681SAndroid Build Coastguard Worker         if (!Error.empty())
196*9880d681SAndroid Build Coastguard Worker           return InternalError;
197*9880d681SAndroid Build Coastguard Worker         if (Ret)
198*9880d681SAndroid Build Coastguard Worker           return KeepSuffix;
199*9880d681SAndroid Build Coastguard Worker       }
200*9880d681SAndroid Build Coastguard Worker       if (!Prefix.empty()) {
201*9880d681SAndroid Build Coastguard Worker         bool Ret = TestFuncs(Prefix, Error);
202*9880d681SAndroid Build Coastguard Worker         if (!Error.empty())
203*9880d681SAndroid Build Coastguard Worker           return InternalError;
204*9880d681SAndroid Build Coastguard Worker         if (Ret)
205*9880d681SAndroid Build Coastguard Worker           return KeepPrefix;
206*9880d681SAndroid Build Coastguard Worker       }
207*9880d681SAndroid Build Coastguard Worker       return NoFailure;
208*9880d681SAndroid Build Coastguard Worker     }
209*9880d681SAndroid Build Coastguard Worker 
210*9880d681SAndroid Build Coastguard Worker     bool TestFuncs(const std::vector<Function*> &Prefix, std::string &Error);
211*9880d681SAndroid Build Coastguard Worker   };
212*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
213*9880d681SAndroid Build Coastguard Worker 
214*9880d681SAndroid Build Coastguard Worker /// Given two modules, link them together and run the program, checking to see
215*9880d681SAndroid Build Coastguard Worker /// if the program matches the diff. If there is an error, return NULL. If not,
216*9880d681SAndroid Build Coastguard Worker /// return the merged module. The Broken argument will be set to true if the
217*9880d681SAndroid Build Coastguard Worker /// output is different. If the DeleteInputs argument is set to true then this
218*9880d681SAndroid Build Coastguard Worker /// function deletes both input modules before it returns.
219*9880d681SAndroid Build Coastguard Worker ///
testMergedProgram(const BugDriver & BD,std::unique_ptr<Module> M1,std::unique_ptr<Module> M2,std::string & Error,bool & Broken)220*9880d681SAndroid Build Coastguard Worker static std::unique_ptr<Module> testMergedProgram(const BugDriver &BD,
221*9880d681SAndroid Build Coastguard Worker                                                  std::unique_ptr<Module> M1,
222*9880d681SAndroid Build Coastguard Worker                                                  std::unique_ptr<Module> M2,
223*9880d681SAndroid Build Coastguard Worker                                                  std::string &Error,
224*9880d681SAndroid Build Coastguard Worker                                                  bool &Broken) {
225*9880d681SAndroid Build Coastguard Worker   if (Linker::linkModules(*M1, std::move(M2)))
226*9880d681SAndroid Build Coastguard Worker     exit(1);
227*9880d681SAndroid Build Coastguard Worker 
228*9880d681SAndroid Build Coastguard Worker   // Execute the program.
229*9880d681SAndroid Build Coastguard Worker   Broken = BD.diffProgram(M1.get(), "", "", false, &Error);
230*9880d681SAndroid Build Coastguard Worker   if (!Error.empty())
231*9880d681SAndroid Build Coastguard Worker     return nullptr;
232*9880d681SAndroid Build Coastguard Worker   return M1;
233*9880d681SAndroid Build Coastguard Worker }
234*9880d681SAndroid Build Coastguard Worker 
235*9880d681SAndroid Build Coastguard Worker /// TestFuncs - split functions in a Module into two groups: those that are
236*9880d681SAndroid Build Coastguard Worker /// under consideration for miscompilation vs. those that are not, and test
237*9880d681SAndroid Build Coastguard Worker /// accordingly. Each group of functions becomes a separate Module.
238*9880d681SAndroid Build Coastguard Worker ///
TestFuncs(const std::vector<Function * > & Funcs,std::string & Error)239*9880d681SAndroid Build Coastguard Worker bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
240*9880d681SAndroid Build Coastguard Worker                                             std::string &Error) {
241*9880d681SAndroid Build Coastguard Worker   // Test to see if the function is misoptimized if we ONLY run it on the
242*9880d681SAndroid Build Coastguard Worker   // functions listed in Funcs.
243*9880d681SAndroid Build Coastguard Worker   outs() << "Checking to see if the program is misoptimized when "
244*9880d681SAndroid Build Coastguard Worker          << (Funcs.size()==1 ? "this function is" : "these functions are")
245*9880d681SAndroid Build Coastguard Worker          << " run through the pass"
246*9880d681SAndroid Build Coastguard Worker          << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
247*9880d681SAndroid Build Coastguard Worker   PrintFunctionList(Funcs);
248*9880d681SAndroid Build Coastguard Worker   outs() << '\n';
249*9880d681SAndroid Build Coastguard Worker 
250*9880d681SAndroid Build Coastguard Worker   // Create a clone for two reasons:
251*9880d681SAndroid Build Coastguard Worker   // * If the optimization passes delete any function, the deleted function
252*9880d681SAndroid Build Coastguard Worker   //   will be in the clone and Funcs will still point to valid memory
253*9880d681SAndroid Build Coastguard Worker   // * If the optimization passes use interprocedural information to break
254*9880d681SAndroid Build Coastguard Worker   //   a function, we want to continue with the original function. Otherwise
255*9880d681SAndroid Build Coastguard Worker   //   we can conclude that a function triggers the bug when in fact one
256*9880d681SAndroid Build Coastguard Worker   //   needs a larger set of original functions to do so.
257*9880d681SAndroid Build Coastguard Worker   ValueToValueMapTy VMap;
258*9880d681SAndroid Build Coastguard Worker   Module *Clone = CloneModule(BD.getProgram(), VMap).release();
259*9880d681SAndroid Build Coastguard Worker   Module *Orig = BD.swapProgramIn(Clone);
260*9880d681SAndroid Build Coastguard Worker 
261*9880d681SAndroid Build Coastguard Worker   std::vector<Function*> FuncsOnClone;
262*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
263*9880d681SAndroid Build Coastguard Worker     Function *F = cast<Function>(VMap[Funcs[i]]);
264*9880d681SAndroid Build Coastguard Worker     FuncsOnClone.push_back(F);
265*9880d681SAndroid Build Coastguard Worker   }
266*9880d681SAndroid Build Coastguard Worker 
267*9880d681SAndroid Build Coastguard Worker   // Split the module into the two halves of the program we want.
268*9880d681SAndroid Build Coastguard Worker   VMap.clear();
269*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);
270*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> ToOptimize =
271*9880d681SAndroid Build Coastguard Worker       SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap);
272*9880d681SAndroid Build Coastguard Worker 
273*9880d681SAndroid Build Coastguard Worker   bool Broken =
274*9880d681SAndroid Build Coastguard Worker       TestFn(BD, std::move(ToOptimize), std::move(ToNotOptimize), Error);
275*9880d681SAndroid Build Coastguard Worker 
276*9880d681SAndroid Build Coastguard Worker   delete BD.swapProgramIn(Orig);
277*9880d681SAndroid Build Coastguard Worker 
278*9880d681SAndroid Build Coastguard Worker   return Broken;
279*9880d681SAndroid Build Coastguard Worker }
280*9880d681SAndroid Build Coastguard Worker 
281*9880d681SAndroid Build Coastguard Worker /// DisambiguateGlobalSymbols - Give anonymous global values names.
282*9880d681SAndroid Build Coastguard Worker ///
DisambiguateGlobalSymbols(Module * M)283*9880d681SAndroid Build Coastguard Worker static void DisambiguateGlobalSymbols(Module *M) {
284*9880d681SAndroid Build Coastguard Worker   for (Module::global_iterator I = M->global_begin(), E = M->global_end();
285*9880d681SAndroid Build Coastguard Worker        I != E; ++I)
286*9880d681SAndroid Build Coastguard Worker     if (!I->hasName())
287*9880d681SAndroid Build Coastguard Worker       I->setName("anon_global");
288*9880d681SAndroid Build Coastguard Worker   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
289*9880d681SAndroid Build Coastguard Worker     if (!I->hasName())
290*9880d681SAndroid Build Coastguard Worker       I->setName("anon_fn");
291*9880d681SAndroid Build Coastguard Worker }
292*9880d681SAndroid Build Coastguard Worker 
293*9880d681SAndroid Build Coastguard Worker /// Given a reduced list of functions that still exposed the bug, check to see
294*9880d681SAndroid Build Coastguard Worker /// if we can extract the loops in the region without obscuring the bug.  If so,
295*9880d681SAndroid Build Coastguard Worker /// it reduces the amount of code identified.
296*9880d681SAndroid Build Coastguard Worker ///
ExtractLoops(BugDriver & BD,bool (* TestFn)(BugDriver &,std::unique_ptr<Module>,std::unique_ptr<Module>,std::string &),std::vector<Function * > & MiscompiledFunctions,std::string & Error)297*9880d681SAndroid Build Coastguard Worker static bool ExtractLoops(BugDriver &BD,
298*9880d681SAndroid Build Coastguard Worker                          bool (*TestFn)(BugDriver &, std::unique_ptr<Module>,
299*9880d681SAndroid Build Coastguard Worker                                         std::unique_ptr<Module>, std::string &),
300*9880d681SAndroid Build Coastguard Worker                          std::vector<Function *> &MiscompiledFunctions,
301*9880d681SAndroid Build Coastguard Worker                          std::string &Error) {
302*9880d681SAndroid Build Coastguard Worker   bool MadeChange = false;
303*9880d681SAndroid Build Coastguard Worker   while (1) {
304*9880d681SAndroid Build Coastguard Worker     if (BugpointIsInterrupted) return MadeChange;
305*9880d681SAndroid Build Coastguard Worker 
306*9880d681SAndroid Build Coastguard Worker     ValueToValueMapTy VMap;
307*9880d681SAndroid Build Coastguard Worker     std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);
308*9880d681SAndroid Build Coastguard Worker     Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize.get(),
309*9880d681SAndroid Build Coastguard Worker                                                    MiscompiledFunctions, VMap)
310*9880d681SAndroid Build Coastguard Worker                              .release();
311*9880d681SAndroid Build Coastguard Worker     std::unique_ptr<Module> ToOptimizeLoopExtracted =
312*9880d681SAndroid Build Coastguard Worker         BD.extractLoop(ToOptimize);
313*9880d681SAndroid Build Coastguard Worker     if (!ToOptimizeLoopExtracted) {
314*9880d681SAndroid Build Coastguard Worker       // If the loop extractor crashed or if there were no extractible loops,
315*9880d681SAndroid Build Coastguard Worker       // then this chapter of our odyssey is over with.
316*9880d681SAndroid Build Coastguard Worker       delete ToOptimize;
317*9880d681SAndroid Build Coastguard Worker       return MadeChange;
318*9880d681SAndroid Build Coastguard Worker     }
319*9880d681SAndroid Build Coastguard Worker 
320*9880d681SAndroid Build Coastguard Worker     errs() << "Extracted a loop from the breaking portion of the program.\n";
321*9880d681SAndroid Build Coastguard Worker 
322*9880d681SAndroid Build Coastguard Worker     // Bugpoint is intentionally not very trusting of LLVM transformations.  In
323*9880d681SAndroid Build Coastguard Worker     // particular, we're not going to assume that the loop extractor works, so
324*9880d681SAndroid Build Coastguard Worker     // we're going to test the newly loop extracted program to make sure nothing
325*9880d681SAndroid Build Coastguard Worker     // has broken.  If something broke, then we'll inform the user and stop
326*9880d681SAndroid Build Coastguard Worker     // extraction.
327*9880d681SAndroid Build Coastguard Worker     AbstractInterpreter *AI = BD.switchToSafeInterpreter();
328*9880d681SAndroid Build Coastguard Worker     bool Failure;
329*9880d681SAndroid Build Coastguard Worker     std::unique_ptr<Module> New =
330*9880d681SAndroid Build Coastguard Worker         testMergedProgram(BD, std::move(ToOptimizeLoopExtracted),
331*9880d681SAndroid Build Coastguard Worker                           std::move(ToNotOptimize), Error, Failure);
332*9880d681SAndroid Build Coastguard Worker     if (!New)
333*9880d681SAndroid Build Coastguard Worker       return false;
334*9880d681SAndroid Build Coastguard Worker 
335*9880d681SAndroid Build Coastguard Worker     // Delete the original and set the new program.
336*9880d681SAndroid Build Coastguard Worker     Module *Old = BD.swapProgramIn(New.release());
337*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
338*9880d681SAndroid Build Coastguard Worker       MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]);
339*9880d681SAndroid Build Coastguard Worker     delete Old;
340*9880d681SAndroid Build Coastguard Worker 
341*9880d681SAndroid Build Coastguard Worker     if (Failure) {
342*9880d681SAndroid Build Coastguard Worker       BD.switchToInterpreter(AI);
343*9880d681SAndroid Build Coastguard Worker 
344*9880d681SAndroid Build Coastguard Worker       // Merged program doesn't work anymore!
345*9880d681SAndroid Build Coastguard Worker       errs() << "  *** ERROR: Loop extraction broke the program. :("
346*9880d681SAndroid Build Coastguard Worker              << " Please report a bug!\n";
347*9880d681SAndroid Build Coastguard Worker       errs() << "      Continuing on with un-loop-extracted version.\n";
348*9880d681SAndroid Build Coastguard Worker 
349*9880d681SAndroid Build Coastguard Worker       BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc",
350*9880d681SAndroid Build Coastguard Worker                             ToNotOptimize.get());
351*9880d681SAndroid Build Coastguard Worker       BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc",
352*9880d681SAndroid Build Coastguard Worker                             ToOptimize);
353*9880d681SAndroid Build Coastguard Worker       BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc",
354*9880d681SAndroid Build Coastguard Worker                             ToOptimizeLoopExtracted.get());
355*9880d681SAndroid Build Coastguard Worker 
356*9880d681SAndroid Build Coastguard Worker       errs() << "Please submit the "
357*9880d681SAndroid Build Coastguard Worker              << OutputPrefix << "-loop-extract-fail-*.bc files.\n";
358*9880d681SAndroid Build Coastguard Worker       delete ToOptimize;
359*9880d681SAndroid Build Coastguard Worker       return MadeChange;
360*9880d681SAndroid Build Coastguard Worker     }
361*9880d681SAndroid Build Coastguard Worker     delete ToOptimize;
362*9880d681SAndroid Build Coastguard Worker     BD.switchToInterpreter(AI);
363*9880d681SAndroid Build Coastguard Worker 
364*9880d681SAndroid Build Coastguard Worker     outs() << "  Testing after loop extraction:\n";
365*9880d681SAndroid Build Coastguard Worker     // Clone modules, the tester function will free them.
366*9880d681SAndroid Build Coastguard Worker     std::unique_ptr<Module> TOLEBackup =
367*9880d681SAndroid Build Coastguard Worker         CloneModule(ToOptimizeLoopExtracted.get(), VMap);
368*9880d681SAndroid Build Coastguard Worker     std::unique_ptr<Module> TNOBackup = CloneModule(ToNotOptimize.get(), VMap);
369*9880d681SAndroid Build Coastguard Worker 
370*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
371*9880d681SAndroid Build Coastguard Worker       MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]);
372*9880d681SAndroid Build Coastguard Worker 
373*9880d681SAndroid Build Coastguard Worker     Failure = TestFn(BD, std::move(ToOptimizeLoopExtracted),
374*9880d681SAndroid Build Coastguard Worker                      std::move(ToNotOptimize), Error);
375*9880d681SAndroid Build Coastguard Worker     if (!Error.empty())
376*9880d681SAndroid Build Coastguard Worker       return false;
377*9880d681SAndroid Build Coastguard Worker 
378*9880d681SAndroid Build Coastguard Worker     ToOptimizeLoopExtracted = std::move(TOLEBackup);
379*9880d681SAndroid Build Coastguard Worker     ToNotOptimize = std::move(TNOBackup);
380*9880d681SAndroid Build Coastguard Worker 
381*9880d681SAndroid Build Coastguard Worker     if (!Failure) {
382*9880d681SAndroid Build Coastguard Worker       outs() << "*** Loop extraction masked the problem.  Undoing.\n";
383*9880d681SAndroid Build Coastguard Worker       // If the program is not still broken, then loop extraction did something
384*9880d681SAndroid Build Coastguard Worker       // that masked the error.  Stop loop extraction now.
385*9880d681SAndroid Build Coastguard Worker 
386*9880d681SAndroid Build Coastguard Worker       std::vector<std::pair<std::string, FunctionType*> > MisCompFunctions;
387*9880d681SAndroid Build Coastguard Worker       for (Function *F : MiscompiledFunctions) {
388*9880d681SAndroid Build Coastguard Worker         MisCompFunctions.emplace_back(F->getName(), F->getFunctionType());
389*9880d681SAndroid Build Coastguard Worker       }
390*9880d681SAndroid Build Coastguard Worker 
391*9880d681SAndroid Build Coastguard Worker       if (Linker::linkModules(*ToNotOptimize,
392*9880d681SAndroid Build Coastguard Worker                               std::move(ToOptimizeLoopExtracted)))
393*9880d681SAndroid Build Coastguard Worker         exit(1);
394*9880d681SAndroid Build Coastguard Worker 
395*9880d681SAndroid Build Coastguard Worker       MiscompiledFunctions.clear();
396*9880d681SAndroid Build Coastguard Worker       for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
397*9880d681SAndroid Build Coastguard Worker         Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);
398*9880d681SAndroid Build Coastguard Worker 
399*9880d681SAndroid Build Coastguard Worker         assert(NewF && "Function not found??");
400*9880d681SAndroid Build Coastguard Worker         MiscompiledFunctions.push_back(NewF);
401*9880d681SAndroid Build Coastguard Worker       }
402*9880d681SAndroid Build Coastguard Worker 
403*9880d681SAndroid Build Coastguard Worker       BD.setNewProgram(ToNotOptimize.release());
404*9880d681SAndroid Build Coastguard Worker       return MadeChange;
405*9880d681SAndroid Build Coastguard Worker     }
406*9880d681SAndroid Build Coastguard Worker 
407*9880d681SAndroid Build Coastguard Worker     outs() << "*** Loop extraction successful!\n";
408*9880d681SAndroid Build Coastguard Worker 
409*9880d681SAndroid Build Coastguard Worker     std::vector<std::pair<std::string, FunctionType*> > MisCompFunctions;
410*9880d681SAndroid Build Coastguard Worker     for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
411*9880d681SAndroid Build Coastguard Worker            E = ToOptimizeLoopExtracted->end(); I != E; ++I)
412*9880d681SAndroid Build Coastguard Worker       if (!I->isDeclaration())
413*9880d681SAndroid Build Coastguard Worker         MisCompFunctions.emplace_back(I->getName(), I->getFunctionType());
414*9880d681SAndroid Build Coastguard Worker 
415*9880d681SAndroid Build Coastguard Worker     // Okay, great!  Now we know that we extracted a loop and that loop
416*9880d681SAndroid Build Coastguard Worker     // extraction both didn't break the program, and didn't mask the problem.
417*9880d681SAndroid Build Coastguard Worker     // Replace the current program with the loop extracted version, and try to
418*9880d681SAndroid Build Coastguard Worker     // extract another loop.
419*9880d681SAndroid Build Coastguard Worker     if (Linker::linkModules(*ToNotOptimize, std::move(ToOptimizeLoopExtracted)))
420*9880d681SAndroid Build Coastguard Worker       exit(1);
421*9880d681SAndroid Build Coastguard Worker 
422*9880d681SAndroid Build Coastguard Worker     // All of the Function*'s in the MiscompiledFunctions list are in the old
423*9880d681SAndroid Build Coastguard Worker     // module.  Update this list to include all of the functions in the
424*9880d681SAndroid Build Coastguard Worker     // optimized and loop extracted module.
425*9880d681SAndroid Build Coastguard Worker     MiscompiledFunctions.clear();
426*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
427*9880d681SAndroid Build Coastguard Worker       Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);
428*9880d681SAndroid Build Coastguard Worker 
429*9880d681SAndroid Build Coastguard Worker       assert(NewF && "Function not found??");
430*9880d681SAndroid Build Coastguard Worker       MiscompiledFunctions.push_back(NewF);
431*9880d681SAndroid Build Coastguard Worker     }
432*9880d681SAndroid Build Coastguard Worker 
433*9880d681SAndroid Build Coastguard Worker     BD.setNewProgram(ToNotOptimize.release());
434*9880d681SAndroid Build Coastguard Worker     MadeChange = true;
435*9880d681SAndroid Build Coastguard Worker   }
436*9880d681SAndroid Build Coastguard Worker }
437*9880d681SAndroid Build Coastguard Worker 
438*9880d681SAndroid Build Coastguard Worker namespace {
439*9880d681SAndroid Build Coastguard Worker   class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
440*9880d681SAndroid Build Coastguard Worker     BugDriver &BD;
441*9880d681SAndroid Build Coastguard Worker     bool (*TestFn)(BugDriver &, std::unique_ptr<Module>,
442*9880d681SAndroid Build Coastguard Worker                    std::unique_ptr<Module>, std::string &);
443*9880d681SAndroid Build Coastguard Worker     std::vector<Function*> FunctionsBeingTested;
444*9880d681SAndroid Build Coastguard Worker   public:
ReduceMiscompiledBlocks(BugDriver & bd,bool (* F)(BugDriver &,std::unique_ptr<Module>,std::unique_ptr<Module>,std::string &),const std::vector<Function * > & Fns)445*9880d681SAndroid Build Coastguard Worker     ReduceMiscompiledBlocks(BugDriver &bd,
446*9880d681SAndroid Build Coastguard Worker                             bool (*F)(BugDriver &, std::unique_ptr<Module>,
447*9880d681SAndroid Build Coastguard Worker                                       std::unique_ptr<Module>, std::string &),
448*9880d681SAndroid Build Coastguard Worker                             const std::vector<Function *> &Fns)
449*9880d681SAndroid Build Coastguard Worker         : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
450*9880d681SAndroid Build Coastguard Worker 
doTest(std::vector<BasicBlock * > & Prefix,std::vector<BasicBlock * > & Suffix,std::string & Error)451*9880d681SAndroid Build Coastguard Worker     TestResult doTest(std::vector<BasicBlock*> &Prefix,
452*9880d681SAndroid Build Coastguard Worker                       std::vector<BasicBlock*> &Suffix,
453*9880d681SAndroid Build Coastguard Worker                       std::string &Error) override {
454*9880d681SAndroid Build Coastguard Worker       if (!Suffix.empty()) {
455*9880d681SAndroid Build Coastguard Worker         bool Ret = TestFuncs(Suffix, Error);
456*9880d681SAndroid Build Coastguard Worker         if (!Error.empty())
457*9880d681SAndroid Build Coastguard Worker           return InternalError;
458*9880d681SAndroid Build Coastguard Worker         if (Ret)
459*9880d681SAndroid Build Coastguard Worker           return KeepSuffix;
460*9880d681SAndroid Build Coastguard Worker       }
461*9880d681SAndroid Build Coastguard Worker       if (!Prefix.empty()) {
462*9880d681SAndroid Build Coastguard Worker         bool Ret = TestFuncs(Prefix, Error);
463*9880d681SAndroid Build Coastguard Worker         if (!Error.empty())
464*9880d681SAndroid Build Coastguard Worker           return InternalError;
465*9880d681SAndroid Build Coastguard Worker         if (Ret)
466*9880d681SAndroid Build Coastguard Worker           return KeepPrefix;
467*9880d681SAndroid Build Coastguard Worker       }
468*9880d681SAndroid Build Coastguard Worker       return NoFailure;
469*9880d681SAndroid Build Coastguard Worker     }
470*9880d681SAndroid Build Coastguard Worker 
471*9880d681SAndroid Build Coastguard Worker     bool TestFuncs(const std::vector<BasicBlock*> &BBs, std::string &Error);
472*9880d681SAndroid Build Coastguard Worker   };
473*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
474*9880d681SAndroid Build Coastguard Worker 
475*9880d681SAndroid Build Coastguard Worker /// TestFuncs - Extract all blocks for the miscompiled functions except for the
476*9880d681SAndroid Build Coastguard Worker /// specified blocks.  If the problem still exists, return true.
477*9880d681SAndroid Build Coastguard Worker ///
TestFuncs(const std::vector<BasicBlock * > & BBs,std::string & Error)478*9880d681SAndroid Build Coastguard Worker bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs,
479*9880d681SAndroid Build Coastguard Worker                                         std::string &Error) {
480*9880d681SAndroid Build Coastguard Worker   // Test to see if the function is misoptimized if we ONLY run it on the
481*9880d681SAndroid Build Coastguard Worker   // functions listed in Funcs.
482*9880d681SAndroid Build Coastguard Worker   outs() << "Checking to see if the program is misoptimized when all ";
483*9880d681SAndroid Build Coastguard Worker   if (!BBs.empty()) {
484*9880d681SAndroid Build Coastguard Worker     outs() << "but these " << BBs.size() << " blocks are extracted: ";
485*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
486*9880d681SAndroid Build Coastguard Worker       outs() << BBs[i]->getName() << " ";
487*9880d681SAndroid Build Coastguard Worker     if (BBs.size() > 10) outs() << "...";
488*9880d681SAndroid Build Coastguard Worker   } else {
489*9880d681SAndroid Build Coastguard Worker     outs() << "blocks are extracted.";
490*9880d681SAndroid Build Coastguard Worker   }
491*9880d681SAndroid Build Coastguard Worker   outs() << '\n';
492*9880d681SAndroid Build Coastguard Worker 
493*9880d681SAndroid Build Coastguard Worker   // Split the module into the two halves of the program we want.
494*9880d681SAndroid Build Coastguard Worker   ValueToValueMapTy VMap;
495*9880d681SAndroid Build Coastguard Worker   Module *Clone = CloneModule(BD.getProgram(), VMap).release();
496*9880d681SAndroid Build Coastguard Worker   Module *Orig = BD.swapProgramIn(Clone);
497*9880d681SAndroid Build Coastguard Worker   std::vector<Function*> FuncsOnClone;
498*9880d681SAndroid Build Coastguard Worker   std::vector<BasicBlock*> BBsOnClone;
499*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = FunctionsBeingTested.size(); i != e; ++i) {
500*9880d681SAndroid Build Coastguard Worker     Function *F = cast<Function>(VMap[FunctionsBeingTested[i]]);
501*9880d681SAndroid Build Coastguard Worker     FuncsOnClone.push_back(F);
502*9880d681SAndroid Build Coastguard Worker   }
503*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
504*9880d681SAndroid Build Coastguard Worker     BasicBlock *BB = cast<BasicBlock>(VMap[BBs[i]]);
505*9880d681SAndroid Build Coastguard Worker     BBsOnClone.push_back(BB);
506*9880d681SAndroid Build Coastguard Worker   }
507*9880d681SAndroid Build Coastguard Worker   VMap.clear();
508*9880d681SAndroid Build Coastguard Worker 
509*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);
510*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> ToOptimize =
511*9880d681SAndroid Build Coastguard Worker       SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap);
512*9880d681SAndroid Build Coastguard Worker 
513*9880d681SAndroid Build Coastguard Worker   // Try the extraction.  If it doesn't work, then the block extractor crashed
514*9880d681SAndroid Build Coastguard Worker   // or something, in which case bugpoint can't chase down this possibility.
515*9880d681SAndroid Build Coastguard Worker   if (std::unique_ptr<Module> New =
516*9880d681SAndroid Build Coastguard Worker           BD.extractMappedBlocksFromModule(BBsOnClone, ToOptimize.get())) {
517*9880d681SAndroid Build Coastguard Worker     bool Ret = TestFn(BD, std::move(New), std::move(ToNotOptimize), Error);
518*9880d681SAndroid Build Coastguard Worker     delete BD.swapProgramIn(Orig);
519*9880d681SAndroid Build Coastguard Worker     return Ret;
520*9880d681SAndroid Build Coastguard Worker   }
521*9880d681SAndroid Build Coastguard Worker   delete BD.swapProgramIn(Orig);
522*9880d681SAndroid Build Coastguard Worker   return false;
523*9880d681SAndroid Build Coastguard Worker }
524*9880d681SAndroid Build Coastguard Worker 
525*9880d681SAndroid Build Coastguard Worker /// Given a reduced list of functions that still expose the bug, extract as many
526*9880d681SAndroid Build Coastguard Worker /// basic blocks from the region as possible without obscuring the bug.
527*9880d681SAndroid Build Coastguard Worker ///
ExtractBlocks(BugDriver & BD,bool (* TestFn)(BugDriver &,std::unique_ptr<Module>,std::unique_ptr<Module>,std::string &),std::vector<Function * > & MiscompiledFunctions,std::string & Error)528*9880d681SAndroid Build Coastguard Worker static bool ExtractBlocks(BugDriver &BD,
529*9880d681SAndroid Build Coastguard Worker                           bool (*TestFn)(BugDriver &, std::unique_ptr<Module>,
530*9880d681SAndroid Build Coastguard Worker                                          std::unique_ptr<Module>,
531*9880d681SAndroid Build Coastguard Worker                                          std::string &),
532*9880d681SAndroid Build Coastguard Worker                           std::vector<Function *> &MiscompiledFunctions,
533*9880d681SAndroid Build Coastguard Worker                           std::string &Error) {
534*9880d681SAndroid Build Coastguard Worker   if (BugpointIsInterrupted) return false;
535*9880d681SAndroid Build Coastguard Worker 
536*9880d681SAndroid Build Coastguard Worker   std::vector<BasicBlock*> Blocks;
537*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
538*9880d681SAndroid Build Coastguard Worker     for (BasicBlock &BB : *MiscompiledFunctions[i])
539*9880d681SAndroid Build Coastguard Worker       Blocks.push_back(&BB);
540*9880d681SAndroid Build Coastguard Worker 
541*9880d681SAndroid Build Coastguard Worker   // Use the list reducer to identify blocks that can be extracted without
542*9880d681SAndroid Build Coastguard Worker   // obscuring the bug.  The Blocks list will end up containing blocks that must
543*9880d681SAndroid Build Coastguard Worker   // be retained from the original program.
544*9880d681SAndroid Build Coastguard Worker   unsigned OldSize = Blocks.size();
545*9880d681SAndroid Build Coastguard Worker 
546*9880d681SAndroid Build Coastguard Worker   // Check to see if all blocks are extractible first.
547*9880d681SAndroid Build Coastguard Worker   bool Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions)
548*9880d681SAndroid Build Coastguard Worker                                   .TestFuncs(std::vector<BasicBlock*>(), Error);
549*9880d681SAndroid Build Coastguard Worker   if (!Error.empty())
550*9880d681SAndroid Build Coastguard Worker     return false;
551*9880d681SAndroid Build Coastguard Worker   if (Ret) {
552*9880d681SAndroid Build Coastguard Worker     Blocks.clear();
553*9880d681SAndroid Build Coastguard Worker   } else {
554*9880d681SAndroid Build Coastguard Worker     ReduceMiscompiledBlocks(BD, TestFn,
555*9880d681SAndroid Build Coastguard Worker                             MiscompiledFunctions).reduceList(Blocks, Error);
556*9880d681SAndroid Build Coastguard Worker     if (!Error.empty())
557*9880d681SAndroid Build Coastguard Worker       return false;
558*9880d681SAndroid Build Coastguard Worker     if (Blocks.size() == OldSize)
559*9880d681SAndroid Build Coastguard Worker       return false;
560*9880d681SAndroid Build Coastguard Worker   }
561*9880d681SAndroid Build Coastguard Worker 
562*9880d681SAndroid Build Coastguard Worker   ValueToValueMapTy VMap;
563*9880d681SAndroid Build Coastguard Worker   Module *ProgClone = CloneModule(BD.getProgram(), VMap).release();
564*9880d681SAndroid Build Coastguard Worker   Module *ToExtract =
565*9880d681SAndroid Build Coastguard Worker       SplitFunctionsOutOfModule(ProgClone, MiscompiledFunctions, VMap)
566*9880d681SAndroid Build Coastguard Worker           .release();
567*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> Extracted =
568*9880d681SAndroid Build Coastguard Worker       BD.extractMappedBlocksFromModule(Blocks, ToExtract);
569*9880d681SAndroid Build Coastguard Worker   if (!Extracted) {
570*9880d681SAndroid Build Coastguard Worker     // Weird, extraction should have worked.
571*9880d681SAndroid Build Coastguard Worker     errs() << "Nondeterministic problem extracting blocks??\n";
572*9880d681SAndroid Build Coastguard Worker     delete ProgClone;
573*9880d681SAndroid Build Coastguard Worker     delete ToExtract;
574*9880d681SAndroid Build Coastguard Worker     return false;
575*9880d681SAndroid Build Coastguard Worker   }
576*9880d681SAndroid Build Coastguard Worker 
577*9880d681SAndroid Build Coastguard Worker   // Otherwise, block extraction succeeded.  Link the two program fragments back
578*9880d681SAndroid Build Coastguard Worker   // together.
579*9880d681SAndroid Build Coastguard Worker   delete ToExtract;
580*9880d681SAndroid Build Coastguard Worker 
581*9880d681SAndroid Build Coastguard Worker   std::vector<std::pair<std::string, FunctionType*> > MisCompFunctions;
582*9880d681SAndroid Build Coastguard Worker   for (Module::iterator I = Extracted->begin(), E = Extracted->end();
583*9880d681SAndroid Build Coastguard Worker        I != E; ++I)
584*9880d681SAndroid Build Coastguard Worker     if (!I->isDeclaration())
585*9880d681SAndroid Build Coastguard Worker       MisCompFunctions.emplace_back(I->getName(), I->getFunctionType());
586*9880d681SAndroid Build Coastguard Worker 
587*9880d681SAndroid Build Coastguard Worker   if (Linker::linkModules(*ProgClone, std::move(Extracted)))
588*9880d681SAndroid Build Coastguard Worker     exit(1);
589*9880d681SAndroid Build Coastguard Worker 
590*9880d681SAndroid Build Coastguard Worker   // Set the new program and delete the old one.
591*9880d681SAndroid Build Coastguard Worker   BD.setNewProgram(ProgClone);
592*9880d681SAndroid Build Coastguard Worker 
593*9880d681SAndroid Build Coastguard Worker   // Update the list of miscompiled functions.
594*9880d681SAndroid Build Coastguard Worker   MiscompiledFunctions.clear();
595*9880d681SAndroid Build Coastguard Worker 
596*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
597*9880d681SAndroid Build Coastguard Worker     Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first);
598*9880d681SAndroid Build Coastguard Worker     assert(NewF && "Function not found??");
599*9880d681SAndroid Build Coastguard Worker     MiscompiledFunctions.push_back(NewF);
600*9880d681SAndroid Build Coastguard Worker   }
601*9880d681SAndroid Build Coastguard Worker 
602*9880d681SAndroid Build Coastguard Worker   return true;
603*9880d681SAndroid Build Coastguard Worker }
604*9880d681SAndroid Build Coastguard Worker 
605*9880d681SAndroid Build Coastguard Worker /// This is a generic driver to narrow down miscompilations, either in an
606*9880d681SAndroid Build Coastguard Worker /// optimization or a code generator.
607*9880d681SAndroid Build Coastguard Worker ///
608*9880d681SAndroid Build Coastguard Worker static std::vector<Function *>
DebugAMiscompilation(BugDriver & BD,bool (* TestFn)(BugDriver &,std::unique_ptr<Module>,std::unique_ptr<Module>,std::string &),std::string & Error)609*9880d681SAndroid Build Coastguard Worker DebugAMiscompilation(BugDriver &BD,
610*9880d681SAndroid Build Coastguard Worker                      bool (*TestFn)(BugDriver &, std::unique_ptr<Module>,
611*9880d681SAndroid Build Coastguard Worker                                     std::unique_ptr<Module>, std::string &),
612*9880d681SAndroid Build Coastguard Worker                      std::string &Error) {
613*9880d681SAndroid Build Coastguard Worker   // Okay, now that we have reduced the list of passes which are causing the
614*9880d681SAndroid Build Coastguard Worker   // failure, see if we can pin down which functions are being
615*9880d681SAndroid Build Coastguard Worker   // miscompiled... first build a list of all of the non-external functions in
616*9880d681SAndroid Build Coastguard Worker   // the program.
617*9880d681SAndroid Build Coastguard Worker   std::vector<Function*> MiscompiledFunctions;
618*9880d681SAndroid Build Coastguard Worker   Module *Prog = BD.getProgram();
619*9880d681SAndroid Build Coastguard Worker   for (Function &F : *Prog)
620*9880d681SAndroid Build Coastguard Worker     if (!F.isDeclaration())
621*9880d681SAndroid Build Coastguard Worker       MiscompiledFunctions.push_back(&F);
622*9880d681SAndroid Build Coastguard Worker 
623*9880d681SAndroid Build Coastguard Worker   // Do the reduction...
624*9880d681SAndroid Build Coastguard Worker   if (!BugpointIsInterrupted)
625*9880d681SAndroid Build Coastguard Worker     ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
626*9880d681SAndroid Build Coastguard Worker                                                        Error);
627*9880d681SAndroid Build Coastguard Worker   if (!Error.empty()) {
628*9880d681SAndroid Build Coastguard Worker     errs() << "\n***Cannot reduce functions: ";
629*9880d681SAndroid Build Coastguard Worker     return MiscompiledFunctions;
630*9880d681SAndroid Build Coastguard Worker   }
631*9880d681SAndroid Build Coastguard Worker   outs() << "\n*** The following function"
632*9880d681SAndroid Build Coastguard Worker          << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
633*9880d681SAndroid Build Coastguard Worker          << " being miscompiled: ";
634*9880d681SAndroid Build Coastguard Worker   PrintFunctionList(MiscompiledFunctions);
635*9880d681SAndroid Build Coastguard Worker   outs() << '\n';
636*9880d681SAndroid Build Coastguard Worker 
637*9880d681SAndroid Build Coastguard Worker   // See if we can rip any loops out of the miscompiled functions and still
638*9880d681SAndroid Build Coastguard Worker   // trigger the problem.
639*9880d681SAndroid Build Coastguard Worker 
640*9880d681SAndroid Build Coastguard Worker   if (!BugpointIsInterrupted && !DisableLoopExtraction) {
641*9880d681SAndroid Build Coastguard Worker     bool Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions, Error);
642*9880d681SAndroid Build Coastguard Worker     if (!Error.empty())
643*9880d681SAndroid Build Coastguard Worker       return MiscompiledFunctions;
644*9880d681SAndroid Build Coastguard Worker     if (Ret) {
645*9880d681SAndroid Build Coastguard Worker       // Okay, we extracted some loops and the problem still appears.  See if
646*9880d681SAndroid Build Coastguard Worker       // we can eliminate some of the created functions from being candidates.
647*9880d681SAndroid Build Coastguard Worker       DisambiguateGlobalSymbols(BD.getProgram());
648*9880d681SAndroid Build Coastguard Worker 
649*9880d681SAndroid Build Coastguard Worker       // Do the reduction...
650*9880d681SAndroid Build Coastguard Worker       if (!BugpointIsInterrupted)
651*9880d681SAndroid Build Coastguard Worker         ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
652*9880d681SAndroid Build Coastguard Worker                                                            Error);
653*9880d681SAndroid Build Coastguard Worker       if (!Error.empty())
654*9880d681SAndroid Build Coastguard Worker         return MiscompiledFunctions;
655*9880d681SAndroid Build Coastguard Worker 
656*9880d681SAndroid Build Coastguard Worker       outs() << "\n*** The following function"
657*9880d681SAndroid Build Coastguard Worker              << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
658*9880d681SAndroid Build Coastguard Worker              << " being miscompiled: ";
659*9880d681SAndroid Build Coastguard Worker       PrintFunctionList(MiscompiledFunctions);
660*9880d681SAndroid Build Coastguard Worker       outs() << '\n';
661*9880d681SAndroid Build Coastguard Worker     }
662*9880d681SAndroid Build Coastguard Worker   }
663*9880d681SAndroid Build Coastguard Worker 
664*9880d681SAndroid Build Coastguard Worker   if (!BugpointIsInterrupted && !DisableBlockExtraction) {
665*9880d681SAndroid Build Coastguard Worker     bool Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions, Error);
666*9880d681SAndroid Build Coastguard Worker     if (!Error.empty())
667*9880d681SAndroid Build Coastguard Worker       return MiscompiledFunctions;
668*9880d681SAndroid Build Coastguard Worker     if (Ret) {
669*9880d681SAndroid Build Coastguard Worker       // Okay, we extracted some blocks and the problem still appears.  See if
670*9880d681SAndroid Build Coastguard Worker       // we can eliminate some of the created functions from being candidates.
671*9880d681SAndroid Build Coastguard Worker       DisambiguateGlobalSymbols(BD.getProgram());
672*9880d681SAndroid Build Coastguard Worker 
673*9880d681SAndroid Build Coastguard Worker       // Do the reduction...
674*9880d681SAndroid Build Coastguard Worker       ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
675*9880d681SAndroid Build Coastguard Worker                                                          Error);
676*9880d681SAndroid Build Coastguard Worker       if (!Error.empty())
677*9880d681SAndroid Build Coastguard Worker         return MiscompiledFunctions;
678*9880d681SAndroid Build Coastguard Worker 
679*9880d681SAndroid Build Coastguard Worker       outs() << "\n*** The following function"
680*9880d681SAndroid Build Coastguard Worker              << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
681*9880d681SAndroid Build Coastguard Worker              << " being miscompiled: ";
682*9880d681SAndroid Build Coastguard Worker       PrintFunctionList(MiscompiledFunctions);
683*9880d681SAndroid Build Coastguard Worker       outs() << '\n';
684*9880d681SAndroid Build Coastguard Worker     }
685*9880d681SAndroid Build Coastguard Worker   }
686*9880d681SAndroid Build Coastguard Worker 
687*9880d681SAndroid Build Coastguard Worker   return MiscompiledFunctions;
688*9880d681SAndroid Build Coastguard Worker }
689*9880d681SAndroid Build Coastguard Worker 
690*9880d681SAndroid Build Coastguard Worker /// This is the predicate function used to check to see if the "Test" portion of
691*9880d681SAndroid Build Coastguard Worker /// the program is misoptimized.  If so, return true.  In any case, both module
692*9880d681SAndroid Build Coastguard Worker /// arguments are deleted.
693*9880d681SAndroid Build Coastguard Worker ///
TestOptimizer(BugDriver & BD,std::unique_ptr<Module> Test,std::unique_ptr<Module> Safe,std::string & Error)694*9880d681SAndroid Build Coastguard Worker static bool TestOptimizer(BugDriver &BD, std::unique_ptr<Module> Test,
695*9880d681SAndroid Build Coastguard Worker                           std::unique_ptr<Module> Safe, std::string &Error) {
696*9880d681SAndroid Build Coastguard Worker   // Run the optimization passes on ToOptimize, producing a transformed version
697*9880d681SAndroid Build Coastguard Worker   // of the functions being tested.
698*9880d681SAndroid Build Coastguard Worker   outs() << "  Optimizing functions being tested: ";
699*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> Optimized =
700*9880d681SAndroid Build Coastguard Worker       BD.runPassesOn(Test.get(), BD.getPassesToRun());
701*9880d681SAndroid Build Coastguard Worker   if (!Optimized) {
702*9880d681SAndroid Build Coastguard Worker     errs() << " Error running this sequence of passes"
703*9880d681SAndroid Build Coastguard Worker            << " on the input program!\n";
704*9880d681SAndroid Build Coastguard Worker     delete BD.swapProgramIn(Test.get());
705*9880d681SAndroid Build Coastguard Worker     BD.EmitProgressBitcode(Test.get(), "pass-error",  false);
706*9880d681SAndroid Build Coastguard Worker     return BD.debugOptimizerCrash();
707*9880d681SAndroid Build Coastguard Worker   }
708*9880d681SAndroid Build Coastguard Worker   outs() << "done.\n";
709*9880d681SAndroid Build Coastguard Worker 
710*9880d681SAndroid Build Coastguard Worker   outs() << "  Checking to see if the merged program executes correctly: ";
711*9880d681SAndroid Build Coastguard Worker   bool Broken;
712*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> New = testMergedProgram(
713*9880d681SAndroid Build Coastguard Worker       BD, std::move(Optimized), std::move(Safe), Error, Broken);
714*9880d681SAndroid Build Coastguard Worker   if (New) {
715*9880d681SAndroid Build Coastguard Worker     outs() << (Broken ? " nope.\n" : " yup.\n");
716*9880d681SAndroid Build Coastguard Worker     // Delete the original and set the new program.
717*9880d681SAndroid Build Coastguard Worker     delete BD.swapProgramIn(New.release());
718*9880d681SAndroid Build Coastguard Worker   }
719*9880d681SAndroid Build Coastguard Worker   return Broken;
720*9880d681SAndroid Build Coastguard Worker }
721*9880d681SAndroid Build Coastguard Worker 
722*9880d681SAndroid Build Coastguard Worker /// debugMiscompilation - This method is used when the passes selected are not
723*9880d681SAndroid Build Coastguard Worker /// crashing, but the generated output is semantically different from the
724*9880d681SAndroid Build Coastguard Worker /// input.
725*9880d681SAndroid Build Coastguard Worker ///
debugMiscompilation(std::string * Error)726*9880d681SAndroid Build Coastguard Worker void BugDriver::debugMiscompilation(std::string *Error) {
727*9880d681SAndroid Build Coastguard Worker   // Make sure something was miscompiled...
728*9880d681SAndroid Build Coastguard Worker   if (!BugpointIsInterrupted)
729*9880d681SAndroid Build Coastguard Worker     if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun, *Error)) {
730*9880d681SAndroid Build Coastguard Worker       if (Error->empty())
731*9880d681SAndroid Build Coastguard Worker         errs() << "*** Optimized program matches reference output!  No problem"
732*9880d681SAndroid Build Coastguard Worker                << " detected...\nbugpoint can't help you with your problem!\n";
733*9880d681SAndroid Build Coastguard Worker       return;
734*9880d681SAndroid Build Coastguard Worker     }
735*9880d681SAndroid Build Coastguard Worker 
736*9880d681SAndroid Build Coastguard Worker   outs() << "\n*** Found miscompiling pass"
737*9880d681SAndroid Build Coastguard Worker          << (getPassesToRun().size() == 1 ? "" : "es") << ": "
738*9880d681SAndroid Build Coastguard Worker          << getPassesString(getPassesToRun()) << '\n';
739*9880d681SAndroid Build Coastguard Worker   EmitProgressBitcode(Program, "passinput");
740*9880d681SAndroid Build Coastguard Worker 
741*9880d681SAndroid Build Coastguard Worker   std::vector<Function *> MiscompiledFunctions =
742*9880d681SAndroid Build Coastguard Worker     DebugAMiscompilation(*this, TestOptimizer, *Error);
743*9880d681SAndroid Build Coastguard Worker   if (!Error->empty())
744*9880d681SAndroid Build Coastguard Worker     return;
745*9880d681SAndroid Build Coastguard Worker 
746*9880d681SAndroid Build Coastguard Worker   // Output a bunch of bitcode files for the user...
747*9880d681SAndroid Build Coastguard Worker   outs() << "Outputting reduced bitcode files which expose the problem:\n";
748*9880d681SAndroid Build Coastguard Worker   ValueToValueMapTy VMap;
749*9880d681SAndroid Build Coastguard Worker   Module *ToNotOptimize = CloneModule(getProgram(), VMap).release();
750*9880d681SAndroid Build Coastguard Worker   Module *ToOptimize =
751*9880d681SAndroid Build Coastguard Worker       SplitFunctionsOutOfModule(ToNotOptimize, MiscompiledFunctions, VMap)
752*9880d681SAndroid Build Coastguard Worker           .release();
753*9880d681SAndroid Build Coastguard Worker 
754*9880d681SAndroid Build Coastguard Worker   outs() << "  Non-optimized portion: ";
755*9880d681SAndroid Build Coastguard Worker   EmitProgressBitcode(ToNotOptimize, "tonotoptimize", true);
756*9880d681SAndroid Build Coastguard Worker   delete ToNotOptimize;  // Delete hacked module.
757*9880d681SAndroid Build Coastguard Worker 
758*9880d681SAndroid Build Coastguard Worker   outs() << "  Portion that is input to optimizer: ";
759*9880d681SAndroid Build Coastguard Worker   EmitProgressBitcode(ToOptimize, "tooptimize");
760*9880d681SAndroid Build Coastguard Worker   delete ToOptimize;      // Delete hacked module.
761*9880d681SAndroid Build Coastguard Worker }
762*9880d681SAndroid Build Coastguard Worker 
763*9880d681SAndroid Build Coastguard Worker /// Get the specified modules ready for code generator testing.
764*9880d681SAndroid Build Coastguard Worker ///
CleanupAndPrepareModules(BugDriver & BD,std::unique_ptr<Module> & Test,Module * Safe)765*9880d681SAndroid Build Coastguard Worker static void CleanupAndPrepareModules(BugDriver &BD,
766*9880d681SAndroid Build Coastguard Worker                                      std::unique_ptr<Module> &Test,
767*9880d681SAndroid Build Coastguard Worker                                      Module *Safe) {
768*9880d681SAndroid Build Coastguard Worker   // Clean up the modules, removing extra cruft that we don't need anymore...
769*9880d681SAndroid Build Coastguard Worker   Test = BD.performFinalCleanups(Test.get());
770*9880d681SAndroid Build Coastguard Worker 
771*9880d681SAndroid Build Coastguard Worker   // If we are executing the JIT, we have several nasty issues to take care of.
772*9880d681SAndroid Build Coastguard Worker   if (!BD.isExecutingJIT()) return;
773*9880d681SAndroid Build Coastguard Worker 
774*9880d681SAndroid Build Coastguard Worker   // First, if the main function is in the Safe module, we must add a stub to
775*9880d681SAndroid Build Coastguard Worker   // the Test module to call into it.  Thus, we create a new function `main'
776*9880d681SAndroid Build Coastguard Worker   // which just calls the old one.
777*9880d681SAndroid Build Coastguard Worker   if (Function *oldMain = Safe->getFunction("main"))
778*9880d681SAndroid Build Coastguard Worker     if (!oldMain->isDeclaration()) {
779*9880d681SAndroid Build Coastguard Worker       // Rename it
780*9880d681SAndroid Build Coastguard Worker       oldMain->setName("llvm_bugpoint_old_main");
781*9880d681SAndroid Build Coastguard Worker       // Create a NEW `main' function with same type in the test module.
782*9880d681SAndroid Build Coastguard Worker       Function *newMain =
783*9880d681SAndroid Build Coastguard Worker           Function::Create(oldMain->getFunctionType(),
784*9880d681SAndroid Build Coastguard Worker                            GlobalValue::ExternalLinkage, "main", Test.get());
785*9880d681SAndroid Build Coastguard Worker       // Create an `oldmain' prototype in the test module, which will
786*9880d681SAndroid Build Coastguard Worker       // corresponds to the real main function in the same module.
787*9880d681SAndroid Build Coastguard Worker       Function *oldMainProto = Function::Create(oldMain->getFunctionType(),
788*9880d681SAndroid Build Coastguard Worker                                                 GlobalValue::ExternalLinkage,
789*9880d681SAndroid Build Coastguard Worker                                                 oldMain->getName(), Test.get());
790*9880d681SAndroid Build Coastguard Worker       // Set up and remember the argument list for the main function.
791*9880d681SAndroid Build Coastguard Worker       std::vector<Value*> args;
792*9880d681SAndroid Build Coastguard Worker       for (Function::arg_iterator
793*9880d681SAndroid Build Coastguard Worker              I = newMain->arg_begin(), E = newMain->arg_end(),
794*9880d681SAndroid Build Coastguard Worker              OI = oldMain->arg_begin(); I != E; ++I, ++OI) {
795*9880d681SAndroid Build Coastguard Worker         I->setName(OI->getName());    // Copy argument names from oldMain
796*9880d681SAndroid Build Coastguard Worker         args.push_back(&*I);
797*9880d681SAndroid Build Coastguard Worker       }
798*9880d681SAndroid Build Coastguard Worker 
799*9880d681SAndroid Build Coastguard Worker       // Call the old main function and return its result
800*9880d681SAndroid Build Coastguard Worker       BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain);
801*9880d681SAndroid Build Coastguard Worker       CallInst *call = CallInst::Create(oldMainProto, args, "", BB);
802*9880d681SAndroid Build Coastguard Worker 
803*9880d681SAndroid Build Coastguard Worker       // If the type of old function wasn't void, return value of call
804*9880d681SAndroid Build Coastguard Worker       ReturnInst::Create(Safe->getContext(), call, BB);
805*9880d681SAndroid Build Coastguard Worker     }
806*9880d681SAndroid Build Coastguard Worker 
807*9880d681SAndroid Build Coastguard Worker   // The second nasty issue we must deal with in the JIT is that the Safe
808*9880d681SAndroid Build Coastguard Worker   // module cannot directly reference any functions defined in the test
809*9880d681SAndroid Build Coastguard Worker   // module.  Instead, we use a JIT API call to dynamically resolve the
810*9880d681SAndroid Build Coastguard Worker   // symbol.
811*9880d681SAndroid Build Coastguard Worker 
812*9880d681SAndroid Build Coastguard Worker   // Add the resolver to the Safe module.
813*9880d681SAndroid Build Coastguard Worker   // Prototype: void *getPointerToNamedFunction(const char* Name)
814*9880d681SAndroid Build Coastguard Worker   Constant *resolverFunc =
815*9880d681SAndroid Build Coastguard Worker     Safe->getOrInsertFunction("getPointerToNamedFunction",
816*9880d681SAndroid Build Coastguard Worker                     Type::getInt8PtrTy(Safe->getContext()),
817*9880d681SAndroid Build Coastguard Worker                     Type::getInt8PtrTy(Safe->getContext()),
818*9880d681SAndroid Build Coastguard Worker                        (Type *)nullptr);
819*9880d681SAndroid Build Coastguard Worker 
820*9880d681SAndroid Build Coastguard Worker   // Use the function we just added to get addresses of functions we need.
821*9880d681SAndroid Build Coastguard Worker   for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
822*9880d681SAndroid Build Coastguard Worker     if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc &&
823*9880d681SAndroid Build Coastguard Worker         !F->isIntrinsic() /* ignore intrinsics */) {
824*9880d681SAndroid Build Coastguard Worker       Function *TestFn = Test->getFunction(F->getName());
825*9880d681SAndroid Build Coastguard Worker 
826*9880d681SAndroid Build Coastguard Worker       // Don't forward functions which are external in the test module too.
827*9880d681SAndroid Build Coastguard Worker       if (TestFn && !TestFn->isDeclaration()) {
828*9880d681SAndroid Build Coastguard Worker         // 1. Add a string constant with its name to the global file
829*9880d681SAndroid Build Coastguard Worker         Constant *InitArray =
830*9880d681SAndroid Build Coastguard Worker           ConstantDataArray::getString(F->getContext(), F->getName());
831*9880d681SAndroid Build Coastguard Worker         GlobalVariable *funcName =
832*9880d681SAndroid Build Coastguard Worker           new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
833*9880d681SAndroid Build Coastguard Worker                              GlobalValue::InternalLinkage, InitArray,
834*9880d681SAndroid Build Coastguard Worker                              F->getName() + "_name");
835*9880d681SAndroid Build Coastguard Worker 
836*9880d681SAndroid Build Coastguard Worker         // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
837*9880d681SAndroid Build Coastguard Worker         // sbyte* so it matches the signature of the resolver function.
838*9880d681SAndroid Build Coastguard Worker 
839*9880d681SAndroid Build Coastguard Worker         // GetElementPtr *funcName, ulong 0, ulong 0
840*9880d681SAndroid Build Coastguard Worker         std::vector<Constant*> GEPargs(2,
841*9880d681SAndroid Build Coastguard Worker                      Constant::getNullValue(Type::getInt32Ty(F->getContext())));
842*9880d681SAndroid Build Coastguard Worker         Value *GEP = ConstantExpr::getGetElementPtr(InitArray->getType(),
843*9880d681SAndroid Build Coastguard Worker                                                     funcName, GEPargs);
844*9880d681SAndroid Build Coastguard Worker         std::vector<Value*> ResolverArgs;
845*9880d681SAndroid Build Coastguard Worker         ResolverArgs.push_back(GEP);
846*9880d681SAndroid Build Coastguard Worker 
847*9880d681SAndroid Build Coastguard Worker         // Rewrite uses of F in global initializers, etc. to uses of a wrapper
848*9880d681SAndroid Build Coastguard Worker         // function that dynamically resolves the calls to F via our JIT API
849*9880d681SAndroid Build Coastguard Worker         if (!F->use_empty()) {
850*9880d681SAndroid Build Coastguard Worker           // Create a new global to hold the cached function pointer.
851*9880d681SAndroid Build Coastguard Worker           Constant *NullPtr = ConstantPointerNull::get(F->getType());
852*9880d681SAndroid Build Coastguard Worker           GlobalVariable *Cache =
853*9880d681SAndroid Build Coastguard Worker             new GlobalVariable(*F->getParent(), F->getType(),
854*9880d681SAndroid Build Coastguard Worker                                false, GlobalValue::InternalLinkage,
855*9880d681SAndroid Build Coastguard Worker                                NullPtr,F->getName()+".fpcache");
856*9880d681SAndroid Build Coastguard Worker 
857*9880d681SAndroid Build Coastguard Worker           // Construct a new stub function that will re-route calls to F
858*9880d681SAndroid Build Coastguard Worker           FunctionType *FuncTy = F->getFunctionType();
859*9880d681SAndroid Build Coastguard Worker           Function *FuncWrapper = Function::Create(FuncTy,
860*9880d681SAndroid Build Coastguard Worker                                                    GlobalValue::InternalLinkage,
861*9880d681SAndroid Build Coastguard Worker                                                    F->getName() + "_wrapper",
862*9880d681SAndroid Build Coastguard Worker                                                    F->getParent());
863*9880d681SAndroid Build Coastguard Worker           BasicBlock *EntryBB  = BasicBlock::Create(F->getContext(),
864*9880d681SAndroid Build Coastguard Worker                                                     "entry", FuncWrapper);
865*9880d681SAndroid Build Coastguard Worker           BasicBlock *DoCallBB = BasicBlock::Create(F->getContext(),
866*9880d681SAndroid Build Coastguard Worker                                                     "usecache", FuncWrapper);
867*9880d681SAndroid Build Coastguard Worker           BasicBlock *LookupBB = BasicBlock::Create(F->getContext(),
868*9880d681SAndroid Build Coastguard Worker                                                     "lookupfp", FuncWrapper);
869*9880d681SAndroid Build Coastguard Worker 
870*9880d681SAndroid Build Coastguard Worker           // Check to see if we already looked up the value.
871*9880d681SAndroid Build Coastguard Worker           Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB);
872*9880d681SAndroid Build Coastguard Worker           Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal,
873*9880d681SAndroid Build Coastguard Worker                                        NullPtr, "isNull");
874*9880d681SAndroid Build Coastguard Worker           BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB);
875*9880d681SAndroid Build Coastguard Worker 
876*9880d681SAndroid Build Coastguard Worker           // Resolve the call to function F via the JIT API:
877*9880d681SAndroid Build Coastguard Worker           //
878*9880d681SAndroid Build Coastguard Worker           // call resolver(GetElementPtr...)
879*9880d681SAndroid Build Coastguard Worker           CallInst *Resolver =
880*9880d681SAndroid Build Coastguard Worker             CallInst::Create(resolverFunc, ResolverArgs, "resolver", LookupBB);
881*9880d681SAndroid Build Coastguard Worker 
882*9880d681SAndroid Build Coastguard Worker           // Cast the result from the resolver to correctly-typed function.
883*9880d681SAndroid Build Coastguard Worker           CastInst *CastedResolver =
884*9880d681SAndroid Build Coastguard Worker             new BitCastInst(Resolver,
885*9880d681SAndroid Build Coastguard Worker                             PointerType::getUnqual(F->getFunctionType()),
886*9880d681SAndroid Build Coastguard Worker                             "resolverCast", LookupBB);
887*9880d681SAndroid Build Coastguard Worker 
888*9880d681SAndroid Build Coastguard Worker           // Save the value in our cache.
889*9880d681SAndroid Build Coastguard Worker           new StoreInst(CastedResolver, Cache, LookupBB);
890*9880d681SAndroid Build Coastguard Worker           BranchInst::Create(DoCallBB, LookupBB);
891*9880d681SAndroid Build Coastguard Worker 
892*9880d681SAndroid Build Coastguard Worker           PHINode *FuncPtr = PHINode::Create(NullPtr->getType(), 2,
893*9880d681SAndroid Build Coastguard Worker                                              "fp", DoCallBB);
894*9880d681SAndroid Build Coastguard Worker           FuncPtr->addIncoming(CastedResolver, LookupBB);
895*9880d681SAndroid Build Coastguard Worker           FuncPtr->addIncoming(CachedVal, EntryBB);
896*9880d681SAndroid Build Coastguard Worker 
897*9880d681SAndroid Build Coastguard Worker           // Save the argument list.
898*9880d681SAndroid Build Coastguard Worker           std::vector<Value*> Args;
899*9880d681SAndroid Build Coastguard Worker           for (Argument &A : FuncWrapper->args())
900*9880d681SAndroid Build Coastguard Worker             Args.push_back(&A);
901*9880d681SAndroid Build Coastguard Worker 
902*9880d681SAndroid Build Coastguard Worker           // Pass on the arguments to the real function, return its result
903*9880d681SAndroid Build Coastguard Worker           if (F->getReturnType()->isVoidTy()) {
904*9880d681SAndroid Build Coastguard Worker             CallInst::Create(FuncPtr, Args, "", DoCallBB);
905*9880d681SAndroid Build Coastguard Worker             ReturnInst::Create(F->getContext(), DoCallBB);
906*9880d681SAndroid Build Coastguard Worker           } else {
907*9880d681SAndroid Build Coastguard Worker             CallInst *Call = CallInst::Create(FuncPtr, Args,
908*9880d681SAndroid Build Coastguard Worker                                               "retval", DoCallBB);
909*9880d681SAndroid Build Coastguard Worker             ReturnInst::Create(F->getContext(),Call, DoCallBB);
910*9880d681SAndroid Build Coastguard Worker           }
911*9880d681SAndroid Build Coastguard Worker 
912*9880d681SAndroid Build Coastguard Worker           // Use the wrapper function instead of the old function
913*9880d681SAndroid Build Coastguard Worker           F->replaceAllUsesWith(FuncWrapper);
914*9880d681SAndroid Build Coastguard Worker         }
915*9880d681SAndroid Build Coastguard Worker       }
916*9880d681SAndroid Build Coastguard Worker     }
917*9880d681SAndroid Build Coastguard Worker   }
918*9880d681SAndroid Build Coastguard Worker 
919*9880d681SAndroid Build Coastguard Worker   if (verifyModule(*Test) || verifyModule(*Safe)) {
920*9880d681SAndroid Build Coastguard Worker     errs() << "Bugpoint has a bug, which corrupted a module!!\n";
921*9880d681SAndroid Build Coastguard Worker     abort();
922*9880d681SAndroid Build Coastguard Worker   }
923*9880d681SAndroid Build Coastguard Worker }
924*9880d681SAndroid Build Coastguard Worker 
925*9880d681SAndroid Build Coastguard Worker /// This is the predicate function used to check to see if the "Test" portion of
926*9880d681SAndroid Build Coastguard Worker /// the program is miscompiled by the code generator under test.  If so, return
927*9880d681SAndroid Build Coastguard Worker /// true.  In any case, both module arguments are deleted.
928*9880d681SAndroid Build Coastguard Worker ///
TestCodeGenerator(BugDriver & BD,std::unique_ptr<Module> Test,std::unique_ptr<Module> Safe,std::string & Error)929*9880d681SAndroid Build Coastguard Worker static bool TestCodeGenerator(BugDriver &BD, std::unique_ptr<Module> Test,
930*9880d681SAndroid Build Coastguard Worker                               std::unique_ptr<Module> Safe,
931*9880d681SAndroid Build Coastguard Worker                               std::string &Error) {
932*9880d681SAndroid Build Coastguard Worker   CleanupAndPrepareModules(BD, Test, Safe.get());
933*9880d681SAndroid Build Coastguard Worker 
934*9880d681SAndroid Build Coastguard Worker   SmallString<128> TestModuleBC;
935*9880d681SAndroid Build Coastguard Worker   int TestModuleFD;
936*9880d681SAndroid Build Coastguard Worker   std::error_code EC = sys::fs::createTemporaryFile("bugpoint.test", "bc",
937*9880d681SAndroid Build Coastguard Worker                                                     TestModuleFD, TestModuleBC);
938*9880d681SAndroid Build Coastguard Worker   if (EC) {
939*9880d681SAndroid Build Coastguard Worker     errs() << BD.getToolName() << "Error making unique filename: "
940*9880d681SAndroid Build Coastguard Worker            << EC.message() << "\n";
941*9880d681SAndroid Build Coastguard Worker     exit(1);
942*9880d681SAndroid Build Coastguard Worker   }
943*9880d681SAndroid Build Coastguard Worker   if (BD.writeProgramToFile(TestModuleBC.str(), TestModuleFD, Test.get())) {
944*9880d681SAndroid Build Coastguard Worker     errs() << "Error writing bitcode to `" << TestModuleBC.str()
945*9880d681SAndroid Build Coastguard Worker            << "'\nExiting.";
946*9880d681SAndroid Build Coastguard Worker     exit(1);
947*9880d681SAndroid Build Coastguard Worker   }
948*9880d681SAndroid Build Coastguard Worker 
949*9880d681SAndroid Build Coastguard Worker   FileRemover TestModuleBCRemover(TestModuleBC.str(), !SaveTemps);
950*9880d681SAndroid Build Coastguard Worker 
951*9880d681SAndroid Build Coastguard Worker   // Make the shared library
952*9880d681SAndroid Build Coastguard Worker   SmallString<128> SafeModuleBC;
953*9880d681SAndroid Build Coastguard Worker   int SafeModuleFD;
954*9880d681SAndroid Build Coastguard Worker   EC = sys::fs::createTemporaryFile("bugpoint.safe", "bc", SafeModuleFD,
955*9880d681SAndroid Build Coastguard Worker                                     SafeModuleBC);
956*9880d681SAndroid Build Coastguard Worker   if (EC) {
957*9880d681SAndroid Build Coastguard Worker     errs() << BD.getToolName() << "Error making unique filename: "
958*9880d681SAndroid Build Coastguard Worker            << EC.message() << "\n";
959*9880d681SAndroid Build Coastguard Worker     exit(1);
960*9880d681SAndroid Build Coastguard Worker   }
961*9880d681SAndroid Build Coastguard Worker 
962*9880d681SAndroid Build Coastguard Worker   if (BD.writeProgramToFile(SafeModuleBC.str(), SafeModuleFD, Safe.get())) {
963*9880d681SAndroid Build Coastguard Worker     errs() << "Error writing bitcode to `" << SafeModuleBC
964*9880d681SAndroid Build Coastguard Worker            << "'\nExiting.";
965*9880d681SAndroid Build Coastguard Worker     exit(1);
966*9880d681SAndroid Build Coastguard Worker   }
967*9880d681SAndroid Build Coastguard Worker 
968*9880d681SAndroid Build Coastguard Worker   FileRemover SafeModuleBCRemover(SafeModuleBC.str(), !SaveTemps);
969*9880d681SAndroid Build Coastguard Worker 
970*9880d681SAndroid Build Coastguard Worker   std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str(), Error);
971*9880d681SAndroid Build Coastguard Worker   if (!Error.empty())
972*9880d681SAndroid Build Coastguard Worker     return false;
973*9880d681SAndroid Build Coastguard Worker 
974*9880d681SAndroid Build Coastguard Worker   FileRemover SharedObjectRemover(SharedObject, !SaveTemps);
975*9880d681SAndroid Build Coastguard Worker 
976*9880d681SAndroid Build Coastguard Worker   // Run the code generator on the `Test' code, loading the shared library.
977*9880d681SAndroid Build Coastguard Worker   // The function returns whether or not the new output differs from reference.
978*9880d681SAndroid Build Coastguard Worker   bool Result = BD.diffProgram(BD.getProgram(), TestModuleBC.str(),
979*9880d681SAndroid Build Coastguard Worker                                SharedObject, false, &Error);
980*9880d681SAndroid Build Coastguard Worker   if (!Error.empty())
981*9880d681SAndroid Build Coastguard Worker     return false;
982*9880d681SAndroid Build Coastguard Worker 
983*9880d681SAndroid Build Coastguard Worker   if (Result)
984*9880d681SAndroid Build Coastguard Worker     errs() << ": still failing!\n";
985*9880d681SAndroid Build Coastguard Worker   else
986*9880d681SAndroid Build Coastguard Worker     errs() << ": didn't fail.\n";
987*9880d681SAndroid Build Coastguard Worker 
988*9880d681SAndroid Build Coastguard Worker   return Result;
989*9880d681SAndroid Build Coastguard Worker }
990*9880d681SAndroid Build Coastguard Worker 
991*9880d681SAndroid Build Coastguard Worker /// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
992*9880d681SAndroid Build Coastguard Worker ///
debugCodeGenerator(std::string * Error)993*9880d681SAndroid Build Coastguard Worker bool BugDriver::debugCodeGenerator(std::string *Error) {
994*9880d681SAndroid Build Coastguard Worker   if ((void*)SafeInterpreter == (void*)Interpreter) {
995*9880d681SAndroid Build Coastguard Worker     std::string Result = executeProgramSafely(Program, "bugpoint.safe.out",
996*9880d681SAndroid Build Coastguard Worker                                               Error);
997*9880d681SAndroid Build Coastguard Worker     if (Error->empty()) {
998*9880d681SAndroid Build Coastguard Worker       outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match "
999*9880d681SAndroid Build Coastguard Worker              << "the reference diff.  This may be due to a\n    front-end "
1000*9880d681SAndroid Build Coastguard Worker              << "bug or a bug in the original program, but this can also "
1001*9880d681SAndroid Build Coastguard Worker              << "happen if bugpoint isn't running the program with the "
1002*9880d681SAndroid Build Coastguard Worker              << "right flags or input.\n    I left the result of executing "
1003*9880d681SAndroid Build Coastguard Worker              << "the program with the \"safe\" backend in this file for "
1004*9880d681SAndroid Build Coastguard Worker              << "you: '"
1005*9880d681SAndroid Build Coastguard Worker              << Result << "'.\n";
1006*9880d681SAndroid Build Coastguard Worker     }
1007*9880d681SAndroid Build Coastguard Worker     return true;
1008*9880d681SAndroid Build Coastguard Worker   }
1009*9880d681SAndroid Build Coastguard Worker 
1010*9880d681SAndroid Build Coastguard Worker   DisambiguateGlobalSymbols(Program);
1011*9880d681SAndroid Build Coastguard Worker 
1012*9880d681SAndroid Build Coastguard Worker   std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator,
1013*9880d681SAndroid Build Coastguard Worker                                                       *Error);
1014*9880d681SAndroid Build Coastguard Worker   if (!Error->empty())
1015*9880d681SAndroid Build Coastguard Worker     return true;
1016*9880d681SAndroid Build Coastguard Worker 
1017*9880d681SAndroid Build Coastguard Worker   // Split the module into the two halves of the program we want.
1018*9880d681SAndroid Build Coastguard Worker   ValueToValueMapTy VMap;
1019*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> ToNotCodeGen = CloneModule(getProgram(), VMap);
1020*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<Module> ToCodeGen =
1021*9880d681SAndroid Build Coastguard Worker       SplitFunctionsOutOfModule(ToNotCodeGen.get(), Funcs, VMap);
1022*9880d681SAndroid Build Coastguard Worker 
1023*9880d681SAndroid Build Coastguard Worker   // Condition the modules
1024*9880d681SAndroid Build Coastguard Worker   CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen.get());
1025*9880d681SAndroid Build Coastguard Worker 
1026*9880d681SAndroid Build Coastguard Worker   SmallString<128> TestModuleBC;
1027*9880d681SAndroid Build Coastguard Worker   int TestModuleFD;
1028*9880d681SAndroid Build Coastguard Worker   std::error_code EC = sys::fs::createTemporaryFile("bugpoint.test", "bc",
1029*9880d681SAndroid Build Coastguard Worker                                                     TestModuleFD, TestModuleBC);
1030*9880d681SAndroid Build Coastguard Worker   if (EC) {
1031*9880d681SAndroid Build Coastguard Worker     errs() << getToolName() << "Error making unique filename: "
1032*9880d681SAndroid Build Coastguard Worker            << EC.message() << "\n";
1033*9880d681SAndroid Build Coastguard Worker     exit(1);
1034*9880d681SAndroid Build Coastguard Worker   }
1035*9880d681SAndroid Build Coastguard Worker 
1036*9880d681SAndroid Build Coastguard Worker   if (writeProgramToFile(TestModuleBC.str(), TestModuleFD, ToCodeGen.get())) {
1037*9880d681SAndroid Build Coastguard Worker     errs() << "Error writing bitcode to `" << TestModuleBC
1038*9880d681SAndroid Build Coastguard Worker            << "'\nExiting.";
1039*9880d681SAndroid Build Coastguard Worker     exit(1);
1040*9880d681SAndroid Build Coastguard Worker   }
1041*9880d681SAndroid Build Coastguard Worker 
1042*9880d681SAndroid Build Coastguard Worker   // Make the shared library
1043*9880d681SAndroid Build Coastguard Worker   SmallString<128> SafeModuleBC;
1044*9880d681SAndroid Build Coastguard Worker   int SafeModuleFD;
1045*9880d681SAndroid Build Coastguard Worker   EC = sys::fs::createTemporaryFile("bugpoint.safe", "bc", SafeModuleFD,
1046*9880d681SAndroid Build Coastguard Worker                                     SafeModuleBC);
1047*9880d681SAndroid Build Coastguard Worker   if (EC) {
1048*9880d681SAndroid Build Coastguard Worker     errs() << getToolName() << "Error making unique filename: "
1049*9880d681SAndroid Build Coastguard Worker            << EC.message() << "\n";
1050*9880d681SAndroid Build Coastguard Worker     exit(1);
1051*9880d681SAndroid Build Coastguard Worker   }
1052*9880d681SAndroid Build Coastguard Worker 
1053*9880d681SAndroid Build Coastguard Worker   if (writeProgramToFile(SafeModuleBC.str(), SafeModuleFD,
1054*9880d681SAndroid Build Coastguard Worker                          ToNotCodeGen.get())) {
1055*9880d681SAndroid Build Coastguard Worker     errs() << "Error writing bitcode to `" << SafeModuleBC
1056*9880d681SAndroid Build Coastguard Worker            << "'\nExiting.";
1057*9880d681SAndroid Build Coastguard Worker     exit(1);
1058*9880d681SAndroid Build Coastguard Worker   }
1059*9880d681SAndroid Build Coastguard Worker   std::string SharedObject = compileSharedObject(SafeModuleBC.str(), *Error);
1060*9880d681SAndroid Build Coastguard Worker   if (!Error->empty())
1061*9880d681SAndroid Build Coastguard Worker     return true;
1062*9880d681SAndroid Build Coastguard Worker 
1063*9880d681SAndroid Build Coastguard Worker   outs() << "You can reproduce the problem with the command line: \n";
1064*9880d681SAndroid Build Coastguard Worker   if (isExecutingJIT()) {
1065*9880d681SAndroid Build Coastguard Worker     outs() << "  lli -load " << SharedObject << " " << TestModuleBC;
1066*9880d681SAndroid Build Coastguard Worker   } else {
1067*9880d681SAndroid Build Coastguard Worker     outs() << "  llc " << TestModuleBC << " -o " << TestModuleBC
1068*9880d681SAndroid Build Coastguard Worker            << ".s\n";
1069*9880d681SAndroid Build Coastguard Worker     outs() << "  cc " << SharedObject << " " << TestModuleBC.str()
1070*9880d681SAndroid Build Coastguard Worker               << ".s -o " << TestModuleBC << ".exe";
1071*9880d681SAndroid Build Coastguard Worker #if defined (HAVE_LINK_R)
1072*9880d681SAndroid Build Coastguard Worker     outs() << " -Wl,-R.";
1073*9880d681SAndroid Build Coastguard Worker #endif
1074*9880d681SAndroid Build Coastguard Worker     outs() << "\n";
1075*9880d681SAndroid Build Coastguard Worker     outs() << "  " << TestModuleBC << ".exe";
1076*9880d681SAndroid Build Coastguard Worker   }
1077*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = InputArgv.size(); i != e; ++i)
1078*9880d681SAndroid Build Coastguard Worker     outs() << " " << InputArgv[i];
1079*9880d681SAndroid Build Coastguard Worker   outs() << '\n';
1080*9880d681SAndroid Build Coastguard Worker   outs() << "The shared object was created with:\n  llc -march=c "
1081*9880d681SAndroid Build Coastguard Worker          << SafeModuleBC.str() << " -o temporary.c\n"
1082*9880d681SAndroid Build Coastguard Worker          << "  cc -xc temporary.c -O2 -o " << SharedObject;
1083*9880d681SAndroid Build Coastguard Worker   if (TargetTriple.getArch() == Triple::sparc)
1084*9880d681SAndroid Build Coastguard Worker     outs() << " -G";              // Compile a shared library, `-G' for Sparc
1085*9880d681SAndroid Build Coastguard Worker   else
1086*9880d681SAndroid Build Coastguard Worker     outs() << " -fPIC -shared";   // `-shared' for Linux/X86, maybe others
1087*9880d681SAndroid Build Coastguard Worker 
1088*9880d681SAndroid Build Coastguard Worker   outs() << " -fno-strict-aliasing\n";
1089*9880d681SAndroid Build Coastguard Worker 
1090*9880d681SAndroid Build Coastguard Worker   return false;
1091*9880d681SAndroid Build Coastguard Worker }
1092