blob: 932f19ef6064fca56381218db5175ad9badab3c5 [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
Anton Korobeynikovb10308e2007-01-28 13:31:35 +000045static cl::opt<bool>
46Relink("relink",
47 cl::desc("Turn external linkage for callees of function to delete"));
48
Chris Lattner5ff62e92002-07-22 02:10:13 +000049// ExtractFunc - The function to extract from the module... defaults to main.
Chris Lattnerc7a09852002-07-25 16:31:09 +000050static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000051ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
52 cl::value_desc("function"));
53
Chris Lattner579d9142002-05-22 20:27:00 +000054int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000055 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000056 try {
57 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
58 sys::PrintStackTraceOnErrorSignal();
Chris Lattner579d9142002-05-22 20:27:00 +000059
Reid Spencer1ef8bda2004-12-30 05:36:08 +000060 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
61 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000062 cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000063 return 1;
64 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +000065
66 // Figure out which function we should extract
67 Function *F = M.get()->getNamedFunction(ExtractFunc);
68 if (F == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000069 cerr << argv[0] << ": program doesn't contain function named '"
70 << ExtractFunc << "'!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000071 return 1;
72 }
73
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000074 // In addition to deleting all other functions, we also want to spiff it
75 // up a little bit. Do this now.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000076 PassManager Passes;
Chris Lattner831b1212006-06-16 18:23:49 +000077 Passes.add(new TargetData(M.get())); // Use correct TargetData
Reid Spencer1ef8bda2004-12-30 05:36:08 +000078 // Either isolate the function or delete it from the Module
Anton Korobeynikovb10308e2007-01-28 13:31:35 +000079 Passes.add(createFunctionExtractionPass(F, DeleteFn, Relink));
80 if (!DeleteFn)
81 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Misha Brukmande03bc02005-04-24 17:36:05 +000082 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Reid Spencer1ef8bda2004-12-30 05:36:08 +000083
84 std::ostream *Out = 0;
85
86 if (OutputFilename != "-") { // Not stdout?
87 if (!Force && std::ifstream(OutputFilename.c_str())) {
88 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000089 cerr << argv[0] << ": error opening '" << OutputFilename
90 << "': file exists!\n"
91 << "Use -f command line argument to force output\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000092 return 1;
93 }
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000094 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
95 std::ios::binary;
96 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
Reid Spencer1ef8bda2004-12-30 05:36:08 +000097 } else { // Specified stdout
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000098 // FIXME: cout is not binary!
Misha Brukman3da94ae2005-04-22 00:00:37 +000099 Out = &std::cout;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000100 }
101
Bill Wendlinge8156192006-12-07 01:30:32 +0000102 OStream L(*Out);
Bill Wendling68fe61d2006-11-29 00:19:40 +0000103 Passes.add(new WriteBytecodePass(&L)); // Write bytecode to file...
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000104 Passes.run(*M.get());
105
106 if (Out != &std::cout)
107 delete Out;
108 return 0;
109 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000110 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000111 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000112 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +0000113 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000114 return 1;
Chris Lattner579d9142002-05-22 20:27:00 +0000115}