blob: 74766450e8c416633b7b0ecac1501ee253dd8897 [file] [log] [blame]
Lang Hames53ccf882015-02-22 01:45:31 +00001//===---- 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
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000010#include "llvm/ADT/STLExtras.h"
Lang Hames93de2a12015-01-23 21:25:00 +000011#include "llvm/ADT/Triple.h"
Lang Hames93de2a12015-01-23 21:25:00 +000012#include "llvm/ExecutionEngine/Orc/CloneSubModule.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000013#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
Lang Hames93de2a12015-01-23 21:25:00 +000014#include "llvm/IR/CallSite.h"
15#include "llvm/IR/IRBuilder.h"
16#include <set>
17
Lang Hames93de2a12015-01-23 21:25:00 +000018namespace llvm {
Lang Hamese7380612015-02-21 20:44:36 +000019namespace orc {
Lang Hames93de2a12015-01-23 21:25:00 +000020
Lang Hames27547142015-02-17 01:18:38 +000021GlobalVariable* createImplPointer(Function &F, const Twine &Name,
22 Constant *Initializer) {
23 assert(F.getParent() && "Function isn't in a module.");
24 if (!Initializer)
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);
Lang Hames93de2a12015-01-23 21:25:00 +000030}
31
Lang Hames27547142015-02-17 01:18:38 +000032void 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);
43 Call->setTailCall();
44 Builder.CreateRet(Call);
Lang Hames93de2a12015-01-23 21:25:00 +000045}
46
Lang Hames27547142015-02-17 01:18:38 +000047void partition(Module &M, const ModulePartitionMap &PMap) {
Lang Hames93de2a12015-01-23 21:25:00 +000048
Lang Hames27547142015-02-17 01:18:38 +000049 for (auto &KVPair : PMap) {
Lang Hames93de2a12015-01-23 21:25:00 +000050
Lang Hames27547142015-02-17 01:18:38 +000051 auto ExtractGlobalVars =
52 [&](GlobalVariable &New, const GlobalVariable &Orig,
53 ValueToValueMapTy &VMap) {
54 if (KVPair.second.count(&Orig)) {
55 copyGVInitializer(New, Orig, VMap);
56 }
Lang Hames2f507442015-04-02 05:28:10 +000057 if (New.hasLocalLinkage()) {
Lang Hames27547142015-02-17 01:18:38 +000058 New.setLinkage(GlobalValue::ExternalLinkage);
59 New.setVisibility(GlobalValue::HiddenVisibility);
60 }
Lang Hames93de2a12015-01-23 21:25:00 +000061 };
62
Lang Hames27547142015-02-17 01:18:38 +000063 auto ExtractFunctions =
64 [&](Function &New, const Function &Orig, ValueToValueMapTy &VMap) {
65 if (KVPair.second.count(&Orig))
66 copyFunctionBody(New, Orig, VMap);
Lang Hames2f507442015-04-02 05:28:10 +000067 if (New.hasLocalLinkage()) {
Lang Hames27547142015-02-17 01:18:38 +000068 New.setLinkage(GlobalValue::ExternalLinkage);
69 New.setVisibility(GlobalValue::HiddenVisibility);
70 }
71 };
Lang Hames93de2a12015-01-23 21:25:00 +000072
Lang Hames27547142015-02-17 01:18:38 +000073 CloneSubModule(*KVPair.first, M, ExtractGlobalVars, ExtractFunctions,
74 false);
75 }
76}
Lang Hames93de2a12015-01-23 21:25:00 +000077
Lang Hames27547142015-02-17 01:18:38 +000078FullyPartitionedModule fullyPartition(Module &M) {
79 FullyPartitionedModule MP;
80
81 ModulePartitionMap PMap;
82
83 for (auto &F : M) {
84
85 if (F.isDeclaration())
Lang Hames93de2a12015-01-23 21:25:00 +000086 continue;
87
Lang Hames27547142015-02-17 01:18:38 +000088 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);
Lang Hames93de2a12015-01-23 21:25:00 +000093 }
94
Lang Hames27547142015-02-17 01:18:38 +000095 MP.GlobalVars =
96 llvm::make_unique<Module>((M.getName() + ".globals_and_stubs").str(),
97 M.getContext());
98 MP.GlobalVars->setDataLayout(M.getDataLayout());
99
100 MP.Commons =
101 llvm::make_unique<Module>((M.getName() + ".commons").str(), M.getContext());
102 MP.Commons->setDataLayout(M.getDataLayout());
103
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);
110 else
111 PMap[MP.GlobalVars.get()].insert(&GV);
112
113 partition(M, PMap);
114
115 return MP;
Lang Hames93de2a12015-01-23 21:25:00 +0000116}
117
Lang Hamese7380612015-02-21 20:44:36 +0000118} // End namespace orc.
119} // End namespace llvm.