blob: 78d0426be1847b17ea3f2f5c1d288c51384f3b74 [file] [log] [blame]
Chris Lattner2148bcb2003-09-10 19:42:51 +00001//===- extract.cpp - LLVM function extraction utility ---------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000031
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000032static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000033OutputFilename("o", cl::desc("Specify output filename"),
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000034 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
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000066 // In addition to deleting all other functions, we also want to spiff it
67 // up a little bit. Do this now.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000068 PassManager Passes;
69 Passes.add(new TargetData("extract", M.get())); // Use correct TargetData
70 // Either isolate the function or delete it from the Module
71 Passes.add(createFunctionExtractionPass(F, DeleteFn));
72 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
73 Passes.add(createFunctionResolvingPass()); // Delete prototypes
74 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
75
76 std::ostream *Out = 0;
77
78 if (OutputFilename != "-") { // Not stdout?
79 if (!Force && std::ifstream(OutputFilename.c_str())) {
80 // If force is not specified, make sure not to overwrite a file!
81 std::cerr << argv[0] << ": error opening '" << OutputFilename
82 << "': file exists!\n"
83 << "Use -f command line argument to force output\n";
84 return 1;
85 }
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000086 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
87 std::ios::binary;
88 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
Reid Spencer1ef8bda2004-12-30 05:36:08 +000089 } else { // Specified stdout
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000090 // FIXME: cout is not binary!
Misha Brukman3da94ae2005-04-22 00:00:37 +000091 Out = &std::cout;
Reid Spencer1ef8bda2004-12-30 05:36:08 +000092 }
93
94 Passes.add(new WriteBytecodePass(Out)); // Write bytecode to file...
95 Passes.run(*M.get());
96
97 if (Out != &std::cout)
98 delete Out;
99 return 0;
100 } catch (const std::string& msg) {
101 std::cerr << argv[0] << ": " << msg << "\n";
102 } catch (...) {
103 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +0000104 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000105 return 1;
Chris Lattner579d9142002-05-22 20:27:00 +0000106}