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/PassManager.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/IRReader.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/SystemUtils.h"
30 #include "llvm/Support/ToolOutputFile.h"
31 #include "llvm/Transforms/IPO.h"
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"));
40 static cl::opt<std::string>
41 OutputFilename("o", cl::desc("Specify output filename"),
42 cl::value_desc("filename"), cl::init("-"));
45 Force("f", cl::desc("Enable binary output on terminals"));
48 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
50 // ExtractFuncs - The functions to extract from the module.
51 static cl::list<std::string>
52 ExtractFuncs("func", cl::desc("Specify function to extract"),
53 cl::ZeroOrMore, cl::value_desc("function"));
55 // ExtractRegExpFuncs - The functions, matched via regular expression, to
56 // extract from the module.
57 static cl::list<std::string>
58 ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
59 "regular expression"),
60 cl::ZeroOrMore, cl::value_desc("rfunction"));
62 // ExtractAlias - The alias to extract from the module.
63 static cl::list<std::string>
64 ExtractAliases("alias", cl::desc("Specify alias to extract"),
65 cl::ZeroOrMore, cl::value_desc("alias"));
68 // ExtractRegExpAliases - The aliases, matched via regular expression, to
69 // extract from the module.
70 static cl::list<std::string>
71 ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a "
72 "regular expression"),
73 cl::ZeroOrMore, cl::value_desc("ralias"));
75 // ExtractGlobals - The globals to extract from the module.
76 static cl::list<std::string>
77 ExtractGlobals("glob", cl::desc("Specify global to extract"),
78 cl::ZeroOrMore, cl::value_desc("global"));
80 // ExtractRegExpGlobals - The globals, matched via regular expression, to
81 // extract from the module...
82 static cl::list<std::string>
83 ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
84 "regular expression"),
85 cl::ZeroOrMore, cl::value_desc("rglobal"));
89 cl::desc("Write output as LLVM assembly"), cl::Hidden);
91 int main(int argc, char **argv) {
92 // Print a stack trace if we signal out.
93 sys::PrintStackTraceOnErrorSignal();
94 PrettyStackTraceProgram X(argc, argv);
96 LLVMContext &Context = getGlobalContext();
97 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
98 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
100 // Use lazy loading, since we only care about selected global values.
102 std::auto_ptr<Module> M;
103 M.reset(getLazyIRFileModule(InputFilename, Err, Context));
106 Err.print(argv[0], errs());
110 // Use SetVector to avoid duplicates.
111 SetVector<GlobalValue *> GVs;
113 // Figure out which aliases we should extract.
114 for (size_t i = 0, e = ExtractAliases.size(); i != e; ++i) {
115 GlobalAlias *GA = M->getNamedAlias(ExtractAliases[i]);
117 errs() << argv[0] << ": program doesn't contain alias named '"
118 << ExtractAliases[i] << "'!\n";
124 // Extract aliases via regular expression matching.
125 for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) {
127 Regex RegEx(ExtractRegExpAliases[i]);
128 if (!RegEx.isValid(Error)) {
129 errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' "
130 "invalid regex: " << Error;
133 for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end();
135 if (RegEx.match(GA->getName())) {
141 errs() << argv[0] << ": program doesn't contain global named '"
142 << ExtractRegExpAliases[i] << "'!\n";
147 // Figure out which globals we should extract.
148 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
149 GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
151 errs() << argv[0] << ": program doesn't contain global named '"
152 << ExtractGlobals[i] << "'!\n";
158 // Extract globals via regular expression matching.
159 for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
161 Regex RegEx(ExtractRegExpGlobals[i]);
162 if (!RegEx.isValid(Error)) {
163 errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
164 "invalid regex: " << Error;
167 for (Module::global_iterator GV = M->global_begin(),
168 E = M->global_end(); GV != E; GV++) {
169 if (RegEx.match(GV->getName())) {
175 errs() << argv[0] << ": program doesn't contain global named '"
176 << ExtractRegExpGlobals[i] << "'!\n";
181 // Figure out which functions we should extract.
182 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
183 GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
185 errs() << argv[0] << ": program doesn't contain function named '"
186 << ExtractFuncs[i] << "'!\n";
191 // Extract functions via regular expression matching.
192 for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
194 StringRef RegExStr = ExtractRegExpFuncs[i];
195 Regex RegEx(RegExStr);
196 if (!RegEx.isValid(Error)) {
197 errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
198 "invalid regex: " << Error;
201 for (Module::iterator F = M->begin(), E = M->end(); F != E;
203 if (RegEx.match(F->getName())) {
209 errs() << argv[0] << ": program doesn't contain global named '"
210 << ExtractRegExpFuncs[i] << "'!\n";
215 // Materialize requisite global values.
217 for (size_t i = 0, e = GVs.size(); i != e; ++i) {
218 GlobalValue *GV = GVs[i];
219 if (GV->isMaterializable()) {
221 if (GV->Materialize(&ErrInfo)) {
222 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
228 // Deleting. Materialize every GV that's *not* in GVs.
229 SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
230 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
232 GlobalVariable *G = I;
233 if (!GVSet.count(G) && G->isMaterializable()) {
235 if (G->Materialize(&ErrInfo)) {
236 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
241 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
243 if (!GVSet.count(F) && F->isMaterializable()) {
245 if (F->Materialize(&ErrInfo)) {
246 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
253 // In addition to deleting all other functions, we also want to spiff it
254 // up a little bit. Do this now.
256 Passes.add(new DataLayout(M.get())); // Use correct DataLayout
258 std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
260 Passes.add(createGVExtractionPass(Gvs, DeleteFn));
262 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
263 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
264 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
266 std::string ErrorInfo;
267 tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
268 raw_fd_ostream::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());