blob: de56080c2c1602ff996b0b0d2fa183284f4aa710 [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
23using std::cerr;
Chris Lattnerc7a09852002-07-25 16:31:09 +000024using std::string;
Chris Lattnere04f4b62002-05-10 15:43:07 +000025
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000026
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000027// The OptimizationList is automatically populated with registered Passes by the
28// PassNameParser.
29//
Chris Lattner2053a2a2002-07-26 21:09:32 +000030static cl::list<const PassInfo*, bool,
31 FilteredPassNameParser<PassInfo::Optimization> >
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000032OptimizationList(cl::desc("Optimizations available:"));
33
34
35// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000036//
Chris Lattner5ff62e92002-07-22 02:10:13 +000037static cl::opt<string>
38InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
39
40static cl::opt<string>
41OutputFilename("o", cl::desc("Override output filename"),
42 cl::value_desc("filename"));
43
44static cl::opt<bool>
45Force("f", cl::desc("Overwrite output files"));
46
47static cl::opt<bool>
48PrintEachXForm("p", cl::desc("Print module after each transformation"));
49
50static cl::opt<bool>
Chris Lattnerf9e173e2002-07-31 16:52:49 +000051Quiet("q", cl::desc("Don't print 'program modified' message"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000052
53static cl::alias
54QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
55
Chris Lattner0be41012002-02-01 04:54:11 +000056
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000057//===----------------------------------------------------------------------===//
58// main for opt
59//
Chris Lattner00950542001-06-06 20:29:01 +000060int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000061 cl::ParseCommandLineOptions(argc, argv,
62 " llvm .bc -> .bc modular optimizer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000063
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000064 // FIXME: The choice of target should be controllable on the command line.
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000065 TargetData TD("opt target");
66
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000067 // Allocate a full target machine description only if necessary...
68 // FIXME: The choice of target should be controllable on the command line.
69 std::auto_ptr<TargetMachine> target;
70
71 TargetMachine* TM = NULL;
72
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000073 // Load the input module...
Chris Lattner63202322001-11-26 19:22:39 +000074 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
75 if (M.get() == 0) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +000076 cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000077 return 1;
78 }
79
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000080 // Figure out what stream we are supposed to write to...
Chris Lattner697954c2002-01-20 22:54:45 +000081 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner1e78f362001-07-23 19:27:24 +000082 if (OutputFilename != "") {
Chris Lattner888912d2002-01-22 21:07:24 +000083 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000084 // If force is not specified, make sure not to overwrite a file!
Chris Lattnerd4c7f272002-07-30 21:43:25 +000085 cerr << argv[0] << ": error opening '" << OutputFilename
86 << "': file exists!\n"
Chris Lattner697954c2002-01-20 22:54:45 +000087 << "Use -f command line argument to force output\n";
88 return 1;
89 }
90 Out = new std::ofstream(OutputFilename.c_str());
91
Chris Lattner00950542001-06-06 20:29:01 +000092 if (!Out->good()) {
Chris Lattnerd4c7f272002-07-30 21:43:25 +000093 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +000094 return 1;
95 }
Chris Lattner76d12292002-04-18 19:55:25 +000096
97 // Make sure that the Output file gets unlink'd from the disk if we get a
98 // SIGINT
99 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +0000100 }
101
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000102 // Create a PassManager to hold and optimize the collection of passes we are
103 // about to build...
104 //
105 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000106
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000107 // Create a new optimization pass for each one specified on the command line
108 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000109 const PassInfo *Opt = OptimizationList[i];
110
111 if (Opt->getNormalCtor())
112 Passes.add(Opt->getNormalCtor()());
113 else if (Opt->getDataCtor())
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000114 Passes.add(Opt->getDataCtor()(TD)); // Provide dummy target data...
115 else if (Opt->getTargetCtor()) {
116 if (target.get() == NULL)
117 target.reset(allocateSparcTargetMachine()); // FIXME: target option
118 assert(target.get() && "Could not allocate target machine!");
119 Passes.add(Opt->getTargetCtor()(*target.get()));
120 } else
Chris Lattnerd4c7f272002-07-30 21:43:25 +0000121 cerr << argv[0] << ": cannot create pass: " << Opt->getPassName() << "\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000122
123 if (PrintEachXForm)
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000124 Passes.add(new PrintModulePass(&cerr));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000125 }
126
Chris Lattner22d26d72002-02-20 17:56:53 +0000127 // Check that the module is well formed on completion of optimization
128 Passes.add(createVerifierPass());
129
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000130 // Write bytecode out to disk or cout as the last step...
131 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
132
133 // Now that we have all of the passes ready, run them.
Chris Lattner7e708292002-06-25 16:13:24 +0000134 if (Passes.run(*M.get()) && !Quiet)
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000135 cerr << "Program modified.\n";
136
Chris Lattner00950542001-06-06 20:29:01 +0000137 return 0;
138}