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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2014-01-13 15:38:24 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeWriterPass.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-12 19:10:32 +08:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2013-03-26 10:25:37 +08:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-11-10 01:49:19 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2014-04-30 07:26:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2006-12-06 09:18:01 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2009-03-06 13:34:10 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2011-09-17 05:09:17 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2013-03-26 10:25:37 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-05-23 04:27:00 +08:00
|
|
|
#include <memory>
|
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>
|
2009-08-25 23:34:52 +08:00
|
|
|
Force("f", cl::desc("Enable binary output on terminals"));
|
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
|
|
|
|
2011-09-17 05:09:17 +08:00
|
|
|
// ExtractFuncs - The functions to extract from the module.
|
2010-02-11 07:58:53 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
ExtractFuncs("func", cl::desc("Specify function to extract"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("function"));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2013-09-19 07:31:10 +08:00
|
|
|
// ExtractRegExpFuncs - The functions, matched via regular expression, to
|
2011-09-17 05:09:17 +08: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 10:23:07 +08: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-17 05:09:17 +08:00
|
|
|
// ExtractGlobals - The globals to extract from the module.
|
2010-02-11 07:58:53 +08:00
|
|
|
static cl::list<std::string>
|
|
|
|
ExtractGlobals("glob", cl::desc("Specify global to extract"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("global"));
|
2008-03-08 03:51:57 +08:00
|
|
|
|
2011-09-17 05:09:17 +08: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-12 04:46:33 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
OutputAssembly("S",
|
|
|
|
cl::desc("Write output as LLVM assembly"), cl::Hidden);
|
|
|
|
|
2015-04-15 11:14:06 +08:00
|
|
|
static cl::opt<bool> PreserveBitcodeUseListOrder(
|
|
|
|
"preserve-bc-uselistorder",
|
|
|
|
cl::desc("Preserve use-list order when writing LLVM bitcode."),
|
|
|
|
cl::init(true), cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<bool> PreserveAssemblyUseListOrder(
|
|
|
|
"preserve-ll-uselistorder",
|
|
|
|
cl::desc("Preserve use-list order when writing LLVM assembly."),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
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.
|
2016-06-09 08:53:21 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2009-03-06 13:34:10 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
2009-07-02 00:58:40 +08:00
|
|
|
|
2016-04-15 05:59:01 +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
|
|
|
|
2010-08-26 07:33:07 +08:00
|
|
|
// Use lazy loading, since we only care about selected global values.
|
2009-09-12 04:46:33 +08:00
|
|
|
SMDiagnostic Err;
|
2014-08-27 01:29:46 +08:00
|
|
|
std::unique_ptr<Module> M = getLazyIRFileModule(InputFilename, Err, Context);
|
2009-09-12 04:46:33 +08:00
|
|
|
|
2014-04-25 12:24:47 +08:00
|
|
|
if (!M.get()) {
|
2011-10-16 12:47:35 +08:00
|
|
|
Err.print(argv[0], errs());
|
2007-05-06 13:13:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-17 05:09:17 +08:00
|
|
|
// Use SetVector to avoid duplicates.
|
|
|
|
SetVector<GlobalValue *> GVs;
|
2010-02-11 07:58:53 +08:00
|
|
|
|
2012-10-29 10:23:07 +08: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-11 07:58:53 +08:00
|
|
|
// Figure out which globals we should extract.
|
|
|
|
for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
|
2011-12-31 03:17:23 +08:00
|
|
|
GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
|
2010-02-11 07:58:53 +08:00
|
|
|
if (!GV) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain global named '"
|
|
|
|
<< ExtractGlobals[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2011-09-17 05:09:17 +08: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-09 03:30:17 +08:00
|
|
|
for (auto &GV : M->globals()) {
|
|
|
|
if (RegEx.match(GV.getName())) {
|
|
|
|
GVs.insert(&GV);
|
2011-09-17 05:09:17 +08:00
|
|
|
match = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!match) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain global named '"
|
|
|
|
<< ExtractRegExpGlobals[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2010-02-11 07:58:53 +08:00
|
|
|
}
|
2008-03-08 03:51:57 +08:00
|
|
|
|
2010-02-11 07:58:53 +08:00
|
|
|
// Figure out which functions we should extract.
|
|
|
|
for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
|
2011-12-31 03:17:23 +08:00
|
|
|
GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
|
2010-02-11 07:58:53 +08:00
|
|
|
if (!GV) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain function named '"
|
|
|
|
<< ExtractFuncs[i] << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2011-09-17 05:09:17 +08: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-31 03:17:23 +08:00
|
|
|
for (Module::iterator F = M->begin(), E = M->end(); F != E;
|
2011-09-17 05:09:17 +08: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 13:13:17 +08:00
|
|
|
}
|
2002-11-20 02:42:59 +08:00
|
|
|
|
2016-11-10 05:30:33 +08:00
|
|
|
// Use *argv instead of argv[0] to work around a wrong GCC warning.
|
|
|
|
ExitOnError ExitOnErr(std::string(*argv) + ": error reading input: ");
|
2016-11-10 01:49:19 +08:00
|
|
|
|
|
|
|
auto Materialize = [&](GlobalValue &GV) { ExitOnErr(GV.materialize()); };
|
2015-12-19 06:40:27 +08:00
|
|
|
|
|
|
|
// Materialize requisite global values.
|
|
|
|
if (!DeleteFn) {
|
|
|
|
for (size_t i = 0, e = GVs.size(); i != e; ++i)
|
|
|
|
Materialize(*GVs[i]);
|
|
|
|
} else {
|
2010-09-23 08:33:13 +08:00
|
|
|
// Deleting. Materialize every GV that's *not* in GVs.
|
|
|
|
SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
|
2014-05-09 03:30:17 +08:00
|
|
|
for (auto &F : *M) {
|
2015-12-19 06:40:27 +08:00
|
|
|
if (!GVSet.count(&F))
|
|
|
|
Materialize(F);
|
2010-08-26 07:33:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-16 03:00:20 +08:00
|
|
|
{
|
|
|
|
std::vector<GlobalValue *> Gvs(GVs.begin(), GVs.end());
|
|
|
|
legacy::PassManager Extract;
|
|
|
|
Extract.add(createGVExtractionPass(Gvs, DeleteFn));
|
|
|
|
Extract.run(*M);
|
|
|
|
|
|
|
|
// Now that we have all the GVs we want, mark the module as fully
|
|
|
|
// materialized.
|
|
|
|
// FIXME: should the GVExtractionPass handle this?
|
2016-11-10 01:49:19 +08:00
|
|
|
ExitOnErr(M->materializeAll());
|
2016-01-16 03:00:20 +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.
|
2015-02-13 18:01:29 +08:00
|
|
|
legacy::PassManager Passes;
|
2008-03-08 03:51:57 +08:00
|
|
|
|
2007-05-06 13:13:17 +08:00
|
|
|
if (!DeleteFn)
|
|
|
|
Passes.add(createGlobalDCEPass()); // Delete unreachable globals
|
2010-07-02 03:58:05 +08:00
|
|
|
Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
|
2007-05-06 13:13:17 +08:00
|
|
|
Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
|
|
|
|
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
|
|
|
tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
|
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
2009-08-23 10:56:05 +08:00
|
|
|
return 1;
|
2007-05-06 13:13:17 +08:00
|
|
|
}
|
2004-02-19 00:53:59 +08:00
|
|
|
|
2009-09-12 04:46:33 +08:00
|
|
|
if (OutputAssembly)
|
2015-04-15 08:34:24 +08:00
|
|
|
Passes.add(
|
2015-04-15 11:14:06 +08:00
|
|
|
createPrintModulePass(Out.os(), "", PreserveAssemblyUseListOrder));
|
|
|
|
else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
|
|
|
|
Passes.add(createBitcodeWriterPass(Out.os(), PreserveBitcodeUseListOrder));
|
2009-08-25 23:34:52 +08:00
|
|
|
|
2007-05-06 13:13:17 +08:00
|
|
|
Passes.run(*M.get());
|
|
|
|
|
2010-08-20 09:12:13 +08:00
|
|
|
// Declare success.
|
|
|
|
Out.keep();
|
|
|
|
|
2007-05-06 13:13:17 +08:00
|
|
|
return 0;
|
2002-05-23 04:27:00 +08:00
|
|
|
}
|