blob: 4131177bd401f06d2b787e9280b2185b9794c577 [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"
Chris Lattner76d46322006-12-06 01:18:01 +000025#include "llvm/Support/ManagedStatic.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000026#include "llvm/Support/PrettyStackTrace.h"
Chad Rosierf0d37862011-09-16 21:09:17 +000027#include "llvm/Support/Regex.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000028#include "llvm/Support/Signals.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000029#include "llvm/Support/SourceMgr.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000030#include "llvm/Support/SystemUtils.h"
31#include "llvm/Support/ToolOutputFile.h"
32#include "llvm/Transforms/IPO.h"
Chris Lattner055de182002-05-22 20:27:00 +000033#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000034using namespace llvm;
35
Chris Lattnerf5cad152002-07-22 02:10:13 +000036// InputFilename - The filename to read from.
Chris Lattner64a67272002-07-25 16:31:09 +000037static cl::opt<std::string>
Gabor Greife16561c2007-07-05 17:07:56 +000038InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Chris Lattnerf5cad152002-07-22 02:10:13 +000039 cl::init("-"), cl::value_desc("filename"));
Misha Brukman650ba8e2005-04-22 00:00:37 +000040
Chris Lattner3b203f52004-02-18 16:53:59 +000041static cl::opt<std::string>
Misha Brukman650ba8e2005-04-22 00:00:37 +000042OutputFilename("o", cl::desc("Specify output filename"),
Chris Lattner3b203f52004-02-18 16:53:59 +000043 cl::value_desc("filename"), cl::init("-"));
44
45static cl::opt<bool>
Dan Gohman61a87962009-08-25 15:34:52 +000046Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000047
Misha Brukman2ccac822004-04-22 23:07:39 +000048static cl::opt<bool>
Andrew Lenharth3f13b662008-03-07 19:51:57 +000049DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
Misha Brukman2ccac822004-04-22 23:07:39 +000050
Chad Rosierf0d37862011-09-16 21:09:17 +000051// ExtractFuncs - The functions to extract from the module.
Dan Gohmanf684e452010-02-10 23:58:53 +000052static cl::list<std::string>
53ExtractFuncs("func", cl::desc("Specify function to extract"),
54 cl::ZeroOrMore, cl::value_desc("function"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000055
Andrew Trickdc073ad2013-09-18 23:31:10 +000056// ExtractRegExpFuncs - The functions, matched via regular expression, to
Chad Rosierf0d37862011-09-16 21:09:17 +000057// extract from the module.
58static cl::list<std::string>
59ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
60 "regular expression"),
61 cl::ZeroOrMore, cl::value_desc("rfunction"));
62
Rafael Espindola7043858a2012-10-29 02:23:07 +000063// ExtractAlias - The alias to extract from the module.
64static cl::list<std::string>
65ExtractAliases("alias", cl::desc("Specify alias to extract"),
66 cl::ZeroOrMore, cl::value_desc("alias"));
67
68
69// ExtractRegExpAliases - The aliases, matched via regular expression, to
70// extract from the module.
71static cl::list<std::string>
72ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a "
73 "regular expression"),
74 cl::ZeroOrMore, cl::value_desc("ralias"));
75
Chad Rosierf0d37862011-09-16 21:09:17 +000076// ExtractGlobals - The globals to extract from the module.
Dan Gohmanf684e452010-02-10 23:58:53 +000077static cl::list<std::string>
78ExtractGlobals("glob", cl::desc("Specify global to extract"),
79 cl::ZeroOrMore, cl::value_desc("global"));
Andrew Lenharth3f13b662008-03-07 19:51:57 +000080
Chad Rosierf0d37862011-09-16 21:09:17 +000081// ExtractRegExpGlobals - The globals, matched via regular expression, to
82// extract from the module...
83static cl::list<std::string>
84ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
85 "regular expression"),
86 cl::ZeroOrMore, cl::value_desc("rglobal"));
87
Dan Gohmane5929232009-09-11 20:46:33 +000088static cl::opt<bool>
89OutputAssembly("S",
90 cl::desc("Write output as LLVM assembly"), cl::Hidden);
91
Chris Lattner055de182002-05-22 20:27:00 +000092int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +000093 // Print a stack trace if we signal out.
Chris Lattner27936992007-05-06 05:13:17 +000094 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000095 PrettyStackTraceProgram X(argc, argv);
Owen Anderson6773d382009-07-01 16:58:40 +000096
Owen Anderson19251ec2009-07-15 22:16:10 +000097 LLVMContext &Context = getGlobalContext();
Chris Lattnere3fc2d12009-03-06 05:34:10 +000098 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
99 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
Chris Lattner055de182002-05-22 20:27:00 +0000100
Dan Gohmanbf154592010-08-25 23:33:07 +0000101 // Use lazy loading, since we only care about selected global values.
Dan Gohmane5929232009-09-11 20:46:33 +0000102 SMDiagnostic Err;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000103 std::unique_ptr<Module> M;
Dan Gohmanbf154592010-08-25 23:33:07 +0000104 M.reset(getLazyIRFileModule(InputFilename, Err, Context));
Dan Gohmane5929232009-09-11 20:46:33 +0000105
Craig Toppere6cb63e2014-04-25 04:24:47 +0000106 if (!M.get()) {
Chris Lattnera3a06812011-10-16 04:47:35 +0000107 Err.print(argv[0], errs());
Chris Lattner27936992007-05-06 05:13:17 +0000108 return 1;
109 }
110
Chad Rosierf0d37862011-09-16 21:09:17 +0000111 // Use SetVector to avoid duplicates.
112 SetVector<GlobalValue *> GVs;
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000113
Rafael Espindola7043858a2012-10-29 02:23:07 +0000114 // 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]);
117 if (!GA) {
118 errs() << argv[0] << ": program doesn't contain alias named '"
119 << ExtractAliases[i] << "'!\n";
120 return 1;
121 }
122 GVs.insert(GA);
123 }
124
125 // Extract aliases via regular expression matching.
126 for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) {
127 std::string Error;
128 Regex RegEx(ExtractRegExpAliases[i]);
129 if (!RegEx.isValid(Error)) {
130 errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' "
131 "invalid regex: " << Error;
132 }
133 bool match = false;
134 for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end();
135 GA != E; GA++) {
136 if (RegEx.match(GA->getName())) {
137 GVs.insert(&*GA);
138 match = true;
139 }
140 }
141 if (!match) {
142 errs() << argv[0] << ": program doesn't contain global named '"
143 << ExtractRegExpAliases[i] << "'!\n";
144 return 1;
145 }
146 }
147
Dan Gohmanf684e452010-02-10 23:58:53 +0000148 // Figure out which globals we should extract.
149 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000150 GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
Dan Gohmanf684e452010-02-10 23:58:53 +0000151 if (!GV) {
152 errs() << argv[0] << ": program doesn't contain global named '"
153 << ExtractGlobals[i] << "'!\n";
154 return 1;
155 }
Chad Rosierf0d37862011-09-16 21:09:17 +0000156 GVs.insert(GV);
157 }
158
159 // Extract globals via regular expression matching.
160 for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
161 std::string Error;
162 Regex RegEx(ExtractRegExpGlobals[i]);
163 if (!RegEx.isValid(Error)) {
164 errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
165 "invalid regex: " << Error;
166 }
167 bool match = false;
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000168 for (Module::global_iterator GV = M->global_begin(),
169 E = M->global_end(); GV != E; GV++) {
Chad Rosierf0d37862011-09-16 21:09:17 +0000170 if (RegEx.match(GV->getName())) {
171 GVs.insert(&*GV);
172 match = true;
173 }
174 }
175 if (!match) {
176 errs() << argv[0] << ": program doesn't contain global named '"
177 << ExtractRegExpGlobals[i] << "'!\n";
178 return 1;
179 }
Dan Gohmanf684e452010-02-10 23:58:53 +0000180 }
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000181
Dan Gohmanf684e452010-02-10 23:58:53 +0000182 // Figure out which functions we should extract.
183 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000184 GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
Dan Gohmanf684e452010-02-10 23:58:53 +0000185 if (!GV) {
186 errs() << argv[0] << ": program doesn't contain function named '"
187 << ExtractFuncs[i] << "'!\n";
188 return 1;
189 }
Chad Rosierf0d37862011-09-16 21:09:17 +0000190 GVs.insert(GV);
191 }
192 // Extract functions via regular expression matching.
193 for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
194 std::string Error;
195 StringRef RegExStr = ExtractRegExpFuncs[i];
196 Regex RegEx(RegExStr);
197 if (!RegEx.isValid(Error)) {
198 errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
199 "invalid regex: " << Error;
200 }
201 bool match = false;
Nick Lewycky5079e4d2011-12-30 19:17:23 +0000202 for (Module::iterator F = M->begin(), E = M->end(); F != E;
Chad Rosierf0d37862011-09-16 21:09:17 +0000203 F++) {
204 if (RegEx.match(F->getName())) {
205 GVs.insert(&*F);
206 match = true;
207 }
208 }
209 if (!match) {
210 errs() << argv[0] << ": program doesn't contain global named '"
211 << ExtractRegExpFuncs[i] << "'!\n";
212 return 1;
213 }
Chris Lattner27936992007-05-06 05:13:17 +0000214 }
215
Dan Gohmanbf154592010-08-25 23:33:07 +0000216 // Materialize requisite global values.
Dan Gohman0d2c07c2010-09-23 00:33:13 +0000217 if (!DeleteFn)
218 for (size_t i = 0, e = GVs.size(); i != e; ++i) {
219 GlobalValue *GV = GVs[i];
220 if (GV->isMaterializable()) {
221 std::string ErrInfo;
222 if (GV->Materialize(&ErrInfo)) {
223 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
224 return 1;
225 }
226 }
227 }
228 else {
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();
232 I != E; ++I) {
233 GlobalVariable *G = I;
234 if (!GVSet.count(G) && G->isMaterializable()) {
235 std::string ErrInfo;
236 if (G->Materialize(&ErrInfo)) {
237 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
238 return 1;
239 }
240 }
241 }
242 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
243 Function *F = I;
244 if (!GVSet.count(F) && F->isMaterializable()) {
245 std::string ErrInfo;
246 if (F->Materialize(&ErrInfo)) {
247 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
248 return 1;
249 }
Dan Gohmanbf154592010-08-25 23:33:07 +0000250 }
251 }
252 }
253
Chris Lattner27936992007-05-06 05:13:17 +0000254 // In addition to deleting all other functions, we also want to spiff it
255 // up a little bit. Do this now.
256 PassManager Passes;
Rafael Espindola93512512014-02-25 17:30:31 +0000257 Passes.add(new DataLayoutPass(M.get())); // Use correct DataLayout
Andrew Lenharth3f13b662008-03-07 19:51:57 +0000258
Chad Rosierf0d37862011-09-16 21:09:17 +0000259 std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
260
261 Passes.add(createGVExtractionPass(Gvs, DeleteFn));
Chris Lattner27936992007-05-06 05:13:17 +0000262 if (!DeleteFn)
263 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Devang Patel9b2a93a2010-07-01 19:58:05 +0000264 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
Chris Lattner27936992007-05-06 05:13:17 +0000265 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
266
Chris Lattnerabd17362009-08-23 02:56:05 +0000267 std::string ErrorInfo;
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000268 tool_output_file Out(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None);
Chris Lattnerabd17362009-08-23 02:56:05 +0000269 if (!ErrorInfo.empty()) {
270 errs() << ErrorInfo << '\n';
Chris Lattnerabd17362009-08-23 02:56:05 +0000271 return 1;
Chris Lattner27936992007-05-06 05:13:17 +0000272 }
273
Dan Gohmane5929232009-09-11 20:46:33 +0000274 if (OutputAssembly)
Chandler Carruth9d805132014-01-12 11:30:46 +0000275 Passes.add(createPrintModulePass(Out.os()));
Dan Gohmana2233f22010-09-01 14:20:41 +0000276 else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
277 Passes.add(createBitcodeWriterPass(Out.os()));
Dan Gohman61a87962009-08-25 15:34:52 +0000278
Chris Lattner27936992007-05-06 05:13:17 +0000279 Passes.run(*M.get());
280
Dan Gohman4cc73ba2010-08-20 01:12:13 +0000281 // Declare success.
282 Out.keep();
283
Chris Lattner27936992007-05-06 05:13:17 +0000284 return 0;
Chris Lattner055de182002-05-22 20:27:00 +0000285}