/3rd_party/llvm/lib/Transforms/Utils/CloneModule.cpp

https://code.google.com/p/softart/ · C++ · 122 lines · 75 code · 12 blank · 35 comment · 21 complexity · 0e5d72439272f00d2369f94736422d01 MD5 · raw file

  1. //===- CloneModule.cpp - Clone an entire module ---------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // 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/IR/Constant.h"
  16. #include "llvm/IR/DerivedTypes.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/Transforms/Utils/ValueMapper.h"
  19. using namespace llvm;
  20. /// CloneModule - Return an exact copy of the specified module. This is not as
  21. /// easy as it might seem because we have to worry about making copies of global
  22. /// variables and functions, and making their (initializers and references,
  23. /// respectively) refer to the right globals.
  24. ///
  25. Module *llvm::CloneModule(const Module *M) {
  26. // Create the value map that maps things from the old module over to the new
  27. // module.
  28. ValueToValueMapTy VMap;
  29. return CloneModule(M, VMap);
  30. }
  31. Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) {
  32. // First off, we need to create the new module.
  33. Module *New = new Module(M->getModuleIdentifier(), M->getContext());
  34. New->setDataLayout(M->getDataLayout());
  35. New->setTargetTriple(M->getTargetTriple());
  36. New->setModuleInlineAsm(M->getModuleInlineAsm());
  37. // Loop over all of the global variables, making corresponding globals in the
  38. // new module. Here we add them to the VMap and to the new Module. We
  39. // don't worry about attributes or initializers, they will come later.
  40. //
  41. for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
  42. I != E; ++I) {
  43. GlobalVariable *GV = new GlobalVariable(*New,
  44. I->getType()->getElementType(),
  45. I->isConstant(), I->getLinkage(),
  46. (Constant*) 0, I->getName(),
  47. (GlobalVariable*) 0,
  48. I->getThreadLocalMode(),
  49. I->getType()->getAddressSpace());
  50. GV->copyAttributesFrom(I);
  51. VMap[I] = GV;
  52. }
  53. // Loop over the functions in the module, making external functions as before
  54. for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
  55. Function *NF =
  56. Function::Create(cast<FunctionType>(I->getType()->getElementType()),
  57. I->getLinkage(), I->getName(), New);
  58. NF->copyAttributesFrom(I);
  59. VMap[I] = NF;
  60. }
  61. // Loop over the aliases in the module
  62. for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
  63. I != E; ++I) {
  64. GlobalAlias *GA = new GlobalAlias(I->getType(), I->getLinkage(),
  65. I->getName(), NULL, New);
  66. GA->copyAttributesFrom(I);
  67. VMap[I] = GA;
  68. }
  69. // Now that all of the things that global variable initializer can refer to
  70. // have been created, loop through and copy the global variable referrers
  71. // over... We also set the attributes on the global now.
  72. //
  73. for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
  74. I != E; ++I) {
  75. GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
  76. if (I->hasInitializer())
  77. GV->setInitializer(MapValue(I->getInitializer(), VMap));
  78. }
  79. // Similarly, copy over function bodies now...
  80. //
  81. for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
  82. Function *F = cast<Function>(VMap[I]);
  83. if (!I->isDeclaration()) {
  84. Function::arg_iterator DestI = F->arg_begin();
  85. for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
  86. ++J) {
  87. DestI->setName(J->getName());
  88. VMap[J] = DestI++;
  89. }
  90. SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
  91. CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
  92. }
  93. }
  94. // And aliases
  95. for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
  96. I != E; ++I) {
  97. GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
  98. if (const Constant *C = I->getAliasee())
  99. GA->setAliasee(MapValue(C, VMap));
  100. }
  101. // And named metadata....
  102. for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
  103. E = M->named_metadata_end(); I != E; ++I) {
  104. const NamedMDNode &NMD = *I;
  105. NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
  106. for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
  107. NewNMD->addOperand(MapValue(NMD.getOperand(i), VMap));
  108. }
  109. return New;
  110. }