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/ExecutionEngine/Orc/IndirectionUtils.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/Triple.h" 13 #include "llvm/ExecutionEngine/Orc/OrcABISupport.h" 14 #include "llvm/IR/CallSite.h" 15 #include "llvm/IR/IRBuilder.h" 16 #include "llvm/Transforms/Utils/Cloning.h" 17 #include <sstream> 18 19 namespace llvm { 20 namespace orc { 21 22 void JITCompileCallbackManager::anchor() {} 23 void IndirectStubsManager::anchor() {} 24 25 std::unique_ptr<JITCompileCallbackManager> 26 createLocalCompileCallbackManager(const Triple &T, 27 JITTargetAddress ErrorHandlerAddress) { 28 switch (T.getArch()) { 29 default: return nullptr; 30 31 case Triple::aarch64: { 32 typedef orc::LocalJITCompileCallbackManager<orc::OrcAArch64> CCMgrT; 33 return llvm::make_unique<CCMgrT>(ErrorHandlerAddress); 34 } 35 36 case Triple::x86: { 37 typedef orc::LocalJITCompileCallbackManager<orc::OrcI386> CCMgrT; 38 return llvm::make_unique<CCMgrT>(ErrorHandlerAddress); 39 } 40 41 case Triple::x86_64: { 42 if ( T.getOS() == Triple::OSType::Win32 ) { 43 typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64_Win32> CCMgrT; 44 return llvm::make_unique<CCMgrT>(ErrorHandlerAddress); 45 } else { 46 typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64_SysV> CCMgrT; 47 return llvm::make_unique<CCMgrT>(ErrorHandlerAddress); 48 } 49 } 50 51 } 52 } 53 54 std::function<std::unique_ptr<IndirectStubsManager>()> 55 createLocalIndirectStubsManagerBuilder(const Triple &T) { 56 switch (T.getArch()) { 57 default: return nullptr; 58 59 case Triple::aarch64: 60 return [](){ 61 return llvm::make_unique< 62 orc::LocalIndirectStubsManager<orc::OrcAArch64>>(); 63 }; 64 65 case Triple::x86: 66 return [](){ 67 return llvm::make_unique< 68 orc::LocalIndirectStubsManager<orc::OrcI386>>(); 69 }; 70 71 case Triple::x86_64: 72 if (T.getOS() == Triple::OSType::Win32) { 73 return [](){ 74 return llvm::make_unique< 75 orc::LocalIndirectStubsManager<orc::OrcX86_64_Win32>>(); 76 }; 77 } else { 78 return [](){ 79 return llvm::make_unique< 80 orc::LocalIndirectStubsManager<orc::OrcX86_64_SysV>>(); 81 }; 82 } 83 84 } 85 } 86 87 Constant* createIRTypedAddress(FunctionType &FT, JITTargetAddress Addr) { 88 Constant *AddrIntVal = 89 ConstantInt::get(Type::getInt64Ty(FT.getContext()), Addr); 90 Constant *AddrPtrVal = 91 ConstantExpr::getCast(Instruction::IntToPtr, AddrIntVal, 92 PointerType::get(&FT, 0)); 93 return AddrPtrVal; 94 } 95 96 GlobalVariable* createImplPointer(PointerType &PT, Module &M, 97 const Twine &Name, Constant *Initializer) { 98 auto IP = new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage, 99 Initializer, Name, nullptr, 100 GlobalValue::NotThreadLocal, 0, true); 101 IP->setVisibility(GlobalValue::HiddenVisibility); 102 return IP; 103 } 104 105 void makeStub(Function &F, Value &ImplPointer) { 106 assert(F.isDeclaration() && "Can't turn a definition into a stub."); 107 assert(F.getParent() && "Function isn't in a module."); 108 Module &M = *F.getParent(); 109 BasicBlock *EntryBlock = BasicBlock::Create(M.getContext(), "entry", &F); 110 IRBuilder<> Builder(EntryBlock); 111 LoadInst *ImplAddr = Builder.CreateLoad(&ImplPointer); 112 std::vector<Value*> CallArgs; 113 for (auto &A : F.args()) 114 CallArgs.push_back(&A); 115 CallInst *Call = Builder.CreateCall(ImplAddr, CallArgs); 116 Call->setTailCall(); 117 Call->setAttributes(F.getAttributes()); 118 if (F.getReturnType()->isVoidTy()) 119 Builder.CreateRetVoid(); 120 else 121 Builder.CreateRet(Call); 122 } 123 124 // Utility class for renaming global values and functions during partitioning. 125 class GlobalRenamer { 126 public: 127 128 static bool needsRenaming(const Value &New) { 129 return !New.hasName() || New.getName().startswith("\01L"); 130 } 131 132 const std::string& getRename(const Value &Orig) { 133 // See if we have a name for this global. 134 { 135 auto I = Names.find(&Orig); 136 if (I != Names.end()) 137 return I->second; 138 } 139 140 // Nope. Create a new one. 141 // FIXME: Use a more robust uniquing scheme. (This may blow up if the user 142 // writes a "__orc_anon[[:digit:]]* method). 143 unsigned ID = Names.size(); 144 std::ostringstream NameStream; 145 NameStream << "__orc_anon" << ID++; 146 auto I = Names.insert(std::make_pair(&Orig, NameStream.str())); 147 return I.first->second; 148 } 149 private: 150 DenseMap<const Value*, std::string> Names; 151 }; 152 153 static void raiseVisibilityOnValue(GlobalValue &V, GlobalRenamer &R) { 154 if (V.hasLocalLinkage()) { 155 if (R.needsRenaming(V)) 156 V.setName(R.getRename(V)); 157 V.setLinkage(GlobalValue::ExternalLinkage); 158 V.setVisibility(GlobalValue::HiddenVisibility); 159 } 160 V.setUnnamedAddr(GlobalValue::UnnamedAddr::None); 161 assert(!R.needsRenaming(V) && "Invalid global name."); 162 } 163 164 void makeAllSymbolsExternallyAccessible(Module &M) { 165 GlobalRenamer Renamer; 166 167 for (auto &F : M) 168 raiseVisibilityOnValue(F, Renamer); 169 170 for (auto &GV : M.globals()) 171 raiseVisibilityOnValue(GV, Renamer); 172 173 for (auto &A : M.aliases()) 174 raiseVisibilityOnValue(A, Renamer); 175 } 176 177 Function* cloneFunctionDecl(Module &Dst, const Function &F, 178 ValueToValueMapTy *VMap) { 179 assert(F.getParent() != &Dst && "Can't copy decl over existing function."); 180 Function *NewF = 181 Function::Create(cast<FunctionType>(F.getValueType()), 182 F.getLinkage(), F.getName(), &Dst); 183 NewF->copyAttributesFrom(&F); 184 185 if (VMap) { 186 (*VMap)[&F] = NewF; 187 auto NewArgI = NewF->arg_begin(); 188 for (auto ArgI = F.arg_begin(), ArgE = F.arg_end(); ArgI != ArgE; 189 ++ArgI, ++NewArgI) 190 (*VMap)[&*ArgI] = &*NewArgI; 191 } 192 193 return NewF; 194 } 195 196 void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap, 197 ValueMaterializer *Materializer, 198 Function *NewF) { 199 assert(!OrigF.isDeclaration() && "Nothing to move"); 200 if (!NewF) 201 NewF = cast<Function>(VMap[&OrigF]); 202 else 203 assert(VMap[&OrigF] == NewF && "Incorrect function mapping in VMap."); 204 assert(NewF && "Function mapping missing from VMap."); 205 assert(NewF->getParent() != OrigF.getParent() && 206 "moveFunctionBody should only be used to move bodies between " 207 "modules."); 208 209 SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned. 210 CloneFunctionInto(NewF, &OrigF, VMap, /*ModuleLevelChanges=*/true, Returns, 211 "", nullptr, nullptr, Materializer); 212 OrigF.deleteBody(); 213 } 214 215 GlobalVariable* cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV, 216 ValueToValueMapTy *VMap) { 217 assert(GV.getParent() != &Dst && "Can't copy decl over existing global var."); 218 GlobalVariable *NewGV = new GlobalVariable( 219 Dst, GV.getValueType(), GV.isConstant(), 220 GV.getLinkage(), nullptr, GV.getName(), nullptr, 221 GV.getThreadLocalMode(), GV.getType()->getAddressSpace()); 222 NewGV->copyAttributesFrom(&GV); 223 if (VMap) 224 (*VMap)[&GV] = NewGV; 225 return NewGV; 226 } 227 228 void moveGlobalVariableInitializer(GlobalVariable &OrigGV, 229 ValueToValueMapTy &VMap, 230 ValueMaterializer *Materializer, 231 GlobalVariable *NewGV) { 232 assert(OrigGV.hasInitializer() && "Nothing to move"); 233 if (!NewGV) 234 NewGV = cast<GlobalVariable>(VMap[&OrigGV]); 235 else 236 assert(VMap[&OrigGV] == NewGV && 237 "Incorrect global variable mapping in VMap."); 238 assert(NewGV->getParent() != OrigGV.getParent() && 239 "moveGlobalVariable should only be used to move initializers between " 240 "modules"); 241 242 NewGV->setInitializer(MapValue(OrigGV.getInitializer(), VMap, RF_None, 243 nullptr, Materializer)); 244 } 245 246 GlobalAlias* cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA, 247 ValueToValueMapTy &VMap) { 248 assert(OrigA.getAliasee() && "Original alias doesn't have an aliasee?"); 249 auto *NewA = GlobalAlias::create(OrigA.getValueType(), 250 OrigA.getType()->getPointerAddressSpace(), 251 OrigA.getLinkage(), OrigA.getName(), &Dst); 252 NewA->copyAttributesFrom(&OrigA); 253 VMap[&OrigA] = NewA; 254 return NewA; 255 } 256 257 void cloneModuleFlagsMetadata(Module &Dst, const Module &Src, 258 ValueToValueMapTy &VMap) { 259 auto *MFs = Src.getModuleFlagsMetadata(); 260 if (!MFs) 261 return; 262 for (auto *MF : MFs->operands()) 263 Dst.addModuleFlag(MapMetadata(MF, VMap)); 264 } 265 266 } // End namespace orc. 267 } // End namespace llvm. 268