Print the top 20 most frequently executed blocks. Fix sort predicate problem
[oota-llvm.git] / tools / bugpoint / OptimizerDriver.cpp
1 //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an interface that allows bugpoint to run various passes
11 // without the threat of a buggy pass corrupting bugpoint (of course, bugpoint
12 // may have its own bugs, but that's another story...).  It achieves this by
13 // forking a copy of itself and having the child process do the optimizations.
14 // If this client dies, we can always fork a new one.  :)
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "BugDriver.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/Analysis/Verifier.h"
21 #include "llvm/Bytecode/WriteBytecodePass.h"
22 #include "llvm/Target/TargetData.h"
23 #include "Support/FileUtilities.h"
24 #include <fstream>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28
29 /// writeProgramToFile - This writes the current "Program" to the named bytecode
30 /// file.  If an error occurs, true is returned.
31 ///
32 bool BugDriver::writeProgramToFile(const std::string &Filename,
33                                    Module *M) const {
34   std::ofstream Out(Filename.c_str());
35   if (!Out.good()) return true;
36   WriteBytecodeToFile(M ? M : Program, Out);
37   return false;
38 }
39
40
41 /// EmitProgressBytecode - This function is used to output the current Program
42 /// to a file named "bugpoint-ID.bc".
43 ///
44 void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) {
45   // Output the input to the current pass to a bytecode file, emit a message
46   // telling the user how to reproduce it: opt -foo blah.bc
47   //
48   std::string Filename = "bugpoint-" + ID + ".bc";
49   if (writeProgramToFile(Filename)) {
50     std::cerr <<  "Error opening file '" << Filename << "' for writing!\n";
51     return;
52   }
53
54   std::cout << "Emitted bytecode to '" << Filename << "'\n";
55   if (NoFlyer) return;
56   std::cout << "\n*** You can reproduce the problem with: ";
57
58   unsigned PassType = PassesToRun[0]->getPassType();
59   for (unsigned i = 1, e = PassesToRun.size(); i != e; ++i)
60     PassType &= PassesToRun[i]->getPassType();
61
62   if (PassType & PassInfo::Analysis)
63     std::cout << "analyze";
64   else if (PassType & PassInfo::Optimization)
65     std::cout << "opt";
66   else if (PassType & PassInfo::LLC)
67     std::cout << "llc";
68   else
69     std::cout << "bugpoint";
70   std::cout << " " << Filename << " ";
71   std::cout << getPassesString(PassesToRun) << "\n";
72 }
73
74 static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes,
75                      const std::string &OutFilename) {
76   std::ofstream OutFile(OutFilename.c_str());
77   if (!OutFile.good()) {
78     std::cerr << "Error opening bytecode file: " << OutFilename << "\n";
79     exit(1);
80   }
81
82   PassManager PM;
83   // Make sure that the appropriate target data is always used...
84   PM.add(new TargetData("bugpoint", Program));
85
86   for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
87     if (Passes[i]->getNormalCtor())
88       PM.add(Passes[i]->getNormalCtor()());
89     else
90       std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName()
91                 << "\n";
92   }
93   // Check that the module is well formed on completion of optimization
94   PM.add(createVerifierPass());
95
96   // Write bytecode out to disk as the last step...
97   PM.add(new WriteBytecodePass(&OutFile));
98
99   // Run all queued passes.
100   PM.run(*Program);
101 }
102
103 /// runPasses - Run the specified passes on Program, outputting a bytecode file
104 /// and writing the filename into OutputFile if successful.  If the
105 /// optimizations fail for some reason (optimizer crashes), return true,
106 /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
107 /// deleted on success, and the filename string is undefined.  This prints to
108 /// cout a single line message indicating whether compilation was successful or
109 /// failed.
110 ///
111 bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
112                           std::string &OutputFilename, bool DeleteOutput,
113                           bool Quiet) const{
114   std::cout << std::flush;
115   OutputFilename = getUniqueFilename("bugpoint-output.bc");
116
117   pid_t child_pid;
118   switch (child_pid = fork()) {
119   case -1:    // Error occurred
120     std::cerr << ToolName << ": Error forking!\n";
121     exit(1);
122   case 0:     // Child process runs passes.
123     RunChild(Program, Passes, OutputFilename);
124     exit(0);  // If we finish successfully, return 0!
125   default:    // Parent continues...
126     break;
127   }
128
129   // Wait for the child process to get done.
130   int Status;
131   if (wait(&Status) != child_pid) {
132     std::cerr << "Error waiting for child process!\n";
133     exit(1);
134   }
135
136   // If we are supposed to delete the bytecode file, remove it now
137   // unconditionally...  this may fail if the file was never created, but that's
138   // ok.
139   if (DeleteOutput)
140     removeFile(OutputFilename);
141
142   bool ExitedOK = WIFEXITED(Status) && WEXITSTATUS(Status) == 0;
143   
144   if (!Quiet) {
145     if (ExitedOK)
146       std::cout << "Success!\n";
147     else if (WIFEXITED(Status))
148       std::cout << "Exited with error code '" << WEXITSTATUS(Status) << "'\n";
149     else if (WIFSIGNALED(Status))
150       std::cout << "Crashed with signal #" << WTERMSIG(Status) << "\n";
151 #ifdef WCOREDUMP
152     else if (WCOREDUMP(Status))
153       std::cout << "Dumped core\n";
154 #endif
155     else
156       std::cout << "Failed for unknown reason!\n";
157   }
158
159   // Was the child successful?
160   return !ExitedOK;
161 }