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