Factor PassNamePArser out into llvm/Support/PassNameParser.h
[oota-llvm.git] / tools / opt / opt.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM 'OPT' UTILITY 
3 //
4 // Optimizations may be specified an arbitrary number of times on the command
5 // line, they are run in the order specified.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Module.h"
10 #include "llvm/PassManager.h"
11 #include "llvm/Bytecode/Reader.h"
12 #include "llvm/Bytecode/WriteBytecodePass.h"
13 #include "llvm/Assembly/PrintModulePass.h"
14 #include "llvm/Analysis/Verifier.h"
15 #include "llvm/Target/TargetData.h"
16 #include "llvm/Support/PassNameParser.h"
17 #include "Support/Signals.h"
18 #include <fstream>
19 #include <memory>
20 #include <algorithm>
21
22 using std::cerr;
23 using std::string;
24
25
26 // The OptimizationList is automatically populated with registered Passes by the
27 // PassNameParser.
28 //
29 static cl::list<const PassInfo*, bool,
30                 FilteredPassNameParser<PassInfo::Optimization> >
31 OptimizationList(cl::desc("Optimizations available:"));
32
33
34 // Other command line options...
35 //
36 static cl::opt<string>
37 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
38
39 static cl::opt<string>
40 OutputFilename("o", cl::desc("Override output filename"),
41                cl::value_desc("filename"));
42
43 static cl::opt<bool>
44 Force("f", cl::desc("Overwrite output files"));
45
46 static cl::opt<bool>
47 PrintEachXForm("p", cl::desc("Print module after each transformation"));
48
49 static cl::opt<bool>
50 Quiet("q", cl::desc("Don't print modifying pass names"));
51
52 static cl::alias
53 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
54
55
56 //===----------------------------------------------------------------------===//
57 // main for opt
58 //
59 int main(int argc, char **argv) {
60   cl::ParseCommandLineOptions(argc, argv,
61                               " llvm .bc -> .bc modular optimizer\n");
62
63   // FIXME: This should be parameterizable eventually for different target
64   // types...
65   TargetData TD("opt target");
66
67   // Load the input module...
68   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
69   if (M.get() == 0) {
70     cerr << "bytecode didn't read correctly.\n";
71     return 1;
72   }
73
74   // Figure out what stream we are supposed to write to...
75   std::ostream *Out = &std::cout;  // Default to printing to stdout...
76   if (OutputFilename != "") {
77     if (!Force && std::ifstream(OutputFilename.c_str())) {
78       // If force is not specified, make sure not to overwrite a file!
79       cerr << "Error opening '" << OutputFilename << "': File exists!\n"
80            << "Use -f command line argument to force output\n";
81       return 1;
82     }
83     Out = new std::ofstream(OutputFilename.c_str());
84
85     if (!Out->good()) {
86       cerr << "Error opening " << OutputFilename << "!\n";
87       return 1;
88     }
89
90     // Make sure that the Output file gets unlink'd from the disk if we get a
91     // SIGINT
92     RemoveFileOnSignal(OutputFilename);
93   }
94
95   // Create a PassManager to hold and optimize the collection of passes we are
96   // about to build...
97   //
98   PassManager Passes;
99
100   // Create a new optimization pass for each one specified on the command line
101   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
102     const PassInfo *Opt = OptimizationList[i];
103     
104     if (Opt->getNormalCtor())
105       Passes.add(Opt->getNormalCtor()());
106     else if (Opt->getDataCtor())
107       Passes.add(Opt->getDataCtor()(TD));  // Pass dummy target data...
108     else
109       cerr << "Cannot create pass: " << Opt->getPassName() << "\n";
110
111     if (PrintEachXForm)
112       Passes.add(new PrintModulePass(&cerr));
113   }
114
115   // Check that the module is well formed on completion of optimization
116   Passes.add(createVerifierPass());
117
118   // Write bytecode out to disk or cout as the last step...
119   Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
120
121   // Now that we have all of the passes ready, run them.
122   if (Passes.run(*M.get()) && !Quiet)
123     cerr << "Program modified.\n";
124
125   return 0;
126 }