blob: 0fc10df7cef4982306c6f3db4bbcf553107586ba [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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 Anderson25209b42009-07-01 16:58:40 +000015#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "llvm/Module.h"
17#include "llvm/PassManager.h"
18#include "llvm/Bitcode/ReaderWriter.h"
19#include "llvm/Transforms/IPO.h"
20#include "llvm/Target/TargetData.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/ManagedStatic.h"
23#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000024#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmandd7ca8e2009-07-15 17:29:42 +000025#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/System/Signals.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include <memory>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028using namespace llvm;
29
30// InputFilename - The filename to read from.
31static cl::opt<std::string>
32InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
33 cl::init("-"), cl::value_desc("filename"));
34
35static cl::opt<std::string>
36OutputFilename("o", cl::desc("Specify output filename"),
37 cl::value_desc("filename"), cl::init("-"));
38
39static cl::opt<bool>
40Force("f", cl::desc("Overwrite output files"));
41
42static cl::opt<bool>
Andrew Lenharth38a17672008-03-07 19:51:57 +000043DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
45static cl::opt<bool>
46Relink("relink",
47 cl::desc("Turn external linkage for callees of function to delete"));
48
Andrew Lenharth38a17672008-03-07 19:51:57 +000049// ExtractFunc - The function to extract from the module...
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050static cl::opt<std::string>
Andrew Lenharth38a17672008-03-07 19:51:57 +000051ExtractFunc("func", cl::desc("Specify function to extract"), cl::init(""),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 cl::value_desc("function"));
53
Andrew Lenharth38a17672008-03-07 19:51:57 +000054// ExtractGlobal - The global to extract from the module...
55static cl::opt<std::string>
56ExtractGlobal("glob", cl::desc("Specify global to extract"), cl::init(""),
57 cl::value_desc("global"));
58
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +000060 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +000062 PrettyStackTraceProgram X(argc, argv);
Owen Anderson25209b42009-07-01 16:58:40 +000063
Owen Andersone84b8b32009-07-15 22:16:10 +000064 LLVMContext &Context = getGlobalContext();
Chris Lattnere6012df2009-03-06 05:34:10 +000065 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
66 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067
68 std::auto_ptr<Module> M;
69
70 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename);
71 if (Buffer == 0) {
Reid Spencer0e8e10d2007-08-08 21:19:01 +000072 cerr << argv[0] << ": Error reading file '" + InputFilename + "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 return 1;
74 } else {
Owen Andersona148fdd2009-07-01 21:22:36 +000075 M.reset(ParseBitcodeFile(Buffer, Context));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 }
77 delete Buffer;
78
79 if (M.get() == 0) {
80 cerr << argv[0] << ": bitcode didn't read correctly.\n";
81 return 1;
82 }
83
84 // Figure out which function we should extract
Dan Gohman5e0550b2009-04-20 16:19:02 +000085 GlobalVariable *G = !ExtractGlobal.empty() ?
Andrew Lenharth38a17672008-03-07 19:51:57 +000086 M.get()->getNamedGlobal(ExtractGlobal) : 0;
87
88 // Figure out which function we should extract
Dan Gohman5e0550b2009-04-20 16:19:02 +000089 if (ExtractFunc.empty() && ExtractGlobal.empty()) ExtractFunc = "main";
Andrew Lenharthdf35b3c2008-03-07 20:07:24 +000090 Function *F = M.get()->getFunction(ExtractFunc);
Andrew Lenharth38a17672008-03-07 19:51:57 +000091
92 if (F == 0 && G == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 cerr << argv[0] << ": program doesn't contain function named '"
Andrew Lenharth38a17672008-03-07 19:51:57 +000094 << ExtractFunc << "' or a global named '" << ExtractGlobal << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 return 1;
96 }
97
98 // In addition to deleting all other functions, we also want to spiff it
99 // up a little bit. Do this now.
100 PassManager Passes;
101 Passes.add(new TargetData(M.get())); // Use correct TargetData
102 // Either isolate the function or delete it from the Module
Andrew Lenharth38a17672008-03-07 19:51:57 +0000103 std::vector<GlobalValue*> GVs;
104 if (F) GVs.push_back(F);
105 if (G) GVs.push_back(G);
106
107 Passes.add(createGVExtractionPass(GVs, DeleteFn, Relink));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 if (!DeleteFn)
109 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
110 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
111 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
112
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000113 raw_ostream *Out = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
115 if (OutputFilename != "-") { // Not stdout?
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000116 std::string ErrorInfo;
117 Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
118 Force, ErrorInfo);
119 if (!ErrorInfo.empty()) {
120 errs() << ErrorInfo << '\n';
121 if (!Force)
122 errs() << "Use -f command line argument to force output\n";
123 delete Out;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 return 1;
125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 } else { // Specified stdout
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000127 // FIXME: errs() is not binary!
128 Out = &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 }
130
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000131 Passes.add(createBitcodeWriterPass(*Out));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 Passes.run(*M.get());
133
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000134 if (Out != &errs())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 delete Out;
136 return 0;
137}