blob: 0298df2b2d5f8bb23d8eda59418ba73fae55bd26 [file] [log] [blame]
Chris Lattner2148bcb2003-09-10 19:42:51 +00001//===- extract.cpp - LLVM function extraction utility ---------------------===//
Chris Lattner579d9142002-05-22 20:27:00 +00002//
3// This utility changes the input module to only contain a single function,
4// which is primarily used for debugging transformations.
5//
6//===----------------------------------------------------------------------===//
7
8#include "llvm/Module.h"
9#include "llvm/PassManager.h"
10#include "llvm/Bytecode/Reader.h"
11#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner33974ca2002-07-23 22:04:40 +000012#include "llvm/Transforms/IPO.h"
Chris Lattner579d9142002-05-22 20:27:00 +000013#include "Support/CommandLine.h"
14#include <memory>
15
Chris Lattner5ff62e92002-07-22 02:10:13 +000016// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000017static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000018InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
19 cl::init("-"), cl::value_desc("filename"));
20
21
22// ExtractFunc - The function to extract from the module... defaults to main.
Chris Lattnerc7a09852002-07-25 16:31:09 +000023static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000024ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
25 cl::value_desc("function"));
26
Chris Lattner579d9142002-05-22 20:27:00 +000027int main(int argc, char **argv) {
28 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
29
30 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
31 if (M.get() == 0) {
Chris Lattner50e3a202002-07-30 21:43:22 +000032 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattner579d9142002-05-22 20:27:00 +000033 return 1;
34 }
35
Chris Lattner5113eb02002-11-19 18:42:59 +000036 // Figure out which function we should extract
37 Function *F = M.get()->getNamedFunction(ExtractFunc);
38 if (F == 0) {
39 std::cerr << argv[0] << ": program doesn't contain function named '"
40 << ExtractFunc << "'!\n";
41 return 1;
42 }
43
Chris Lattner579d9142002-05-22 20:27:00 +000044 // In addition to just parsing the input from GCC, we also want to spiff it up
45 // a little bit. Do this now.
46 //
47 PassManager Passes;
Chris Lattner5113eb02002-11-19 18:42:59 +000048 Passes.add(createFunctionExtractionPass(F)); // Extract the function
Chris Lattner579d9142002-05-22 20:27:00 +000049 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Chris Lattner68ed3182002-10-12 20:50:16 +000050 Passes.add(createFunctionResolvingPass()); // Delete prototypes
Chris Lattner33974ca2002-07-23 22:04:40 +000051 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Chris Lattner579d9142002-05-22 20:27:00 +000052 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...
53
Chris Lattner7e708292002-06-25 16:13:24 +000054 Passes.run(*M.get());
Chris Lattner579d9142002-05-22 20:27:00 +000055 return 0;
56}