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