bool SrcIsDeclaration = isDeclaration(Src);
bool DestIsDeclaration = isDeclaration(Dest);
+ // FIXME: Make datalayout mandatory and just use getDataLayout().
+ DataLayout DL(Dest.getParent());
+
if (SrcIsDeclaration) {
// If Src is external or if both Src & Dest are external.. Just link the
// external globals, we aren't adding anything.
// If Dest is external but Src is not:
return true;
+ if (Src.hasCommonLinkage()) {
+ if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage())
+ return true;
+
+ if (!Dest.hasCommonLinkage())
+ return false;
+
+ uint64_t DestSize = DL.getTypeAllocSize(Dest.getType()->getElementType());
+ uint64_t SrcSize = DL.getTypeAllocSize(Src.getType()->getElementType());
+ return SrcSize > DestSize;
+ }
+
if (Src.isWeakForLinker()) {
assert(!Dest.hasExternalWeakLinkage());
assert(!Dest.hasAvailableExternallyLinkage());
+
if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage())
return true;
- return (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) &&
- Src.hasCommonLinkage();
+ return false;
}
if (Dest.isWeakForLinker()) {
GlobalValue *DGV = getLinkedToGlobal(SGV);
llvm::Optional<GlobalValue::VisibilityTypes> NewVisibility;
bool HasUnnamedAddr = SGV->hasUnnamedAddr();
+ unsigned Alignment = SGV->getAlignment();
bool LinkFromSrc = false;
Comdat *DC = nullptr;
return true;
NewVisibility = NV;
HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
+ if (DGV->hasCommonLinkage() && SGV->hasCommonLinkage())
+ Alignment = std::max(Alignment, DGV->getAlignment());
+ else if (!LinkFromSrc)
+ Alignment = DGV->getAlignment();
// If we're not linking from the source, then keep the definition that we
// have.
if (!LinkFromSrc) {
// Special case for const propagation.
- if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
+ if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) {
+ DGVar->setAlignment(Alignment);
+
if (DGVar->isDeclaration() && SGV->isConstant() &&
!DGVar->isConstant())
DGVar->setConstant(true);
+ }
// Set calculated linkage, visibility and unnamed_addr.
DGV->setLinkage(NewLinkage);
SGV->getType()->getAddressSpace());
// Propagate alignment, visibility and section info.
copyGVAttributes(NewDGV, SGV);
+ NewDGV->setAlignment(Alignment);
if (NewVisibility)
NewDGV->setVisibility(*NewVisibility);
NewDGV->setUnnamedAddr(HasUnnamedAddr);