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/CloneSubModule.h"
13 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
14 #include "llvm/IR/CallSite.h"
15 #include "llvm/IR/IRBuilder.h"
22 Constant* 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));
31 GlobalVariable* createImplPointer(PointerType &PT, Module &M,
32 const Twine &Name, Constant *Initializer) {
34 Initializer = Constant::getNullValue(&PT);
35 return new GlobalVariable(M, &PT, false, GlobalValue::ExternalLinkage,
36 Initializer, Name, nullptr,
37 GlobalValue::NotThreadLocal, 0, true);
40 void 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);
52 Builder.CreateRet(Call);
55 // Utility class for renaming global values and functions during partitioning.
59 static bool needsRenaming(const Value &New) {
60 if (!New.hasName() || New.getName().startswith("\01L"))
65 const std::string& getRename(const Value &Orig) {
66 // See if we have a name for this global.
68 auto I = Names.find(&Orig);
73 // Nope. Create a new one.
74 // FIXME: Use a more robust uniquing scheme. (This may blow up if the user
75 // writes a "__orc_anon[[:digit:]]* method).
76 unsigned ID = Names.size();
77 std::ostringstream NameStream;
78 NameStream << "__orc_anon" << ID++;
79 auto I = Names.insert(std::make_pair(&Orig, NameStream.str()));
80 return I.first->second;
83 DenseMap<const Value*, std::string> Names;
86 void partition(Module &M, const ModulePartitionMap &PMap) {
88 GlobalRenamer Renamer;
90 for (auto &KVPair : PMap) {
92 auto ExtractGlobalVars =
93 [&](GlobalVariable &New, const GlobalVariable &Orig,
94 ValueToValueMapTy &VMap) {
95 if (KVPair.second.count(&Orig)) {
96 copyGVInitializer(New, Orig, VMap);
98 if (New.hasLocalLinkage()) {
99 if (Renamer.needsRenaming(New))
100 New.setName(Renamer.getRename(Orig));
101 New.setLinkage(GlobalValue::ExternalLinkage);
102 New.setVisibility(GlobalValue::HiddenVisibility);
104 assert(!Renamer.needsRenaming(New) && "Invalid global name.");
107 auto ExtractFunctions =
108 [&](Function &New, const Function &Orig, ValueToValueMapTy &VMap) {
109 if (KVPair.second.count(&Orig))
110 copyFunctionBody(New, Orig, VMap);
111 if (New.hasLocalLinkage()) {
112 if (Renamer.needsRenaming(New))
113 New.setName(Renamer.getRename(Orig));
114 New.setLinkage(GlobalValue::ExternalLinkage);
115 New.setVisibility(GlobalValue::HiddenVisibility);
117 assert(!Renamer.needsRenaming(New) && "Invalid function name.");
120 CloneSubModule(*KVPair.first, M, ExtractGlobalVars, ExtractFunctions,
125 FullyPartitionedModule fullyPartition(Module &M) {
126 FullyPartitionedModule MP;
128 ModulePartitionMap PMap;
132 if (F.isDeclaration())
135 std::string NewModuleName = (M.getName() + "." + F.getName()).str();
136 MP.Functions.push_back(
137 llvm::make_unique<Module>(NewModuleName, M.getContext()));
138 MP.Functions.back()->setDataLayout(M.getDataLayout());
139 PMap[MP.Functions.back().get()].insert(&F);
143 llvm::make_unique<Module>((M.getName() + ".globals_and_stubs").str(),
145 MP.GlobalVars->setDataLayout(M.getDataLayout());
148 llvm::make_unique<Module>((M.getName() + ".commons").str(), M.getContext());
149 MP.Commons->setDataLayout(M.getDataLayout());
151 // Make sure there's at least an empty set for the stubs map or we'll fail
152 // to clone anything for it (including the decls).
153 PMap[MP.GlobalVars.get()] = ModulePartitionMap::mapped_type();
154 for (auto &GV : M.globals())
155 if (GV.getLinkage() == GlobalValue::CommonLinkage)
156 PMap[MP.Commons.get()].insert(&GV);
158 PMap[MP.GlobalVars.get()].insert(&GV);
165 } // End namespace orc.
166 } // End namespace llvm.