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"
9 void llvm::copyGVInitializer(GlobalVariable &New, const GlobalVariable &Orig,
10 ValueToValueMapTy &VMap) {
11 if (Orig.hasInitializer())
12 New.setInitializer(MapValue(Orig.getInitializer(), VMap));
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();
21 DestI->setName(J->getName());
25 SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
26 CloneFunctionInto(&New, &Orig, VMap, /*ModuleLevelChanges=*/true, Returns);
30 void llvm::CloneSubModule(llvm::Module &Dst, const Module &Src,
31 HandleGlobalVariableFtor HandleGlobalVariable,
32 HandleFunctionFtor HandleFunction, bool CloneInlineAsm) {
34 ValueToValueMapTy VMap;
37 Dst.appendModuleInlineAsm(Src.getModuleInlineAsm());
39 // Copy global variables (but not initializers, yet).
40 for (Module::const_global_iterator I = Src.global_begin(), E = Src.global_end();
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);
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) {
53 Function::Create(cast<FunctionType>(I->getType()->getElementType()),
54 I->getLinkage(), I->getName(), &Dst);
55 NF->copyAttributesFrom(I);
59 // Loop over the aliases in the module
60 for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end();
62 auto *PTy = cast<PointerType>(I->getType());
64 GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
65 I->getLinkage(), I->getName(), &Dst);
66 GA->copyAttributesFrom(I);
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();
75 GlobalVariable &GV = *cast<GlobalVariable>(VMap[I]);
76 HandleGlobalVariable(GV, *I, VMap);
79 // Similarly, copy over function bodies now...
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);
87 for (Module::const_alias_iterator I = Src.alias_begin(), E = Src.alias_end();
89 GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
90 if (const Constant *C = I->getAliasee())
91 GA->setAliasee(MapValue(C, VMap));
94 // And named metadata....
95 for (Module::const_named_metadata_iterator I = Src.named_metadata_begin(),
96 E = Src.named_metadata_end();
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));