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 #include <sstream> 18 19 namespace llvm { 20 namespace orc { 21 22 Constant* createIRTypedAddress(FunctionType &FT, TargetAddress Addr) { 23 Constant *AddrIntVal = 24 ConstantInt::get(Type::getInt64Ty(FT.getContext()), Addr); 25 Constant *AddrPtrVal = 26 ConstantExpr::getCast(Instruction::IntToPtr, AddrIntVal, 27 PointerType::get(&FT, 0)); 28 return AddrPtrVal; 29 } 30 31 GlobalVariable* createImplPointer(PointerType &PT, Module &M, 32 const Twine &Name, Constant *Initializer) { 33 if (!Initializer) 34 Initializer = Constant::getNullValue(&PT); 35 return new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage, 36 Initializer, Name, nullptr, 37 GlobalValue::NotThreadLocal, 0, true); 38 } 39 40 void makeStub(Function &F, GlobalVariable &ImplPointer) { 41 assert(F.isDeclaration() && "Can't turn a definition into a stub."); 42 assert(F.getParent() && "Function isn't in a module."); 43 Module &M = *F.getParent(); 44 BasicBlock *EntryBlock = BasicBlock::Create(M.getContext(), "entry", &F); 45 IRBuilder<> Builder(EntryBlock); 46 LoadInst *ImplAddr = Builder.CreateLoad(&ImplPointer); 47 std::vector<Value*> CallArgs; 48 for (auto &A : F.args()) 49 CallArgs.push_back(&A); 50 CallInst *Call = Builder.CreateCall(ImplAddr, CallArgs); 51 Call->setTailCall(); 52 Builder.CreateRet(Call); 53 } 54 55 // Utility class for renaming global values and functions during partitioning. 56 class GlobalRenamer { 57 public: 58 59 static bool needsRenaming(const Value &New) { 60 if (!New.hasName() || New.getName().startswith("\01L")) 61 return true; 62 return false; 63 } 64 65 const std::string& getRename(const Value &Orig) { 66 // See if we have a name for this global. 67 { 68 auto I = Names.find(&Orig); 69 if (I != Names.end()) 70 return I->second; 71 } 72 73 // Nope. Create a new one. 74 // FIXME: Use a more robust uniquing scheme. (This may blow up if the user 75 // writes a "__orc_anon[[:digit:]]* method). 76 unsigned ID = Names.size(); 77 std::ostringstream NameStream; 78 NameStream << "__orc_anon" << ID++; 79 auto I = Names.insert(std::make_pair(&Orig, NameStream.str())); 80 return I.first->second; 81 } 82 private: 83 DenseMap<const Value*, std::string> Names; 84 }; 85 86 void partition(Module &M, const ModulePartitionMap &PMap) { 87 88 GlobalRenamer Renamer; 89 90 for (auto &KVPair : PMap) { 91 92 auto ExtractGlobalVars = 93 [&](GlobalVariable &New, const GlobalVariable &Orig, 94 ValueToValueMapTy &VMap) { 95 if (KVPair.second.count(&Orig)) { 96 copyGVInitializer(New, Orig, VMap); 97 } 98 if (New.hasLocalLinkage()) { 99 if (Renamer.needsRenaming(New)) 100 New.setName(Renamer.getRename(Orig)); 101 New.setLinkage(GlobalValue::ExternalLinkage); 102 New.setVisibility(GlobalValue::HiddenVisibility); 103 } 104 assert(!Renamer.needsRenaming(New) && "Invalid global name."); 105 }; 106 107 auto ExtractFunctions = 108 [&](Function &New, const Function &Orig, ValueToValueMapTy &VMap) { 109 if (KVPair.second.count(&Orig)) 110 copyFunctionBody(New, Orig, VMap); 111 if (New.hasLocalLinkage()) { 112 if (Renamer.needsRenaming(New)) 113 New.setName(Renamer.getRename(Orig)); 114 New.setLinkage(GlobalValue::ExternalLinkage); 115 New.setVisibility(GlobalValue::HiddenVisibility); 116 } 117 assert(!Renamer.needsRenaming(New) && "Invalid function name."); 118 }; 119 120 CloneSubModule(*KVPair.first, M, ExtractGlobalVars, ExtractFunctions, 121 false); 122 } 123 } 124 125 FullyPartitionedModule fullyPartition(Module &M) { 126 FullyPartitionedModule MP; 127 128 ModulePartitionMap PMap; 129 130 for (auto &F : M) { 131 132 if (F.isDeclaration()) 133 continue; 134 135 std::string NewModuleName = (M.getName() + "." + F.getName()).str(); 136 MP.Functions.push_back( 137 llvm::make_unique<Module>(NewModuleName, M.getContext())); 138 MP.Functions.back()->setDataLayout(M.getDataLayout()); 139 PMap[MP.Functions.back().get()].insert(&F); 140 } 141 142 MP.GlobalVars = 143 llvm::make_unique<Module>((M.getName() + ".globals_and_stubs").str(), 144 M.getContext()); 145 MP.GlobalVars->setDataLayout(M.getDataLayout()); 146 147 MP.Commons = 148 llvm::make_unique<Module>((M.getName() + ".commons").str(), M.getContext()); 149 MP.Commons->setDataLayout(M.getDataLayout()); 150 151 // Make sure there's at least an empty set for the stubs map or we'll fail 152 // to clone anything for it (including the decls). 153 PMap[MP.GlobalVars.get()] = ModulePartitionMap::mapped_type(); 154 for (auto &GV : M.globals()) 155 if (GV.getLinkage() == GlobalValue::CommonLinkage) 156 PMap[MP.Commons.get()].insert(&GV); 157 else 158 PMap[MP.GlobalVars.get()].insert(&GV); 159 160 partition(M, PMap); 161 162 return MP; 163 } 164 165 } // End namespace orc. 166 } // End namespace llvm. 167