1 //===----------------------------------------------------------------------===//
2 // LLVM Modular Optimizer Utility: opt
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/TargetMachine.h"
16 #include "llvm/Target/TargetMachineImpls.h"
17 #include "llvm/Support/PassNameParser.h"
18 #include "Support/Signals.h"
24 // The OptimizationList is automatically populated with registered Passes by the
27 static cl::list<const PassInfo*, bool,
28 FilteredPassNameParser<PassInfo::Optimization> >
29 OptimizationList(cl::desc("Optimizations available:"));
32 // Other command line options...
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
42 Force("f", cl::desc("Overwrite output files"));
45 PrintEachXForm("p", cl::desc("Print module after each transformation"));
48 NoOutput("disable-output",
49 cl::desc("Do not write result bytecode file"), cl::Hidden);
52 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
55 Quiet("q", cl::desc("Don't print 'program modified' message"));
58 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
61 //===----------------------------------------------------------------------===//
64 int main(int argc, char **argv) {
65 cl::ParseCommandLineOptions(argc, argv,
66 " llvm .bc -> .bc modular optimizer\n");
68 // Allocate a full target machine description only if necessary...
69 // FIXME: The choice of target should be controllable on the command line.
70 std::auto_ptr<TargetMachine> target;
72 TargetMachine* TM = NULL;
73 std::string ErrorMessage;
75 // Load the input module...
76 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
78 std::cerr << argv[0] << ": ";
79 if (ErrorMessage.size())
80 std::cerr << ErrorMessage << "\n";
82 std::cerr << "bytecode didn't read correctly.\n";
86 // Figure out what stream we are supposed to write to...
87 std::ostream *Out = &std::cout; // Default to printing to stdout...
88 if (OutputFilename != "") {
89 if (!Force && std::ifstream(OutputFilename.c_str())) {
90 // If force is not specified, make sure not to overwrite a file!
91 std::cerr << argv[0] << ": error opening '" << OutputFilename
92 << "': file exists!\n"
93 << "Use -f command line argument to force output\n";
96 Out = new std::ofstream(OutputFilename.c_str());
99 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
103 // Make sure that the Output file gets unlinked from the disk if we get a
105 RemoveFileOnSignal(OutputFilename);
108 // Create a PassManager to hold and optimize the collection of passes we are
113 // Add an appropriate TargetData instance for this module...
114 Passes.add(new TargetData("opt", M.get()));
116 // Create a new optimization pass for each one specified on the command line
117 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
118 const PassInfo *Opt = OptimizationList[i];
120 if (Opt->getNormalCtor())
121 Passes.add(Opt->getNormalCtor()());
122 else if (Opt->getTargetCtor()) {
124 if (target.get() == NULL)
125 target.reset(allocateSparcTargetMachine()); // FIXME: target option
127 assert(target.get() && "Could not allocate target machine!");
128 Passes.add(Opt->getTargetCtor()(*target.get()));
130 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
134 Passes.add(new PrintModulePass(&std::cerr));
137 // Check that the module is well formed on completion of optimization
139 Passes.add(createVerifierPass());
141 // Write bytecode out to disk or cout as the last step...
143 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
145 // Now that we have all of the passes ready, run them.
146 if (Passes.run(*M.get()) && !Quiet)
147 std::cerr << "Program modified.\n";