blob: 2ed11c52b2b3a35b978a4f152e02555e5d11f46d [file] [log] [blame]
Misha Brukmane22594f2005-04-24 17:36:05 +00001//===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner055de182002-05-22 20:27:00 +00009//
10// This utility changes the input module to only contain a single function,
11// which is primarily used for debugging transformations.
12//
13//===----------------------------------------------------------------------===//
14
Owen Anderson6773d382009-07-01 16:58:40 +000015#include "llvm/LLVMContext.h"
Chris Lattner055de182002-05-22 20:27:00 +000016#include "llvm/Module.h"
17#include "llvm/PassManager.h"
Dan Gohmane5929232009-09-11 20:46:33 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner27936992007-05-06 05:13:17 +000019#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattnerda02d412002-07-23 22:04:40 +000020#include "llvm/Transforms/IPO.h"
Brian Gaeke8fb7af12003-10-28 22:22:16 +000021#include "llvm/Target/TargetData.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Dan Gohmane5929232009-09-11 20:46:33 +000023#include "llvm/Support/IRReader.h"
Chris Lattner76d46322006-12-06 01:18:01 +000024#include "llvm/Support/ManagedStatic.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Dan Gohman0df7ea42010-10-07 20:32:40 +000026#include "llvm/Support/ToolOutputFile.h"
Dan Gohman61a87962009-08-25 15:34:52 +000027#include "llvm/Support/SystemUtils.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000028#include "llvm/Support/Signals.h"
Chad Rosierf0d37862011-09-16 21:09:17 +000029#include "llvm/Support/Regex.h"
Dan Gohman0d2c07c2010-09-23 00:33:13 +000030#include "llvm/ADT/SmallPtrSet.h"
Chad Rosierf0d37862011-09-16 21:09:17 +000031#include "llvm/ADT/SetVector.h"
Chris Lattner055de182002-05-22 20:27:00 +000032#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000033using namespace llvm;
34
Chris Lattnerf5cad152002-07-22 02:10:13 +000035// InputFilename - The filename to read from.
Chris Lattner64a67272002-07-25 16:31:09 +000036static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000037InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000038 cl::init("-"), cl::value_desc("filename"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000039
Chris Lattner3b203f52004-02-18 16:53:59 +000040static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000041OutputFilename("o", cl::desc("Specify output filename"),
Chris Lattner3b203f52004-02-18 16:53:59 +000042 cl::value_desc("filename"), cl::init("-"));
43
44static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000045Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000046
Misha Brukman2ccac822004-04-22 23:07:39 +000047static cl::opt<bool>
Andrew Lenharth3f13b662008-03-07 19:51:57 +000048DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
Misha Brukman2ccac822004-04-22 23:07:39 +000049
Chad Rosierf0d37862011-09-16 21:09:17 +000050// ExtractFuncs - The functions to extract from the module.
Dan Gohmanf684e452010-02-10 23:58:53 +000051static cl::list<std::string>
52ExtractFuncs("func", cl::desc("Specify function to extract"),
53 cl::ZeroOrMore, cl::value_desc("function"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000054
Chad Rosierf0d37862011-09-16 21:09:17 +000055// ExtractRegExpFuncs - The functions, matched via regular expression, to
56// extract from the module.
57static cl::list<std::string>
58ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
59 "regular expression"),
60 cl::ZeroOrMore, cl::value_desc("rfunction"));
61
62// ExtractGlobals - The globals to extract from the module.
Dan Gohmanf684e452010-02-10 23:58:53 +000063static cl::list<std::string>
64ExtractGlobals("glob", cl::desc("Specify global to extract"),
65 cl::ZeroOrMore, cl::value_desc("global"));
Andrew Lenharth3f13b662008-03-07 19:51:57 +000066
Chad Rosierf0d37862011-09-16 21:09:17 +000067// ExtractRegExpGlobals - The globals, matched via regular expression, to
68// extract from the module...
69static cl::list<std::string>
70ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
71 "regular expression"),
72 cl::ZeroOrMore, cl::value_desc("rglobal"));
73
Dan Gohmane5929232009-09-11 20:46:33 +000074static cl::opt<bool>
75OutputAssembly("S",
76 cl::desc("Write output as LLVM assembly"), cl::Hidden);
77
Chris Lattner055de182002-05-22 20:27:00 +000078int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +000079 // Print a stack trace if we signal out.
Chris Lattner27936992007-05-06 05:13:17 +000080 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000081 PrettyStackTraceProgram X(argc, argv);
Owen Anderson6773d382009-07-01 16:58:40 +000082
Owen Anderson19251ec2009-07-15 22:16:10 +000083 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000084 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
85 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
Chris Lattner055de182002-05-22 20:27:00 +000086
Dan Gohmanbf154592010-08-25 23:33:07 +000087 // Use lazy loading, since we only care about selected global values.
Dan Gohmane5929232009-09-11 20:46:33 +000088 SMDiagnostic Err;
Chris Lattner27936992007-05-06 05:13:17 +000089 std::auto_ptr<Module> M;
Dan Gohmanbf154592010-08-25 23:33:07 +000090 M.reset(getLazyIRFileModule(InputFilename, Err, Context));
Dan Gohmane5929232009-09-11 20:46:33 +000091
Chris Lattner27936992007-05-06 05:13:17 +000092 if (M.get() == 0) {
Chris Lattnera3a06812011-10-16 04:47:35 +000093 Err.print(argv[0], errs());
Chris Lattner27936992007-05-06 05:13:17 +000094 return 1;
95 }
96
Chad Rosierf0d37862011-09-16 21:09:17 +000097 // Use SetVector to avoid duplicates.
98 SetVector<GlobalValue *> GVs;
Andrew Lenharth3f13b662008-03-07 19:51:57 +000099
Dan Gohmanf684e452010-02-10 23:58:53 +0000100 // Figure out which globals we should extract.
101 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000102 GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
Dan Gohmanf684e452010-02-10 23:58:53 +0000103 if (!GV) {
104 errs() << argv[0] << ": program doesn't contain global named '"
105 << ExtractGlobals[i] << "'!\n";
106 return 1;
107 }
Chad Rosierf0d37862011-09-16 21:09:17 +0000108 GVs.insert(GV);
109 }
110
111 // Extract globals via regular expression matching.
112 for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
113 std::string Error;
114 Regex RegEx(ExtractRegExpGlobals[i]);
115 if (!RegEx.isValid(Error)) {
116 errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
117 "invalid regex: " << Error;
118 }
119 bool match = false;
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000120 for (Module::global_iterator GV = M->global_begin(),
121 E = M->global_end(); GV != E; GV++) {
Chad Rosierf0d37862011-09-16 21:09:17 +0000122 if (RegEx.match(GV->getName())) {
123 GVs.insert(&*GV);
124 match = true;
125 }
126 }
127 if (!match) {
128 errs() << argv[0] << ": program doesn't contain global named '"
129 << ExtractRegExpGlobals[i] << "'!\n";
130 return 1;
131 }
Dan Gohmanf684e452010-02-10 23:58:53 +0000132 }
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000133
Dan Gohmanf684e452010-02-10 23:58:53 +0000134 // Figure out which functions we should extract.
135 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000136 GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
Dan Gohmanf684e452010-02-10 23:58:53 +0000137 if (!GV) {
138 errs() << argv[0] << ": program doesn't contain function named '"
139 << ExtractFuncs[i] << "'!\n";
140 return 1;
141 }
Chad Rosierf0d37862011-09-16 21:09:17 +0000142 GVs.insert(GV);
143 }
144 // Extract functions via regular expression matching.
145 for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
146 std::string Error;
147 StringRef RegExStr = ExtractRegExpFuncs[i];
148 Regex RegEx(RegExStr);
149 if (!RegEx.isValid(Error)) {
150 errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
151 "invalid regex: " << Error;
152 }
153 bool match = false;
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000154 for (Module::iterator F = M->begin(), E = M->end(); F != E;
Chad Rosierf0d37862011-09-16 21:09:17 +0000155 F++) {
156 if (RegEx.match(F->getName())) {
157 GVs.insert(&*F);
158 match = true;
159 }
160 }
161 if (!match) {
162 errs() << argv[0] << ": program doesn't contain global named '"
163 << ExtractRegExpFuncs[i] << "'!\n";
164 return 1;
165 }
Chris Lattner27936992007-05-06 05:13:17 +0000166 }
167
Dan Gohmanbf154592010-08-25 23:33:07 +0000168 // Materialize requisite global values.
Dan Gohman0d2c07c2010-09-23 00:33:13 +0000169 if (!DeleteFn)
170 for (size_t i = 0, e = GVs.size(); i != e; ++i) {
171 GlobalValue *GV = GVs[i];
172 if (GV->isMaterializable()) {
173 std::string ErrInfo;
174 if (GV->Materialize(&ErrInfo)) {
175 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
176 return 1;
177 }
178 }
179 }
180 else {
181 // Deleting. Materialize every GV that's *not* in GVs.
182 SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
183 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
184 I != E; ++I) {
185 GlobalVariable *G = I;
186 if (!GVSet.count(G) && G->isMaterializable()) {
187 std::string ErrInfo;
188 if (G->Materialize(&ErrInfo)) {
189 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
190 return 1;
191 }
192 }
193 }
194 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
195 Function *F = I;
196 if (!GVSet.count(F) && F->isMaterializable()) {
197 std::string ErrInfo;
198 if (F->Materialize(&ErrInfo)) {
199 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
200 return 1;
201 }
Dan Gohmanbf154592010-08-25 23:33:07 +0000202 }
203 }
204 }
205
Chris Lattner27936992007-05-06 05:13:17 +0000206 // In addition to deleting all other functions, we also want to spiff it
207 // up a little bit. Do this now.
208 PassManager Passes;
209 Passes.add(new TargetData(M.get())); // Use correct TargetData
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000210
Chad Rosierf0d37862011-09-16 21:09:17 +0000211 std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
212
213 Passes.add(createGVExtractionPass(Gvs, DeleteFn));
Chris Lattner27936992007-05-06 05:13:17 +0000214 if (!DeleteFn)
215 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Devang Patel9b2a93a2010-07-01 19:58:05 +0000216 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
Chris Lattner27936992007-05-06 05:13:17 +0000217 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
218
Chris Lattnerabd17362009-08-23 02:56:05 +0000219 std::string ErrorInfo;
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000220 tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
221 raw_fd_ostream::F_Binary);
Chris Lattnerabd17362009-08-23 02:56:05 +0000222 if (!ErrorInfo.empty()) {
223 errs() << ErrorInfo << '\n';
Chris Lattnerabd17362009-08-23 02:56:05 +0000224 return 1;
Chris Lattner27936992007-05-06 05:13:17 +0000225 }
226
Dan Gohmane5929232009-09-11 20:46:33 +0000227 if (OutputAssembly)
Dan Gohmana2233f22010-09-01 14:20:41 +0000228 Passes.add(createPrintModulePass(&Out.os()));
229 else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
230 Passes.add(createBitcodeWriterPass(Out.os()));
Dan Gohman61a87962009-08-25 15:34:52 +0000231
Chris Lattner27936992007-05-06 05:13:17 +0000232 Passes.run(*M.get());
233
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000234 // Declare success.
235 Out.keep();
236
Chris Lattner27936992007-05-06 05:13:17 +0000237 return 0;
Chris Lattner055de182002-05-22 20:27:00 +0000238}