[llvm] r238159 - Turn MCSectionData into a field of MCSection.

Rafael Espindola rafael.espindola at gmail.com
Mon May 25 16:14:17 PDT 2015


Author: rafael
Date: Mon May 25 18:14:17 2015
New Revision: 238159

URL: http://llvm.org/viewvc/llvm-project?rev=238159&view=rev
Log:
Turn MCSectionData into a field of MCSection.

This also changes MCAssembler to store a vector of MCSections instead of an
iplist of MCSectionData.

Modified:
    llvm/trunk/include/llvm/MC/MCAssembler.h
    llvm/trunk/include/llvm/MC/MCSection.h
    llvm/trunk/lib/MC/ELFObjectWriter.cpp
    llvm/trunk/lib/MC/MCAssembler.cpp
    llvm/trunk/lib/MC/MCSection.cpp
    llvm/trunk/lib/MC/MachObjectWriter.cpp
    llvm/trunk/lib/MC/WinCOFFObjectWriter.cpp
    llvm/trunk/lib/Target/R600/MCTargetDesc/AMDGPUAsmBackend.cpp

Modified: llvm/trunk/include/llvm/MC/MCAssembler.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCAssembler.h?rev=238159&r1=238158&r2=238159&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCAssembler.h (original)
+++ llvm/trunk/include/llvm/MC/MCAssembler.h Mon May 25 18:14:17 2015
@@ -12,6 +12,7 @@
 
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/ilist.h"
@@ -551,11 +552,11 @@ class MCAssembler {
   friend class MCAsmLayout;
 
 public:
-  typedef iplist<MCSectionData> SectionDataListType;
+  typedef SetVector<MCSection *> SectionListType;
   typedef std::vector<const MCSymbol *> SymbolDataListType;
 
-  typedef SectionDataListType::const_iterator const_iterator;
-  typedef SectionDataListType::iterator iterator;
+  typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
+  typedef pointee_iterator<SectionListType::iterator> iterator;
 
   typedef pointee_iterator<SymbolDataListType::const_iterator>
   const_symbol_iterator;
@@ -599,17 +600,12 @@ private:
 
   raw_ostream &OS;
 
-  iplist<MCSectionData> Sections;
+  SectionListType Sections;
 
   SymbolDataListType Symbols;
 
   DenseSet<const MCSymbol *> LocalsUsedInReloc;
 
-  /// The map of sections to their associated assembler backend data.
-  //
-  // FIXME: Avoid this indirection?
-  DenseMap<const MCSection *, MCSectionData *> SectionMap;
-
   std::vector<IndirectSymbolData> IndirectSymbols;
 
   std::vector<DataRegionData> DataRegions;
@@ -888,24 +884,22 @@ public:
   /// \name Backend Data Access
   /// @{
 
-  MCSectionData &getSectionData(const MCSection &Section) const {
-    MCSectionData *Entry = SectionMap.lookup(&Section);
-    assert(Entry && "Missing section data!");
-    return *Entry;
+  MCSectionData &getSectionData(MCSection &Section) {
+    assert(Sections.count(&Section) && "Unknown Seciton");
+    return Section.getSectionData();
+  }
+
+  const MCSectionData &getSectionData(const MCSection &Section) const {
+    return const_cast<MCAssembler *>(this)
+        ->getSectionData(const_cast<MCSection &>(Section));
   }
 
   MCSectionData &getOrCreateSectionData(MCSection &Section,
                                         bool *Created = nullptr) {
-    MCSectionData *&Entry = SectionMap[&Section];
-
+    bool C = Sections.insert(&Section);
     if (Created)
-      *Created = !Entry;
-    if (!Entry) {
-      Entry = new MCSectionData(Section);
-      Sections.push_back(Entry);
-    }
-
-    return *Entry;
+      *Created = C;
+    return Section.getSectionData();
   }
 
   bool hasSymbolData(const MCSymbol &Symbol) const { return Symbol.hasData(); }

Modified: llvm/trunk/include/llvm/MC/MCSection.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCSection.h?rev=238159&r1=238158&r2=238159&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCSection.h (original)
+++ llvm/trunk/include/llvm/MC/MCSection.h Mon May 25 18:14:17 2015
@@ -31,7 +31,7 @@ class MCSection;
 class MCSymbol;
 class raw_ostream;
 
-class MCSectionData : public ilist_node<MCSectionData> {
+class MCSectionData {
   friend class MCAsmLayout;
 
   MCSectionData(const MCSectionData &) = delete;
@@ -62,9 +62,7 @@ private:
   /// @}
 
 public:
-  // Only for use as sentinel.
-  MCSectionData();
-  MCSectionData(MCSection &Section);
+  explicit MCSectionData(MCSection &Section);
 
   MCSection &getSection() const { return *Section; }
 
@@ -144,9 +142,10 @@ private:
   /// Whether this section has had instructions emitted into it.
   unsigned HasInstructions : 1;
 
+  MCSectionData Data;
+
 protected:
-  MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin)
-      : Begin(Begin), HasInstructions(false), Variant(V), Kind(K) {}
+  MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin);
   SectionVariant Variant;
   SectionKind Kind;
 
@@ -191,6 +190,31 @@ public:
   bool hasInstructions() const { return HasInstructions; }
   void setHasInstructions(bool Value) { HasInstructions = Value; }
 
+  MCSectionData &getSectionData() { return Data; }
+  const MCSectionData &getSectionData() const {
+    return const_cast<MCSection *>(this)->getSectionData();
+  }
+
+  MCSectionData::FragmentListType &getFragmentList();
+  const MCSectionData::FragmentListType &getFragmentList() const {
+    return const_cast<MCSection *>(this)->getFragmentList();
+  }
+
+  MCSectionData::iterator begin();
+  MCSectionData::const_iterator begin() const {
+    return const_cast<MCSection *>(this)->begin();
+  }
+
+  MCSectionData::iterator end();
+  MCSectionData::const_iterator end() const {
+    return const_cast<MCSection *>(this)->end();
+  }
+
+  MCSectionData::reverse_iterator rend();
+  MCSectionData::const_reverse_iterator rend() const {
+    return const_cast<MCSection *>(this)->rend();
+  }
+
   virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS,
                                     const MCExpr *Subsection) const = 0;
 

Modified: llvm/trunk/lib/MC/ELFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/ELFObjectWriter.cpp?rev=238159&r1=238158&r2=238159&view=diff
==============================================================================
--- llvm/trunk/lib/MC/ELFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/ELFObjectWriter.cpp Mon May 25 18:14:17 2015
@@ -1348,8 +1348,9 @@ void ELFObjectWriter::WriteObject(MCAsse
   SectionOffsetsTy SectionOffsets;
   std::vector<MCSectionELF *> Groups;
   std::vector<MCSectionELF *> Relocations;
-  for (const MCSectionData &SD : Asm) {
-    const MCSectionELF &Section = static_cast<MCSectionELF &>(SD.getSection());
+  for (const MCSection &Sec : Asm) {
+    const MCSectionELF &Section = static_cast<const MCSectionELF &>(Sec);
+    const MCSectionData &SD = Section.getSectionData();
 
     uint64_t Padding = OffsetToAlignment(OS.tell(), Section.getAlignment());
     WriteZeros(Padding);

Modified: llvm/trunk/lib/MC/MCAssembler.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCAssembler.cpp?rev=238159&r1=238158&r2=238159&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCAssembler.cpp (original)
+++ llvm/trunk/lib/MC/MCAssembler.cpp Mon May 25 18:14:17 2015
@@ -69,11 +69,11 @@ MCAsmLayout::MCAsmLayout(MCAssembler &As
  {
   // Compute the section layout order. Virtual sections must go last.
   for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
-    if (!it->getSection().isVirtualSection())
-      SectionOrder.push_back(&*it);
+    if (!it->isVirtualSection())
+      SectionOrder.push_back(&it->getSectionData());
   for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
-    if (it->getSection().isVirtualSection())
-      SectionOrder.push_back(&*it);
+    if (it->isVirtualSection())
+      SectionOrder.push_back(&it->getSectionData());
 }
 
 bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
@@ -290,8 +290,6 @@ MCEncodedFragmentWithFixups::~MCEncodedF
 
 /* *** */
 
-MCSectionData::MCSectionData() : Section(nullptr) {}
-
 MCSectionData::MCSectionData(MCSection &Section) : Section(&Section) {}
 
 MCSectionData::iterator
@@ -342,7 +340,6 @@ MCAssembler::~MCAssembler() {
 void MCAssembler::reset() {
   Sections.clear();
   Symbols.clear();
-  SectionMap.clear();
   IndirectSymbols.clear();
   DataRegions.clear();
   LinkerOptions.clear();
@@ -862,9 +859,9 @@ void MCAssembler::Finish() {
     // Create dummy fragments to eliminate any empty sections, this simplifies
     // layout.
     if (it->getFragmentList().empty())
-      new MCDataFragment(it);
+      new MCDataFragment(&it->getSectionData());
 
-    it->getSection().setOrdinal(SectionIndex++);
+    it->setOrdinal(SectionIndex++);
   }
 
   // Assign layout order indices to sections and fragments.
@@ -1084,8 +1081,8 @@ bool MCAssembler::layoutOnce(MCAsmLayout
 
   bool WasRelaxed = false;
   for (iterator it = begin(), ie = end(); it != ie; ++it) {
-    MCSectionData &SD = *it;
-    while (layoutSectionOnce(Layout, SD))
+    MCSection &Sec = *it;
+    while (layoutSectionOnce(Layout, Sec.getSectionData()))
       WasRelaxed = true;
   }
 
@@ -1261,7 +1258,7 @@ void MCAssembler::dump() {
   OS << "  Sections:[\n    ";
   for (iterator it = begin(), ie = end(); it != ie; ++it) {
     if (it != begin()) OS << ",\n    ";
-    it->dump();
+    it->getSectionData().dump();
   }
   OS << "],\n";
   OS << "  Symbols:[";

Modified: llvm/trunk/lib/MC/MCSection.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCSection.cpp?rev=238159&r1=238158&r2=238159&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCSection.cpp (original)
+++ llvm/trunk/lib/MC/MCSection.cpp Mon May 25 18:14:17 2015
@@ -19,6 +19,9 @@ using namespace llvm;
 // MCSection
 //===----------------------------------------------------------------------===//
 
+MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin)
+    : Begin(Begin), HasInstructions(false), Data(*this), Variant(V), Kind(K) {}
+
 MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
   if (!End)
     End = Ctx.createTempSymbol("sec_end", true);
@@ -49,6 +52,14 @@ void MCSection::setBundleLockState(Bundl
   ++BundleLockNestingDepth;
 }
 
+MCSectionData::iterator MCSection::begin() { return Data.begin(); }
+
+MCSectionData::iterator MCSection::end() { return Data.end(); }
+
+MCSectionData::FragmentListType &MCSection::getFragmentList() {
+  return Data.getFragmentList();
+}
+
 MCSectionData::iterator MCSectionData::begin() { return Fragments.begin(); }
 
 MCSectionData::iterator MCSectionData::end() { return Fragments.end(); }

Modified: llvm/trunk/lib/MC/MachObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MachObjectWriter.cpp?rev=238159&r1=238158&r2=238159&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MachObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/MachObjectWriter.cpp Mon May 25 18:14:17 2015
@@ -540,7 +540,7 @@ void MachObjectWriter::ComputeSymbolTabl
   unsigned Index = 1;
   for (MCAssembler::iterator it = Asm.begin(),
          ie = Asm.end(); it != ie; ++it, ++Index)
-    SectionIndexMap[&it->getSection()] = Index;
+    SectionIndexMap[&*it] = Index;
   assert(Index <= 256 && "Too many sections!");
 
   // Build the string table.
@@ -622,7 +622,8 @@ void MachObjectWriter::ComputeSymbolTabl
   for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
     UndefinedSymbolData[i].Symbol->setIndex(Index++);
 
-  for (const MCSectionData &SD : Asm) {
+  for (const MCSection &Section : Asm) {
+    const MCSectionData &SD = Section.getSectionData();
     std::vector<RelAndSymbol> &Relocs = Relocations[&SD];
     for (RelAndSymbol &Rel : Relocs) {
       if (!Rel.Sym)
@@ -801,7 +802,7 @@ void MachObjectWriter::WriteObject(MCAss
   uint64_t VMSize = 0;
   for (MCAssembler::const_iterator it = Asm.begin(),
          ie = Asm.end(); it != ie; ++it) {
-    const MCSectionData &SD = *it;
+    const MCSectionData &SD = it->getSectionData();
     uint64_t Address = getSectionAddress(&SD);
     uint64_t Size = Layout.getSectionAddressSize(&SD);
     uint64_t FileSize = Layout.getSectionFileSize(&SD);
@@ -832,10 +833,11 @@ void MachObjectWriter::WriteObject(MCAss
   uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
   for (MCAssembler::const_iterator it = Asm.begin(),
          ie = Asm.end(); it != ie; ++it) {
-    std::vector<RelAndSymbol> &Relocs = Relocations[it];
+    const MCSectionData &SD = it->getSectionData();
+    std::vector<RelAndSymbol> &Relocs = Relocations[&SD];
     unsigned NumRelocs = Relocs.size();
-    uint64_t SectionStart = SectionDataStart + getSectionAddress(it);
-    WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
+    uint64_t SectionStart = SectionDataStart + getSectionAddress(&SD);
+    WriteSection(Asm, Layout, SD, SectionStart, RelocTableEnd, NumRelocs);
     RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info);
   }
 
@@ -911,9 +913,10 @@ void MachObjectWriter::WriteObject(MCAss
   // Write the actual section data.
   for (MCAssembler::const_iterator it = Asm.begin(),
          ie = Asm.end(); it != ie; ++it) {
-    Asm.writeSectionData(it, Layout);
+    const MCSectionData &SD = it->getSectionData();
+    Asm.writeSectionData(&SD, Layout);
 
-    uint64_t Pad = getPaddingSize(it, Layout);
+    uint64_t Pad = getPaddingSize(&SD, Layout);
     WriteZeros(Pad);
   }
 
@@ -925,7 +928,7 @@ void MachObjectWriter::WriteObject(MCAss
          ie = Asm.end(); it != ie; ++it) {
     // Write the section relocation entries, in reverse order to match 'as'
     // (approximately, the exact algorithm is more complicated than this).
-    std::vector<RelAndSymbol> &Relocs = Relocations[it];
+    std::vector<RelAndSymbol> &Relocs = Relocations[&it->getSectionData()];
     for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
       Write32(Relocs[e - i - 1].MRE.r_word0);
       Write32(Relocs[e - i - 1].MRE.r_word1);

Modified: llvm/trunk/lib/MC/WinCOFFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/WinCOFFObjectWriter.cpp?rev=238159&r1=238158&r2=238159&view=diff
==============================================================================
--- llvm/trunk/lib/MC/WinCOFFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/WinCOFFObjectWriter.cpp Mon May 25 18:14:17 2015
@@ -636,7 +636,7 @@ void WinCOFFObjectWriter::ExecutePostLay
   // "Define" each section & symbol. This creates section & symbol
   // entries in the staging area.
   for (const auto &Section : Asm)
-    DefineSection(Section);
+    DefineSection(Section.getSectionData());
 
   for (const MCSymbol &Symbol : Asm.symbols())
     if (ExportSymbol(Symbol, Asm))
@@ -946,12 +946,13 @@ void WinCOFFObjectWriter::WriteObject(MC
   offset += COFF::SectionSize * Header.NumberOfSections;
 
   for (const auto &Section : Asm) {
-    COFFSection *Sec = SectionMap[&Section.getSection()];
+    COFFSection *Sec = SectionMap[&Section];
 
     if (Sec->Number == -1)
       continue;
 
-    Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
+    Sec->Header.SizeOfRawData =
+        Layout.getSectionAddressSize(&Section.getSectionData());
 
     if (IsPhysicalSection(Sec)) {
       // Align the section data to a four byte boundary.
@@ -1035,7 +1036,7 @@ void WinCOFFObjectWriter::WriteObject(MC
 
         WriteZeros(SectionDataPadding);
 
-        Asm.writeSectionData(j, Layout);
+        Asm.writeSectionData(&j->getSectionData(), Layout);
       }
 
       if ((*i)->Relocations.size() > 0) {

Modified: llvm/trunk/lib/Target/R600/MCTargetDesc/AMDGPUAsmBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/MCTargetDesc/AMDGPUAsmBackend.cpp?rev=238159&r1=238158&r2=238159&view=diff
==============================================================================
--- llvm/trunk/lib/Target/R600/MCTargetDesc/AMDGPUAsmBackend.cpp (original)
+++ llvm/trunk/lib/Target/R600/MCTargetDesc/AMDGPUAsmBackend.cpp Mon May 25 18:14:17 2015
@@ -67,7 +67,7 @@ public:
 void AMDGPUMCObjectWriter::WriteObject(MCAssembler &Asm,
                                        const MCAsmLayout &Layout) {
   for (MCAssembler::iterator I = Asm.begin(), E = Asm.end(); I != E; ++I) {
-    Asm.writeSectionData(I, Layout);
+    Asm.writeSectionData(&I->getSectionData(), Layout);
   }
 }
 





More information about the llvm-commits mailing list