blob: d032e24ccd005511b84e8d4627ed98f904fb4ce6 [file] [log] [blame]
Chris Lattner0eafc312001-10-18 06:05:15 +00001//===----------------------------------------------------------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerd4c7f272002-07-30 21:43:25 +00009// LLVM Modular Optimizer Utility: opt
Chris Lattner00950542001-06-06 20:29:01 +000010//
Chris Lattner00950542001-06-06 20:29:01 +000011// Optimizations may be specified an arbitrary number of times on the command
12// line, they are run in the order specified.
13//
Chris Lattner0eafc312001-10-18 06:05:15 +000014//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000015
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/Module.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000017#include "llvm/PassManager.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Bytecode/Reader.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000019#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000020#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000021#include "llvm/Analysis/Verifier.h"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattner6560b6b2002-10-29 20:48:09 +000023#include "llvm/Target/TargetMachineImpls.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000024#include "llvm/Support/PassNameParser.h"
Chris Lattner76d12292002-04-18 19:55:25 +000025#include "Support/Signals.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000026#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000027#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000028#include <algorithm>
Anand Shukla63aaa112002-06-25 21:43:28 +000029
Brian Gaeked0fde302003-11-11 22:41:34 +000030using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000031
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000032// The OptimizationList is automatically populated with registered Passes by the
33// PassNameParser.
34//
Chris Lattner2053a2a2002-07-26 21:09:32 +000035static cl::list<const PassInfo*, bool,
36 FilteredPassNameParser<PassInfo::Optimization> >
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000037OptimizationList(cl::desc("Optimizations available:"));
38
39
40// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000041//
Chris Lattner6c8103f2003-05-22 20:13:16 +000042static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000043InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
44
Chris Lattner6c8103f2003-05-22 20:13:16 +000045static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000046OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerb592fc22003-12-10 14:41:33 +000047 cl::value_desc("filename"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000048
49static cl::opt<bool>
50Force("f", cl::desc("Overwrite output files"));
51
52static cl::opt<bool>
53PrintEachXForm("p", cl::desc("Print module after each transformation"));
54
55static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000056NoOutput("disable-output",
57 cl::desc("Do not write result bytecode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000058
59static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000060NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000061
62static cl::opt<bool>
Chris Lattnerf9e173e2002-07-31 16:52:49 +000063Quiet("q", cl::desc("Don't print 'program modified' message"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000064
65static cl::alias
66QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
67
Chris Lattner0be41012002-02-01 04:54:11 +000068
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000069//===----------------------------------------------------------------------===//
70// main for opt
71//
Chris Lattner00950542001-06-06 20:29:01 +000072int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000073 cl::ParseCommandLineOptions(argc, argv,
74 " llvm .bc -> .bc modular optimizer\n");
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000075
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000076 // Allocate a full target machine description only if necessary...
77 // FIXME: The choice of target should be controllable on the command line.
78 std::auto_ptr<TargetMachine> target;
79
80 TargetMachine* TM = NULL;
Chris Lattner56620da2003-04-16 20:51:36 +000081 std::string ErrorMessage;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000082
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000083 // Load the input module...
Chris Lattner56620da2003-04-16 20:51:36 +000084 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattner63202322001-11-26 19:22:39 +000085 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000086 std::cerr << argv[0] << ": ";
Chris Lattner56620da2003-04-16 20:51:36 +000087 if (ErrorMessage.size())
Chris Lattner6c8103f2003-05-22 20:13:16 +000088 std::cerr << ErrorMessage << "\n";
Chris Lattner56620da2003-04-16 20:51:36 +000089 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000090 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000091 return 1;
92 }
93
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000094 // Figure out what stream we are supposed to write to...
Chris Lattner697954c2002-01-20 22:54:45 +000095 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattnerb592fc22003-12-10 14:41:33 +000096 if (OutputFilename != "-") {
Chris Lattner888912d2002-01-22 21:07:24 +000097 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +000098 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +000099 std::cerr << argv[0] << ": error opening '" << OutputFilename
100 << "': file exists!\n"
101 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000102 return 1;
103 }
104 Out = new std::ofstream(OutputFilename.c_str());
105
Chris Lattner00950542001-06-06 20:29:01 +0000106 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000107 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000108 return 1;
109 }
Chris Lattner76d12292002-04-18 19:55:25 +0000110
Misha Brukman452fea92003-10-10 17:56:49 +0000111 // Make sure that the Output file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000112 // SIGINT
113 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +0000114 }
115
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000116 // Create a PassManager to hold and optimize the collection of passes we are
117 // about to build...
118 //
119 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000120
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000121 // Add an appropriate TargetData instance for this module...
122 Passes.add(new TargetData("opt", M.get()));
123
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000124 // Create a new optimization pass for each one specified on the command line
125 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000126 const PassInfo *Opt = OptimizationList[i];
127
128 if (Opt->getNormalCtor())
129 Passes.add(Opt->getNormalCtor()());
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000130 else if (Opt->getTargetCtor()) {
Chris Lattner0af1e8e2003-04-16 23:02:16 +0000131#if 0
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000132 if (target.get() == NULL)
133 target.reset(allocateSparcTargetMachine()); // FIXME: target option
Chris Lattner0af1e8e2003-04-16 23:02:16 +0000134#endif
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000135 assert(target.get() && "Could not allocate target machine!");
136 Passes.add(Opt->getTargetCtor()(*target.get()));
137 } else
Chris Lattner6c8103f2003-05-22 20:13:16 +0000138 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
139 << "\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000140
141 if (PrintEachXForm)
Chris Lattner6c8103f2003-05-22 20:13:16 +0000142 Passes.add(new PrintModulePass(&std::cerr));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000143 }
144
Chris Lattner22d26d72002-02-20 17:56:53 +0000145 // Check that the module is well formed on completion of optimization
Chris Lattnerf3bafc12003-02-12 18:45:08 +0000146 if (!NoVerify)
147 Passes.add(createVerifierPass());
Chris Lattner22d26d72002-02-20 17:56:53 +0000148
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000149 // Write bytecode out to disk or cout as the last step...
Chris Lattnerd70b68e2003-02-12 18:43:33 +0000150 if (!NoOutput)
151 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000152
153 // Now that we have all of the passes ready, run them.
Chris Lattner7e708292002-06-25 16:13:24 +0000154 if (Passes.run(*M.get()) && !Quiet)
Chris Lattner6c8103f2003-05-22 20:13:16 +0000155 std::cerr << "Program modified.\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000156
Chris Lattner00950542001-06-06 20:29:01 +0000157 return 0;
158}