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