static void copyComdat(GlobalObject *Dst, const GlobalObject *Src) { const Comdat *SC = Src->getComdat(); if (!SC) return; Comdat *DC = Dst->getParent()->getOrInsertComdat(SC->getName()); DC->setSelectionKind(SC->getSelectionKind()); Dst->setComdat(DC); }
void rewriteComdat(Module &M, GlobalObject *GO, const std::string &Source, const std::string &Target) { if (Comdat *CD = GO->getComdat()) { auto &Comdats = M.getComdatSymbolTable(); Comdat *C = M.getOrInsertComdat(Target); C->setSelectionKind(CD->getSelectionKind()); GO->setComdat(C); Comdats.erase(Comdats.find(Source)); } }
Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV, bool ForAlias) { GlobalValue *DGV = getLinkedToGlobal(SGV); bool ShouldLink = shouldLink(DGV, *SGV); // just missing from map if (ShouldLink) { auto I = ValueMap.find(SGV); if (I != ValueMap.end()) return cast<Constant>(I->second); I = AliasValueMap.find(SGV); if (I != AliasValueMap.end()) return cast<Constant>(I->second); } if (!ShouldLink && ForAlias) DGV = nullptr; // Handle the ultra special appending linkage case first. assert(!DGV || SGV->hasAppendingLinkage() == DGV->hasAppendingLinkage()); if (SGV->hasAppendingLinkage()) return linkAppendingVarProto(cast_or_null<GlobalVariable>(DGV), cast<GlobalVariable>(SGV)); GlobalValue *NewGV; if (DGV && !ShouldLink) { NewGV = DGV; } else { // If we are done linking global value bodies (i.e. we are performing // metadata linking), don't link in the global value due to this // reference, simply map it to null. if (DoneLinkingBodies) return nullptr; NewGV = copyGlobalValueProto(SGV, ShouldLink); if (ShouldLink || !ForAlias) forceRenaming(NewGV, SGV->getName()); } // Overloaded intrinsics have overloaded types names as part of their // names. If we renamed overloaded types we should rename the intrinsic // as well. if (Function *F = dyn_cast<Function>(NewGV)) if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) NewGV = Remangled.getValue(); if (ShouldLink || ForAlias) { if (const Comdat *SC = SGV->getComdat()) { if (auto *GO = dyn_cast<GlobalObject>(NewGV)) { Comdat *DC = DstM.getOrInsertComdat(SC->getName()); DC->setSelectionKind(SC->getSelectionKind()); GO->setComdat(DC); } } } if (!ShouldLink && ForAlias) NewGV->setLinkage(GlobalValue::InternalLinkage); Constant *C = NewGV; if (DGV) C = ConstantExpr::getBitCast(NewGV, TypeMap.get(SGV->getType())); if (DGV && NewGV != DGV) { DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType())); DGV->eraseFromParent(); } return C; }