llvm-extract.cpp 14 KB

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