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