CloneModule.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "llvm-c/Core.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. // Create the value map that maps things from the old module over to the new
  28. // module.
  29. ValueToValueMapTy VMap;
  30. return CloneModule(M, VMap);
  31. }
  32. Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) {
  33. // First off, we need to create the new module.
  34. Module *New = new Module(M->getModuleIdentifier(), M->getContext());
  35. New->setDataLayout(M->getDataLayout());
  36. New->setTargetTriple(M->getTargetTriple());
  37. New->setModuleInlineAsm(M->getModuleInlineAsm());
  38. // Loop over all of the global variables, making corresponding globals in the
  39. // new module. Here we add them to the VMap and to the new Module. We
  40. // don't worry about attributes or initializers, they will come later.
  41. //
  42. for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
  43. I != E; ++I) {
  44. GlobalVariable *GV = new GlobalVariable(*New,
  45. I->getType()->getElementType(),
  46. I->isConstant(), I->getLinkage(),
  47. (Constant*) nullptr, I->getName(),
  48. (GlobalVariable*) nullptr,
  49. I->getThreadLocalMode(),
  50. I->getType()->getAddressSpace());
  51. GV->copyAttributesFrom(I);
  52. VMap[I] = GV;
  53. }
  54. // Loop over the functions in the module, making external functions as before
  55. for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
  56. Function *NF =
  57. Function::Create(cast<FunctionType>(I->getType()->getElementType()),
  58. I->getLinkage(), I->getName(), New);
  59. NF->copyAttributesFrom(I);
  60. VMap[I] = NF;
  61. }
  62. // Loop over the aliases in the module
  63. for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
  64. I != E; ++I) {
  65. auto *PTy = cast<PointerType>(I->getType());
  66. auto *GA = GlobalAlias::create(PTy, I->getLinkage(), I->getName(), New);
  67. GA->copyAttributesFrom(I);
  68. VMap[I] = GA;
  69. }
  70. // Now that all of the things that global variable initializer can refer to
  71. // have been created, loop through and copy the global variable referrers
  72. // over... We also set the attributes on the global now.
  73. //
  74. for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
  75. I != E; ++I) {
  76. GlobalVariable *GV = cast<GlobalVariable>(VMap[I]);
  77. if (I->hasInitializer())
  78. GV->setInitializer(MapValue(I->getInitializer(), VMap));
  79. }
  80. // Similarly, copy over function bodies now...
  81. //
  82. for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
  83. Function *F = cast<Function>(VMap[I]);
  84. if (!I->isDeclaration()) {
  85. Function::arg_iterator DestI = F->arg_begin();
  86. for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end();
  87. ++J) {
  88. DestI->setName(J->getName());
  89. VMap[J] = DestI++;
  90. }
  91. SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
  92. CloneFunctionInto(F, I, VMap, /*ModuleLevelChanges=*/true, Returns);
  93. }
  94. if (I->hasPersonalityFn())
  95. F->setPersonalityFn(MapValue(I->getPersonalityFn(), VMap));
  96. }
  97. // And aliases
  98. for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
  99. I != E; ++I) {
  100. GlobalAlias *GA = cast<GlobalAlias>(VMap[I]);
  101. if (const Constant *C = I->getAliasee())
  102. GA->setAliasee(MapValue(C, VMap));
  103. }
  104. // And named metadata....
  105. for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
  106. E = M->named_metadata_end(); I != E; ++I) {
  107. const NamedMDNode &NMD = *I;
  108. NamedMDNode *NewNMD = New->getOrInsertNamedMetadata(NMD.getName());
  109. for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
  110. NewNMD->addOperand(MapMetadata(NMD.getOperand(i), VMap));
  111. }
  112. return New;
  113. }
  114. // extern "C" { // HLSL Change -Don't use c linkage.
  115. LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
  116. return wrap(CloneModule(unwrap(M)));
  117. }
  118. // } // HLSL Change -Don't use c linkage.