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