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/MemoryBuffer.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/SystemUtils.h"
29 #include "llvm/System/Signals.h"
33 // InputFilename - The filename to read from.
34 static cl::opt<std::string>
35 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
36 cl::init("-"), cl::value_desc("filename"));
38 static cl::opt<std::string>
39 OutputFilename("o", cl::desc("Specify output filename"),
40 cl::value_desc("filename"), cl::init("-"));
43 Force("f", cl::desc("Enable binary output on terminals"));
46 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
50 cl::desc("Turn external linkage for callees of function to delete"));
52 // ExtractFuncs - The functions to extract from the module...
53 static cl::list<std::string>
54 ExtractFuncs("func", cl::desc("Specify function to extract"),
55 cl::ZeroOrMore, cl::value_desc("function"));
57 // ExtractGlobals - The globals to extract from the module...
58 static cl::list<std::string>
59 ExtractGlobals("glob", cl::desc("Specify global to extract"),
60 cl::ZeroOrMore, cl::value_desc("global"));
64 cl::desc("Write output as LLVM assembly"), cl::Hidden);
66 int main(int argc, char **argv) {
67 // Print a stack trace if we signal out.
68 sys::PrintStackTraceOnErrorSignal();
69 PrettyStackTraceProgram X(argc, argv);
71 LLVMContext &Context = getGlobalContext();
72 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
73 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
76 std::auto_ptr<Module> M;
77 M.reset(ParseIRFile(InputFilename, Err, Context));
80 Err.Print(argv[0], errs());
84 std::vector<GlobalValue *> GVs;
86 // Figure out which globals we should extract.
87 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
88 GlobalValue *GV = M.get()->getNamedGlobal(ExtractGlobals[i]);
90 errs() << argv[0] << ": program doesn't contain global named '"
91 << ExtractGlobals[i] << "'!\n";
97 // Figure out which functions we should extract.
98 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
99 GlobalValue *GV = M.get()->getFunction(ExtractFuncs[i]);
101 errs() << argv[0] << ": program doesn't contain function named '"
102 << ExtractFuncs[i] << "'!\n";
108 // In addition to deleting all other functions, we also want to spiff it
109 // up a little bit. Do this now.
111 Passes.add(new TargetData(M.get())); // Use correct TargetData
113 Passes.add(createGVExtractionPass(GVs, DeleteFn, Relink));
115 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
116 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
117 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
119 // Make sure that the Output file gets unlinked from the disk if we get a
121 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
123 std::string ErrorInfo;
124 raw_fd_ostream Out(OutputFilename.c_str(), ErrorInfo,
125 raw_fd_ostream::F_Binary);
126 if (!ErrorInfo.empty()) {
127 errs() << ErrorInfo << '\n';
132 Passes.add(createPrintModulePass(&Out));
133 else if (Force || !CheckBitcodeOutputToConsole(Out, true))
134 Passes.add(createBitcodeWriterPass(Out));
136 Passes.run(*M.get());