Skip to content

Commit 4d2bc0a

Browse files
authored
[BOLT] Extract comparator for sorting functions by index into helper function (#116217)
This change extracts the comparator for sorting functions by index into a helper function `compareBinaryFunctionByIndex()` Not sure why the comparator used in `BinaryContext::getSortedFunctions()` is not same as the other two places. I think they should use the same comparator, so I also change `BinaryContext::getSortedFunctions()` to use `compareBinaryFunctionByIndex()` for sorting functions.
1 parent f2129ca commit 4d2bc0a

File tree

4 files changed

+16
-27
lines changed

4 files changed

+16
-27
lines changed

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,6 +2407,19 @@ inline raw_ostream &operator<<(raw_ostream &OS,
24072407
return OS;
24082408
}
24092409

2410+
/// Compare function by index if it is valid, fall back to the original address
2411+
/// otherwise.
2412+
inline bool compareBinaryFunctionByIndex(const BinaryFunction *A,
2413+
const BinaryFunction *B) {
2414+
if (A->hasValidIndex() && B->hasValidIndex())
2415+
return A->getIndex() < B->getIndex();
2416+
if (A->hasValidIndex() && !B->hasValidIndex())
2417+
return true;
2418+
if (!A->hasValidIndex() && B->hasValidIndex())
2419+
return false;
2420+
return A->getAddress() < B->getAddress();
2421+
}
2422+
24102423
} // namespace bolt
24112424

24122425
// GraphTraits specializations for function basic block graphs (CFGs)

bolt/lib/Core/BinaryContext.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,13 +1607,7 @@ std::vector<BinaryFunction *> BinaryContext::getSortedFunctions() {
16071607
SortedFunctions.begin(),
16081608
[](BinaryFunction &BF) { return &BF; });
16091609

1610-
llvm::stable_sort(SortedFunctions,
1611-
[](const BinaryFunction *A, const BinaryFunction *B) {
1612-
if (A->hasValidIndex() && B->hasValidIndex()) {
1613-
return A->getIndex() < B->getIndex();
1614-
}
1615-
return A->hasValidIndex();
1616-
});
1610+
llvm::stable_sort(SortedFunctions, compareBinaryFunctionByIndex);
16171611
return SortedFunctions;
16181612
}
16191613

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -385,16 +385,7 @@ bool BinaryFunction::isForwardCall(const MCSymbol *CalleeSymbol) const {
385385
if (CalleeBF) {
386386
if (CalleeBF->isInjected())
387387
return true;
388-
389-
if (hasValidIndex() && CalleeBF->hasValidIndex()) {
390-
return getIndex() < CalleeBF->getIndex();
391-
} else if (hasValidIndex() && !CalleeBF->hasValidIndex()) {
392-
return true;
393-
} else if (!hasValidIndex() && CalleeBF->hasValidIndex()) {
394-
return false;
395-
} else {
396-
return getAddress() < CalleeBF->getAddress();
397-
}
388+
return compareBinaryFunctionByIndex(this, CalleeBF);
398389
} else {
399390
// Absolute symbol.
400391
ErrorOr<uint64_t> CalleeAddressOrError = BC.getSymbolValue(*CalleeSymbol);

bolt/lib/Passes/ReorderFunctions.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,16 +473,7 @@ Error ReorderFunctions::runOnFunctions(BinaryContext &BC) {
473473
[](BinaryFunction &BF) { return &BF; });
474474

475475
// Sort functions by index.
476-
llvm::stable_sort(SortedFunctions,
477-
[](const BinaryFunction *A, const BinaryFunction *B) {
478-
if (A->hasValidIndex() && B->hasValidIndex())
479-
return A->getIndex() < B->getIndex();
480-
if (A->hasValidIndex() && !B->hasValidIndex())
481-
return true;
482-
if (!A->hasValidIndex() && B->hasValidIndex())
483-
return false;
484-
return A->getAddress() < B->getAddress();
485-
});
476+
llvm::stable_sort(SortedFunctions, compareBinaryFunctionByIndex);
486477

487478
for (const BinaryFunction *Func : SortedFunctions) {
488479
if (!Func->hasValidIndex())

0 commit comments

Comments
 (0)