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 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));
30 GlobalVariable* createImplPointer(PointerType &PT, Module &M,
31 const Twine &Name, Constant *Initializer) {
33 Initializer = Constant::getNullValue(&PT);
34 return new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage,
35 Initializer, Name, nullptr,
36 GlobalValue::NotThreadLocal, 0, true);
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);
51 Builder.CreateRet(Call);
54 void partition(Module &M, const ModulePartitionMap &PMap) {
56 for (auto &KVPair : PMap) {
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);
65 if (New.hasLocalLinkage()) {
66 New.setLinkage(GlobalValue::ExternalLinkage);
67 New.setVisibility(GlobalValue::HiddenVisibility);
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);
82 CloneSubModule(*KVPair.first, M, ExtractGlobalVars, ExtractFunctions,
87 FullyPartitionedModule fullyPartition(Module &M) {
88 FullyPartitionedModule MP;
90 ModulePartitionMap PMap;
94 if (F.isDeclaration())
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);
105 llvm::make_unique<Module>((M.getName() + ".globals_and_stubs").str(),
107 MP.GlobalVars->setDataLayout(M.getDataLayout());
110 llvm::make_unique<Module>((M.getName() + ".commons").str(), M.getContext());
111 MP.Commons->setDataLayout(M.getDataLayout());
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);
120 PMap[MP.GlobalVars.get()].insert(&GV);
127 } // End namespace orc.
128 } // End namespace llvm.