Mercurial > hg > Members > tobaru > cbc > CbC_llvm
comparison lib/Transforms/Utils/CloneModule.cpp @ 120:1172e4bd9c6f
update 4.0.0
author | mir3636 |
---|---|
date | Fri, 25 Nov 2016 19:14:25 +0900 |
parents | 7d135dc70f03 |
children | 803732b1fca8 |
comparison
equal
deleted
inserted
replaced
101:34baf5011add | 120:1172e4bd9c6f |
---|---|
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 std::unique_ptr<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 function_ref<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 std::unique_ptr<Module> New = | 43 std::unique_ptr<Module> New = |
44 llvm::make_unique<Module>(M->getModuleIdentifier(), M->getContext()); | 44 llvm::make_unique<Module>(M->getModuleIdentifier(), M->getContext()); |
45 New->setDataLayout(M->getDataLayout()); | 45 New->setDataLayout(M->getDataLayout()); |
46 New->setTargetTriple(M->getTargetTriple()); | 46 New->setTargetTriple(M->getTargetTriple()); |
62 GV->copyAttributesFrom(&*I); | 62 GV->copyAttributesFrom(&*I); |
63 VMap[&*I] = GV; | 63 VMap[&*I] = GV; |
64 } | 64 } |
65 | 65 |
66 // 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 |
67 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { | 67 for (const Function &I : *M) { |
68 Function *NF = | 68 Function *NF = Function::Create(cast<FunctionType>(I.getValueType()), |
69 Function::Create(cast<FunctionType>(I->getValueType()), | 69 I.getLinkage(), I.getName(), New.get()); |
70 I->getLinkage(), I->getName(), New.get()); | 70 NF->copyAttributesFrom(&I); |
71 NF->copyAttributesFrom(&*I); | 71 VMap[&I] = NF; |
72 VMap[&*I] = NF; | |
73 } | 72 } |
74 | 73 |
75 // Loop over the aliases in the module | 74 // Loop over the aliases in the module |
76 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); | 75 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
77 I != E; ++I) { | 76 I != E; ++I) { |
107 // have been created, loop through and copy the global variable referrers | 106 // have been created, loop through and copy the global variable referrers |
108 // over... We also set the attributes on the global now. | 107 // over... We also set the attributes on the global now. |
109 // | 108 // |
110 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); | 109 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); |
111 I != E; ++I) { | 110 I != E; ++I) { |
111 if (I->isDeclaration()) | |
112 continue; | |
113 | |
112 GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]); | 114 GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]); |
113 if (!ShouldCloneDefinition(&*I)) { | 115 if (!ShouldCloneDefinition(&*I)) { |
114 // Skip after setting the correct linkage for an external reference. | 116 // Skip after setting the correct linkage for an external reference. |
115 GV->setLinkage(GlobalValue::ExternalLinkage); | 117 GV->setLinkage(GlobalValue::ExternalLinkage); |
116 continue; | 118 continue; |
117 } | 119 } |
118 if (I->hasInitializer()) | 120 if (I->hasInitializer()) |
119 GV->setInitializer(MapValue(I->getInitializer(), VMap)); | 121 GV->setInitializer(MapValue(I->getInitializer(), VMap)); |
122 | |
123 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs; | |
124 I->getAllMetadata(MDs); | |
125 for (auto MD : MDs) | |
126 GV->addMetadata(MD.first, *MapMetadata(MD.second, VMap)); | |
120 } | 127 } |
121 | 128 |
122 // Similarly, copy over function bodies now... | 129 // Similarly, copy over function bodies now... |
123 // | 130 // |
124 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { | 131 for (const Function &I : *M) { |
125 Function *F = cast<Function>(VMap[&*I]); | 132 if (I.isDeclaration()) |
126 if (!ShouldCloneDefinition(&*I)) { | 133 continue; |
134 | |
135 Function *F = cast<Function>(VMap[&I]); | |
136 if (!ShouldCloneDefinition(&I)) { | |
127 // Skip after setting the correct linkage for an external reference. | 137 // Skip after setting the correct linkage for an external reference. |
128 F->setLinkage(GlobalValue::ExternalLinkage); | 138 F->setLinkage(GlobalValue::ExternalLinkage); |
139 // Personality function is not valid on a declaration. | |
140 F->setPersonalityFn(nullptr); | |
129 continue; | 141 continue; |
130 } | 142 } |
131 if (!I->isDeclaration()) { | |
132 Function::arg_iterator DestI = F->arg_begin(); | |
133 for (Function::const_arg_iterator J = I->arg_begin(); J != I->arg_end(); | |
134 ++J) { | |
135 DestI->setName(J->getName()); | |
136 VMap[&*J] = &*DestI++; | |
137 } | |
138 | 143 |
139 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned. | 144 Function::arg_iterator DestI = F->arg_begin(); |
140 CloneFunctionInto(F, &*I, VMap, /*ModuleLevelChanges=*/true, Returns); | 145 for (Function::const_arg_iterator J = I.arg_begin(); J != I.arg_end(); |
146 ++J) { | |
147 DestI->setName(J->getName()); | |
148 VMap[&*J] = &*DestI++; | |
141 } | 149 } |
142 | 150 |
143 if (I->hasPersonalityFn()) | 151 SmallVector<ReturnInst *, 8> Returns; // Ignore returns cloned. |
144 F->setPersonalityFn(MapValue(I->getPersonalityFn(), VMap)); | 152 CloneFunctionInto(F, &I, VMap, /*ModuleLevelChanges=*/true, Returns); |
153 | |
154 if (I.hasPersonalityFn()) | |
155 F->setPersonalityFn(MapValue(I.getPersonalityFn(), VMap)); | |
145 } | 156 } |
146 | 157 |
147 // And aliases | 158 // And aliases |
148 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); | 159 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
149 I != E; ++I) { | 160 I != E; ++I) { |