[llvm] r358053 - [DWARF] Simplify LineTable::findRowInSeq

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 10 00:44:23 PDT 2019


Author: maskray
Date: Wed Apr 10 00:44:23 2019
New Revision: 358053

URL: http://llvm.org/viewvc/llvm-project?rev=358053&view=rev
Log:
[DWARF] Simplify LineTable::findRowInSeq

We want the last row whose address is less than or equal to Address.
This can be computed as upper_bound - 1, which is simpler than
lower_bound followed by skipping equal rows in a loop.

Since FirstRow (LowPC) does not satisfy the predicate (OrderByAddress)
while LastRow-1 (HighPC) satisfies the predicate. We can decrease the
search range by two, i.e.

upper_bound [FirstRow,LastRow) = upper_bound [FirstRow+1,LastRow-1)

Modified:
    llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugLine.cpp

Modified: llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugLine.cpp?rev=358053&r1=358052&r2=358053&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugLine.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugLine.cpp Wed Apr 10 00:44:23 2019
@@ -860,46 +860,22 @@ uint32_t DWARFDebugLine::LineTable::find
   if (!Seq.containsPC(Address))
     return UnknownRowIndex;
   assert(Seq.SectionIndex == Address.SectionIndex);
-  // Search for instruction address in the rows describing the sequence.
-  // Rows are stored in a vector, so we may use arithmetical operations with
-  // iterators.
+  // In some cases, e.g. first instruction in a function, the compiler generates
+  // two entries, both with the same address. We want the last one.
+  //
+  // In general we want a non-empty range: the last row whose address is less
+  // than or equal to Address. This can be computed as upper_bound - 1.
   DWARFDebugLine::Row Row;
   Row.Address = Address;
   RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
   RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
-  LineTable::RowIter RowPos = std::lower_bound(
-      FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
-  // Since Address is in Seq, FirstRow <= RowPos < LastRow.
-  assert(FirstRow <= RowPos && RowPos < LastRow);
+  assert(FirstRow->Address.Address <= Row.Address.Address &&
+         Row.Address.Address < LastRow[-1].Address.Address);
+  RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row,
+                                    DWARFDebugLine::Row::orderByAddress) -
+                   1;
   assert(Seq.SectionIndex == RowPos->Address.SectionIndex);
-  if (RowPos->Address.Address != Address.Address) {
-    // lower_bound either lands on the RowPos with the same Address
-    // as the queried one, or on the first that's larger.
-    assert(RowPos->Address.Address > Address.Address);
-    // We know RowPos can't be FirstRow, in this case,
-    // because the queried Address is in Seq. So if it were
-    // FirstRow, then RowPos->Address.Address == Address.Address,
-    // and we wouldn't be here.
-    assert(RowPos != FirstRow);
-    --RowPos;
-  }
-  // In some cases, e.g. first instruction in a function, the compiler generates
-  // two entries, both with the same address. We want the last one.
-  // There are 2 cases wrt. RowPos and the addresses in records before/after it:
-  // 1) RowPos's address is the one we looked for. In this case, we want to
-  // skip any potential empty ranges.
-  // 2) RowPos's address is less than the one we looked for. In that case, we
-  // arrived here by finding the first range with a greater address,
-  // then decrementing 1. If the address of this range is part of a sequence of
-  // empty ones, it is the last one.
-  // In either case, the loop below lands on the correct RowPos.
-  while (RowPos->Address.Address == (RowPos + 1)->Address.Address) {
-    ++RowPos;
-  }
-
-  assert(RowPos < LastRow);
-  uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
-  return Index;
+  return RowPos - Rows.begin();
 }
 
 uint32_t DWARFDebugLine::LineTable::lookupAddress(




More information about the llvm-commits mailing list