1//===-- llvm-split: command line tool for testing module splitter ---------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program can be used to test the llvm::SplitModule function.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/StringExtras.h"
15#include "llvm/Bitcode/ReaderWriter.h"
16#include "llvm/IR/LLVMContext.h"
17#include "llvm/IRReader/IRReader.h"
18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/FileSystem.h"
20#include "llvm/Support/SourceMgr.h"
21#include "llvm/Support/ToolOutputFile.h"
22#include "llvm/Support/raw_ostream.h"
23#include "llvm/Transforms/Utils/SplitModule.h"
24
25using namespace llvm;
26
27static cl::opt<std::string>
28InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
29    cl::init("-"), cl::value_desc("filename"));
30
31static cl::opt<std::string>
32OutputFilename("o", cl::desc("Override output filename"),
33               cl::value_desc("filename"));
34
35static cl::opt<unsigned> NumOutputs("j", cl::Prefix, cl::init(2),
36                                    cl::desc("Number of output files"));
37
38int main(int argc, char **argv) {
39  LLVMContext &Context = getGlobalContext();
40  SMDiagnostic Err;
41  cl::ParseCommandLineOptions(argc, argv, "LLVM module splitter\n");
42
43  std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
44
45  if (!M) {
46    Err.print(argv[0], errs());
47    return 1;
48  }
49
50  unsigned I = 0;
51  SplitModule(std::move(M), NumOutputs, [&](std::unique_ptr<Module> MPart) {
52    std::error_code EC;
53    std::unique_ptr<tool_output_file> Out(new tool_output_file(
54        OutputFilename + utostr(I++), EC, sys::fs::F_None));
55    if (EC) {
56      errs() << EC.message() << '\n';
57      exit(1);
58    }
59
60    WriteBitcodeToFile(MPart.get(), Out->os());
61
62    // Declare success.
63    Out->keep();
64  });
65
66  return 0;
67}
68