2005-04-24 19:36:05 +02:00
|
|
|
//===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:44:31 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-05-22 22:27:00 +02:00
|
|
|
//
|
|
|
|
// This utility changes the input module to only contain a single function,
|
|
|
|
// which is primarily used for debugging transformations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-01 18:58:40 +02:00
|
|
|
#include "llvm/LLVMContext.h"
|
2002-05-22 22:27:00 +02:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/PassManager.h"
|
2009-09-11 22:46:33 +02:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2007-05-06 07:13:17 +02:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2002-07-24 00:04:40 +02:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2003-10-28 23:22:16 +01:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-09-11 22:46:33 +02:00
|
|
|
#include "llvm/Support/IRReader.h"
|
2006-12-06 02:18:01 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-03-06 06:34:10 +01:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2010-10-07 22:32:40 +02:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2009-08-25 17:34:52 +02:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Signals.h"
|
2010-09-23 02:33:13 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2002-05-22 22:27:00 +02:00
|
|
|
#include <memory>
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-07-22 04:10:13 +02:00
|
|
|
// InputFilename - The filename to read from.
|
2002-07-25 18:31:09 +02:00
|
|
|
static cl::opt<std::string>
|
2007-07-05 19:07:56 +02:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
|
2002-07-22 04:10:13 +02:00
|
|
|
cl::init("-"), cl::value_desc("filename"));
|
2005-04-22 02:00:37 +02:00
|
|
|
|
2004-02-18 17:53:59 +01:00
|
|
|
static cl::opt<std::string>
|
2005-04-22 02:00:37 +02:00
|
|
|
OutputFilename("o", cl::desc("Specify output filename"),
|
2004-02-18 17:53:59 +01:00
|
|
|
cl::value_desc("filename"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
2009-08-25 17:34:52 +02:00
|
|
|
Force("f", cl::desc("Enable binary output on terminals"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2004-04-23 01:07:39 +02:00
|
|
|
static cl::opt<bool>
|
2008-03-07 20:51:57 +01:00
|
|
|
DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
|
2004-04-23 01:07:39 +02:00
|
|
|
|
2010-02-11 00:58:53 +01:00
|
|
|
// ExtractFuncs - The functions to extract from the module...
|
|
|
|
static cl::list<std::string>
|
|
|
|
ExtractFuncs("func", cl::desc("Specify function to extract"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("function"));
|
2002-07-22 04:10:13 +02:00
|
|
|
|
2010-02-11 00:58:53 +01:00
|
|
|
// ExtractGlobals - The globals to extract from the module...
|
|
|
|
static cl::list<std::string>
|
|
|
|
ExtractGlobals("glob", cl::desc("Specify global to extract"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("global"));
|
2008-03-07 20:51:57 +01:00
|
|
|
|
2009-09-11 22:46:33 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
OutputAssembly("S",
|
|
|
|
cl::desc("Write output as LLVM assembly"), cl::Hidden);
|
|
|
|
|
2002-05-22 22:27:00 +02:00
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 06:34:10 +01:00
|
|
|
// Print a stack trace if we signal out.
|
2007-05-06 07:13:17 +02:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2009-03-06 06:34:10 +01:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2009-07-01 18:58:40 +02:00
|
|
|
|
2009-07-16 00:16:10 +02:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
2009-03-06 06:34:10 +01:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
|
2002-05-22 22:27:00 +02:00
|
|
|
|
2010-08-26 01:33:07 +02:00
|
|
|
// Use lazy loading, since we only care about selected global values.
|
2009-09-11 22:46:33 +02:00
|
|
|
SMDiagnostic Err;
|
2007-05-06 07:13:17 +02:00
|
|
|
std::auto_ptr<Module> M;
|
2010-08-26 01:33:07 +02:00
|
|
|
M.reset(getLazyIRFileModule(InputFilename, Err, Context));
|
2009-09-11 22:46:33 +02:00
|
|
|
|
2007-05-06 07:13:17 +02:00
|
|
|
if (M.get() == 0) {
|
2009-09-11 22:46:33 +02:00
|
|
|
Err.Print(argv[0], errs());
|
2007-05-06 07:13:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-02-11 00:58:53 +01:00
|
|
|
std::vector<GlobalValue *> GVs;
|
|
|
|
|
|
|
|
// Figure out which globals we should extract.
|
|
|
|
for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
|
|
|
|
GlobalValue *GV = M.get()->getNamedGlobal(ExtractGlobals[i]);
|
|
|
|
if (!GV) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain global named '"
|
|
|
|
<< ExtractGlobals[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
GVs.push_back(GV);
|
|
|
|
}
|
2008-03-07 20:51:57 +01:00
|
|
|
|
2010-02-11 00:58:53 +01:00
|
|
|
// Figure out which functions we should extract.
|
|
|
|
for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
|
|
|
|
GlobalValue *GV = M.get()->getFunction(ExtractFuncs[i]);
|
|
|
|
if (!GV) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain function named '"
|
|
|
|
<< ExtractFuncs[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
GVs.push_back(GV);
|
2007-05-06 07:13:17 +02:00
|
|
|
}
|
2002-11-19 19:42:59 +01:00
|
|
|
|
2010-08-26 01:33:07 +02:00
|
|
|
// Materialize requisite global values.
|
2010-09-23 02:33:13 +02:00
|
|
|
if (!DeleteFn)
|
|
|
|
for (size_t i = 0, e = GVs.size(); i != e; ++i) {
|
|
|
|
GlobalValue *GV = GVs[i];
|
|
|
|
if (GV->isMaterializable()) {
|
|
|
|
std::string ErrInfo;
|
|
|
|
if (GV->Materialize(&ErrInfo)) {
|
|
|
|
errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Deleting. Materialize every GV that's *not* in GVs.
|
|
|
|
SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
|
|
|
|
for (Module::global_iterator I = M->global_begin(), E = M->global_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
GlobalVariable *G = I;
|
|
|
|
if (!GVSet.count(G) && G->isMaterializable()) {
|
|
|
|
std::string ErrInfo;
|
|
|
|
if (G->Materialize(&ErrInfo)) {
|
|
|
|
errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
|
|
|
Function *F = I;
|
|
|
|
if (!GVSet.count(F) && F->isMaterializable()) {
|
|
|
|
std::string ErrInfo;
|
|
|
|
if (F->Materialize(&ErrInfo)) {
|
|
|
|
errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2010-08-26 01:33:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-06 07:13:17 +02: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
|
2008-03-07 20:51:57 +01:00
|
|
|
|
2010-08-26 02:22:55 +02:00
|
|
|
Passes.add(createGVExtractionPass(GVs, DeleteFn));
|
2007-05-06 07:13:17 +02:00
|
|
|
if (!DeleteFn)
|
|
|
|
Passes.add(createGlobalDCEPass()); // Delete unreachable globals
|
2010-07-01 21:58:05 +02:00
|
|
|
Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
|
2007-05-06 07:13:17 +02:00
|
|
|
Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
|
|
|
|
Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
|
|
|
|
|
2009-08-23 04:56:05 +02:00
|
|
|
std::string ErrorInfo;
|
2010-08-20 03:12:13 +02:00
|
|
|
tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
|
|
|
|
raw_fd_ostream::F_Binary);
|
2009-08-23 04:56:05 +02:00
|
|
|
if (!ErrorInfo.empty()) {
|
|
|
|
errs() << ErrorInfo << '\n';
|
|
|
|
return 1;
|
2007-05-06 07:13:17 +02:00
|
|
|
}
|
2004-02-18 17:53:59 +01:00
|
|
|
|
2009-09-11 22:46:33 +02:00
|
|
|
if (OutputAssembly)
|
2010-09-01 16:20:41 +02:00
|
|
|
Passes.add(createPrintModulePass(&Out.os()));
|
|
|
|
else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
|
|
|
|
Passes.add(createBitcodeWriterPass(Out.os()));
|
2009-08-25 17:34:52 +02:00
|
|
|
|
2007-05-06 07:13:17 +02:00
|
|
|
Passes.run(*M.get());
|
|
|
|
|
2010-08-20 03:12:13 +02:00
|
|
|
// Declare success.
|
|
|
|
Out.keep();
|
|
|
|
|
2007-05-06 07:13:17 +02:00
|
|
|
return 0;
|
2002-05-22 22:27:00 +02:00
|
|
|
}
|