Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 9 | // |
| 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 Kramer | 00e08fc | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 16 | #ifndef LLVM_TOOLS_BUGPOINT_BUGDRIVER_H |
| 17 | #define LLVM_TOOLS_BUGPOINT_BUGDRIVER_H |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 18 | |
Chandler Carruth | 7225e27 | 2014-03-04 11:26:31 +0000 | [diff] [blame] | 19 | #include "llvm/IR/ValueMap.h" |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Error.h" |
Rafael Espindola | 324f141 | 2017-11-16 17:35:50 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 23 | #include <memory> |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 24 | #include <string> |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 25 | #include <vector> |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 26 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | namespace llvm { |
| 28 | |
Dan Gohman | d50330c | 2009-04-22 15:57:18 +0000 | [diff] [blame] | 29 | class Value; |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 30 | class PassInfo; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 31 | class Module; |
Bill Wendling | 4e3be89 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 32 | class GlobalVariable; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 33 | class Function; |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 34 | class BasicBlock; |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 35 | class AbstractInterpreter; |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 36 | class Instruction; |
Benjamin Kramer | 12ddd40 | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 37 | class LLVMContext; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 38 | |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 39 | class DebugCrashes; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 40 | |
Davide Italiano | f7b2acb | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 41 | class CC; |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 43 | extern bool DisableSimplifyCFG; |
| 44 | |
Chris Lattner | f9aaae0 | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 45 | /// BugpointIsInterrupted - Set to true when the user presses ctrl-c. |
| 46 | /// |
| 47 | extern bool BugpointIsInterrupted; |
| 48 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 49 | class BugDriver { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 50 | LLVMContext &Context; |
Dan Gohman | 197f728 | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 51 | const char *ToolName; // argv[0] of bugpoint |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 52 | std::string ReferenceOutputFile; // Name of `good' output file |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 53 | std::unique_ptr<Module> Program; // The raw program, linked together |
Rafael Espindola | 8261dfe | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 54 | std::vector<std::string> PassesToRun; |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 55 | AbstractInterpreter *Interpreter; // How to run the program |
| 56 | AbstractInterpreter *SafeInterpreter; // To generate reference output, etc. |
Davide Italiano | f7b2acb | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 57 | CC *cc; |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 58 | bool run_find_bugs; |
Chris Lattner | 9686ae7 | 2006-06-13 03:10:48 +0000 | [diff] [blame] | 59 | unsigned Timeout; |
Anton Korobeynikov | 9ba8a76 | 2007-02-16 19:11:07 +0000 | [diff] [blame] | 60 | unsigned MemoryLimit; |
Jeffrey Yasskin | c3e6859 | 2010-03-19 00:09:28 +0000 | [diff] [blame] | 61 | bool UseValgrind; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 62 | |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 63 | // FIXME: sort out public/private distinctions... |
Chris Lattner | 06905db | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 64 | friend class ReducePassList; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 65 | friend class ReduceMisCodegenFunctions; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 66 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 67 | public: |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 68 | BugDriver(const char *toolname, bool find_bugs, unsigned timeout, |
| 69 | unsigned memlimit, bool use_valgrind, LLVMContext &ctxt); |
Jeffrey Yasskin | c1dc067 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 70 | ~BugDriver(); |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 71 | |
Dan Gohman | 197f728 | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 72 | const char *getToolName() const { return ToolName; } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 73 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 74 | LLVMContext &getContext() const { return Context; } |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 75 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 76 | // 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 Kramer | 36538ff | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 80 | void addPass(std::string p) { PassesToRun.push_back(std::move(p)); } |
Rafael Espindola | 8261dfe | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 81 | void setPassesToRun(const std::vector<std::string> &PTR) { |
Chris Lattner | 9c6cfe1 | 2003-10-17 23:03:16 +0000 | [diff] [blame] | 82 | PassesToRun = PTR; |
| 83 | } |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 84 | const std::vector<std::string> &getPassesToRun() const { return PassesToRun; } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 85 | |
| 86 | /// run - The top level method that is invoked after all of the instance |
Reid Spencer | c4bb052 | 2005-12-22 20:02:55 +0000 | [diff] [blame] | 87 | /// 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 Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 89 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 90 | Error run(); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 92 | /// 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 Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 95 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 96 | Error debugOptimizerCrash(const std::string &ID = "passes"); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 0252626 | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 98 | /// 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 Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 101 | Error debugCodeGeneratorCrash(); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 102 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 103 | /// 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 Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 106 | Error debugMiscompilation(); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 108 | /// 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 Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 114 | bool debugPassMiscompilation(const PassInfo *ThePass, |
Jeff Cohen | ea3e5e5 | 2005-04-22 04:13:13 +0000 | [diff] [blame] | 115 | const std::string &ReferenceOutput); |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 116 | |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 117 | /// compileSharedObject - This method creates a SharedObject from a given |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 118 | /// BitcodeFile for debugging a code generator. |
Chris Lattner | a0f5b15 | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 119 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 120 | Expected<std::string> compileSharedObject(const std::string &BitcodeFile); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 121 | |
| 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 Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 125 | Error debugCodeGenerator(); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 126 | |
Misha Brukman | fe04db8 | 2003-07-28 20:59:16 +0000 | [diff] [blame] | 127 | /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT |
| 128 | /// |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 129 | bool isExecutingJIT(); |
| 130 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 131 | Module &getProgram() const { return *Program; } |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 132 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 133 | /// Set the current module to the specified module, returning the old one. |
| 134 | std::unique_ptr<Module> swapProgramIn(std::unique_ptr<Module> M); |
Chris Lattner | 06905db | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 135 | |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 136 | AbstractInterpreter *switchToSafeInterpreter() { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 137 | AbstractInterpreter *Old = Interpreter; |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 138 | Interpreter = (AbstractInterpreter *)SafeInterpreter; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 139 | return Old; |
| 140 | } |
| 141 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 142 | void switchToInterpreter(AbstractInterpreter *AI) { Interpreter = AI; } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 143 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 144 | /// 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 Lattner | 06905db | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 148 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 149 | /// Try to compile the specified module. This is used for code generation |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 150 | /// crash testing. |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 151 | Error compileProgram(Module &M) const; |
Chris Lattner | ea9212c | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 152 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 153 | /// 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 Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 156 | std::string OutputFilename, |
| 157 | std::string Bitcode, |
| 158 | const std::string &SharedObjects, |
| 159 | AbstractInterpreter *AI) const; |
Chris Lattner | a36ec88 | 2004-02-18 21:35:28 +0000 | [diff] [blame] | 160 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 161 | /// 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 Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 164 | Expected<std::string> |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 165 | executeProgramSafely(const Module &Program, |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 166 | const std::string &OutputFile) const; |
Chris Lattner | a36ec88 | 2004-02-18 21:35:28 +0000 | [diff] [blame] | 167 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 168 | /// 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 Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 172 | "bugpoint.reference.out-%%%%%%%"); |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 173 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 174 | /// 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 Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 179 | const std::string &BitcodeFile = "", |
| 180 | const std::string &SharedObj = "", |
| 181 | bool RemoveBitcode = false) const; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 182 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 183 | /// 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 Espindola | ca356af | 2010-08-05 00:29:04 +0000 | [diff] [blame] | 185 | bool NoFlyer = false) const; |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 186 | |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 187 | /// 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 Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 191 | /// |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 192 | std::unique_ptr<Module> deleteInstructionFromProgram(const Instruction *I, |
| 193 | unsigned Simp); |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 194 | |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 195 | /// 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 Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 198 | /// modify how the code behaves. |
| 199 | /// |
Rafael Espindola | 46e5502 | 2018-04-24 20:15:27 +0000 | [diff] [blame] | 200 | std::unique_ptr<Module> performFinalCleanups(std::unique_ptr<Module> M, |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 201 | bool MayModifySemantics = false); |
Chris Lattner | 0cc8807 | 2004-02-18 21:50:26 +0000 | [diff] [blame] | 202 | |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 203 | /// 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 Lattner | 7546c38 | 2004-03-14 20:02:07 +0000 | [diff] [blame] | 207 | |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 208 | /// 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 Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 216 | |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 217 | /// 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 Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 219 | std::unique_ptr<Module> runPassesOn(Module *M, |
| 220 | const std::vector<std::string> &Passes, |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 221 | unsigned NumExtraArgs = 0, |
| 222 | const char *const *ExtraArgs = nullptr); |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 223 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 224 | /// runPasses - Run the specified passes on Program, outputting a bitcode |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 225 | /// file and writting the filename into OutputFile if successful. If the |
| 226 | /// optimizations fail for some reason (optimizer crashes), return true, |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 227 | /// otherwise return false. If DeleteOutput is set to true, the bitcode is |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 228 | /// deleted on success, and the filename string is undefined. This prints to |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 229 | /// outs() a single line message indicating whether compilation was successful |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 230 | /// or failed, unless Quiet is set. ExtraArgs specifies additional arguments |
| 231 | /// to pass to the child bugpoint instance. |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 232 | /// |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 233 | bool runPasses(Module &Program, const std::vector<std::string> &PassesToRun, |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 234 | std::string &OutputFilename, bool DeleteOutput = false, |
Nick Lewycky | 6fa98b1 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 235 | bool Quiet = false, unsigned NumExtraArgs = 0, |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 236 | const char *const *ExtraArgs = nullptr) const; |
Philip Reames | c29dedc | 2016-06-29 03:01:13 +0000 | [diff] [blame] | 237 | |
| 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 Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 242 | bool runPasses(Module &M, const std::vector<std::string> &PassesToRun) const { |
Philip Reames | c29dedc | 2016-06-29 03:01:13 +0000 | [diff] [blame] | 243 | std::string Filename; |
| 244 | return runPasses(M, PassesToRun, Filename, true); |
| 245 | } |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 246 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 247 | /// 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 Lattner | 11b8cd1 | 2004-04-05 22:01:48 +0000 | [diff] [blame] | 254 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 255 | /// 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 Espindola | 88088f4 | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 258 | bool writeProgramToFile(const std::string &Filename, int FD, |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 259 | const Module &M) const; |
| 260 | bool writeProgramToFile(int FD, const Module &M) const; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 11b8cd1 | 2004-04-05 22:01:48 +0000 | [diff] [blame] | 262 | private: |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 263 | /// initializeExecutionEnvironment - This method is used to set up the |
| 264 | /// environment for executing LLVM programs. |
| 265 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 266 | Error initializeExecutionEnvironment(); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 267 | }; |
| 268 | |
Rafael Espindola | 324f141 | 2017-11-16 17:35:50 +0000 | [diff] [blame] | 269 | struct DiscardTemp { |
| 270 | sys::fs::TempFile &File; |
| 271 | ~DiscardTemp(); |
| 272 | }; |
| 273 | |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 274 | /// Given a bitcode or assembly input filename, parse and return it, or return |
| 275 | /// null if not possible. |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 276 | /// |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 277 | std::unique_ptr<Module> parseInputFile(StringRef InputFilename, |
| 278 | LLVMContext &ctxt); |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 280 | /// 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 Espindola | 8261dfe | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 283 | std::string getPassesString(const std::vector<std::string> &Passes); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 284 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 285 | /// PrintFunctionList - prints out list of problematic functions |
| 286 | /// |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 287 | void PrintFunctionList(const std::vector<Function *> &Funcs); |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 288 | |
Bill Wendling | 4e3be89 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 289 | /// PrintGlobalVariableList - prints out list of problematic global variables |
| 290 | /// |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 291 | void PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs); |
Bill Wendling | 4e3be89 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 292 | |
Hal Finkel | c818be0 | 2015-11-26 19:23:49 +0000 | [diff] [blame] | 293 | // DeleteGlobalInitializer - "Remove" the global variable by deleting its |
| 294 | // initializer, making it external. |
| 295 | // |
| 296 | void DeleteGlobalInitializer(GlobalVariable *GV); |
| 297 | |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 298 | // DeleteFunctionBody - "Remove" the function by deleting all of it's basic |
| 299 | // blocks, making it external. |
| 300 | // |
| 301 | void DeleteFunctionBody(Function *F); |
| 302 | |
Rafael Espindola | 18d7050 | 2015-12-09 00:34:10 +0000 | [diff] [blame] | 303 | /// 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. |
| 305 | std::unique_ptr<Module> |
| 306 | SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F, |
| 307 | ValueToValueMapTy &VMap); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 308 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 309 | } // End llvm namespace |
| 310 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 311 | #endif |