1 //===---- IndirectionUtils.cpp - Utilities for call indirection in Orc ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
21 GlobalVariable* createImplPointer(Function &F, const Twine &Name,
22 Constant *Initializer) {
23 assert(F.getParent() && "Function isn't in a module.");
25 Initializer = Constant::getNullValue(F.getType());
26 Module &M = *F.getParent();
27 return new GlobalVariable(M, F.getType(), false, GlobalValue::ExternalLinkage,
28 Initializer, Name, nullptr,
29 GlobalValue::NotThreadLocal, 0, true);
32 void makeStub(Function &F, GlobalVariable &ImplPointer) {
33 assert(F.isDeclaration() && "Can't turn a definition into a stub.");
34 assert(F.getParent() && "Function isn't in a module.");
35 Module &M = *F.getParent();
36 BasicBlock *EntryBlock = BasicBlock::Create(M.getContext(), "entry", &F);
37 IRBuilder<> Builder(EntryBlock);
38 LoadInst *ImplAddr = Builder.CreateLoad(&ImplPointer);
39 std::vector<Value*> CallArgs;
40 for (auto &A : F.args())
41 CallArgs.push_back(&A);
42 CallInst *Call = Builder.CreateCall(ImplAddr, CallArgs);
44 Builder.CreateRet(Call);
47 void partition(Module &M, const ModulePartitionMap &PMap) {
49 for (auto &KVPair : PMap) {
51 auto ExtractGlobalVars =
52 [&](GlobalVariable &New, const GlobalVariable &Orig,
53 ValueToValueMapTy &VMap) {
54 if (KVPair.second.count(&Orig)) {
55 copyGVInitializer(New, Orig, VMap);
57 if (New.getLinkage() == GlobalValue::PrivateLinkage) {
58 New.setLinkage(GlobalValue::ExternalLinkage);
59 New.setVisibility(GlobalValue::HiddenVisibility);
63 auto ExtractFunctions =
64 [&](Function &New, const Function &Orig, ValueToValueMapTy &VMap) {
65 if (KVPair.second.count(&Orig))
66 copyFunctionBody(New, Orig, VMap);
67 if (New.getLinkage() == GlobalValue::InternalLinkage) {
68 New.setLinkage(GlobalValue::ExternalLinkage);
69 New.setVisibility(GlobalValue::HiddenVisibility);
73 CloneSubModule(*KVPair.first, M, ExtractGlobalVars, ExtractFunctions,
78 FullyPartitionedModule fullyPartition(Module &M) {
79 FullyPartitionedModule MP;
81 ModulePartitionMap PMap;
85 if (F.isDeclaration())
88 std::string NewModuleName = (M.getName() + "." + F.getName()).str();
89 MP.Functions.push_back(
90 llvm::make_unique<Module>(NewModuleName, M.getContext()));
91 MP.Functions.back()->setDataLayout(M.getDataLayout());
92 PMap[MP.Functions.back().get()].insert(&F);
96 llvm::make_unique<Module>((M.getName() + ".globals_and_stubs").str(),
98 MP.GlobalVars->setDataLayout(M.getDataLayout());
101 llvm::make_unique<Module>((M.getName() + ".commons").str(), M.getContext());
102 MP.Commons->setDataLayout(M.getDataLayout());
104 // Make sure there's at least an empty set for the stubs map or we'll fail
105 // to clone anything for it (including the decls).
106 PMap[MP.GlobalVars.get()] = ModulePartitionMap::mapped_type();
107 for (auto &GV : M.globals())
108 if (GV.getLinkage() == GlobalValue::CommonLinkage)
109 PMap[MP.Commons.get()].insert(&GV);
111 PMap[MP.GlobalVars.get()].insert(&GV);
118 } // End namespace orc.
119 } // End namespace llvm.