[Alignment][NFC] migrate DataLayout::getPreferredAlignment

This patch is part of a series to introduce an Alignment type.
See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html
See this patch for the introduction of the type: https://reviews.llvm.org/D64790

Differential Revision: https://reviews.llvm.org/D82752
This commit is contained in:
Guillaume Chatelet
2020-06-29 11:24:36 +00:00
parent 3521ecf1f8
commit 368a5e3a66
11 changed files with 53 additions and 48 deletions

View File

@@ -846,15 +846,14 @@ int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
return Result;
}
/// getPreferredAlignment - Return the preferred alignment of the specified
/// global. This includes an explicitly requested alignment (if the global
/// has one).
unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
unsigned GVAlignment = GV->getAlignment();
/// getPreferredAlign - Return the preferred alignment of the specified global.
/// This includes an explicitly requested alignment (if the global has one).
Align DataLayout::getPreferredAlign(const GlobalVariable *GV) const {
MaybeAlign GVAlignment = GV->getAlign();
// If a section is specified, always precisely honor explicit alignment,
// so we don't insert padding into a section we don't control.
if (GVAlignment && GV->hasSection())
return GVAlignment;
return *GVAlignment;
// If no explicit alignment is specified, compute the alignment based on
// the IR type. If an alignment is specified, increase it to match the ABI
@@ -863,30 +862,24 @@ unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
// FIXME: Not sure it makes sense to use the alignment of the type if
// there's already an explicit alignment specification.
Type *ElemType = GV->getValueType();
unsigned Alignment = getPrefTypeAlignment(ElemType);
if (GVAlignment >= Alignment) {
Alignment = GVAlignment;
} else if (GVAlignment != 0) {
Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
Align Alignment = getPrefTypeAlign(ElemType);
if (GVAlignment) {
if (*GVAlignment >= Alignment)
Alignment = *GVAlignment;
else
Alignment = std::max(*GVAlignment, getABITypeAlign(ElemType));
}
// If no explicit alignment is specified, and the global is large, increase
// the alignment to 16.
// FIXME: Why 16, specifically?
if (GV->hasInitializer() && GVAlignment == 0) {
if (Alignment < 16) {
if (GV->hasInitializer() && !GVAlignment) {
if (Alignment < Align(16)) {
// If the global is not external, see if it is large. If so, give it a
// larger alignment.
if (getTypeSizeInBits(ElemType) > 128)
Alignment = 16; // 16-byte alignment.
Alignment = Align(16); // 16-byte alignment.
}
}
return Alignment;
}
/// getPreferredAlignmentLog - Return the preferred alignment of the
/// specified global, returned in log form. This includes an explicitly
/// requested alignment (if the global has one).
unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
return Log2_32(getPreferredAlignment(GV));
}