GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition) { GlobalValue *NewGV; if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) { NewGV = copyGlobalVariableProto(SGVar); } else if (auto *SF = dyn_cast<Function>(SGV)) { NewGV = copyFunctionProto(SF); } else { if (ForDefinition) NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV)); else NewGV = new GlobalVariable( DstM, TypeMap.get(SGV->getType()->getElementType()), /*isConstant*/ false, GlobalValue::ExternalLinkage, /*init*/ nullptr, SGV->getName(), /*insertbefore*/ nullptr, SGV->getThreadLocalMode(), SGV->getType()->getAddressSpace()); } if (ForDefinition) NewGV->setLinkage(SGV->getLinkage()); else if (SGV->hasExternalWeakLinkage() || SGV->hasWeakLinkage() || SGV->hasLinkOnceLinkage()) NewGV->setLinkage(GlobalValue::ExternalWeakLinkage); NewGV->copyAttributesFrom(SGV); return NewGV; }
static void convertAliasToDeclaration(GlobalAlias &GA, Module &M) { GlobalValue *GVal = GA.getBaseObject(); GlobalValue *NewGV; if (auto *GVar = dyn_cast<GlobalVariable>(GVal)) { GlobalVariable *NewGVar = new GlobalVariable( M, GVar->getType()->getElementType(), GVar->isConstant(), GVar->getLinkage(), /*init*/ nullptr, GA.getName(), GVar, GVar->getThreadLocalMode(), GVar->getType()->getAddressSpace()); NewGV = NewGVar; NewGV->copyAttributesFrom(GVar); } else { auto *F = dyn_cast<Function>(GVal); assert(F); Function *NewF = Function::Create(F->getFunctionType(), F->getLinkage(), GA.getName(), &M); NewGV = NewF; NewGV->copyAttributesFrom(F); } GA.replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, GA.getType())); GA.eraseFromParent(); }
GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition) { GlobalValue *NewGV; if (auto *SGVar = dyn_cast<GlobalVariable>(SGV)) { NewGV = copyGlobalVariableProto(SGVar); } else if (auto *SF = dyn_cast<Function>(SGV)) { NewGV = copyFunctionProto(SF); } else { if (ForDefinition) NewGV = copyGlobalAliasProto(cast<GlobalAlias>(SGV)); else NewGV = new GlobalVariable( DstM, TypeMap.get(SGV->getValueType()), /*isConstant*/ false, GlobalValue::ExternalLinkage, /*init*/ nullptr, SGV->getName(), /*insertbefore*/ nullptr, SGV->getThreadLocalMode(), SGV->getType()->getAddressSpace()); } if (ForDefinition) NewGV->setLinkage(SGV->getLinkage()); else if (SGV->hasExternalWeakLinkage()) NewGV->setLinkage(GlobalValue::ExternalWeakLinkage); NewGV->copyAttributesFrom(SGV); if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) { // Metadata for global variables and function declarations is copied eagerly. if (isa<GlobalVariable>(SGV) || SGV->isDeclaration()) NewGO->copyMetadata(cast<GlobalObject>(SGV), 0); } // Remove these copied constants in case this stays a declaration, since // they point to the source module. If the def is linked the values will // be mapped in during linkFunctionBody. if (auto *NewF = dyn_cast<Function>(NewGV)) { NewF->setPersonalityFn(nullptr); NewF->setPrefixData(nullptr); NewF->setPrologueData(nullptr); } return NewGV; }