151cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner//===- CloneModule.cpp - Clone an entire module ---------------------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
951cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner//
1051cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner// This file implements the CloneModule interface which makes a copy of an
1151cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner// entire module.
1251cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner//
1351cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner//===----------------------------------------------------------------------===//
1451cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner
1551cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner#include "llvm/Transforms/Utils/Cloning.h"
160b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constant.h"
170b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
180b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h"
1905ea54e8869a81b8dd846397175f218f97968907Dan Gohman#include "llvm/Transforms/Utils/ValueMapper.h"
20f7703df4968084c18c248c1feea9961c19a32e6aChris Lattnerusing namespace llvm;
21d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
2251cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner/// CloneModule - Return an exact copy of the specified module.  This is not as
2351cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner/// easy as it might seem because we have to worry about making copies of global
24cf00c4ab3ba308d45d98c5ccab87362cf802facbMisha Brukman/// variables and functions, and making their (initializers and references,
2551cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner/// respectively) refer to the right globals.
2651cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner///
27f7703df4968084c18c248c1feea9961c19a32e6aChris LattnerModule *llvm::CloneModule(const Module *M) {
28782e60150eb9370a60fa51ed49bd23036588756aChris Lattner  // Create the value map that maps things from the old module over to the new
29782e60150eb9370a60fa51ed49bd23036588756aChris Lattner  // module.
30774cca70b10bc679daff8203d639d9004a2eb194Devang Patel  ValueToValueMapTy VMap;
3129d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel  return CloneModule(M, VMap);
32782e60150eb9370a60fa51ed49bd23036588756aChris Lattner}
33782e60150eb9370a60fa51ed49bd23036588756aChris Lattner
341afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris LattnerModule *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) {
351afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  // First off, we need to create the new module.
368b477ed579794ba6d76915d56b3f448a7dd20120Owen Anderson  Module *New = new Module(M->getModuleIdentifier(), M->getContext());
3726f238589f9bb372d24b6fb2bc32edbf046fd9eeReid Spencer  New->setDataLayout(M->getDataLayout());
38c4e8c9f318a4cb9ff75d8955482a8ca6412803cdChris Lattner  New->setTargetTriple(M->getTargetTriple());
393e2fa7a746270452316523f27b9055b007feba32Chris Lattner  New->setModuleInlineAsm(M->getModuleInlineAsm());
401afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner
4151cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  // Loop over all of the global variables, making corresponding globals in the
4229d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel  // new module.  Here we add them to the VMap and to the new Module.  We
4351cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  // don't worry about attributes or initializers, they will come later.
4451cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  //
45c154cef9a1e7db1afd42e4b227571debe7a986f5Chris Lattner  for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
46a6bf66d0d6c1ed9bb657f72bc89c432c4ee96f63Nick Lewycky       I != E; ++I) {
47e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson    GlobalVariable *GV = new GlobalVariable(*New,
483d29df3e8a203b167d8071ea6f805b21db18a5afOwen Anderson                                            I->getType()->getElementType(),
490455bb89cde2c1153c6ed1a5bf5b380ed6d43675Eli Friedman                                            I->isConstant(), I->getLinkage(),
50dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                            (Constant*) nullptr, I->getName(),
51dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                            (GlobalVariable*) nullptr,
52ce718ff9f42c7da092eaa01dd0242e8d5ba84713Hans Wennborg                                            I->getThreadLocalMode(),
530455bb89cde2c1153c6ed1a5bf5b380ed6d43675Eli Friedman                                            I->getType()->getAddressSpace());
540455bb89cde2c1153c6ed1a5bf5b380ed6d43675Eli Friedman    GV->copyAttributesFrom(I);
5529d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel    VMap[I] = GV;
56a6bf66d0d6c1ed9bb657f72bc89c432c4ee96f63Nick Lewycky  }
5751cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner
5851cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  // Loop over the functions in the module, making external functions as before
59c154cef9a1e7db1afd42e4b227571debe7a986f5Chris Lattner  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
6000b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen    Function *NF =
61051a950000e21935165db56695e35bade668193bGabor Greif      Function::Create(cast<FunctionType>(I->getType()->getElementType()),
620455bb89cde2c1153c6ed1a5bf5b380ed6d43675Eli Friedman                       I->getLinkage(), I->getName(), New);
6328c3cff8250b3fe2adc6479306fe7dbdb48a1bdbDuncan Sands    NF->copyAttributesFrom(I);
6429d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel    VMap[I] = NF;
65c154cef9a1e7db1afd42e4b227571debe7a986f5Chris Lattner  }
6651cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner
67a289511090b2b48e0824349865e5bc9cb4b7d33cAnton Korobeynikov  // Loop over the aliases in the module
68a289511090b2b48e0824349865e5bc9cb4b7d33cAnton Korobeynikov  for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
690455bb89cde2c1153c6ed1a5bf5b380ed6d43675Eli Friedman       I != E; ++I) {
70dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    auto *PTy = cast<PointerType>(I->getType());
71dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    auto *GA =
72dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
73dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                            I->getLinkage(), I->getName(), New);
740455bb89cde2c1153c6ed1a5bf5b380ed6d43675Eli Friedman    GA->copyAttributesFrom(I);
750455bb89cde2c1153c6ed1a5bf5b380ed6d43675Eli Friedman    VMap[I] = GA;
760455bb89cde2c1153c6ed1a5bf5b380ed6d43675Eli Friedman  }
77a289511090b2b48e0824349865e5bc9cb4b7d33cAnton Korobeynikov
7851cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  // Now that all of the things that global variable initializer can refer to
7951cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  // have been created, loop through and copy the global variable referrers
8051cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  // over...  We also set the attributes on the global now.
8151cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  //
82c154cef9a1e7db1afd42e4b227571debe7a986f5Chris Lattner  for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
83c154cef9a1e7db1afd42e4b227571debe7a986f5Chris Lattner       I != E; ++I) {
8429d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel    GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
8551cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner    if (I->hasInitializer())
861afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner      GV->setInitializer(MapValue(I->getInitializer(), VMap));
8751cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  }
8851cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner
8951cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  // Similarly, copy over function bodies now...
9051cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  //
9151cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
9229d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel    Function *F = cast<Function>(VMap[I]);
935cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer    if (!I->isDeclaration()) {
94e4d5c441e04bdc00ccf1804744af670655123b07Chris Lattner      Function::arg_iterator DestI = F->arg_begin();
95c154cef9a1e7db1afd42e4b227571debe7a986f5Chris Lattner      for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
96c154cef9a1e7db1afd42e4b227571debe7a986f5Chris Lattner           ++J) {
9751cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner        DestI->setName(J->getName());
9829d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel        VMap[J] = DestI++;
9951cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner      }
10051cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner
101ec1bea0d94372985a0a5eb283e644c6d0dd345dcChris Lattner      SmallVector<ReturnInst*, 8> Returns;  // Ignore returns cloned.
1026cb8c23db1c3becdce6dfbf1b7f1677faca4251eDan Gohman      CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
10351cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner    }
10451cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  }
10551cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner
106a289511090b2b48e0824349865e5bc9cb4b7d33cAnton Korobeynikov  // And aliases
107a289511090b2b48e0824349865e5bc9cb4b7d33cAnton Korobeynikov  for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
108a289511090b2b48e0824349865e5bc9cb4b7d33cAnton Korobeynikov       I != E; ++I) {
10929d3dd8a64791031eea00ffbae51843dc9982df9Devang Patel    GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
110cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    if (const Constant *C = I->getAliasee())
111dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      GA->setAliasee(cast<GlobalObject>(MapValue(C, VMap)));
112a289511090b2b48e0824349865e5bc9cb4b7d33cAnton Korobeynikov  }
113d8800e797261285f91c2a3382215fd740e6f875dDevang Patel
114d8800e797261285f91c2a3382215fd740e6f875dDevang Patel  // And named metadata....
115d8800e797261285f91c2a3382215fd740e6f875dDevang Patel  for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
116d8800e797261285f91c2a3382215fd740e6f875dDevang Patel         E = M->named_metadata_end(); I != E; ++I) {
117d8800e797261285f91c2a3382215fd740e6f875dDevang Patel    const NamedMDNode &NMD = *I;
11817aa92c92a925b4a674440c7ef088c223990e854Dan Gohman    NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
119d8800e797261285f91c2a3382215fd740e6f875dDevang Patel    for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
1201afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner      NewNMD->addOperand(MapValue(NMD.getOperand(i), VMap));
121d8800e797261285f91c2a3382215fd740e6f875dDevang Patel  }
1223bf329f49512e633df430c097bfd5bdaa122ba55Devang Patel
12351cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner  return New;
12451cbcbf435d1aaa1a5269d62b5d0b31b57316b4aChris Lattner}
125