BugDriver.h revision c3e6859d8dc9014fee8023497153add9a2148f22
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
19d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman#include "llvm/ADT/DenseMap.h"
20afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include <vector>
21afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include <string>
225073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman
23d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
25d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohmanclass Value;
26afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerclass 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
50afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  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);
68218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner
69197f728d49fa0cc0baa5aadb2b905fbd8c22a81eDan Gohman  const char *getToolName() const { return ToolName; }
70afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
714434ed44c45c87a72b7a0bf2f91211f895022b91Owen Anderson  LLVMContext& getContext() { 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);
77afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  template<class It>
78afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
799c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner  void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
809c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner    PassesToRun = PTR;
819c6cfe1bffd37f29a265457b7515839c445b3e6aChris Lattner  }
82efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  const std::vector<const PassInfo*> &getPassesToRun() const {
83efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    return PassesToRun;
84efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  }
85afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
86afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// run - The top level method that is invoked after all of the instance
87c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer  /// variables are set up from command line arguments. The \p as_child argument
88c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer  /// indicates whether the driver is to run in parent mode or child mode.
89afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
90afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool run();
91afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
92025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// debugOptimizerCrash - This method is called when some optimizer pass
93025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// crashes on input.  It attempts to prune down the testcase to something
94025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// reasonable, and figure out exactly which pass is crashing.
95afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
966a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  bool debugOptimizerCrash(const std::string &ID = "passes");
973da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
98025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// debugCodeGeneratorCrash - This method is called when the code generator
99025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// crashes on an input.  It attempts to reduce the input as much as possible
100025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  /// while still causing the code generator to crash.
101025262692a6710de29a48e2b3905672cd12d13d2Chris Lattner  bool debugCodeGeneratorCrash();
102afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
103afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// debugMiscompilation - This method is used when the passes selected are not
104afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// crashing, but the generated output is semantically different from the
105afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// input.
106afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool debugMiscompilation();
107afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
108218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// debugPassMiscompilation - This method is called when the specified pass
109218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// miscompiles Program as input.  It tries to reduce the testcase to
110218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// something that smaller that still miscompiles the program.
111218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// ReferenceOutput contains the filename of the file containing the output we
112218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// are to match.
113218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  ///
114218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  bool debugPassMiscompilation(const PassInfo *ThePass,
115ea3e5e56fdc56e5c2ddafb36eab26676e137dfa0Jeff Cohen                               const std::string &ReferenceOutput);
116218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner
1175073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  /// compileSharedObject - This method creates a SharedObject from a given
1188ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// BitcodeFile for debugging a code generator.
119a0f5b15e1eb8642d92b3141a6b88a5729ea979dcChris Lattner  ///
1208ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  std::string compileSharedObject(const std::string &BitcodeFile);
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.
1255073336cd4da5df4ae13a167582d1dc90f32e4e0Misha Brukman  bool debugCodeGenerator();
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.
13306905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  bool runPasses(Module *M = 0) {
13406905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner    if (M == 0) M = Program;
13506905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner    std::swap(M, Program);
13606905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner    bool Result = runPasses(PassesToRun);
13706905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner    std::swap(M, Program);
13806905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner    return Result;
13906905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  }
14006905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner
141efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *getProgram() const { return Program; }
142efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
143efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// swapProgramIn - Set the current module to the specified module, returning
144efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// the old one.
145efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  Module *swapProgramIn(Module *M) {
146efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    Module *OldProgram = Program;
147efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    Program = M;
148efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner    return OldProgram;
149efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  }
15006905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner
15170ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  AbstractInterpreter *switchToSafeInterpreter() {
152a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    AbstractInterpreter *Old = Interpreter;
15370ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman    Interpreter = (AbstractInterpreter*)SafeInterpreter;
154a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    return Old;
155a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
156a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner
157a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  void switchToInterpreter(AbstractInterpreter *AI) {
158a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner    Interpreter = AI;
159a57d86b436549503a7f96c5266444e022bdbaf55Chris Lattner  }
1603da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
16106905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// setNewProgram - If we reduce or update the program somehow, call this
16206905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// method to update bugdriver with it.  This deletes the old module and sets
16306905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  /// the specified one as the current program.
16406905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner  void setNewProgram(Module *M);
16506905db7d2a2b83c1b3236d5552629ada2d8d56dChris Lattner
166ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner  /// compileProgram - Try to compile the specified module, throwing an
167ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner  /// exception if an error occurs, or returning normally if not.  This is used
168ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner  /// for code generation crash testing.
169ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner  ///
170ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner  void compileProgram(Module *M);
171ea9212ca964ff6587227016f86a44160e586a4c8Chris Lattner
172a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// executeProgram - This method runs "Program", capturing the output of the
173a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// program to a file, returning the filename of the file.  A recommended
174a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// filename may be optionally specified.  If there is a problem with the code
175a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// generator (e.g., llc crashes), this will throw an exception.
176a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  ///
177a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  std::string executeProgram(std::string RequestedOutputFilename = "",
1788ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif                             std::string Bitcode = "",
179a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner                             const std::string &SharedObjects = "",
180a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner                             AbstractInterpreter *AI = 0,
181a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner                             bool *ProgramExitedNonzero = 0);
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
185a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// the code generator (e.g., llc crashes), this will throw an exception.
186a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  ///
18770ef449741da8b1ef035e04a55958652a0200ba1Dan Gohman  std::string executeProgramSafely(std::string OutputFile = "");
188a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner
1896a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// createReferenceFile - calls compileProgram and then records the output
1906a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// into ReferenceOutputFile. Returns true if reference file created, false
1916a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
1926a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// this function.
1936a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  ///
194c0431fe1ca52c5d159c604957f337aa3eb1ec3d3Chris Lattner  bool createReferenceFile(Module *M, const std::string &Filename
195c0431fe1ca52c5d159c604957f337aa3eb1ec3d3Chris Lattner                                            = "bugpoint.reference.out");
1966a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins
197a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// diffProgram - This method executes the specified module and diffs the
198a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// output against the file specified by ReferenceOutputFile.  If the output
199a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// is different, true is returned.  If there is a problem with the code
200a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  /// generator (e.g., llc crashes), this will throw an exception.
201a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner  ///
2028ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  bool diffProgram(const std::string &BitcodeFile = "",
203a36ec88203a979a686b0ed1d49e0d7039194f040Chris Lattner                   const std::string &SharedObj = "",
2048ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif                   bool RemoveBitcode = false);
2056a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins
2068ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// EmitProgressBitcode - This function is used to output the current Program
2070cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// to a file named "bugpoint-ID.bc".
2080cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  ///
2098ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  void EmitProgressBitcode(const std::string &ID, bool NoFlyer = false);
2100cc8807029f577996a442b96d24c3346ed6de091Chris Lattner
2110cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// deleteInstructionFromProgram - This method clones the current Program and
2120cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// deletes the specified instruction from the cloned module.  It then runs a
2130cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
2140cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// which depends on the value.  The modified module is then returned.
2150cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  ///
2160cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp)
2170cc8807029f577996a442b96d24c3346ed6de091Chris Lattner    const;
2180cc8807029f577996a442b96d24c3346ed6de091Chris Lattner
2190cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// performFinalCleanups - This method clones the current Program and performs
2200cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// a series of cleanups intended to get rid of extra cruft on the module.  If
2210cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// the MayModifySemantics argument is true, then the cleanups is allowed to
2220cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  /// modify how the code behaves.
2230cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  ///
2240cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
2250cc8807029f577996a442b96d24c3346ed6de091Chris Lattner
2267546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  /// ExtractLoop - Given a module, extract up to one loop from it into a new
2277546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  /// function.  This returns null if there are no extractable loops in the
2287546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  /// program or if the loop extractor crashes.
2297546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  Module *ExtractLoop(Module *M);
2307546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
2315e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
2325e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// into their own functions.  The only detail is that M is actually a module
2335e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// cloned from the one the BBs are in, so some mapping needs to be performed.
2345e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// If this operation fails for some reason (ie the implementation is buggy),
2355e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// this function should return null, otherwise it returns a new Module.
2365e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  Module *ExtractMappedBlocksFromModule(const std::vector<BasicBlock*> &BBs,
2375e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                                        Module *M);
2385e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2393b6441e1050a9a2a55c79f58513191b3195fdaf7Chris Lattner  /// runPassesOn - Carefully run the specified set of pass on the specified
2403b6441e1050a9a2a55c79f58513191b3195fdaf7Chris Lattner  /// module, returning the transformed module on success, or a null pointer on
2410a002569003fd23a4e117fe54a9bb8a6673b86daChris Lattner  /// failure.  If AutoDebugCrashes is set to true, then bugpoint will
2420a002569003fd23a4e117fe54a9bb8a6673b86daChris Lattner  /// automatically attempt to track down a crashing pass if one exists, and
2430a002569003fd23a4e117fe54a9bb8a6673b86daChris Lattner  /// this method will never return null.
2440a002569003fd23a4e117fe54a9bb8a6673b86daChris Lattner  Module *runPassesOn(Module *M, const std::vector<const PassInfo*> &Passes,
2456fa98b13206583e6eb90b8304758b35548914944Nick Lewycky                      bool AutoDebugCrashes = false, unsigned NumExtraArgs = 0,
2466fa98b13206583e6eb90b8304758b35548914944Nick Lewycky                      const char * const *ExtraArgs = NULL);
2473b6441e1050a9a2a55c79f58513191b3195fdaf7Chris Lattner
2488ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// runPasses - Run the specified passes on Program, outputting a bitcode
249afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// file and writting the filename into OutputFile if successful.  If the
250afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// optimizations fail for some reason (optimizer crashes), return true,
2518ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// otherwise return false.  If DeleteOutput is set to true, the bitcode is
252afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// deleted on success, and the filename string is undefined.  This prints to
253ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman  /// outs() a single line message indicating whether compilation was successful
2546fa98b13206583e6eb90b8304758b35548914944Nick Lewycky  /// or failed, unless Quiet is set.  ExtraArgs specifies additional arguments
2556fa98b13206583e6eb90b8304758b35548914944Nick Lewycky  /// to pass to the child bugpoint instance.
256afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
257afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
258218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner                 std::string &OutputFilename, bool DeleteOutput = false,
2596fa98b13206583e6eb90b8304758b35548914944Nick Lewycky                 bool Quiet = false, unsigned NumExtraArgs = 0,
2606fa98b13206583e6eb90b8304758b35548914944Nick Lewycky                 const char * const *ExtraArgs = NULL) const;
2616a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins
2626a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// runManyPasses - Take the specified pass list and create different
2636a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// combinations of passes to compile the program with. Compile the program with
2646a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// each set and mark test to see if it compiled correctly. If the passes
2656a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// compiled correctly output nothing and rearrange the passes into a new order.
2666a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// If the passes did not compile correctly, output the command required to
2676a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  /// recreate the failure. This returns true if a compiler error is found.
2686a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  ///
2696a3f31cb707972ebde1e45a61fa8f5bcff132ebaPatrick Jenkins  bool runManyPasses(const std::vector<const PassInfo*> &AllPasses);
27011b8cd197a740bc9af2f27cb88d535c4be2cdd0eChris Lattner
271efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// writeProgramToFile - This writes the current "Program" to the named
2728ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif  /// bitcode file.  If an error occurs, true is returned.
273efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  ///
274efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
275afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
27611b8cd197a740bc9af2f27cb88d535c4be2cdd0eChris Lattnerprivate:
277afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// runPasses - Just like the method above, but this just returns true or
278afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// false indicating whether or not the optimizer crashed on the specified
279afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// input (true = crashed).
280afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
281afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
282afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner                 bool DeleteOutput = true) const {
283afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner    std::string Filename;
284afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner    return runPasses(PassesToRun, Filename, DeleteOutput);
285afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  }
286afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
287c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer  /// runAsChild - The actual "runPasses" guts that runs in a child process.
288c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer  int runPassesAsChild(const std::vector<const PassInfo*> &PassesToRun);
289c4bb052ecccfafa0ffa928d0b061db35734ee2eeReid Spencer
290218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// initializeExecutionEnvironment - This method is used to set up the
291218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// environment for executing LLVM programs.
292218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  ///
293218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  bool initializeExecutionEnvironment();
294afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner};
295afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
2968ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif/// ParseInputFile - Given a bitcode or assembly input filename, parse and
297efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// return it, or return null if not possible.
298efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner///
29931895e73591d3c9ceae731a1274c8f56194b9616Owen AndersonModule *ParseInputFile(const std::string &InputFilename,
3004434ed44c45c87a72b7a0bf2f91211f895022b91Owen Anderson                       LLVMContext& ctxt);
301efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
302efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
303640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner/// getPassesString - Turn a list of passes into a string which indicates the
304640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner/// command line options that must be passed to add the passes.
305640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner///
306640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattnerstd::string getPassesString(const std::vector<const PassInfo*> &Passes);
307640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
308efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner/// PrintFunctionList - prints out list of problematic functions
309efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner///
310efdc0b505712d1ca4460def27e51c430f033d58dChris Lattnervoid PrintFunctionList(const std::vector<Function*> &Funcs);
311efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner
3124e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendling/// PrintGlobalVariableList - prints out list of problematic global variables
3134e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendling///
3144e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendlingvoid PrintGlobalVariableList(const std::vector<GlobalVariable*> &GVs);
3154e3be89cb5cde6e2df294c64db3bc28133b67594Bill Wendling
316aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
317aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner// blocks, making it external.
318aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner//
319aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattnervoid DeleteFunctionBody(Function *F);
320aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner
321be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
322be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// module, split the functions OUT of the specified module, and place them in
323be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// the new module.
324d50330cd02b00c8e3de40e8544c45701b9891d87Dan GohmanModule *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F,
325d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman                                  DenseMap<const Value*, Value*> &ValueMap);
326be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
327d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
328d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
329afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#endif
330