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 GlobalVariable* 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); 30 } 31 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); 43 Call->setTailCall(); 44 Builder.CreateRet(Call); 45 } 46 47 void partition(Module &M, const ModulePartitionMap &PMap) { 48 49 for (auto &KVPair : PMap) { 50 51 auto ExtractGlobalVars = 52 [&](GlobalVariable &New, const GlobalVariable &Orig, 53 ValueToValueMapTy &VMap) { 54 if (KVPair.second.count(&Orig)) { 55 copyGVInitializer(New, Orig, VMap); 56 } 57 if (New.hasLocalLinkage()) { 58 New.setLinkage(GlobalValue::ExternalLinkage); 59 New.setVisibility(GlobalValue::HiddenVisibility); 60 } 61 }; 62 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.hasLocalLinkage()) { 68 New.setLinkage(GlobalValue::ExternalLinkage); 69 New.setVisibility(GlobalValue::HiddenVisibility); 70 } 71 }; 72 73 CloneSubModule(*KVPair.first, M, ExtractGlobalVars, ExtractFunctions, 74 false); 75 } 76 } 77 78 FullyPartitionedModule fullyPartition(Module &M) { 79 FullyPartitionedModule MP; 80 81 ModulePartitionMap PMap; 82 83 for (auto &F : M) { 84 85 if (F.isDeclaration()) 86 continue; 87 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); 93 } 94 95 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; 116 } 117 118 } // End namespace orc. 119 } // End namespace llvm. 120