2005-04-25 01:36:05 +08:00
|
|
|
//===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
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"
|
2017-04-07 04:51:40 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2014-01-12 19:10:32 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2017-04-07 04:51:40 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.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"
|
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"
|
2018-04-14 02:26:06 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2011-09-17 05:09:17 +08:00
|
|
|
#include "llvm/Support/Regex.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;
|
|
|
|
|
2019-06-13 05:08:19 +08:00
|
|
|
cl::OptionCategory ExtractCat("llvm-extract Options");
|
|
|
|
|
2002-07-22 10:10:13 +08:00
|
|
|
// InputFilename - The filename to read from.
|
2019-06-13 05:08:19 +08:00
|
|
|
static cl::opt<std::string> InputFilename(cl::Positional,
|
|
|
|
cl::desc("<input bitcode file>"),
|
|
|
|
cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2019-06-13 05:08:19 +08:00
|
|
|
static cl::opt<std::string> OutputFilename("o",
|
|
|
|
cl::desc("Specify output filename"),
|
|
|
|
cl::value_desc("filename"),
|
|
|
|
cl::init("-"), cl::cat(ExtractCat));
|
2004-02-19 00:53:59 +08:00
|
|
|
|
2019-06-13 05:08:19 +08:00
|
|
|
static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"),
|
|
|
|
cl::cat(ExtractCat));
|
2002-07-22 10:10:13 +08:00
|
|
|
|
2019-06-13 05:08:19 +08:00
|
|
|
static cl::opt<bool> DeleteFn("delete",
|
|
|
|
cl::desc("Delete specified Globals from Module"),
|
|
|
|
cl::cat(ExtractCat));
|
2004-04-23 07:07:39 +08:00
|
|
|
|
2020-02-02 01:34:10 +08:00
|
|
|
static cl::opt<bool> KeepConstInit("keep-const-init",
|
|
|
|
cl::desc("Keep initializers of constants"),
|
|
|
|
cl::cat(ExtractCat));
|
|
|
|
|
2017-04-07 04:51:40 +08:00
|
|
|
static cl::opt<bool>
|
2019-06-13 05:08:19 +08:00
|
|
|
Recursive("recursive", cl::desc("Recursively extract all called functions"),
|
|
|
|
cl::cat(ExtractCat));
|
2017-04-07 04:51:40 +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>
|
2019-06-13 05:08:19 +08:00
|
|
|
ExtractFuncs("func", cl::desc("Specify function to extract"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("function"),
|
|
|
|
cl::cat(ExtractCat));
|
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>
|
2019-06-13 05:08:19 +08:00
|
|
|
ExtractRegExpFuncs("rfunc",
|
|
|
|
cl::desc("Specify function(s) to extract using a "
|
|
|
|
"regular expression"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("rfunction"),
|
|
|
|
cl::cat(ExtractCat));
|
2011-09-17 05:09:17 +08:00
|
|
|
|
2018-01-24 05:51:34 +08:00
|
|
|
// ExtractBlocks - The blocks to extract from the module.
|
2019-06-13 05:08:19 +08:00
|
|
|
static cl::list<std::string> ExtractBlocks(
|
2019-08-21 04:45:16 +08:00
|
|
|
"bb",
|
|
|
|
cl::desc(
|
|
|
|
"Specify <function, basic block1[;basic block2...]> pairs to extract.\n"
|
|
|
|
"Each pair will create a function.\n"
|
|
|
|
"If multiple basic blocks are specified in one pair,\n"
|
|
|
|
"the first block in the sequence should dominate the rest.\n"
|
|
|
|
"eg:\n"
|
|
|
|
" --bb=f:bb1;bb2 will extract one function with both bb1 and bb2;\n"
|
|
|
|
" --bb=f:bb1 --bb=f:bb2 will extract two functions, one with bb1, one "
|
|
|
|
"with bb2."),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("function:bb1[;bb2...]"),
|
|
|
|
cl::cat(ExtractCat));
|
2018-01-24 05:51:34 +08:00
|
|
|
|
2012-10-29 10:23:07 +08:00
|
|
|
// ExtractAlias - The alias to extract from the module.
|
|
|
|
static cl::list<std::string>
|
2019-06-13 05:08:19 +08:00
|
|
|
ExtractAliases("alias", cl::desc("Specify alias to extract"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("alias"),
|
|
|
|
cl::cat(ExtractCat));
|
2012-10-29 10:23:07 +08:00
|
|
|
|
|
|
|
// ExtractRegExpAliases - The aliases, matched via regular expression, to
|
|
|
|
// extract from the module.
|
|
|
|
static cl::list<std::string>
|
2019-06-13 05:08:19 +08:00
|
|
|
ExtractRegExpAliases("ralias",
|
|
|
|
cl::desc("Specify alias(es) to extract using a "
|
|
|
|
"regular expression"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("ralias"),
|
|
|
|
cl::cat(ExtractCat));
|
2012-10-29 10:23:07 +08:00
|
|
|
|
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>
|
2019-06-13 05:08:19 +08:00
|
|
|
ExtractGlobals("glob", cl::desc("Specify global to extract"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("global"),
|
|
|
|
cl::cat(ExtractCat));
|
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>
|
2019-06-13 05:08:19 +08:00
|
|
|
ExtractRegExpGlobals("rglob",
|
|
|
|
cl::desc("Specify global(s) to extract using a "
|
|
|
|
"regular expression"),
|
|
|
|
cl::ZeroOrMore, cl::value_desc("rglobal"),
|
|
|
|
cl::cat(ExtractCat));
|
2011-09-17 05:09:17 +08:00
|
|
|
|
2019-06-13 05:08:19 +08:00
|
|
|
static cl::opt<bool> OutputAssembly("S",
|
|
|
|
cl::desc("Write output as LLVM assembly"),
|
|
|
|
cl::Hidden, cl::cat(ExtractCat));
|
2009-09-12 04:46:33 +08:00
|
|
|
|
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."),
|
2019-06-13 05:08:19 +08:00
|
|
|
cl::init(true), cl::Hidden, cl::cat(ExtractCat));
|
2015-04-15 11:14:06 +08:00
|
|
|
|
|
|
|
static cl::opt<bool> PreserveAssemblyUseListOrder(
|
|
|
|
"preserve-ll-uselistorder",
|
|
|
|
cl::desc("Preserve use-list order when writing LLVM assembly."),
|
2019-06-13 05:08:19 +08:00
|
|
|
cl::init(false), cl::Hidden, cl::cat(ExtractCat));
|
2015-04-15 11:14:06 +08:00
|
|
|
|
2002-05-23 04:27:00 +08:00
|
|
|
int main(int argc, char **argv) {
|
2018-04-14 02:26:06 +08:00
|
|
|
InitLLVM X(argc, argv);
|
2009-07-02 00:58:40 +08:00
|
|
|
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext Context;
|
2019-06-13 05:08:19 +08:00
|
|
|
cl::HideUnrelatedOptions(ExtractCat);
|
2009-03-06 13:34:10 +08:00
|
|
|
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
|
|
|
|
2018-01-24 05:51:34 +08:00
|
|
|
// Figure out which BasicBlocks we should extract.
|
2019-04-30 00:14:03 +08:00
|
|
|
SmallVector<SmallVector<BasicBlock *, 16>, 4> GroupOfBBs;
|
2018-01-24 05:51:34 +08:00
|
|
|
for (StringRef StrPair : ExtractBlocks) {
|
|
|
|
auto BBInfo = StrPair.split(':');
|
|
|
|
// Get the function.
|
|
|
|
Function *F = M->getFunction(BBInfo.first);
|
|
|
|
if (!F) {
|
|
|
|
errs() << argv[0] << ": program doesn't contain a function named '"
|
|
|
|
<< BBInfo.first << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// Do not materialize this function.
|
|
|
|
GVs.insert(F);
|
2019-04-30 00:14:03 +08:00
|
|
|
// Get the basic blocks.
|
|
|
|
SmallVector<BasicBlock *, 16> BBs;
|
|
|
|
SmallVector<StringRef, 16> BBNames;
|
|
|
|
BBInfo.second.split(BBNames, ';', /*MaxSplit=*/-1,
|
|
|
|
/*KeepEmpty=*/false);
|
|
|
|
for (StringRef BBName : BBNames) {
|
|
|
|
auto Res = llvm::find_if(*F, [&](const BasicBlock &BB) {
|
|
|
|
return BB.getName().equals(BBName);
|
|
|
|
});
|
|
|
|
if (Res == F->end()) {
|
|
|
|
errs() << argv[0] << ": function " << F->getName()
|
|
|
|
<< " doesn't contain a basic block named '" << BBInfo.second
|
|
|
|
<< "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
BBs.push_back(&*Res);
|
2018-01-24 05:51:34 +08:00
|
|
|
}
|
2019-04-30 00:14:03 +08:00
|
|
|
GroupOfBBs.push_back(BBs);
|
2018-01-24 05:51:34 +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
|
|
|
|
2017-04-07 04:51:40 +08:00
|
|
|
if (Recursive) {
|
|
|
|
std::vector<llvm::Function *> Workqueue;
|
|
|
|
for (GlobalValue *GV : GVs) {
|
|
|
|
if (auto *F = dyn_cast<Function>(GV)) {
|
|
|
|
Workqueue.push_back(F);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!Workqueue.empty()) {
|
|
|
|
Function *F = &*Workqueue.back();
|
|
|
|
Workqueue.pop_back();
|
|
|
|
ExitOnErr(F->materialize());
|
|
|
|
for (auto &BB : *F) {
|
|
|
|
for (auto &I : BB) {
|
2019-04-05 07:30:47 +08:00
|
|
|
CallBase *CB = dyn_cast<CallBase>(&I);
|
|
|
|
if (!CB)
|
2017-04-07 04:51:40 +08:00
|
|
|
continue;
|
2019-04-05 07:30:47 +08:00
|
|
|
Function *CF = CB->getCalledFunction();
|
2017-04-07 04:51:40 +08:00
|
|
|
if (!CF)
|
|
|
|
continue;
|
|
|
|
if (CF->isDeclaration() || GVs.count(CF))
|
|
|
|
continue;
|
|
|
|
GVs.insert(CF);
|
|
|
|
Workqueue.push_back(CF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2020-02-02 01:34:10 +08:00
|
|
|
Extract.add(createGVExtractionPass(Gvs, DeleteFn, KeepConstInit));
|
2016-01-16 03:00:20 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-24 05:51:34 +08:00
|
|
|
// Extract the specified basic blocks from the module and erase the existing
|
|
|
|
// functions.
|
|
|
|
if (!ExtractBlocks.empty()) {
|
|
|
|
legacy::PassManager PM;
|
2019-04-30 00:14:03 +08:00
|
|
|
PM.add(createBlockExtractorPass(GroupOfBBs, true));
|
2018-01-24 05:51:34 +08:00
|
|
|
PM.run(*M);
|
|
|
|
}
|
|
|
|
|
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;
|
2019-08-05 13:43:48 +08:00
|
|
|
ToolOutputFile Out(OutputFilename, EC, sys::fs::OF_None);
|
2014-08-26 02:16:47 +08:00
|
|
|
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
|
|
|
}
|