dumpNode() does not need to print MachineInstrs.
[oota-llvm.git] / tools / opt / opt.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM Modular Optimizer Utility: opt
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 'program modified' message"));
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 << argv[0] << ": 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 << argv[0] << ": error opening '" << OutputFilename
80            << "': file exists!\n"
81            << "Use -f command line argument to force output\n";
82       return 1;
83     }
84     Out = new std::ofstream(OutputFilename.c_str());
85
86     if (!Out->good()) {
87       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
88       return 1;
89     }
90
91     // Make sure that the Output file gets unlink'd from the disk if we get a
92     // SIGINT
93     RemoveFileOnSignal(OutputFilename);
94   }
95
96   // Create a PassManager to hold and optimize the collection of passes we are
97   // about to build...
98   //
99   PassManager Passes;
100
101   // Create a new optimization pass for each one specified on the command line
102   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
103     const PassInfo *Opt = OptimizationList[i];
104     
105     if (Opt->getNormalCtor())
106       Passes.add(Opt->getNormalCtor()());
107     else if (Opt->getDataCtor())
108       Passes.add(Opt->getDataCtor()(TD));  // Pass dummy target data...
109     else
110       cerr << argv[0] << ": cannot create pass: " << Opt->getPassName() << "\n";
111
112     if (PrintEachXForm)
113       Passes.add(new PrintModulePass(&cerr));
114   }
115
116   // Check that the module is well formed on completion of optimization
117   Passes.add(createVerifierPass());
118
119   // Write bytecode out to disk or cout as the last step...
120   Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
121
122   // Now that we have all of the passes ready, run them.
123   if (Passes.run(*M.get()) && !Quiet)
124     cerr << "Program modified.\n";
125
126   return 0;
127 }