blob: 612b65fd561db3a91755da486af2420294b299d6 [file] [log] [blame]
Chris Lattner76351aa2004-04-02 05:06:57 +00001//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
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 Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattner00950542001-06-06 20:29:01 +000010// Optimizations may be specified an arbitrary number of times on the command
11// line, they are run in the order specified.
12//
Chris Lattner0eafc312001-10-18 06:05:15 +000013//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000014
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/Module.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000016#include "llvm/PassManager.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/Bytecode/Reader.h"
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000018#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerffa6f9c2001-10-19 15:39:14 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner22d26d72002-02-20 17:56:53 +000020#include "llvm/Analysis/Verifier.h"
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner6560b6b2002-10-29 20:48:09 +000022#include "llvm/Target/TargetMachineImpls.h"
Chris Lattner2053a2a2002-07-26 21:09:32 +000023#include "llvm/Support/PassNameParser.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Chris Lattner4a1de8b2004-07-11 01:08:19 +000025#include "Support/PluginLoader.h"
Chris Lattner76351aa2004-04-02 05:06:57 +000026#include "Support/SystemUtils.h"
Chris Lattner73e11d72001-10-18 06:13:08 +000027#include <fstream>
Chris Lattner63202322001-11-26 19:22:39 +000028#include <memory>
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000029#include <algorithm>
Anand Shukla63aaa112002-06-25 21:43:28 +000030
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
Chris Lattner9d6e7eb2002-04-12 18:21:13 +000032
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000033// The OptimizationList is automatically populated with registered Passes by the
34// PassNameParser.
35//
Chris Lattner2053a2a2002-07-26 21:09:32 +000036static cl::list<const PassInfo*, bool,
37 FilteredPassNameParser<PassInfo::Optimization> >
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000038OptimizationList(cl::desc("Optimizations available:"));
39
40
41// Other command line options...
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000042//
Chris Lattner6c8103f2003-05-22 20:13:16 +000043static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000044InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
45
Chris Lattner6c8103f2003-05-22 20:13:16 +000046static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000047OutputFilename("o", cl::desc("Override output filename"),
Chris Lattnerb592fc22003-12-10 14:41:33 +000048 cl::value_desc("filename"), cl::init("-"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000049
50static cl::opt<bool>
51Force("f", cl::desc("Overwrite output files"));
52
53static cl::opt<bool>
54PrintEachXForm("p", cl::desc("Print module after each transformation"));
55
56static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000057NoOutput("disable-output",
58 cl::desc("Do not write result bytecode file"), cl::Hidden);
Chris Lattnerd70b68e2003-02-12 18:43:33 +000059
60static cl::opt<bool>
Chris Lattnerddd5b412003-02-26 20:00:41 +000061NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerf3bafc12003-02-12 18:45:08 +000062
63static cl::opt<bool>
Chris Lattner3153e4f2004-05-27 20:32:10 +000064Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
Chris Lattner5ff62e92002-07-22 02:10:13 +000065
Reid Spencerec7eb452004-05-27 16:28:54 +000066static cl::alias
67QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
68
Chris Lattner0be41012002-02-01 04:54:11 +000069
Chris Lattnerc0ce68b2002-07-23 18:12:22 +000070//===----------------------------------------------------------------------===//
71// main for opt
72//
Chris Lattner00950542001-06-06 20:29:01 +000073int main(int argc, char **argv) {
Chris Lattner8f367bd2001-07-23 02:35:57 +000074 cl::ParseCommandLineOptions(argc, argv,
75 " llvm .bc -> .bc modular optimizer\n");
Chris Lattnerf73b4ca2004-02-19 20:32:12 +000076 PrintStackTraceOnErrorSignal();
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000077
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000078 // Allocate a full target machine description only if necessary...
79 // FIXME: The choice of target should be controllable on the command line.
80 std::auto_ptr<TargetMachine> target;
81
82 TargetMachine* TM = NULL;
Chris Lattner56620da2003-04-16 20:51:36 +000083 std::string ErrorMessage;
Vikram S. Adve18fdfc42002-09-16 16:09:43 +000084
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000085 // Load the input module...
Chris Lattner56620da2003-04-16 20:51:36 +000086 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattner63202322001-11-26 19:22:39 +000087 if (M.get() == 0) {
Chris Lattner6c8103f2003-05-22 20:13:16 +000088 std::cerr << argv[0] << ": ";
Chris Lattner56620da2003-04-16 20:51:36 +000089 if (ErrorMessage.size())
Chris Lattner6c8103f2003-05-22 20:13:16 +000090 std::cerr << ErrorMessage << "\n";
Chris Lattner56620da2003-04-16 20:51:36 +000091 else
Chris Lattner6c8103f2003-05-22 20:13:16 +000092 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner00950542001-06-06 20:29:01 +000093 return 1;
94 }
95
Chris Lattnerfb1b3f12002-01-31 00:47:12 +000096 // Figure out what stream we are supposed to write to...
Chris Lattner697954c2002-01-20 22:54:45 +000097 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattnerb592fc22003-12-10 14:41:33 +000098 if (OutputFilename != "-") {
Chris Lattner888912d2002-01-22 21:07:24 +000099 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000100 // If force is not specified, make sure not to overwrite a file!
Chris Lattner6c8103f2003-05-22 20:13:16 +0000101 std::cerr << argv[0] << ": error opening '" << OutputFilename
102 << "': file exists!\n"
103 << "Use -f command line argument to force output\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000104 return 1;
105 }
106 Out = new std::ofstream(OutputFilename.c_str());
107
Chris Lattner00950542001-06-06 20:29:01 +0000108 if (!Out->good()) {
Chris Lattner6c8103f2003-05-22 20:13:16 +0000109 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000110 return 1;
111 }
Chris Lattner76d12292002-04-18 19:55:25 +0000112
Misha Brukman452fea92003-10-10 17:56:49 +0000113 // Make sure that the Output file gets unlinked from the disk if we get a
Chris Lattner76d12292002-04-18 19:55:25 +0000114 // SIGINT
115 RemoveFileOnSignal(OutputFilename);
Chris Lattner00950542001-06-06 20:29:01 +0000116 }
117
Chris Lattner76351aa2004-04-02 05:06:57 +0000118 // If the output is set to be emitted to standard out, and standard out is a
119 // console, print out a warning message and refuse to do it. We don't impress
120 // anyone by spewing tons of binary goo to a terminal.
Reid Spencerec7eb452004-05-27 16:28:54 +0000121 if (Out == &std::cout && isStandardOutAConsole() && !Force && !NoOutput
122 && !Quiet) {
Chris Lattner76351aa2004-04-02 05:06:57 +0000123 std::cerr << "WARNING: It looks like you're attempting to print out a "
124 << "bytecode file. I'm\ngoing to pretend you didn't ask me to do"
125 << " this (for your own good). If you\nREALLY want to taste LLVM"
126 << " bytecode first hand, you can force output with the\n'-f'"
127 << " option.\n\n";
128 NoOutput = true;
129 }
130
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000131 // Create a PassManager to hold and optimize the collection of passes we are
132 // about to build...
133 //
134 PassManager Passes;
Chris Lattner00950542001-06-06 20:29:01 +0000135
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000136 // Add an appropriate TargetData instance for this module...
137 Passes.add(new TargetData("opt", M.get()));
138
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000139 // Create a new optimization pass for each one specified on the command line
140 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
Chris Lattnerc0ce68b2002-07-23 18:12:22 +0000141 const PassInfo *Opt = OptimizationList[i];
142
143 if (Opt->getNormalCtor())
144 Passes.add(Opt->getNormalCtor()());
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000145 else if (Opt->getTargetCtor()) {
Chris Lattner0af1e8e2003-04-16 23:02:16 +0000146#if 0
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000147 if (target.get() == NULL)
148 target.reset(allocateSparcTargetMachine()); // FIXME: target option
Chris Lattner0af1e8e2003-04-16 23:02:16 +0000149#endif
Vikram S. Adve18fdfc42002-09-16 16:09:43 +0000150 assert(target.get() && "Could not allocate target machine!");
151 Passes.add(Opt->getTargetCtor()(*target.get()));
152 } else
Chris Lattner6c8103f2003-05-22 20:13:16 +0000153 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
154 << "\n";
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000155
156 if (PrintEachXForm)
Chris Lattner6c8103f2003-05-22 20:13:16 +0000157 Passes.add(new PrintModulePass(&std::cerr));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000158 }
159
Chris Lattner22d26d72002-02-20 17:56:53 +0000160 // Check that the module is well formed on completion of optimization
Chris Lattnerf3bafc12003-02-12 18:45:08 +0000161 if (!NoVerify)
162 Passes.add(createVerifierPass());
Chris Lattner22d26d72002-02-20 17:56:53 +0000163
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000164 // Write bytecode out to disk or cout as the last step...
Chris Lattnerd70b68e2003-02-12 18:43:33 +0000165 if (!NoOutput)
166 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000167
168 // Now that we have all of the passes ready, run them.
Chris Lattner3153e4f2004-05-27 20:32:10 +0000169 Passes.run(*M.get());
Chris Lattnerfb1b3f12002-01-31 00:47:12 +0000170
Reid Spencerec7eb452004-05-27 16:28:54 +0000171 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000172}