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