1 //===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This utility changes the input module to only contain a single function,
11 // which is primarily used for debugging transformations.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/LLVMContext.h"
16 #include "llvm/Module.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "llvm/Bitcode/ReaderWriter.h"
20 #include "llvm/Transforms/IPO.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/IRReader.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/SystemUtils.h"
28 #include "llvm/System/Signals.h"
32 // InputFilename - The filename to read from.
33 static cl::opt<std::string>
34 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
35 cl::init("-"), cl::value_desc("filename"));
37 static cl::opt<std::string>
38 OutputFilename("o", cl::desc("Specify output filename"),
39 cl::value_desc("filename"), cl::init("-"));
42 Force("f", cl::desc("Enable binary output on terminals"));
45 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
47 // ExtractFuncs - The functions to extract from the module...
48 static cl::list<std::string>
49 ExtractFuncs("func", cl::desc("Specify function to extract"),
50 cl::ZeroOrMore, cl::value_desc("function"));
52 // ExtractGlobals - The globals to extract from the module...
53 static cl::list<std::string>
54 ExtractGlobals("glob", cl::desc("Specify global to extract"),
55 cl::ZeroOrMore, cl::value_desc("global"));
59 cl::desc("Write output as LLVM assembly"), cl::Hidden);
61 int main(int argc, char **argv) {
62 // Print a stack trace if we signal out.
63 sys::PrintStackTraceOnErrorSignal();
64 PrettyStackTraceProgram X(argc, argv);
66 LLVMContext &Context = getGlobalContext();
67 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
68 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
70 // Use lazy loading, since we only care about selected global values.
72 std::auto_ptr<Module> M;
73 M.reset(getLazyIRFileModule(InputFilename, Err, Context));
76 Err.Print(argv[0], errs());
80 std::vector<GlobalValue *> GVs;
82 // Figure out which globals we should extract.
83 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
84 GlobalValue *GV = M.get()->getNamedGlobal(ExtractGlobals[i]);
86 errs() << argv[0] << ": program doesn't contain global named '"
87 << ExtractGlobals[i] << "'!\n";
93 // Figure out which functions we should extract.
94 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
95 GlobalValue *GV = M.get()->getFunction(ExtractFuncs[i]);
97 errs() << argv[0] << ": program doesn't contain function named '"
98 << ExtractFuncs[i] << "'!\n";
104 // Materialize requisite global values.
105 for (size_t i = 0, e = GVs.size(); i != e; ++i) {
106 GlobalValue *GV = GVs[i];
107 if (GV->isMaterializable()) {
109 if (GV->Materialize(&ErrInfo)) {
110 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n";
116 // In addition to deleting all other functions, we also want to spiff it
117 // up a little bit. Do this now.
119 Passes.add(new TargetData(M.get())); // Use correct TargetData
121 Passes.add(createGVExtractionPass(GVs, DeleteFn));
123 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
124 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
125 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
126 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
128 std::string ErrorInfo;
129 tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
130 raw_fd_ostream::F_Binary);
131 if (!ErrorInfo.empty()) {
132 errs() << ErrorInfo << '\n';
137 Passes.add(createPrintModulePass(&Out.os()));
138 else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true))
139 Passes.add(createBitcodeWriterPass(Out.os()));
141 Passes.run(*M.get());