1 //===---- IndirectionUtils.cpp - Utilities for call indirection in Orc ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
22 void JITCompileCallbackManagerBase::anchor() {}
23 void IndirectStubsManagerBase::anchor() {}
25 Constant* createIRTypedAddress(FunctionType &FT, TargetAddress Addr) {
26 Constant *AddrIntVal =
27 ConstantInt::get(Type::getInt64Ty(FT.getContext()), Addr);
28 Constant *AddrPtrVal =
29 ConstantExpr::getCast(Instruction::IntToPtr, AddrIntVal,
30 PointerType::get(&FT, 0));
34 GlobalVariable* createImplPointer(PointerType &PT, Module &M,
35 const Twine &Name, Constant *Initializer) {
36 auto IP = new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage,
37 Initializer, Name, nullptr,
38 GlobalValue::NotThreadLocal, 0, true);
39 IP->setVisibility(GlobalValue::HiddenVisibility);
43 void makeStub(Function &F, Value &ImplPointer) {
44 assert(F.isDeclaration() && "Can't turn a definition into a stub.");
45 assert(F.getParent() && "Function isn't in a module.");
46 Module &M = *F.getParent();
47 BasicBlock *EntryBlock = BasicBlock::Create(M.getContext(), "entry", &F);
48 IRBuilder<> Builder(EntryBlock);
49 LoadInst *ImplAddr = Builder.CreateLoad(&ImplPointer);
50 std::vector<Value*> CallArgs;
51 for (auto &A : F.args())
52 CallArgs.push_back(&A);
53 CallInst *Call = Builder.CreateCall(ImplAddr, CallArgs);
55 Call->setAttributes(F.getAttributes());
56 if (F.getReturnType()->isVoidTy())
57 Builder.CreateRetVoid();
59 Builder.CreateRet(Call);
62 // Utility class for renaming global values and functions during partitioning.
66 static bool needsRenaming(const Value &New) {
67 if (!New.hasName() || New.getName().startswith("\01L"))
72 const std::string& getRename(const Value &Orig) {
73 // See if we have a name for this global.
75 auto I = Names.find(&Orig);
80 // Nope. Create a new one.
81 // FIXME: Use a more robust uniquing scheme. (This may blow up if the user
82 // writes a "__orc_anon[[:digit:]]* method).
83 unsigned ID = Names.size();
84 std::ostringstream NameStream;
85 NameStream << "__orc_anon" << ID++;
86 auto I = Names.insert(std::make_pair(&Orig, NameStream.str()));
87 return I.first->second;
90 DenseMap<const Value*, std::string> Names;
93 static void raiseVisibilityOnValue(GlobalValue &V, GlobalRenamer &R) {
94 if (V.hasLocalLinkage()) {
95 if (R.needsRenaming(V))
96 V.setName(R.getRename(V));
97 V.setLinkage(GlobalValue::ExternalLinkage);
98 V.setVisibility(GlobalValue::HiddenVisibility);
100 V.setUnnamedAddr(false);
101 assert(!R.needsRenaming(V) && "Invalid global name.");
104 void makeAllSymbolsExternallyAccessible(Module &M) {
105 GlobalRenamer Renamer;
108 raiseVisibilityOnValue(F, Renamer);
110 for (auto &GV : M.globals())
111 raiseVisibilityOnValue(GV, Renamer);
113 for (auto &A : M.aliases())
114 raiseVisibilityOnValue(A, Renamer);
117 Function* cloneFunctionDecl(Module &Dst, const Function &F,
118 ValueToValueMapTy *VMap) {
119 assert(F.getParent() != &Dst && "Can't copy decl over existing function.");
121 Function::Create(cast<FunctionType>(F.getType()->getElementType()),
122 F.getLinkage(), F.getName(), &Dst);
123 NewF->copyAttributesFrom(&F);
127 auto NewArgI = NewF->arg_begin();
128 for (auto ArgI = F.arg_begin(), ArgE = F.arg_end(); ArgI != ArgE;
130 (*VMap)[&*ArgI] = &*NewArgI;
136 void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap,
137 ValueMaterializer *Materializer,
139 assert(!OrigF.isDeclaration() && "Nothing to move");
141 NewF = cast<Function>(VMap[&OrigF]);
143 assert(VMap[&OrigF] == NewF && "Incorrect function mapping in VMap.");
144 assert(NewF && "Function mapping missing from VMap.");
145 assert(NewF->getParent() != OrigF.getParent() &&
146 "moveFunctionBody should only be used to move bodies between "
149 SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
150 CloneFunctionInto(NewF, &OrigF, VMap, /*ModuleLevelChanges=*/true, Returns,
151 "", nullptr, nullptr, Materializer);
155 GlobalVariable* cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV,
156 ValueToValueMapTy *VMap) {
157 assert(GV.getParent() != &Dst && "Can't copy decl over existing global var.");
158 GlobalVariable *NewGV = new GlobalVariable(
159 Dst, GV.getType()->getElementType(), GV.isConstant(),
160 GV.getLinkage(), nullptr, GV.getName(), nullptr,
161 GV.getThreadLocalMode(), GV.getType()->getAddressSpace());
162 NewGV->copyAttributesFrom(&GV);
164 (*VMap)[&GV] = NewGV;
168 void moveGlobalVariableInitializer(GlobalVariable &OrigGV,
169 ValueToValueMapTy &VMap,
170 ValueMaterializer *Materializer,
171 GlobalVariable *NewGV) {
172 assert(OrigGV.hasInitializer() && "Nothing to move");
174 NewGV = cast<GlobalVariable>(VMap[&OrigGV]);
176 assert(VMap[&OrigGV] == NewGV &&
177 "Incorrect global variable mapping in VMap.");
178 assert(NewGV->getParent() != OrigGV.getParent() &&
179 "moveGlobalVariable should only be used to move initializers between "
182 NewGV->setInitializer(MapValue(OrigGV.getInitializer(), VMap, RF_None,
183 nullptr, Materializer));
186 GlobalAlias* cloneGlobalAliasDecl(Module &Dst, const GlobalAlias &OrigA,
187 ValueToValueMapTy &VMap) {
188 assert(OrigA.getAliasee() && "Original alias doesn't have an aliasee?");
189 auto *NewA = GlobalAlias::create(OrigA.getValueType(),
190 OrigA.getType()->getPointerAddressSpace(),
191 OrigA.getLinkage(), OrigA.getName(), &Dst);
192 NewA->copyAttributesFrom(&OrigA);
197 } // End namespace orc.
198 } // End namespace llvm.