blob: 8c2f43a4f7d2a8b16c1e46da1091e66b32b60416 [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"
Dan Gohmanbe2d4e72010-09-23 00:33:13 +000029#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner579d9142002-05-22 20:27:00 +000030#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000031using namespace llvm;
32
Chris Lattner5ff62e92002-07-22 02:10:13 +000033// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000034static cl::opt<std::string>
Gabor Greifa99be512007-07-05 17:07:56 +000035InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000036 cl::init("-"), cl::value_desc("filename"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000037
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000038static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000039OutputFilename("o", cl::desc("Specify output filename"),
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000040 cl::value_desc("filename"), cl::init("-"));
41
42static cl::opt<bool>
Dan Gohmanbaa26392009-08-25 15:34:52 +000043Force("f", cl::desc("Enable binary output on terminals"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000044
Misha Brukmanca718e42004-04-22 23:07:39 +000045static cl::opt<bool>
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000046DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
Misha Brukmanca718e42004-04-22 23:07:39 +000047
Dan Gohmana499d202010-02-10 23:58:53 +000048// ExtractFuncs - The functions to extract from the module...
49static cl::list<std::string>
50ExtractFuncs("func", cl::desc("Specify function to extract"),
51 cl::ZeroOrMore, cl::value_desc("function"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000052
Dan Gohmana499d202010-02-10 23:58:53 +000053// ExtractGlobals - The globals to extract from the module...
54static cl::list<std::string>
55ExtractGlobals("glob", cl::desc("Specify global to extract"),
56 cl::ZeroOrMore, cl::value_desc("global"));
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000057
Dan Gohmanec080462009-09-11 20:46:33 +000058static cl::opt<bool>
59OutputAssembly("S",
60 cl::desc("Write output as LLVM assembly"), cl::Hidden);
61
Chris Lattner579d9142002-05-22 20:27:00 +000062int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000063 // Print a stack trace if we signal out.
Chris Lattnerc48e1db2007-05-06 05:13:17 +000064 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +000065 PrettyStackTraceProgram X(argc, argv);
Owen Anderson8b477ed2009-07-01 16:58:40 +000066
Owen Anderson0d7c6952009-07-15 22:16:10 +000067 LLVMContext &Context = getGlobalContext();
Chris Lattnercc14d252009-03-06 05:34:10 +000068 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
69 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
Chris Lattner579d9142002-05-22 20:27:00 +000070
Dan Gohman44f95332010-08-25 23:33:07 +000071 // Use lazy loading, since we only care about selected global values.
Dan Gohmanec080462009-09-11 20:46:33 +000072 SMDiagnostic Err;
Chris Lattnerc48e1db2007-05-06 05:13:17 +000073 std::auto_ptr<Module> M;
Dan Gohman44f95332010-08-25 23:33:07 +000074 M.reset(getLazyIRFileModule(InputFilename, Err, Context));
Dan Gohmanec080462009-09-11 20:46:33 +000075
Chris Lattnerc48e1db2007-05-06 05:13:17 +000076 if (M.get() == 0) {
Dan Gohmanec080462009-09-11 20:46:33 +000077 Err.Print(argv[0], errs());
Chris Lattnerc48e1db2007-05-06 05:13:17 +000078 return 1;
79 }
80
Dan Gohmana499d202010-02-10 23:58:53 +000081 std::vector<GlobalValue *> GVs;
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000082
Dan Gohmana499d202010-02-10 23:58:53 +000083 // Figure out which globals we should extract.
84 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
85 GlobalValue *GV = M.get()->getNamedGlobal(ExtractGlobals[i]);
86 if (!GV) {
87 errs() << argv[0] << ": program doesn't contain global named '"
88 << ExtractGlobals[i] << "'!\n";
89 return 1;
90 }
91 GVs.push_back(GV);
92 }
Andrew Lenharthd245a8a2008-03-07 19:51:57 +000093
Dan Gohmana499d202010-02-10 23:58:53 +000094 // Figure out which functions we should extract.
95 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
96 GlobalValue *GV = M.get()->getFunction(ExtractFuncs[i]);
97 if (!GV) {
98 errs() << argv[0] << ": program doesn't contain function named '"
99 << ExtractFuncs[i] << "'!\n";
100 return 1;
101 }
102 GVs.push_back(GV);
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000103 }
104
Dan Gohman44f95332010-08-25 23:33:07 +0000105 // Materialize requisite global values.
Dan Gohmanbe2d4e72010-09-23 00:33:13 +0000106 if (!DeleteFn)
107 for (size_t i = 0, e = GVs.size(); i != e; ++i) {
108 GlobalValue *GV = GVs[i];
109 if (GV->isMaterializable()) {
110 std::string ErrInfo;
111 if (GV->Materialize(&ErrInfo)) {
112 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
113 return 1;
114 }
115 }
116 }
117 else {
118 // Deleting. Materialize every GV that's *not* in GVs.
119 SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end());
120 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
121 I != E; ++I) {
122 GlobalVariable *G = I;
123 if (!GVSet.count(G) && G->isMaterializable()) {
124 std::string ErrInfo;
125 if (G->Materialize(&ErrInfo)) {
126 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
127 return 1;
128 }
129 }
130 }
131 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
132 Function *F = I;
133 if (!GVSet.count(F) && F->isMaterializable()) {
134 std::string ErrInfo;
135 if (F->Materialize(&ErrInfo)) {
136 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
137 return 1;
138 }
Dan Gohman44f95332010-08-25 23:33:07 +0000139 }
140 }
141 }
142
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000143 // In addition to deleting all other functions, we also want to spiff it
144 // up a little bit. Do this now.
145 PassManager Passes;
146 Passes.add(new TargetData(M.get())); // Use correct TargetData
Andrew Lenharthd245a8a2008-03-07 19:51:57 +0000147
Dan Gohmanb4e3cda2010-08-26 00:22:55 +0000148 Passes.add(createGVExtractionPass(GVs, DeleteFn));
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000149 if (!DeleteFn)
150 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Devang Patelc1874b72010-07-01 19:58:05 +0000151 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000152 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
153 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
154
Chris Lattner51a11322009-08-23 02:56:05 +0000155 std::string ErrorInfo;
Dan Gohman2df95042010-08-20 01:12:13 +0000156 tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
157 raw_fd_ostream::F_Binary);
Chris Lattner51a11322009-08-23 02:56:05 +0000158 if (!ErrorInfo.empty()) {
159 errs() << ErrorInfo << '\n';
Chris Lattner51a11322009-08-23 02:56:05 +0000160 return 1;
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000161 }
162
Dan Gohmanec080462009-09-11 20:46:33 +0000163 if (OutputAssembly)
Dan Gohmand4c45432010-09-01 14:20:41 +0000164 Passes.add(createPrintModulePass(&Out.os()));
165 else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
166 Passes.add(createBitcodeWriterPass(Out.os()));
Dan Gohmanbaa26392009-08-25 15:34:52 +0000167
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000168 Passes.run(*M.get());
169
Dan Gohman2df95042010-08-20 01:12:13 +0000170 // Declare success.
171 Out.keep();
172
Chris Lattnerc48e1db2007-05-06 05:13:17 +0000173 return 0;
Chris Lattner579d9142002-05-22 20:27:00 +0000174}