Turn MCSectionData into a field of MCSection.
authorRafael Espindola <rafael.espindola@gmail.com>
Mon, 25 May 2015 23:14:17 +0000 (23:14 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Mon, 25 May 2015 23:14:17 +0000 (23:14 +0000)
This also changes MCAssembler to store a vector of MCSections instead of an
iplist of MCSectionData.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@238159 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 368846a6efe4f8b8b290614b921cc260e5d083ca..427c1099d58965efbf99e9d88de6a99f2d8aa041 100644 (file)
@@ -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(); }
index 5b1aeda126dc9918a4eb7aa4340d70cb28e5518f..5ca83398bd0dff71eacb8ca475386dc527b15924 100644 (file)
@@ -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;
 
index a211dcfecf8c09828328a1de3ccad3a9b3be7b31..6dd8bd7466978fc11d4e8c6234587b3845f2113d 100644 (file)
@@ -1348,8 +1348,9 @@ void ELFObjectWriter::WriteObject(MCAssembler &Asm,
   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);
index 2fa023ee120810cf90f5d1a2e7ae08132ec3afe3..fe4e22d2104036916fda9fcff484013a2297acea 100644 (file)
@@ -69,11 +69,11 @@ MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
  {
   // 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::~MCEncodedFragmentWithFixups() {
 
 /* *** */
 
-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 &Layout) {
 
   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:[";
index 523c53787ce17879ef239cfa0402adee093c2bc0..6ad6496fcb92ea5faf35408cae433545d7ac4255 100644 (file)
@@ -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(BundleLockStateType NewState) {
   ++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(); }
index 79de0f9a9362937f7075b71787525f1bdbe00958..240aa4da07635441a133994d979845fa69c27d9a 100644 (file)
@@ -540,7 +540,7 @@ void MachObjectWriter::ComputeSymbolTable(
   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::ComputeSymbolTable(
   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(MCAssembler &Asm,
   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(MCAssembler &Asm,
   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(MCAssembler &Asm,
   // 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(MCAssembler &Asm,
          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);
index 565bca3ee93b717e5e7bb6b2d1dbd9608fea0c33..3866eb38663bbfe4f7eac6aa25e2fcc0448d364f 100644 (file)
@@ -636,7 +636,7 @@ void WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
   // "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(MCAssembler &Asm,
   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(MCAssembler &Asm,
 
         WriteZeros(SectionDataPadding);
 
-        Asm.writeSectionData(j, Layout);
+        Asm.writeSectionData(&j->getSectionData(), Layout);
       }
 
       if ((*i)->Relocations.size() > 0) {
index a231caef2522e9aee5d15f6c60bf21fb90d59541..9704457d1aaf3b1693d9292fa9f5d770d97bb57b 100644 (file)
@@ -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);
   }
 }