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/Module.h"
00017 #include "llvm/DerivedTypes.h"
00018 #include "llvm/TypeSymbolTable.h"
00019 #include "llvm/Constant.h"
00020 #include "llvm/Transforms/Utils/ValueMapper.h"
00021 using namespace llvm;
00022 
00023 /// CloneModule - Return an exact copy of the specified module.  This is not as
00024 /// easy as it might seem because we have to worry about making copies of global
00025 /// variables and functions, and making their (initializers and references,
00026 /// respectively) refer to the right globals.
00027 ///
00028 Module *llvm::CloneModule(const Module *M) {
00029   // Create the value map that maps things from the old module over to the new
00030   // module.
00031   DenseMap<const Value*, Value*> ValueMap;
00032   return CloneModule(M, ValueMap);
00033 }
00034 
00035 Module *llvm::CloneModule(const Module *M,
00036                           DenseMap<const Value*, Value*> &ValueMap) {
00037   // First off, we need to create the new module...
00038   Module *New = new Module(M->getModuleIdentifier());
00039   New->setDataLayout(M->getDataLayout());
00040   New->setTargetTriple(M->getTargetTriple());
00041   New->setModuleInlineAsm(M->getModuleInlineAsm());
00042 
00043   // Copy all of the type symbol table entries over.
00044   const TypeSymbolTable &TST = M->getTypeSymbolTable();
00045   for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 
00046        TI != TE; ++TI)
00047     New->addTypeName(TI->first, TI->second);
00048   
00049   // Copy all of the dependent libraries over.
00050   for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
00051     New->addLibrary(*I);
00052 
00053   // Loop over all of the global variables, making corresponding globals in the
00054   // new module.  Here we add them to the ValueMap and to the new Module.  We
00055   // don't worry about attributes or initializers, they will come later.
00056   //
00057   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
00058        I != E; ++I) {
00059     GlobalVariable *GV = new GlobalVariable(I->getType()->getElementType(),
00060                                             false,
00061                                             GlobalValue::ExternalLinkage, 0,
00062                                             I->getName(), New);
00063     GV->setAlignment(I->getAlignment());
00064     ValueMap[I] = GV;
00065   }
00066 
00067   // Loop over the functions in the module, making external functions as before
00068   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
00069     Function *NF =
00070       Function::Create(cast<FunctionType>(I->getType()->getElementType()),
00071                        GlobalValue::ExternalLinkage, I->getName(), New);
00072     NF->copyAttributesFrom(I);
00073     ValueMap[I] = NF;
00074   }
00075 
00076   // Loop over the aliases in the module
00077   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
00078        I != E; ++I)
00079     ValueMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage,
00080                                   I->getName(), NULL, New);
00081   
00082   // Now that all of the things that global variable initializer can refer to
00083   // have been created, loop through and copy the global variable referrers
00084   // over...  We also set the attributes on the global now.
00085   //
00086   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
00087        I != E; ++I) {
00088     GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
00089     if (I->hasInitializer())
00090       GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
00091                                                  ValueMap)));
00092     GV->setLinkage(I->getLinkage());
00093     GV->setThreadLocal(I->isThreadLocal());
00094     GV->setConstant(I->isConstant());
00095   }
00096 
00097   // Similarly, copy over function bodies now...
00098   //
00099   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
00100     Function *F = cast<Function>(ValueMap[I]);
00101     if (!I->isDeclaration()) {
00102       Function::arg_iterator DestI = F->arg_begin();
00103       for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
00104            ++J) {
00105         DestI->setName(J->getName());
00106         ValueMap[J] = DestI++;
00107       }
00108 
00109       std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
00110       CloneFunctionInto(F, I, ValueMap, Returns);
00111     }
00112 
00113     F->setLinkage(I->getLinkage());
00114   }
00115 
00116   // And aliases
00117   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
00118        I != E; ++I) {
00119     GlobalAlias *GA = cast<GlobalAlias>(ValueMap[I]);
00120     GA->setLinkage(I->getLinkage());
00121     if (const Constant* C = I->getAliasee())
00122       GA->setAliasee(cast<Constant>(MapValue(C, ValueMap)));
00123   }
00124   
00125   return New;
00126 }