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"
32 // The OptimizationList is automatically populated with registered Passes by the
35 static cl::list<const PassInfo*, bool,
36 FilteredPassNameParser<PassInfo::Optimization> >
37 OptimizationList(cl::desc("Optimizations available:"));
40 // Other command line options...
42 static cl::opt<std::string>
43 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
45 static cl::opt<std::string>
46 OutputFilename("o", cl::desc("Override output filename"),
47 cl::value_desc("filename"), cl::init("-"));
50 Force("f", cl::desc("Overwrite output files"));
53 PrintEachXForm("p", cl::desc("Print module after each transformation"));
56 NoOutput("disable-output",
57 cl::desc("Do not write result bytecode file"), cl::Hidden);
60 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
63 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
66 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
69 //===----------------------------------------------------------------------===//
72 int main(int argc, char **argv) {
73 cl::ParseCommandLineOptions(argc, argv,
74 " llvm .bc -> .bc modular optimizer\n");
75 sys::PrintStackTraceOnErrorSignal();
77 // Allocate a full target machine description only if necessary...
78 // FIXME: The choice of target should be controllable on the command line.
79 std::auto_ptr<TargetMachine> target;
81 TargetMachine* TM = NULL;
82 std::string ErrorMessage;
84 // Load the input module...
85 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
87 std::cerr << argv[0] << ": ";
88 if (ErrorMessage.size())
89 std::cerr << ErrorMessage << "\n";
91 std::cerr << "bytecode didn't read correctly.\n";
95 // Figure out what stream we are supposed to write to...
96 std::ostream *Out = &std::cout; // Default to printing to stdout...
97 if (OutputFilename != "-") {
98 if (!Force && std::ifstream(OutputFilename.c_str())) {
99 // If force is not specified, make sure not to overwrite a file!
100 std::cerr << argv[0] << ": error opening '" << OutputFilename
101 << "': file exists!\n"
102 << "Use -f command line argument to force output\n";
105 Out = new std::ofstream(OutputFilename.c_str());
108 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
112 // Make sure that the Output file gets unlinked from the disk if we get a
114 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
117 // If the output is set to be emitted to standard out, and standard out is a
118 // console, print out a warning message and refuse to do it. We don't impress
119 // anyone by spewing tons of binary goo to a terminal.
120 if (Out == &std::cout && isStandardOutAConsole() && !Force && !NoOutput
122 std::cerr << "WARNING: It looks like you're attempting to print out a "
123 << "bytecode file. I'm\ngoing to pretend you didn't ask me to do"
124 << " this (for your own good). If you\nREALLY want to taste LLVM"
125 << " bytecode first-hand, you can force output with the\n`-f'"
130 // Create a PassManager to hold and optimize the collection of passes we are
135 // Add an appropriate TargetData instance for this module...
136 Passes.add(new TargetData("opt", M.get()));
138 // Create a new optimization pass for each one specified on the command line
139 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
140 const PassInfo *Opt = OptimizationList[i];
142 if (Opt->getNormalCtor())
143 Passes.add(Opt->getNormalCtor()());
144 else if (Opt->getTargetCtor()) {
146 if (target.get() == NULL)
147 target.reset(allocateSparcTargetMachine()); // FIXME: target option
149 assert(target.get() && "Could not allocate target machine!");
150 Passes.add(Opt->getTargetCtor()(*target.get()));
152 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
156 Passes.add(new PrintModulePass(&std::cerr));
159 // Check that the module is well formed on completion of optimization
161 Passes.add(createVerifierPass());
163 // Write bytecode out to disk or cout as the last step...
165 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
167 // Now that we have all of the passes ready, run them.
168 Passes.run(*M.get());