6b8dc3e7faecd8a97022d3c89241531c41809378
[oota-llvm.git] / lib / ExecutionEngine / Orc / IndirectionUtils.cpp
1 //===---- IndirectionUtils.cpp - Utilities for call indirection in Orc ----===//
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 #include "llvm/ADT/Triple.h"
11 #include "llvm/ExecutionEngine/Orc/CloneSubModule.h"
12 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
13 #include "llvm/IR/CallSite.h"
14 #include "llvm/IR/IRBuilder.h"
15 #include <set>
16
17 using namespace llvm;
18
19 namespace llvm {
20 namespace orc {
21
22 GlobalVariable* createImplPointer(Function &F, const Twine &Name,
23                                   Constant *Initializer) {
24   assert(F.getParent() && "Function isn't in a module.");
25   if (!Initializer)
26     Initializer = Constant::getNullValue(F.getType());
27   Module &M = *F.getParent();
28   return new GlobalVariable(M, F.getType(), false, GlobalValue::ExternalLinkage,
29                             Initializer, Name, nullptr,
30                             GlobalValue::NotThreadLocal, 0, true);
31 }
32
33 void makeStub(Function &F, GlobalVariable &ImplPointer) {
34   assert(F.isDeclaration() && "Can't turn a definition into a stub.");
35   assert(F.getParent() && "Function isn't in a module.");
36   Module &M = *F.getParent();
37   BasicBlock *EntryBlock = BasicBlock::Create(M.getContext(), "entry", &F);
38   IRBuilder<> Builder(EntryBlock);
39   LoadInst *ImplAddr = Builder.CreateLoad(&ImplPointer);
40   std::vector<Value*> CallArgs;
41   for (auto &A : F.args())
42     CallArgs.push_back(&A);
43   CallInst *Call = Builder.CreateCall(ImplAddr, CallArgs);
44   Call->setTailCall();
45   Builder.CreateRet(Call);
46 }
47
48 void partition(Module &M, const ModulePartitionMap &PMap) {
49
50   for (auto &KVPair : PMap) {
51
52     auto ExtractGlobalVars =
53       [&](GlobalVariable &New, const GlobalVariable &Orig,
54           ValueToValueMapTy &VMap) {
55         if (KVPair.second.count(&Orig)) {
56           copyGVInitializer(New, Orig, VMap);
57         }
58         if (New.getLinkage() == GlobalValue::PrivateLinkage) {
59           New.setLinkage(GlobalValue::ExternalLinkage);
60           New.setVisibility(GlobalValue::HiddenVisibility);
61         }
62       };
63
64     auto ExtractFunctions =
65       [&](Function &New, const Function &Orig, ValueToValueMapTy &VMap) {
66         if (KVPair.second.count(&Orig))
67           copyFunctionBody(New, Orig, VMap);
68         if (New.getLinkage() == GlobalValue::InternalLinkage) {
69           New.setLinkage(GlobalValue::ExternalLinkage);
70           New.setVisibility(GlobalValue::HiddenVisibility);
71         }
72       };
73
74     CloneSubModule(*KVPair.first, M, ExtractGlobalVars, ExtractFunctions,
75                    false);
76   }
77 }
78
79 FullyPartitionedModule fullyPartition(Module &M) {
80   FullyPartitionedModule MP;
81
82   ModulePartitionMap PMap;
83
84   for (auto &F : M) {
85
86     if (F.isDeclaration())
87       continue;
88
89     std::string NewModuleName = (M.getName() + "." + F.getName()).str();
90     MP.Functions.push_back(
91       llvm::make_unique<Module>(NewModuleName, M.getContext()));
92     MP.Functions.back()->setDataLayout(M.getDataLayout());
93     PMap[MP.Functions.back().get()].insert(&F);
94   }
95
96   MP.GlobalVars =
97     llvm::make_unique<Module>((M.getName() + ".globals_and_stubs").str(),
98                               M.getContext());
99   MP.GlobalVars->setDataLayout(M.getDataLayout());
100
101   MP.Commons =
102     llvm::make_unique<Module>((M.getName() + ".commons").str(), M.getContext());
103   MP.Commons->setDataLayout(M.getDataLayout());
104
105   // Make sure there's at least an empty set for the stubs map or we'll fail
106   // to clone anything for it (including the decls).
107   PMap[MP.GlobalVars.get()] = ModulePartitionMap::mapped_type();
108   for (auto &GV : M.globals())
109     if (GV.getLinkage() == GlobalValue::CommonLinkage)
110       PMap[MP.Commons.get()].insert(&GV);
111     else
112       PMap[MP.GlobalVars.get()].insert(&GV);
113
114   partition(M, PMap);
115
116   return MP;
117 }
118
119 } // End namespace orc.
120 } // End namespace llvm.