[llvm] cde799f - [NC,MIPS] Avoid some registerSection calls

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 23 16:18:01 PDT 2024


Author: Fangrui Song
Date: 2024-06-23T16:17:57-07:00
New Revision: cde799ff444b986e591a572b47765606c17c35a4

URL: https://github.com/llvm/llvm-project/commit/cde799ff444b986e591a572b47765606c17c35a4
DIFF: https://github.com/llvm/llvm-project/commit/cde799ff444b986e591a572b47765606c17c35a4.diff

LOG: [NC,MIPS] Avoid some registerSection calls

Similar to abbf3bce94e0848f746e39186d36ebb938c1c5de.

switchSection calls registerSection internally.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCELFStreamer.h
    llvm/lib/Target/Mips/MCTargetDesc/MipsOptionRecord.cpp
    llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCELFStreamer.h b/llvm/include/llvm/MC/MCELFStreamer.h
index cad1661c6dfb8..44c585a74f15d 100644
--- a/llvm/include/llvm/MC/MCELFStreamer.h
+++ b/llvm/include/llvm/MC/MCELFStreamer.h
@@ -46,7 +46,7 @@ class MCELFStreamer : public MCObjectStreamer {
   /// @{
 
   void initSections(bool NoExecStack, const MCSubtargetInfo &STI) override;
-  void changeSection(MCSection *Section, uint32_t Subsection) override;
+  void changeSection(MCSection *Section, uint32_t Subsection = 0) override;
   void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
   void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCDataFragment &F,
                       uint64_t Offset) override;

diff  --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsOptionRecord.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsOptionRecord.cpp
index f1aa90d240238..6b013de274772 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsOptionRecord.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsOptionRecord.cpp
@@ -20,7 +20,6 @@
 using namespace llvm;
 
 void MipsRegInfoRecord::EmitMipsOptionRecord() {
-  MCAssembler &MCA = Streamer->getAssembler();
   MipsTargetStreamer *MTS =
       static_cast<MipsTargetStreamer *>(Streamer->getTargetStreamer());
 
@@ -36,7 +35,6 @@ void MipsRegInfoRecord::EmitMipsOptionRecord() {
     MCSectionELF *Sec =
         Context.getELFSection(".MIPS.options", ELF::SHT_MIPS_OPTIONS,
                               ELF::SHF_ALLOC | ELF::SHF_MIPS_NOSTRIP, 1);
-    MCA.registerSection(*Sec);
     Sec->setAlignment(Align(8));
     Streamer->switchSection(Sec);
 
@@ -54,7 +52,6 @@ void MipsRegInfoRecord::EmitMipsOptionRecord() {
   } else {
     MCSectionELF *Sec = Context.getELFSection(".reginfo", ELF::SHT_MIPS_REGINFO,
                                               ELF::SHF_ALLOC, 24);
-    MCA.registerSection(*Sec);
     Sec->setAlignment(MTS->getABI().IsN32() ? Align(8) : Align(4));
     Streamer->switchSection(Sec);
 

diff  --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp
index bdf299ae8ac11..2f39d091d86d7 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsTargetStreamer.cpp
@@ -890,14 +890,15 @@ void MipsTargetELFStreamer::emitLabel(MCSymbol *S) {
 void MipsTargetELFStreamer::finish() {
   MCAssembler &MCA = getStreamer().getAssembler();
   const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo();
+  MCELFStreamer &S = getStreamer();
 
   // .bss, .text and .data are always at least 16-byte aligned.
   MCSection &TextSection = *OFI.getTextSection();
-  MCA.registerSection(TextSection);
+  S.changeSection(&TextSection);
   MCSection &DataSection = *OFI.getDataSection();
-  MCA.registerSection(DataSection);
+  S.changeSection(&DataSection);
   MCSection &BSSSection = *OFI.getBSSSection();
-  MCA.registerSection(BSSSection);
+  S.changeSection(&BSSSection);
 
   TextSection.ensureMinAlignment(Align(16));
   DataSection.ensureMinAlignment(Align(16));
@@ -908,16 +909,15 @@ void MipsTargetELFStreamer::finish() {
     // verifying the output of IAS against the output of other assemblers but
     // it's not necessary to produce a correct object and increases section
     // size.
-    MCStreamer &OS = getStreamer();
-    for (MCSection &S : MCA) {
-      MCSectionELF &Section = static_cast<MCSectionELF &>(S);
+    for (MCSection &Sec : MCA) {
+      MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
 
       Align Alignment = Section.getAlign();
-      OS.switchSection(&Section);
+      S.switchSection(&Section);
       if (Section.useCodeAlign())
-        OS.emitCodeAlignment(Alignment, &STI, Alignment.value());
+        S.emitCodeAlignment(Alignment, &STI, Alignment.value());
       else
-        OS.emitValueToAlignment(Alignment, 0, 1, Alignment.value());
+        S.emitValueToAlignment(Alignment, 0, 1, Alignment.value());
     }
   }
 
@@ -1015,19 +1015,15 @@ void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) {
   MCContext &Context = MCA.getContext();
   MCStreamer &OS = getStreamer();
 
+  OS.pushSection();
   MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS, 0);
+  OS.switchSection(Sec);
+  Sec->setAlignment(Align(4));
 
   MCSymbol *Sym = Context.getOrCreateSymbol(Name);
   const MCSymbolRefExpr *ExprRef =
       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Context);
 
-  MCA.registerSection(*Sec);
-  Sec->setAlignment(Align(4));
-
-  OS.pushSection();
-
-  OS.switchSection(Sec);
-
   OS.emitValueImpl(ExprRef, 4);
 
   OS.emitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask
@@ -1306,9 +1302,8 @@ void MipsTargetELFStreamer::emitMipsAbiFlags() {
   MCStreamer &OS = getStreamer();
   MCSectionELF *Sec = Context.getELFSection(
       ".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS, ELF::SHF_ALLOC, 24);
-  MCA.registerSection(*Sec);
-  Sec->setAlignment(Align(8));
   OS.switchSection(Sec);
+  Sec->setAlignment(Align(8));
 
   OS << ABIFlagsSection;
 }


        


More information about the llvm-commits mailing list