[DWARF] Simplify LineTable::findRowInSeq
authorFangrui Song <maskray@google.com>
Wed, 10 Apr 2019 07:44:23 +0000 (07:44 +0000)
committerFangrui Song <maskray@google.com>
Wed, 10 Apr 2019 07:44:23 +0000 (07:44 +0000)
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)

llvm-svn: 358053

llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp

index a5513e8..9a72065 100644 (file)
@@ -860,46 +860,22 @@ uint32_t DWARFDebugLine::LineTable::findRowInSeq(
   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(