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