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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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/SymbolTable.h"
00019 #include "llvm/Constant.h"
00020 #include "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   std::map<const Value*, Value*> ValueMap;
00032 
00033   return CloneModule(M, ValueMap);
00034 }
00035 
00036 Module *llvm::CloneModule(const Module *M, std::map<const Value*, Value*> &ValueMap) {
00037   // First off, we need to create the new module...
00038   Module *New = new Module(M->getModuleIdentifier());
00039   New->setEndianness(M->getEndianness());
00040   New->setPointerSize(M->getPointerSize());
00041   New->setTargetTriple(M->getTargetTriple());
00042   New->setModuleInlineAsm(M->getModuleInlineAsm());
00043 
00044   // Copy all of the type symbol table entries over.
00045   const SymbolTable &SymTab = M->getSymbolTable();
00046   SymbolTable::type_const_iterator TypeI = SymTab.type_begin();
00047   SymbolTable::type_const_iterator TypeE = SymTab.type_end();
00048   for (; TypeI != TypeE; ++TypeI)
00049     New->addTypeName(TypeI->first, TypeI->second);
00050   
00051   // Copy all of the dependent libraries over.
00052   for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
00053     New->addLibrary(*I);
00054 
00055   // Loop over all of the global variables, making corresponding globals in the
00056   // new module.  Here we add them to the ValueMap and to the new Module.  We
00057   // don't worry about attributes or initializers, they will come later.
00058   //
00059   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
00060        I != E; ++I)
00061     ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
00062                                      GlobalValue::ExternalLinkage, 0,
00063                                      I->getName(), New);
00064 
00065   // Loop over the functions in the module, making external functions as before
00066   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
00067     Function *NF =
00068       new Function(cast<FunctionType>(I->getType()->getElementType()),
00069                    GlobalValue::ExternalLinkage, I->getName(), New);
00070     NF->setCallingConv(I->getCallingConv());
00071     ValueMap[I]= NF;
00072   }
00073 
00074   // Now that all of the things that global variable initializer can refer to
00075   // have been created, loop through and copy the global variable referrers
00076   // over...  We also set the attributes on the global now.
00077   //
00078   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
00079        I != E; ++I) {
00080     GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
00081     if (I->hasInitializer())
00082       GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
00083                                                  ValueMap)));
00084     GV->setLinkage(I->getLinkage());
00085   }
00086 
00087   // Similarly, copy over function bodies now...
00088   //
00089   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
00090     Function *F = cast<Function>(ValueMap[I]);
00091     if (!I->isExternal()) {
00092       Function::arg_iterator DestI = F->arg_begin();
00093       for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
00094            ++J) {
00095         DestI->setName(J->getName());
00096         ValueMap[J] = DestI++;
00097       }
00098 
00099       std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
00100       CloneFunctionInto(F, I, ValueMap, Returns);
00101     }
00102 
00103     F->setLinkage(I->getLinkage());
00104   }
00105 
00106   return New;
00107 }
00108 
00109 // vim: sw=2