blob: 60a0134ef7b9afcac0889e9f66f6f71f88f4739d [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 Lattnerf2e292c2007-02-07 21:41:02 +000022#include "llvm/Support/Compressor.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000023#include "llvm/Support/ManagedStatic.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000024#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000025#include "llvm/System/Signals.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000026#include <iostream>
Chris Lattner579d9142002-05-22 20:27:00 +000027#include <memory>
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000028#include <fstream>
Brian Gaeked0fde302003-11-11 22:41:34 +000029using namespace llvm;
30
Chris Lattner5ff62e92002-07-22 02:10:13 +000031// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000032static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000033InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
34 cl::init("-"), cl::value_desc("filename"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000035
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000036static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000037OutputFilename("o", cl::desc("Specify output filename"),
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000038 cl::value_desc("filename"), cl::init("-"));
39
40static cl::opt<bool>
41Force("f", cl::desc("Overwrite output files"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000042
Misha Brukmanca718e42004-04-22 23:07:39 +000043static cl::opt<bool>
44DeleteFn("delete", cl::desc("Delete specified function from Module"));
45
Anton Korobeynikovb10308e2007-01-28 13:31:35 +000046static cl::opt<bool>
47Relink("relink",
48 cl::desc("Turn external linkage for callees of function to delete"));
49
Chris Lattner5ff62e92002-07-22 02:10:13 +000050// ExtractFunc - The function to extract from the module... defaults to main.
Chris Lattnerc7a09852002-07-25 16:31:09 +000051static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000052ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
53 cl::value_desc("function"));
54
Chris Lattner579d9142002-05-22 20:27:00 +000055int main(int argc, char **argv) {
Chris Lattnerc30598b2006-12-06 01:18:01 +000056 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000057 try {
58 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
59 sys::PrintStackTraceOnErrorSignal();
Chris Lattner579d9142002-05-22 20:27:00 +000060
Chris Lattnerf2e292c2007-02-07 21:41:02 +000061 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename,
62 Compressor::decompressToNewBuffer));
Reid Spencer1ef8bda2004-12-30 05:36:08 +000063 if (M.get() == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000064 cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000065 return 1;
66 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +000067
68 // Figure out which function we should extract
Reid Spencercdedc3b2007-02-05 21:17:53 +000069 Function *F = M.get()->getFunction(ExtractFunc);
Reid Spencer1ef8bda2004-12-30 05:36:08 +000070 if (F == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000071 cerr << argv[0] << ": program doesn't contain function named '"
72 << ExtractFunc << "'!\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000073 return 1;
74 }
75
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000076 // In addition to deleting all other functions, we also want to spiff it
77 // up a little bit. Do this now.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000078 PassManager Passes;
Chris Lattner831b1212006-06-16 18:23:49 +000079 Passes.add(new TargetData(M.get())); // Use correct TargetData
Reid Spencer1ef8bda2004-12-30 05:36:08 +000080 // Either isolate the function or delete it from the Module
Anton Korobeynikovb10308e2007-01-28 13:31:35 +000081 Passes.add(createFunctionExtractionPass(F, DeleteFn, Relink));
82 if (!DeleteFn)
83 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Misha Brukmande03bc02005-04-24 17:36:05 +000084 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Reid Spencercdedc3b2007-02-05 21:17:53 +000085 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
Reid Spencer1ef8bda2004-12-30 05:36:08 +000086
87 std::ostream *Out = 0;
88
89 if (OutputFilename != "-") { // Not stdout?
90 if (!Force && std::ifstream(OutputFilename.c_str())) {
91 // If force is not specified, make sure not to overwrite a file!
Bill Wendlinge8156192006-12-07 01:30:32 +000092 cerr << argv[0] << ": error opening '" << OutputFilename
93 << "': file exists!\n"
94 << "Use -f command line argument to force output\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +000095 return 1;
96 }
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000097 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
98 std::ios::binary;
99 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000100 } else { // Specified stdout
Jeff Cohen5fb6ed42005-01-22 17:36:17 +0000101 // FIXME: cout is not binary!
Misha Brukman3da94ae2005-04-22 00:00:37 +0000102 Out = &std::cout;
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000103 }
104
Bill Wendlinge8156192006-12-07 01:30:32 +0000105 OStream L(*Out);
Bill Wendling68fe61d2006-11-29 00:19:40 +0000106 Passes.add(new WriteBytecodePass(&L)); // Write bytecode to file...
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000107 Passes.run(*M.get());
108
109 if (Out != &std::cout)
110 delete Out;
111 return 0;
112 } catch (const std::string& msg) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000113 cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000114 } catch (...) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000115 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +0000116 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000117 return 1;
Chris Lattner579d9142002-05-22 20:27:00 +0000118}