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