- ValueMap[SGV] = NewDGV;
-
- // Keep track that this is an appending variable.
- if (SGV->hasAppendingLinkage())
- AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
- continue;
- }
-
- // If the visibilities of the symbols disagree and the destination is a
- // prototype, take the visibility of its input.
- if (DGV->isDeclaration())
- DGV->setVisibility(SGV->getVisibility());
-
- if (DGV->hasAppendingLinkage()) {
- // No linking is performed yet. Just insert a new copy of the global, and
- // keep track of the fact that it is an appending variable in the
- // AppendingVars map. The name is cleared out so that no linkage is
- // performed.
- GlobalVariable *NewDGV =
- new GlobalVariable(*Dest, SGV->getType()->getElementType(),
- SGV->isConstant(), SGV->getLinkage(), /*init*/0,
- "", 0, false,
- SGV->getType()->getAddressSpace());
-
- // Set alignment allowing CopyGVAttributes merge it with alignment of SGV.
- NewDGV->setAlignment(DGV->getAlignment());
- // Propagate alignment, section and visibility info.
- CopyGVAttributes(NewDGV, SGV);
-
- // Make sure to remember this mapping...
- ValueMap[SGV] = NewDGV;
-
- // Keep track that this is an appending variable...
- AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
- continue;
- }
-
- if (LinkFromSrc) {
- if (isa<GlobalAlias>(DGV))
- return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
- "': symbol multiple defined");
-
- // If the types don't match, and if we are to link from the source, nuke
- // DGV and create a new one of the appropriate type. Note that the thing
- // we are replacing may be a function (if a prototype, weak, etc) or a
- // global variable.
- GlobalVariable *NewDGV =
- new GlobalVariable(*Dest, SGV->getType()->getElementType(),
- SGV->isConstant(), NewLinkage, /*init*/0,
- DGV->getName(), 0, false,
- SGV->getType()->getAddressSpace());
-
- // Propagate alignment, section, and visibility info.
- CopyGVAttributes(NewDGV, SGV);
- DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV,
- DGV->getType()));
-
- // DGV will conflict with NewDGV because they both had the same
- // name. We must erase this now so ForceRenaming doesn't assert
- // because DGV might not have internal linkage.
- if (GlobalVariable *Var = dyn_cast<GlobalVariable>(DGV))
- Var->eraseFromParent();
- else
- cast<Function>(DGV)->eraseFromParent();
-
- // If the symbol table renamed the global, but it is an externally visible
- // symbol, DGV must be an existing global with internal linkage. Rename.
- if (NewDGV->getName() != SGV->getName() && !NewDGV->hasLocalLinkage())
- ForceRenaming(NewDGV, SGV->getName());
-
- // Inherit const as appropriate.
- NewDGV->setConstant(SGV->isConstant());
-
- // Make sure to remember this mapping.
- ValueMap[SGV] = NewDGV;
- continue;
- }
-
- // Not "link from source", keep the one in the DestModule and remap the
- // input onto it.
-
- // Special case for const propagation.
- if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
- if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
- DGVar->setConstant(true);
-
- // SGV is global, but DGV is alias.
- if (isa<GlobalAlias>(DGV)) {
- // The only valid mappings are:
- // - SGV is external declaration, which is effectively a no-op.
- // - SGV is weak, when we just need to throw SGV out.
- if (!SGV->isDeclaration() && !SGV->isWeakForLinker())
- return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
- "': symbol multiple defined");