Skip to content

Commit 63ee4a3

Browse files
committed
Addressed comments: DenseMap, pointer to reference, simplify if else
1 parent 13e82ab commit 63ee4a3

File tree

5 files changed

+24
-31
lines changed

5 files changed

+24
-31
lines changed

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "bolt/Core/JumpTable.h"
3636
#include "bolt/Core/MCPlus.h"
3737
#include "bolt/Utils/NameResolver.h"
38+
#include "llvm/ADT/DenseMap.h"
3839
#include "llvm/ADT/STLExtras.h"
3940
#include "llvm/ADT/SmallString.h"
4041
#include "llvm/ADT/StringRef.h"
@@ -424,7 +425,8 @@ class BinaryFunction {
424425
unsigned LSDATypeEncoding{dwarf::DW_EH_PE_omit};
425426

426427
/// All compilation units this function belongs to.
427-
SmallVector<DWARFUnit *, 1> DwarfUnitVec;
428+
/// Maps DWARF unit offset to the unit pointer.
429+
DenseMap<uint64_t, DWARFUnit *> DwarfUnitMap;
428430

429431
/// Last computed hash value. Note that the value could be recomputed using
430432
/// different parameters by every pass.
@@ -2409,19 +2411,16 @@ class BinaryFunction {
24092411
void
24102412
computeBlockHashes(HashFunction HashFunction = HashFunction::Default) const;
24112413

2412-
void addDWARFUnit(DWARFUnit *Unit) { DwarfUnitVec.push_back(Unit); }
2414+
void addDWARFUnit(DWARFUnit *Unit) { DwarfUnitMap[Unit->getOffset()] = Unit; }
24132415

24142416
void removeDWARFUnit(DWARFUnit *Unit) {
2415-
auto *It = std::find(DwarfUnitVec.begin(), DwarfUnitVec.end(), Unit);
2416-
// If found, erase it
2417-
if (It != DwarfUnitVec.end()) {
2418-
DwarfUnitVec.erase(It);
2419-
}
2417+
DwarfUnitMap.erase(Unit->getOffset());
24202418
}
24212419

24222420
/// Return DWARF compile units for this function.
2423-
const SmallVector<DWARFUnit *, 1>& getDWARFUnits() const {
2424-
return DwarfUnitVec;
2421+
/// Returns a reference to the map of DWARF unit offsets to units.
2422+
const DenseMap<uint64_t, DWARFUnit *> &getDWARFUnits() const {
2423+
return DwarfUnitMap;
24252424
}
24262425

24272426
const DWARFDebugLine::LineTable *

bolt/include/bolt/Core/DebugData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ class ClusteredRows {
867867
}
868868

869869
template <typename T> void populate(const T Vec) {
870-
assert(Vec.size() == Size && "");
870+
assert(Vec.size() == Size && "Sizes must match");
871871
DebugLineTableRowRef *CurRawPtr = beginPtr();
872872
for (DebugLineTableRowRef RowRef : Vec) {
873873
*CurRawPtr = RowRef;

bolt/lib/Core/BinaryContext.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,27 +1700,25 @@ void BinaryContext::preprocessDebugInfo() {
17001700

17011701
// Go forward and add all units from ranges that cover the function
17021702
while (++It != AllRanges.end()) {
1703-
if (It->LowPC <= FunctionAddress && FunctionAddress < It->HighPC) {
1704-
Function.addDWARFUnit(It->Unit);
1705-
} else {
1703+
if (It->LowPC > FunctionAddress || FunctionAddress >= It->HighPC)
17061704
break;
1707-
}
1705+
Function.addDWARFUnit(It->Unit);
17081706
}
17091707
}
17101708

17111709
// Discover units with debug info that needs to be updated.
17121710
for (const auto &KV : BinaryFunctions) {
17131711
const BinaryFunction &BF = KV.second;
17141712
if (shouldEmit(BF) && !BF.getDWARFUnits().empty())
1715-
for (const DWARFUnit *Unit : BF.getDWARFUnits())
1713+
for (const auto &[_, Unit] : BF.getDWARFUnits())
17161714
ProcessedCUs.insert(Unit);
17171715
}
17181716
// Clear debug info for functions from units that we are not going to process.
17191717
for (auto &KV : BinaryFunctions) {
17201718
BinaryFunction &BF = KV.second;
17211719
// Collect units to remove to avoid iterator invalidation
17221720
SmallVector<DWARFUnit *, 1> UnitsToRemove;
1723-
for (auto *Unit : BF.getDWARFUnits()) {
1721+
for (const auto &[_, Unit] : BF.getDWARFUnits()) {
17241722
if (!ProcessedCUs.count(Unit))
17251723
UnitsToRemove.push_back(Unit);
17261724
}

bolt/lib/Core/BinaryEmitter.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ bool BinaryEmitter::emitFunction(BinaryFunction &Function,
438438
}
439439

440440
if (opts::UpdateDebugSections && !Function.getDWARFUnits().empty())
441-
for (DWARFUnit *Unit : Function.getDWARFUnits())
441+
for (const auto &[_, Unit] : Function.getDWARFUnits())
442442
emitLineInfoEnd(Function, EndSymbol, Unit);
443443

444444
// Exception handling info for the function.
@@ -687,10 +687,10 @@ SMLoc BinaryEmitter::emitLineInfo(const BinaryFunction &BF, SMLoc NewLoc,
687687
const ClusteredRows *Cluster = ClusteredRows::fromSMLoc(NewLoc);
688688

689689
auto addToLineTable = [&](DebugLineTableRowRef RowReference,
690-
const DWARFUnit *TargetCU, unsigned Flags,
691-
MCSymbol *InstrLabel,
690+
const DWARFUnit &TargetCU, unsigned Flags,
691+
MCSymbol &InstrLabel,
692692
const DWARFDebugLine::Row &CurrentRow) {
693-
const uint64_t TargetUnitIndex = TargetCU->getOffset();
693+
const uint64_t TargetUnitIndex = TargetCU.getOffset();
694694
unsigned TargetFilenum = CurrentRow.File;
695695
const uint32_t CurrentUnitIndex = RowReference.DwCompileUnitIndex;
696696
// If the CU id from the current instruction location does not
@@ -711,7 +711,7 @@ SMLoc BinaryEmitter::emitLineInfo(const BinaryFunction &BF, SMLoc NewLoc,
711711
.getMCLineSections()
712712
.getMCLineEntries();
713713
const auto *It = MapLineEntries.find(Streamer.getCurrentSectionOnly());
714-
auto NewLineEntry = MCDwarfLineEntry(InstrLabel, DwarfLoc);
714+
auto NewLineEntry = MCDwarfLineEntry(&InstrLabel, DwarfLoc);
715715

716716
// Check if line table exists and has entries before doing comparison
717717
if (It != MapLineEntries.end() && !It->second.empty()) {
@@ -750,21 +750,17 @@ SMLoc BinaryEmitter::emitLineInfo(const BinaryFunction &BF, SMLoc NewLoc,
750750
if (FirstInstr)
751751
Flags |= DWARF2_FLAG_IS_STMT;
752752
const auto &FunctionDwarfUnits = BF.getDWARFUnits();
753-
const auto *It = std::find_if(
754-
FunctionDwarfUnits.begin(), FunctionDwarfUnits.end(),
755-
[RowReference](const DWARFUnit *Unit) {
756-
return Unit->getOffset() == RowReference.DwCompileUnitIndex;
757-
});
753+
auto It = FunctionDwarfUnits.find(RowReference.DwCompileUnitIndex);
758754
if (It != FunctionDwarfUnits.end()) {
759-
addToLineTable(RowReference, *It, Flags, InstrLabel, CurrentRow);
755+
addToLineTable(RowReference, *It->second, Flags, *InstrLabel, CurrentRow);
760756
continue;
761757
}
762758
// This rows is from CU that did not contain the original function.
763759
// This might happen if BOLT moved/inlined that instruction from other CUs.
764760
// In this case, we need to insert it to all CUs that the function
765761
// originally beloned to.
766-
for (const DWARFUnit *Unit : BF.getDWARFUnits()) {
767-
addToLineTable(RowReference, Unit, Flags, InstrLabel, CurrentRow);
762+
for (const auto &[_, Unit] : BF.getDWARFUnits()) {
763+
addToLineTable(RowReference, *Unit, Flags, *InstrLabel, CurrentRow);
768764
}
769765
}
770766

@@ -1148,7 +1144,7 @@ void BinaryEmitter::emitDebugLineInfoForOriginalFunctions() {
11481144
continue;
11491145

11501146
// Loop through all CUs in the function
1151-
for (DWARFUnit *Unit : Function.getDWARFUnits()) {
1147+
for (const auto &[_, Unit] : Function.getDWARFUnits()) {
11521148
const DWARFDebugLine::LineTable *LineTable =
11531149
Function.getDWARFLineTableForUnit(Unit);
11541150
if (!LineTable)

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ Error BinaryFunction::disassemble() {
14901490
add_instruction:
14911491
if (!getDWARFUnits().empty()) {
14921492
SmallVector<DebugLineTableRowRef, 1> Rows;
1493-
for (DWARFUnit *Unit : getDWARFUnits()) {
1493+
for (const auto &[_, Unit] : getDWARFUnits()) {
14941494
const DWARFDebugLine::LineTable *LineTable =
14951495
getDWARFLineTableForUnit(Unit);
14961496
if (!LineTable)

0 commit comments

Comments
 (0)