llvm-extract.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This utility changes the input module to only contain a single function,
  10. // which is primarily used for debugging transformations.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/SetVector.h"
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/Bitcode/BitcodeWriterPass.h"
  16. #include "llvm/IR/DataLayout.h"
  17. #include "llvm/IR/IRPrintingPasses.h"
  18. #include "llvm/IR/Instructions.h"
  19. #include "llvm/IR/LLVMContext.h"
  20. #include "llvm/IR/LegacyPassManager.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/IRReader/IRReader.h"
  23. #include "llvm/Support/CommandLine.h"
  24. #include "llvm/Support/Error.h"
  25. #include "llvm/Support/FileSystem.h"
  26. #include "llvm/Support/InitLLVM.h"
  27. #include "llvm/Support/Regex.h"
  28. #include "llvm/Support/SourceMgr.h"
  29. #include "llvm/Support/SystemUtils.h"
  30. #include "llvm/Support/ToolOutputFile.h"
  31. #include "llvm/Transforms/IPO.h"
  32. #include <memory>
  33. #include <utility>
  34. using namespace llvm;
  35. cl::OptionCategory ExtractCat("llvm-extract Options");
  36. // InputFilename - The filename to read from.
  37. static cl::opt<std::string> InputFilename(cl::Positional,
  38. cl::desc("<input bitcode file>"),
  39. cl::init("-"),
  40. cl::value_desc("filename"));
  41. static cl::opt<std::string> OutputFilename("o",
  42. cl::desc("Specify output filename"),
  43. cl::value_desc("filename"),
  44. cl::init("-"), cl::cat(ExtractCat));
  45. static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"),
  46. cl::cat(ExtractCat));
  47. static cl::opt<bool> DeleteFn("delete",
  48. cl::desc("Delete specified Globals from Module"),
  49. cl::cat(ExtractCat));
  50. static cl::opt<bool> KeepConstInit("keep-const-init",
  51. cl::desc("Keep initializers of constants"),
  52. cl::cat(ExtractCat));
  53. static cl::opt<bool>
  54. Recursive("recursive", cl::desc("Recursively extract all called functions"),
  55. cl::cat(ExtractCat));
  56. // ExtractFuncs - The functions to extract from the module.
  57. static cl::list<std::string>
  58. ExtractFuncs("func", cl::desc("Specify function to extract"),
  59. cl::ZeroOrMore, cl::value_desc("function"),
  60. cl::cat(ExtractCat));
  61. // ExtractRegExpFuncs - The functions, matched via regular expression, to
  62. // extract from the module.
  63. static cl::list<std::string>
  64. ExtractRegExpFuncs("rfunc",
  65. cl::desc("Specify function(s) to extract using a "
  66. "regular expression"),
  67. cl::ZeroOrMore, cl::value_desc("rfunction"),
  68. cl::cat(ExtractCat));
  69. // ExtractBlocks - The blocks to extract from the module.
  70. static cl::list<std::string> ExtractBlocks(
  71. "bb",
  72. cl::desc(
  73. "Specify <function, basic block1[;basic block2...]> pairs to extract.\n"
  74. "Each pair will create a function.\n"
  75. "If multiple basic blocks are specified in one pair,\n"
  76. "the first block in the sequence should dominate the rest.\n"
  77. "eg:\n"
  78. " --bb=f:bb1;bb2 will extract one function with both bb1 and bb2;\n"
  79. " --bb=f:bb1 --bb=f:bb2 will extract two functions, one with bb1, one "
  80. "with bb2."),
  81. cl::ZeroOrMore, cl::value_desc("function:bb1[;bb2...]"),
  82. cl::cat(ExtractCat));
  83. // ExtractAlias - The alias to extract from the module.
  84. static cl::list<std::string>
  85. ExtractAliases("alias", cl::desc("Specify alias to extract"),
  86. cl::ZeroOrMore, cl::value_desc("alias"),
  87. cl::cat(ExtractCat));
  88. // ExtractRegExpAliases - The aliases, matched via regular expression, to
  89. // extract from the module.
  90. static cl::list<std::string>
  91. ExtractRegExpAliases("ralias",
  92. cl::desc("Specify alias(es) to extract using a "
  93. "regular expression"),
  94. cl::ZeroOrMore, cl::value_desc("ralias"),
  95. cl::cat(ExtractCat));
  96. // ExtractGlobals - The globals to extract from the module.
  97. static cl::list<std::string>
  98. ExtractGlobals("glob", cl::desc("Specify global to extract"),
  99. cl::ZeroOrMore, cl::value_desc("global"),
  100. cl::cat(ExtractCat));
  101. // ExtractRegExpGlobals - The globals, matched via regular expression, to
  102. // extract from the module...
  103. static cl::list<std::string>
  104. ExtractRegExpGlobals("rglob",
  105. cl::desc("Specify global(s) to extract using a "
  106. "regular expression"),
  107. cl::ZeroOrMore, cl::value_desc("rglobal"),
  108. cl::cat(ExtractCat));
  109. static cl::opt<bool> OutputAssembly("S",
  110. cl::desc("Write output as LLVM assembly"),
  111. cl::Hidden, cl::cat(ExtractCat));
  112. static cl::opt<bool> PreserveBitcodeUseListOrder(
  113. "preserve-bc-uselistorder",
  114. cl::desc("Preserve use-list order when writing LLVM bitcode."),
  115. cl::init(true), cl::Hidden, cl::cat(ExtractCat));
  116. static cl::opt<bool> PreserveAssemblyUseListOrder(
  117. "preserve-ll-uselistorder",
  118. cl::desc("Preserve use-list order when writing LLVM assembly."),
  119. cl::init(false), cl::Hidden, cl::cat(ExtractCat));
  120. int main(int argc, char **argv) {
  121. InitLLVM X(argc, argv);
  122. LLVMContext Context;
  123. cl::HideUnrelatedOptions(ExtractCat);
  124. cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
  125. // Use lazy loading, since we only care about selected global values.
  126. SMDiagnostic Err;
  127. std::unique_ptr<Module> M = getLazyIRFileModule(InputFilename, Err, Context);
  128. if (!M.get()) {
  129. Err.print(argv[0], errs());
  130. return 1;
  131. }
  132. // Use SetVector to avoid duplicates.
  133. SetVector<GlobalValue *> GVs;
  134. // Figure out which aliases we should extract.
  135. for (size_t i = 0, e = ExtractAliases.size(); i != e; ++i) {
  136. GlobalAlias *GA = M->getNamedAlias(ExtractAliases[i]);
  137. if (!GA) {
  138. errs() << argv[0] << ": program doesn't contain alias named '"
  139. << ExtractAliases[i] << "'!\n";
  140. return 1;
  141. }
  142. GVs.insert(GA);
  143. }
  144. // Extract aliases via regular expression matching.
  145. for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) {
  146. std::string Error;
  147. Regex RegEx(ExtractRegExpAliases[i]);
  148. if (!RegEx.isValid(Error)) {
  149. errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' "
  150. "invalid regex: " << Error;
  151. }
  152. bool match = false;
  153. for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end();
  154. GA != E; GA++) {
  155. if (RegEx.match(GA->getName())) {
  156. GVs.insert(&*GA);
  157. match = true;
  158. }
  159. }
  160. if (!match) {
  161. errs() << argv[0] << ": program doesn't contain global named '"
  162. << ExtractRegExpAliases[i] << "'!\n";
  163. return 1;
  164. }
  165. }
  166. // Figure out which globals we should extract.
  167. for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
  168. GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
  169. if (!GV) {
  170. errs() << argv[0] << ": program doesn't contain global named '"
  171. << ExtractGlobals[i] << "'!\n";
  172. return 1;
  173. }
  174. GVs.insert(GV);
  175. }
  176. // Extract globals via regular expression matching.
  177. for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
  178. std::string Error;
  179. Regex RegEx(ExtractRegExpGlobals[i]);
  180. if (!RegEx.isValid(Error)) {
  181. errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
  182. "invalid regex: " << Error;
  183. }
  184. bool match = false;
  185. for (auto &GV : M->globals()) {
  186. if (RegEx.match(GV.getName())) {
  187. GVs.insert(&GV);
  188. match = true;
  189. }
  190. }
  191. if (!match) {
  192. errs() << argv[0] << ": program doesn't contain global named '"
  193. << ExtractRegExpGlobals[i] << "'!\n";
  194. return 1;
  195. }
  196. }
  197. // Figure out which functions we should extract.
  198. for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
  199. GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
  200. if (!GV) {
  201. errs() << argv[0] << ": program doesn't contain function named '"
  202. << ExtractFuncs[i] << "'!\n";
  203. return 1;
  204. }
  205. GVs.insert(GV);
  206. }
  207. // Extract functions via regular expression matching.
  208. for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
  209. std::string Error;
  210. StringRef RegExStr = ExtractRegExpFuncs[i];
  211. Regex RegEx(RegExStr);
  212. if (!RegEx.isValid(Error)) {
  213. errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
  214. "invalid regex: " << Error;
  215. }
  216. bool match = false;
  217. for (Module::iterator F = M->begin(), E = M->end(); F != E;
  218. F++) {
  219. if (RegEx.match(F->getName())) {
  220. GVs.insert(&*F);
  221. match = true;
  222. }
  223. }
  224. if (!match) {
  225. errs() << argv[0] << ": program doesn't contain global named '"
  226. << ExtractRegExpFuncs[i] << "'!\n";
  227. return 1;
  228. }
  229. }
  230. // Figure out which BasicBlocks we should extract.
  231. SmallVector<std::pair<Function *, SmallVector<StringRef, 16>>, 2> BBMap;
  232. for (StringRef StrPair : ExtractBlocks) {
  233. SmallVector<StringRef, 16> BBNames;
  234. auto BBInfo = StrPair.split(':');
  235. // Get the function.
  236. Function *F = M->getFunction(BBInfo.first);
  237. if (!F) {
  238. errs() << argv[0] << ": program doesn't contain a function named '"
  239. << BBInfo.first << "'!\n";
  240. return 1;
  241. }
  242. // Add the function to the materialize list, and store the basic block names
  243. // to check after materialization.
  244. GVs.insert(F);
  245. BBInfo.second.split(BBNames, ';', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
  246. BBMap.push_back({F, std::move(BBNames)});
  247. }
  248. // Use *argv instead of argv[0] to work around a wrong GCC warning.
  249. ExitOnError ExitOnErr(std::string(*argv) + ": error reading input: ");
  250. if (Recursive) {
  251. std::vector<llvm::Function *> Workqueue;
  252. for (GlobalValue *GV : GVs) {
  253. if (auto *F = dyn_cast<Function>(GV)) {
  254. Workqueue.push_back(F);
  255. }
  256. }
  257. while (!Workqueue.empty()) {
  258. Function *F = &*Workqueue.back();
  259. Workqueue.pop_back();
  260. ExitOnErr(F->materialize());
  261. for (auto &BB : *F) {
  262. for (auto &I : BB) {
  263. CallBase *CB = dyn_cast<CallBase>(&I);
  264. if (!CB)
  265. continue;
  266. Function *CF = CB->getCalledFunction();
  267. if (!CF)
  268. continue;
  269. if (CF->isDeclaration() || GVs.count(CF))
  270. continue;
  271. GVs.insert(CF);
  272. Workqueue.push_back(CF);
  273. }
  274. }
  275. }
  276. }
  277. auto Materialize = [&](GlobalValue &GV) { ExitOnErr(GV.materialize()); };
  278. // Materialize requisite global values.
  279. if (!DeleteFn) {
  280. for (size_t i = 0, e = GVs.size(); i != e; ++i)
  281. Materialize(*GVs[i]);
  282. } else {
  283. // Deleting. Materialize every GV that's *not* in GVs.
  284. SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
  285. for (auto &F : *M) {
  286. if (!GVSet.count(&F))
  287. Materialize(F);
  288. }
  289. }
  290. {
  291. std::vector<GlobalValue *> Gvs(GVs.begin(), GVs.end());
  292. legacy::PassManager Extract;
  293. Extract.add(createGVExtractionPass(Gvs, DeleteFn, KeepConstInit));
  294. Extract.run(*M);
  295. // Now that we have all the GVs we want, mark the module as fully
  296. // materialized.
  297. // FIXME: should the GVExtractionPass handle this?
  298. ExitOnErr(M->materializeAll());
  299. }
  300. // Extract the specified basic blocks from the module and erase the existing
  301. // functions.
  302. if (!ExtractBlocks.empty()) {
  303. // Figure out which BasicBlocks we should extract.
  304. SmallVector<SmallVector<BasicBlock *, 16>, 4> GroupOfBBs;
  305. for (auto &P : BBMap) {
  306. SmallVector<BasicBlock *, 16> BBs;
  307. for (StringRef BBName : P.second) {
  308. // The function has been materialized, so add its matching basic blocks
  309. // to the block extractor list, or fail if a name is not found.
  310. auto Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
  311. return BB.getName().equals(BBName);
  312. });
  313. if (Res == P.first->end()) {
  314. errs() << argv[0] << ": function " << P.first->getName()
  315. << " doesn't contain a basic block named '" << BBName
  316. << "'!\n";
  317. return 1;
  318. }
  319. BBs.push_back(&*Res);
  320. }
  321. GroupOfBBs.push_back(BBs);
  322. }
  323. legacy::PassManager PM;
  324. PM.add(createBlockExtractorPass(GroupOfBBs, true));
  325. PM.run(*M);
  326. }
  327. // In addition to deleting all other functions, we also want to spiff it
  328. // up a little bit. Do this now.
  329. legacy::PassManager Passes;
  330. if (!DeleteFn)
  331. Passes.add(createGlobalDCEPass()); // Delete unreachable globals
  332. Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
  333. Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
  334. std::error_code EC;
  335. ToolOutputFile Out(OutputFilename, EC, sys::fs::OF_None);
  336. if (EC) {
  337. errs() << EC.message() << '\n';
  338. return 1;
  339. }
  340. if (OutputAssembly)
  341. Passes.add(
  342. createPrintModulePass(Out.os(), "", PreserveAssemblyUseListOrder));
  343. else if (Force || !CheckBitcodeOutputToConsole(Out.os()))
  344. Passes.add(createBitcodeWriterPass(Out.os(), PreserveBitcodeUseListOrder));
  345. Passes.run(*M.get());
  346. // Declare success.
  347. Out.keep();
  348. return 0;
  349. }