Skip to content

Commit 569bbe4

Browse files
committed
Address Feedback Nr.4
1 parent 16e8e20 commit 569bbe4

File tree

1 file changed

+44
-67
lines changed

1 file changed

+44
-67
lines changed

llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp

Lines changed: 44 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,90 +1391,67 @@ bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(
13911391
std::optional<uint64_t> StmtSequenceOffset) const {
13921392
if (Sequences.empty())
13931393
return false;
1394+
uint64_t EndAddr = Address.Address + Size;
1395+
// First, find an instruction sequence containing the given address.
1396+
DWARFDebugLine::Sequence Sequence;
1397+
Sequence.SectionIndex = Address.SectionIndex;
1398+
Sequence.HighPC = Address.Address;
1399+
SequenceIter LastSeq = Sequences.end();
1400+
SequenceIter SeqPos;
13941401

1395-
const uint64_t EndAddr = Address.Address + Size;
1402+
if (StmtSequenceOffset) {
1403+
// If we have a statement sequence offset, find the specific sequence
1404+
// Binary search for sequence with matching StmtSeqOffset
1405+
Sequence.StmtSeqOffset = *StmtSequenceOffset;
1406+
SeqPos = std::lower_bound(Sequences.begin(), LastSeq, Sequence,
1407+
[](const DWARFDebugLine::Sequence &LHS,
1408+
const DWARFDebugLine::Sequence &RHS) {
1409+
return LHS.StmtSeqOffset < RHS.StmtSeqOffset;
1410+
});
1411+
1412+
// If sequence not found or doesn't contain the address, return false
1413+
if (SeqPos == LastSeq || SeqPos->StmtSeqOffset != *StmtSequenceOffset ||
1414+
!SeqPos->containsPC(Address))
1415+
return false;
13961416

1397-
// Helper: Given a sequence and a starting address, find the subset
1398-
// of rows within [Address, EndAddr) and append them to `Result`.
1399-
auto addRowsFromSequence = [&](const Sequence &Seq,
1400-
object::SectionedAddress StartAddr,
1401-
uint64_t EndAddress, bool IsFirstSequence) {
1402-
// If this sequence does not intersect our [StartAddr, EndAddress) range,
1403-
// do nothing.
1404-
if (Seq.HighPC <= StartAddr.Address || Seq.LowPC >= EndAddress)
1405-
return;
1417+
// Set LastSeq to just past this sequence since we only want this one
1418+
LastSeq = std::next(SeqPos);
1419+
} else {
1420+
// No specific sequence requested, find first sequence containing address
1421+
SeqPos = std::upper_bound(Sequences.begin(), LastSeq, Sequence,
1422+
DWARFDebugLine::Sequence::orderByHighPC);
1423+
if (SeqPos == LastSeq || !SeqPos->containsPC(Address))
1424+
return false;
1425+
}
14061426

1407-
// For the "first" sequence in the search, we must figure out which row
1408-
// actually starts within our address range.
1409-
uint32_t FirstRowIndex = Seq.FirstRowIndex;
1410-
if (IsFirstSequence)
1411-
FirstRowIndex = findRowInSeq(Seq, StartAddr);
1427+
SequenceIter StartPos = SeqPos;
14121428

1413-
// Similarly, compute the last row that is within [StartAddr, EndAddress).
1429+
// Process sequences that overlap with the desired range
1430+
while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
1431+
const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
1432+
// For the first sequence, we need to find which row in the sequence is the
1433+
// first in our range.
1434+
uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
1435+
if (SeqPos == StartPos)
1436+
FirstRowIndex = findRowInSeq(CurSeq, Address);
1437+
1438+
// Figure out the last row in the range.
14141439
uint32_t LastRowIndex =
1415-
findRowInSeq(Seq, {EndAddress - 1, StartAddr.SectionIndex});
1440+
findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex});
14161441
if (LastRowIndex == UnknownRowIndex)
1417-
LastRowIndex = Seq.LastRowIndex - 1;
1442+
LastRowIndex = CurSeq.LastRowIndex - 1;
14181443

14191444
assert(FirstRowIndex != UnknownRowIndex);
14201445
assert(LastRowIndex != UnknownRowIndex);
14211446

1422-
// Append all row indices in [FirstRowIndex, LastRowIndex].
14231447
for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
14241448
Result.push_back(I);
14251449
}
1426-
};
14271450

1428-
// If a stmt_sequence_offset was provided, do a binary search to find the
1429-
// single sequence that matches that offset. Then add only its relevant rows.
1430-
if (StmtSequenceOffset) {
1431-
// Binary-search the Sequences by their StmtSeqOffset:
1432-
auto It = llvm::lower_bound(Sequences, *StmtSequenceOffset,
1433-
[](const Sequence &Seq, uint64_t OffsetVal) {
1434-
return Seq.StmtSeqOffset < OffsetVal;
1435-
});
1436-
1437-
// If not found or mismatched, there’s no match to return.
1438-
if (It == Sequences.end() || It->StmtSeqOffset != *StmtSequenceOffset)
1439-
return false;
1440-
1441-
// Now add rows from the discovered sequence if it intersects [Address,
1442-
// EndAddr).
1443-
addRowsFromSequence(*It, Address, EndAddr, /*IsFirstSequence=*/true);
1444-
return !Result.empty();
1445-
}
1446-
1447-
// Otherwise, fall back to logic of walking sequences by Address.
1448-
// We first find a sequence containing `Address` (via upper_bound on HighPC),
1449-
// then proceed forward through overlapping sequences in ascending order.
1450-
1451-
// Construct a dummy Sequence to find where `Address` fits by HighPC.
1452-
DWARFDebugLine::Sequence SearchSeq;
1453-
SearchSeq.SectionIndex = Address.SectionIndex;
1454-
SearchSeq.HighPC = Address.Address;
1455-
1456-
auto LastSeq = Sequences.end();
1457-
auto SeqPos = llvm::upper_bound(Sequences, SearchSeq,
1458-
DWARFDebugLine::Sequence::orderByHighPC);
1459-
if (SeqPos == LastSeq || !SeqPos->containsPC(Address))
1460-
return false;
1461-
1462-
// This marks the first sequence we found that might contain Address.
1463-
const auto StartPos = SeqPos;
1464-
1465-
// Walk forward until sequences no longer intersect [Address, EndAddr).
1466-
while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
1467-
// Add rows only if the sequence is in the same section:
1468-
if (SeqPos->SectionIndex == Address.SectionIndex) {
1469-
// For the very first sequence, we need the row that lines up with
1470-
// `Address`.
1471-
bool IsFirst = (SeqPos == StartPos);
1472-
addRowsFromSequence(*SeqPos, Address, EndAddr, IsFirst);
1473-
}
14741451
++SeqPos;
14751452
}
14761453

1477-
return !Result.empty();
1454+
return true;
14781455
}
14791456

14801457
std::optional<StringRef>

0 commit comments

Comments
 (0)