comparison lib/Transforms/Utils/CloneModule.cpp @ 100:7d135dc70f03

LLVM 3.9
author Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
date Tue, 26 Jan 2016 22:53:40 +0900
parents afa8332a0e37
children 1172e4bd9c6f
comparison
equal deleted inserted replaced
96:6418606d0ead 100:7d135dc70f03
18 #include "llvm/IR/Module.h" 18 #include "llvm/IR/Module.h"
19 #include "llvm/Transforms/Utils/ValueMapper.h" 19 #include "llvm/Transforms/Utils/ValueMapper.h"
20 #include "llvm-c/Core.h" 20 #include "llvm-c/Core.h"
21 using namespace llvm; 21 using namespace llvm;
22 22
23 /// CloneModule - Return an exact copy of the specified module. This is not as 23 /// This is not as easy as it might seem because we have to worry about making
24 /// easy as it might seem because we have to worry about making copies of global 24 /// copies of global variables and functions, and making their (initializers and
25 /// variables and functions, and making their (initializers and references, 25 /// references, respectively) refer to the right globals.
26 /// respectively) refer to the right globals.
27 /// 26 ///
28 Module *llvm::CloneModule(const Module *M) { 27 std::unique_ptr<Module> llvm::CloneModule(const Module *M) {
29 // Create the value map that maps things from the old module over to the new 28 // Create the value map that maps things from the old module over to the new
30 // module. 29 // module.
31 ValueToValueMapTy VMap; 30 ValueToValueMapTy VMap;
32 return CloneModule(M, VMap); 31 return CloneModule(M, VMap);
33 } 32 }
34 33
35 Module *llvm::CloneModule(const Module *M, ValueToValueMapTy &VMap) { 34 std::unique_ptr<Module> llvm::CloneModule(const Module *M,
35 ValueToValueMapTy &VMap) {
36 return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; }); 36 return CloneModule(M, VMap, [](const GlobalValue *GV) { return true; });
37 } 37 }
38 38
39 Module *llvm::CloneModule( 39 std::unique_ptr<Module> llvm::CloneModule(
40 const Module *M, ValueToValueMapTy &VMap, 40 const Module *M, ValueToValueMapTy &VMap,
41 std::function<bool(const GlobalValue *)> ShouldCloneDefinition) { 41 std::function<bool(const GlobalValue *)> ShouldCloneDefinition) {
42 // First off, we need to create the new module. 42 // First off, we need to create the new module.
43 Module *New = new Module(M->getModuleIdentifier(), M->getContext()); 43 std::unique_ptr<Module> New =
44 llvm::make_unique<Module>(M->getModuleIdentifier(), M->getContext());
44 New->setDataLayout(M->getDataLayout()); 45 New->setDataLayout(M->getDataLayout());
45 New->setTargetTriple(M->getTargetTriple()); 46 New->setTargetTriple(M->getTargetTriple());
46 New->setModuleInlineAsm(M->getModuleInlineAsm()); 47 New->setModuleInlineAsm(M->getModuleInlineAsm());
47 48
48 // Loop over all of the global variables, making corresponding globals in the 49 // Loop over all of the global variables, making corresponding globals in the
50 // don't worry about attributes or initializers, they will come later. 51 // don't worry about attributes or initializers, they will come later.
51 // 52 //
52 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); 53 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
53 I != E; ++I) { 54 I != E; ++I) {
54 GlobalVariable *GV = new GlobalVariable(*New, 55 GlobalVariable *GV = new GlobalVariable(*New,
55 I->getType()->getElementType(), 56 I->getValueType(),
56 I->isConstant(), I->getLinkage(), 57 I->isConstant(), I->getLinkage(),
57 (Constant*) nullptr, I->getName(), 58 (Constant*) nullptr, I->getName(),
58 (GlobalVariable*) nullptr, 59 (GlobalVariable*) nullptr,
59 I->getThreadLocalMode(), 60 I->getThreadLocalMode(),
60 I->getType()->getAddressSpace()); 61 I->getType()->getAddressSpace());
63 } 64 }
64 65
65 // Loop over the functions in the module, making external functions as before 66 // Loop over the functions in the module, making external functions as before
66 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { 67 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
67 Function *NF = 68 Function *NF =
68 Function::Create(cast<FunctionType>(I->getType()->getElementType()), 69 Function::Create(cast<FunctionType>(I->getValueType()),
69 I->getLinkage(), I->getName(), New); 70 I->getLinkage(), I->getName(), New.get());
70 NF->copyAttributesFrom(&*I); 71 NF->copyAttributesFrom(&*I);
71 VMap[&*I] = NF; 72 VMap[&*I] = NF;
72 } 73 }
73 74
74 // Loop over the aliases in the module 75 // Loop over the aliases in the module
80 // FIXME: Once pointee types are gone we can probably pick one or the 81 // FIXME: Once pointee types are gone we can probably pick one or the
81 // other. 82 // other.
82 GlobalValue *GV; 83 GlobalValue *GV;
83 if (I->getValueType()->isFunctionTy()) 84 if (I->getValueType()->isFunctionTy())
84 GV = Function::Create(cast<FunctionType>(I->getValueType()), 85 GV = Function::Create(cast<FunctionType>(I->getValueType()),
85 GlobalValue::ExternalLinkage, I->getName(), New); 86 GlobalValue::ExternalLinkage, I->getName(),
87 New.get());
86 else 88 else
87 GV = new GlobalVariable( 89 GV = new GlobalVariable(
88 *New, I->getValueType(), false, GlobalValue::ExternalLinkage, 90 *New, I->getValueType(), false, GlobalValue::ExternalLinkage,
89 (Constant *)nullptr, I->getName(), (GlobalVariable *)nullptr, 91 (Constant *)nullptr, I->getName(), (GlobalVariable *)nullptr,
90 I->getThreadLocalMode(), I->getType()->getAddressSpace()); 92 I->getThreadLocalMode(), I->getType()->getAddressSpace());
94 // correctness. 96 // correctness.
95 continue; 97 continue;
96 } 98 }
97 auto *GA = GlobalAlias::create(I->getValueType(), 99 auto *GA = GlobalAlias::create(I->getValueType(),
98 I->getType()->getPointerAddressSpace(), 100 I->getType()->getPointerAddressSpace(),
99 I->getLinkage(), I->getName(), New); 101 I->getLinkage(), I->getName(), New.get());
100 GA->copyAttributesFrom(&*I); 102 GA->copyAttributesFrom(&*I);
101 VMap[&*I] = GA; 103 VMap[&*I] = GA;
102 } 104 }
103 105
104 // Now that all of the things that global variable initializer can refer to 106 // Now that all of the things that global variable initializer can refer to
166 } 168 }
167 169
168 extern "C" { 170 extern "C" {
169 171
170 LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) { 172 LLVMModuleRef LLVMCloneModule(LLVMModuleRef M) {
171 return wrap(CloneModule(unwrap(M))); 173 return wrap(CloneModule(unwrap(M)).release());
172 } 174 }
173 175
174 } 176 }