2004-04-02 13:06:57 +08:00
|
|
|
//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
|
2003-10-21 01:47:21 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
// Optimizations may be specified an arbitrary number of times on the command
|
|
|
|
// line, they are run in the order specified.
|
|
|
|
//
|
2001-10-18 14:05:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-01-31 08:47:12 +08:00
|
|
|
#include "llvm/PassManager.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2002-01-31 08:47:12 +08:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2001-10-19 23:39:14 +08:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2002-02-21 01:56:53 +08:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2002-09-17 00:09:43 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2002-07-27 05:09:32 +08:00
|
|
|
#include "llvm/Support/PassNameParser.h"
|
2004-05-27 13:41:36 +08:00
|
|
|
#include "llvm/System/Signals.h"
|
2004-07-11 09:08:19 +08:00
|
|
|
#include "Support/PluginLoader.h"
|
2004-04-02 13:06:57 +08:00
|
|
|
#include "Support/SystemUtils.h"
|
2001-10-18 14:13:08 +08:00
|
|
|
#include <fstream>
|
2001-11-27 03:22:39 +08:00
|
|
|
#include <memory>
|
2002-07-24 02:12:22 +08:00
|
|
|
#include <algorithm>
|
2002-06-26 05:43:28 +08:00
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
using namespace llvm;
|
2002-04-14 02:32:47 +08:00
|
|
|
|
2002-07-24 02:12:22 +08:00
|
|
|
// The OptimizationList is automatically populated with registered Passes by the
|
|
|
|
// PassNameParser.
|
2002-01-31 08:47:12 +08:00
|
|
|
//
|
2002-07-27 05:09:32 +08:00
|
|
|
static cl::list<const PassInfo*, bool,
|
|
|
|
FilteredPassNameParser<PassInfo::Optimization> >
|
2002-07-24 02:12:22 +08:00
|
|
|
OptimizationList(cl::desc("Optimizations available:"));
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2002-04-13 02:21:13 +08:00
|
|
|
|
2002-07-24 02:12:22 +08:00
|
|
|
// Other command line options...
|
2002-01-31 08:47:12 +08:00
|
|
|
//
|
2003-05-23 04:13:16 +08:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 10:10:13 +08:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
|
|
|
|
|
2003-05-23 04:13:16 +08:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 10:10:13 +08:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
2003-12-10 22:41:33 +08:00
|
|
|
cl::value_desc("filename"), cl::init("-"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Overwrite output files"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
PrintEachXForm("p", cl::desc("Print module after each transformation"));
|
|
|
|
|
2003-02-13 02:43:33 +08:00
|
|
|
static cl::opt<bool>
|
2003-02-27 04:00:41 +08:00
|
|
|
NoOutput("disable-output",
|
|
|
|
cl::desc("Do not write result bytecode file"), cl::Hidden);
|
2003-02-13 02:43:33 +08:00
|
|
|
|
2003-02-13 02:45:08 +08:00
|
|
|
static cl::opt<bool>
|
2003-02-27 04:00:41 +08:00
|
|
|
NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
|
2003-02-13 02:45:08 +08:00
|
|
|
|
2002-07-22 10:10:13 +08:00
|
|
|
static cl::opt<bool>
|
2004-05-28 04:32:10 +08:00
|
|
|
Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2004-05-28 00:28:54 +08:00
|
|
|
static cl::alias
|
|
|
|
QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
|
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2002-07-24 02:12:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// main for opt
|
|
|
|
//
|
2001-07-23 10:35:57 +08:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
" llvm .bc -> .bc modular optimizer\n");
|
2004-08-30 03:28:55 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2002-01-31 08:47:12 +08:00
|
|
|
|
2002-09-17 00:09:43 +08:00
|
|
|
// Allocate a full target machine description only if necessary...
|
|
|
|
// FIXME: The choice of target should be controllable on the command line.
|
|
|
|
std::auto_ptr<TargetMachine> target;
|
|
|
|
|
|
|
|
TargetMachine* TM = NULL;
|
2003-04-17 04:51:36 +08:00
|
|
|
std::string ErrorMessage;
|
2002-09-17 00:09:43 +08:00
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Load the input module...
|
2003-04-17 04:51:36 +08:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
|
2001-11-27 03:22:39 +08:00
|
|
|
if (M.get() == 0) {
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << argv[0] << ": ";
|
2003-04-17 04:51:36 +08:00
|
|
|
if (ErrorMessage.size())
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << ErrorMessage << "\n";
|
2003-04-17 04:51:36 +08:00
|
|
|
else
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << "bytecode didn't read correctly.\n";
|
2001-06-07 04:29:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Figure out what stream we are supposed to write to...
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostream *Out = &std::cout; // Default to printing to stdout...
|
2003-12-10 22:41:33 +08:00
|
|
|
if (OutputFilename != "-") {
|
2002-01-23 05:07:24 +08:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-21 06:54:45 +08:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
2002-01-21 06:54:45 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
if (!Out->good()) {
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
|
2001-06-07 04:29:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2002-04-19 03:55:25 +08:00
|
|
|
|
2003-10-11 01:56:49 +08:00
|
|
|
// Make sure that the Output file gets unlinked from the disk if we get a
|
2002-04-19 03:55:25 +08:00
|
|
|
// SIGINT
|
2004-08-30 03:28:55 +08:00
|
|
|
sys::RemoveFileOnSignal(OutputFilename);
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2004-04-02 13:06:57 +08:00
|
|
|
// If the output is set to be emitted to standard out, and standard out is a
|
|
|
|
// console, print out a warning message and refuse to do it. We don't impress
|
|
|
|
// anyone by spewing tons of binary goo to a terminal.
|
2004-05-28 00:28:54 +08:00
|
|
|
if (Out == &std::cout && isStandardOutAConsole() && !Force && !NoOutput
|
|
|
|
&& !Quiet) {
|
2004-04-02 13:06:57 +08:00
|
|
|
std::cerr << "WARNING: It looks like you're attempting to print out a "
|
|
|
|
<< "bytecode file. I'm\ngoing to pretend you didn't ask me to do"
|
|
|
|
<< " this (for your own good). If you\nREALLY want to taste LLVM"
|
|
|
|
<< " bytecode first hand, you can force output with the\n'-f'"
|
|
|
|
<< " option.\n\n";
|
|
|
|
NoOutput = true;
|
|
|
|
}
|
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Create a PassManager to hold and optimize the collection of passes we are
|
|
|
|
// about to build...
|
|
|
|
//
|
|
|
|
PassManager Passes;
|
|
|
|
|
2003-04-25 03:13:02 +08:00
|
|
|
// Add an appropriate TargetData instance for this module...
|
|
|
|
Passes.add(new TargetData("opt", M.get()));
|
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Create a new optimization pass for each one specified on the command line
|
|
|
|
for (unsigned i = 0; i < OptimizationList.size(); ++i) {
|
2002-07-24 02:12:22 +08:00
|
|
|
const PassInfo *Opt = OptimizationList[i];
|
|
|
|
|
|
|
|
if (Opt->getNormalCtor())
|
|
|
|
Passes.add(Opt->getNormalCtor()());
|
2002-09-17 00:09:43 +08:00
|
|
|
else if (Opt->getTargetCtor()) {
|
2003-04-17 07:02:16 +08:00
|
|
|
#if 0
|
2002-09-17 00:09:43 +08:00
|
|
|
if (target.get() == NULL)
|
|
|
|
target.reset(allocateSparcTargetMachine()); // FIXME: target option
|
2003-04-17 07:02:16 +08:00
|
|
|
#endif
|
2002-09-17 00:09:43 +08:00
|
|
|
assert(target.get() && "Could not allocate target machine!");
|
|
|
|
Passes.add(Opt->getTargetCtor()(*target.get()));
|
|
|
|
} else
|
2003-05-23 04:13:16 +08:00
|
|
|
std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
|
|
|
|
<< "\n";
|
2002-01-31 08:47:12 +08:00
|
|
|
|
|
|
|
if (PrintEachXForm)
|
2003-05-23 04:13:16 +08:00
|
|
|
Passes.add(new PrintModulePass(&std::cerr));
|
2002-01-31 08:47:12 +08:00
|
|
|
}
|
|
|
|
|
2002-02-21 01:56:53 +08:00
|
|
|
// Check that the module is well formed on completion of optimization
|
2003-02-13 02:45:08 +08:00
|
|
|
if (!NoVerify)
|
|
|
|
Passes.add(createVerifierPass());
|
2002-02-21 01:56:53 +08:00
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Write bytecode out to disk or cout as the last step...
|
2003-02-13 02:43:33 +08:00
|
|
|
if (!NoOutput)
|
|
|
|
Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
|
2002-01-31 08:47:12 +08:00
|
|
|
|
|
|
|
// Now that we have all of the passes ready, run them.
|
2004-05-28 04:32:10 +08:00
|
|
|
Passes.run(*M.get());
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2004-05-28 00:28:54 +08:00
|
|
|
return 0;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|