[Orc] New JIT APIs.
[oota-llvm.git] / lib / ExecutionEngine / Orc / CloneSubModule.cpp
1 #include "llvm/ExecutionEngine/Orc/CloneSubModule.h"
2 #include "llvm/IR/Function.h"
3 #include "llvm/IR/GlobalVariable.h"
4 #include "llvm/IR/Module.h"
5 #include "llvm/Transforms/Utils/Cloning.h"
6
7 using namespace llvm;
8
9 void llvm::copyGVInitializer(GlobalVariable &New, const GlobalVariable &Orig,
10                              ValueToValueMapTy &VMap) {
11   if (Orig.hasInitializer())
12     New.setInitializer(MapValue(Orig.getInitializer(), VMap));
13 }
14
15 void llvm::copyFunctionBody(Function &New, const Function &Orig,
16                             ValueToValueMapTy &VMap) {
17   if (!Orig.isDeclaration()) {
18     Function::arg_iterator DestI = New.arg_begin();
19     for (Function::const_arg_iterator J = Orig.arg_begin(); J != Orig.arg_end();
20          ++J) {
21       DestI->setName(J->getName());
22       VMap[J] = DestI++;
23     }
24
25     SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
26     CloneFunctionInto(&New, &Orig, VMap, /*ModuleLevelChanges=*/true, Returns);
27   }
28 }
29
30 std::unique_ptr<Module>
31 llvm::CloneSubModule(const Module &M,
32                      HandleGlobalVariableFtor HandleGlobalVariable,
33                      HandleFunctionFtor HandleFunction, bool KeepInlineAsm) {
34
35   ValueToValueMapTy VMap;
36
37   // First off, we need to create the new module.
38   std::unique_ptr<Module> New =
39       llvm::make_unique<Module>(M.getModuleIdentifier(), M.getContext());
40
41   New->setDataLayout(M.getDataLayout());
42   New->setTargetTriple(M.getTargetTriple());
43   if (KeepInlineAsm)
44     New->setModuleInlineAsm(M.getModuleInlineAsm());
45
46   // Copy global variables (but not initializers, yet).
47   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
48        I != E; ++I) {
49     GlobalVariable *GV = new GlobalVariable(
50         *New, I->getType()->getElementType(), I->isConstant(), I->getLinkage(),
51         (Constant *)nullptr, I->getName(), (GlobalVariable *)nullptr,
52         I->getThreadLocalMode(), I->getType()->getAddressSpace());
53     GV->copyAttributesFrom(I);
54     VMap[I] = GV;
55   }
56
57   // Loop over the functions in the module, making external functions as before
58   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
59     Function *NF =
60         Function::Create(cast<FunctionType>(I->getType()->getElementType()),
61                          I->getLinkage(), I->getName(), &*New);
62     NF->copyAttributesFrom(I);
63     VMap[I] = NF;
64   }
65
66   // Loop over the aliases in the module
67   for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
68        I != E; ++I) {
69     auto *PTy = cast<PointerType>(I->getType());
70     auto *GA =
71         GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
72                             I->getLinkage(), I->getName(), &*New);
73     GA->copyAttributesFrom(I);
74     VMap[I] = GA;
75   }
76
77   // Now that all of the things that global variable initializer can refer to
78   // have been created, loop through and copy the global variable referrers
79   // over...  We also set the attributes on the global now.
80   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
81        I != E; ++I) {
82     GlobalVariable &GV = *cast<GlobalVariable>(VMap[I]);
83     HandleGlobalVariable(GV, *I, VMap);
84   }
85
86   // Similarly, copy over function bodies now...
87   //
88   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
89     Function &F = *cast<Function>(VMap[I]);
90     HandleFunction(F, *I, VMap);
91   }
92
93   // And aliases
94   for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
95        I != E; ++I) {
96     GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
97     if (const Constant *C = I->getAliasee())
98       GA->setAliasee(MapValue(C, VMap));
99   }
100
101   // And named metadata....
102   for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
103                                              E = M.named_metadata_end();
104        I != E; ++I) {
105     const NamedMDNode &NMD = *I;
106     NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
107     for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
108       NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
109   }
110
111   return New;
112 }