106f32e7eSjoerg //===- CloneModule.cpp - Clone an entire module ---------------------------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This file implements the CloneModule interface which makes a copy of an
1006f32e7eSjoerg // entire module.
1106f32e7eSjoerg //
1206f32e7eSjoerg //===----------------------------------------------------------------------===//
1306f32e7eSjoerg
1406f32e7eSjoerg #include "llvm/IR/Constant.h"
1506f32e7eSjoerg #include "llvm/IR/DerivedTypes.h"
1606f32e7eSjoerg #include "llvm/IR/Module.h"
1706f32e7eSjoerg #include "llvm/Transforms/Utils/Cloning.h"
1806f32e7eSjoerg #include "llvm/Transforms/Utils/ValueMapper.h"
1906f32e7eSjoerg using namespace llvm;
2006f32e7eSjoerg
copyComdat(GlobalObject * Dst,const GlobalObject * Src)2106f32e7eSjoerg static void copyComdat(GlobalObject *Dst, const GlobalObject *Src) {
2206f32e7eSjoerg const Comdat *SC = Src->getComdat();
2306f32e7eSjoerg if (!SC)
2406f32e7eSjoerg return;
2506f32e7eSjoerg Comdat *DC = Dst->getParent()->getOrInsertComdat(SC->getName());
2606f32e7eSjoerg DC->setSelectionKind(SC->getSelectionKind());
2706f32e7eSjoerg Dst->setComdat(DC);
2806f32e7eSjoerg }
2906f32e7eSjoerg
3006f32e7eSjoerg /// This is not as easy as it might seem because we have to worry about making
3106f32e7eSjoerg /// copies of global variables and functions, and making their (initializers and
3206f32e7eSjoerg /// references, respectively) refer to the right globals.
3306f32e7eSjoerg ///
CloneModule(const Module & M)3406f32e7eSjoerg std::unique_ptr<Module> llvm::CloneModule(const Module &M) {
3506f32e7eSjoerg // Create the value map that maps things from the old module over to the new
3606f32e7eSjoerg // module.
3706f32e7eSjoerg ValueToValueMapTy VMap;
3806f32e7eSjoerg return CloneModule(M, VMap);
3906f32e7eSjoerg }
4006f32e7eSjoerg
CloneModule(const Module & M,ValueToValueMapTy & VMap)4106f32e7eSjoerg std::unique_ptr<Module> llvm::CloneModule(const Module &M,
4206f32e7eSjoerg ValueToValueMapTy &VMap) {
4306f32e7eSjoerg return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; });
4406f32e7eSjoerg }
4506f32e7eSjoerg
CloneModule(const Module & M,ValueToValueMapTy & VMap,function_ref<bool (const GlobalValue *)> ShouldCloneDefinition)4606f32e7eSjoerg std::unique_ptr<Module> llvm::CloneModule(
4706f32e7eSjoerg const Module &M, ValueToValueMapTy &VMap,
4806f32e7eSjoerg function_ref<bool(const GlobalValue *)> ShouldCloneDefinition) {
4906f32e7eSjoerg // First off, we need to create the new module.
5006f32e7eSjoerg std::unique_ptr<Module> New =
5106f32e7eSjoerg std::make_unique<Module>(M.getModuleIdentifier(), M.getContext());
5206f32e7eSjoerg New->setSourceFileName(M.getSourceFileName());
5306f32e7eSjoerg New->setDataLayout(M.getDataLayout());
5406f32e7eSjoerg New->setTargetTriple(M.getTargetTriple());
5506f32e7eSjoerg New->setModuleInlineAsm(M.getModuleInlineAsm());
5606f32e7eSjoerg
5706f32e7eSjoerg // Loop over all of the global variables, making corresponding globals in the
5806f32e7eSjoerg // new module. Here we add them to the VMap and to the new Module. We
5906f32e7eSjoerg // don't worry about attributes or initializers, they will come later.
6006f32e7eSjoerg //
6106f32e7eSjoerg for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
6206f32e7eSjoerg I != E; ++I) {
6306f32e7eSjoerg GlobalVariable *GV = new GlobalVariable(*New,
6406f32e7eSjoerg I->getValueType(),
6506f32e7eSjoerg I->isConstant(), I->getLinkage(),
6606f32e7eSjoerg (Constant*) nullptr, I->getName(),
6706f32e7eSjoerg (GlobalVariable*) nullptr,
6806f32e7eSjoerg I->getThreadLocalMode(),
6906f32e7eSjoerg I->getType()->getAddressSpace());
7006f32e7eSjoerg GV->copyAttributesFrom(&*I);
7106f32e7eSjoerg VMap[&*I] = GV;
7206f32e7eSjoerg }
7306f32e7eSjoerg
7406f32e7eSjoerg // Loop over the functions in the module, making external functions as before
7506f32e7eSjoerg for (const Function &I : M) {
7606f32e7eSjoerg Function *NF =
7706f32e7eSjoerg Function::Create(cast<FunctionType>(I.getValueType()), I.getLinkage(),
7806f32e7eSjoerg I.getAddressSpace(), I.getName(), New.get());
7906f32e7eSjoerg NF->copyAttributesFrom(&I);
8006f32e7eSjoerg VMap[&I] = NF;
8106f32e7eSjoerg }
8206f32e7eSjoerg
8306f32e7eSjoerg // Loop over the aliases in the module
8406f32e7eSjoerg for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
8506f32e7eSjoerg I != E; ++I) {
8606f32e7eSjoerg if (!ShouldCloneDefinition(&*I)) {
8706f32e7eSjoerg // An alias cannot act as an external reference, so we need to create
8806f32e7eSjoerg // either a function or a global variable depending on the value type.
8906f32e7eSjoerg // FIXME: Once pointee types are gone we can probably pick one or the
9006f32e7eSjoerg // other.
9106f32e7eSjoerg GlobalValue *GV;
9206f32e7eSjoerg if (I->getValueType()->isFunctionTy())
9306f32e7eSjoerg GV = Function::Create(cast<FunctionType>(I->getValueType()),
9406f32e7eSjoerg GlobalValue::ExternalLinkage,
9506f32e7eSjoerg I->getAddressSpace(), I->getName(), New.get());
9606f32e7eSjoerg else
9706f32e7eSjoerg GV = new GlobalVariable(
9806f32e7eSjoerg *New, I->getValueType(), false, GlobalValue::ExternalLinkage,
9906f32e7eSjoerg nullptr, I->getName(), nullptr,
10006f32e7eSjoerg I->getThreadLocalMode(), I->getType()->getAddressSpace());
10106f32e7eSjoerg VMap[&*I] = GV;
10206f32e7eSjoerg // We do not copy attributes (mainly because copying between different
10306f32e7eSjoerg // kinds of globals is forbidden), but this is generally not required for
10406f32e7eSjoerg // correctness.
10506f32e7eSjoerg continue;
10606f32e7eSjoerg }
10706f32e7eSjoerg auto *GA = GlobalAlias::create(I->getValueType(),
10806f32e7eSjoerg I->getType()->getPointerAddressSpace(),
10906f32e7eSjoerg I->getLinkage(), I->getName(), New.get());
11006f32e7eSjoerg GA->copyAttributesFrom(&*I);
11106f32e7eSjoerg VMap[&*I] = GA;
11206f32e7eSjoerg }
11306f32e7eSjoerg
11406f32e7eSjoerg // Now that all of the things that global variable initializer can refer to
11506f32e7eSjoerg // have been created, loop through and copy the global variable referrers
11606f32e7eSjoerg // over... We also set the attributes on the global now.
11706f32e7eSjoerg //
118*da58b97aSjoerg for (const GlobalVariable &G : M.globals()) {
119*da58b97aSjoerg GlobalVariable *GV = cast<GlobalVariable>(VMap[&G]);
120*da58b97aSjoerg
121*da58b97aSjoerg SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
122*da58b97aSjoerg G.getAllMetadata(MDs);
123*da58b97aSjoerg for (auto MD : MDs)
124*da58b97aSjoerg GV->addMetadata(MD.first, *MapMetadata(MD.second, VMap));
125*da58b97aSjoerg
126*da58b97aSjoerg if (G.isDeclaration())
12706f32e7eSjoerg continue;
12806f32e7eSjoerg
129*da58b97aSjoerg if (!ShouldCloneDefinition(&G)) {
13006f32e7eSjoerg // Skip after setting the correct linkage for an external reference.
13106f32e7eSjoerg GV->setLinkage(GlobalValue::ExternalLinkage);
13206f32e7eSjoerg continue;
13306f32e7eSjoerg }
134*da58b97aSjoerg if (G.hasInitializer())
135*da58b97aSjoerg GV->setInitializer(MapValue(G.getInitializer(), VMap));
13606f32e7eSjoerg
137*da58b97aSjoerg copyComdat(GV, &G);
13806f32e7eSjoerg }
13906f32e7eSjoerg
14006f32e7eSjoerg // Similarly, copy over function bodies now...
14106f32e7eSjoerg //
14206f32e7eSjoerg for (const Function &I : M) {
14306f32e7eSjoerg if (I.isDeclaration())
14406f32e7eSjoerg continue;
14506f32e7eSjoerg
14606f32e7eSjoerg Function *F = cast<Function>(VMap[&I]);
14706f32e7eSjoerg if (!ShouldCloneDefinition(&I)) {
14806f32e7eSjoerg // Skip after setting the correct linkage for an external reference.
14906f32e7eSjoerg F->setLinkage(GlobalValue::ExternalLinkage);
15006f32e7eSjoerg // Personality function is not valid on a declaration.
15106f32e7eSjoerg F->setPersonalityFn(nullptr);
15206f32e7eSjoerg continue;
15306f32e7eSjoerg }
15406f32e7eSjoerg
15506f32e7eSjoerg Function::arg_iterator DestI = F->arg_begin();
15606f32e7eSjoerg for (Function::const_arg_iterator J = I.arg_begin(); J != I.arg_end();
15706f32e7eSjoerg ++J) {
15806f32e7eSjoerg DestI->setName(J->getName());
15906f32e7eSjoerg VMap[&*J] = &*DestI++;
16006f32e7eSjoerg }
16106f32e7eSjoerg
16206f32e7eSjoerg SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned.
163*da58b97aSjoerg CloneFunctionInto(F, &I, VMap, CloneFunctionChangeType::ClonedModule,
164*da58b97aSjoerg Returns);
16506f32e7eSjoerg
16606f32e7eSjoerg if (I.hasPersonalityFn())
16706f32e7eSjoerg F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap));
16806f32e7eSjoerg
16906f32e7eSjoerg copyComdat(F, &I);
17006f32e7eSjoerg }
17106f32e7eSjoerg
17206f32e7eSjoerg // And aliases
17306f32e7eSjoerg for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
17406f32e7eSjoerg I != E; ++I) {
17506f32e7eSjoerg // We already dealt with undefined aliases above.
17606f32e7eSjoerg if (!ShouldCloneDefinition(&*I))
17706f32e7eSjoerg continue;
17806f32e7eSjoerg GlobalAlias *GA = cast<GlobalAlias>(VMap[&*I]);
17906f32e7eSjoerg if (const Constant *C = I->getAliasee())
18006f32e7eSjoerg GA->setAliasee(MapValue(C, VMap));
18106f32e7eSjoerg }
18206f32e7eSjoerg
18306f32e7eSjoerg // And named metadata....
18406f32e7eSjoerg for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
18506f32e7eSjoerg E = M.named_metadata_end();
18606f32e7eSjoerg I != E; ++I) {
18706f32e7eSjoerg const NamedMDNode &NMD = *I;
18806f32e7eSjoerg NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
18906f32e7eSjoerg for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
19006f32e7eSjoerg NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
19106f32e7eSjoerg }
19206f32e7eSjoerg
19306f32e7eSjoerg return New;
19406f32e7eSjoerg }
19506f32e7eSjoerg
19606f32e7eSjoerg extern "C" {
19706f32e7eSjoerg
LLVMCloneModule(LLVMModuleRef M)19806f32e7eSjoerg LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
19906f32e7eSjoerg return wrap(CloneModule(*unwrap(M)).release());
20006f32e7eSjoerg }
20106f32e7eSjoerg
20206f32e7eSjoerg }
203