1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Optimizations may be specified an arbitrary number of times on the command
11 // line, they are run in the order specified.
13 //===----------------------------------------------------------------------===//
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"
33 // The OptimizationList is automatically populated with registered Passes by the
36 static cl::list<const PassInfo*, bool,
37 FilteredPassNameParser<PassInfo::Optimization> >
38 OptimizationList(cl::desc("Optimizations available:"));
41 // Other command line options...
43 static cl::opt<std::string>
44 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
46 static cl::opt<std::string>
47 OutputFilename("o", cl::desc("Override output filename"),
48 cl::value_desc("filename"), cl::init("-"));
51 Force("f", cl::desc("Overwrite output files"));
54 PrintEachXForm("p", cl::desc("Print module after each transformation"));
57 NoOutput("disable-output",
58 cl::desc("Do not write result bytecode file"), cl::Hidden);
61 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
64 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
67 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
70 //===----------------------------------------------------------------------===//
73 int main(int argc, char **argv) {
75 cl::ParseCommandLineOptions(argc, argv,
76 " llvm .bc -> .bc modular optimizer\n");
77 sys::PrintStackTraceOnErrorSignal();
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;
83 TargetMachine* TM = NULL;
84 std::string ErrorMessage;
86 // Load the input module...
87 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
89 std::cerr << argv[0] << ": ";
90 if (ErrorMessage.size())
91 std::cerr << ErrorMessage << "\n";
93 std::cerr << "bytecode didn't read correctly.\n";
97 // Figure out what stream we are supposed to write to...
98 // FIXME: cout is not binary!
99 std::ostream *Out = &std::cout; // Default to printing to stdout...
100 if (OutputFilename != "-") {
101 if (!Force && std::ifstream(OutputFilename.c_str())) {
102 // If force is not specified, make sure not to overwrite a file!
103 std::cerr << argv[0] << ": error opening '" << OutputFilename
104 << "': file exists!\n"
105 << "Use -f command line argument to force output\n";
108 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
110 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
113 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
117 // Make sure that the Output file gets unlinked from the disk if we get a
119 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
122 // If the output is set to be emitted to standard out, and standard out is a
123 // console, print out a warning message and refuse to do it. We don't
124 // impress anyone by spewing tons of binary goo to a terminal.
125 if (!Force && !NoOutput && CheckBytecodeOutputToConsole(Out,!Quiet)) {
129 // Create a PassManager to hold and optimize the collection of passes we are
134 // Add an appropriate TargetData instance for this module...
135 Passes.add(new TargetData("opt", M.get()));
137 // Create a new optimization pass for each one specified on the command line
138 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
139 const PassInfo *Opt = OptimizationList[i];
141 if (Opt->getNormalCtor())
142 Passes.add(Opt->getNormalCtor()());
143 else if (Opt->getTargetCtor()) {
145 if (target.get() == NULL)
146 target.reset(allocateSparcTargetMachine()); // FIXME: target option
148 assert(target.get() && "Could not allocate target machine!");
149 Passes.add(Opt->getTargetCtor()(*target.get()));
151 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
155 Passes.add(new PrintModulePass(&std::cerr));
158 // Check that the module is well formed on completion of optimization
160 Passes.add(createVerifierPass());
162 // Write bytecode out to disk or cout as the last step...
164 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
166 // Now that we have all of the passes ready, run them.
167 Passes.run(*M.get());
170 } catch (const std::string& msg) {
171 std::cerr << argv[0] << ": " << msg << "\n";
173 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";