blob: 2ed11c52b2b3a35b978a4f152e02555e5d11f46d [file] [log] [blame]
Misha Brukmande03bc02005-04-24 17:36:05 +00001//===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner579d9142002-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 Anderson8b477ed2009-07-01 16:58:40 +000015#include "llvm/LLVMContext.h"
Chris Lattner579d9142002-05-22 20:27:00 +000016#include "llvm/Module.h"
17#include "llvm/PassManager.h"
Dan Gohmanec080462009-09-11 20:46:33 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnerc48e1db2007-05-06 05:13:17 +000019#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner33974ca2002-07-23 22:04:40 +000020#include "llvm/Transforms/IPO.h"
Brian Gaeke20408902003-10-28 22:22:16 +000021#include "llvm/Target/TargetData.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Dan Gohmanec080462009-09-11 20:46:33 +000023#include "llvm/Support/IRReader.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000024#include "llvm/Support/ManagedStatic.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000025#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmane4f1a9b2010-10-07 20:32:40 +000026#include "llvm/Support/ToolOutputFile.h"
Dan Gohmanbaa26392009-08-25 15:34:52 +000027#include "llvm/Support/SystemUtils.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000028#include "llvm/Support/Signals.h"
Chad Rosier4e0a55d2011-09-16 21:09:17 +000029#include "llvm/Support/Regex.h"
Dan Gohmanbe2d4e72010-09-23 00:33:13 +000030#include "llvm/ADT/SmallPtrSet.h"
Chad Rosier4e0a55d2011-09-16 21:09:17 +000031#include "llvm/ADT/SetVector.h"
Chris Lattner579d9142002-05-22 20:27:00 +000032#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000033using namespace llvm;
34
Chris Lattner5ff62e92002-07-22 02:10:13 +000035// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000036static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000037InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000038 cl::init("-"), cl::value_desc("filename"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000039
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000040static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000041OutputFilename("o", cl::desc("Specify output filename"),
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000042 cl::value_desc("filename"), cl::init("-"));
43
44static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000045Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000046
Misha Brukmanca718e42004-04-22 23:07:39 +000047static cl::opt<bool>
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000048DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
Misha Brukmanca718e42004-04-22 23:07:39 +000049
Chad Rosier4e0a55d2011-09-16 21:09:17 +000050// ExtractFuncs - The functions to extract from the module.
Dan Gohmana499d202010-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 Lattner5ff62e92002-07-22 02:10:13 +000054
Chad Rosier4e0a55d2011-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 Gohmana499d202010-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 Lenharthd245a8a2008-03-07 19:51:57 +000066
Chad Rosier4e0a55d2011-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 Gohmanec080462009-09-11 20:46:33 +000074static cl::opt<bool>
75OutputAssembly("S",
76 cl::desc("Write output as LLVM assembly"), cl::Hidden);
77
Chris Lattner579d9142002-05-22 20:27:00 +000078int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000079 // Print a stack trace if we signal out.
Chris Lattnerc48e1db2007-05-06 05:13:17 +000080 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +000081 PrettyStackTraceProgram X(argc, argv);
Owen Anderson8b477ed2009-07-01 16:58:40 +000082
Owen Anderson0d7c6952009-07-15 22:16:10 +000083 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +000084 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
85 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
Chris Lattner579d9142002-05-22 20:27:00 +000086
Dan Gohman44f95332010-08-25 23:33:07 +000087 // Use lazy loading, since we only care about selected global values.
Dan Gohmanec080462009-09-11 20:46:33 +000088 SMDiagnostic Err;
Chris Lattnerc48e1db2007-05-06 05:13:17 +000089 std::auto_ptr<Module> M;
Dan Gohman44f95332010-08-25 23:33:07 +000090 M.reset(getLazyIRFileModule(InputFilename, Err, Context));
Dan Gohmanec080462009-09-11 20:46:33 +000091
Chris Lattnerc48e1db2007-05-06 05:13:17 +000092 if (M.get() == 0) {
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000093 Err.print(argv[0], errs());
Chris Lattnerc48e1db2007-05-06 05:13:17 +000094 return 1;
95 }
96
Chad Rosier4e0a55d2011-09-16 21:09:17 +000097 // Use SetVector to avoid duplicates.
98 SetVector<GlobalValue *> GVs;
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000099
Dan Gohmana499d202010-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 Lewyckydb186c42011-12-30 19:17:23 +0000102 GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]);
Dan Gohmana499d202010-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 Rosier4e0a55d2011-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 Lewyckydb186c42011-12-30 19:17:23 +0000120 for (Module::global_iterator GV = M->global_begin(),
121 E = M->global_end(); GV != E; GV++) {
Chad Rosier4e0a55d2011-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 Gohmana499d202010-02-10 23:58:53 +0000132 }
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000133
Dan Gohmana499d202010-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 Lewyckydb186c42011-12-30 19:17:23 +0000136 GlobalValue *GV = M->getFunction(ExtractFuncs[i]);
Dan Gohmana499d202010-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 Rosier4e0a55d2011-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 Lewyckydb186c42011-12-30 19:17:23 +0000154 for (Module::iterator F = M->begin(), E = M->end(); F != E;
Chad Rosier4e0a55d2011-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 Lattnerc48e1db2007-05-06 05:13:17 +0000166 }
167
Dan Gohman44f95332010-08-25 23:33:07 +0000168 // Materialize requisite global values.
Dan Gohmanbe2d4e72010-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 Gohman44f95332010-08-25 23:33:07 +0000202 }
203 }
204 }
205
Chris Lattnerc48e1db2007-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 Lenharthd245a8a2008-03-07 19:51:57 +0000210
Chad Rosier4e0a55d2011-09-16 21:09:17 +0000211 std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
212
213 Passes.add(createGVExtractionPass(Gvs, DeleteFn));
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000214 if (!DeleteFn)
215 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Devang Patelc1874b72010-07-01 19:58:05 +0000216 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000217 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
218
Chris Lattner51a11322009-08-23 02:56:05 +0000219 std::string ErrorInfo;
Dan Gohman2df95042010-08-20 01:12:13 +0000220 tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
221 raw_fd_ostream::F_Binary);
Chris Lattner51a11322009-08-23 02:56:05 +0000222 if (!ErrorInfo.empty()) {
223 errs() << ErrorInfo << '\n';
Chris Lattner51a11322009-08-23 02:56:05 +0000224 return 1;
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000225 }
226
Dan Gohmanec080462009-09-11 20:46:33 +0000227 if (OutputAssembly)
Dan Gohmand4c45432010-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 Gohmanbaa26392009-08-25 15:34:52 +0000231
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000232 Passes.run(*M.get());
233
Dan Gohman2df95042010-08-20 01:12:13 +0000234 // Declare success.
235 Out.keep();
236
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000237 return 0;
Chris Lattner579d9142002-05-22 20:27:00 +0000238}