llvm-extract.cpp 14 KB

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