blob: 4f3236f802bb1927c2aafe953ef9ad4f7b980a61 [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"
Bill Wendling68fe61d2006-11-29 00:19:40 +000022#include "llvm/Support/Streams.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000023#include "llvm/System/Signals.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000024#include <iostream>
Chris Lattner579d9142002-05-22 20:27:00 +000025#include <memory>
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000026#include <fstream>
Brian Gaeked0fde302003-11-11 22:41:34 +000027using namespace llvm;
28
Chris Lattner5ff62e92002-07-22 02:10:13 +000029// InputFilename - The filename to read from.
Chris Lattnerc7a09852002-07-25 16:31:09 +000030static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000031InputFilename(cl::Positional, cl::desc("<input bytecode file>"),
32 cl::init("-"), cl::value_desc("filename"));
Misha Brukman3da94ae2005-04-22 00:00:37 +000033
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000034static cl::opt<std::string>
Misha Brukman3da94ae2005-04-22 00:00:37 +000035OutputFilename("o", cl::desc("Specify output filename"),
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000036 cl::value_desc("filename"), cl::init("-"));
37
38static cl::opt<bool>
39Force("f", cl::desc("Overwrite output files"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000040
Misha Brukmanca718e42004-04-22 23:07:39 +000041static cl::opt<bool>
42DeleteFn("delete", cl::desc("Delete specified function from Module"));
43
Chris Lattner5ff62e92002-07-22 02:10:13 +000044// ExtractFunc - The function to extract from the module... defaults to main.
Chris Lattnerc7a09852002-07-25 16:31:09 +000045static cl::opt<std::string>
Chris Lattner5ff62e92002-07-22 02:10:13 +000046ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
47 cl::value_desc("function"));
48
Chris Lattner579d9142002-05-22 20:27:00 +000049int main(int argc, char **argv) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +000050 try {
51 cl::ParseCommandLineOptions(argc, argv, " llvm extractor\n");
52 sys::PrintStackTraceOnErrorSignal();
Chris Lattner579d9142002-05-22 20:27:00 +000053
Reid Spencer1ef8bda2004-12-30 05:36:08 +000054 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
55 if (M.get() == 0) {
Bill Wendling68fe61d2006-11-29 00:19:40 +000056 llvm_cerr << argv[0] << ": bytecode didn't read correctly.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +000057 return 1;
58 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +000059
60 // Figure out which function we should extract
61 Function *F = M.get()->getNamedFunction(ExtractFunc);
62 if (F == 0) {
Bill Wendling68fe61d2006-11-29 00:19:40 +000063 llvm_cerr << argv[0] << ": program doesn't contain function named '"
Reid Spencer1ef8bda2004-12-30 05:36:08 +000064 << ExtractFunc << "'!\n";
65 return 1;
66 }
67
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000068 // In addition to deleting all other functions, we also want to spiff it
69 // up a little bit. Do this now.
Reid Spencer1ef8bda2004-12-30 05:36:08 +000070 PassManager Passes;
Chris Lattner831b1212006-06-16 18:23:49 +000071 Passes.add(new TargetData(M.get())); // Use correct TargetData
Reid Spencer1ef8bda2004-12-30 05:36:08 +000072 // Either isolate the function or delete it from the Module
73 Passes.add(createFunctionExtractionPass(F, DeleteFn));
Misha Brukmande03bc02005-04-24 17:36:05 +000074 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
75 Passes.add(createFunctionResolvingPass()); // Delete prototypes
76 Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Reid Spencer1ef8bda2004-12-30 05:36:08 +000077
78 std::ostream *Out = 0;
79
80 if (OutputFilename != "-") { // Not stdout?
81 if (!Force && std::ifstream(OutputFilename.c_str())) {
82 // If force is not specified, make sure not to overwrite a file!
Bill Wendling68fe61d2006-11-29 00:19:40 +000083 llvm_cerr << argv[0] << ": error opening '" << OutputFilename
Reid Spencer1ef8bda2004-12-30 05:36:08 +000084 << "': file exists!\n"
85 << "Use -f command line argument to force output\n";
86 return 1;
87 }
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000088 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
89 std::ios::binary;
90 Out = new std::ofstream(OutputFilename.c_str(), io_mode);
Reid Spencer1ef8bda2004-12-30 05:36:08 +000091 } else { // Specified stdout
Jeff Cohen5fb6ed42005-01-22 17:36:17 +000092 // FIXME: cout is not binary!
Misha Brukman3da94ae2005-04-22 00:00:37 +000093 Out = &std::cout;
Reid Spencer1ef8bda2004-12-30 05:36:08 +000094 }
95
Bill Wendling68fe61d2006-11-29 00:19:40 +000096 llvm_ostream L(*Out);
97 Passes.add(new WriteBytecodePass(&L)); // Write bytecode to file...
Reid Spencer1ef8bda2004-12-30 05:36:08 +000098 Passes.run(*M.get());
99
100 if (Out != &std::cout)
101 delete Out;
102 return 0;
103 } catch (const std::string& msg) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000104 llvm_cerr << argv[0] << ": " << msg << "\n";
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000105 } catch (...) {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000106 llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Chris Lattnerba9cc1f2004-02-18 16:53:59 +0000107 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000108 return 1;
Chris Lattner579d9142002-05-22 20:27:00 +0000109}