blob: 743bfb8339a254c3b48e76704df4de28f99804fa [file] [log] [blame]
Chris Lattner0eafc312001-10-18 06:05:15 +00001//===----------------------------------------------------------------------===//
Chris Lattnerd4c7f272002-07-30 21:43:25 +00002// LLVM Modular Optimizer Utility: opt
Chris Lattner00950542001-06-06 20:29:01 +00003//
Chris Lattner00950542001-06-06 20:29:01 +00004// Optimizations may be specified an arbitrary number of times on the command
5// line, they are run in the order specified.
6//
Chris Lattner0eafc312001-10-18 06:05:15 +00007//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00008
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/Module.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000010#include "llvm/PassManager.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/Bytecode/Reader.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000012#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000013#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000014#include "llvm/Analysis/Verifier.h"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000015#include "llvm/Target/TargetMachine.h"
Chris Lattner6560b6b2002-10-29 20:48:09 +000016#include "llvm/Target/TargetMachineImpls.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000017#include "llvm/Support/PassNameParser.h"
Chris Lattner76d12292002-04-18 19:55:25 +000018#include "Support/Signals.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000019#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000020#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000021#include <algorithm>
Anand Shukla63aaa112002-06-25 21:43:28 +000022
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000023
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000024// The OptimizationList is automatically populated with registered Passes by the
25// PassNameParser.
26//
Chris Lattner2053a2a2002-07-26 21:09:32 +000027static cl::list<const PassInfo*, bool,
28 FilteredPassNameParser<PassInfo::Optimization> >
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000029OptimizationList(cl::desc("Optimizations available:"));
30
31
32// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000033//
Chris Lattner6c8103f2003-05-22 20:13:16 +000034static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000035InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
36
Chris Lattner6c8103f2003-05-22 20:13:16 +000037static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000038OutputFilename("o", cl::desc("Override output filename"),
39 cl::value_desc("filename"));
40
41static cl::opt<bool>
42Force("f", cl::desc("Overwrite output files"));
43
44static cl::opt<bool>
45PrintEachXForm("p", cl::desc("Print module after each transformation"));
46
47static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000048NoOutput("disable-output",
49 cl::desc("Do not write result bytecode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000050
51static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000052NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000053
54static cl::opt<bool>
Chris Lattnerf9e173e2002-07-31 16:52:49 +000055Quiet("q", cl::desc("Don't print 'program modified' message"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000056
57static cl::alias
58QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
59
Chris Lattner0be41012002-02-01 04:54:11 +000060
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000061//===----------------------------------------------------------------------===//
62// main for opt
63//
Chris Lattner00950542001-06-06 20:29:01 +000064int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000065 cl::ParseCommandLineOptions(argc, argv,
66 " llvm .bc -> .bc modular optimizer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000067
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000068 // 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;
71
72 TargetMachine* TM = NULL;
Chris Lattner56620da2003-04-16 20:51:36 +000073 std::string ErrorMessage;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000074
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000075 // Load the input module...
Chris Lattner56620da2003-04-16 20:51:36 +000076 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattner63202322001-11-26 19:22:39 +000077 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000078 std::cerr << argv[0] << ": ";
Chris Lattner56620da2003-04-16 20:51:36 +000079 if (ErrorMessage.size())
Chris Lattner6c8103f2003-05-22 20:13:16 +000080 std::cerr << ErrorMessage << "\n";
Chris Lattner56620da2003-04-16 20:51:36 +000081 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000082 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000083 return 1;
84 }
85
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000086 // Figure out what stream we are supposed to write to...
Chris Lattner697954c2002-01-20 22:54:45 +000087 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +000088 if (OutputFilename != "") {
Chris Lattner888912d2002-01-22 21:07:24 +000089 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000090 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000091 std::cerr << argv[0] << ": error opening '" << OutputFilename
92 << "': file exists!\n"
93 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +000094 return 1;
95 }
96 Out = new std::ofstream(OutputFilename.c_str());
97
Chris Lattner00950542001-06-06 20:29:01 +000098 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000099 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000100 return 1;
101 }
Chris Lattner76d12292002-04-18 19:55:25 +0000102
103 // Make sure that the Output file gets unlink'd from the disk if we get a
104 // SIGINT
105 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +0000106 }
107
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000108 // Create a PassManager to hold and optimize the collection of passes we are
109 // about to build...
110 //
111 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000112
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000113 // Add an appropriate TargetData instance for this module...
114 Passes.add(new TargetData("opt", M.get()));
115
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000116 // Create a new optimization pass for each one specified on the command line
117 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000118 const PassInfo *Opt = OptimizationList[i];
119
120 if (Opt->getNormalCtor())
121 Passes.add(Opt->getNormalCtor()());
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000122 else if (Opt->getTargetCtor()) {
Chris Lattner0af1e8e2003-04-16 23:02:16 +0000123#if 0
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000124 if (target.get() == NULL)
125 target.reset(allocateSparcTargetMachine()); // FIXME: target option
Chris Lattner0af1e8e2003-04-16 23:02:16 +0000126#endif
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000127 assert(target.get() && "Could not allocate target machine!");
128 Passes.add(Opt->getTargetCtor()(*target.get()));
129 } else
Chris Lattner6c8103f2003-05-22 20:13:16 +0000130 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
131 << "\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000132
133 if (PrintEachXForm)
Chris Lattner6c8103f2003-05-22 20:13:16 +0000134 Passes.add(new PrintModulePass(&std::cerr));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000135 }
136
Chris Lattner22d26d72002-02-20 17:56:53 +0000137 // Check that the module is well formed on completion of optimization
Chris Lattnerf3bafc12003-02-12 18:45:08 +0000138 if (!NoVerify)
139 Passes.add(createVerifierPass());
Chris Lattner22d26d72002-02-20 17:56:53 +0000140
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000141 // Write bytecode out to disk or cout as the last step...
Chris Lattnerd70b68e2003-02-12 18:43:33 +0000142 if (!NoOutput)
143 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000144
145 // Now that we have all of the passes ready, run them.
Chris Lattner7e708292002-06-25 16:13:24 +0000146 if (Passes.run(*M.get()) && !Quiet)
Chris Lattner6c8103f2003-05-22 20:13:16 +0000147 std::cerr << "Program modified.\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000148
Chris Lattner00950542001-06-06 20:29:01 +0000149 return 0;
150}