de56080c2c1602ff996b0b0d2fa183284f4aa710
[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/TargetMachine.h"
16 #include "llvm/Target/TargetMachineImpls.h"
17 #include "llvm/Support/PassNameParser.h"
18 #include "Support/Signals.h"
19 #include <fstream>
20 #include <memory>
21 #include <algorithm>
22
23 using std::cerr;
24 using std::string;
25
26
27 // The OptimizationList is automatically populated with registered Passes by the
28 // PassNameParser.
29 //
30 static cl::list<const PassInfo*, bool,
31                 FilteredPassNameParser<PassInfo::Optimization> >
32 OptimizationList(cl::desc("Optimizations available:"));
33
34
35 // Other command line options...
36 //
37 static cl::opt<string>
38 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
39
40 static cl::opt<string>
41 OutputFilename("o", cl::desc("Override output filename"),
42                cl::value_desc("filename"));
43
44 static cl::opt<bool>
45 Force("f", cl::desc("Overwrite output files"));
46
47 static cl::opt<bool>
48 PrintEachXForm("p", cl::desc("Print module after each transformation"));
49
50 static cl::opt<bool>
51 Quiet("q", cl::desc("Don't print 'program modified' message"));
52
53 static cl::alias
54 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
55
56
57 //===----------------------------------------------------------------------===//
58 // main for opt
59 //
60 int main(int argc, char **argv) {
61   cl::ParseCommandLineOptions(argc, argv,
62                               " llvm .bc -> .bc modular optimizer\n");
63
64   // FIXME: The choice of target should be controllable on the command line.
65   TargetData TD("opt target");
66
67   // Allocate a full target machine description only if necessary...
68   // FIXME: The choice of target should be controllable on the command line.
69   std::auto_ptr<TargetMachine> target;
70
71   TargetMachine* TM = NULL;
72
73   // Load the input module...
74   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
75   if (M.get() == 0) {
76     cerr << argv[0] << ": bytecode didn't read correctly.\n";
77     return 1;
78   }
79
80   // Figure out what stream we are supposed to write to...
81   std::ostream *Out = &std::cout;  // Default to printing to stdout...
82   if (OutputFilename != "") {
83     if (!Force && std::ifstream(OutputFilename.c_str())) {
84       // If force is not specified, make sure not to overwrite a file!
85       cerr << argv[0] << ": error opening '" << OutputFilename
86            << "': file exists!\n"
87            << "Use -f command line argument to force output\n";
88       return 1;
89     }
90     Out = new std::ofstream(OutputFilename.c_str());
91
92     if (!Out->good()) {
93       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
94       return 1;
95     }
96
97     // Make sure that the Output file gets unlink'd from the disk if we get a
98     // SIGINT
99     RemoveFileOnSignal(OutputFilename);
100   }
101
102   // Create a PassManager to hold and optimize the collection of passes we are
103   // about to build...
104   //
105   PassManager Passes;
106
107   // Create a new optimization pass for each one specified on the command line
108   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
109     const PassInfo *Opt = OptimizationList[i];
110     
111     if (Opt->getNormalCtor())
112       Passes.add(Opt->getNormalCtor()());
113     else if (Opt->getDataCtor())
114       Passes.add(Opt->getDataCtor()(TD));    // Provide dummy target data...
115     else if (Opt->getTargetCtor()) {
116       if (target.get() == NULL)
117         target.reset(allocateSparcTargetMachine()); // FIXME: target option
118       assert(target.get() && "Could not allocate target machine!");
119       Passes.add(Opt->getTargetCtor()(*target.get()));
120     } else
121       cerr << argv[0] << ": cannot create pass: " << Opt->getPassName() << "\n";
122
123     if (PrintEachXForm)
124       Passes.add(new PrintModulePass(&cerr));
125   }
126
127   // Check that the module is well formed on completion of optimization
128   Passes.add(createVerifierPass());
129
130   // Write bytecode out to disk or cout as the last step...
131   Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
132
133   // Now that we have all of the passes ready, run them.
134   if (Passes.run(*M.get()) && !Quiet)
135     cerr << "Program modified.\n";
136
137   return 0;
138 }