diff --git a/bolt/lib/Core/BinaryEmitter.cpp b/bolt/lib/Core/BinaryEmitter.cpp --- a/bolt/lib/Core/BinaryEmitter.cpp +++ b/bolt/lib/Core/BinaryEmitter.cpp @@ -300,8 +300,7 @@ // Set section alignment to at least maximum possible object alignment. // We need this to support LongJmp and other passes that calculates // tentative layout. - if (Section->getAlign() < opts::AlignFunctions) - Section->setAlignment(Align(opts::AlignFunctions)); + Section->ensureMinAlignment(Align(opts::AlignFunctions)); Streamer.emitCodeAlignment(Align(BinaryFunction::MinAlign), &*BC.STI); uint16_t MaxAlignBytes = FF.isSplitFragment() diff --git a/llvm/include/llvm/MC/MCSection.h b/llvm/include/llvm/MC/MCSection.h --- a/llvm/include/llvm/MC/MCSection.h +++ b/llvm/include/llvm/MC/MCSection.h @@ -140,6 +140,12 @@ Align getAlign() const { return Alignment; } void setAlignment(Align Value) { Alignment = Value; } + /// Makes sure that Alignment is at least MinAlignment. + void ensureMinAlignment(Align MinAlignment) { + if (Alignment < MinAlignment) + Alignment = MinAlignment; + } + unsigned getOrdinal() const { return Ordinal; } void setOrdinal(unsigned Value) { Ordinal = Value; } diff --git a/llvm/lib/MC/MCELFStreamer.cpp b/llvm/lib/MC/MCELFStreamer.cpp --- a/llvm/lib/MC/MCELFStreamer.cpp +++ b/llvm/lib/MC/MCELFStreamer.cpp @@ -140,9 +140,8 @@ // needs to be aligned to at least the bundle size. static void setSectionAlignmentForBundling(const MCAssembler &Assembler, MCSection *Section) { - if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() && - Section->getAlign() < Assembler.getBundleAlignSize()) - Section->setAlignment(Align(Assembler.getBundleAlignSize())); + if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions()) + Section->ensureMinAlignment(Align(Assembler.getBundleAlignSize())); } void MCELFStreamer::changeSection(MCSection *Section, diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp --- a/llvm/lib/MC/MCObjectStreamer.cpp +++ b/llvm/lib/MC/MCObjectStreamer.cpp @@ -650,8 +650,7 @@ // Update the maximum alignment on the current section if necessary. MCSection *CurSec = getCurrentSectionOnly(); - if (CurSec->getAlign() < Alignment) - CurSec->setAlignment(Alignment); + CurSec->ensureMinAlignment(Alignment); } void MCObjectStreamer::emitCodeAlignment(Align Alignment, diff --git a/llvm/lib/MC/MCWinCOFFStreamer.cpp b/llvm/lib/MC/MCWinCOFFStreamer.cpp --- a/llvm/lib/MC/MCWinCOFFStreamer.cpp +++ b/llvm/lib/MC/MCWinCOFFStreamer.cpp @@ -190,8 +190,7 @@ MCSection *SXData = getContext().getObjectFileInfo()->getSXDataSection(); getAssembler().registerSection(*SXData); - if (SXData->getAlign() < 4) - SXData->setAlignment(Align(4)); + SXData->ensureMinAlignment(Align(4)); new MCSymbolIdFragment(Symbol, SXData); @@ -207,8 +206,7 @@ void MCWinCOFFStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) { MCSection *Sec = getCurrentSectionOnly(); getAssembler().registerSection(*Sec); - if (Sec->getAlign() < 4) - Sec->setAlignment(Align(4)); + Sec->ensureMinAlignment(Align(4)); new MCSymbolIdFragment(Symbol, getCurrentSectionOnly()); diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp --- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp @@ -253,8 +253,7 @@ // CP microcode requires the kernel descriptor to be allocated on 64 byte // alignment. Streamer.emitValueToAlignment(Align(64), 0, 1, 0); - if (ReadOnlySection.getAlign() < 64) - ReadOnlySection.setAlignment(Align(64)); + ReadOnlySection.ensureMinAlignment(Align(64)); const GCNSubtarget &STM = MF->getSubtarget(); diff --git a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCELFStreamer.cpp b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCELFStreamer.cpp --- a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCELFStreamer.cpp +++ b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCELFStreamer.cpp @@ -117,8 +117,7 @@ } // Update the maximum alignment of the section if necessary. - if (Section.getAlign() < ByteAlignment) - Section.setAlignment(Align(ByteAlignment)); + Section.ensureMinAlignment(Align(ByteAlignment)); switchSection(P.first, P.second); } else { diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp --- a/llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp +++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp @@ -899,9 +899,9 @@ MCSection &BSSSection = *OFI.getBSSSection(); MCA.registerSection(BSSSection); - TextSection.setAlignment(std::max(Align(16), TextSection.getAlign())); - DataSection.setAlignment(std::max(Align(16), DataSection.getAlign())); - BSSSection.setAlignment(std::max(Align(16), BSSSection.getAlign())); + TextSection.ensureMinAlignment(Align(16)); + DataSection.ensureMinAlignment(Align(16)); + BSSSection.ensureMinAlignment(Align(16)); if (RoundSectionSizes) { // Make sections sizes a multiple of the alignment. This is useful for 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 @@ -2589,8 +2589,7 @@ getObjFileLowering().SectionForGlobal(GO, GOKind, TM)); Align GOAlign = getGVAlignment(GO, GO->getParent()->getDataLayout()); - if (GOAlign > Csect->getAlign()) - Csect->setAlignment(GOAlign); + Csect->ensureMinAlignment(GOAlign); }; // We need to know, up front, the alignment of csects for the assembly path, diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp --- a/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp +++ b/llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp @@ -584,8 +584,7 @@ // Update the maximum alignment on the current section if necessary. MCSection *Sec = OS.getCurrentSectionOnly(); - if (Sec->getAlign() < AlignBoundary) - Sec->setAlignment(AlignBoundary); + Sec->ensureMinAlignment(AlignBoundary); } Optional X86AsmBackend::getFixupKind(StringRef Name) const {