BugDriver.h revision 8be3291f5942e3ae4a5d66c480e7aabe2f771031
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
508be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  std::vector<const PassInfo*> PassesToRun;
51218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  AbstractInterpreter *Interpreter;   // How to run the program
5270ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  AbstractInterpreter *SafeInterpreter;  // To generate reference output, etc.
53a259c9be2acc9528ec7feb3cfd51dcde36d87bb3Misha Brukman  GCC *gcc;
54c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer  bool run_as_child;
556a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  bool run_find_bugs;
569686ae7f4ea5f19ce77e31e64e0916db41a82662Chris Lattner  unsigned Timeout;
579ba8a76f8baaa1092d60ccfbc04e7efdc207c98fAnton Korobeynikov  unsigned MemoryLimit;
58c3e6859d8dc9014fee8023497153add9a2148f22Jeffrey Yasskin  bool UseValgrind;
59640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
60aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner  // FIXME: sort out public/private distinctions...
6106905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  friend class ReducePassList;
625073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  friend class ReduceMisCodegenFunctions;
635073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
64afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerpublic:
656a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  BugDriver(const char *toolname, bool as_child, bool find_bugs,
66c3e6859d8dc9014fee8023497153add9a2148f22Jeffrey Yasskin            unsigned timeout, unsigned memlimit, bool use_valgrind,
67c3e6859d8dc9014fee8023497153add9a2148f22Jeffrey Yasskin            LLVMContext& ctxt);
68c1dc0679706f7538cd17169b920967c54661e5b6Jeffrey Yasskin  ~BugDriver();
69218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner
70197f728d49fa0cc0baa5aadb2b905fbd8c22a81eDan Gohman  const char *getToolName() const { return ToolName; }
71afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
724434ed44c45c87a72b7a0bf2f91211f895022b91Owen Anderson  LLVMContext& getContext() { return Context; }
738b477ed579794ba6d76915d56b3f448a7dd20120Owen Anderson
74afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  // Set up methods... these methods are used to copy information about the
75afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  // command line arguments into instance variables of BugDriver.
76afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  //
77afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool addSources(const std::vector<std::string> &FileNames);
78afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  template<class It>
79afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
808be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
819c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner    PassesToRun = PTR;
829c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner  }
838be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  const std::vector<const PassInfo*> &getPassesToRun() const {
84efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    return PassesToRun;
85efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  }
86afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
87afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// run - The top level method that is invoked after all of the instance
88c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer  /// variables are set up from command line arguments. The \p as_child argument
89c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer  /// indicates whether the driver is to run in parent mode or child mode.
90afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
9122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  bool run(std::string &ErrMsg);
92afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
93025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// debugOptimizerCrash - This method is called when some optimizer pass
94025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// crashes on input.  It attempts to prune down the testcase to something
95025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// reasonable, and figure out exactly which pass is crashing.
96afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
976a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  bool debugOptimizerCrash(const std::string &ID = "passes");
983da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
99025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// debugCodeGeneratorCrash - This method is called when the code generator
100025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// crashes on an input.  It attempts to reduce the input as much as possible
101025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// while still causing the code generator to crash.
10222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  bool debugCodeGeneratorCrash(std::string &Error);
103afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
104afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// debugMiscompilation - This method is used when the passes selected are not
105afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// crashing, but the generated output is semantically different from the
106afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// input.
10722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  void debugMiscompilation(std::string *Error);
108afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
109218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// debugPassMiscompilation - This method is called when the specified pass
110218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// miscompiles Program as input.  It tries to reduce the testcase to
111218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// something that smaller that still miscompiles the program.
112218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// ReferenceOutput contains the filename of the file containing the output we
113218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// are to match.
114218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  ///
1158be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  bool debugPassMiscompilation(const PassInfo *ThePass,
116ea3e5e56fdc56e5c2ddafb36eab26676e137dfa0Jeff Cohen                               const std::string &ReferenceOutput);
117218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner
1185073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  /// compileSharedObject - This method creates a SharedObject from a given
1198ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// BitcodeFile for debugging a code generator.
120a0f5b15e1eb8642d92b3141a6b88a5729ea979dcChris Lattner  ///
12122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  std::string compileSharedObject(const std::string &BitcodeFile,
12222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                                  std::string &Error);
1235073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
1245073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  /// debugCodeGenerator - This method narrows down a module to a function or
1255073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  /// set of functions, using the CBE as a ``safe'' code generator for other
1265073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  /// functions that are not under consideration.
12722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  bool debugCodeGenerator(std::string *Error);
1285073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
129fe04db890b87d9ac4c4a607e6bd0035e8cc2ad6cMisha Brukman  /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
130fe04db890b87d9ac4c4a607e6bd0035e8cc2ad6cMisha Brukman  ///
13191eabc13d3a456cc4b387d3d7fdb041d976732c7Misha Brukman  bool isExecutingJIT();
13291eabc13d3a456cc4b387d3d7fdb041d976732c7Misha Brukman
13306905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// runPasses - Run all of the passes in the "PassesToRun" list, discard the
13406905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// output, and return true if any of the passes crashed.
13506905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  bool runPasses(Module *M = 0) {
13606905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner    if (M == 0) M = Program;
13706905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner    std::swap(M, Program);
13806905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner    bool Result = runPasses(PassesToRun);
13906905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner    std::swap(M, Program);
14006905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner    return Result;
14106905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  }
14206905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner
143efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *getProgram() const { return Program; }
144efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
145efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// swapProgramIn - Set the current module to the specified module, returning
146efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// the old one.
147efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *swapProgramIn(Module *M) {
148efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    Module *OldProgram = Program;
149efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    Program = M;
150efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    return OldProgram;
151efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  }
15206905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner
15370ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  AbstractInterpreter *switchToSafeInterpreter() {
154a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    AbstractInterpreter *Old = Interpreter;
15570ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    Interpreter = (AbstractInterpreter*)SafeInterpreter;
156a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    return Old;
157a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
158a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
159a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  void switchToInterpreter(AbstractInterpreter *AI) {
160a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    Interpreter = AI;
161a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
1623da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
16306905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// setNewProgram - If we reduce or update the program somehow, call this
16406905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// method to update bugdriver with it.  This deletes the old module and sets
16506905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// the specified one as the current program.
16606905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  void setNewProgram(Module *M);
16706905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner
16822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// compileProgram - Try to compile the specified module, returning false and
16922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// setting Error if an error occurs.  This is used for code generation
17022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// crash testing.
171ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner  ///
17222ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  void compileProgram(Module *M, std::string *Error);
173ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner
174a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// executeProgram - This method runs "Program", capturing the output of the
175ac15da67055a33ce418be23ce7681358b3093aebNick Lewycky  /// program to a file.  A recommended filename may be optionally specified.
176a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  ///
17722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  std::string executeProgram(std::string OutputFilename,
17822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                             std::string Bitcode,
17922ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                             const std::string &SharedObjects,
18022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                             AbstractInterpreter *AI,
18122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                             std::string *Error);
182a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner
18370ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  /// executeProgramSafely - Used to create reference output with the "safe"
184a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// backend, if reference output is not provided.  If there is a problem with
18522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// the code generator (e.g., llc crashes), this will return false and set
18622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// Error.
187a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  ///
18822ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  std::string executeProgramSafely(std::string OutputFile, std::string *Error);
189a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner
1906a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// createReferenceFile - calls compileProgram and then records the output
1916a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// into ReferenceOutputFile. Returns true if reference file created, false
1926a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
1936a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// this function.
1946a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  ///
195c0431fe1ca52c5d159c604957f337aa3eb1ec3d3Chris Lattner  bool createReferenceFile(Module *M, const std::string &Filename
196c0431fe1ca52c5d159c604957f337aa3eb1ec3d3Chris Lattner                                            = "bugpoint.reference.out");
1976a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins
198a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// diffProgram - This method executes the specified module and diffs the
199a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// output against the file specified by ReferenceOutputFile.  If the output
20022ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// is different, 1 is returned.  If there is a problem with the code
20122ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky  /// generator (e.g., llc crashes), this will return -1 and set Error.
202a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  ///
2038ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  bool diffProgram(const std::string &BitcodeFile = "",
204a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner                   const std::string &SharedObj = "",
20522ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                   bool RemoveBitcode = false,
20622ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky                   std::string *Error = 0);
20722ff748712b348300e51248339b6e8cf9b59e2c6Nick Lewycky
2088ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// EmitProgressBitcode - This function is used to output the current Program
2090cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// to a file named "bugpoint-ID.bc".
2100cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  ///
2118ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  void EmitProgressBitcode(const std::string &ID, bool NoFlyer = false);
2120cc8807029f577996a442b96d24c3346ed6de091Chris Lattner
2130cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// deleteInstructionFromProgram - This method clones the current Program and
2140cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// deletes the specified instruction from the cloned module.  It then runs a
2150cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
2160cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// which depends on the value.  The modified module is then returned.
2170cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  ///
2180cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp)
2190cc8807029f577996a442b96d24c3346ed6de091Chris Lattner    const;
2200cc8807029f577996a442b96d24c3346ed6de091Chris Lattner
2210cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// performFinalCleanups - This method clones the current Program and performs
2220cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// a series of cleanups intended to get rid of extra cruft on the module.  If
2230cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// the MayModifySemantics argument is true, then the cleanups is allowed to
2240cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// modify how the code behaves.
2250cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  ///
2260cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
2270cc8807029f577996a442b96d24c3346ed6de091Chris Lattner
2287546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  /// ExtractLoop - Given a module, extract up to one loop from it into a new
2297546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  /// function.  This returns null if there are no extractable loops in the
2307546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  /// program or if the loop extractor crashes.
2317546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  Module *ExtractLoop(Module *M);
2327546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
2335e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
2345e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// into their own functions.  The only detail is that M is actually a module
2355e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// cloned from the one the BBs are in, so some mapping needs to be performed.
2365e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// If this operation fails for some reason (ie the implementation is buggy),
2375e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// this function should return null, otherwise it returns a new Module.
2385e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  Module *ExtractMappedBlocksFromModule(const std::vector<BasicBlock*> &BBs,
2395e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                                        Module *M);
2405e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2413b6441e1050a9a2a55c79f58513191b3195fdaf7Chris Lattner  /// runPassesOn - Carefully run the specified set of pass on the specified
2423b6441e1050a9a2a55c79f58513191b3195fdaf7Chris Lattner  /// module, returning the transformed module on success, or a null pointer on
2430a002569003fd23a4e117fe54a9bb8a6673b86daChris Lattner  /// failure.  If AutoDebugCrashes is set to true, then bugpoint will
2440a002569003fd23a4e117fe54a9bb8a6673b86daChris Lattner  /// automatically attempt to track down a crashing pass if one exists, and
2450a002569003fd23a4e117fe54a9bb8a6673b86daChris Lattner  /// this method will never return null.
2468be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  Module *runPassesOn(Module *M, const std::vector<const PassInfo*> &Passes,
2476fa98b13206583e6eb90b8304758b35548914944Nick Lewycky                      bool AutoDebugCrashes = false, unsigned NumExtraArgs = 0,
2486fa98b13206583e6eb90b8304758b35548914944Nick Lewycky                      const char * const *ExtraArgs = NULL);
2493b6441e1050a9a2a55c79f58513191b3195fdaf7Chris Lattner
2508ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// runPasses - Run the specified passes on Program, outputting a bitcode
251afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// file and writting the filename into OutputFile if successful.  If the
252afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// optimizations fail for some reason (optimizer crashes), return true,
2538ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// otherwise return false.  If DeleteOutput is set to true, the bitcode is
254afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// deleted on success, and the filename string is undefined.  This prints to
255ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman  /// outs() a single line message indicating whether compilation was successful
2566fa98b13206583e6eb90b8304758b35548914944Nick Lewycky  /// or failed, unless Quiet is set.  ExtraArgs specifies additional arguments
2576fa98b13206583e6eb90b8304758b35548914944Nick Lewycky  /// to pass to the child bugpoint instance.
258afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
2598be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  bool runPasses(const std::vector<const PassInfo*> &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  ///
2718be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  bool runManyPasses(const std::vector<const PassInfo*> &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  ///
277efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  bool writeProgramToFile(const std::string &Filename, Module *M = 0) 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  ///
2848be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
285afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner                 bool DeleteOutput = true) const {
286afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner    std::string Filename;
287afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner    return runPasses(PassesToRun, Filename, DeleteOutput);
288afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  }
289afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
290c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer  /// runAsChild - The actual "runPasses" guts that runs in a child process.
2918be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Anderson  int runPassesAsChild(const std::vector<const PassInfo*> &PassesToRun);
292c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer
293218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// initializeExecutionEnvironment - This method is used to set up the
294218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// environment for executing LLVM programs.
295218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  ///
296218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  bool initializeExecutionEnvironment();
297afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner};
298afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
2998ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif/// ParseInputFile - Given a bitcode or assembly input filename, parse and
300efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// return it, or return null if not possible.
301efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner///
30231895e73591d3c9ceae731a1274c8f56194b9616Owen AndersonModule *ParseInputFile(const std::string &InputFilename,
3034434ed44c45c87a72b7a0bf2f91211f895022b91Owen Anderson                       LLVMContext& ctxt);
304efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
305efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
306640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner/// getPassesString - Turn a list of passes into a string which indicates the
307640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner/// command line options that must be passed to add the passes.
308640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner///
3098be3291f5942e3ae4a5d66c480e7aabe2f771031Owen Andersonstd::string getPassesString(const std::vector<const PassInfo*> &Passes);
310640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
311efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// PrintFunctionList - prints out list of problematic functions
312efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner///
313efdc0b505712d1ca4460def27e51c430f033d58dChris Lattnervoid PrintFunctionList(const std::vector<Function*> &Funcs);
314efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
3154e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendling/// PrintGlobalVariableList - prints out list of problematic global variables
3164e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendling///
3174e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendlingvoid PrintGlobalVariableList(const std::vector<GlobalVariable*> &GVs);
3184e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendling
319aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
320aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner// blocks, making it external.
321aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner//
322aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattnervoid DeleteFunctionBody(Function *F);
323aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner
324be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
325be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// module, split the functions OUT of the specified module, and place them in
326be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// the new module.
327d50330cd02b00c8e3de40e8544c45701b9891d87Dan GohmanModule *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F,
328e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel                                  ValueMap<const Value*, Value*> &VMap);
329be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
330d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
331d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
332afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#endif
333