1 //===- CloneModule.cpp - Clone an entire module ---------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by the LLVM research group and is distributed under 6 // the University of Illinois Open Source License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the CloneModule interface which makes a copy of an 11 // entire module. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Transforms/Utils/Cloning.h" 16 #include "llvm/Module.h" 17 #include "llvm/DerivedTypes.h" 18 #include "llvm/SymbolTable.h" 19 #include "llvm/Constant.h" 20 #include "ValueMapper.h" 21 using namespace llvm; 22 23 /// CloneModule - Return an exact copy of the specified module. This is not as 24 /// easy as it might seem because we have to worry about making copies of global 25 /// variables and functions, and making their (initializers and references, 26 /// respectively) refer to the right globals. 27 /// 28 Module *llvm::CloneModule(const Module *M) { 29 // First off, we need to create the new module... 30 Module *New = new Module(M->getModuleIdentifier()); 31 New->setEndianness(M->getEndianness()); 32 New->setPointerSize(M->getPointerSize()); 33 34 // Copy all of the type symbol table entries over... 35 const SymbolTable &SymTab = M->getSymbolTable(); 36 SymbolTable::type_const_iterator TypeI = SymTab.type_begin(); 37 SymbolTable::type_const_iterator TypeE = SymTab.type_end(); 38 for ( ; TypeI != TypeE; ++TypeI ) { 39 New->addTypeName(TypeI->first, TypeI->second); 40 } 41 42 // Create the value map that maps things from the old module over to the new 43 // module. 44 std::map<const Value*, Value*> ValueMap; 45 46 // Loop over all of the global variables, making corresponding globals in the 47 // new module. Here we add them to the ValueMap and to the new Module. We 48 // don't worry about attributes or initializers, they will come later. 49 // 50 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); 51 I != E; ++I) 52 ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false, 53 GlobalValue::ExternalLinkage, 0, 54 I->getName(), New); 55 56 // Loop over the functions in the module, making external functions as before 57 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { 58 Function *NF = 59 new Function(cast<FunctionType>(I->getType()->getElementType()), 60 GlobalValue::ExternalLinkage, I->getName(), New); 61 NF->setCallingConv(I->getCallingConv()); 62 ValueMap[I]= NF; 63 } 64 65 // Now that all of the things that global variable initializer can refer to 66 // have been created, loop through and copy the global variable referrers 67 // over... We also set the attributes on the global now. 68 // 69 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); 70 I != E; ++I) { 71 GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]); 72 if (I->hasInitializer()) 73 GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(), 74 ValueMap))); 75 GV->setLinkage(I->getLinkage()); 76 } 77 78 // Similarly, copy over function bodies now... 79 // 80 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { 81 Function *F = cast<Function>(ValueMap[I]); 82 if (!I->isExternal()) { 83 Function::arg_iterator DestI = F->arg_begin(); 84 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end(); 85 ++J) { 86 DestI->setName(J->getName()); 87 ValueMap[J] = DestI++; 88 } 89 90 std::vector<ReturnInst*> Returns; // Ignore returns cloned... 91 CloneFunctionInto(F, I, ValueMap, Returns); 92 } 93 94 F->setLinkage(I->getLinkage()); 95 } 96 97 return New; 98 } 99 100 // vim: sw=2 101