blob: ddd1921657b3324fe5f5d7a03b9b764150b3dfe0 [file] [log] [blame]
Lang Hames53ccf882015-02-22 01:45:31 +00001//===---- 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
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000010#include "llvm/ADT/STLExtras.h"
Lang Hames93de2a12015-01-23 21:25:00 +000011#include "llvm/ADT/Triple.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000012#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
Lang Hames93de2a12015-01-23 21:25:00 +000013#include "llvm/IR/CallSite.h"
14#include "llvm/IR/IRBuilder.h"
Lang Hamescd68eba2015-05-05 17:37:18 +000015#include "llvm/Transforms/Utils/Cloning.h"
Lang Hames93de2a12015-01-23 21:25:00 +000016#include <set>
Lang Hamesc6de4582015-04-12 20:05:51 +000017#include <sstream>
Lang Hames93de2a12015-01-23 21:25:00 +000018
Lang Hames93de2a12015-01-23 21:25:00 +000019namespace llvm {
Lang Hamese7380612015-02-21 20:44:36 +000020namespace orc {
Lang Hames93de2a12015-01-23 21:25:00 +000021
Lang Hameseb9bdb52015-04-11 00:23:49 +000022Constant* createIRTypedAddress(FunctionType &FT, TargetAddress Addr) {
23 Constant *AddrIntVal =
24 ConstantInt::get(Type::getInt64Ty(FT.getContext()), Addr);
25 Constant *AddrPtrVal =
26 ConstantExpr::getCast(Instruction::IntToPtr, AddrIntVal,
27 PointerType::get(&FT, 0));
28 return AddrPtrVal;
29}
30
31GlobalVariable* createImplPointer(PointerType &PT, Module &M,
32 const Twine &Name, Constant *Initializer) {
Lang Hamescd68eba2015-05-05 17:37:18 +000033 auto IP = new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage,
34 Initializer, Name, nullptr,
35 GlobalValue::NotThreadLocal, 0, true);
36 IP->setVisibility(GlobalValue::HiddenVisibility);
37 return IP;
Lang Hames93de2a12015-01-23 21:25:00 +000038}
39
Lang Hames27547142015-02-17 01:18:38 +000040void makeStub(Function &F, GlobalVariable &ImplPointer) {
41 assert(F.isDeclaration() && "Can't turn a definition into a stub.");
42 assert(F.getParent() && "Function isn't in a module.");
43 Module &M = *F.getParent();
44 BasicBlock *EntryBlock = BasicBlock::Create(M.getContext(), "entry", &F);
45 IRBuilder<> Builder(EntryBlock);
46 LoadInst *ImplAddr = Builder.CreateLoad(&ImplPointer);
47 std::vector<Value*> CallArgs;
48 for (auto &A : F.args())
49 CallArgs.push_back(&A);
50 CallInst *Call = Builder.CreateCall(ImplAddr, CallArgs);
51 Call->setTailCall();
Lang Hamesdc4260d2015-04-20 20:41:45 +000052 Call->setAttributes(F.getAttributes());
Lang Hamescd68eba2015-05-05 17:37:18 +000053 if (F.getReturnType()->isVoidTy())
54 Builder.CreateRetVoid();
55 else
56 Builder.CreateRet(Call);
Lang Hames93de2a12015-01-23 21:25:00 +000057}
58
Lang Hamesc6de4582015-04-12 20:05:51 +000059// Utility class for renaming global values and functions during partitioning.
60class GlobalRenamer {
61public:
62
63 static bool needsRenaming(const Value &New) {
64 if (!New.hasName() || New.getName().startswith("\01L"))
65 return true;
66 return false;
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 }
86private:
87 DenseMap<const Value*, std::string> Names;
88};
89
Lang Hamescd68eba2015-05-05 17:37:18 +000090static 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);
Lang Hamesac31a1f2015-05-04 23:30:01 +000096 }
Lang Hamescd68eba2015-05-05 17:37:18 +000097 V.setUnnamedAddr(false);
98 assert(!R.needsRenaming(V) && "Invalid global name.");
Lang Hamesa68970d2015-05-04 22:03:10 +000099}
Lang Hames27547142015-02-17 01:18:38 +0000100
Lang Hamescd68eba2015-05-05 17:37:18 +0000101void makeAllSymbolsExternallyAccessible(Module &M) {
102 GlobalRenamer Renamer;
Lang Hames93de2a12015-01-23 21:25:00 +0000103
Lang Hamescd68eba2015-05-05 17:37:18 +0000104 for (auto &F : M)
105 raiseVisibilityOnValue(F, Renamer);
Lang Hamesac31a1f2015-05-04 23:30:01 +0000106
Lang Hamescd68eba2015-05-05 17:37:18 +0000107 for (auto &GV : M.globals())
108 raiseVisibilityOnValue(GV, Renamer);
109}
Lang Hamesac31a1f2015-05-04 23:30:01 +0000110
Lang Hamescd68eba2015-05-05 17:37:18 +0000111Function* cloneFunctionDecl(Module &Dst, const Function &F,
112 ValueToValueMapTy *VMap) {
113 assert(F.getParent() != &Dst && "Can't copy decl over existing function.");
114 Function *NewF =
115 Function::Create(cast<FunctionType>(F.getType()->getElementType()),
116 F.getLinkage(), F.getName(), &Dst);
117 NewF->copyAttributesFrom(&F);
Lang Hamesac31a1f2015-05-04 23:30:01 +0000118
Lang Hamescd68eba2015-05-05 17:37:18 +0000119 if (VMap) {
120 (*VMap)[&F] = NewF;
121 auto NewArgI = NewF->arg_begin();
122 for (auto ArgI = F.arg_begin(), ArgE = F.arg_end(); ArgI != ArgE;
123 ++ArgI, ++NewArgI)
124 (*VMap)[ArgI] = NewArgI;
Lang Hames93de2a12015-01-23 21:25:00 +0000125 }
126
Lang Hamescd68eba2015-05-05 17:37:18 +0000127 return NewF;
128}
Lang Hames27547142015-02-17 01:18:38 +0000129
Lang Hamescd68eba2015-05-05 17:37:18 +0000130void moveFunctionBody(Function &OrigF, ValueToValueMapTy &VMap,
131 ValueMaterializer *Materializer,
132 Function *NewF) {
133 assert(!OrigF.isDeclaration() && "Nothing to move");
134 if (!NewF)
135 NewF = cast<Function>(VMap[&OrigF]);
136 else
137 assert(VMap[&OrigF] == NewF && "Incorrect function mapping in VMap.");
138 assert(NewF && "Function mapping missing from VMap.");
139 assert(NewF->getParent() != OrigF.getParent() &&
140 "moveFunctionBody should only be used to move bodies between "
141 "modules.");
Lang Hames27547142015-02-17 01:18:38 +0000142
Lang Hamescd68eba2015-05-05 17:37:18 +0000143 SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
144 CloneFunctionInto(NewF, &OrigF, VMap, /*ModuleLevelChanges=*/true, Returns,
145 "", nullptr, nullptr, Materializer);
146 OrigF.deleteBody();
147}
Lang Hames27547142015-02-17 01:18:38 +0000148
Lang Hamescd68eba2015-05-05 17:37:18 +0000149GlobalVariable* cloneGlobalVariableDecl(Module &Dst, const GlobalVariable &GV,
150 ValueToValueMapTy *VMap) {
151 assert(GV.getParent() != &Dst && "Can't copy decl over existing global var.");
152 GlobalVariable *NewGV = new GlobalVariable(
153 Dst, GV.getType()->getElementType(), GV.isConstant(),
154 GV.getLinkage(), nullptr, GV.getName(), nullptr,
155 GV.getThreadLocalMode(), GV.getType()->getAddressSpace());
156 NewGV->copyAttributesFrom(&GV);
157 if (VMap)
158 (*VMap)[&GV] = NewGV;
159 return NewGV;
160}
Lang Hames27547142015-02-17 01:18:38 +0000161
Lang Hamescd68eba2015-05-05 17:37:18 +0000162void moveGlobalVariableInitializer(GlobalVariable &OrigGV,
163 ValueToValueMapTy &VMap,
164 ValueMaterializer *Materializer,
165 GlobalVariable *NewGV) {
166 assert(OrigGV.hasInitializer() && "Nothing to move");
167 if (!NewGV)
168 NewGV = cast<GlobalVariable>(VMap[&OrigGV]);
169 else
170 assert(VMap[&OrigGV] == NewGV &&
171 "Incorrect global variable mapping in VMap.");
172 assert(NewGV->getParent() != OrigGV.getParent() &&
173 "moveGlobalVariable should only be used to move initializers between "
174 "modules");
175
176 NewGV->setInitializer(MapValue(OrigGV.getInitializer(), VMap, RF_None,
177 nullptr, Materializer));
Lang Hames93de2a12015-01-23 21:25:00 +0000178}
179
Lang Hames44780ac2015-10-06 22:55:05 +0000180GlobalAlias* cloneGlobalAlias(Module &Dst, const GlobalAlias &OrigA,
181 ValueToValueMapTy &VMap,
182 ValueMaterializer *Materializer) {
183 assert(OrigA.getAliasee() && "Original alias doesn't have an aliasee?");
184 auto *NewA = GlobalAlias::create(OrigA.getValueType(),
185 OrigA.getType()->getPointerAddressSpace(),
186 OrigA.getLinkage(), OrigA.getName(), &Dst);
187 NewA->copyAttributesFrom(&OrigA);
188 VMap[&OrigA] = NewA;
189 NewA->setAliasee(cast<Constant>(MapValue(OrigA.getAliasee(), VMap, RF_None,
190 nullptr, Materializer)));
191 return NewA;
192}
193
Lang Hamese7380612015-02-21 20:44:36 +0000194} // End namespace orc.
195} // End namespace llvm.