Skip to content

Commit 368a5e3

Browse files
committed
[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
1 parent 3521ecf commit 368a5e3

File tree

11 files changed

+53
-48
lines changed

11 files changed

+53
-48
lines changed

llvm/include/llvm/IR/DataLayout.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,13 +574,26 @@ class DataLayout {
574574
/// Returns the preferred alignment of the specified global.
575575
///
576576
/// This includes an explicitly requested alignment (if the global has one).
577-
unsigned getPreferredAlignment(const GlobalVariable *GV) const;
577+
Align getPreferredAlign(const GlobalVariable *GV) const;
578+
579+
/// Returns the preferred alignment of the specified global.
580+
///
581+
/// This includes an explicitly requested alignment (if the global has one).
582+
LLVM_ATTRIBUTE_DEPRECATED(
583+
inline unsigned getPreferredAlignment(const GlobalVariable *GV) const,
584+
"Use getPreferredAlign instead") {
585+
return getPreferredAlign(GV).value();
586+
}
578587

579588
/// Returns the preferred alignment of the specified global, returned
580589
/// in log form.
581590
///
582591
/// This includes an explicitly requested alignment (if the global has one).
583-
unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
592+
LLVM_ATTRIBUTE_DEPRECATED(
593+
inline unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const,
594+
"Inline where needed") {
595+
return Log2(getPreferredAlign(GV));
596+
}
584597
};
585598

586599
inline DataLayout *unwrap(LLVMTargetDataRef P) {

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ Align AsmPrinter::getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
161161
Align InAlign) {
162162
Align Alignment;
163163
if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
164-
Alignment = Align(DL.getPreferredAlignment(GVar));
164+
Alignment = DL.getPreferredAlign(GVar);
165165

166166
// If InAlign is specified, round it to it.
167167
if (InAlign > Alignment)

llvm/lib/CodeGen/GlobalMerge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
464464
Type *Ty = Globals[j]->getValueType();
465465

466466
// Make sure we use the same alignment AsmPrinter would use.
467-
Align Alignment(DL.getPreferredAlignment(Globals[j]));
467+
Align Alignment = DL.getPreferredAlign(Globals[j]);
468468
unsigned Padding = alignTo(MergedSize, Alignment) - MergedSize;
469469
MergedSize += Padding;
470470
MergedSize += DL.getTypeAllocSize(Ty);

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -599,11 +599,11 @@ getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
599599
// We also need alignment here.
600600
// FIXME: this is getting the alignment of the character, not the
601601
// alignment of the global!
602-
unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
602+
Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign(
603603
cast<GlobalVariable>(GO));
604604

605605
std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
606-
Name = SizeSpec + utostr(Align);
606+
Name = SizeSpec + utostr(Alignment.value());
607607
} else if (Kind.isMergeableConst()) {
608608
Name = ".rodata.cst";
609609
Name += utostr(EntrySize);
@@ -1145,16 +1145,16 @@ MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
11451145

11461146
// FIXME: Alignment check should be handled by section classifier.
11471147
if (Kind.isMergeable1ByteCString() &&
1148-
GO->getParent()->getDataLayout().getPreferredAlignment(
1149-
cast<GlobalVariable>(GO)) < 32)
1148+
GO->getParent()->getDataLayout().getPreferredAlign(
1149+
cast<GlobalVariable>(GO)) < Align(32))
11501150
return CStringSection;
11511151

11521152
// Do not put 16-bit arrays in the UString section if they have an
11531153
// externally visible label, this runs into issues with certain linker
11541154
// versions.
11551155
if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
1156-
GO->getParent()->getDataLayout().getPreferredAlignment(
1157-
cast<GlobalVariable>(GO)) < 32)
1156+
GO->getParent()->getDataLayout().getPreferredAlign(
1157+
cast<GlobalVariable>(GO)) < Align(32))
11581158
return UStringSection;
11591159

