• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/IR/Verifier.h"
18 #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 
26 using namespace llvm;
27 
28 static cl::opt<std::string>
29 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
30     cl::init("-"), cl::value_desc("filename"));
31 
32 static cl::opt<std::string>
33 OutputFilename("o", cl::desc("Override output filename"),
34                cl::value_desc("filename"));
35 
36 static cl::opt<unsigned> NumOutputs("j", cl::Prefix, cl::init(2),
37                                     cl::desc("Number of output files"));
38 
39 static cl::opt<bool>
40     PreserveLocals("preserve-locals", cl::Prefix, cl::init(false),
41                    cl::desc("Split without externalizing locals"));
42 
main(int argc,char ** argv)43 int main(int argc, char **argv) {
44   LLVMContext Context;
45   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;
58     std::unique_ptr<tool_output_file> Out(new tool_output_file(
59         OutputFilename + utostr(I++), EC, sys::fs::F_None));
60     if (EC) {
61       errs() << EC.message() << '\n';
62       exit(1);
63     }
64 
65     verifyModule(*MPart);
66     WriteBitcodeToFile(MPart.get(), Out->os());
67 
68     // Declare success.
69     Out->keep();
70   }, PreserveLocals);
71 
72   return 0;
73 }
74