diff --git a/llvm/include/llvm/CodeGen/MachineFunction.h b/llvm/include/llvm/CodeGen/MachineFunction.h index ef783f276b7d4..1f8feda4119cc 100644 --- a/llvm/include/llvm/CodeGen/MachineFunction.h +++ b/llvm/include/llvm/CodeGen/MachineFunction.h @@ -818,6 +818,8 @@ class LLVM_ABI MachineFunction { Alignment = A; } + Align getPreferredAlignment() const; + /// exposesReturnsTwice - Returns true if the function calls setjmp or /// any other similar functions with attribute "returns twice" without /// having the attribute itself. diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 10df9c1f97eae..0bc76590d3208 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -978,7 +978,7 @@ void AsmPrinter::emitFunctionHeader() { emitLinkage(&F, CurrentFnSym); if (MAI->hasFunctionAlignment()) - emitAlignment(MF->getAlignment(), &F); + emitAlignment(MF->getPreferredAlignment(), &F); if (MAI->hasDotTypeDotSizeDirective()) OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction); diff --git a/llvm/lib/CodeGen/AsmPrinter/WinException.cpp b/llvm/lib/CodeGen/AsmPrinter/WinException.cpp index 13fd270ec7410..90d8196ffb82a 100644 --- a/llvm/lib/CodeGen/AsmPrinter/WinException.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/WinException.cpp @@ -204,8 +204,8 @@ void WinException::beginFunclet(const MachineBasicBlock &MBB, // We want our funclet's entry point to be aligned such that no nops will be // present after the label. - Asm->emitAlignment(std::max(Asm->MF->getAlignment(), MBB.getAlignment()), - &F); + Asm->emitAlignment( + std::max(Asm->MF->getPreferredAlignment(), MBB.getAlignment()), &F); // Now that we've emitted the alignment directive, point at our funclet. Asm->OutStreamer->emitLabel(Sym); diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp index 224231cb90fe2..a026fb9e6195b 100644 --- a/llvm/lib/CodeGen/MachineFunction.cpp +++ b/llvm/lib/CodeGen/MachineFunction.cpp @@ -211,11 +211,6 @@ void MachineFunction::init() { ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); Alignment = STI.getTargetLowering()->getMinFunctionAlignment(); - // FIXME: Shouldn't use pref alignment if explicit alignment is set on F. - if (!F.hasOptSize()) - Alignment = std::max(Alignment, - STI.getTargetLowering()->getPrefFunctionAlignment()); - // -fsanitize=function and -fsanitize=kcfi instrument indirect function calls // to load a type hash before the function label. Ensure functions are aligned // by a least 4 to avoid unaligned access, which is especially important for @@ -330,6 +325,19 @@ bool MachineFunction::shouldSplitStack() const { return getFunction().hasFnAttribute("split-stack"); } +Align MachineFunction::getPreferredAlignment() const { + Align PrefAlignment; + + if (MaybeAlign A = F.getPreferredAlignment()) + PrefAlignment = *A; + else if (!F.hasOptSize()) + PrefAlignment = STI.getTargetLowering()->getPrefFunctionAlignment(); + else + PrefAlignment = Align(1); + + return std::max(PrefAlignment, getAlignment()); +} + [[nodiscard]] unsigned MachineFunction::addFrameInst(const MCCFIInstruction &Inst) { FrameInstructions.push_back(Inst); diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp b/llvm/lib/Target/X86/X86AsmPrinter.cpp index 84b921222a116..96f566c1a3f1a 100644 --- a/llvm/lib/Target/X86/X86AsmPrinter.cpp +++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp @@ -155,7 +155,7 @@ void X86AsmPrinter::EmitKCFITypePadding(const MachineFunction &MF, if (HasType) PrefixBytes += 5; - emitNops(offsetToAlignment(PrefixBytes, MF.getAlignment())); + emitNops(offsetToAlignment(PrefixBytes, MF.getPreferredAlignment())); } /// emitKCFITypeId - Emit the KCFI type information in architecture specific diff --git a/llvm/test/CodeGen/PowerPC/alloca-crspill.ll b/llvm/test/CodeGen/PowerPC/alloca-crspill.ll index cbcfd9a6a0dab..bd88b5d89202d 100644 --- a/llvm/test/CodeGen/PowerPC/alloca-crspill.ll +++ b/llvm/test/CodeGen/PowerPC/alloca-crspill.ll @@ -26,7 +26,7 @@ entry: declare signext i32 @do_something(ptr) ; CHECK: name: test -; CHECK: alignment: 16 +; CHECK: alignment: 4 ; CHECK: liveins: ; CHECK64: - { reg: '$x3', virtual-reg: '' } ; CHECK32: - { reg: '$r3', virtual-reg: '' }