11601160
// With MachO only variables whose corresponding symbol starts with 'l' or
@@ -2043,13 +2043,13 @@ MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal(
20432043
}
20442044

20452045
if (Kind.isMergeableCString()) {
2046-
unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
2046+
Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign(
20472047
cast<GlobalVariable>(GO));
20482048

20492049
unsigned EntrySize = getEntrySizeForKind(Kind);
20502050
std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
20512051
SmallString<128> Name;
2052-
Name = SizeSpec + utostr(Align);
2052+
Name = SizeSpec + utostr(Alignment.value());
20532053

20542054
return getContext().getXCOFFSection(
20552055
Name, XCOFF::XMC_RO, XCOFF::XTY_SD,

llvm/lib/ExecutionEngine/ExecutionEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class GVMemoryBlock final : public CallbackVH {
108108
Type *ElTy = GV->getValueType();
109109
size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
110110
void *RawMemory = ::operator new(
111-
alignTo(sizeof(GVMemoryBlock), TD.getPreferredAlignment(GV)) + GVSize);
111+
alignTo(sizeof(GVMemoryBlock), TD.getPreferredAlign(GV)) + GVSize);
112112
new(RawMemory) GVMemoryBlock(GV);
113113
return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
114114
}

llvm/lib/IR/DataLayout.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -846,15 +846,14 @@ int64_t DataLayout::getIndexedOffsetInType(Type *ElemTy,
846846
return Result;
847847
}
848848

849-
/// getPreferredAlignment - Return the preferred alignment of the specified
850-
/// global. This includes an explicitly requested alignment (if the global
851-
/// has one).
852-
unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const {
853-
unsigned GVAlignment = GV->getAlignment();
849+
/// getPreferredAlign - Return the preferred alignment of the specified global.
850+
/// This includes an explicitly requested alignment (if the global has one).
851+
Align DataLayout::getPreferredAlign(const GlobalVariable *GV) const {
852+
MaybeAlign GVAlignment = GV->getAlign();
854853
// If a section is specified, always precisely honor explicit alignment,
855854
// so we don't insert padding into a section we don't control.
856855
if (GVAlignment && GV->hasSection())
857-
return GVAlignment;
856+
return *GVAlignment;
858857

859858
// If no explicit alignment is specified, compute the alignment based on
860859
// 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 {
863862
// FIXME: Not sure it makes sense to use the alignment of the type if
864863
// there's already an explicit alignment specification.
865864
Type *ElemType = GV->getValueType();
866-
unsigned Alignment = getPrefTypeAlignment(ElemType);
867-
if (GVAlignment >= Alignment) {
868-
Alignment = GVAlignment;
869-
} else if (GVAlignment != 0) {
870-
Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType));
865+
Align Alignment = getPrefTypeAlign(ElemType);
866+
if (GVAlignment) {
867+
if (*GVAlignment >= Alignment)
868+
Alignment = *GVAlignment;
869+
else
870+
Alignment = std::max(*GVAlignment, getABITypeAlign(ElemType));
871871
}
872872

873873
// If no explicit alignment is specified, and the global is large, increase
874874
// the alignment to 16.
875875
// FIXME: Why 16, specifically?
876-
if (GV->hasInitializer() && GVAlignment == 0) {
877-
if (Alignment < 16) {
876+
if (GV->hasInitializer() && !GVAlignment) {
877+
if (Alignment < Align(16)) {
878878
// If the global is not external, see if it is large. If so, give it a
879879
// larger alignment.
880880
if (getTypeSizeInBits(ElemType) > 128)
881-
Alignment = 16; // 16-byte alignment.
881+
Alignment = Align(16); // 16-byte alignment.
882882
}
883883
}
884884
return Alignment;
885885
}
886-
887-
/// getPreferredAlignmentLog - Return the preferred alignment of the
888-
/// specified global, returned in log form. This includes an explicitly
889-
/// requested alignment (if the global has one).
890-
unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
891-
return Log2_32(getPreferredAlignment(GV));
892-
}

llvm/lib/IR/Value.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ Align Value::getPointerAlignment(const DataLayout &DL) const {
768768
// it the preferred alignment. Otherwise, we have to assume that it
769769
// may only have the minimum ABI alignment.
770770
if (GVar->isStrongDefinitionForLinker())
771-
return Align(DL.getPreferredAlignment(GVar));
771+
return DL.getPreferredAlign(GVar);
772772
else
773773
return DL.getABITypeAlign(ObjectType);
774774
}

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3571,7 +3571,7 @@ static SDValue promoteToConstantPool(const ARMTargetLowering *TLI,
35713571
// that are strings for simplicity.
35723572
auto *CDAInit = dyn_cast<ConstantDataArray>(Init);
35733573
unsigned Size = DAG.getDataLayout().getTypeAllocSize(Init->getType());
3574-
unsigned PrefAlign = DAG.getDataLayout().getPreferredAlignment(GVar);
3574+
Align PrefAlign = DAG.getDataLayout().getPreferredAlign(GVar);
35753575
unsigned RequiredPadding = 4 - (Size % 4);
35763576
bool PaddingPossible =
35773577
RequiredPadding == 4 || (CDAInit && CDAInit->isString());

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,16 +1702,15 @@ void PPCAIXAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
17021702

17031703
// Handle common symbols.
17041704
if (GVKind.isCommon() || GVKind.isBSSLocal()) {
1705-
unsigned Align =
1706-
GV->getAlignment() ? GV->getAlignment() : DL.getPreferredAlignment(GV);
1705+
Align Alignment = GV->getAlign().getValueOr(DL.getPreferredAlign(GV));
17071706
uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
17081707

17091708
if (GVKind.isBSSLocal())
17101709
OutStreamer->emitXCOFFLocalCommonSymbol(
17111710
OutContext.getOrCreateSymbol(GVSym->getUnqualifiedName()), Size,
1712-
GVSym, Align);
1711+
GVSym, Alignment.value());
17131712
else
1714-
OutStreamer->emitCommonSymbol(GVSym, Size, Align);
1713+
OutStreamer->emitCommonSymbol(GVSym, Size, Alignment.value());
17151714
return;
17161715
}
17171716

llvm/lib/Target/Target.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
124124

125125
unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
126126
LLVMValueRef GlobalVar) {
127-
return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
127+
return unwrap(TD)
128+
->getPreferredAlign(unwrap<GlobalVariable>(GlobalVar))
129+
.value();
128130
}
129131

130132
unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,

llvm/lib/Transforms/IPO/ConstantMerge.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,9 @@ static void copyDebugLocMetadata(const GlobalVariable *From,
8484
To->addDebugInfo(MD);
8585
}
8686

87-
static unsigned getAlignment(GlobalVariable *GV) {
88-
unsigned Align = GV->getAlignment();
89-
if (Align)
90-
return Align;
91-
return GV->getParent()->getDataLayout().getPreferredAlignment(GV);
87+
static Align getAlign(GlobalVariable *GV) {
88+
return GV->getAlign().getValueOr(
89+
GV->getParent()->getDataLayout().getPreferredAlign(GV));
9290
}
9391

9492
static bool
@@ -120,8 +118,8 @@ static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New) {
120118
<< New->getName() << "\n");
121119

122120
// Bump the alignment if necessary.
123-
if (Old->getAlignment() || New->getAlignment())
124-
New->setAlignment(Align(std::max(getAlignment(Old), getAlignment(New))));
121+
if (Old->getAlign() || New->getAlign())
122+
New->setAlignment(std::max(getAlign(Old), getAlign(New)));
125123

126124
copyDebugLocMetadata(Old, New);
127125
Old->replaceAllUsesWith(NewConstant);

0 commit comments

Comments
 (0)