LLVM API Documentation
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(), M->getContext()); 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(*New, 00060 I->getType()->getElementType(), 00061 false, 00062 GlobalValue::ExternalLinkage, 0, 00063 I->getName()); 00064 GV->setAlignment(I->getAlignment()); 00065 ValueMap[I] = GV; 00066 } 00067 00068 // Loop over the functions in the module, making external functions as before 00069 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { 00070 Function *NF = 00071 Function::Create(cast<FunctionType>(I->getType()->getElementType()), 00072 GlobalValue::ExternalLinkage, I->getName(), New); 00073 NF->copyAttributesFrom(I); 00074 ValueMap[I] = NF; 00075 } 00076 00077 // Loop over the aliases in the module 00078 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); 00079 I != E; ++I) 00080 ValueMap[I] = new GlobalAlias(I->getType(), GlobalAlias::ExternalLinkage, 00081 I->getName(), NULL, New); 00082 00083 // Now that all of the things that global variable initializer can refer to 00084 // have been created, loop through and copy the global variable referrers 00085 // over... We also set the attributes on the global now. 00086 // 00087 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); 00088 I != E; ++I) { 00089 GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]); 00090 if (I->hasInitializer()) 00091 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(), 00092 ValueMap, 00093 M->getContext()))); 00094 GV->setLinkage(I->getLinkage()); 00095 GV->setThreadLocal(I->isThreadLocal()); 00096 GV->setConstant(I->isConstant()); 00097 } 00098 00099 // Similarly, copy over function bodies now... 00100 // 00101 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { 00102 Function *F = cast<Function>(ValueMap[I]); 00103 if (!I->isDeclaration()) { 00104 Function::arg_iterator DestI = F->arg_begin(); 00105 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end(); 00106 ++J) { 00107 DestI->setName(J->getName()); 00108 ValueMap[J] = DestI++; 00109 } 00110 00111 std::vector<ReturnInst*> Returns; // Ignore returns cloned... 00112 CloneFunctionInto(F, I, ValueMap, Returns); 00113 } 00114 00115 F->setLinkage(I->getLinkage()); 00116 } 00117 00118 // And aliases 00119 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); 00120 I != E; ++I) { 00121 GlobalAlias *GA = cast<GlobalAlias>(ValueMap[I]); 00122 GA->setLinkage(I->getLinkage()); 00123 if (const Constant* C = I->getAliasee()) 00124 GA->setAliasee(cast<Constant>(MapValue(C, ValueMap, M->getContext()))); 00125 } 00126 00127 return New; 00128 }