blob: a6edc721c94ecd003bc6c68f575008f2b32797a8 [file] [log] [blame]
Misha Brukmande03bc02005-04-24 17:36:05 +00001//===- llvm-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 Lattnerc30598b2006-12-06 01:18:01 +000022#include "llvm/Support/ManagedStatic.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000023#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000024#include "llvm/System/Signals.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000025#include <iostream>
Chris Lattner579d9142002-05-22 20:27:00 +000026#include <memory>
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000027#include <fstream>
Brian Gaeked0fde302003-11-11 22:41:34 +000028using namespace llvm;
29
Chris Lattner5ff62e92002-07-22 02:10:13 +000030// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000031static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000032InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
33 cl::init("-"), cl::value_desc("filename"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000034
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000035static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000036OutputFilename("o", cl::desc("Specify output filename"),
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000037 cl::value_desc("filename"), cl::init("-"));
38
39static cl::opt<bool>
40Force("f", cl::desc("Overwrite output files"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000041
Misha Brukmanca718e42004-04-22 23:07:39 +000042static cl::opt<bool>
43DeleteFn("delete", cl::desc("Delete specified function from Module"));
44
Chris Lattner5ff62e92002-07-22 02:10:13 +000045// ExtractFunc - The function to extract from the module... defaults to main.
Chris Lattnerc7a09852002-07-25 16:31:09 +000046static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000047ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
48 cl::value_desc("function"));
49
Chris Lattner579d9142002-05-22 20:27:00 +000050int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000051 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000052 try {
53 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
54 sys::PrintStackTraceOnErrorSignal();
Chris Lattner579d9142002-05-22 20:27:00 +000055
Reid Spencer1ef8bda2004-12-30 05:36:08 +000056 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
57 if (M.get() == 0) {
Bill Wendling68fe61d2006-11-29 00:19:40 +000058 llvm_cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000059 return 1;
60 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +000061
62 // Figure out which function we should extract
63 Function *F = M.get()->getNamedFunction(ExtractFunc);
64 if (F == 0) {
Bill Wendling68fe61d2006-11-29 00:19:40 +000065 llvm_cerr << argv[0] << ": program doesn't contain function named '"
Reid Spencer1ef8bda2004-12-30 05:36:08 +000066 << ExtractFunc << "'!\n";
67 return 1;
68 }
69
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000070 // In addition to deleting all other functions, we also want to spiff it
71 // up a little bit. Do this now.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000072 PassManager Passes;
Chris Lattner831b1212006-06-16 18:23:49 +000073 Passes.add(new TargetData(M.get())); // Use correct TargetData
Reid Spencer1ef8bda2004-12-30 05:36:08 +000074 // Either isolate the function or delete it from the Module
75 Passes.add(createFunctionExtractionPass(F, DeleteFn));
Misha Brukmande03bc02005-04-24 17:36:05 +000076 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
77 Passes.add(createFunctionResolvingPass()); // Delete prototypes
78 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Reid Spencer1ef8bda2004-12-30 05:36:08 +000079
80 std::ostream *Out = 0;
81
82 if (OutputFilename != "-") { // Not stdout?
83 if (!Force && std::ifstream(OutputFilename.c_str())) {
84 // If force is not specified, make sure not to overwrite a file!
Bill Wendling68fe61d2006-11-29 00:19:40 +000085 llvm_cerr << argv[0] << ": error opening '" << OutputFilename
Reid Spencer1ef8bda2004-12-30 05:36:08 +000086 << "': file exists!\n"
87 << "Use -f command line argument to force output\n";
88 return 1;
89 }
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000090 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
91 std::ios::binary;
92 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
Reid Spencer1ef8bda2004-12-30 05:36:08 +000093 } else { // Specified stdout
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000094 // FIXME: cout is not binary!
Misha Brukman3da94ae2005-04-22 00:00:37 +000095 Out = &std::cout;
Reid Spencer1ef8bda2004-12-30 05:36:08 +000096 }
97
Bill Wendling68fe61d2006-11-29 00:19:40 +000098 llvm_ostream L(*Out);
99 Passes.add(new WriteBytecodePass(&L)); // Write bytecode to file...
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000100 Passes.run(*M.get());
101
102 if (Out != &std::cout)
103 delete Out;
104 return 0;
105 } catch (const std::string& msg) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000106 llvm_cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000107 } catch (...) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000108 llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +0000109 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000110 return 1;
Chris Lattner579d9142002-05-22 20:27:00 +0000111}