1 #include "llvm/Bitcode/ReaderWriter.h"
2 #include "llvm/IR/Function.h"
3 #include "llvm/IR/GlobalVariable.h"
4 #include "llvm/IR/LLVMContext.h"
5 #include "llvm/IR/Module.h"
6 #include "llvm/Support/CommandLine.h"
7 #include "llvm/Support/ManagedStatic.h"
8 #include "llvm/Support/MemoryBuffer.h"
9 #include "llvm/Support/FileSystem.h"
10 #include "llvm/Support/raw_ostream.h"
11 #include "llvm/Support/ErrorOr.h"
12 #include "llvm/Support/ToolOutputFile.h"
13 #include "llvm/Config/llvm-config.h"
14 
15 #include <system_error>
16 
17 #define LLVM_360 \
18   (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR == 6)
19 
20 
21 using namespace llvm;
22 
23 static cl::opt<std::string>
24 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
25 
26 static cl::opt<std::string>
27 OutputFilename("o", cl::desc("Output filename"),
28                cl::value_desc("filename"));
29 
30 int main(int argc, char **argv) {
31   LLVMContext &Context = getGlobalContext();
32   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
33 
34   cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
35 
36   std::string ErrorMessage;
37   Module *M = nullptr;
38 
39   {
40     ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
41       MemoryBuffer::getFile(InputFilename);
42     std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
43     if (std::error_code  ec = BufferOrErr.getError())
44       ErrorMessage = ec.message();
45     else {
46 #if LLVM_360
47       ErrorOr<Module *>
48 #else
49       ErrorOr<std::unique_ptr<Module>>
50 #endif
51       ModuleOrErr =
52           parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
53       if (std::error_code ec = ModuleOrErr.getError())
54         ErrorMessage = ec.message();
55 #if LLVM_360
56       M = ModuleOrErr.get();
57 #else
58       M = ModuleOrErr.get().release();
59 #endif
60     }
61   }
62 
63   if (!M) {
64     errs() << argv[0] << ": ";
65     if (ErrorMessage.size())
66       errs() << ErrorMessage << "\n";
67     else
68       errs() << "bitcode didn't read correctly.\n";
69     return 1;
70   }
71 
72   // Set linkage of every external definition to linkonce_odr.
73   for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
74     if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
75       i->setLinkage(GlobalValue::LinkOnceODRLinkage);
76   }
77 
78   for (Module::global_iterator i = M->global_begin(), e = M->global_end();
79        i != e; ++i) {
80     if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
81       i->setLinkage(GlobalValue::LinkOnceODRLinkage);
82   }
83 
84   if (OutputFilename.empty()) {
85     errs() << "no output file\n";
86     return 1;
87   }
88 
89   std::error_code EC;
90   std::unique_ptr<tool_output_file> Out
91   (new tool_output_file(OutputFilename, EC, sys::fs::F_None));
92   if (EC) {
93     errs() << EC.message() << '\n';
94     exit(1);
95   }
96 
97   WriteBitcodeToFile(M, Out->os());
98 
99   // Declare success.
100   Out->keep();
101   return 0;
102 }
103 
104