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"
|
2001-10-19 04:06:45 +08:00
|
|
|
#include "llvm/Transforms/ConstantMerge.h"
|
2001-10-31 12:29:44 +08:00
|
|
|
#include "llvm/Transforms/CleanupGCCOutput.h"
|
2001-11-01 10:41:09 +08:00
|
|
|
#include "llvm/Transforms/LevelChange.h"
|
2002-04-27 15:27:19 +08:00
|
|
|
#include "llvm/Transforms/FunctionInlining.h"
|
2002-01-22 09:04:08 +08:00
|
|
|
#include "llvm/Transforms/ChangeAllocations.h"
|
2002-05-08 04:03:27 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-01-21 15:52:35 +08:00
|
|
|
#include "llvm/Transforms/IPO/SimpleStructMutation.h"
|
2002-04-28 13:49:53 +08:00
|
|
|
#include "llvm/Transforms/IPO/Internalize.h"
|
2001-11-27 03:22:39 +08:00
|
|
|
#include "llvm/Transforms/IPO/GlobalDCE.h"
|
2002-03-29 02:08:07 +08:00
|
|
|
#include "llvm/Transforms/IPO/PoolAllocate.h"
|
2002-05-08 03:18:48 +08:00
|
|
|
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
|
2002-01-22 07:17:48 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/TraceValues.h"
|
2002-02-27 03:57:59 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/ProfilePaths.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>
|
2001-06-30 14:38:31 +08:00
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Opts enum - All of the transformations we can do...
|
2001-07-23 10:35:57 +08:00
|
|
|
enum Opts {
|
|
|
|
// Basic optimizations
|
2002-04-28 08:48:17 +08:00
|
|
|
dce, die, constprop, gcse, inlining, constmerge, strip, mstrip, mergereturn,
|
2001-07-23 10:35:57 +08:00
|
|
|
|
2001-10-18 14:05:15 +08:00
|
|
|
// Miscellaneous Transformations
|
2002-04-13 02:21:13 +08:00
|
|
|
raiseallocs, funcresolve, cleangcc, lowerrefs,
|
2002-02-21 01:56:53 +08:00
|
|
|
|
|
|
|
// Printing and verifying...
|
2002-04-14 02:32:47 +08:00
|
|
|
print, printm, verify,
|
2001-10-18 14:05:15 +08:00
|
|
|
|
2001-07-23 10:35:57 +08:00
|
|
|
// More powerful optimizations
|
2002-05-09 06:18:34 +08:00
|
|
|
indvars, instcombine, sccp, adce, raise, reassociate, mem2reg,
|
2001-11-27 03:22:39 +08:00
|
|
|
|
2002-02-27 03:57:59 +08:00
|
|
|
// Instrumentation
|
|
|
|
trace, tracem, paths,
|
|
|
|
|
2001-11-27 03:22:39 +08:00
|
|
|
// Interprocedural optimizations...
|
2002-04-28 13:49:53 +08:00
|
|
|
internalize, globaldce, swapstructs, sortstructs, poolalloc,
|
2001-07-23 10:35:57 +08:00
|
|
|
};
|
|
|
|
|
2002-04-14 02:32:47 +08:00
|
|
|
static Pass *createPrintFunctionPass() {
|
|
|
|
return new PrintFunctionPass("Current Function: \n", &cerr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pass *createPrintModulePass() {
|
|
|
|
return new PrintModulePass(&cerr);
|
2002-01-31 08:47:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// OptTable - Correlate enum Opts to Pass constructors...
|
|
|
|
//
|
2001-06-07 04:29:01 +08:00
|
|
|
struct {
|
2001-07-23 10:35:57 +08:00
|
|
|
enum Opts OptID;
|
2002-01-31 08:47:12 +08:00
|
|
|
Pass * (*PassCtor)();
|
2001-06-07 04:29:01 +08:00
|
|
|
} OptTable[] = {
|
2002-04-27 14:56:12 +08:00
|
|
|
{ dce , createDeadCodeEliminationPass },
|
|
|
|
{ die , createDeadInstEliminationPass },
|
|
|
|
{ constprop , createConstantPropogationPass },
|
2002-04-28 08:48:17 +08:00
|
|
|
{ gcse , createGCSEPass },
|
2002-04-27 14:56:12 +08:00
|
|
|
{ inlining , createFunctionInliningPass },
|
|
|
|
{ constmerge , createConstantMergePass },
|
|
|
|
{ strip , createSymbolStrippingPass },
|
|
|
|
{ mstrip , createFullSymbolStrippingPass },
|
2002-04-27 15:27:19 +08:00
|
|
|
{ mergereturn, createUnifyFunctionExitNodesPass },
|
2002-02-27 05:46:54 +08:00
|
|
|
|
2002-04-27 14:56:12 +08:00
|
|
|
{ indvars , createIndVarSimplifyPass },
|
|
|
|
{ instcombine, createInstructionCombiningPass },
|
|
|
|
{ sccp , createSCCPPass },
|
2002-05-08 06:15:01 +08:00
|
|
|
{ adce , createAggressiveDCEPass },
|
2002-02-27 05:46:54 +08:00
|
|
|
{ raise , createRaisePointerReferencesPass },
|
2002-05-09 06:18:34 +08:00
|
|
|
{ reassociate, createReassociatePass },
|
2002-04-27 14:56:12 +08:00
|
|
|
{ mem2reg , createPromoteMemoryToRegister },
|
|
|
|
{ lowerrefs, createDecomposeMultiDimRefsPass },
|
2002-02-13 01:17:33 +08:00
|
|
|
|
2002-02-27 05:46:54 +08:00
|
|
|
{ trace , createTraceValuesPassForBasicBlocks },
|
2002-04-27 14:56:12 +08:00
|
|
|
{ tracem , createTraceValuesPassForFunction },
|
|
|
|
{ paths , createProfilePathsPass },
|
2002-02-27 04:04:59 +08:00
|
|
|
|
2002-04-14 02:32:47 +08:00
|
|
|
{ print , createPrintFunctionPass },
|
2002-04-27 14:56:12 +08:00
|
|
|
{ printm , createPrintModulePass },
|
|
|
|
{ verify , createVerifierPass },
|
2002-02-27 04:04:59 +08:00
|
|
|
|
2002-04-27 14:56:12 +08:00
|
|
|
{ raiseallocs, createRaiseAllocationsPass },
|
|
|
|
{ cleangcc , createCleanupGCCOutputPass },
|
2002-04-13 02:21:13 +08:00
|
|
|
{ funcresolve, createFunctionResolvingPass },
|
2002-04-28 13:49:53 +08:00
|
|
|
|
|
|
|
{ internalize, createInternalizePass },
|
2002-04-27 14:56:12 +08:00
|
|
|
{ globaldce , createGlobalDCEPass },
|
2002-02-27 05:46:54 +08:00
|
|
|
{ swapstructs, createSwapElementsPass },
|
|
|
|
{ sortstructs, createSortElementsPass },
|
2002-03-29 02:08:07 +08:00
|
|
|
{ poolalloc , createPoolAllocatePass },
|
2001-06-07 04:29:01 +08:00
|
|
|
};
|
|
|
|
|
2002-04-13 02:21:13 +08:00
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Command line option handling code...
|
|
|
|
//
|
2001-07-24 04:22:30 +08: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 08:47:12 +08:00
|
|
|
cl::Flag PrintEachXForm("p", "Print module after each transformation");
|
2001-07-23 10:35:57 +08:00
|
|
|
cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
|
2001-07-24 04:22:30 +08:00
|
|
|
cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
|
2001-07-23 10:35:57 +08:00
|
|
|
cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
|
2001-12-15 00:50:35 +08:00
|
|
|
clEnumVal(dce , "Dead Code Elimination"),
|
2002-03-15 06:36:15 +08:00
|
|
|
clEnumVal(die , "Dead Instruction Elimination"),
|
2002-02-01 12:54:11 +08:00
|
|
|
clEnumVal(constprop , "Simple constant propogation"),
|
2002-04-28 08:48:17 +08:00
|
|
|
clEnumVal(gcse , "Global Common Subexpression Elimination"),
|
2002-04-14 02:32:47 +08:00
|
|
|
clEnumValN(inlining , "inline", "Function integration"),
|
2001-12-15 00:50:35 +08:00
|
|
|
clEnumVal(constmerge , "Merge identical global constants"),
|
2002-02-01 12:54:11 +08:00
|
|
|
clEnumVal(strip , "Strip symbols"),
|
|
|
|
clEnumVal(mstrip , "Strip module symbols"),
|
2002-04-14 02:32:47 +08:00
|
|
|
clEnumVal(mergereturn, "Unify function exit nodes"),
|
2002-02-01 12:54:11 +08:00
|
|
|
|
2001-12-15 00:50:35 +08:00
|
|
|
clEnumVal(indvars , "Simplify Induction Variables"),
|
2002-01-31 08:47:12 +08:00
|
|
|
clEnumVal(instcombine, "Combine redundant instructions"),
|
2001-12-15 00:50:35 +08:00
|
|
|
clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
|
2002-05-08 06:15:01 +08:00
|
|
|
clEnumVal(adce , "Aggressive DCE"),
|
2002-05-09 06:18:34 +08:00
|
|
|
clEnumVal(reassociate, "Reassociate expressions"),
|
2002-02-13 01:17:33 +08:00
|
|
|
clEnumVal(mem2reg , "Promote alloca locations to registers"),
|
2001-11-10 15:16:10 +08:00
|
|
|
|
2002-04-28 13:49:53 +08:00
|
|
|
clEnumVal(internalize, "Mark all fn's internal except for main"),
|
2001-12-15 00:50:35 +08:00
|
|
|
clEnumVal(globaldce , "Remove unreachable globals"),
|
2001-11-27 03:22:39 +08:00
|
|
|
clEnumVal(swapstructs, "Swap structure types around"),
|
2002-01-21 15:31:50 +08:00
|
|
|
clEnumVal(sortstructs, "Sort structure elements"),
|
2002-03-29 02:08:07 +08:00
|
|
|
clEnumVal(poolalloc , "Pool allocate disjoint datastructures"),
|
2001-11-27 03:22:39 +08:00
|
|
|
|
2002-01-22 08:13:51 +08:00
|
|
|
clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
|
2001-12-15 00:50:35 +08:00
|
|
|
clEnumVal(cleangcc , "Cleanup GCC Output"),
|
2002-04-13 02:21:13 +08:00
|
|
|
clEnumVal(funcresolve, "Resolve calls to foo(...) to foo(<concrete types>)"),
|
2001-12-15 00:50:35 +08:00
|
|
|
clEnumVal(raise , "Raise to Higher Level"),
|
2002-04-14 02:32:47 +08:00
|
|
|
clEnumVal(trace , "Insert BB and Function trace code"),
|
|
|
|
clEnumVal(tracem , "Insert Function trace code only"),
|
2002-02-27 03:57:59 +08:00
|
|
|
clEnumVal(paths , "Insert path profiling instrumentation"),
|
2002-04-14 02:32:47 +08:00
|
|
|
clEnumVal(print , "Print working function to stderr"),
|
|
|
|
clEnumVal(printm , "Print working module to stderr"),
|
2002-02-21 01:56:53 +08:00
|
|
|
clEnumVal(verify , "Verify module is well formed"),
|
2002-03-24 11:19:54 +08:00
|
|
|
clEnumVal(lowerrefs , "Decompose multi-dimensional structure/array refs to use one index per instruction"),
|
2001-07-23 10:35:57 +08:00
|
|
|
0);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
// 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) {
|
|
|
|
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-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.
|
|
|
|
if (Passes.run(M.get()) && !Quiet)
|
|
|
|
cerr << "Program modified.\n";
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|