[NFC] Add helper method to ensure min alignment on MCSection
authorGuillaume Chatelet <gchatelet@google.com>
Thu, 24 Nov 2022 20:54:12 +0000 (20:54 +0000)
committerGuillaume Chatelet <gchatelet@google.com>
Mon, 28 Nov 2022 10:00:34 +0000 (10:00 +0000)
Follow up on D138653.

Differential Revision: https://reviews.llvm.org/D138686

bolt/lib/Core/BinaryEmitter.cpp
llvm/include/llvm/MC/MCSection.h
llvm/lib/MC/MCELFStreamer.cpp
llvm/lib/MC/MCObjectStreamer.cpp
llvm/lib/MC/MCWinCOFFStreamer.cpp
llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCELFStreamer.cpp
llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp
llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp

index d282e5c..3196f21 100644 (file)
@@ -300,8 +300,7 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
     // 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()
index 5382d84..f55ebf8 100644 (file)
@@ -140,6 +140,12 @@ public:
   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; }
 
index f2399d0..60e997c 100644 (file)
@@ -140,9 +140,8 @@ void MCELFStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
 // 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,
index 2ddfb08..a6c3ecf 100644 (file)
@@ -650,8 +650,7 @@ void MCObjectStreamer::emitValueToAlignment(Align Alignment, int64_t Value,
 
   // 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,
index 854b83c..6790247 100644 (file)
@@ -190,8 +190,7 @@ void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) {
 
   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::emitCOFFSafeSEH(MCSymbol const *Symbol) {
 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());
 
index a4f2488..73bd446 100644 (file)
@@ -253,8 +253,7 @@ void AMDGPUAsmPrinter::emitFunctionBodyEnd() {
   // 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<GCNSubtarget>();
 
index 11f1bbd..cdf4d25 100644 (file)
@@ -117,8 +117,7 @@ void HexagonMCELFStreamer::HexagonMCEmitCommonSymbol(MCSymbol *Symbol,
     }
 
     // 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 {
index 4e54e96..9893c60 100644 (file)
@@ -899,9 +899,9 @@ void MipsTargetELFStreamer::finish() {
   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
index 9e94dab..0952e1c 100644 (file)
@@ -2589,8 +2589,7 @@ bool PPCAIXAsmPrinter::doInitialization(Module &M) {
         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,
index 412b9db..a5d11cc 100644 (file)
@@ -584,8 +584,7 @@ void X86AsmBackend::emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst)
 
   // 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<MCFixupKind> X86AsmBackend::getFixupKind(StringRef Name) const {