blob: c7f942af27ead1376a2884d406d9961c9fae8b16 [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/CommandLine.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000022#include "llvm/System/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
Misha Brukmanca718e42004-04-22 23:07:39 +000039static cl::opt<bool>
40DeleteFn("delete", cl::desc("Delete specified function from Module"));
41
Chris Lattner5ff62e92002-07-22 02:10:13 +000042// ExtractFunc - The function to extract from the module... defaults to main.
Chris Lattnerc7a09852002-07-25 16:31:09 +000043static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000044ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
45 cl::value_desc("function"));
46
Chris Lattner579d9142002-05-22 20:27:00 +000047int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000048 try {
49 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
50 sys::PrintStackTraceOnErrorSignal();
Chris Lattner579d9142002-05-22 20:27:00 +000051
Reid Spencer1ef8bda2004-12-30 05:36:08 +000052 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
53 if (M.get() == 0) {
54 std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000055 return 1;
56 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +000057
58 // Figure out which function we should extract
59 Function *F = M.get()->getNamedFunction(ExtractFunc);
60 if (F == 0) {
61 std::cerr << argv[0] << ": program doesn't contain function named '"
62 << ExtractFunc << "'!\n";
63 return 1;
64 }
65
66 // In addition to deleting all other functions, we also want to spiff it up a
67 // little bit. Do this now.
68 //
69 PassManager Passes;
70 Passes.add(new TargetData("extract", M.get())); // Use correct TargetData
71 // Either isolate the function or delete it from the Module
72 Passes.add(createFunctionExtractionPass(F, DeleteFn));
73 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
74 Passes.add(createFunctionResolvingPass()); // Delete prototypes
75 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
76
77 std::ostream *Out = 0;
78
79 if (OutputFilename != "-") { // Not stdout?
80 if (!Force && std::ifstream(OutputFilename.c_str())) {
81 // If force is not specified, make sure not to overwrite a file!
82 std::cerr << argv[0] << ": error opening '" << OutputFilename
83 << "': file exists!\n"
84 << "Use -f command line argument to force output\n";
85 return 1;
86 }
87 Out = new std::ofstream(OutputFilename.c_str());
88 } else { // Specified stdout
89 Out = &std::cout;
90 }
91
92 Passes.add(new WriteBytecodePass(Out)); // Write bytecode to file...
93 Passes.run(*M.get());
94
95 if (Out != &std::cout)
96 delete Out;
97 return 0;
98 } catch (const std::string& msg) {
99 std::cerr << argv[0] << ": " << msg << "\n";
100 } catch (...) {
101 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +0000102 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000103 return 1;
Chris Lattner579d9142002-05-22 20:27:00 +0000104}