CloneModule.cpp 3.8 KB

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