2002-05-22 20:27:00 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LLVM extract Utility
|
|
|
|
//
|
|
|
|
// This utility changes the input module to only contain a single function,
|
|
|
|
// which is primarily used for debugging transformations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2002-07-23 22:04:40 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-05-22 20:27:00 +00:00
|
|
|
#include "Support/CommandLine.h"
|
|
|
|
#include <memory>
|
|
|
|
|
2002-07-22 02:10:13 +00:00
|
|
|
// InputFilename - The filename to read from.
|
2002-07-25 16:31:09 +00:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 02:10:13 +00:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
|
|
|
|
cl::init("-"), cl::value_desc("filename"));
|
|
|
|
|
|
|
|
|
|
|
|
// ExtractFunc - The function to extract from the module... defaults to main.
|
2002-07-25 16:31:09 +00:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 02:10:13 +00:00
|
|
|
ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
|
|
|
|
cl::value_desc("function"));
|
|
|
|
|
2002-05-22 20:27:00 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
|
|
|
|
|
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
|
|
|
|
if (M.get() == 0) {
|
2002-07-30 21:43:22 +00:00
|
|
|
std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
|
2002-05-22 20:27:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-11-19 18:42:59 +00:00
|
|
|
// Figure out which function we should extract
|
|
|
|
Function *F = M.get()->getNamedFunction(ExtractFunc);
|
|
|
|
if (F == 0) {
|
|
|
|
std::cerr << argv[0] << ": program doesn't contain function named '"
|
|
|
|
<< ExtractFunc << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-05-22 20:27:00 +00:00
|
|
|
// In addition to just parsing the input from GCC, we also want to spiff it up
|
|
|
|
// a little bit. Do this now.
|
|
|
|
//
|
|
|
|
PassManager Passes;
|
2002-11-19 18:42:59 +00:00
|
|
|
Passes.add(createFunctionExtractionPass(F)); // Extract the function
|
2002-05-22 20:27:00 +00:00
|
|
|
Passes.add(createGlobalDCEPass()); // Delete unreachable globals
|
2002-10-12 20:50:16 +00:00
|
|
|
Passes.add(createFunctionResolvingPass()); // Delete prototypes
|
2002-07-23 22:04:40 +00:00
|
|
|
Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
|
2002-05-22 20:27:00 +00:00
|
|
|
Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
Passes.run(*M.get());
|
2002-05-22 20:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|