[llvm] 368a5e3 - [Alignment][NFC] migrate DataLayout::getPreferredAlignment
Guillaume Chatelet via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 29 04:30:08 PDT 2020
Author: Guillaume Chatelet
Date: 2020-06-29T11:24:36Z
New Revision: 368a5e3a666ff38432be538370c46ff800fface1
URL: https://github.com/llvm/llvm-project/commit/368a5e3a666ff38432be538370c46ff800fface1
DIFF: https://github.com/llvm/llvm-project/commit/368a5e3a666ff38432be538370c46ff800fface1.diff
LOG: [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
Added:
Modified:
llvm/include/llvm/IR/DataLayout.h
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/lib/CodeGen/GlobalMerge.cpp
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
llvm/lib/ExecutionEngine/ExecutionEngine.cpp
llvm/lib/IR/DataLayout.cpp
llvm/lib/IR/Value.cpp
llvm/lib/Target/ARM/ARMISelLowering.cpp
llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
llvm/lib/Target/Target.cpp
llvm/lib/Transforms/IPO/ConstantMerge.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h
index ed2336265ab6..703670c9e1db 100644
--- a/llvm/include/llvm/IR/DataLayout.h
+++ b/llvm/include/llvm/IR/DataLayout.h
@@ -574,13 +574,26 @@ class DataLayout {
/// Returns the preferred alignment of the specified global.
///
/// This includes an explicitly requested alignment (if the global has one).
- unsigned getPreferredAlignment(const GlobalVariable *GV) const;
+ Align getPreferredAlign(const GlobalVariable *GV) const;
+
+ /// Returns the preferred alignment of the specified global.
+ ///
+ /// This includes an explicitly requested alignment (if the global has one).
+ LLVM_ATTRIBUTE_DEPRECATED(
+ inline unsigned getPreferredAlignment(const GlobalVariable *GV) const,
+ "Use getPreferredAlign instead") {
+ return getPreferredAlign(GV).value();
+ }
/// Returns the preferred alignment of the specified global, returned
/// in log form.
///
/// This includes an explicitly requested alignment (if the global has one).
- unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const;
+ LLVM_ATTRIBUTE_DEPRECATED(
+ inline unsigned getPreferredAlignmentLog(const GlobalVariable *GV) const,
+ "Inline where needed") {
+ return Log2(getPreferredAlign(GV));
+ }
};
inline DataLayout *unwrap(LLVMTargetDataRef P) {
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 1540a19687c5..ef61bcb605eb 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -161,7 +161,7 @@ Align AsmPrinter::getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
Align InAlign) {
Align Alignment;
if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
- Alignment = Align(DL.getPreferredAlignment(GVar));
+ Alignment = DL.getPreferredAlign(GVar);
// If InAlign is specified, round it to it.
if (InAlign > Alignment)
diff --git a/llvm/lib/CodeGen/GlobalMerge.cpp b/llvm/lib/CodeGen/GlobalMerge.cpp
index 6d736aa981a3..1e20c02ba160 100644
--- a/llvm/lib/CodeGen/GlobalMerge.cpp
+++ b/llvm/lib/CodeGen/GlobalMerge.cpp
@@ -464,7 +464,7 @@ bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
Type *Ty = Globals[j]->getValueType();
// Make sure we use the same alignment AsmPrinter would use.
- Align Alignment(DL.getPreferredAlignment(Globals[j]));
+ Align Alignment = DL.getPreferredAlign(Globals[j]);
unsigned Padding = alignTo(MergedSize, Alignment) - MergedSize;
MergedSize += Padding;
MergedSize += DL.getTypeAllocSize(Ty);
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 03254ed77d71..8d670300933f 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -599,11 +599,11 @@ getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
// We also need alignment here.
// FIXME: this is getting the alignment of the character, not the
// alignment of the global!
- unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
+ Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign(
cast<GlobalVariable>(GO));
std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
- Name = SizeSpec + utostr(Align);
+ Name = SizeSpec + utostr(Alignment.value());
} else if (Kind.isMergeableConst()) {
Name = ".rodata.cst";
Name += utostr(EntrySize);
@@ -1145,16 +1145,16 @@ MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
// FIXME: Alignment check should be handled by section classifier.
if (Kind.isMergeable1ByteCString() &&
- GO->getParent()->getDataLayout().getPreferredAlignment(
- cast<GlobalVariable>(GO)) < 32)
+ GO->getParent()->getDataLayout().getPreferredAlign(
+ cast<GlobalVariable>(GO)) < Align(32))
return CStringSection;
// Do not put 16-bit arrays in the UString section if they have an
// externally visible label, this runs into issues with certain linker
// versions.
if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
- GO->getParent()->getDataLayout().getPreferredAlignment(
- cast<GlobalVariable>(GO)) < 32)
+ GO->getParent()->getDataLayout().getPreferredAlign(
+ cast<GlobalVariable>(GO)) < Align(32))
return UStringSection;
// With MachO only variables whose corresponding symbol starts with 'l' or
@@ -2043,13 +2043,13 @@ MCSection *TargetLoweringObjectFileXCOFF::SelectSectionForGlobal(
}
if (Kind.isMergeableCString()) {
- unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
+ Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign(
cast<GlobalVariable>(GO));
unsigned EntrySize = getEntrySizeForKind(Kind);
std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
SmallString<128> Name;
- Name = SizeSpec + utostr(Align);
+ Name = SizeSpec + utostr(Alignment.value());
return getContext().getXCOFFSection(
Name, XCOFF::XMC_RO, XCOFF::XTY_SD,
diff --git a/llvm/lib/ExecutionEngine/ExecutionEngine.cpp b/llvm/lib/ExecutionEngine/ExecutionEngine.cpp
index 6e8f88e21c8b..6b384f1d0eba 100644
--- a/llvm/lib/ExecutionEngine/ExecutionEngine.cpp
+++ b/llvm/lib/ExecutionEngine/ExecutionEngine.cpp
@@ -108,7 +108,7 @@ class GVMemoryBlock final : public CallbackVH {
Type *ElTy = GV->getValueType();
size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
void *RawMemory = ::operator new(
- alignTo(sizeof(GVMemoryBlock), TD.getPreferredAlignment(GV)) + GVSize);
+ alignTo(sizeof(GVMemoryBlock), TD.getPreferredAlign(GV)) + GVSize);
new(RawMemory) GVMemoryBlock(GV);
return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
}
diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp
index 58ef87a21ecb..b0613c808c0b 100644
--- a/llvm/lib/IR/DataLayout.cpp
+++ b/llvm/lib/IR/DataLayout.cpp
@@ -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));
-}
diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp
index e264eb1f7d72..cfb2bcd778dc 100644
--- a/llvm/lib/IR/Value.cpp
+++ b/llvm/lib/IR/Value.cpp
@@ -768,7 +768,7 @@ Align Value::getPointerAlignment(const DataLayout &DL) const {
// it the preferred alignment. Otherwise, we have to assume that it
// may only have the minimum ABI alignment.
if (GVar->isStrongDefinitionForLinker())
- return Align(DL.getPreferredAlignment(GVar));
+ return DL.getPreferredAlign(GVar);
else
return DL.getABITypeAlign(ObjectType);
}
diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp
index 3b1b7040f7a4..9c4cd10a9a13 100644
--- a/llvm/lib/Target/ARM/ARMISelLowering.cpp
+++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp
@@ -3571,7 +3571,7 @@ static SDValue promoteToConstantPool(const ARMTargetLowering *TLI,
// that are strings for simplicity.
auto *CDAInit = dyn_cast<ConstantDataArray>(Init);
unsigned Size = DAG.getDataLayout().getTypeAllocSize(Init->getType());
- unsigned PrefAlign = DAG.getDataLayout().getPreferredAlignment(GVar);
+ Align PrefAlign = DAG.getDataLayout().getPreferredAlign(GVar);
unsigned RequiredPadding = 4 - (Size % 4);
bool PaddingPossible =
RequiredPadding == 4 || (CDAInit && CDAInit->isString());
diff --git a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
index 4f92d1092def..7aa4165e90c0 100644
--- a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
+++ b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
@@ -1702,16 +1702,15 @@ void PPCAIXAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
// Handle common symbols.
if (GVKind.isCommon() || GVKind.isBSSLocal()) {
- unsigned Align =
- GV->getAlignment() ? GV->getAlignment() : DL.getPreferredAlignment(GV);
+ Align Alignment = GV->getAlign().getValueOr(DL.getPreferredAlign(GV));
uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
if (GVKind.isBSSLocal())
OutStreamer->emitXCOFFLocalCommonSymbol(
OutContext.getOrCreateSymbol(GVSym->getUnqualifiedName()), Size,
- GVSym, Align);
+ GVSym, Alignment.value());
else
- OutStreamer->emitCommonSymbol(GVSym, Size, Align);
+ OutStreamer->emitCommonSymbol(GVSym, Size, Alignment.value());
return;
}
diff --git a/llvm/lib/Target/Target.cpp b/llvm/lib/Target/Target.cpp
index 8a46c77492c5..ade097111fbe 100644
--- a/llvm/lib/Target/Target.cpp
+++ b/llvm/lib/Target/Target.cpp
@@ -124,7 +124,9 @@ unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
LLVMValueRef GlobalVar) {
- return unwrap(TD)->getPreferredAlignment(unwrap<GlobalVariable>(GlobalVar));
+ return unwrap(TD)
+ ->getPreferredAlign(unwrap<GlobalVariable>(GlobalVar))
+ .value();
}
unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
diff --git a/llvm/lib/Transforms/IPO/ConstantMerge.cpp b/llvm/lib/Transforms/IPO/ConstantMerge.cpp
index ea1278aa108f..67f1438b9b6a 100644
--- a/llvm/lib/Transforms/IPO/ConstantMerge.cpp
+++ b/llvm/lib/Transforms/IPO/ConstantMerge.cpp
@@ -84,11 +84,9 @@ static void copyDebugLocMetadata(const GlobalVariable *From,
To->addDebugInfo(MD);
}
-static unsigned getAlignment(GlobalVariable *GV) {
- unsigned Align = GV->getAlignment();
- if (Align)
- return Align;
- return GV->getParent()->getDataLayout().getPreferredAlignment(GV);
+static Align getAlign(GlobalVariable *GV) {
+ return GV->getAlign().getValueOr(
+ GV->getParent()->getDataLayout().getPreferredAlign(GV));
}
static bool
@@ -120,8 +118,8 @@ static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New) {
<< New->getName() << "\n");
// Bump the alignment if necessary.
- if (Old->getAlignment() || New->getAlignment())
- New->setAlignment(Align(std::max(getAlignment(Old), getAlignment(New))));
+ if (Old->getAlign() || New->getAlign())
+ New->setAlignment(std::max(getAlign(Old), getAlign(New)));
copyDebugLocMetadata(Old, New);
Old->replaceAllUsesWith(NewConstant);
More information about the llvm-commits
mailing list