blob: e70b7b00622c3f1e88ec08a0ecac78497f8da398 [file] [log] [blame]
Chris Lattner46e18c72004-04-02 05:06:57 +00001//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
John Criswell09344dc2003-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 Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner2f7c9632001-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 Lattner6fc7ff42001-10-18 06:05:15 +000013//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +000014
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include "llvm/Module.h"
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000016#include "llvm/PassManager.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/Bytecode/Reader.h"
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000018#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner39239672001-10-19 15:39:14 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnerf608d7f2002-02-20 17:56:53 +000020#include "llvm/Analysis/Verifier.h"
Vikram S. Adve82491b72002-09-16 16:09:43 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattnera9d552b2002-10-29 20:48:09 +000022#include "llvm/Target/TargetMachineImpls.h"
Chris Lattnere79bf382002-07-26 21:09:32 +000023#include "llvm/Support/PassNameParser.h"
Chris Lattner278f5152004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Chris Lattner46e18c72004-04-02 05:06:57 +000025#include "Support/SystemUtils.h"
Chris Lattnered8947f2001-10-18 06:13:08 +000026#include <fstream>
Chris Lattnerba5220d2001-11-26 19:22:39 +000027#include <memory>
Chris Lattner5a48a242002-07-23 18:12:22 +000028#include <algorithm>
Anand Shukla68c99772002-06-25 21:43:28 +000029
Brian Gaeke960707c2003-11-11 22:41:34 +000030using namespace llvm;
Chris Lattnerb86b11a2002-04-12 18:21:13 +000031
Chris Lattner5a48a242002-07-23 18:12:22 +000032// The OptimizationList is automatically populated with registered Passes by the
33// PassNameParser.
34//
Chris Lattnere79bf382002-07-26 21:09:32 +000035static cl::list<const PassInfo*, bool,
36 FilteredPassNameParser<PassInfo::Optimization> >
Chris Lattner5a48a242002-07-23 18:12:22 +000037OptimizationList(cl::desc("Optimizations available:"));
38
39
40// Other command line options...
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000041//
Chris Lattner02a16832003-05-22 20:13:16 +000042static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000043InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
44
Chris Lattner02a16832003-05-22 20:13:16 +000045static cl::opt<std::string>
Chris Lattnerf5cad152002-07-22 02:10:13 +000046OutputFilename("o", cl::desc("Override output filename"),
Chris Lattner98cd4bf2003-12-10 14:41:33 +000047 cl::value_desc("filename"), cl::init("-"));
Chris Lattnerf5cad152002-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 Lattner30f40d92003-02-26 20:00:41 +000056NoOutput("disable-output",
57 cl::desc("Do not write result bytecode file"), cl::Hidden);
Chris Lattner4dbe59b2003-02-12 18:43:33 +000058
59static cl::opt<bool>
Chris Lattner30f40d92003-02-26 20:00:41 +000060NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
Chris Lattnerb84505992003-02-12 18:45:08 +000061
62static cl::opt<bool>
Chris Lattnerb05cd8a2002-07-31 16:52:49 +000063Quiet("q", cl::desc("Don't print 'program modified' message"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000064
Chris Lattner4db2f2c2002-02-01 04:54:11 +000065
Chris Lattner5a48a242002-07-23 18:12:22 +000066//===----------------------------------------------------------------------===//
67// main for opt
68//
Chris Lattner2f7c9632001-06-06 20:29:01 +000069int main(int argc, char **argv) {
Chris Lattner0af24642001-07-23 02:35:57 +000070 cl::ParseCommandLineOptions(argc, argv,
71 " llvm .bc -> .bc modular optimizer\n");
Chris Lattner12439ff2004-02-19 20:32:12 +000072 PrintStackTraceOnErrorSignal();
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000073
Vikram S. Adve82491b72002-09-16 16:09:43 +000074 // Allocate a full target machine description only if necessary...
75 // FIXME: The choice of target should be controllable on the command line.
76 std::auto_ptr<TargetMachine> target;
77
78 TargetMachine* TM = NULL;
Chris Lattnerf46a02c2003-04-16 20:51:36 +000079 std::string ErrorMessage;
Vikram S. Adve82491b72002-09-16 16:09:43 +000080
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000081 // Load the input module...
Chris Lattnerf46a02c2003-04-16 20:51:36 +000082 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
Chris Lattnerba5220d2001-11-26 19:22:39 +000083 if (M.get() == 0) {
Chris Lattner02a16832003-05-22 20:13:16 +000084 std::cerr << argv[0] << ": ";
Chris Lattnerf46a02c2003-04-16 20:51:36 +000085 if (ErrorMessage.size())
Chris Lattner02a16832003-05-22 20:13:16 +000086 std::cerr << ErrorMessage << "\n";
Chris Lattnerf46a02c2003-04-16 20:51:36 +000087 else
Chris Lattner02a16832003-05-22 20:13:16 +000088 std::cerr << "bytecode didn't read correctly.\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +000089 return 1;
90 }
91
Chris Lattnerc90d6ba2002-01-31 00:47:12 +000092 // Figure out what stream we are supposed to write to...
Chris Lattner7f74a562002-01-20 22:54:45 +000093 std::ostream *Out = &std::cout; // Default to printing to stdout...
Chris Lattner98cd4bf2003-12-10 14:41:33 +000094 if (OutputFilename != "-") {
Chris Lattner0e11e542002-01-22 21:07:24 +000095 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner7f74a562002-01-20 22:54:45 +000096 // If force is not specified, make sure not to overwrite a file!
Chris Lattner02a16832003-05-22 20:13:16 +000097 std::cerr << argv[0] << ": error opening '" << OutputFilename
98 << "': file exists!\n"
99 << "Use -f command line argument to force output\n";
Chris Lattner7f74a562002-01-20 22:54:45 +0000100 return 1;
101 }
102 Out = new std::ofstream(OutputFilename.c_str());
103
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104 if (!Out->good()) {
Chris Lattner02a16832003-05-22 20:13:16 +0000105 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000106 return 1;
107 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000108
Misha Brukmand6769742003-10-10 17:56:49 +0000109 // Make sure that the Output file gets unlinked from the disk if we get a
Chris Lattnerc065ad82002-04-18 19:55:25 +0000110 // SIGINT
111 RemoveFileOnSignal(OutputFilename);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000112 }
113
Chris Lattner46e18c72004-04-02 05:06:57 +0000114 // If the output is set to be emitted to standard out, and standard out is a
115 // console, print out a warning message and refuse to do it. We don't impress
116 // anyone by spewing tons of binary goo to a terminal.
117 if (Out == &std::cout && isStandardOutAConsole() && !Force && !NoOutput) {
118 std::cerr << "WARNING: It looks like you're attempting to print out a "
119 << "bytecode file. I'm\ngoing to pretend you didn't ask me to do"
120 << " this (for your own good). If you\nREALLY want to taste LLVM"
121 << " bytecode first hand, you can force output with the\n'-f'"
122 << " option.\n\n";
123 NoOutput = true;
124 }
125
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000126 // Create a PassManager to hold and optimize the collection of passes we are
127 // about to build...
128 //
129 PassManager Passes;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130
Chris Lattnerd571e2a2003-04-24 19:13:02 +0000131 // Add an appropriate TargetData instance for this module...
132 Passes.add(new TargetData("opt", M.get()));
133
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000134 // Create a new optimization pass for each one specified on the command line
135 for (unsigned i = 0; i < OptimizationList.size(); ++i) {
Chris Lattner5a48a242002-07-23 18:12:22 +0000136 const PassInfo *Opt = OptimizationList[i];
137
138 if (Opt->getNormalCtor())
139 Passes.add(Opt->getNormalCtor()());
Vikram S. Adve82491b72002-09-16 16:09:43 +0000140 else if (Opt->getTargetCtor()) {
Chris Lattner52c71382003-04-16 23:02:16 +0000141#if 0
Vikram S. Adve82491b72002-09-16 16:09:43 +0000142 if (target.get() == NULL)
143 target.reset(allocateSparcTargetMachine()); // FIXME: target option
Chris Lattner52c71382003-04-16 23:02:16 +0000144#endif
Vikram S. Adve82491b72002-09-16 16:09:43 +0000145 assert(target.get() && "Could not allocate target machine!");
146 Passes.add(Opt->getTargetCtor()(*target.get()));
147 } else
Chris Lattner02a16832003-05-22 20:13:16 +0000148 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
149 << "\n";
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000150
151 if (PrintEachXForm)
Chris Lattner02a16832003-05-22 20:13:16 +0000152 Passes.add(new PrintModulePass(&std::cerr));
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000153 }
154
Chris Lattnerf608d7f2002-02-20 17:56:53 +0000155 // Check that the module is well formed on completion of optimization
Chris Lattnerb84505992003-02-12 18:45:08 +0000156 if (!NoVerify)
157 Passes.add(createVerifierPass());
Chris Lattnerf608d7f2002-02-20 17:56:53 +0000158
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000159 // Write bytecode out to disk or cout as the last step...
Chris Lattner4dbe59b2003-02-12 18:43:33 +0000160 if (!NoOutput)
161 Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000162
163 // Now that we have all of the passes ready, run them.
Reid Spencer78736712004-05-27 08:26:22 +0000164 if (Passes.run(*M.get()))
165 return 0;
Chris Lattnerc90d6ba2002-01-31 00:47:12 +0000166
Reid Spencer78736712004-05-27 08:26:22 +0000167 return 1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168}