LCOV - code coverage report
Current view: top level - lib/ExecutionEngine/Orc - IndirectionUtils.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 88 114 77.2 %
Date: 2017-09-14 15:23:50 Functions: 13 17 76.5 %
Legend: Lines: hit not hit

          Line data    Source code
       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           0 : void JITCompileCallbackManager::anchor() {}
      23           0 : void IndirectStubsManager::anchor() {}
      24             : 
      25             : std::unique_ptr<JITCompileCallbackManager>
      26          10 : createLocalCompileCallbackManager(const Triple &T,
      27             :                                   JITTargetAddress ErrorHandlerAddress) {
      28          10 :   switch (T.getArch()) {
      29           0 :     default: return nullptr;
      30             : 
      31           0 :     case Triple::aarch64: {
      32             :       typedef orc::LocalJITCompileCallbackManager<orc::OrcAArch64> CCMgrT;
      33           0 :       return llvm::make_unique<CCMgrT>(ErrorHandlerAddress);
      34             :     }
      35             : 
      36           0 :     case Triple::x86: {
      37             :       typedef orc::LocalJITCompileCallbackManager<orc::OrcI386> CCMgrT;
      38           0 :       return llvm::make_unique<CCMgrT>(ErrorHandlerAddress);
      39             :     }
      40             : 
      41          10 :     case Triple::x86_64: {
      42          10 :       if ( T.getOS() == Triple::OSType::Win32 ) {
      43             :         typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64_Win32> CCMgrT;
      44           0 :         return llvm::make_unique<CCMgrT>(ErrorHandlerAddress);
      45             :       } else {
      46             :         typedef orc::LocalJITCompileCallbackManager<orc::OrcX86_64_SysV> CCMgrT;
      47          40 :         return llvm::make_unique<CCMgrT>(ErrorHandlerAddress);
      48             :       }
      49             :     }
      50             : 
      51             :   }
      52             : }
      53             : 
      54             : std::function<std::unique_ptr<IndirectStubsManager>()>
      55          10 : createLocalIndirectStubsManagerBuilder(const Triple &T) {
      56          10 :   switch (T.getArch()) {
      57           0 :     default: return nullptr;
      58             : 
      59             :     case Triple::aarch64:
      60             :       return [](){
      61             :         return llvm::make_unique<
      62             :                        orc::LocalIndirectStubsManager<orc::OrcAArch64>>();
      63           0 :       };
      64             : 
      65             :     case Triple::x86:
      66             :       return [](){
      67             :         return llvm::make_unique<
      68             :                        orc::LocalIndirectStubsManager<orc::OrcI386>>();
      69           0 :       };
      70             : 
      71          10 :     case Triple::x86_64:
      72          10 :       if (T.getOS() == Triple::OSType::Win32) {
      73             :         return [](){
      74             :           return llvm::make_unique<
      75             :                      orc::LocalIndirectStubsManager<orc::OrcX86_64_Win32>>();
      76           0 :         };
      77             :       } else {
      78             :         return [](){
      79             :           return llvm::make_unique<
      80             :                      orc::LocalIndirectStubsManager<orc::OrcX86_64_SysV>>();
      81          21 :         };
      82             :       }
      83             : 
      84             :   }
      85             : }
      86             : 
      87           0 : Constant* createIRTypedAddress(FunctionType &FT, JITTargetAddress Addr) {
      88             :   Constant *AddrIntVal =
      89           0 :     ConstantInt::get(Type::getInt64Ty(FT.getContext()), Addr);
      90             :   Constant *AddrPtrVal =
      91             :     ConstantExpr::getCast(Instruction::IntToPtr, AddrIntVal,
      92           0 :                           PointerType::get(&FT, 0));
      93           0 :   return AddrPtrVal;
      94             : }
      95             : 
      96           7 : 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           7 :                                GlobalValue::NotThreadLocal, 0, true);
     101          14 :   IP->setVisibility(GlobalValue::HiddenVisibility);
     102           7 :   return IP;
     103             : }
     104             : 
     105           7 : 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           7 :   Module &M = *F.getParent();
     109          14 :   BasicBlock *EntryBlock = BasicBlock::Create(M.getContext(), "entry", &F);
     110          21 :   IRBuilder<> Builder(EntryBlock);
     111           7 :   LoadInst *ImplAddr = Builder.CreateLoad(&ImplPointer);
     112          14 :   std::vector<Value*> CallArgs;
     113          10 :   for (auto &A : F.args())
     114           6 :     CallArgs.push_back(&A);
     115          21 :   CallInst *Call = Builder.CreateCall(ImplAddr, CallArgs);
     116           7 :   Call->setTailCall();
     117          14 :   Call->setAttributes(F.getAttributes());
     118          14 :   if (F.getReturnType()->isVoidTy())
     119           5 :     Builder.CreateRetVoid();
     120             :   else
     121           2 :     Builder.CreateRet(Call);
     122           7 : }
     123             : 
     124             : // Utility class for renaming global values and functions during partitioning.
     125          36 : class GlobalRenamer {
     126             : public:
     127             : 
     128           6 :   static bool needsRenaming(const Value &New) {
     129          12 :     return !New.hasName() || New.getName().startswith("\01L");
     130             :   }
     131             : 
     132           0 :   const std::string& getRename(const Value &Orig) {
     133             :     // See if we have a name for this global.
     134             :     {
     135           0 :       auto I = Names.find(&Orig);
     136           0 :       if (I != Names.end())
     137           0 :         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           0 :     unsigned ID = Names.size();
     144           0 :     std::ostringstream NameStream;
     145           0 :     NameStream << "__orc_anon" << ID++;
     146           0 :     auto I = Names.insert(std::make_pair(&Orig, NameStream.str()));
     147           0 :     return I.first->second;
     148             :   }
     149             : private:
     150             :   DenseMap<const Value*, std::string> Names;
     151             : };
     152             : 
     153          33 : static void raiseVisibilityOnValue(GlobalValue &V, GlobalRenamer &R) {
     154             :   if (V.hasLocalLinkage()) {
     155           6 :     if (R.needsRenaming(V))
     156           0 :       V.setName(R.getRename(V));
     157           6 :     V.setLinkage(GlobalValue::ExternalLinkage);
     158             :     V.setVisibility(GlobalValue::HiddenVisibility);
     159             :   }
     160          33 :   V.setUnnamedAddr(GlobalValue::UnnamedAddr::None);
     161             :   assert(!R.needsRenaming(V) && "Invalid global name.");
     162          33 : }
     163             : 
     164           9 : void makeAllSymbolsExternallyAccessible(Module &M) {
     165          18 :   GlobalRenamer Renamer;
     166             : 
     167          50 :   for (auto &F : M)
     168          23 :     raiseVisibilityOnValue(F, Renamer);
     169             : 
     170          17 :   for (auto &GV : M.globals())
     171           8 :     raiseVisibilityOnValue(GV, Renamer);
     172             : 
     173          11 :   for (auto &A : M.aliases())
     174           2 :     raiseVisibilityOnValue(A, Renamer);
     175           9 : }
     176             : 
     177          29 : 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         116 :     Function::Create(cast<FunctionType>(F.getValueType()),
     182          87 :                      F.getLinkage(), F.getName(), &Dst);
     183          29 :   NewF->copyAttributesFrom(&F);
     184             : 
     185          29 :   if (VMap) {
     186          32 :     (*VMap)[&F] = NewF;
     187          16 :     auto NewArgI = NewF->arg_begin();
     188          45 :     for (auto ArgI = F.arg_begin(), ArgE = F.arg_end(); ArgI != ArgE;
     189             :          ++ArgI, ++NewArgI)
     190          26 :       (*VMap)[&*ArgI] = &*NewArgI;
     191             :   }
     192             : 
     193          29 :   return NewF;
     194             : }
     195             : 
     196          16 : void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap,
     197             :                       ValueMaterializer *Materializer,
     198             :                       Function *NewF) {
     199             :   assert(!OrigF.isDeclaration() && "Nothing to move");
     200          16 :   if (!NewF)
     201          32 :     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          32 :   SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
     210          16 :   CloneFunctionInto(NewF, &OrigF, VMap, /*ModuleLevelChanges=*/true, Returns,
     211             :                     "", nullptr, nullptr, Materializer);
     212          16 :   OrigF.deleteBody();
     213          16 : }
     214             : 
     215          13 : 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          26 :       Dst, GV.getValueType(), GV.isConstant(),
     220          39 :       GV.getLinkage(), nullptr, GV.getName(), nullptr,
     221          65 :       GV.getThreadLocalMode(), GV.getType()->getAddressSpace());
     222          13 :   NewGV->copyAttributesFrom(&GV);
     223          13 :   if (VMap)
     224          14 :     (*VMap)[&GV] = NewGV;
     225          13 :   return NewGV;
     226             : }
     227             : 
     228           7 : void moveGlobalVariableInitializer(GlobalVariable &OrigGV,
     229             :                                    ValueToValueMapTy &VMap,
     230             :                                    ValueMaterializer *Materializer,
     231             :                                    GlobalVariable *NewGV) {
     232             :   assert(OrigGV.hasInitializer() && "Nothing to move");
     233           7 :   if (!NewGV)
     234          14 :     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           7 :   NewGV->setInitializer(MapValue(OrigGV.getInitializer(), VMap, RF_None,
     243             :                                  nullptr, Materializer));
     244           7 : }
     245             : 
     246           2 : GlobalAlias* cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA,
     247             :                                   ValueToValueMapTy &VMap) {
     248             :   assert(OrigA.getAliasee() && "Original alias doesn't have an aliasee?");
     249          10 :   auto *NewA = GlobalAlias::create(OrigA.getValueType(),
     250           4 :                                    OrigA.getType()->getPointerAddressSpace(),
     251           6 :                                    OrigA.getLinkage(), OrigA.getName(), &Dst);
     252           4 :   NewA->copyAttributesFrom(&OrigA);
     253           4 :   VMap[&OrigA] = NewA;
     254           2 :   return NewA;
     255             : }
     256             : 
     257           4 : void cloneModuleFlagsMetadata(Module &Dst, const Module &Src,
     258             :                               ValueToValueMapTy &VMap) {
     259           4 :   auto *MFs = Src.getModuleFlagsMetadata();
     260           4 :   if (!MFs)
     261             :     return;
     262           2 :   for (auto *MF : MFs->operands())
     263           1 :     Dst.addModuleFlag(MapMetadata(MF, VMap));
     264             : }
     265             : 
     266             : } // End namespace orc.
     267             : } // End namespace llvm.

Generated by: LCOV version 1.13