Home | History | Annotate | Download | only in Utils
      1 //===- CloneModule.cpp - Clone an entire module ---------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the CloneModule interface which makes a copy of an
     11 // entire module.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "llvm/Transforms/Utils/Cloning.h"
     16 #include "llvm/IR/Constant.h"
     17 #include "llvm/IR/DerivedTypes.h"
     18 #include "llvm/IR/Module.h"
     19 #include "llvm/Transforms/Utils/ValueMapper.h"
     20 #include "llvm-c/Core.h"
     21 using namespace llvm;
     22 
     23 /// CloneModule - Return an exact copy of the specified module.  This is not as
     24 /// easy as it might seem because we have to worry about making copies of global
     25 /// variables and functions, and making their (initializers and references,
     26 /// respectively) refer to the right globals.
     27 ///
     28 Module *llvm::CloneModule(const Module *M) {
     29   // Create the value map that maps things from the old module over to the new
     30   // module.
     31   ValueToValueMapTy VMap;
     32   return CloneModule(M, VMap);
     33 }
     34 
     35 Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) {
     36   // First off, we need to create the new module.
     37   Module *New = new Module(M->getModuleIdentifier(), M->getContext());
     38   New->setDataLayout(M->getDataLayout());
     39   New->setTargetTriple(M->getTargetTriple());
     40   New->setModuleInlineAsm(M->getModuleInlineAsm());
     41 
     42   // Loop over all of the global variables, making corresponding globals in the
     43   // new module.  Here we add them to the VMap and to the new Module.  We
     44   // don't worry about attributes or initializers, they will come later.
     45   //
     46   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
     47        I != E; ++I) {
     48     GlobalVariable *GV = new GlobalVariable(*New,
     49                                             I->getType()->getElementType(),
     50                                             I->isConstant(), I->getLinkage(),
     51                                             (Constant*) nullptr, I->getName(),
     52                                             (GlobalVariable*) nullptr,
     53                                             I->getThreadLocalMode(),
     54                                             I->getType()->getAddressSpace());
     55     GV->copyAttributesFrom(I);
     56     VMap[I] = GV;
     57   }
     58 
     59   // Loop over the functions in the module, making external functions as before
     60   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
     61     Function *NF =
     62       Function::Create(cast<FunctionType>(I->getType()->getElementType()),
     63                        I->getLinkage(), I->getName(), New);
     64     NF->copyAttributesFrom(I);
     65     VMap[I] = NF;
     66   }
     67 
     68   // Loop over the aliases in the module
     69   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
     70        I != E; ++I) {
     71     auto *PTy = cast<PointerType>(I->getType());
     72     auto *GA =
     73         GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
     74                             I->getLinkage(), I->getName(), New);
     75     GA->copyAttributesFrom(I);
     76     VMap[I] = GA;
     77   }
     78 
     79   // Now that all of the things that global variable initializer can refer to
     80   // have been created, loop through and copy the global variable referrers
     81   // over...  We also set the attributes on the global now.
     82   //
     83   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
     84        I != E; ++I) {
     85     GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
     86     if (I->hasInitializer())
     87       GV->setInitializer(MapValue(I->getInitializer(), VMap));
     88   }
     89 
     90   // Similarly, copy over function bodies now...
     91   //
     92   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
     93     Function *F = cast<Function>(VMap[I]);
     94     if (!I->isDeclaration()) {
     95       Function::arg_iterator DestI = F->arg_begin();
     96       for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
     97            ++J) {
     98         DestI->setName(J->getName());
     99         VMap[J] = DestI++;
    100       }
    101 
    102       SmallVector<ReturnInst*, 8> Returns;  // Ignore returns cloned.
    103       CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
    104     }
    105   }
    106 
    107   // And aliases
    108   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
    109        I != E; ++I) {
    110     GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
    111     if (const Constant *C = I->getAliasee())
    112       GA->setAliasee(MapValue(C, VMap));
    113   }
    114 
    115   // And named metadata....
    116   for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
    117          E = M->named_metadata_end(); I != E; ++I) {
    118     const NamedMDNode &NMD = *I;
    119     NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
    120     for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
    121       NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
    122   }
    123 
    124   return New;
    125 }
    126 
    127 extern "C" {
    128 
    129 LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
    130   return wrap(CloneModule(unwrap(M)));
    131 }
    132 
    133 }
    134