2001-10-18 06:05:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00: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 06:05:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-01-31 00:47:12 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2002-01-31 00:47:12 +00:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2001-10-19 15:39:14 +00:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2002-02-20 17:56:53 +00:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2001-10-18 20:06:45 +00:00
|
|
|
#include "llvm/Transforms/ConstantMerge.h"
|
2001-10-31 04:29:44 +00:00
|
|
|
#include "llvm/Transforms/CleanupGCCOutput.h"
|
2001-11-01 02:41:09 +00:00
|
|
|
#include "llvm/Transforms/LevelChange.h"
|
2002-04-27 07:27:19 +00:00
|
|
|
#include "llvm/Transforms/FunctionInlining.h"
|
2002-01-22 01:04:08 +00:00
|
|
|
#include "llvm/Transforms/ChangeAllocations.h"
|
2002-05-07 20:03:27 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-01-21 07:52:35 +00:00
|
|
|
#include "llvm/Transforms/IPO/SimpleStructMutation.h"
|
2002-04-28 05:49:53 +00:00
|
|
|
#include "llvm/Transforms/IPO/Internalize.h"
|
2001-11-26 19:22:39 +00:00
|
|
|
#include "llvm/Transforms/IPO/GlobalDCE.h"
|
2002-03-28 18:08:07 +00:00
|
|
|
#include "llvm/Transforms/IPO/PoolAllocate.h"
|
2002-05-07 19:18:48 +00:00
|
|
|
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
|
2002-01-21 23:17:48 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/TraceValues.h"
|
2002-02-26 19:57:59 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/ProfilePaths.h"
|
2002-05-10 15:43:07 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2002-04-18 19:55:25 +00:00
|
|
|
#include "Support/Signals.h"
|
2001-10-18 06:13:08 +00:00
|
|
|
#include <fstream>
|
2001-11-26 19:22:39 +00:00
|
|
|
#include <memory>
|
2002-06-25 21:43:28 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using std::cerr;
|
2001-06-30 06:38:31 +00:00
|
|
|
|
2002-05-10 15:43:07 +00:00
|
|
|
// FIXME: This should be parameterizable eventually for different target
|
|
|
|
// types...
|
|
|
|
static TargetData TD("opt target");
|
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// Opts enum - All of the transformations we can do...
|
2001-07-23 02:35:57 +00:00
|
|
|
enum Opts {
|
|
|
|
// Basic optimizations
|
2002-05-10 22:44:37 +00:00
|
|
|
dce, die, constprop, gcse, licm, inlining, constmerge,
|
2002-05-21 20:05:16 +00:00
|
|
|
strip, mstrip, mergereturn, simplifycfg,
|
2001-07-23 02:35:57 +00:00
|
|
|
|
2001-10-18 06:05:15 +00:00
|
|
|
// Miscellaneous Transformations
|
2002-05-10 15:43:07 +00:00
|
|
|
raiseallocs, lowerallocs, funcresolve, cleangcc, lowerrefs,
|
2002-02-20 17:56:53 +00:00
|
|
|
|
|
|
|
// Printing and verifying...
|
2002-04-13 18:32:47 +00:00
|
|
|
print, printm, verify,
|
2001-10-18 06:05:15 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
// More powerful optimizations
|
2002-05-10 05:41:49 +00:00
|
|
|
indvars, instcombine, sccp, adce, raise, reassociate, mem2reg, pinodes,
|
2001-11-26 19:22:39 +00:00
|
|
|
|
2002-02-26 19:57:59 +00:00
|
|
|
// Instrumentation
|
|
|
|
trace, tracem, paths,
|
|
|
|
|
2001-11-26 19:22:39 +00:00
|
|
|
// Interprocedural optimizations...
|
2002-04-28 05:49:53 +00:00
|
|
|
internalize, globaldce, swapstructs, sortstructs, poolalloc,
|
2001-07-23 02:35:57 +00:00
|
|
|
};
|
|
|
|
|
2002-04-13 18:32:47 +00:00
|
|
|
static Pass *createPrintFunctionPass() {
|
|
|
|
return new PrintFunctionPass("Current Function: \n", &cerr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pass *createPrintModulePass() {
|
|
|
|
return new PrintModulePass(&cerr);
|
2002-01-31 00:47:12 +00:00
|
|
|
}
|
|
|
|
|
2002-05-10 15:43:07 +00:00
|
|
|
static Pass *createLowerAllocationsPassNT() {
|
|
|
|
return createLowerAllocationsPass(TD);
|
|
|
|
}
|
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// OptTable - Correlate enum Opts to Pass constructors...
|
|
|
|
//
|
2001-06-06 20:29:01 +00:00
|
|
|
struct {
|
2001-07-23 02:35:57 +00:00
|
|
|
enum Opts OptID;
|
2002-01-31 00:47:12 +00:00
|
|
|
Pass * (*PassCtor)();
|
2001-06-06 20:29:01 +00:00
|
|
|
} OptTable[] = {
|
2002-05-21 20:05:16 +00:00
|
|
|
{ dce , createDeadCodeEliminationPass },
|
|
|
|
{ die , createDeadInstEliminationPass },
|
|
|
|
{ constprop , createConstantPropogationPass },
|
|
|
|
{ gcse , createGCSEPass },
|
|
|
|
{ licm , createLICMPass },
|
|
|
|
{ inlining , createFunctionInliningPass },
|
|
|
|
{ constmerge , createConstantMergePass },
|
|
|
|
{ strip , createSymbolStrippingPass },
|
|
|
|
{ mstrip , createFullSymbolStrippingPass },
|
2002-04-27 07:27:19 +00:00
|
|
|
{ mergereturn, createUnifyFunctionExitNodesPass },
|
2002-05-21 20:05:16 +00:00
|
|
|
{ simplifycfg, createCFGSimplificationPass },
|
2002-02-26 21:46:54 +00:00
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
{ indvars , createIndVarSimplifyPass },
|
|
|
|
{ instcombine, createInstructionCombiningPass },
|
|
|
|
{ sccp , createSCCPPass },
|
2002-05-07 22:15:01 +00:00
|
|
|
{ adce , createAggressiveDCEPass },
|
2002-02-26 21:46:54 +00:00
|
|
|
{ raise , createRaisePointerReferencesPass },
|
2002-05-08 22:18:34 +00:00
|
|
|
{ reassociate, createReassociatePass },
|
2002-04-27 06:56:12 +00:00
|
|
|
{ mem2reg , createPromoteMemoryToRegister },
|
2002-05-10 05:41:49 +00:00
|
|
|
{ pinodes , createPiNodeInsertionPass },
|
|
|
|
{ lowerrefs , createDecomposeMultiDimRefsPass },
|
2002-02-12 17:17:33 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
{ trace , createTraceValuesPassForBasicBlocks },
|
2002-04-27 06:56:12 +00:00
|
|
|
{ tracem , createTraceValuesPassForFunction },
|
2002-05-21 20:05:16 +00:00
|
|
|
{ paths , createProfilePathsPass },
|
2002-04-13 18:32:47 +00:00
|
|
|
{ print , createPrintFunctionPass },
|
2002-04-27 06:56:12 +00:00
|
|
|
{ printm , createPrintModulePass },
|
|
|
|
{ verify , createVerifierPass },
|
2002-02-26 20:04:59 +00:00
|
|
|
|
2002-05-10 15:43:07 +00:00
|
|
|
{ raiseallocs, createRaiseAllocationsPass },
|
|
|
|
{ lowerallocs, createLowerAllocationsPassNT },
|
|
|
|
{ cleangcc , createCleanupGCCOutputPass },
|
|
|
|
{ funcresolve, createFunctionResolvingPass },
|
2002-04-28 05:49:53 +00:00
|
|
|
|
|
|
|
{ internalize, createInternalizePass },
|
2002-04-27 06:56:12 +00:00
|
|
|
{ globaldce , createGlobalDCEPass },
|
2002-02-26 21:46:54 +00:00
|
|
|
{ swapstructs, createSwapElementsPass },
|
|
|
|
{ sortstructs, createSortElementsPass },
|
2002-03-28 18:08:07 +00:00
|
|
|
{ poolalloc , createPoolAllocatePass },
|
2001-06-06 20:29:01 +00:00
|
|
|
};
|
|
|
|
|
2002-04-12 18:21:13 +00:00
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// Command line option handling code...
|
|
|
|
//
|
2001-07-23 20:22:30 +00:00
|
|
|
cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
|
|
|
|
cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
|
|
|
|
cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
|
2002-01-31 00:47:12 +00:00
|
|
|
cl::Flag PrintEachXForm("p", "Print module after each transformation");
|
2001-07-23 02:35:57 +00:00
|
|
|
cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
|
2001-07-23 20:22:30 +00:00
|
|
|
cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
|
2001-07-23 02:35:57 +00:00
|
|
|
cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(dce , "Dead Code Elimination"),
|
2002-03-14 22:36:15 +00:00
|
|
|
clEnumVal(die , "Dead Instruction Elimination"),
|
2002-02-01 04:54:11 +00:00
|
|
|
clEnumVal(constprop , "Simple constant propogation"),
|
2002-04-28 00:48:17 +00:00
|
|
|
clEnumVal(gcse , "Global Common Subexpression Elimination"),
|
2002-05-10 22:44:37 +00:00
|
|
|
clEnumVal(licm , "Loop Invariant Code Motion"),
|
2002-04-13 18:32:47 +00:00
|
|
|
clEnumValN(inlining , "inline", "Function integration"),
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(constmerge , "Merge identical global constants"),
|
2002-02-01 04:54:11 +00:00
|
|
|
clEnumVal(strip , "Strip symbols"),
|
|
|
|
clEnumVal(mstrip , "Strip module symbols"),
|
2002-04-13 18:32:47 +00:00
|
|
|
clEnumVal(mergereturn, "Unify function exit nodes"),
|
2002-05-21 20:05:16 +00:00
|
|
|
clEnumVal(simplifycfg, "CFG Simplification"),
|
2002-02-01 04:54:11 +00:00
|
|
|
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(indvars , "Simplify Induction Variables"),
|
2002-01-31 00:47:12 +00:00
|
|
|
clEnumVal(instcombine, "Combine redundant instructions"),
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
|
2002-05-07 22:15:01 +00:00
|
|
|
clEnumVal(adce , "Aggressive DCE"),
|
2002-05-08 22:18:34 +00:00
|
|
|
clEnumVal(reassociate, "Reassociate expressions"),
|
2002-02-12 17:17:33 +00:00
|
|
|
clEnumVal(mem2reg , "Promote alloca locations to registers"),
|
2002-05-10 05:41:49 +00:00
|
|
|
clEnumVal(pinodes , "Insert Pi nodes after definitions"),
|
2001-11-10 07:16:10 +00:00
|
|
|
|
2002-04-28 05:49:53 +00:00
|
|
|
clEnumVal(internalize, "Mark all fn's internal except for main"),
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(globaldce , "Remove unreachable globals"),
|
2001-11-26 19:22:39 +00:00
|
|
|
clEnumVal(swapstructs, "Swap structure types around"),
|
2002-01-21 07:31:50 +00:00
|
|
|
clEnumVal(sortstructs, "Sort structure elements"),
|
2002-03-28 18:08:07 +00:00
|
|
|
clEnumVal(poolalloc , "Pool allocate disjoint datastructures"),
|
2001-11-26 19:22:39 +00:00
|
|
|
|
2002-01-22 00:13:51 +00:00
|
|
|
clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
|
2002-05-10 15:43:07 +00:00
|
|
|
clEnumVal(lowerallocs, "Lower allocations from instructions to calls (TD)"),
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(cleangcc , "Cleanup GCC Output"),
|
2002-04-12 18:21:13 +00:00
|
|
|
clEnumVal(funcresolve, "Resolve calls to foo(...) to foo(<concrete types>)"),
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(raise , "Raise to Higher Level"),
|
2002-04-13 18:32:47 +00:00
|
|
|
clEnumVal(trace , "Insert BB and Function trace code"),
|
|
|
|
clEnumVal(tracem , "Insert Function trace code only"),
|
2002-02-26 19:57:59 +00:00
|
|
|
clEnumVal(paths , "Insert path profiling instrumentation"),
|
2002-04-13 18:32:47 +00:00
|
|
|
clEnumVal(print , "Print working function to stderr"),
|
|
|
|
clEnumVal(printm , "Print working module to stderr"),
|
2002-02-20 17:56:53 +00:00
|
|
|
clEnumVal(verify , "Verify module is well formed"),
|
2002-03-24 03:19:54 +00:00
|
|
|
clEnumVal(lowerrefs , "Decompose multi-dimensional structure/array refs to use one index per instruction"),
|
2001-07-23 02:35:57 +00:00
|
|
|
0);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
" llvm .bc -> .bc modular optimizer\n");
|
2002-01-31 00:47:12 +00:00
|
|
|
|
|
|
|
// Load the input module...
|
2001-11-26 19:22:39 +00:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
|
|
|
|
if (M.get() == 0) {
|
2001-06-06 20:29:01 +00:00
|
|
|
cerr << "bytecode didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// Figure out what stream we are supposed to write to...
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream *Out = &std::cout; // Default to printing to stdout...
|
2001-07-23 19:27:24 +00:00
|
|
|
if (OutputFilename != "") {
|
2002-01-22 21:07:24 +00:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-20 22:54:45 +00: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-06 20:29:01 +00:00
|
|
|
if (!Out->good()) {
|
2001-07-23 19:27:24 +00:00
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-04-18 19:55:25 +00:00
|
|
|
|
|
|
|
// Make sure that the Output file gets unlink'd from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2002-01-31 00:47:12 +00: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) {
|
|
|
|
enum Opts Opt = OptimizationList[i];
|
|
|
|
for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
|
|
|
|
if (Opt == OptTable[j].OptID) {
|
|
|
|
Passes.add(OptTable[j].PassCtor());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PrintEachXForm)
|
|
|
|
Passes.add(new PrintModulePass(&std::cerr));
|
|
|
|
}
|
|
|
|
|
2002-02-20 17:56:53 +00:00
|
|
|
// Check that the module is well formed on completion of optimization
|
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
2002-01-31 00:47:12 +00: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-25 16:13:24 +00:00
|
|
|
if (Passes.run(*M.get()) && !Quiet)
|
2002-01-31 00:47:12 +00:00
|
|
|
cerr << "Program modified.\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|