Chris Lattner | e4dbb1a | 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" |
| 11 | #include "llvm/Constant.h" |
| 12 | #include "ValueMapper.h" |
| 13 | |
| 14 | /// CloneModule - Return an exact copy of the specified module. This is not as |
| 15 | /// easy as it might seem because we have to worry about making copies of global |
| 16 | /// variables and functions, and making their (intializers and references, |
| 17 | /// respectively) refer to the right globals. |
| 18 | /// |
| 19 | Module *CloneModule(const Module *M) { |
| 20 | // First off, we need to create the new module... |
Chris Lattner | d36ad92 | 2003-04-22 18:02:26 +0000 | [diff] [blame] | 21 | Module *New = new Module(M->getModuleIdentifier()); |
Chris Lattner | 0aebf8f | 2003-04-24 15:54:40 +0000 | [diff] [blame^] | 22 | New->setEndianness(M->getEndianness()); |
| 23 | New->setPointerSize(M->getPointerSize()); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 24 | |
| 25 | // Create the value map that maps things from the old module over to the new |
| 26 | // module. |
| 27 | std::map<const Value*, Value*> ValueMap; |
| 28 | |
| 29 | // Loop over all of the global variables, making corresponding globals in the |
| 30 | // new module. Here we add them to the ValueMap and to the new Module. We |
| 31 | // don't worry about attributes or initializers, they will come later. |
| 32 | // |
| 33 | for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 34 | ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false, |
| 35 | GlobalValue::ExternalLinkage, 0, |
| 36 | I->getName(), New); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 37 | |
| 38 | // Loop over the functions in the module, making external functions as before |
| 39 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 40 | ValueMap[I]=new Function(cast<FunctionType>(I->getType()->getElementType()), |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 41 | GlobalValue::ExternalLinkage, I->getName(), New); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 42 | |
| 43 | // Now that all of the things that global variable initializer can refer to |
| 44 | // have been created, loop through and copy the global variable referrers |
| 45 | // over... We also set the attributes on the global now. |
| 46 | // |
| 47 | for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) { |
| 48 | GlobalVariable *GV = cast<GlobalVariable>(ValueMap[I]); |
| 49 | if (I->hasInitializer()) |
| 50 | GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(), |
| 51 | ValueMap))); |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 52 | GV->setLinkage(I->getLinkage()); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Similarly, copy over function bodies now... |
| 56 | // |
| 57 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { |
| 58 | Function *F = cast<Function>(ValueMap[I]); |
| 59 | if (!I->isExternal()) { |
| 60 | Function::aiterator DestI = F->abegin(); |
| 61 | for (Function::const_aiterator J = I->abegin(); J != I->aend(); ++J) { |
| 62 | DestI->setName(J->getName()); |
| 63 | ValueMap[J] = DestI++; |
| 64 | } |
| 65 | |
| 66 | std::vector<ReturnInst*> Returns; // Ignore returns cloned... |
| 67 | CloneFunctionInto(F, I, ValueMap, Returns); |
| 68 | } |
| 69 | |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 70 | F->setLinkage(I->getLinkage()); |
Chris Lattner | e4dbb1a | 2002-11-20 20:47:41 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return New; |
| 74 | } |