1 //===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This utility changes the input module to only contain a single function,
11 // which is primarily used for debugging transformations.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "llvm/Bitcode/ReaderWriter.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IRReader/IRReader.h"
23 #include "llvm/PassManager.h"
24 #include "llvm/Support/CommandLine.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"
36 // InputFilename - The filename to read from.
37 static cl::opt<std::string>
38 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
39 cl::init("-"), cl::value_desc("filename"));
41 static cl::opt<std::string>
42 OutputFilename("o", cl::desc("Specify output filename"),
43 cl::value_desc("filename"), cl::init("-"));
46 Force("f", cl::desc("Enable binary output on terminals"));
49 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
51 // ExtractFuncs - The functions to extract from the module.
52 static cl::list<std::string>
53 ExtractFuncs("func", cl::desc("Specify function to extract"),
54 cl::ZeroOrMore, cl::value_desc("function"));
56 // ExtractRegExpFuncs - The functions, matched via regular expression, to
57 // extract from the module.
58 static cl::list<std::string>
59 ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
60 "regular expression"),
61 cl::ZeroOrMore, cl::value_desc("rfunction"));
63 // ExtractAlias - The alias to extract from the module.
64 static cl::list<std::string>
65 ExtractAliases("alias", cl::desc("Specify alias to extract"),
66 cl::ZeroOrMore, cl::value_desc("alias"));
69 // ExtractRegExpAliases - The aliases, matched via regular expression, to
70 // extract from the module.
71 static cl::list<std::string>
72 ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a "
73 "regular expression"),
74 cl::ZeroOrMore, cl::value_desc("ralias"));
76 // ExtractGlobals - The globals to extract from the module.
77 static cl::list<std::string>
78 ExtractGlobals("glob", cl::desc("Specify global to extract"),
79 cl::ZeroOrMore, cl::value_desc("global"));
81 // ExtractRegExpGlobals - The globals, matched via regular expression, to
82 // extract from the module...
83 static cl::list<std::string>
84 ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
85 "regular expression"),
86 cl::ZeroOrMore, cl::value_desc("rglobal"));
90 cl::desc("Write output as LLVM assembly"), cl::Hidden);
92 int main(int argc, char **argv) {
93 // Print a stack trace if we signal out.
94 sys::PrintStackTraceOnErrorSignal();
95 PrettyStackTraceProgram X(argc, argv);
97 LLVMContext &Context = getGlobalContext();
98 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
99 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
101 // Use lazy loading, since we only care about selected global values.
104 M.reset(getLazyIRFileModule(InputFilename, Err, Context));
107 Err.print(argv[0], errs());
111 // Use SetVector to avoid duplicates.
112 SetVector<GlobalValue *> GVs;
114 // Figure out which aliases we should extract.
115 for (size_t i = 0, e = ExtractAliases.size(); i != e; ++i) {
116 GlobalAlias *GA = M->getNamedAlias(ExtractAliases[i]);
118 errs() << argv[0] << ": program doesn't contain alias named '"
119 << ExtractAliases[i] << "'!\n";
125 // Extract aliases via regular expression matching.
126 for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) {
128 Regex RegEx(ExtractRegExpAliases[i]);
129 if (!RegEx.isValid(Error)) {
130 errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' "
131 "invalid regex: " << Error;
134 for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end();
136 if (RegEx.match(GA->getName())) {
142 errs() << argv[0] << ": program doesn't contain global named '"
143 << ExtractRegExpAliases[i] << "'!\n";
148 // Figure out which globals we should extract.
149 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
150 GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
152 errs() << argv[0] << ": program doesn't contain global named '"
153 << ExtractGlobals[i] << "'!\n";
159 // Extract globals via regular expression matching.
160 for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
162 Regex RegEx(ExtractRegExpGlobals[i]);
163 if (!RegEx.isValid(Error)) {
164 errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
165 "invalid regex: " << Error;
168 for (Module::global_iterator GV = M->global_begin(),
169 E = M->global_end(); GV != E; GV++) {
170 if (RegEx.match(GV->getName())) {
176 errs() << argv[0] << ": program doesn't contain global named '"
177 << ExtractRegExpGlobals[i] << "'!\n";
182 // Figure out which functions we should extract.
183 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
184 GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
186 errs() << argv[0] << ": program doesn't contain function named '"
187 << ExtractFuncs[i] << "'!\n";
192 // Extract functions via regular expression matching.
193 for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
195 StringRef RegExStr = ExtractRegExpFuncs[i];
196 Regex RegEx(RegExStr);
197 if (!RegEx.isValid(Error)) {
198 errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
199 "invalid regex: " << Error;
202 for (Module::iterator F = M->begin(), E = M->end(); F != E;
204 if (RegEx.match(F->getName())) {
210 errs() << argv[0] << ": program doesn't contain global named '"
211 << ExtractRegExpFuncs[i] << "'!\n";
216 // Materialize requisite global values.
218 for (size_t i = 0, e = GVs.size(); i != e; ++i) {
219 GlobalValue *GV = GVs[i];
220 if (GV->isMaterializable()) {
222 if (GV->Materialize(&ErrInfo)) {
223 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
229 // Deleting. Materialize every GV that's *not* in GVs.
230 SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
231 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
233 GlobalVariable *G = I;
234 if (!GVSet.count(G) && G->isMaterializable()) {
236 if (G->Materialize(&ErrInfo)) {
237 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
242 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
244 if (!GVSet.count(F) && F->isMaterializable()) {
246 if (F->Materialize(&ErrInfo)) {
247 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
254 // In addition to deleting all other functions, we also want to spiff it
255 // up a little bit. Do this now.
257 Passes.add(new DataLayout(M.get())); // Use correct DataLayout
259 std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
261 Passes.add(createGVExtractionPass(Gvs, DeleteFn));
263 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
264 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
265 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
267 std::string ErrorInfo;
268 tool_output_file Out(OutputFilename.c_str(), ErrorInfo, sys::fs::F_Binary);
269 if (!ErrorInfo.empty()) {
270 errs() << ErrorInfo << '\n';
275 Passes.add(createPrintModulePass(&Out.os()));
276 else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
277 Passes.add(createBitcodeWriterPass(Out.os()));
279 Passes.run(*M.get());