blob: bc60ae753548bd9d8072ead83010962498e3dec4 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerafade922002-11-20 22:28:10 +00009//
10// This class contains all of the shared state and information that is used by
11// the BugPoint tool to track down errors in optimizations. This class is the
12// main driver class that invokes all sub-functionality.
13//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000016#ifndef LLVM_TOOLS_BUGPOINT_BUGDRIVER_H
17#define LLVM_TOOLS_BUGPOINT_BUGDRIVER_H
Chris Lattnerafade922002-11-20 22:28:10 +000018
Chandler Carruth7225e272014-03-04 11:26:31 +000019#include "llvm/IR/ValueMap.h"
Justin Bognerd8090ae2016-09-06 17:18:22 +000020#include "llvm/Support/Error.h"
Rafael Espindola324f1412017-11-16 17:35:50 +000021#include "llvm/Support/FileSystem.h"
Rafael Espindola1ed219a2010-10-13 01:36:30 +000022#include "llvm/Transforms/Utils/ValueMapper.h"
Rafael Espindola1bfd87a2014-08-26 17:19:03 +000023#include <memory>
Chris Lattnerafade922002-11-20 22:28:10 +000024#include <string>
Chandler Carruthf010c462012-12-04 10:44:52 +000025#include <vector>
Misha Brukman50733362003-07-24 18:17:43 +000026
Brian Gaeked0fde302003-11-11 22:41:34 +000027namespace llvm {
28
Dan Gohmand50330c2009-04-22 15:57:18 +000029class Value;
Owen Anderson8be32912010-07-20 08:26:15 +000030class PassInfo;
Chris Lattnerafade922002-11-20 22:28:10 +000031class Module;
Bill Wendling4e3be892006-10-25 18:36:14 +000032class GlobalVariable;
Chris Lattnerafade922002-11-20 22:28:10 +000033class Function;
Chris Lattner5e783ab2004-05-11 21:54:13 +000034class BasicBlock;
Chris Lattner218e26e2002-12-23 23:49:59 +000035class AbstractInterpreter;
Chris Lattner65207852003-01-23 02:48:33 +000036class Instruction;
Benjamin Kramer12ddd402009-08-11 17:45:13 +000037class LLVMContext;
Chris Lattnerafade922002-11-20 22:28:10 +000038
Chris Lattneraae33f92003-04-24 22:24:58 +000039class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000040
Davide Italianof7b2acb2015-10-14 20:29:54 +000041class CC;
Misha Brukmana259c9b2003-07-24 21:59:10 +000042
Chris Lattner47ae4a12003-08-05 15:51:05 +000043extern bool DisableSimplifyCFG;
44
Chris Lattnerf9aaae02005-08-02 02:16:17 +000045/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
46///
47extern bool BugpointIsInterrupted;
48
Chris Lattnerafade922002-11-20 22:28:10 +000049class BugDriver {
Justin Bogner388e8b92016-09-02 01:21:37 +000050 LLVMContext &Context;
Dan Gohman197f7282009-08-05 20:21:17 +000051 const char *ToolName; // argv[0] of bugpoint
Misha Brukmana259c9b2003-07-24 21:59:10 +000052 std::string ReferenceOutputFile; // Name of `good' output file
Rafael Espindolacdac12c2018-02-14 21:44:34 +000053 std::unique_ptr<Module> Program; // The raw program, linked together
Rafael Espindola8261dfe2010-08-08 03:55:08 +000054 std::vector<std::string> PassesToRun;
Justin Bogner388e8b92016-09-02 01:21:37 +000055 AbstractInterpreter *Interpreter; // How to run the program
56 AbstractInterpreter *SafeInterpreter; // To generate reference output, etc.
Davide Italianof7b2acb2015-10-14 20:29:54 +000057 CC *cc;
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000058 bool run_find_bugs;
Chris Lattner9686ae72006-06-13 03:10:48 +000059 unsigned Timeout;
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000060 unsigned MemoryLimit;
Jeffrey Yasskinc3e68592010-03-19 00:09:28 +000061 bool UseValgrind;
Chris Lattner640f22e2003-04-24 17:02:17 +000062
Chris Lattneraae33f92003-04-24 22:24:58 +000063 // FIXME: sort out public/private distinctions...
Chris Lattner06905db2004-02-18 21:24:48 +000064 friend class ReducePassList;
Misha Brukman50733362003-07-24 18:17:43 +000065 friend class ReduceMisCodegenFunctions;
Misha Brukman50733362003-07-24 18:17:43 +000066
Chris Lattnerafade922002-11-20 22:28:10 +000067public:
Justin Bogner388e8b92016-09-02 01:21:37 +000068 BugDriver(const char *toolname, bool find_bugs, unsigned timeout,
69 unsigned memlimit, bool use_valgrind, LLVMContext &ctxt);
Jeffrey Yasskinc1dc0672010-03-22 05:23:37 +000070 ~BugDriver();
Chris Lattner218e26e2002-12-23 23:49:59 +000071
Dan Gohman197f7282009-08-05 20:21:17 +000072 const char *getToolName() const { return ToolName; }
Chris Lattnerafade922002-11-20 22:28:10 +000073
Justin Bogner388e8b92016-09-02 01:21:37 +000074 LLVMContext &getContext() const { return Context; }
Owen Anderson8b477ed2009-07-01 16:58:40 +000075
Chris Lattnerafade922002-11-20 22:28:10 +000076 // Set up methods... these methods are used to copy information about the
77 // command line arguments into instance variables of BugDriver.
78 //
79 bool addSources(const std::vector<std::string> &FileNames);
Benjamin Kramer36538ff2016-06-08 19:09:22 +000080 void addPass(std::string p) { PassesToRun.push_back(std::move(p)); }
Rafael Espindola8261dfe2010-08-08 03:55:08 +000081 void setPassesToRun(const std::vector<std::string> &PTR) {
Chris Lattner9c6cfe12003-10-17 23:03:16 +000082 PassesToRun = PTR;
83 }
Justin Bogner388e8b92016-09-02 01:21:37 +000084 const std::vector<std::string> &getPassesToRun() const { return PassesToRun; }
Chris Lattnerafade922002-11-20 22:28:10 +000085
86 /// run - The top level method that is invoked after all of the instance
Reid Spencerc4bb0522005-12-22 20:02:55 +000087 /// variables are set up from command line arguments. The \p as_child argument
88 /// indicates whether the driver is to run in parent mode or child mode.
Chris Lattnerafade922002-11-20 22:28:10 +000089 ///
Justin Bognerd8090ae2016-09-06 17:18:22 +000090 Error run();
Chris Lattnerafade922002-11-20 22:28:10 +000091
Chris Lattner02526262004-02-18 21:02:04 +000092 /// debugOptimizerCrash - This method is called when some optimizer pass
93 /// crashes on input. It attempts to prune down the testcase to something
94 /// reasonable, and figure out exactly which pass is crashing.
Chris Lattnerafade922002-11-20 22:28:10 +000095 ///
Justin Bognerd8090ae2016-09-06 17:18:22 +000096 Error debugOptimizerCrash(const std::string &ID = "passes");
Misha Brukman3da94ae2005-04-22 00:00:37 +000097
Chris Lattner02526262004-02-18 21:02:04 +000098 /// debugCodeGeneratorCrash - This method is called when the code generator
99 /// crashes on an input. It attempts to reduce the input as much as possible
100 /// while still causing the code generator to crash.
Justin Bognerd8090ae2016-09-06 17:18:22 +0000101 Error debugCodeGeneratorCrash();
Chris Lattnerafade922002-11-20 22:28:10 +0000102
Chris Lattnerafade922002-11-20 22:28:10 +0000103 /// debugMiscompilation - This method is used when the passes selected are not
104 /// crashing, but the generated output is semantically different from the
105 /// input.
Justin Bognerd8090ae2016-09-06 17:18:22 +0000106 Error debugMiscompilation();
Chris Lattnerafade922002-11-20 22:28:10 +0000107
Chris Lattner218e26e2002-12-23 23:49:59 +0000108 /// debugPassMiscompilation - This method is called when the specified pass
109 /// miscompiles Program as input. It tries to reduce the testcase to
110 /// something that smaller that still miscompiles the program.
111 /// ReferenceOutput contains the filename of the file containing the output we
112 /// are to match.
113 ///
Owen Anderson8be32912010-07-20 08:26:15 +0000114 bool debugPassMiscompilation(const PassInfo *ThePass,
Jeff Cohenea3e5e52005-04-22 04:13:13 +0000115 const std::string &ReferenceOutput);
Chris Lattner218e26e2002-12-23 23:49:59 +0000116
Misha Brukman50733362003-07-24 18:17:43 +0000117 /// compileSharedObject - This method creates a SharedObject from a given
Gabor Greif8ff70c22007-07-04 21:55:50 +0000118 /// BitcodeFile for debugging a code generator.
Chris Lattnera0f5b152003-10-14 21:09:11 +0000119 ///
Justin Bognerd8090ae2016-09-06 17:18:22 +0000120 Expected<std::string> compileSharedObject(const std::string &BitcodeFile);
Misha Brukman50733362003-07-24 18:17:43 +0000121
122 /// debugCodeGenerator - This method narrows down a module to a function or
123 /// set of functions, using the CBE as a ``safe'' code generator for other
124 /// functions that are not under consideration.
Justin Bognerd8090ae2016-09-06 17:18:22 +0000125 Error debugCodeGenerator();
Misha Brukman50733362003-07-24 18:17:43 +0000126
Misha Brukmanfe04db82003-07-28 20:59:16 +0000127 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
128 ///
Misha Brukman91eabc12003-07-28 19:16:14 +0000129 bool isExecutingJIT();
130
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000131 Module &getProgram() const { return *Program; }
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000132
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000133 /// Set the current module to the specified module, returning the old one.
134 std::unique_ptr<Module> swapProgramIn(std::unique_ptr<Module> M);
Chris Lattner06905db2004-02-18 21:24:48 +0000135
Dan Gohman70ef4492008-12-08 04:02:47 +0000136 AbstractInterpreter *switchToSafeInterpreter() {
Chris Lattnera57d86b2004-04-05 22:58:16 +0000137 AbstractInterpreter *Old = Interpreter;
Justin Bogner388e8b92016-09-02 01:21:37 +0000138 Interpreter = (AbstractInterpreter *)SafeInterpreter;
Chris Lattnera57d86b2004-04-05 22:58:16 +0000139 return Old;
140 }
141
Justin Bogner388e8b92016-09-02 01:21:37 +0000142 void switchToInterpreter(AbstractInterpreter *AI) { Interpreter = AI; }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000143
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000144 /// If we reduce or update the program somehow, call this method to update
145 /// bugdriver with it. This deletes the old module and sets the specified one
146 /// as the current program.
147 void setNewProgram(std::unique_ptr<Module> M);
Chris Lattner06905db2004-02-18 21:24:48 +0000148
Justin Bognerd8090ae2016-09-06 17:18:22 +0000149 /// Try to compile the specified module. This is used for code generation
Nick Lewycky22ff7482010-04-12 05:08:25 +0000150 /// crash testing.
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000151 Error compileProgram(Module &M) const;
Chris Lattnerea9212c2004-02-18 23:25:22 +0000152
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000153 /// This method runs "Program", capturing the output of the program to a file.
154 /// A recommended filename may be optionally specified.
155 Expected<std::string> executeProgram(const Module &Program,
Justin Bognerd8090ae2016-09-06 17:18:22 +0000156 std::string OutputFilename,
157 std::string Bitcode,
158 const std::string &SharedObjects,
159 AbstractInterpreter *AI) const;
Chris Lattnera36ec882004-02-18 21:35:28 +0000160
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000161 /// Used to create reference output with the "safe" backend, if reference
162 /// output is not provided. If there is a problem with the code generator
163 /// (e.g., llc crashes), this will return false and set Error.
Justin Bognerd8090ae2016-09-06 17:18:22 +0000164 Expected<std::string>
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000165 executeProgramSafely(const Module &Program,
Justin Bognerd8090ae2016-09-06 17:18:22 +0000166 const std::string &OutputFile) const;
Chris Lattnera36ec882004-02-18 21:35:28 +0000167
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000168 /// Calls compileProgram and then records the output into ReferenceOutputFile.
169 /// Returns true if reference file created, false otherwise. Note:
170 /// initializeExecutionEnvironment should be called BEFORE this function.
171 Error createReferenceFile(Module &M, const std::string &Filename =
Justin Bognerd8090ae2016-09-06 17:18:22 +0000172 "bugpoint.reference.out-%%%%%%%");
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000173
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000174 /// This method executes the specified module and diffs the output against the
175 /// file specified by ReferenceOutputFile. If the output is different, 1 is
176 /// returned. If there is a problem with the code generator (e.g., llc
177 /// crashes), this will return -1 and set Error.
178 Expected<bool> diffProgram(const Module &Program,
Justin Bognerd8090ae2016-09-06 17:18:22 +0000179 const std::string &BitcodeFile = "",
180 const std::string &SharedObj = "",
181 bool RemoveBitcode = false) const;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000182
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000183 /// This function is used to output M to a file named "bugpoint-ID.bc".
184 void EmitProgressBitcode(const Module &M, const std::string &ID,
Rafael Espindolaca356af2010-08-05 00:29:04 +0000185 bool NoFlyer = false) const;
Chris Lattner0cc88072004-02-18 21:50:26 +0000186
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000187 /// This method clones the current Program and deletes the specified
188 /// instruction from the cloned module. It then runs a series of cleanup
189 /// passes (ADCE and SimplifyCFG) to eliminate any code which depends on the
190 /// value. The modified module is then returned.
Chris Lattner0cc88072004-02-18 21:50:26 +0000191 ///
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000192 std::unique_ptr<Module> deleteInstructionFromProgram(const Instruction *I,
193 unsigned Simp);
Chris Lattner0cc88072004-02-18 21:50:26 +0000194
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000195 /// This method clones the current Program and performs a series of cleanups
196 /// intended to get rid of extra cruft on the module. If the
197 /// MayModifySemantics argument is true, then the cleanups is allowed to
Chris Lattner0cc88072004-02-18 21:50:26 +0000198 /// modify how the code behaves.
199 ///
Rafael Espindola46e55022018-04-24 20:15:27 +0000200 std::unique_ptr<Module> performFinalCleanups(std::unique_ptr<Module> M,
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000201 bool MayModifySemantics = false);
Chris Lattner0cc88072004-02-18 21:50:26 +0000202
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000203 /// Given a module, extract up to one loop from it into a new function. This
204 /// returns null if there are no extractable loops in the program or if the
205 /// loop extractor crashes.
206 std::unique_ptr<Module> extractLoop(Module *M);
Chris Lattner7546c382004-03-14 20:02:07 +0000207
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000208 /// Extract all but the specified basic blocks into their own functions. The
209 /// only detail is that M is actually a module cloned from the one the BBs are
210 /// in, so some mapping needs to be performed. If this operation fails for
211 /// some reason (ie the implementation is buggy), this function should return
212 /// null, otherwise it returns a new Module.
213 std::unique_ptr<Module>
214 extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs,
215 Module *M);
Chris Lattner5e783ab2004-05-11 21:54:13 +0000216
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000217 /// Carefully run the specified set of pass on the specified/ module,
218 /// returning the transformed module on success, or a null pointer on failure.
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000219 std::unique_ptr<Module> runPassesOn(Module *M,
220 const std::vector<std::string> &Passes,
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000221 unsigned NumExtraArgs = 0,
222 const char *const *ExtraArgs = nullptr);
Chris Lattner3b6441e2004-03-14 21:17:03 +0000223
Gabor Greif8ff70c22007-07-04 21:55:50 +0000224 /// runPasses - Run the specified passes on Program, outputting a bitcode
Chris Lattnerafade922002-11-20 22:28:10 +0000225 /// file and writting the filename into OutputFile if successful. If the
226 /// optimizations fail for some reason (optimizer crashes), return true,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000227 /// otherwise return false. If DeleteOutput is set to true, the bitcode is
Chris Lattnerafade922002-11-20 22:28:10 +0000228 /// deleted on success, and the filename string is undefined. This prints to
Dan Gohmanac95cc72009-07-16 15:30:09 +0000229 /// outs() a single line message indicating whether compilation was successful
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000230 /// or failed, unless Quiet is set. ExtraArgs specifies additional arguments
231 /// to pass to the child bugpoint instance.
Chris Lattnerafade922002-11-20 22:28:10 +0000232 ///
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000233 bool runPasses(Module &Program, const std::vector<std::string> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +0000234 std::string &OutputFilename, bool DeleteOutput = false,
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000235 bool Quiet = false, unsigned NumExtraArgs = 0,
Justin Bogner388e8b92016-09-02 01:21:37 +0000236 const char *const *ExtraArgs = nullptr) const;
Philip Reamesc29dedc2016-06-29 03:01:13 +0000237
238 /// runPasses - Just like the method above, but this just returns true or
239 /// false indicating whether or not the optimizer crashed on the specified
240 /// input (true = crashed). Does not produce any output.
241 ///
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000242 bool runPasses(Module &M, const std::vector<std::string> &PassesToRun) const {
Philip Reamesc29dedc2016-06-29 03:01:13 +0000243 std::string Filename;
244 return runPasses(M, PassesToRun, Filename, true);
245 }
Justin Bogner388e8b92016-09-02 01:21:37 +0000246
Justin Bognerd8090ae2016-09-06 17:18:22 +0000247 /// Take the specified pass list and create different combinations of passes
248 /// to compile the program with. Compile the program with each set and mark
249 /// test to see if it compiled correctly. If the passes compiled correctly
250 /// output nothing and rearrange the passes into a new order. If the passes
251 /// did not compile correctly, output the command required to recreate the
252 /// failure.
253 Error runManyPasses(const std::vector<std::string> &AllPasses);
Chris Lattner11b8cd12004-04-05 22:01:48 +0000254
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000255 /// This writes the current "Program" to the named bitcode file. If an error
256 /// occurs, true is returned.
257 bool writeProgramToFile(const std::string &Filename, const Module &M) const;
Rafael Espindola88088f42013-06-18 15:29:32 +0000258 bool writeProgramToFile(const std::string &Filename, int FD,
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000259 const Module &M) const;
260 bool writeProgramToFile(int FD, const Module &M) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000261
Chris Lattner11b8cd12004-04-05 22:01:48 +0000262private:
Chris Lattner218e26e2002-12-23 23:49:59 +0000263 /// initializeExecutionEnvironment - This method is used to set up the
264 /// environment for executing LLVM programs.
265 ///
Justin Bognerd8090ae2016-09-06 17:18:22 +0000266 Error initializeExecutionEnvironment();
Chris Lattnerafade922002-11-20 22:28:10 +0000267};
268
Rafael Espindola324f1412017-11-16 17:35:50 +0000269struct DiscardTemp {
270 sys::fs::TempFile &File;
271 ~DiscardTemp();
272};
273
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000274/// Given a bitcode or assembly input filename, parse and return it, or return
275/// null if not possible.
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000276///
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000277std::unique_ptr<Module> parseInputFile(StringRef InputFilename,
278 LLVMContext &ctxt);
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000279
Chris Lattner640f22e2003-04-24 17:02:17 +0000280/// getPassesString - Turn a list of passes into a string which indicates the
281/// command line options that must be passed to add the passes.
282///
Rafael Espindola8261dfe2010-08-08 03:55:08 +0000283std::string getPassesString(const std::vector<std::string> &Passes);
Chris Lattner640f22e2003-04-24 17:02:17 +0000284
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000285/// PrintFunctionList - prints out list of problematic functions
286///
Justin Bogner388e8b92016-09-02 01:21:37 +0000287void PrintFunctionList(const std::vector<Function *> &Funcs);
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000288
Bill Wendling4e3be892006-10-25 18:36:14 +0000289/// PrintGlobalVariableList - prints out list of problematic global variables
290///
Justin Bogner388e8b92016-09-02 01:21:37 +0000291void PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs);
Bill Wendling4e3be892006-10-25 18:36:14 +0000292
Hal Finkelc818be02015-11-26 19:23:49 +0000293// DeleteGlobalInitializer - "Remove" the global variable by deleting its
294// initializer, making it external.
295//
296void DeleteGlobalInitializer(GlobalVariable *GV);
297
Chris Lattneraae33f92003-04-24 22:24:58 +0000298// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
299// blocks, making it external.
300//
301void DeleteFunctionBody(Function *F);
302
Rafael Espindola18d70502015-12-09 00:34:10 +0000303/// Given a module and a list of functions in the module, split the functions
304/// OUT of the specified module, and place them in the new module.
305std::unique_ptr<Module>
306SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F,
307 ValueToValueMapTy &VMap);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000308
Brian Gaeked0fde302003-11-11 22:41:34 +0000309} // End llvm namespace
310
Chris Lattnerafade922002-11-20 22:28:10 +0000311#endif