[lld] r270346 - Simplify SplitInputSection::getRangeAndSize.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Sat May 21 17:41:39 PDT 2016


Author: ruiu
Date: Sat May 21 19:41:38 2016
New Revision: 270346

URL: http://llvm.org/viewvc/llvm-project?rev=270346&view=rev
Log:
Simplify SplitInputSection::getRangeAndSize.

This patch adds Size member to SectionPiece so that getRangeAndSize
can just return a SectionPiece instead of a std::pair<SectionPiece *, uint_t>.
Also renamed the function.

Modified:
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/InputSection.h
    lld/trunk/ELF/MarkLive.cpp
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=270346&r1=270345&r2=270346&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Sat May 21 19:41:38 2016
@@ -414,7 +414,7 @@ typename ELFT::uint EHInputSection<ELFT>
   // identify the start of the output .eh_frame. Handle this special case.
   if (this->getSectionHdr()->sh_size == 0)
     return Offset;
-  SectionPiece *Piece = this->getRangeAndSize(Offset).first;
+  SectionPiece *Piece = this->getSectionPiece(Offset);
   if (Piece->OutputOff == size_t(-1))
     return -1; // Not in the output
 
@@ -449,8 +449,8 @@ MergeInputSection<ELFT>::MergeInputSecti
       size_t End = findNull(Data, EntSize);
       if (End == StringRef::npos)
         fatal("string is not null terminated");
-      this->Pieces.emplace_back(Offset);
       uintX_t Size = End + EntSize;
+      this->Pieces.emplace_back(Offset, Size);
       Data = Data.substr(Size);
       Offset += Size;
     }
@@ -461,7 +461,7 @@ MergeInputSection<ELFT>::MergeInputSecti
   size_t Size = Data.size();
   assert((Size % EntSize) == 0);
   for (unsigned I = 0, N = Size; I != N; I += EntSize)
-    this->Pieces.emplace_back(I);
+    this->Pieces.emplace_back(I, EntSize);
 }
 
 template <class ELFT>
@@ -470,8 +470,7 @@ bool MergeInputSection<ELFT>::classof(co
 }
 
 template <class ELFT>
-std::pair<SectionPiece *, typename ELFT::uint>
-SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
+SectionPiece *SplitInputSection<ELFT>::getSectionPiece(uintX_t Offset) {
   ArrayRef<uint8_t> D = this->getSectionData();
   StringRef Data((const char *)D.data(), D.size());
   uintX_t Size = Data.size();
@@ -482,16 +481,13 @@ SplitInputSection<ELFT>::getRangeAndSize
   auto I = std::upper_bound(
       Pieces.begin(), Pieces.end(), Offset,
       [](const uintX_t &A, const SectionPiece &B) { return A < B.InputOff; });
-  uintX_t End = (I == Pieces.end()) ? Data.size() : I->InputOff;
   --I;
-  return {&*I, End};
+  return &*I;
 }
 
 template <class ELFT>
 typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
-  std::pair<SectionPiece *, uintX_t> T = this->getRangeAndSize(Offset);
-  SectionPiece &Piece = *T.first;
-  uintX_t End = T.second;
+  SectionPiece &Piece = *this->getSectionPiece(Offset);
   assert(Piece.Live);
 
   // Compute the Addend and if the Base is cached, return.
@@ -502,7 +498,7 @@ typename ELFT::uint MergeInputSection<EL
   // Map the base to the offset in the output section and cache it.
   ArrayRef<uint8_t> D = this->getSectionData();
   StringRef Data((const char *)D.data(), D.size());
-  StringRef Entry = Data.substr(Piece.InputOff, End - Piece.InputOff);
+  StringRef Entry = Data.substr(Piece.InputOff, Piece.Size);
   auto *MOS = static_cast<MergeOutputSection<ELFT> *>(this->OutSec);
   Piece.OutputOff = MOS->getOffset(Entry);
   return Piece.OutputOff + Addend;

Modified: lld/trunk/ELF/InputSection.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.h?rev=270346&r1=270345&r2=270346&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.h (original)
+++ lld/trunk/ELF/InputSection.h Sat May 21 19:41:38 2016
@@ -132,8 +132,10 @@ template <class ELFT> InputSectionBase<E
 
 // SectionPiece represents a piece of splittable section contents.
 struct SectionPiece {
-  SectionPiece(size_t I) : InputOff(I), Live(!Config->GcSections) {}
+  SectionPiece(size_t Off, size_t Size)
+      : InputOff(Off), Size(Size), Live(!Config->GcSections) {}
   size_t InputOff;
+  size_t Size;
   size_t OutputOff = -1;
   bool Live;
 };
@@ -154,7 +156,7 @@ public:
   // rather than a single large blob of data.
   std::vector<SectionPiece> Pieces;
 
-  std::pair<SectionPiece *, uintX_t> getRangeAndSize(uintX_t Offset);
+  SectionPiece *getSectionPiece(uintX_t Offset);
 };
 
 // This corresponds to a SHF_MERGE section of an input file.

Modified: lld/trunk/ELF/MarkLive.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/MarkLive.cpp?rev=270346&r1=270345&r2=270346&view=diff
==============================================================================
--- lld/trunk/ELF/MarkLive.cpp (original)
+++ lld/trunk/ELF/MarkLive.cpp Sat May 21 19:41:38 2016
@@ -136,15 +136,14 @@ template <class ELFT> static bool isRese
 // Starting from GC-root sections, this function visits all reachable
 // sections to set their "Live" bits.
 template <class ELFT> void elf::markLive() {
-  typedef typename ELFT::uint uintX_t;
   SmallVector<InputSection<ELFT> *, 256> Q;
 
   auto Enqueue = [&](ResolvedReloc<ELFT> R) {
     if (!R.Sec)
       return;
     if (auto *MS = dyn_cast<MergeInputSection<ELFT>>(R.Sec)) {
-      std::pair<SectionPiece *, uintX_t> T = MS->getRangeAndSize(R.Offset);
-      T.first->Live = true;
+      SectionPiece *Piece = MS->getSectionPiece(R.Offset);
+      Piece->Live = true;
     }
     if (R.Sec->Live)
       return;

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=270346&r1=270345&r2=270346&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Sat May 21 19:41:38 2016
@@ -1142,9 +1142,6 @@ void EHOutputSection<ELFT>::addSectionAu
 
   DenseMap<uintX_t, uintX_t> OffsetToIndex;
   while (!D.empty()) {
-    unsigned Index = Sec->Pieces.size();
-    Sec->Pieces.emplace_back(Offset);
-
     uintX_t Length = readEntryLength<ELFT>(D);
     // If CIE/FDE data length is zero then Length is 4, this
     // shall be considered a terminator and processing shall end.
@@ -1152,6 +1149,9 @@ void EHOutputSection<ELFT>::addSectionAu
       break;
     StringRef Entry((const char *)D.data(), Length);
 
+    unsigned Index = Sec->Pieces.size();
+    Sec->Pieces.emplace_back(Offset, Length);
+
     uint32_t ID = read32<E>(D.data() + 4);
     if (ID == 0) {
       // CIE

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=270346&r1=270345&r2=270346&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Sat May 21 19:41:38 2016
@@ -855,7 +855,7 @@ template <class ELFT> static bool includ
     if (!D->Section->Live)
       return false;
     if (auto *S = dyn_cast<MergeInputSection<ELFT>>(D->Section))
-      if (!S->getRangeAndSize(D->Value).first->Live)
+      if (!S->getSectionPiece(D->Value)->Live)
         return false;
   }
   return true;




More information about the llvm-commits mailing list