Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 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" |
Chris Lattner | 892310e | 2003-04-24 17:15:33 +0000 | [diff] [blame] | 11 | #include "llvm/SymbolTable.h" |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 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 |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 17 | /// variables and functions, and making their (initializers and references, |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 18 | /// respectively) refer to the right globals. |
| 19 | /// |
| 20 | Module *CloneModule(const Module *M) { |
| 21 | // First off, we need to create the new module... |
Chris Lattner | 37f5941 | 2003-04-22 18:02:26 +0000 | [diff] [blame] | 22 | Module *New = new Module(M->getModuleIdentifier()); |
Chris Lattner | 7899b74 | 2003-04-24 15:54:40 +0000 | [diff] [blame] | 23 | New->setEndianness(M->getEndianness()); |
| 24 | New->setPointerSize(M->getPointerSize()); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 892310e | 2003-04-24 17:15:33 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 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) |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 43 | ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false, |
| 44 | GlobalValue::ExternalLinkage, 0, |
| 45 | I->getName(), New); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 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()), |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 50 | GlobalValue::ExternalLinkage, I->getName(), New); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 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))); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 61 | GV->setLinkage(I->getLinkage()); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 79 | F->setLinkage(I->getLinkage()); |
Chris Lattner | 51cbcbf | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return New; |
| 83 | } |