IndirectionUtils.cpp 6.1 KB

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