diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h --- a/llvm/include/llvm/IR/DataLayout.h +++ b/llvm/include/llvm/IR/DataLayout.h @@ -574,13 +574,26 @@ /// 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 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -161,7 +161,7 @@ Align InAlign) { Align Alignment; if (const GlobalVariable *GVar = dyn_cast(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 --- a/llvm/lib/CodeGen/GlobalMerge.cpp +++ b/llvm/lib/CodeGen/GlobalMerge.cpp @@ -464,7 +464,7 @@ 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 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -599,11 +599,11 @@ // 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(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 @@ // FIXME: Alignment check should be handled by section classifier. if (Kind.isMergeable1ByteCString() && - GO->getParent()->getDataLayout().getPreferredAlignment( - cast(GO)) < 32) + GO->getParent()->getDataLayout().getPreferredAlign( + cast(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(GO)) < 32) + GO->getParent()->getDataLayout().getPreferredAlign( + cast(GO)) < Align(32)) return UStringSection; // With MachO only variables whose corresponding symbol starts with 'l' or @@ -2043,13 +2043,13 @@ } if (Kind.isMergeableCString()) { - unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment( + Align Alignment = GO->getParent()->getDataLayout().getPreferredAlign( cast(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 --- a/llvm/lib/ExecutionEngine/ExecutionEngine.cpp +++ b/llvm/lib/ExecutionEngine/ExecutionEngine.cpp @@ -108,7 +108,7 @@ 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(RawMemory) + sizeof(GVMemoryBlock); } diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp --- a/llvm/lib/IR/DataLayout.cpp +++ b/llvm/lib/IR/DataLayout.cpp @@ -846,15 +846,14 @@ 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 @@ // 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 --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -768,7 +768,7 @@ // 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 --- a/llvm/lib/Target/ARM/ARMISelLowering.cpp +++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp @@ -3571,7 +3571,7 @@ // that are strings for simplicity. auto *CDAInit = dyn_cast(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 --- a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp +++ b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp @@ -1702,16 +1702,15 @@ // 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 --- a/llvm/lib/Target/Target.cpp +++ b/llvm/lib/Target/Target.cpp @@ -124,7 +124,9 @@ unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD, LLVMValueRef GlobalVar) { - return unwrap(TD)->getPreferredAlignment(unwrap(GlobalVar)); + return unwrap(TD) + ->getPreferredAlign(unwrap(GlobalVar)) + .value(); } unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy, diff --git a/llvm/lib/Transforms/IPO/ConstantMerge.cpp b/llvm/lib/Transforms/IPO/ConstantMerge.cpp --- a/llvm/lib/Transforms/IPO/ConstantMerge.cpp +++ b/llvm/lib/Transforms/IPO/ConstantMerge.cpp @@ -84,11 +84,9 @@ 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 @@ << 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);