blob: a6204dc156d1a4ea794a3f6bb527b63b92f51754 [file] [log] [blame]
Peter Collingbournec1e784c2015-08-21 02:48:20 +00001//===-- 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"
Teresa Johnsona5479192016-11-11 05:34:58 +000015#include "llvm/Bitcode/BitcodeWriter.h"
Peter Collingbournec1e784c2015-08-21 02:48:20 +000016#include "llvm/IR/LLVMContext.h"
Evgeniy Stepanov2b6ba77f2016-03-28 21:37:02 +000017#include "llvm/IR/Verifier.h"
Peter Collingbournec1e784c2015-08-21 02:48:20 +000018#include "llvm/IRReader/IRReader.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/FileSystem.h"
21#include "llvm/Support/SourceMgr.h"
22#include "llvm/Support/ToolOutputFile.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/Transforms/Utils/SplitModule.h"
25
26using namespace llvm;
27
28static cl::opt<std::string>
29InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
30 cl::init("-"), cl::value_desc("filename"));
31
32static cl::opt<std::string>
33OutputFilename("o", cl::desc("Override output filename"),
34 cl::value_desc("filename"));
35
36static cl::opt<unsigned> NumOutputs("j", cl::Prefix, cl::init(2),
37 cl::desc("Number of output files"));
38
Sergei Larin72b0e152016-01-18 21:07:13 +000039static cl::opt<bool>
40 PreserveLocals("preserve-locals", cl::Prefix, cl::init(false),
41 cl::desc("Split without externalizing locals"));
42
Peter Collingbournec1e784c2015-08-21 02:48:20 +000043int main(int argc, char **argv) {
Mehdi Amini8be77072016-04-14 21:59:01 +000044 LLVMContext Context;
Peter Collingbournec1e784c2015-08-21 02:48:20 +000045 SMDiagnostic Err;
46 cl::ParseCommandLineOptions(argc, argv, "LLVM module splitter\n");
47
48 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
49
50 if (!M) {
51 Err.print(argv[0], errs());
52 return 1;
53 }
54
55 unsigned I = 0;
56 SplitModule(std::move(M), NumOutputs, [&](std::unique_ptr<Module> MPart) {
57 std::error_code EC;
Reid Kleckner97ca9642017-09-23 01:03:17 +000058 std::unique_ptr<ToolOutputFile> Out(
59 new ToolOutputFile(OutputFilename + utostr(I++), EC, sys::fs::F_None));
Peter Collingbournec1e784c2015-08-21 02:48:20 +000060 if (EC) {
61 errs() << EC.message() << '\n';
62 exit(1);
63 }
64
Evgeniy Stepanov2b6ba77f2016-03-28 21:37:02 +000065 verifyModule(*MPart);
Rafael Espindola06d62072018-02-14 19:11:32 +000066 WriteBitcodeToFile(*MPart, Out->os());
Peter Collingbournec1e784c2015-08-21 02:48:20 +000067
68 // Declare success.
69 Out->keep();
Sergei Larin72b0e152016-01-18 21:07:13 +000070 }, PreserveLocals);
Peter Collingbournec1e784c2015-08-21 02:48:20 +000071
72 return 0;
73}