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