2005-04-25 01:36:05 +08:00
|
|
|
//===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:44:31 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-05-23 04:27:00 +08:00
|
|
|
//
|
|
|
|
// This utility changes the input module to only contain a single function,
|
|
|
|
// which is primarily used for debugging transformations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-02 00:58:40 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2002-05-23 04:27:00 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/PassManager.h"
|
2007-05-06 13:13:17 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2002-07-24 06:04:40 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2003-10-29 06:22:16 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-12-06 09:18:01 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2007-05-06 13:13:17 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-03-06 13:34:10 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2004-05-27 13:41:36 +08:00
|
|
|
#include "llvm/System/Signals.h"
|
2006-11-29 08:19:40 +08:00
|
|
|
#include <iostream>
|
2002-05-23 04:27:00 +08:00
|
|
|
#include <memory>
|
2004-02-19 00:53:59 +08:00
|
|
|
#include <fstream>
|
2003-11-12 06:41:34 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-22 10:10:13 +08:00
|
|
|
// InputFilename - The filename to read from.
|
2002-07-26 00:31:09 +08:00
|
|
|
static cl::opt<std::string>
|
2007-07-06 01:07:56 +08:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
|
2002-07-22 10:10:13 +08:00
|
|
|
cl::init("-"), cl::value_desc("filename"));
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2004-02-19 00:53:59 +08:00
|
|
|
static cl::opt<std::string>
|
2005-04-22 08:00:37 +08:00
|
|
|
OutputFilename("o", cl::desc("Specify output filename"),
|
2004-02-19 00:53:59 +08:00
|
|
|
cl::value_desc("filename"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Overwrite output files"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2004-04-23 07:07:39 +08:00
|
|
|
static cl::opt<bool>
|
2008-03-08 03:51:57 +08:00
|
|
|
DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
|
2004-04-23 07:07:39 +08:00
|
|
|
|
2007-01-28 21:31:35 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
Relink("relink",
|
|
|
|
cl::desc("Turn external linkage for callees of function to delete"));
|
|
|
|
|
2008-03-08 03:51:57 +08:00
|
|
|
// ExtractFunc - The function to extract from the module...
|
2002-07-26 00:31:09 +08:00
|
|
|
static cl::opt<std::string>
|
2008-03-08 03:51:57 +08:00
|
|
|
ExtractFunc("func", cl::desc("Specify function to extract"), cl::init(""),
|
2002-07-22 10:10:13 +08:00
|
|
|
cl::value_desc("function"));
|
|
|
|
|
2008-03-08 03:51:57 +08:00
|
|
|
// ExtractGlobal - The global to extract from the module...
|
|
|
|
static cl::opt<std::string>
|
|
|
|
ExtractGlobal("glob", cl::desc("Specify global to extract"), cl::init(""),
|
|
|
|
cl::value_desc("global"));
|
|
|
|
|
2002-05-23 04:27:00 +08:00
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 13:34:10 +08:00
|
|
|
// Print a stack trace if we signal out.
|
2007-05-06 13:13:17 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2009-03-06 13:34:10 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2009-07-02 00:58:40 +08:00
|
|
|
|
|
|
|
LLVMContext Context;
|
2009-03-06 13:34:10 +08:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
|
2002-05-23 04:27:00 +08:00
|
|
|
|
2007-05-06 13:13:17 +08:00
|
|
|
std::auto_ptr<Module> M;
|
|
|
|
|
2007-05-07 07:45:49 +08:00
|
|
|
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename);
|
2007-05-06 17:29:57 +08:00
|
|
|
if (Buffer == 0) {
|
2007-08-09 05:19:01 +08:00
|
|
|
cerr << argv[0] << ": Error reading file '" + InputFilename + "'\n";
|
2007-05-06 17:29:57 +08:00
|
|
|
return 1;
|
2007-05-06 13:13:17 +08:00
|
|
|
} else {
|
2009-07-02 05:22:36 +08:00
|
|
|
M.reset(ParseBitcodeFile(Buffer, Context));
|
2007-05-06 13:13:17 +08:00
|
|
|
}
|
2007-05-06 17:29:57 +08:00
|
|
|
delete Buffer;
|
2007-05-06 13:13:17 +08:00
|
|
|
|
|
|
|
if (M.get() == 0) {
|
2007-07-06 01:07:56 +08:00
|
|
|
cerr << argv[0] << ": bitcode didn't read correctly.\n";
|
2007-05-06 13:13:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out which function we should extract
|
2009-04-21 00:19:02 +08:00
|
|
|
GlobalVariable *G = !ExtractGlobal.empty() ?
|
2008-03-08 03:51:57 +08:00
|
|
|
M.get()->getNamedGlobal(ExtractGlobal) : 0;
|
|
|
|
|
|
|
|
// Figure out which function we should extract
|
2009-04-21 00:19:02 +08:00
|
|
|
if (ExtractFunc.empty() && ExtractGlobal.empty()) ExtractFunc = "main";
|
2008-03-08 04:07:24 +08:00
|
|
|
Function *F = M.get()->getFunction(ExtractFunc);
|
2008-03-08 03:51:57 +08:00
|
|
|
|
|
|
|
if (F == 0 && G == 0) {
|
2007-05-06 13:13:17 +08:00
|
|
|
cerr << argv[0] << ": program doesn't contain function named '"
|
2008-03-08 03:51:57 +08:00
|
|
|
<< ExtractFunc << "' or a global named '" << ExtractGlobal << "'!\n";
|
2007-05-06 13:13:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2002-11-20 02:42:59 +08:00
|
|
|
|
2007-05-06 13:13:17 +08:00
|
|
|
// In addition to deleting all other functions, we also want to spiff it
|
|
|
|
// up a little bit. Do this now.
|
|
|
|
PassManager Passes;
|
|
|
|
Passes.add(new TargetData(M.get())); // Use correct TargetData
|
|
|
|
// Either isolate the function or delete it from the Module
|
2008-03-08 03:51:57 +08:00
|
|
|
std::vector<GlobalValue*> GVs;
|
|
|
|
if (F) GVs.push_back(F);
|
|
|
|
if (G) GVs.push_back(G);
|
|
|
|
|
|
|
|
Passes.add(createGVExtractionPass(GVs, DeleteFn, Relink));
|
2007-05-06 13:13:17 +08:00
|
|
|
if (!DeleteFn)
|
|
|
|
Passes.add(createGlobalDCEPass()); // Delete unreachable globals
|
|
|
|
Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
|
|
|
|
Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
|
|
|
|
|
|
|
|
std::ostream *Out = 0;
|
|
|
|
|
|
|
|
if (OutputFilename != "-") { // Not stdout?
|
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
2004-02-19 00:53:59 +08:00
|
|
|
}
|
2007-05-06 13:13:17 +08:00
|
|
|
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
|
|
|
|
std::ios::binary;
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str(), io_mode);
|
|
|
|
} else { // Specified stdout
|
|
|
|
// FIXME: cout is not binary!
|
|
|
|
Out = &std::cout;
|
|
|
|
}
|
2004-02-19 00:53:59 +08:00
|
|
|
|
2007-05-06 17:29:57 +08:00
|
|
|
Passes.add(CreateBitcodeWriterPass(*Out));
|
2007-05-06 13:13:17 +08:00
|
|
|
Passes.run(*M.get());
|
|
|
|
|
|
|
|
if (Out != &std::cout)
|
|
|
|
delete Out;
|
|
|
|
return 0;
|
2002-05-23 04:27:00 +08:00
|
|
|
}
|