LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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   // First off, we need to create the new module...
00030   Module *New = new Module(M->getModuleIdentifier());
00031   New->setEndianness(M->getEndianness());
00032   New->setPointerSize(M->getPointerSize());
00033 
00034   // Copy all of the type symbol table entries over...
00035   const SymbolTable &SymTab = M->getSymbolTable();
00036   SymbolTable::type_const_iterator TypeI = SymTab.type_begin();
00037   SymbolTable::type_const_iterator TypeE = SymTab.type_end();
00038   for ( ; TypeI != TypeE; ++TypeI ) {
00039     New->addTypeName(TypeI->first, TypeI->second);
00040   }
00041 
00042   // Create the value map that maps things from the old module over to the new
00043   // module.
00044   std::map<const Value*, Value*> ValueMap;
00045 
00046   // Loop over all of the global variables, making corresponding globals in the
00047   // new module.  Here we add them to the ValueMap and to the new Module.  We
00048   // don't worry about attributes or initializers, they will come later.
00049   //
00050   for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
00051     ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
00052                                      GlobalValue::ExternalLinkage, 0,
00053                                      I->getName(), New);
00054 
00055   // Loop over the functions in the module, making external functions as before
00056   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
00057     ValueMap[I]=new Function(cast<FunctionType>(I->getType()->getElementType()),
00058                              GlobalValue::ExternalLinkage, I->getName(), New);
00059 
00060   // Now that all of the things that global variable initializer can refer to
00061   // have been created, loop through and copy the global variable referrers
00062   // over...  We also set the attributes on the global now.
00063   //
00064   for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
00065     GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]);
00066     if (I->hasInitializer())
00067       GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
00068                                                  ValueMap)));
00069     GV->setLinkage(I->getLinkage());
00070   }
00071 
00072   // Similarly, copy over function bodies now...
00073   //
00074   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
00075     Function *F = cast<Function>(ValueMap[I]);
00076     if (!I->isExternal()) {
00077       Function::aiterator DestI = F->abegin();
00078       for (Function::const_aiterator J = I->abegin(); J != I->aend(); ++J) {
00079         DestI->setName(J->getName());
00080         ValueMap[J] = DestI++;
00081       }
00082 
00083       std::vector<ReturnInst*> Returns;  // Ignore returns cloned...
00084       CloneFunctionInto(F, I, ValueMap, Returns);
00085     }
00086 
00087     F->setLinkage(I->getLinkage());
00088   }
00089 
00090   return New;
00091 }
00092 
00093 // vim: sw=2