blob: 769f82a057dc45369556589e6de797b10b2471b8 [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
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000015#include "llvm/ADT/SetVector.h"
16#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthb7bdfd62014-01-13 07:38:24 +000017#include "llvm/Bitcode/BitcodeWriterPass.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000019#include "llvm/IR/IRPrintingPasses.h"
20#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Module.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000022#include "llvm/IRReader/IRReader.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000023#include "llvm/PassManager.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000025#include "llvm/Support/FileSystem.h"
Chris Lattner76d46322006-12-06 01:18:01 +000026#include "llvm/Support/ManagedStatic.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000027#include "llvm/Support/PrettyStackTrace.h"
Chad Rosierf0d37862011-09-16 21:09:17 +000028#include "llvm/Support/Regex.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000029#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000030#include "llvm/Support/SourceMgr.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include "llvm/Support/SystemUtils.h"
32#include "llvm/Support/ToolOutputFile.h"
33#include "llvm/Transforms/IPO.h"
Chris Lattner055de182002-05-22 20:27:00 +000034#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000035using namespace llvm;
36
Chris Lattnerf5cad152002-07-22 02:10:13 +000037// InputFilename - The filename to read from.
Chris Lattner64a67272002-07-25 16:31:09 +000038static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000039InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000040 cl::init("-"), cl::value_desc("filename"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000041
Chris Lattner3b203f52004-02-18 16:53:59 +000042static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000043OutputFilename("o", cl::desc("Specify output filename"),
Chris Lattner3b203f52004-02-18 16:53:59 +000044 cl::value_desc("filename"), cl::init("-"));
45
46static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000047Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000048
Misha Brukman2ccac822004-04-22 23:07:39 +000049static cl::opt<bool>
Andrew Lenharth3f13b662008-03-07 19:51:57 +000050DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
Misha Brukman2ccac822004-04-22 23:07:39 +000051
Chad Rosierf0d37862011-09-16 21:09:17 +000052// ExtractFuncs - The functions to extract from the module.
Dan Gohmanf684e452010-02-10 23:58:53 +000053static cl::list<std::string>
54ExtractFuncs("func", cl::desc("Specify function to extract"),
55 cl::ZeroOrMore, cl::value_desc("function"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000056
Andrew Trickdc073ad2013-09-18 23:31:10 +000057// ExtractRegExpFuncs - The functions, matched via regular expression, to
Chad Rosierf0d37862011-09-16 21:09:17 +000058// extract from the module.
59static cl::list<std::string>
60ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
61 "regular expression"),
62 cl::ZeroOrMore, cl::value_desc("rfunction"));
63
Rafael Espindola7043858a2012-10-29 02:23:07 +000064// ExtractAlias - The alias to extract from the module.
65static cl::list<std::string>
66ExtractAliases("alias", cl::desc("Specify alias to extract"),
67 cl::ZeroOrMore, cl::value_desc("alias"));
68
69
70// ExtractRegExpAliases - The aliases, matched via regular expression, to
71// extract from the module.
72static cl::list<std::string>
73ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a "
74 "regular expression"),
75 cl::ZeroOrMore, cl::value_desc("ralias"));
76
Chad Rosierf0d37862011-09-16 21:09:17 +000077// ExtractGlobals - The globals to extract from the module.
Dan Gohmanf684e452010-02-10 23:58:53 +000078static cl::list<std::string>
79ExtractGlobals("glob", cl::desc("Specify global to extract"),
80 cl::ZeroOrMore, cl::value_desc("global"));
Andrew Lenharth3f13b662008-03-07 19:51:57 +000081
Chad Rosierf0d37862011-09-16 21:09:17 +000082// ExtractRegExpGlobals - The globals, matched via regular expression, to
83// extract from the module...
84static cl::list<std::string>
85ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
86 "regular expression"),
87 cl::ZeroOrMore, cl::value_desc("rglobal"));
88
Dan Gohmane5929232009-09-11 20:46:33 +000089static cl::opt<bool>
90OutputAssembly("S",
91 cl::desc("Write output as LLVM assembly"), cl::Hidden);
92
Chris Lattner055de182002-05-22 20:27:00 +000093int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +000094 // Print a stack trace if we signal out.
Chris Lattner27936992007-05-06 05:13:17 +000095 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000096 PrettyStackTraceProgram X(argc, argv);
Owen Anderson6773d382009-07-01 16:58:40 +000097
Owen Anderson19251ec2009-07-15 22:16:10 +000098 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000099 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
100 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
Chris Lattner055de182002-05-22 20:27:00 +0000101
Dan Gohmanbf154592010-08-25 23:33:07 +0000102 // Use lazy loading, since we only care about selected global values.
Dan Gohmane5929232009-09-11 20:46:33 +0000103 SMDiagnostic Err;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000104 std::unique_ptr<Module> M;
Dan Gohmanbf154592010-08-25 23:33:07 +0000105 M.reset(getLazyIRFileModule(InputFilename, Err, Context));
Dan Gohmane5929232009-09-11 20:46:33 +0000106
Craig Toppere6cb63e2014-04-25 04:24:47 +0000107 if (!M.get()) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000108 Err.print(argv[0], errs());
Chris Lattner27936992007-05-06 05:13:17 +0000109 return 1;
110 }
111
Chad Rosierf0d37862011-09-16 21:09:17 +0000112 // Use SetVector to avoid duplicates.
113 SetVector<GlobalValue *> GVs;
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000114
Rafael Espindola7043858a2012-10-29 02:23:07 +0000115 // Figure out which aliases we should extract.
116 for (size_t i = 0, e = ExtractAliases.size(); i != e; ++i) {
117 GlobalAlias *GA = M->getNamedAlias(ExtractAliases[i]);
118 if (!GA) {
119 errs() << argv[0] << ": program doesn't contain alias named '"
120 << ExtractAliases[i] << "'!\n";
121 return 1;
122 }
123 GVs.insert(GA);
124 }
125
126 // Extract aliases via regular expression matching.
127 for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) {
128 std::string Error;
129 Regex RegEx(ExtractRegExpAliases[i]);
130 if (!RegEx.isValid(Error)) {
131 errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' "
132 "invalid regex: " << Error;
133 }
134 bool match = false;
135 for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end();
136 GA != E; GA++) {
137 if (RegEx.match(GA->getName())) {
138 GVs.insert(&*GA);
139 match = true;
140 }
141 }
142 if (!match) {
143 errs() << argv[0] << ": program doesn't contain global named '"
144 << ExtractRegExpAliases[i] << "'!\n";
145 return 1;
146 }
147 }
148
Dan Gohmanf684e452010-02-10 23:58:53 +0000149 // Figure out which globals we should extract.
150 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000151 GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
Dan Gohmanf684e452010-02-10 23:58:53 +0000152 if (!GV) {
153 errs() << argv[0] << ": program doesn't contain global named '"
154 << ExtractGlobals[i] << "'!\n";
155 return 1;
156 }
Chad Rosierf0d37862011-09-16 21:09:17 +0000157 GVs.insert(GV);
158 }
159
160 // Extract globals via regular expression matching.
161 for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
162 std::string Error;
163 Regex RegEx(ExtractRegExpGlobals[i]);
164 if (!RegEx.isValid(Error)) {
165 errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
166 "invalid regex: " << Error;
167 }
168 bool match = false;
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000169 for (Module::global_iterator GV = M->global_begin(),
170 E = M->global_end(); GV != E; GV++) {
Chad Rosierf0d37862011-09-16 21:09:17 +0000171 if (RegEx.match(GV->getName())) {
172 GVs.insert(&*GV);
173 match = true;
174 }
175 }
176 if (!match) {
177 errs() << argv[0] << ": program doesn't contain global named '"
178 << ExtractRegExpGlobals[i] << "'!\n";
179 return 1;
180 }
Dan Gohmanf684e452010-02-10 23:58:53 +0000181 }
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000182
Dan Gohmanf684e452010-02-10 23:58:53 +0000183 // Figure out which functions we should extract.
184 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000185 GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
Dan Gohmanf684e452010-02-10 23:58:53 +0000186 if (!GV) {
187 errs() << argv[0] << ": program doesn't contain function named '"
188 << ExtractFuncs[i] << "'!\n";
189 return 1;
190 }
Chad Rosierf0d37862011-09-16 21:09:17 +0000191 GVs.insert(GV);
192 }
193 // Extract functions via regular expression matching.
194 for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
195 std::string Error;
196 StringRef RegExStr = ExtractRegExpFuncs[i];
197 Regex RegEx(RegExStr);
198 if (!RegEx.isValid(Error)) {
199 errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
200 "invalid regex: " << Error;
201 }
202 bool match = false;
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000203 for (Module::iterator F = M->begin(), E = M->end(); F != E;
Chad Rosierf0d37862011-09-16 21:09:17 +0000204 F++) {
205 if (RegEx.match(F->getName())) {
206 GVs.insert(&*F);
207 match = true;
208 }
209 }
210 if (!match) {
211 errs() << argv[0] << ": program doesn't contain global named '"
212 << ExtractRegExpFuncs[i] << "'!\n";
213 return 1;
214 }
Chris Lattner27936992007-05-06 05:13:17 +0000215 }
216
Dan Gohmanbf154592010-08-25 23:33:07 +0000217 // Materialize requisite global values.
Dan Gohman0d2c07c2010-09-23 00:33:13 +0000218 if (!DeleteFn)
219 for (size_t i = 0, e = GVs.size(); i != e; ++i) {
220 GlobalValue *GV = GVs[i];
221 if (GV->isMaterializable()) {
222 std::string ErrInfo;
223 if (GV->Materialize(&ErrInfo)) {
224 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
225 return 1;
226 }
227 }
228 }
229 else {
230 // Deleting. Materialize every GV that's *not* in GVs.
231 SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
232 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
233 I != E; ++I) {
234 GlobalVariable *G = I;
235 if (!GVSet.count(G) && G->isMaterializable()) {
236 std::string ErrInfo;
237 if (G->Materialize(&ErrInfo)) {
238 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
239 return 1;
240 }
241 }
242 }
243 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
244 Function *F = I;
245 if (!GVSet.count(F) && F->isMaterializable()) {
246 std::string ErrInfo;
247 if (F->Materialize(&ErrInfo)) {
248 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
249 return 1;
250 }
Dan Gohmanbf154592010-08-25 23:33:07 +0000251 }
252 }
253 }
254
Chris Lattner27936992007-05-06 05:13:17 +0000255 // In addition to deleting all other functions, we also want to spiff it
256 // up a little bit. Do this now.
257 PassManager Passes;
Rafael Espindola93512512014-02-25 17:30:31 +0000258 Passes.add(new DataLayoutPass(M.get())); // Use correct DataLayout
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000259
Chad Rosierf0d37862011-09-16 21:09:17 +0000260 std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
261
262 Passes.add(createGVExtractionPass(Gvs, DeleteFn));
Chris Lattner27936992007-05-06 05:13:17 +0000263 if (!DeleteFn)
264 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Devang Patel9b2a93a2010-07-01 19:58:05 +0000265 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
Chris Lattner27936992007-05-06 05:13:17 +0000266 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
267
Chris Lattnerabd17362009-08-23 02:56:05 +0000268 std::string ErrorInfo;
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000269 tool_output_file Out(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None);
Chris Lattnerabd17362009-08-23 02:56:05 +0000270 if (!ErrorInfo.empty()) {
271 errs() << ErrorInfo << '\n';
Chris Lattnerabd17362009-08-23 02:56:05 +0000272 return 1;
Chris Lattner27936992007-05-06 05:13:17 +0000273 }
274
Dan Gohmane5929232009-09-11 20:46:33 +0000275 if (OutputAssembly)
Chandler Carruth9d805132014-01-12 11:30:46 +0000276 Passes.add(createPrintModulePass(Out.os()));
Dan Gohmana2233f22010-09-01 14:20:41 +0000277 else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
278 Passes.add(createBitcodeWriterPass(Out.os()));
Dan Gohman61a87962009-08-25 15:34:52 +0000279
Chris Lattner27936992007-05-06 05:13:17 +0000280 Passes.run(*M.get());
281
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000282 // Declare success.
283 Out.keep();
284
Chris Lattner27936992007-05-06 05:13:17 +0000285 return 0;
Chris Lattner055de182002-05-22 20:27:00 +0000286}