1 //===----------------------------------------------------------------------===//
4 // Optimizations may be specified an arbitrary number of times on the command
5 // line, they are run in the order specified.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Module.h"
10 #include "llvm/PassManager.h"
11 #include "llvm/Bytecode/Reader.h"
12 #include "llvm/Bytecode/WriteBytecodePass.h"
13 #include "llvm/Assembly/PrintModulePass.h"
14 #include "llvm/Analysis/Verifier.h"
15 #include "llvm/Target/TargetData.h"
16 #include "llvm/Support/PassNameParser.h"
17 #include "Support/Signals.h"
26 // The OptimizationList is automatically populated with registered Passes by the
29 static cl::list<const PassInfo*, bool,
30 FilteredPassNameParser<PassInfo::Optimization> >
31 OptimizationList(cl::desc("Optimizations available:"));
34 // Other command line options...
36 static cl::opt<string>
37 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
39 static cl::opt<string>
40 OutputFilename("o", cl::desc("Override output filename"),
41 cl::value_desc("filename"));
44 Force("f", cl::desc("Overwrite output files"));
47 PrintEachXForm("p", cl::desc("Print module after each transformation"));
50 Quiet("q", cl::desc("Don't print modifying pass names"));
53 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
56 //===----------------------------------------------------------------------===//
59 int main(int argc, char **argv) {
60 cl::ParseCommandLineOptions(argc, argv,
61 " llvm .bc -> .bc modular optimizer\n");
63 // FIXME: This should be parameterizable eventually for different target
65 TargetData TD("opt target");
67 // Load the input module...
68 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
70 cerr << "bytecode didn't read correctly.\n";
74 // Figure out what stream we are supposed to write to...
75 std::ostream *Out = &std::cout; // Default to printing to stdout...
76 if (OutputFilename != "") {
77 if (!Force && std::ifstream(OutputFilename.c_str())) {
78 // If force is not specified, make sure not to overwrite a file!
79 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
80 << "Use -f command line argument to force output\n";
83 Out = new std::ofstream(OutputFilename.c_str());
86 cerr << "Error opening " << OutputFilename << "!\n";
90 // Make sure that the Output file gets unlink'd from the disk if we get a
92 RemoveFileOnSignal(OutputFilename);
95 // Create a PassManager to hold and optimize the collection of passes we are
100 // Create a new optimization pass for each one specified on the command line
101 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
102 const PassInfo *Opt = OptimizationList[i];
104 if (Opt->getNormalCtor())
105 Passes.add(Opt->getNormalCtor()());
106 else if (Opt->getDataCtor())
107 Passes.add(Opt->getDataCtor()(TD)); // Pass dummy target data...
109 cerr << "Cannot create pass: " << Opt->getPassName() << "\n";
112 Passes.add(new PrintModulePass(&cerr));
115 // Check that the module is well formed on completion of optimization
116 Passes.add(createVerifierPass());
118 // Write bytecode out to disk or cout as the last step...
119 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
121 // Now that we have all of the passes ready, run them.
122 if (Passes.run(*M.get()) && !Quiet)
123 cerr << "Program modified.\n";