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"
10 void copyGVInitializer(GlobalVariable &New, const GlobalVariable &Orig,
11 ValueToValueMapTy &VMap) {
12 if (Orig.hasInitializer())
13 New.setInitializer(MapValue(Orig.getInitializer(), VMap));
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();
22 DestI->setName(J->getName());
26 SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
27 CloneFunctionInto(&New, &Orig, VMap, /*ModuleLevelChanges=*/true, Returns);
31 void CloneSubModule(llvm::Module &Dst, const Module &Src,
32 HandleGlobalVariableFtor HandleGlobalVariable,
33 HandleFunctionFtor HandleFunction, bool CloneInlineAsm) {
35 ValueToValueMapTy VMap;
38 Dst.appendModuleInlineAsm(Src.getModuleInlineAsm());
40 // Copy global variables (but not initializers, yet).
41 for (Module::const_global_iterator I = Src.global_begin(), E = Src.global_end();
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);
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) {
54 Function::Create(cast<FunctionType>(I->getType()->getElementType()),
55 I->getLinkage(), I->getName(), &Dst);
56 NF->copyAttributesFrom(I);
60 // Loop over the aliases in the module
61 for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end();
63 auto *PTy = cast<PointerType>(I->getType());
64 auto *GA = GlobalAlias::create(PTy, I->getLinkage(), I->getName(), &Dst);
65 GA->copyAttributesFrom(I);
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();
74 GlobalVariable &GV = *cast<GlobalVariable>(VMap[I]);
75 HandleGlobalVariable(GV, *I, VMap);
78 // Similarly, copy over function bodies now...
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);
86 for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end();
88 GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
89 if (const Constant *C = I->getAliasee())
90 GA->setAliasee(MapValue(C, VMap));
93 // And named metadata....
94 for (Module::const_named_metadata_iterator I = Src.named_metadata_begin(),
95 E = Src.named_metadata_end();
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));
105 } // End namespace orc.
106 } // End namespace llvm.