llvm-extract.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This utility changes the input module to only contain a single function,
  11. // which is primarily used for debugging transformations.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/ADT/SetVector.h"
  15. #include "llvm/ADT/SmallPtrSet.h"
  16. #include "llvm/Bitcode/BitcodeWriterPass.h"
  17. #include "llvm/IR/DataLayout.h"
  18. #include "llvm/IR/IRPrintingPasses.h"
  19. #include "llvm/IR/LLVMContext.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IRReader/IRReader.h"
  22. #include "llvm/IR/LegacyPassManager.h"
  23. #include "llvm/Support/CommandLine.h"
  24. #include "llvm/Support/FileSystem.h"
  25. #include "llvm/Support/ManagedStatic.h"
  26. #include "llvm/Support/PrettyStackTrace.h"
  27. #include "llvm/Support/Regex.h"
  28. #include "llvm/Support/Signals.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. using namespace llvm;
  35. // InputFilename - The filename to read from.
  36. static cl::opt<std::string>
  37. InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
  38. cl::init("-"), cl::value_desc("filename"));
  39. static cl::opt<std::string>
  40. OutputFilename("o", cl::desc("Specify output filename"),
  41. cl::value_desc("filename"), cl::init("-"));
  42. static cl::opt<bool>
  43. Force("f", cl::desc("Enable binary output on terminals"));
  44. static cl::opt<bool>
  45. DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
  46. // ExtractFuncs - The functions to extract from the module.
  47. static cl::list<std::string>
  48. ExtractFuncs("func", cl::desc("Specify function to extract"),
  49. cl::ZeroOrMore, cl::value_desc("function"));
  50. // ExtractRegExpFuncs - The functions, matched via regular expression, to
  51. // extract from the module.
  52. static cl::list<std::string>
  53. ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
  54. "regular expression"),
  55. cl::ZeroOrMore, cl::value_desc("rfunction"));
  56. // ExtractAlias - The alias to extract from the module.
  57. static cl::list<std::string>
  58. ExtractAliases("alias", cl::desc("Specify alias to extract"),
  59. cl::ZeroOrMore, cl::value_desc("alias"));
  60. // ExtractRegExpAliases - The aliases, matched via regular expression, to
  61. // extract from the module.
  62. static cl::list<std::string>
  63. ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a "
  64. "regular expression"),
  65. cl::ZeroOrMore, cl::value_desc("ralias"));
  66. // ExtractGlobals - The globals to extract from the module.
  67. static cl::list<std::string>
  68. ExtractGlobals("glob", cl::desc("Specify global to extract"),
  69. cl::ZeroOrMore, cl::value_desc("global"));
  70. // ExtractRegExpGlobals - The globals, matched via regular expression, to
  71. // extract from the module...
  72. static cl::list<std::string>
  73. ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
  74. "regular expression"),
  75. cl::ZeroOrMore, cl::value_desc("rglobal"));
  76. static cl::opt<bool>
  77. OutputAssembly("S",
  78. cl::desc("Write output as LLVM assembly"), cl::Hidden);
  79. static cl::opt<bool> PreserveBitcodeUseListOrder(
  80. "preserve-bc-uselistorder",
  81. cl::desc("Preserve use-list order when writing LLVM bitcode."),
  82. cl::init(true), cl::Hidden);
  83. static cl::opt<bool> PreserveAssemblyUseListOrder(
  84. "preserve-ll-uselistorder",
  85. cl::desc("Preserve use-list order when writing LLVM assembly."),
  86. cl::init(false), cl::Hidden);
  87. int __cdecl main(int argc, char **argv) { // HLSL Change - __cdecl
  88. // Print a stack trace if we signal out.
  89. sys::PrintStackTraceOnErrorSignal();
  90. PrettyStackTraceProgram X(argc, argv);
  91. LLVMContext &Context = getGlobalContext();
  92. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  93. cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
  94. // Use lazy loading, since we only care about selected global values.
  95. SMDiagnostic Err;
  96. std::unique_ptr<Module> M = getLazyIRFileModule(InputFilename, Err, Context);
  97. if (!M.get()) {
  98. Err.print(argv[0], errs());
  99. return 1;
  100. }
  101. // Use SetVector to avoid duplicates.
  102. SetVector<GlobalValue *> GVs;
  103. // Figure out which aliases we should extract.
  104. for (size_t i = 0, e = ExtractAliases.size(); i != e; ++i) {
  105. GlobalAlias *GA = M->getNamedAlias(ExtractAliases[i]);
  106. if (!GA) {
  107. errs() << argv[0] << ": program doesn't contain alias named '"
  108. << ExtractAliases[i] << "'!\n";
  109. return 1;
  110. }
  111. GVs.insert(GA);
  112. }
  113. // Extract aliases via regular expression matching.
  114. for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) {
  115. std::string Error;
  116. Regex RegEx(ExtractRegExpAliases[i]);
  117. if (!RegEx.isValid(Error)) {
  118. errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' "
  119. "invalid regex: " << Error;
  120. }
  121. bool match = false;
  122. for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end();
  123. GA != E; GA++) {
  124. if (RegEx.match(GA->getName())) {
  125. GVs.insert(&*GA);
  126. match = true;
  127. }
  128. }
  129. if (!match) {
  130. errs() << argv[0] << ": program doesn't contain global named '"
  131. << ExtractRegExpAliases[i] << "'!\n";
  132. return 1;
  133. }
  134. }
  135. // Figure out which globals we should extract.
  136. for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
  137. GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
  138. if (!GV) {
  139. errs() << argv[0] << ": program doesn't contain global named '"
  140. << ExtractGlobals[i] << "'!\n";
  141. return 1;
  142. }
  143. GVs.insert(GV);
  144. }
  145. // Extract globals via regular expression matching.
  146. for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
  147. std::string Error;
  148. Regex RegEx(ExtractRegExpGlobals[i]);
  149. if (!RegEx.isValid(Error)) {
  150. errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
  151. "invalid regex: " << Error;
  152. }
  153. bool match = false;
  154. for (auto &GV : M->globals()) {
  155. if (RegEx.match(GV.getName())) {
  156. GVs.insert(&GV);
  157. match = true;
  158. }
  159. }
  160. if (!match) {
  161. errs() << argv[0] << ": program doesn't contain global named '"
  162. << ExtractRegExpGlobals[i] << "'!\n";
  163. return 1;
  164. }
  165. }
  166. // Figure out which functions we should extract.
  167. for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
  168. GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
  169. if (!GV) {
  170. errs() << argv[0] << ": program doesn't contain function named '"
  171. << ExtractFuncs[i] << "'!\n";
  172. return 1;
  173. }
  174. GVs.insert(GV);
  175. }
  176. // Extract functions via regular expression matching.
  177. for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
  178. std::string Error;
  179. StringRef RegExStr = ExtractRegExpFuncs[i];
  180. Regex RegEx(RegExStr);
  181. if (!RegEx.isValid(Error)) {
  182. errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
  183. "invalid regex: " << Error;
  184. }
  185. bool match = false;
  186. for (Module::iterator F = M->begin(), E = M->end(); F != E;
  187. F++) {
  188. if (RegEx.match(F->getName())) {
  189. GVs.insert(&*F);
  190. match = true;
  191. }
  192. }
  193. if (!match) {
  194. errs() << argv[0] << ": program doesn't contain global named '"
  195. << ExtractRegExpFuncs[i] << "'!\n";
  196. return 1;
  197. }
  198. }
  199. // Materialize requisite global values.
  200. if (!DeleteFn)
  201. for (size_t i = 0, e = GVs.size(); i != e; ++i) {
  202. GlobalValue *GV = GVs[i];
  203. if (std::error_code EC = GV->materialize()) {
  204. errs() << argv[0] << ": error reading input: " << EC.message() << "\n";
  205. return 1;
  206. }
  207. }
  208. else {
  209. // Deleting. Materialize every GV that's *not* in GVs.
  210. SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
  211. for (auto &G : M->globals()) {
  212. if (!GVSet.count(&G)) {
  213. if (std::error_code EC = G.materialize()) {
  214. errs() << argv[0] << ": error reading input: " << EC.message()
  215. << "\n";
  216. return 1;
  217. }
  218. }
  219. }
  220. for (auto &F : *M) {
  221. if (!GVSet.count(&F)) {
  222. if (std::error_code EC = F.materialize()) {
  223. errs() << argv[0] << ": error reading input: " << EC.message()
  224. << "\n";
  225. return 1;
  226. }
  227. }
  228. }
  229. }
  230. // In addition to deleting all other functions, we also want to spiff it
  231. // up a little bit. Do this now.
  232. legacy::PassManager Passes;
  233. std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
  234. Passes.add(createGVExtractionPass(Gvs, DeleteFn));
  235. if (!DeleteFn)
  236. Passes.add(createGlobalDCEPass()); // Delete unreachable globals
  237. Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
  238. Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
  239. std::error_code EC;
  240. tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
  241. if (EC) {
  242. errs() << EC.message() << '\n';
  243. return 1;
  244. }
  245. if (OutputAssembly)
  246. Passes.add(
  247. createPrintModulePass(Out.os(), "", PreserveAssemblyUseListOrder));
  248. else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
  249. Passes.add(createBitcodeWriterPass(Out.os(), PreserveBitcodeUseListOrder));
  250. Passes.run(*M.get());
  251. // Declare success.
  252. Out.keep();
  253. return 0;
  254. }