blob: 443ee653c8e72811404ef090fafaf2844a5c7685 [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"
Duncan P. N. Exon Smithc55dee12015-04-14 18:33:00 +000022#include "llvm/IR/UseListOrder.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000023#include "llvm/IRReader/IRReader.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000024#include "llvm/IR/LegacyPassManager.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000026#include "llvm/Support/FileSystem.h"
Chris Lattner76d46322006-12-06 01:18:01 +000027#include "llvm/Support/ManagedStatic.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000028#include "llvm/Support/PrettyStackTrace.h"
Chad Rosierf0d37862011-09-16 21:09:17 +000029#include "llvm/Support/Regex.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000030#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000031#include "llvm/Support/SourceMgr.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000032#include "llvm/Support/SystemUtils.h"
33#include "llvm/Support/ToolOutputFile.h"
34#include "llvm/Transforms/IPO.h"
Chris Lattner055de182002-05-22 20:27:00 +000035#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000036using namespace llvm;
37
Chris Lattnerf5cad152002-07-22 02:10:13 +000038// InputFilename - The filename to read from.
Chris Lattner64a67272002-07-25 16:31:09 +000039static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000040InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000041 cl::init("-"), cl::value_desc("filename"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000042
Chris Lattner3b203f52004-02-18 16:53:59 +000043static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000044OutputFilename("o", cl::desc("Specify output filename"),
Chris Lattner3b203f52004-02-18 16:53:59 +000045 cl::value_desc("filename"), cl::init("-"));
46
47static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000048Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000049
Misha Brukman2ccac822004-04-22 23:07:39 +000050static cl::opt<bool>
Andrew Lenharth3f13b662008-03-07 19:51:57 +000051DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
Misha Brukman2ccac822004-04-22 23:07:39 +000052
Chad Rosierf0d37862011-09-16 21:09:17 +000053// ExtractFuncs - The functions to extract from the module.
Dan Gohmanf684e452010-02-10 23:58:53 +000054static cl::list<std::string>
55ExtractFuncs("func", cl::desc("Specify function to extract"),
56 cl::ZeroOrMore, cl::value_desc("function"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000057
Andrew Trickdc073ad2013-09-18 23:31:10 +000058// ExtractRegExpFuncs - The functions, matched via regular expression, to
Chad Rosierf0d37862011-09-16 21:09:17 +000059// extract from the module.
60static cl::list<std::string>
61ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
62 "regular expression"),
63 cl::ZeroOrMore, cl::value_desc("rfunction"));
64
Rafael Espindola7043858a2012-10-29 02:23:07 +000065// ExtractAlias - The alias to extract from the module.
66static cl::list<std::string>
67ExtractAliases("alias", cl::desc("Specify alias to extract"),
68 cl::ZeroOrMore, cl::value_desc("alias"));
69
70
71// ExtractRegExpAliases - The aliases, matched via regular expression, to
72// extract from the module.
73static cl::list<std::string>
74ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a "
75 "regular expression"),
76 cl::ZeroOrMore, cl::value_desc("ralias"));
77
Chad Rosierf0d37862011-09-16 21:09:17 +000078// ExtractGlobals - The globals to extract from the module.
Dan Gohmanf684e452010-02-10 23:58:53 +000079static cl::list<std::string>
80ExtractGlobals("glob", cl::desc("Specify global to extract"),
81 cl::ZeroOrMore, cl::value_desc("global"));
Andrew Lenharth3f13b662008-03-07 19:51:57 +000082
Chad Rosierf0d37862011-09-16 21:09:17 +000083// ExtractRegExpGlobals - The globals, matched via regular expression, to
84// extract from the module...
85static cl::list<std::string>
86ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
87 "regular expression"),
88 cl::ZeroOrMore, cl::value_desc("rglobal"));
89
Dan Gohmane5929232009-09-11 20:46:33 +000090static cl::opt<bool>
91OutputAssembly("S",
92 cl::desc("Write output as LLVM assembly"), cl::Hidden);
93
Chris Lattner055de182002-05-22 20:27:00 +000094int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +000095 // Print a stack trace if we signal out.
Chris Lattner27936992007-05-06 05:13:17 +000096 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000097 PrettyStackTraceProgram X(argc, argv);
Owen Anderson6773d382009-07-01 16:58:40 +000098
Owen Anderson19251ec2009-07-15 22:16:10 +000099 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000100 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Duncan P. N. Exon Smithc55dee12015-04-14 18:33:00 +0000101
102 // Turn on -preserve-bc-uselistorder by default, but let the command-line
103 // override it.
104 setPreserveBitcodeUseListOrder(true);
105
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000106 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
Chris Lattner055de182002-05-22 20:27:00 +0000107
Dan Gohmanbf154592010-08-25 23:33:07 +0000108 // Use lazy loading, since we only care about selected global values.
Dan Gohmane5929232009-09-11 20:46:33 +0000109 SMDiagnostic Err;
Rafael Espindolad233b062014-08-26 17:29:46 +0000110 std::unique_ptr<Module> M = getLazyIRFileModule(InputFilename, Err, Context);
Dan Gohmane5929232009-09-11 20:46:33 +0000111
Craig Toppere6cb63e2014-04-25 04:24:47 +0000112 if (!M.get()) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000113 Err.print(argv[0], errs());
Chris Lattner27936992007-05-06 05:13:17 +0000114 return 1;
115 }
116
Chad Rosierf0d37862011-09-16 21:09:17 +0000117 // Use SetVector to avoid duplicates.
118 SetVector<GlobalValue *> GVs;
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000119
Rafael Espindola7043858a2012-10-29 02:23:07 +0000120 // Figure out which aliases we should extract.
121 for (size_t i = 0, e = ExtractAliases.size(); i != e; ++i) {
122 GlobalAlias *GA = M->getNamedAlias(ExtractAliases[i]);
123 if (!GA) {
124 errs() << argv[0] << ": program doesn't contain alias named '"
125 << ExtractAliases[i] << "'!\n";
126 return 1;
127 }
128 GVs.insert(GA);
129 }
130
131 // Extract aliases via regular expression matching.
132 for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) {
133 std::string Error;
134 Regex RegEx(ExtractRegExpAliases[i]);
135 if (!RegEx.isValid(Error)) {
136 errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' "
137 "invalid regex: " << Error;
138 }
139 bool match = false;
140 for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end();
141 GA != E; GA++) {
142 if (RegEx.match(GA->getName())) {
143 GVs.insert(&*GA);
144 match = true;
145 }
146 }
147 if (!match) {
148 errs() << argv[0] << ": program doesn't contain global named '"
149 << ExtractRegExpAliases[i] << "'!\n";
150 return 1;
151 }
152 }
153
Dan Gohmanf684e452010-02-10 23:58:53 +0000154 // Figure out which globals we should extract.
155 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000156 GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
Dan Gohmanf684e452010-02-10 23:58:53 +0000157 if (!GV) {
158 errs() << argv[0] << ": program doesn't contain global named '"
159 << ExtractGlobals[i] << "'!\n";
160 return 1;
161 }
Chad Rosierf0d37862011-09-16 21:09:17 +0000162 GVs.insert(GV);
163 }
164
165 // Extract globals via regular expression matching.
166 for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
167 std::string Error;
168 Regex RegEx(ExtractRegExpGlobals[i]);
169 if (!RegEx.isValid(Error)) {
170 errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
171 "invalid regex: " << Error;
172 }
173 bool match = false;
Rafael Espindolabfe44e12014-05-08 19:30:17 +0000174 for (auto &GV : M->globals()) {
175 if (RegEx.match(GV.getName())) {
176 GVs.insert(&GV);
Chad Rosierf0d37862011-09-16 21:09:17 +0000177 match = true;
178 }
179 }
180 if (!match) {
181 errs() << argv[0] << ": program doesn't contain global named '"
182 << ExtractRegExpGlobals[i] << "'!\n";
183 return 1;
184 }
Dan Gohmanf684e452010-02-10 23:58:53 +0000185 }
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000186
Dan Gohmanf684e452010-02-10 23:58:53 +0000187 // Figure out which functions we should extract.
188 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000189 GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
Dan Gohmanf684e452010-02-10 23:58:53 +0000190 if (!GV) {
191 errs() << argv[0] << ": program doesn't contain function named '"
192 << ExtractFuncs[i] << "'!\n";
193 return 1;
194 }
Chad Rosierf0d37862011-09-16 21:09:17 +0000195 GVs.insert(GV);
196 }
197 // Extract functions via regular expression matching.
198 for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
199 std::string Error;
200 StringRef RegExStr = ExtractRegExpFuncs[i];
201 Regex RegEx(RegExStr);
202 if (!RegEx.isValid(Error)) {
203 errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
204 "invalid regex: " << Error;
205 }
206 bool match = false;
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000207 for (Module::iterator F = M->begin(), E = M->end(); F != E;
Chad Rosierf0d37862011-09-16 21:09:17 +0000208 F++) {
209 if (RegEx.match(F->getName())) {
210 GVs.insert(&*F);
211 match = true;
212 }
213 }
214 if (!match) {
215 errs() << argv[0] << ": program doesn't contain global named '"
216 << ExtractRegExpFuncs[i] << "'!\n";
217 return 1;
218 }
Chris Lattner27936992007-05-06 05:13:17 +0000219 }
220
Dan Gohmanbf154592010-08-25 23:33:07 +0000221 // Materialize requisite global values.
Dan Gohman0d2c07c2010-09-23 00:33:13 +0000222 if (!DeleteFn)
223 for (size_t i = 0, e = GVs.size(); i != e; ++i) {
224 GlobalValue *GV = GVs[i];
Rafael Espindola246c4fb2014-11-01 16:46:18 +0000225 if (std::error_code EC = GV->materialize()) {
226 errs() << argv[0] << ": error reading input: " << EC.message() << "\n";
227 return 1;
Dan Gohman0d2c07c2010-09-23 00:33:13 +0000228 }
229 }
230 else {
231 // Deleting. Materialize every GV that's *not* in GVs.
232 SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
Rafael Espindolabfe44e12014-05-08 19:30:17 +0000233 for (auto &G : M->globals()) {
Rafael Espindola246c4fb2014-11-01 16:46:18 +0000234 if (!GVSet.count(&G)) {
Rafael Espindola5a52e6d2014-10-24 22:50:48 +0000235 if (std::error_code EC = G.materialize()) {
236 errs() << argv[0] << ": error reading input: " << EC.message()
237 << "\n";
Dan Gohman0d2c07c2010-09-23 00:33:13 +0000238 return 1;
239 }
240 }
241 }
Rafael Espindolabfe44e12014-05-08 19:30:17 +0000242 for (auto &F : *M) {
Rafael Espindola246c4fb2014-11-01 16:46:18 +0000243 if (!GVSet.count(&F)) {
Rafael Espindola5a52e6d2014-10-24 22:50:48 +0000244 if (std::error_code EC = F.materialize()) {
245 errs() << argv[0] << ": error reading input: " << EC.message()
246 << "\n";
Dan Gohman0d2c07c2010-09-23 00:33:13 +0000247 return 1;
248 }
Dan Gohmanbf154592010-08-25 23:33:07 +0000249 }
250 }
251 }
252
Chris Lattner27936992007-05-06 05:13:17 +0000253 // In addition to deleting all other functions, we also want to spiff it
254 // up a little bit. Do this now.
Chandler Carruth30d69c22015-02-13 10:01:29 +0000255 legacy::PassManager Passes;
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000256
Chad Rosierf0d37862011-09-16 21:09:17 +0000257 std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
258
259 Passes.add(createGVExtractionPass(Gvs, DeleteFn));
Chris Lattner27936992007-05-06 05:13:17 +0000260 if (!DeleteFn)
261 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Devang Patel9b2a93a2010-07-01 19:58:05 +0000262 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
Chris Lattner27936992007-05-06 05:13:17 +0000263 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
264
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000265 std::error_code EC;
266 tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
267 if (EC) {
268 errs() << EC.message() << '\n';
Chris Lattnerabd17362009-08-23 02:56:05 +0000269 return 1;
Chris Lattner27936992007-05-06 05:13:17 +0000270 }
271
Dan Gohmane5929232009-09-11 20:46:33 +0000272 if (OutputAssembly)
Chandler Carruth9d805132014-01-12 11:30:46 +0000273 Passes.add(createPrintModulePass(Out.os()));
Dan Gohmana2233f22010-09-01 14:20:41 +0000274 else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
275 Passes.add(createBitcodeWriterPass(Out.os()));
Dan Gohman61a87962009-08-25 15:34:52 +0000276
Chris Lattner27936992007-05-06 05:13:17 +0000277 Passes.run(*M.get());
278
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000279 // Declare success.
280 Out.keep();
281
Chris Lattner27936992007-05-06 05:13:17 +0000282 return 0;
Chris Lattner055de182002-05-22 20:27:00 +0000283}