blob: cf5bc90e42def1264fa9081fa60dc8acc0422c81 [file] [log] [blame]
Chris Lattner2148bcb2003-09-10 19:42:51 +00001//===- extract.cpp - LLVM function extraction utility ---------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner579d9142002-05-22 20:27:00 +000010//
11// This utility changes the input module to only contain a single function,
12// which is primarily used for debugging transformations.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Module.h"
17#include "llvm/PassManager.h"
18#include "llvm/Bytecode/Reader.h"
19#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner33974ca2002-07-23 22:04:40 +000020#include "llvm/Transforms/IPO.h"
Chris Lattner579d9142002-05-22 20:27:00 +000021#include "Support/CommandLine.h"
22#include <memory>
23
Chris Lattner5ff62e92002-07-22 02:10:13 +000024// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000025static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000026InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
27 cl::init("-"), cl::value_desc("filename"));
28
29
30// ExtractFunc - The function to extract from the module... defaults to main.
Chris Lattnerc7a09852002-07-25 16:31:09 +000031static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000032ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
33 cl::value_desc("function"));
34
Chris Lattner579d9142002-05-22 20:27:00 +000035int main(int argc, char **argv) {
36 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
37
38 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
39 if (M.get() == 0) {
Chris Lattner50e3a202002-07-30 21:43:22 +000040 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattner579d9142002-05-22 20:27:00 +000041 return 1;
42 }
43
Chris Lattner5113eb02002-11-19 18:42:59 +000044 // Figure out which function we should extract
45 Function *F = M.get()->getNamedFunction(ExtractFunc);
46 if (F == 0) {
47 std::cerr << argv[0] << ": program doesn't contain function named '"
48 << ExtractFunc << "'!\n";
49 return 1;
50 }
51
Chris Lattner579d9142002-05-22 20:27:00 +000052 // In addition to just parsing the input from GCC, we also want to spiff it up
53 // a little bit. Do this now.
54 //
55 PassManager Passes;
Chris Lattner5113eb02002-11-19 18:42:59 +000056 Passes.add(createFunctionExtractionPass(F)); // Extract the function
Chris Lattner579d9142002-05-22 20:27:00 +000057 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Chris Lattner68ed3182002-10-12 20:50:16 +000058 Passes.add(createFunctionResolvingPass()); // Delete prototypes
Chris Lattner33974ca2002-07-23 22:04:40 +000059 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Chris Lattner579d9142002-05-22 20:27:00 +000060 Passes.add(new WriteBytecodePass(&std::cout)); // Write bytecode to file...
61
Chris Lattner7e708292002-06-25 16:13:24 +000062 Passes.run(*M.get());
Chris Lattner579d9142002-05-22 20:27:00 +000063 return 0;
64}