Fix llvm-extract's "writing bitcode to a terminal" warning, which wasn't
[oota-llvm.git] / tools / llvm-extract / llvm-extract.cpp
1 //===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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
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"
30 #include <memory>
31 using namespace llvm;
32
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"));
37
38 static cl::opt<std::string>
39 OutputFilename("o", cl::desc("Specify output filename"),
40                cl::value_desc("filename"), cl::init("-"));
41
42 static cl::opt<bool>
43 Force("f", cl::desc("Enable binary output on terminals"));
44
45 static cl::opt<bool>
46 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
47
48 static cl::opt<bool>
49 Relink("relink",
50        cl::desc("Turn external linkage for callees of function to delete"));
51
52 // ExtractFunc - The function to extract from the module... 
53 static cl::opt<std::string>
54 ExtractFunc("func", cl::desc("Specify function to extract"), cl::init(""),
55             cl::value_desc("function"));
56
57 // ExtractGlobal - The global to extract from the module...
58 static cl::opt<std::string>
59 ExtractGlobal("glob", cl::desc("Specify global to extract"), cl::init(""),
60               cl::value_desc("global"));
61
62 static cl::opt<bool>
63 OutputAssembly("S",
64                cl::desc("Write output as LLVM assembly"), cl::Hidden);
65
66 int main(int argc, char **argv) {
67   // Print a stack trace if we signal out.
68   sys::PrintStackTraceOnErrorSignal();
69   PrettyStackTraceProgram X(argc, argv);
70
71   LLVMContext &Context = getGlobalContext();
72   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
73   cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n");
74
75   SMDiagnostic Err;
76   std::auto_ptr<Module> M;
77   M.reset(ParseIRFile(InputFilename, Err, Context));
78
79   if (M.get() == 0) {
80     Err.Print(argv[0], errs());
81     return 1;
82   }
83
84   // Figure out which function we should extract
85   GlobalVariable *G = !ExtractGlobal.empty() ?
86     M.get()->getNamedGlobal(ExtractGlobal) : 0;
87
88   // Figure out which function we should extract
89   if (ExtractFunc.empty() && ExtractGlobal.empty()) ExtractFunc = "main";
90   Function *F = M.get()->getFunction(ExtractFunc);
91
92   if (F == 0 && G == 0) {
93     errs() << argv[0] << ": program doesn't contain function named '"
94            << ExtractFunc << "' or a global named '" << ExtractGlobal << "'!\n";
95     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
103   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));
108   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
113   // Make sure that the Output file gets unlinked from the disk if we get a
114   // SIGINT
115   sys::RemoveFileOnSignal(sys::Path(OutputFilename));
116
117   std::string ErrorInfo;
118   raw_fd_ostream Out(OutputFilename.c_str(), ErrorInfo,
119                      raw_fd_ostream::F_Binary);
120   if (!ErrorInfo.empty()) {
121     errs() << ErrorInfo << '\n';
122     return 1;
123   }
124
125   if (OutputAssembly)
126     Passes.add(createPrintModulePass(&Out));
127   else if (Force || !CheckBitcodeOutputToConsole(Out, true))
128     Passes.add(createBitcodeWriterPass(Out));
129
130   Passes.run(*M.get());
131
132   return 0;
133 }