1 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 // LLVM Modular Optimizer Utility: opt
11 // Optimizations may be specified an arbitrary number of times on the command
12 // line, they are run in the order specified.
14 //===----------------------------------------------------------------------===//
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"
31 // The OptimizationList is automatically populated with registered Passes by the
34 static cl::list<const PassInfo*, bool,
35 FilteredPassNameParser<PassInfo::Optimization> >
36 OptimizationList(cl::desc("Optimizations available:"));
39 // Other command line options...
41 static cl::opt<std::string>
42 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
44 static cl::opt<std::string>
45 OutputFilename("o", cl::desc("Override output filename"),
46 cl::value_desc("filename"));
49 Force("f", cl::desc("Overwrite output files"));
52 PrintEachXForm("p", cl::desc("Print module after each transformation"));
55 NoOutput("disable-output",
56 cl::desc("Do not write result bytecode file"), cl::Hidden);
59 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
62 Quiet("q", cl::desc("Don't print 'program modified' message"));
65 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
68 //===----------------------------------------------------------------------===//
71 int main(int argc, char **argv) {
72 cl::ParseCommandLineOptions(argc, argv,
73 " llvm .bc -> .bc modular optimizer\n");
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;
79 TargetMachine* TM = NULL;
80 std::string ErrorMessage;
82 // Load the input module...
83 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
85 std::cerr << argv[0] << ": ";
86 if (ErrorMessage.size())
87 std::cerr << ErrorMessage << "\n";
89 std::cerr << "bytecode didn't read correctly.\n";
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";
103 Out = new std::ofstream(OutputFilename.c_str());
106 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
110 // Make sure that the Output file gets unlinked from the disk if we get a
112 RemoveFileOnSignal(OutputFilename);
115 // Create a PassManager to hold and optimize the collection of passes we are
120 // Add an appropriate TargetData instance for this module...
121 Passes.add(new TargetData("opt", M.get()));
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];
127 if (Opt->getNormalCtor())
128 Passes.add(Opt->getNormalCtor()());
129 else if (Opt->getTargetCtor()) {
131 if (target.get() == NULL)
132 target.reset(allocateSparcTargetMachine()); // FIXME: target option
134 assert(target.get() && "Could not allocate target machine!");
135 Passes.add(Opt->getTargetCtor()(*target.get()));
137 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
141 Passes.add(new PrintModulePass(&std::cerr));
144 // Check that the module is well formed on completion of optimization
146 Passes.add(createVerifierPass());
148 // Write bytecode out to disk or cout as the last step...
150 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
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";