[Orc] Update the Orc indirection utils and refactor the CompileOnDemand layer.
[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 void llvm::CloneSubModule(llvm::Module &Dst, const Module &Src,
31                      HandleGlobalVariableFtor HandleGlobalVariable,
32                      HandleFunctionFtor HandleFunction, bool CloneInlineAsm) {
33
34   ValueToValueMapTy VMap;
35
36   if (CloneInlineAsm)
37     Dst.appendModuleInlineAsm(Src.getModuleInlineAsm());
38
39   // Copy global variables (but not initializers, yet).
40   for (Module::const_global_iterator I = Src.global_begin(), E = Src.global_end();
41        I != E; ++I) {
42     GlobalVariable *GV = new GlobalVariable(
43         Dst, I->getType()->getElementType(), I->isConstant(), I->getLinkage(),
44         (Constant *)nullptr, I->getName(), (GlobalVariable *)nullptr,
45         I->getThreadLocalMode(), I->getType()->getAddressSpace());
46     GV->copyAttributesFrom(I);
47     VMap[I] = GV;
48   }
49
50   // Loop over the functions in the module, making external functions as before
51   for (Module::const_iterator I = Src.begin(), E = Src.end(); I != E; ++I) {
52     Function *NF =
53         Function::Create(cast<FunctionType>(I->getType()->getElementType()),
54                          I->getLinkage(), I->getName(), &Dst);
55     NF->copyAttributesFrom(I);
56     VMap[I] = NF;
57   }
58
59   // Loop over the aliases in the module
60   for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end();
61        I != E; ++I) {
62     auto *PTy = cast<PointerType>(I->getType());
63     auto *GA =
64         GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
65                             I->getLinkage(), I->getName(), &Dst);
66     GA->copyAttributesFrom(I);
67     VMap[I] = GA;
68   }
69
70   // Now that all of the things that global variable initializer can refer to
71   // have been created, loop through and copy the global variable referrers
72   // over...  We also set the attributes on the global now.
73   for (Module::const_global_iterator I = Src.global_begin(), E = Src.global_end();
74        I != E; ++I) {
75     GlobalVariable &GV = *cast<GlobalVariable>(VMap[I]);
76     HandleGlobalVariable(GV, *I, VMap);
77   }
78
79   // Similarly, copy over function bodies now...
80   //
81   for (Module::const_iterator I = Src.begin(), E = Src.end(); I != E; ++I) {
82     Function &F = *cast<Function>(VMap[I]);
83     HandleFunction(F, *I, VMap);
84   }
85
86   // And aliases
87   for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end();
88        I != E; ++I) {
89     GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
90     if (const Constant *C = I->getAliasee())
91       GA->setAliasee(MapValue(C, VMap));
92   }
93
94   // And named metadata....
95   for (Module::const_named_metadata_iterator I = Src.named_metadata_begin(),
96                                              E = Src.named_metadata_end();
97        I != E; ++I) {
98     const NamedMDNode &NMD = *I;
99     NamedMDNode *NewNMD = Dst.getOrInsertNamedMetadata(NMD.getName());
100     for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
101       NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
102   }
103
104 }