[lld] r278554 - [ELF] - Remove excessive loop in LinkerScript<ELFT>::assignAddresses()

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 12 12:32:45 PDT 2016


Author: grimar
Date: Fri Aug 12 14:32:45 2016
New Revision: 278554

URL: http://llvm.org/viewvc/llvm-project?rev=278554&view=rev
Log:
[ELF] - Remove excessive loop in LinkerScript<ELFT>::assignAddresses()

After 278461 "Create only one section for a name in LinkerScript."
this loop is excessive. 
Patch also reorders code slightly to use early return.

Differential revision: https://reviews.llvm.org/D23442

Modified:
    lld/trunk/ELF/LinkerScript.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=278554&r1=278553&r2=278554&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Fri Aug 12 14:32:45 2016
@@ -362,39 +362,39 @@ template <class ELFT> void LinkerScript<
       continue;
     }
 
-    // Find all the sections with required name. There can be more than
-    // one section with such name, if the alignment, flags or type
-    // attribute differs.
     auto *Cmd = cast<OutputSectionCommand>(Base.get());
-    for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
-      if (Sec->getName() != Cmd->Name)
-        continue;
-
-      if (Cmd->AddrExpr)
-        Dot = Cmd->AddrExpr(Dot);
-
-      if (Cmd->AlignExpr)
-        Sec->updateAlignment(Cmd->AlignExpr(Dot));
-
-      if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
-        uintX_t TVA = Dot + ThreadBssOffset;
-        TVA = alignTo(TVA, Sec->getAlignment());
-        Sec->setVA(TVA);
-        assignOffsets(Sec);
-        ThreadBssOffset = TVA - Dot + Sec->getSize();
-        continue;
-      }
-
-      if (Sec->getFlags() & SHF_ALLOC) {
-        Dot = alignTo(Dot, Sec->getAlignment());
-        Sec->setVA(Dot);
-        assignOffsets(Sec);
-        MinVA = std::min(MinVA, Dot);
-        Dot += Sec->getSize();
-        continue;
-      }
+    auto I = llvm::find_if(*OutputSections, [&](OutputSectionBase<ELFT> *S) {
+      return S->getName() == Cmd->Name;
+    });
+    if (I == OutputSections->end())
+      continue;
+    OutputSectionBase<ELFT> *Sec = *I;
+
+    if (Cmd->AddrExpr)
+      Dot = Cmd->AddrExpr(Dot);
+
+    if (Cmd->AlignExpr)
+      Sec->updateAlignment(Cmd->AlignExpr(Dot));
+
+    if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
+      uintX_t TVA = Dot + ThreadBssOffset;
+      TVA = alignTo(TVA, Sec->getAlignment());
+      Sec->setVA(TVA);
+      assignOffsets(Sec);
+      ThreadBssOffset = TVA - Dot + Sec->getSize();
+      continue;
+    }
+
+    if (!(Sec->getFlags() & SHF_ALLOC)) {
       Sec->assignOffsets();
+      continue;
     }
+
+    Dot = alignTo(Dot, Sec->getAlignment());
+    Sec->setVA(Dot);
+    assignOffsets(Sec);
+    MinVA = std::min(MinVA, Dot);
+    Dot += Sec->getSize();
   }
 
   // ELF and Program headers need to be right before the first section in




More information about the llvm-commits mailing list