LLVM API Documentation

CloneModule.cpp
Go to the documentation of this file.
00001 //===- CloneModule.cpp - Clone an entire module ---------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the CloneModule interface which makes a copy of an
00011 // entire module.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Transforms/Utils/Cloning.h"
00016 #include "llvm/IR/Constant.h"
00017 #include "llvm/IR/DerivedTypes.h"
00018 #include "llvm/IR/Module.h"
00019 #include "llvm/Transforms/Utils/ValueMapper.h"
00020 using namespace llvm;
00021 
00022 /// CloneModule - Return an exact copy of the specified module.  This is not as
00023 /// easy as it might seem because we have to worry about making copies of global
00024 /// variables and functions, and making their (initializers and references,
00025 /// respectively) refer to the right globals.
00026 ///
00027 Module *llvm::CloneModule(const Module *M) {
00028   // Create the value map that maps things from the old module over to the new
00029   // module.
00030   ValueToValueMapTy VMap;
00031   return CloneModule(M, VMap);
00032 }
00033 
00034 Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) {
00035   // First off, we need to create the new module.
00036   Module *New = new Module(M->getModuleIdentifier(), M->getContext());
00037   New->setDataLayout(M->getDataLayout());
00038   New->setTargetTriple(M->getTargetTriple());
00039   New->setModuleInlineAsm(M->getModuleInlineAsm());
00040    
00041   // Loop over all of the global variables, making corresponding globals in the
00042   // new module.  Here we add them to the VMap and to the new Module.  We
00043   // don't worry about attributes or initializers, they will come later.
00044   //
00045   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
00046        I != E; ++I) {
00047     GlobalVariable *GV = new GlobalVariable(*New, 
00048                                             I->getType()->getElementType(),
00049                                             I->isConstant(), I->getLinkage(),
00050                                             (Constant*) nullptr, I->getName(),
00051                                             (GlobalVariable*) nullptr,
00052                                             I->getThreadLocalMode(),
00053                                             I->getType()->getAddressSpace());
00054     GV->copyAttributesFrom(I);
00055     VMap[I] = GV;
00056   }
00057 
00058   // Loop over the functions in the module, making external functions as before
00059   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
00060     Function *NF =
00061       Function::Create(cast<FunctionType>(I->getType()->getElementType()),
00062                        I->getLinkage(), I->getName(), New);
00063     NF->copyAttributesFrom(I);
00064     VMap[I] = NF;
00065   }
00066 
00067   // Loop over the aliases in the module
00068   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
00069        I != E; ++I) {
00070     auto *PTy = cast<PointerType>(I->getType());
00071     auto *GA =
00072         GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
00073                             I->getLinkage(), I->getName(), New);
00074     GA->copyAttributesFrom(I);
00075     VMap[I] = GA;
00076   }
00077   
00078   // Now that all of the things that global variable initializer can refer to
00079   // have been created, loop through and copy the global variable referrers
00080   // over...  We also set the attributes on the global now.
00081   //
00082   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
00083        I != E; ++I) {
00084     GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
00085     if (I->hasInitializer())
00086       GV->setInitializer(MapValue(I->getInitializer(), VMap));
00087   }
00088 
00089   // Similarly, copy over function bodies now...
00090   //
00091   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
00092     Function *F = cast<Function>(VMap[I]);
00093     if (!I->isDeclaration()) {
00094       Function::arg_iterator DestI = F->arg_begin();
00095       for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
00096            ++J) {
00097         DestI->setName(J->getName());
00098         VMap[J] = DestI++;
00099       }
00100 
00101       SmallVector<ReturnInst*, 8> Returns;  // Ignore returns cloned.
00102       CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
00103     }
00104   }
00105 
00106   // And aliases
00107   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
00108        I != E; ++I) {
00109     GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
00110     if (const Constant *C = I->getAliasee())
00111       GA->setAliasee(cast<GlobalObject>(MapValue(C, VMap)));
00112   }
00113 
00114   // And named metadata....
00115   for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
00116          E = M->named_metadata_end(); I != E; ++I) {
00117     const NamedMDNode &NMD = *I;
00118     NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
00119     for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
00120       NewNMD->addOperand(MapValue(NMD.getOperand(i), VMap));
00121   }
00122 
00123   return New;
00124 }