2001-10-18 14:05:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
// LLVM 'OPT' UTILITY
|
|
|
|
//
|
|
|
|
// 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-05-10 23:43:07 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include "Support/CommandLine.h"
|
2002-04-19 03:55:25 +08:00
|
|
|
#include "Support/Signals.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
|
|
|
|
|
|
|
using std::cerr;
|
2002-07-26 00:31:09 +08:00
|
|
|
using std::string;
|
2002-05-10 23:43:07 +08:00
|
|
|
|
2002-07-24 02:12:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PassNameParser class - Make use of the pass registration mechanism to
|
|
|
|
// automatically add a command line argument to opt for each pass.
|
|
|
|
//
|
|
|
|
namespace { // anonymous namespace for local class...
|
|
|
|
class PassNameParser : public PassRegistrationListener,
|
|
|
|
public cl::parser<const PassInfo*> {
|
|
|
|
cl::Option *Opt;
|
|
|
|
public:
|
|
|
|
PassNameParser() : Opt(0) {}
|
|
|
|
|
|
|
|
void initialize(cl::Option &O) {
|
|
|
|
Opt = &O;
|
|
|
|
cl::parser<const PassInfo*>::initialize(O);
|
|
|
|
|
|
|
|
// Add all of the passes to the map that got initialized before 'this' did.
|
|
|
|
enumeratePasses();
|
|
|
|
}
|
2002-02-21 01:56:53 +08:00
|
|
|
|
2002-07-24 02:12:22 +08:00
|
|
|
static inline bool ignorablePass(const PassInfo *P) {
|
|
|
|
// Ignore non-selectable and non-constructible passes!
|
|
|
|
return P->getPassArgument() == 0 ||
|
|
|
|
(P->getNormalCtor() == 0 && P->getDataCtor() == 0);
|
|
|
|
}
|
2001-10-18 14:05:15 +08:00
|
|
|
|
2002-07-24 02:12:22 +08:00
|
|
|
// Implement the PassRegistrationListener callbacks used to populate our map
|
|
|
|
//
|
|
|
|
virtual void passRegistered(const PassInfo *P) {
|
|
|
|
if (ignorablePass(P) || !Opt) return;
|
|
|
|
assert(findOption(P->getPassArgument()) == getNumOptions() &&
|
|
|
|
"Two passes with the same argument attempted to be registered!");
|
|
|
|
addLiteralOption(P->getPassArgument(), P, P->getPassName());
|
|
|
|
Opt->addArgument(P->getPassArgument());
|
|
|
|
}
|
|
|
|
virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
|
|
|
|
|
|
|
|
virtual void passUnregistered(const PassInfo *P) {
|
|
|
|
if (ignorablePass(P) || !Opt) return;
|
|
|
|
assert(findOption(P->getPassArgument()) != getNumOptions() &&
|
|
|
|
"Registered Pass not in the pass map!");
|
|
|
|
removeLiteralOption(P->getPassArgument());
|
|
|
|
Opt->removeArgument(P->getPassArgument());
|
|
|
|
}
|
2001-11-27 03:22:39 +08:00
|
|
|
|
2002-07-24 02:12:22 +08:00
|
|
|
// ValLessThan - Provide a sorting comparator for Values elements...
|
|
|
|
typedef std::pair<const char*,
|
|
|
|
std::pair<const PassInfo*, const char*> > ValType;
|
|
|
|
static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
|
|
|
|
return std::string(VT1.first) < std::string(VT2.first);
|
|
|
|
}
|
2002-02-27 03:57:59 +08:00
|
|
|
|
2002-07-24 02:12:22 +08:00
|
|
|
// printOptionInfo - Print out information about this option. Override the
|
|
|
|
// default implementation to sort the table before we print...
|
|
|
|
virtual void printOptionInfo(const cl::Option &O, unsigned GlobalWidth) const{
|
|
|
|
PassNameParser *PNP = const_cast<PassNameParser*>(this);
|
|
|
|
std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
|
|
|
|
cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
|
|
|
|
}
|
2001-07-23 10:35:57 +08:00
|
|
|
};
|
2002-07-24 02:12:22 +08:00
|
|
|
} // end anonymous namespace
|
2001-07-23 10:35:57 +08:00
|
|
|
|
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-24 02:12:22 +08:00
|
|
|
static cl::list<const PassInfo*, bool, PassNameParser>
|
|
|
|
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
|
|
|
//
|
2002-07-22 10:10:13 +08:00
|
|
|
static cl::opt<string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Overwrite output files"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
PrintEachXForm("p", cl::desc("Print module after each transformation"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Quiet("q", cl::desc("Don't print modifying pass names"));
|
|
|
|
|
|
|
|
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");
|
2002-01-31 08:47:12 +08:00
|
|
|
|
2002-07-24 02:12:22 +08:00
|
|
|
// FIXME: This should be parameterizable eventually for different target
|
|
|
|
// types...
|
|
|
|
TargetData TD("opt target");
|
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Load the input module...
|
2001-11-27 03:22:39 +08:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
|
|
|
|
if (M.get() == 0) {
|
2001-06-07 04:29:01 +08:00
|
|
|
cerr << "bytecode didn't read correctly.\n";
|
|
|
|
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...
|
2001-07-24 03:27:24 +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!
|
|
|
|
cerr << "Error opening '" << OutputFilename << "': File exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
if (!Out->good()) {
|
2001-07-24 03:27:24 +08:00
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
2001-06-07 04:29:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2002-04-19 03:55:25 +08:00
|
|
|
|
|
|
|
// Make sure that the Output file gets unlink'd from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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()());
|
|
|
|
else if (Opt->getDataCtor())
|
|
|
|
Passes.add(Opt->getDataCtor()(TD)); // Pass dummy target data...
|
|
|
|
else
|
|
|
|
cerr << "Cannot create pass: " << Opt->getPassName() << "\n";
|
2002-01-31 08:47:12 +08:00
|
|
|
|
|
|
|
if (PrintEachXForm)
|
2002-07-24 02:12:22 +08:00
|
|
|
Passes.add(new PrintModulePass(&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
|
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Write bytecode out to disk or cout as the last step...
|
|
|
|
Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
|
|
|
|
|
|
|
|
// Now that we have all of the passes ready, run them.
|
2002-06-26 00:13:24 +08:00
|
|
|
if (Passes.run(*M.get()) && !Quiet)
|
2002-01-31 08:47:12 +08:00
|
|
|
cerr << "Program modified.\n";
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|