BugDriver.h revision 8261dfed05e32302469ef707cc881fed2c31f85f
1afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
23da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman//
37c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//                     The LLVM Compiler Infrastructure
47c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
521c62da287237d39d0d95004881ea4baae3be6daChris Lattner// This file is distributed under the University of Illinois Open Source
621c62da287237d39d0d95004881ea4baae3be6daChris Lattner// License. See LICENSE.TXT for details.
73da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman//
87c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//===----------------------------------------------------------------------===//
9afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//
10afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// This class contains all of the shared state and information that is used by
11afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// the BugPoint tool to track down errors in optimizations.  This class is the
12afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// main driver class that invokes all sub-functionality.
13afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//
14afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===----------------------------------------------------------------------===//
15afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
16afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#ifndef BUGDRIVER_H
17afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#define BUGDRIVER_H
18afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
19e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel#include "llvm/ADT/ValueMap.h"
20afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include <vector>
21afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include <string>
225073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
23d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
25d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohmanclass Value;
268be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Andersonclass PassInfo;
27afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerclass Module;
284e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendlingclass GlobalVariable;
29afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerclass Function;
305e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnerclass BasicBlock;
31218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattnerclass AbstractInterpreter;
326520785dcd22012535934098942d57c07c7631c2Chris Lattnerclass Instruction;
3312ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramerclass LLVMContext;
34afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
35aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattnerclass DebugCrashes;
36640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
37a259c9be2acc9528ec7feb3cfd51dcde36d87bb3Misha Brukmanclass GCC;
38a259c9be2acc9528ec7feb3cfd51dcde36d87bb3Misha Brukman
3947ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattnerextern bool DisableSimplifyCFG;
4047ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner
41f9aaae06cd2109082cda2b09ef3f23e0e1cff47bChris Lattner/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
42f9aaae06cd2109082cda2b09ef3f23e0e1cff47bChris Lattner///
43f9aaae06cd2109082cda2b09ef3f23e0e1cff47bChris Lattnerextern bool BugpointIsInterrupted;
44f9aaae06cd2109082cda2b09ef3f23e0e1cff47bChris Lattner
45afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerclass BugDriver {
464434ed44c45c87a72b7a0bf2f91211f895022b91Owen Anderson  LLVMContext& Context;
47197f728d49fa0cc0baa5aadb2b905fbd8c22a81eDan Gohman  const char *ToolName;            // argv[0] of bugpoint
48a259c9be2acc9528ec7feb3cfd51dcde36d87bb3Misha Brukman  std::string ReferenceOutputFile; // Name of `good' output file
49afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  Module *Program;             // The raw program, linked together
508261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  std::vector<std::string> PassesToRun;
51218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  AbstractInterpreter *Interpreter;   // How to run the program
5270ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  AbstractInterpreter *SafeInterpreter;  // To generate reference output, etc.
53a259c9be2acc9528ec7feb3cfd51dcde36d87bb3Misha Brukman  GCC *gcc;
546a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  bool run_find_bugs;
559686ae7f4ea5f19ce77e31e64e0916db41a82662Chris Lattner  unsigned Timeout;
569ba8a76f8baaa1092d60ccfbc04e7efdc207c98fAnton Korobeynikov  unsigned MemoryLimit;
57c3e6859d8dc9014fee8023497153add9a2148f22Jeffrey Yasskin  bool UseValgrind;
58640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
59aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner  // FIXME: sort out public/private distinctions...
6006905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  friend class ReducePassList;
615073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  friend class ReduceMisCodegenFunctions;
625073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
63afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerpublic:
647f99f74b7fc298dad4c61c15b064dc951d2b3cbbRafael Espindola  BugDriver(const char *toolname, bool find_bugs,
65c3e6859d8dc9014fee8023497153add9a2148f22Jeffrey Yasskin            unsigned timeout, unsigned memlimit, bool use_valgrind,
66c3e6859d8dc9014fee8023497153add9a2148f22Jeffrey Yasskin            LLVMContext& ctxt);
67c1dc0679706f7538cd17169b920967c54661e5b6Jeffrey Yasskin  ~BugDriver();
68218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner
69197f728d49fa0cc0baa5aadb2b905fbd8c22a81eDan Gohman  const char *getToolName() const { return ToolName; }
70afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
715d8cace94a71169ce8493baa7f3305a27fe0cd84Rafael Espindola  LLVMContext& getContext() const { return Context; }
728b477ed579794ba6d76915d56b3f448a7dd20120Owen Anderson
73afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  // Set up methods... these methods are used to copy information about the
74afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  // command line arguments into instance variables of BugDriver.
75afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  //
76afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool addSources(const std::vector<std::string> &FileNames);
778261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  void addPass(std::string p) { PassesToRun.push_back(p); }
788261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  void setPassesToRun(const std::vector<std::string> &PTR) {
799c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner    PassesToRun = PTR;
809c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner  }
818261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  const std::vector<std::string> &getPassesToRun() const {
82efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    return PassesToRun;
83efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  }
84afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
85afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// run - The top level method that is invoked after all of the instance
86c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer  /// variables are set up from command line arguments. The \p as_child argument
87c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer  /// indicates whether the driver is to run in parent mode or child mode.
88afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
8922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  bool run(std::string &ErrMsg);
90afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
91025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// debugOptimizerCrash - This method is called when some optimizer pass
92025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// crashes on input.  It attempts to prune down the testcase to something
93025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// reasonable, and figure out exactly which pass is crashing.
94afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
956a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  bool debugOptimizerCrash(const std::string &ID = "passes");
963da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
97025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// debugCodeGeneratorCrash - This method is called when the code generator
98025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// crashes on an input.  It attempts to reduce the input as much as possible
99025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// while still causing the code generator to crash.
10022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  bool debugCodeGeneratorCrash(std::string &Error);
101afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
102afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// debugMiscompilation - This method is used when the passes selected are not
103afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// crashing, but the generated output is semantically different from the
104afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// input.
10522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  void debugMiscompilation(std::string *Error);
106afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
107218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// debugPassMiscompilation - This method is called when the specified pass
108218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// miscompiles Program as input.  It tries to reduce the testcase to
109218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// something that smaller that still miscompiles the program.
110218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// ReferenceOutput contains the filename of the file containing the output we
111218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// are to match.
112218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  ///
1138be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  bool debugPassMiscompilation(const PassInfo *ThePass,
114ea3e5e56fdc56e5c2ddafb36eab26676e137dfa0Jeff Cohen                               const std::string &ReferenceOutput);
115218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner
1165073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  /// compileSharedObject - This method creates a SharedObject from a given
1178ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// BitcodeFile for debugging a code generator.
118a0f5b15e1eb8642d92b3141a6b88a5729ea979dcChris Lattner  ///
11922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  std::string compileSharedObject(const std::string &BitcodeFile,
12022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                                  std::string &Error);
1215073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
1225073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  /// debugCodeGenerator - This method narrows down a module to a function or
1235073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  /// set of functions, using the CBE as a ``safe'' code generator for other
1245073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  /// functions that are not under consideration.
12522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  bool debugCodeGenerator(std::string *Error);
1265073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
127fe04db890b87d9ac4c4a607e6bd0035e8cc2ad6cMisha Brukman  /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
128fe04db890b87d9ac4c4a607e6bd0035e8cc2ad6cMisha Brukman  ///
12991eabc13d3a456cc4b387d3d7fdb041d976732c7Misha Brukman  bool isExecutingJIT();
13091eabc13d3a456cc4b387d3d7fdb041d976732c7Misha Brukman
13106905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// runPasses - Run all of the passes in the "PassesToRun" list, discard the
13206905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// output, and return true if any of the passes crashed.
1335d8cace94a71169ce8493baa7f3305a27fe0cd84Rafael Espindola  bool runPasses(Module *M) const {
1345d8cace94a71169ce8493baa7f3305a27fe0cd84Rafael Espindola    return runPasses(M, PassesToRun);
13506905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  }
13606905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner
137efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *getProgram() const { return Program; }
138efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
139efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// swapProgramIn - Set the current module to the specified module, returning
140efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// the old one.
141efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *swapProgramIn(Module *M) {
142efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    Module *OldProgram = Program;
143efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    Program = M;
144efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    return OldProgram;
145efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  }
14606905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner
14770ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  AbstractInterpreter *switchToSafeInterpreter() {
148a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    AbstractInterpreter *Old = Interpreter;
14970ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    Interpreter = (AbstractInterpreter*)SafeInterpreter;
150a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    return Old;
151a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
152a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
153a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  void switchToInterpreter(AbstractInterpreter *AI) {
154a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    Interpreter = AI;
155a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
1563da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
15706905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// setNewProgram - If we reduce or update the program somehow, call this
15806905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// method to update bugdriver with it.  This deletes the old module and sets
15906905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// the specified one as the current program.
16006905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  void setNewProgram(Module *M);
16106905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner
16222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// compileProgram - Try to compile the specified module, returning false and
16322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// setting Error if an error occurs.  This is used for code generation
16422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// crash testing.
165ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner  ///
166248d1c65f1ce5bc04b892998b2c2061e6a5f8e1cRafael Espindola  void compileProgram(Module *M, std::string *Error) const;
167ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner
168a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// executeProgram - This method runs "Program", capturing the output of the
169ac15da67055a33ce418be23ce7681358b3093aebNick Lewycky  /// program to a file.  A recommended filename may be optionally specified.
170a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  ///
17110757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola  std::string executeProgram(const Module *Program,
17210757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola                             std::string OutputFilename,
17322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                             std::string Bitcode,
17422ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                             const std::string &SharedObjects,
17522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                             AbstractInterpreter *AI,
17613793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola                             std::string *Error) const;
177a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner
17870ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  /// executeProgramSafely - Used to create reference output with the "safe"
179a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// backend, if reference output is not provided.  If there is a problem with
18022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// the code generator (e.g., llc crashes), this will return false and set
18122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// Error.
182a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  ///
18310757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola  std::string executeProgramSafely(const Module *Program,
18413793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola                                   std::string OutputFile,
18513793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola                                   std::string *Error) const;
186a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner
1876a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// createReferenceFile - calls compileProgram and then records the output
1886a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// into ReferenceOutputFile. Returns true if reference file created, false
1896a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
1906a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// this function.
1916a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  ///
192c0431fe1ca52c5d159c604957f337aa3eb1ec3d3Chris Lattner  bool createReferenceFile(Module *M, const std::string &Filename
193c0431fe1ca52c5d159c604957f337aa3eb1ec3d3Chris Lattner                                            = "bugpoint.reference.out");
1946a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins
195a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// diffProgram - This method executes the specified module and diffs the
196a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// output against the file specified by ReferenceOutputFile.  If the output
19722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// is different, 1 is returned.  If there is a problem with the code
19822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// generator (e.g., llc crashes), this will return -1 and set Error.
199a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  ///
20010757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola  bool diffProgram(const Module *Program,
20110757dd8e1a66128b205bd04797c8aed0cb7a1bdRafael Espindola                   const std::string &BitcodeFile = "",
202a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner                   const std::string &SharedObj = "",
20322ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                   bool RemoveBitcode = false,
20413793264e7cbf58e3b7b0cff3baac8e0b7a11a9dRafael Espindola                   std::string *Error = 0) const;
20522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky
206bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola  /// EmitProgressBitcode - This function is used to output M to a file named
207bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola  /// "bugpoint-ID.bc".
2080cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  ///
209bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola  void EmitProgressBitcode(const Module *M, const std::string &ID,
210ca356afe09454b3378165ded4eda294bd6341428Rafael Espindola                           bool NoFlyer = false) const;
2110cc8807029f577996a442b96d24c3346ed6de091Chris Lattner
2120cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// deleteInstructionFromProgram - This method clones the current Program and
2130cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// deletes the specified instruction from the cloned module.  It then runs a
2140cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
2150cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// which depends on the value.  The modified module is then returned.
2160cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  ///
2170cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp)
2180cc8807029f577996a442b96d24c3346ed6de091Chris Lattner    const;
2190cc8807029f577996a442b96d24c3346ed6de091Chris Lattner
2200cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// performFinalCleanups - This method clones the current Program and performs
2210cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// a series of cleanups intended to get rid of extra cruft on the module.  If
2220cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// the MayModifySemantics argument is true, then the cleanups is allowed to
2230cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// modify how the code behaves.
2240cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  ///
2250cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
2260cc8807029f577996a442b96d24c3346ed6de091Chris Lattner
2277546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  /// ExtractLoop - Given a module, extract up to one loop from it into a new
2287546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  /// function.  This returns null if there are no extractable loops in the
2297546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  /// program or if the loop extractor crashes.
2307546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  Module *ExtractLoop(Module *M);
2317546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
2325e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
2335e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// into their own functions.  The only detail is that M is actually a module
2345e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// cloned from the one the BBs are in, so some mapping needs to be performed.
2355e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// If this operation fails for some reason (ie the implementation is buggy),
2365e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// this function should return null, otherwise it returns a new Module.
2375e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  Module *ExtractMappedBlocksFromModule(const std::vector<BasicBlock*> &BBs,
2385e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                                        Module *M);
2395e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2403b6441e1050a9a2a55c79f58513191b3195fdaf7Chris Lattner  /// runPassesOn - Carefully run the specified set of pass on the specified
2413b6441e1050a9a2a55c79f58513191b3195fdaf7Chris Lattner  /// module, returning the transformed module on success, or a null pointer on
2420a002569003fd23a4e117fe54a9bb8a6673b86daChris Lattner  /// failure.  If AutoDebugCrashes is set to true, then bugpoint will
2430a002569003fd23a4e117fe54a9bb8a6673b86daChris Lattner  /// automatically attempt to track down a crashing pass if one exists, and
2440a002569003fd23a4e117fe54a9bb8a6673b86daChris Lattner  /// this method will never return null.
2458261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  Module *runPassesOn(Module *M, const std::vector<std::string> &Passes,
2466fa98b13206583e6eb90b8304758b35548914944Nick Lewycky                      bool AutoDebugCrashes = false, unsigned NumExtraArgs = 0,
2476fa98b13206583e6eb90b8304758b35548914944Nick Lewycky                      const char * const *ExtraArgs = NULL);
2483b6441e1050a9a2a55c79f58513191b3195fdaf7Chris Lattner
2498ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// runPasses - Run the specified passes on Program, outputting a bitcode
250afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// file and writting the filename into OutputFile if successful.  If the
251afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// optimizations fail for some reason (optimizer crashes), return true,
2528ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// otherwise return false.  If DeleteOutput is set to true, the bitcode is
253afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// deleted on success, and the filename string is undefined.  This prints to
254ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman  /// outs() a single line message indicating whether compilation was successful
2556fa98b13206583e6eb90b8304758b35548914944Nick Lewycky  /// or failed, unless Quiet is set.  ExtraArgs specifies additional arguments
2566fa98b13206583e6eb90b8304758b35548914944Nick Lewycky  /// to pass to the child bugpoint instance.
257afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
258ca356afe09454b3378165ded4eda294bd6341428Rafael Espindola  bool runPasses(Module *Program,
2598261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola                 const std::vector<std::string> &PassesToRun,
260218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner                 std::string &OutputFilename, bool DeleteOutput = false,
2616fa98b13206583e6eb90b8304758b35548914944Nick Lewycky                 bool Quiet = false, unsigned NumExtraArgs = 0,
2626fa98b13206583e6eb90b8304758b35548914944Nick Lewycky                 const char * const *ExtraArgs = NULL) const;
2636a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins
2646a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// runManyPasses - Take the specified pass list and create different
2656a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// combinations of passes to compile the program with. Compile the program with
2666a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// each set and mark test to see if it compiled correctly. If the passes
2676a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// compiled correctly output nothing and rearrange the passes into a new order.
2686a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// If the passes did not compile correctly, output the command required to
2696a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// recreate the failure. This returns true if a compiler error is found.
2706a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  ///
2718261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  bool runManyPasses(const std::vector<std::string> &AllPasses,
2723472766f9eb7d66f234c390ce1b3a8b76f0ee9ceDuncan Sands                     std::string &ErrMsg);
27311b8cd197a740bc9af2f27cb88d535c4be2cdd0eChris Lattner
274efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// writeProgramToFile - This writes the current "Program" to the named
2758ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// bitcode file.  If an error occurs, true is returned.
276efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  ///
277bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola  bool writeProgramToFile(const std::string &Filename, const Module *M) const;
278afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
27911b8cd197a740bc9af2f27cb88d535c4be2cdd0eChris Lattnerprivate:
280afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// runPasses - Just like the method above, but this just returns true or
281afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// false indicating whether or not the optimizer crashed on the specified
282afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// input (true = crashed).
283afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
2845d8cace94a71169ce8493baa7f3305a27fe0cd84Rafael Espindola  bool runPasses(Module *M,
2858261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola                 const std::vector<std::string> &PassesToRun,
286afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner                 bool DeleteOutput = true) const {
287afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner    std::string Filename;
2885d8cace94a71169ce8493baa7f3305a27fe0cd84Rafael Espindola    return runPasses(M, PassesToRun, Filename, DeleteOutput);
289afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  }
290afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
291218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// initializeExecutionEnvironment - This method is used to set up the
292218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// environment for executing LLVM programs.
293218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  ///
294218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  bool initializeExecutionEnvironment();
295afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner};
296afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
2978ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif/// ParseInputFile - Given a bitcode or assembly input filename, parse and
298efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// return it, or return null if not possible.
299efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner///
30031895e73591d3c9ceae731a1274c8f56194b9616Owen AndersonModule *ParseInputFile(const std::string &InputFilename,
3014434ed44c45c87a72b7a0bf2f91211f895022b91Owen Anderson                       LLVMContext& ctxt);
302efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
303efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
304640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner/// getPassesString - Turn a list of passes into a string which indicates the
305640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner/// command line options that must be passed to add the passes.
306640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner///
3078261dfed05e32302469ef707cc881fed2c31f85fRafael Espindolastd::string getPassesString(const std::vector<std::string> &Passes);
308640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
309efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// PrintFunctionList - prints out list of problematic functions
310efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner///
311efdc0b505712d1ca4460def27e51c430f033d58dChris Lattnervoid PrintFunctionList(const std::vector<Function*> &Funcs);
312efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
3134e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendling/// PrintGlobalVariableList - prints out list of problematic global variables
3144e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendling///
3154e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendlingvoid PrintGlobalVariableList(const std::vector<GlobalVariable*> &GVs);
3164e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendling
317aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
318aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner// blocks, making it external.
319aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner//
320aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattnervoid DeleteFunctionBody(Function *F);
321aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner
322be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
323be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// module, split the functions OUT of the specified module, and place them in
324be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// the new module.
325d50330cd02b00c8e3de40e8544c45701b9891d87Dan GohmanModule *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F,
326e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel                                  ValueMap<const Value*, Value*> &VMap);
327be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
328d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
329d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
330afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#endif
331