blob: b91020db7c77d601aa72ee3310ae5ac49ccda2f3 [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//===----------------------------------------------------------------------===//
Chris Lattner579d9142002-05-22 20:27:00 +00009//
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/Module.h"
16#include "llvm/PassManager.h"
17#include "llvm/Bytecode/Reader.h"
18#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner33974ca2002-07-23 22:04:40 +000019#include "llvm/Transforms/IPO.h"
Brian Gaeke20408902003-10-28 22:22:16 +000020#include "llvm/Target/TargetData.h"
Chris Lattner579d9142002-05-22 20:27:00 +000021#include "Support/CommandLine.h"
Chris Lattner364d1202004-02-19 20:32:39 +000022#include "Support/Signals.h"
Chris Lattner579d9142002-05-22 20:27:00 +000023#include <memory>
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000024#include <fstream>
Brian Gaeked0fde302003-11-11 22:41:34 +000025using namespace llvm;
26
Chris Lattner5ff62e92002-07-22 02:10:13 +000027// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000028static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000029InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
30 cl::init("-"), cl::value_desc("filename"));
31
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000032static cl::opt<std::string>
33OutputFilename("o", cl::desc("Specify output filename"),
34 cl::value_desc("filename"), cl::init("-"));
35
36static cl::opt<bool>
37Force("f", cl::desc("Overwrite output files"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000038
39// ExtractFunc - The function to extract from the module... defaults to main.
Chris Lattnerc7a09852002-07-25 16:31:09 +000040static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000041ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
42 cl::value_desc("function"));
43
Chris Lattner579d9142002-05-22 20:27:00 +000044int main(int argc, char **argv) {
45 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
Chris Lattner364d1202004-02-19 20:32:39 +000046 PrintStackTraceOnErrorSignal();
Chris Lattner579d9142002-05-22 20:27:00 +000047
48 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
49 if (M.get() == 0) {
Chris Lattner50e3a202002-07-30 21:43:22 +000050 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattner579d9142002-05-22 20:27:00 +000051 return 1;
52 }
53
Chris Lattner5113eb02002-11-19 18:42:59 +000054 // Figure out which function we should extract
55 Function *F = M.get()->getNamedFunction(ExtractFunc);
56 if (F == 0) {
57 std::cerr << argv[0] << ": program doesn't contain function named '"
58 << ExtractFunc << "'!\n";
59 return 1;
60 }
61
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000062 // In addition to deleting all other functions, we also want to spiff it up a
63 // little bit. Do this now.
Chris Lattner579d9142002-05-22 20:27:00 +000064 //
65 PassManager Passes;
Brian Gaeke20408902003-10-28 22:22:16 +000066 Passes.add(new TargetData("extract", M.get())); // Use correct TargetData
Chris Lattner5113eb02002-11-19 18:42:59 +000067 Passes.add(createFunctionExtractionPass(F)); // Extract the function
Chris Lattner579d9142002-05-22 20:27:00 +000068 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Chris Lattner68ed3182002-10-12 20:50:16 +000069 Passes.add(createFunctionResolvingPass()); // Delete prototypes
Chris Lattner33974ca2002-07-23 22:04:40 +000070 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Chris Lattner579d9142002-05-22 20:27:00 +000071
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000072 std::ostream *Out = 0;
73
74 if (OutputFilename != "-") { // Not stdout?
75 if (!Force && std::ifstream(OutputFilename.c_str())) {
76 // If force is not specified, make sure not to overwrite a file!
77 std::cerr << argv[0] << ": error opening '" << OutputFilename
78 << "': file exists!\n"
79 << "Use -f command line argument to force output\n";
80 return 1;
81 }
82 Out = new std::ofstream(OutputFilename.c_str());
83 } else { // Specified stdout
84 Out = &std::cout;
85 }
86
87 Passes.add(new WriteBytecodePass(Out)); // Write bytecode to file...
Chris Lattner7e708292002-06-25 16:13:24 +000088 Passes.run(*M.get());
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000089
90 if (Out != &std::cout)
91 delete Out;
Chris Lattner579d9142002-05-22 20:27:00 +000092 return 0;
93}