2005-04-24 17:36:05 +00:00
|
|
|
//===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00: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.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-05-22 20:27:00 +00:00
|
|
|
//
|
|
|
|
// 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"
|
2003-10-28 22:22:16 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2007-02-07 21:41:02 +00:00
|
|
|
#include "llvm/Support/Compressor.h"
|
2006-12-06 01:18:01 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2006-11-29 00:19:40 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
2004-05-27 05:41:36 +00:00
|
|
|
#include "llvm/System/Signals.h"
|
2006-11-29 00:19:40 +00:00
|
|
|
#include <iostream>
|
2002-05-22 20:27:00 +00:00
|
|
|
#include <memory>
|
2004-02-18 16:53:59 +00:00
|
|
|
#include <fstream>
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
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"));
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2004-02-18 16:53:59 +00:00
|
|
|
static cl::opt<std::string>
|
2005-04-22 00:00:37 +00:00
|
|
|
OutputFilename("o", cl::desc("Specify output filename"),
|
2004-02-18 16:53:59 +00:00
|
|
|
cl::value_desc("filename"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Overwrite output files"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2004-04-22 23:07:39 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
DeleteFn("delete", cl::desc("Delete specified function from Module"));
|
|
|
|
|
2007-01-28 13:31:35 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
Relink("relink",
|
|
|
|
cl::desc("Turn external linkage for callees of function to delete"));
|
|
|
|
|
2002-07-22 02:10:13 +00:00
|
|
|
// 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) {
|
2006-12-06 01:18:01 +00:00
|
|
|
llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
|
2004-12-30 05:36:08 +00:00
|
|
|
try {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2002-05-22 20:27:00 +00:00
|
|
|
|
2007-02-07 21:41:02 +00:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename,
|
|
|
|
Compressor::decompressToNewBuffer));
|
2004-12-30 05:36:08 +00:00
|
|
|
if (M.get() == 0) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": bytecode didn't read correctly.\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-05-22 20:27:00 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
// Figure out which function we should extract
|
2007-02-05 21:17:53 +00:00
|
|
|
Function *F = M.get()->getFunction(ExtractFunc);
|
2004-12-30 05:36:08 +00:00
|
|
|
if (F == 0) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": program doesn't contain function named '"
|
|
|
|
<< ExtractFunc << "'!\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-11-19 18:42:59 +00:00
|
|
|
|
2005-01-22 17:36:17 +00:00
|
|
|
// In addition to deleting all other functions, we also want to spiff it
|
|
|
|
// up a little bit. Do this now.
|
2004-12-30 05:36:08 +00:00
|
|
|
PassManager Passes;
|
2006-06-16 18:23:49 +00:00
|
|
|
Passes.add(new TargetData(M.get())); // Use correct TargetData
|
2004-12-30 05:36:08 +00:00
|
|
|
// Either isolate the function or delete it from the Module
|
2007-01-28 13:31:35 +00:00
|
|
|
Passes.add(createFunctionExtractionPass(F, DeleteFn, Relink));
|
|
|
|
if (!DeleteFn)
|
|
|
|
Passes.add(createGlobalDCEPass()); // Delete unreachable globals
|
2005-04-24 17:36:05 +00:00
|
|
|
Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
|
2007-02-05 21:17:53 +00:00
|
|
|
Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
|
2002-05-22 20:27:00 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
std::ostream *Out = 0;
|
2004-02-18 16:53:59 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
if (OutputFilename != "-") { // Not stdout?
|
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2005-01-22 17:36:17 +00:00
|
|
|
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
|
|
|
|
std::ios::binary;
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str(), io_mode);
|
2004-12-30 05:36:08 +00:00
|
|
|
} else { // Specified stdout
|
2005-01-22 17:36:17 +00:00
|
|
|
// FIXME: cout is not binary!
|
2005-04-22 00:00:37 +00:00
|
|
|
Out = &std::cout;
|
2004-02-18 16:53:59 +00:00
|
|
|
}
|
|
|
|
|
2006-12-07 01:30:32 +00:00
|
|
|
OStream L(*Out);
|
2006-11-29 00:19:40 +00:00
|
|
|
Passes.add(new WriteBytecodePass(&L)); // Write bytecode to file...
|
2004-12-30 05:36:08 +00:00
|
|
|
Passes.run(*M.get());
|
2004-02-18 16:53:59 +00:00
|
|
|
|
2004-12-30 05:36:08 +00:00
|
|
|
if (Out != &std::cout)
|
|
|
|
delete Out;
|
|
|
|
return 0;
|
|
|
|
} catch (const std::string& msg) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": " << msg << "\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
} catch (...) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
|
2004-12-30 05:36:08 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2002-05-22 20:27:00 +00:00
|
|
|
}
|