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
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2014-01-13 07:38:24 +00:00
|
|
|
#include "llvm/Bitcode/BitcodeWriterPass.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-12 11:10:32 +00:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2013-03-26 02:25:37 +00:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2015-02-13 10:01:29 +00:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-04-29 23:26:49 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2006-12-06 01:18:01 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-03-06 05:34:10 +00:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2011-09-16 21:09:17 +00:00
|
|
|
#include "llvm/Support/Regex.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Support/Signals.h"
|
2013-03-26 02:25:37 +00:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-05-22 20:27:00 +00:00
|
|
|
#include <memory>
|
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>
|
2007-07-05 17:07:56 +00:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
|
2002-07-22 02:10:13 +00:00
|
|
|
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>
|
2009-08-25 15:34:52 +00:00
|
|
|
Force("f", cl::desc("Enable binary output on terminals"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2004-04-22 23:07:39 +00:00
|
|
|
static cl::opt<bool>
|
2008-03-07 19:51:57 +00:00
|
|
|
DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
|
2004-04-22 23:07:39 +00:00
|
|
|
|
2011-09-16 21:09:17 +00:00
|
|
|
// ExtractFuncs - The functions to extract from the module.
|
2010-02-10 23:58:53 +00:00
|
|
|
static cl::list<std::string>
|
|
|
|
ExtractFuncs("func", cl::desc("Specify function to extract"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("function"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
2013-09-18 23:31:10 +00:00
|
|
|
// ExtractRegExpFuncs - The functions, matched via regular expression, to
|
2011-09-16 21:09:17 +00:00
|
|
|
// extract from the module.
|
|
|
|
static cl::list<std::string>
|
|
|
|
ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
|
|
|
|
"regular expression"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("rfunction"));
|
|
|
|
|
2012-10-29 02:23:07 +00:00
|
|
|
// ExtractAlias - The alias to extract from the module.
|
|
|
|
static cl::list<std::string>
|
|
|
|
ExtractAliases("alias", cl::desc("Specify alias to extract"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("alias"));
|
|
|
|
|
|
|
|
|
|
|
|
// ExtractRegExpAliases - The aliases, matched via regular expression, to
|
|
|
|
// extract from the module.
|
|
|
|
static cl::list<std::string>
|
|
|
|
ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a "
|
|
|
|
"regular expression"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("ralias"));
|
|
|
|
|
2011-09-16 21:09:17 +00:00
|
|
|
// ExtractGlobals - The globals to extract from the module.
|
2010-02-10 23:58:53 +00:00
|
|
|
static cl::list<std::string>
|
|
|
|
ExtractGlobals("glob", cl::desc("Specify global to extract"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("global"));
|
2008-03-07 19:51:57 +00:00
|
|
|
|
2011-09-16 21:09:17 +00:00
|
|
|
// ExtractRegExpGlobals - The globals, matched via regular expression, to
|
|
|
|
// extract from the module...
|
|
|
|
static cl::list<std::string>
|
|
|
|
ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
|
|
|
|
"regular expression"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("rglobal"));
|
|
|
|
|
2009-09-11 20:46:33 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
OutputAssembly("S",
|
|
|
|
cl::desc("Write output as LLVM assembly"), cl::Hidden);
|
|
|
|
|
2002-05-22 20:27:00 +00:00
|
|
|
int main(int argc, char **argv) {
|
2009-03-06 05:34:10 +00:00
|
|
|
// Print a stack trace if we signal out.
|
2007-05-06 05:13:17 +00:00
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
2009-03-06 05:34:10 +00:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2009-07-01 16:58:40 +00:00
|
|
|
|
2009-07-15 22:16:10 +00:00
|
|
|
LLVMContext &Context = getGlobalContext();
|
2009-03-06 05:34:10 +00:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
|
2002-05-22 20:27:00 +00:00
|
|
|
|
2010-08-25 23:33:07 +00:00
|
|
|
// Use lazy loading, since we only care about selected global values.
|
2009-09-11 20:46:33 +00:00
|
|
|
SMDiagnostic Err;
|
2014-08-26 17:29:46 +00:00
|
|
|
std::unique_ptr<Module> M = getLazyIRFileModule(InputFilename, Err, Context);
|
2009-09-11 20:46:33 +00:00
|
|
|
|
2014-04-25 04:24:47 +00:00
|
|
|
if (!M.get()) {
|
2011-10-16 04:47:35 +00:00
|
|
|
Err.print(argv[0], errs());
|
2007-05-06 05:13:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-16 21:09:17 +00:00
|
|
|
// Use SetVector to avoid duplicates.
|
|
|
|
SetVector<GlobalValue *> GVs;
|
2010-02-10 23:58:53 +00:00
|
|
|
|
2012-10-29 02:23:07 +00:00
|
|
|
// Figure out which aliases we should extract.
|
|
|
|
for (size_t i = 0, e = ExtractAliases.size(); i != e; ++i) {
|
|
|
|
GlobalAlias *GA = M->getNamedAlias(ExtractAliases[i]);
|
|
|
|
if (!GA) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain alias named '"
|
|
|
|
<< ExtractAliases[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
GVs.insert(GA);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract aliases via regular expression matching.
|
|
|
|
for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) {
|
|
|
|
std::string Error;
|
|
|
|
Regex RegEx(ExtractRegExpAliases[i]);
|
|
|
|
if (!RegEx.isValid(Error)) {
|
|
|
|
errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' "
|
|
|
|
"invalid regex: " << Error;
|
|
|
|
}
|
|
|
|
bool match = false;
|
|
|
|
for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end();
|
|
|
|
GA != E; GA++) {
|
|
|
|
if (RegEx.match(GA->getName())) {
|
|
|
|
GVs.insert(&*GA);
|
|
|
|
match = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!match) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain global named '"
|
|
|
|
<< ExtractRegExpAliases[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-10 23:58:53 +00:00
|
|
|
// Figure out which globals we should extract.
|
|
|
|
for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
|
2011-12-30 19:17:23 +00:00
|
|
|
GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
|
2010-02-10 23:58:53 +00:00
|
|
|
if (!GV) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain global named '"
|
|
|
|
<< ExtractGlobals[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2011-09-16 21:09:17 +00:00
|
|
|
GVs.insert(GV);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract globals via regular expression matching.
|
|
|
|
for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
|
|
|
|
std::string Error;
|
|
|
|
Regex RegEx(ExtractRegExpGlobals[i]);
|
|
|
|
if (!RegEx.isValid(Error)) {
|
|
|
|
errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
|
|
|
|
"invalid regex: " << Error;
|
|
|
|
}
|
|
|
|
bool match = false;
|
2014-05-08 19:30:17 +00:00
|
|
|
for (auto &GV : M->globals()) {
|
|
|
|
if (RegEx.match(GV.getName())) {
|
|
|
|
GVs.insert(&GV);
|
2011-09-16 21:09:17 +00:00
|
|
|
match = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!match) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain global named '"
|
|
|
|
<< ExtractRegExpGlobals[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2010-02-10 23:58:53 +00:00
|
|
|
}
|
2008-03-07 19:51:57 +00:00
|
|
|
|
2010-02-10 23:58:53 +00:00
|
|
|
// Figure out which functions we should extract.
|
|
|
|
for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
|
2011-12-30 19:17:23 +00:00
|
|
|
GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
|
2010-02-10 23:58:53 +00:00
|
|
|
if (!GV) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain function named '"
|
|
|
|
<< ExtractFuncs[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2011-09-16 21:09:17 +00:00
|
|
|
GVs.insert(GV);
|
|
|
|
}
|
|
|
|
// Extract functions via regular expression matching.
|
|
|
|
for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
|
|
|
|
std::string Error;
|
|
|
|
StringRef RegExStr = ExtractRegExpFuncs[i];
|
|
|
|
Regex RegEx(RegExStr);
|
|
|
|
if (!RegEx.isValid(Error)) {
|
|
|
|
errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
|
|
|
|
"invalid regex: " << Error;
|
|
|
|
}
|
|
|
|
bool match = false;
|
2011-12-30 19:17:23 +00:00
|
|
|
for (Module::iterator F = M->begin(), E = M->end(); F != E;
|
2011-09-16 21:09:17 +00:00
|
|
|
F++) {
|
|
|
|
if (RegEx.match(F->getName())) {
|
|
|
|
GVs.insert(&*F);
|
|
|
|
match = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!match) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain global named '"
|
|
|
|
<< ExtractRegExpFuncs[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2007-05-06 05:13:17 +00:00
|
|
|
}
|
2002-11-19 18:42:59 +00:00
|
|
|
|
2010-08-25 23:33:07 +00:00
|
|
|
// Materialize requisite global values.
|
2010-09-23 00:33:13 +00:00
|
|
|
if (!DeleteFn)
|
|
|
|
for (size_t i = 0, e = GVs.size(); i != e; ++i) {
|
|
|
|
GlobalValue *GV = GVs[i];
|
2014-11-01 16:46:18 +00:00
|
|
|
if (std::error_code EC = GV->materialize()) {
|
|
|
|
errs() << argv[0] << ": error reading input: " << EC.message() << "\n";
|
|
|
|
return 1;
|
2010-09-23 00:33:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Deleting. Materialize every GV that's *not* in GVs.
|
|
|
|
SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
|
2014-05-08 19:30:17 +00:00
|
|
|
for (auto &G : M->globals()) {
|
2014-11-01 16:46:18 +00:00
|
|
|
if (!GVSet.count(&G)) {
|
2014-10-24 22:50:48 +00:00
|
|
|
if (std::error_code EC = G.materialize()) {
|
|
|
|
errs() << argv[0] << ": error reading input: " << EC.message()
|
|
|
|
<< "\n";
|
2010-09-23 00:33:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 19:30:17 +00:00
|
|
|
for (auto &F : *M) {
|
2014-11-01 16:46:18 +00:00
|
|
|
if (!GVSet.count(&F)) {
|
2014-10-24 22:50:48 +00:00
|
|
|
if (std::error_code EC = F.materialize()) {
|
|
|
|
errs() << argv[0] << ": error reading input: " << EC.message()
|
|
|
|
<< "\n";
|
2010-09-23 00:33:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2010-08-25 23:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-06 05:13:17 +00:00
|
|
|
// In addition to deleting all other functions, we also want to spiff it
|
|
|
|
// up a little bit. Do this now.
|
2015-02-13 10:01:29 +00:00
|
|
|
legacy::PassManager Passes;
|
2014-09-10 21:27:43 +00:00
|
|
|
Passes.add(new DataLayoutPass()); // Use correct DataLayout
|
2008-03-07 19:51:57 +00:00
|
|
|
|
2011-09-16 21:09:17 +00:00
|
|
|
std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
|
|
|
|
|
|
|
|
Passes.add(createGVExtractionPass(Gvs, DeleteFn));
|
2007-05-06 05:13:17 +00:00
|
|
|
if (!DeleteFn)
|
|
|
|
Passes.add(createGlobalDCEPass()); // Delete unreachable globals
|
2010-07-01 19:58:05 +00:00
|
|
|
Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
|
2007-05-06 05:13:17 +00:00
|
|
|
Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
|
|
|
|
|
2014-08-25 18:16:47 +00:00
|
|
|
std::error_code EC;
|
|
|
|
tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
|
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
2009-08-23 02:56:05 +00:00
|
|
|
return 1;
|
2007-05-06 05:13:17 +00:00
|
|
|
}
|
2004-02-18 16:53:59 +00:00
|
|
|
|
2009-09-11 20:46:33 +00:00
|
|
|
if (OutputAssembly)
|
2014-01-12 11:30:46 +00:00
|
|
|
Passes.add(createPrintModulePass(Out.os()));
|
2010-09-01 14:20:41 +00:00
|
|
|
else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
|
|
|
|
Passes.add(createBitcodeWriterPass(Out.os()));
|
2009-08-25 15:34:52 +00:00
|
|
|
|
2007-05-06 05:13:17 +00:00
|
|
|
Passes.run(*M.get());
|
|
|
|
|
2010-08-20 01:12:13 +00:00
|
|
|
// Declare success.
|
|
|
|
Out.keep();
|
|
|
|
|
2007-05-06 05:13:17 +00:00
|
|
|
return 0;
|
2002-05-22 20:27:00 +00:00
|
|
|
}
|