]> granicus.if.org Git - llvm/commitdiff
[DWARF] DWARFDebugLine: replace Sequence::orderByLowPC with orderByHighPC
authorFangrui Song <maskray@google.com>
Tue, 9 Apr 2019 15:08:32 +0000 (15:08 +0000)
committerFangrui Song <maskray@google.com>
Tue, 9 Apr 2019 15:08:32 +0000 (15:08 +0000)
In a sorted list of non-overlapping [LowPC,HighPC) ranges, locating an address with
upper_bound on HighPC is simpler than lower_bound on LowPC.

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

include/llvm/DebugInfo/DWARF/DWARFDebugLine.h
lib/DebugInfo/DWARF/DWARFDebugLine.cpp

index a17faf6d211b70edc5668c0c60f3171555a0435a..4304e84350aa9d997e0f8a3e39a9293d6ee9809c 100644 (file)
@@ -206,9 +206,9 @@ public:
 
     void reset();
 
-    static bool orderByLowPC(const Sequence &LHS, const Sequence &RHS) {
-      return std::tie(LHS.SectionIndex, LHS.LowPC) <
-             std::tie(RHS.SectionIndex, RHS.LowPC);
+    static bool orderByHighPC(const Sequence &LHS, const Sequence &RHS) {
+      return std::tie(LHS.SectionIndex, LHS.HighPC) <
+             std::tie(RHS.SectionIndex, RHS.HighPC);
     }
 
     bool isValid() const {
index 4fc6261ae156d9935a2efe928ae519d3bb6dffdb..a5513e87aac5a2e372a9005d153cc271e730d5c4 100644 (file)
@@ -842,7 +842,7 @@ Error DWARFDebugLine::LineTable::parse(
 
   // Sort all sequences so that address lookup will work faster.
   if (!Sequences.empty()) {
-    llvm::sort(Sequences, Sequence::orderByLowPC);
+    llvm::sort(Sequences, Sequence::orderByHighPC);
     // Note: actually, instruction address ranges of sequences should not
     // overlap (in shared objects and executables). If they do, the address
     // lookup would still work, though, but result would be ambiguous.
@@ -919,31 +919,15 @@ uint32_t DWARFDebugLine::LineTable::lookupAddress(
 
 uint32_t DWARFDebugLine::LineTable::lookupAddressImpl(
     object::SectionedAddress Address) const {
-  if (Sequences.empty())
-    return UnknownRowIndex;
   // First, find an instruction sequence containing the given address.
   DWARFDebugLine::Sequence Sequence;
   Sequence.SectionIndex = Address.SectionIndex;
-  Sequence.LowPC = Address.Address;
-  SequenceIter FirstSeq = Sequences.begin();
-  SequenceIter LastSeq = Sequences.end();
-  SequenceIter SeqPos = std::lower_bound(
-      FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
-  DWARFDebugLine::Sequence FoundSeq;
-
-  if (SeqPos == LastSeq) {
-    FoundSeq = Sequences.back();
-  } else if (SeqPos->LowPC == Address.Address &&
-             SeqPos->SectionIndex == Address.SectionIndex) {
-    FoundSeq = *SeqPos;
-  } else {
-    if (SeqPos == FirstSeq)
-      return UnknownRowIndex;
-    FoundSeq = *(SeqPos - 1);
-  }
-  if (FoundSeq.SectionIndex != Address.SectionIndex)
+  Sequence.HighPC = Address.Address;
+  SequenceIter It = llvm::upper_bound(Sequences, Sequence,
+                                      DWARFDebugLine::Sequence::orderByHighPC);
+  if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex)
     return UnknownRowIndex;
-  return findRowInSeq(FoundSeq, Address);
+  return findRowInSeq(*It, Address);
 }
 
 bool DWARFDebugLine::LineTable::lookupAddressRange(
@@ -971,17 +955,11 @@ bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(
   // First, find an instruction sequence containing the given address.
   DWARFDebugLine::Sequence Sequence;
   Sequence.SectionIndex = Address.SectionIndex;
-  Sequence.LowPC = Address.Address;
-  SequenceIter FirstSeq = Sequences.begin();
+  Sequence.HighPC = Address.Address;
   SequenceIter LastSeq = Sequences.end();
-  SequenceIter SeqPos = std::lower_bound(
-      FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
-  if (SeqPos == LastSeq || !SeqPos->containsPC(Address)) {
-    if (SeqPos == FirstSeq)
-      return false;
-    SeqPos--;
-  }
-  if (!SeqPos->containsPC(Address))
+  SequenceIter SeqPos = llvm::upper_bound(
+      Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC);
+  if (SeqPos == LastSeq || !SeqPos->containsPC(Address))
     return false;
 
   SequenceIter StartPos = SeqPos;