- } else if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) {
- // Otherwise, perform the global-global mapping as instructed by
- // GetLinkageResult.
- if (LinkFromSrc) {
- // Propagate alignment, section, and visibility info.
- CopyGVAttributes(DGVar, SGV);
-
- // 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.
- if (SGV->getType() != DGVar->getType()) {
- GlobalVariable *NewDGV =
- new GlobalVariable(SGV->getType()->getElementType(),
- DGVar->isConstant(), DGVar->getLinkage(),
- /*init*/0, DGVar->getName(), Dest, false,
- SGV->getType()->getAddressSpace());
- CopyGVAttributes(NewDGV, DGVar);
- DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDGV,
- DGVar->getType()));
- // DGVar 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.
- DGVar->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 it.
- if (NewDGV->getName() != SGV->getName() &&
- !NewDGV->hasInternalLinkage())
- ForceRenaming(NewDGV, SGV->getName());
-
- DGVar = NewDGV;
- }
-
- // Inherit const as appropriate
- DGVar->setConstant(SGV->isConstant());
-
- // Set initializer to zero, so we can link the stuff later
- DGVar->setInitializer(0);
- } else {
- // Special case for const propagation
- if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
- DGVar->setConstant(true);
- }
-
- // Set calculated linkage
- DGVar->setLinkage(NewLinkage);
+
+ // 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(SGV->getType()->getElementType(), SGV->isConstant(),
+ NewLinkage, /*init*/0, DGV->getName(), Dest, 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();
+ DGV = NewDGV;
+
+ // 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);