9d0837c29d745d8f8058a1313ec5951e2d026902
[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 NoOutput("no-output", cl::desc("Do not write result bytecode file"), cl::Hidden);
52
53 static cl::opt<bool>
54 Quiet("q", cl::desc("Don't print 'program modified' message"));
55
56 static cl::alias
57 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
58
59
60 //===----------------------------------------------------------------------===//
61 // main for opt
62 //
63 int main(int argc, char **argv) {
64   cl::ParseCommandLineOptions(argc, argv,
65                               " llvm .bc -> .bc modular optimizer\n");
66
67   // FIXME: The choice of target should be controllable on the command line.
68   TargetData TD("opt target");
69
70   // Allocate a full target machine description only if necessary...
71   // FIXME: The choice of target should be controllable on the command line.
72   std::auto_ptr<TargetMachine> target;
73
74   TargetMachine* TM = NULL;
75
76   // Load the input module...
77   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
78   if (M.get() == 0) {
79     cerr << argv[0] << ": bytecode didn't read correctly.\n";
80     return 1;
81   }
82
83   // Figure out what stream we are supposed to write to...
84   std::ostream *Out = &std::cout;  // Default to printing to stdout...
85   if (OutputFilename != "") {
86     if (!Force && std::ifstream(OutputFilename.c_str())) {
87       // If force is not specified, make sure not to overwrite a file!
88       cerr << argv[0] << ": error opening '" << OutputFilename
89            << "': file exists!\n"
90            << "Use -f command line argument to force output\n";
91       return 1;
92     }
93     Out = new std::ofstream(OutputFilename.c_str());
94
95     if (!Out->good()) {
96       cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
97       return 1;
98     }
99
100     // Make sure that the Output file gets unlink'd from the disk if we get a
101     // SIGINT
102     RemoveFileOnSignal(OutputFilename);
103   }
104
105   // Create a PassManager to hold and optimize the collection of passes we are
106   // about to build...
107   //
108   PassManager Passes;
109
110   // Create a new optimization pass for each one specified on the command line
111   for (unsigned i = 0; i < OptimizationList.size(); ++i) {
112     const PassInfo *Opt = OptimizationList[i];
113     
114     if (Opt->getNormalCtor())
115       Passes.add(Opt->getNormalCtor()());
116     else if (Opt->getDataCtor())
117       Passes.add(Opt->getDataCtor()(TD));    // Provide dummy target data...
118     else if (Opt->getTargetCtor()) {
119       if (target.get() == NULL)
120         target.reset(allocateSparcTargetMachine()); // FIXME: target option
121       assert(target.get() && "Could not allocate target machine!");
122       Passes.add(Opt->getTargetCtor()(*target.get()));
123     } else
124       cerr << argv[0] << ": cannot create pass: " << Opt->getPassName() << "\n";
125
126     if (PrintEachXForm)
127       Passes.add(new PrintModulePass(&cerr));
128   }
129
130   // Check that the module is well formed on completion of optimization
131   Passes.add(createVerifierPass());
132
133   // Write bytecode out to disk or cout as the last step...
134   if (!NoOutput)
135     Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
136
137   // Now that we have all of the passes ready, run them.
138   if (Passes.run(*M.get()) && !Quiet)
139     cerr << "Program modified.\n";
140
141   return 0;
142 }