[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.
This commit is contained in:
parent
f2129ca94c
commit
4d2bc0adc6
@ -2407,6 +2407,19 @@ inline raw_ostream &operator<<(raw_ostream &OS,
|
|||||||
return OS;
|
return OS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Compare function by index if it is valid, fall back to the original address
|
||||||
|
/// otherwise.
|
||||||
|
inline bool compareBinaryFunctionByIndex(const BinaryFunction *A,
|
||||||
|
const BinaryFunction *B) {
|
||||||
|
if (A->hasValidIndex() && B->hasValidIndex())
|
||||||
|
return A->getIndex() < B->getIndex();
|
||||||
|
if (A->hasValidIndex() && !B->hasValidIndex())
|
||||||
|
return true;
|
||||||
|
if (!A->hasValidIndex() && B->hasValidIndex())
|
||||||
|
return false;
|
||||||
|
return A->getAddress() < B->getAddress();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace bolt
|
} // namespace bolt
|
||||||
|
|
||||||
// GraphTraits specializations for function basic block graphs (CFGs)
|
// GraphTraits specializations for function basic block graphs (CFGs)
|
||||||
|
@ -1607,13 +1607,7 @@ std::vector<BinaryFunction *> BinaryContext::getSortedFunctions() {
|
|||||||
SortedFunctions.begin(),
|
SortedFunctions.begin(),
|
||||||
[](BinaryFunction &BF) { return &BF; });
|
[](BinaryFunction &BF) { return &BF; });
|
||||||
|
|
||||||
llvm::stable_sort(SortedFunctions,
|
llvm::stable_sort(SortedFunctions, compareBinaryFunctionByIndex);
|
||||||
[](const BinaryFunction *A, const BinaryFunction *B) {
|
|
||||||
if (A->hasValidIndex() && B->hasValidIndex()) {
|
|
||||||
return A->getIndex() < B->getIndex();
|
|
||||||
}
|
|
||||||
return A->hasValidIndex();
|
|
||||||
});
|
|
||||||
return SortedFunctions;
|
return SortedFunctions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -385,16 +385,7 @@ bool BinaryFunction::isForwardCall(const MCSymbol *CalleeSymbol) const {
|
|||||||
if (CalleeBF) {
|
if (CalleeBF) {
|
||||||
if (CalleeBF->isInjected())
|
if (CalleeBF->isInjected())
|
||||||
return true;
|
return true;
|
||||||
|
return compareBinaryFunctionByIndex(this, CalleeBF);
|
||||||
if (hasValidIndex() && CalleeBF->hasValidIndex()) {
|
|
||||||
return getIndex() < CalleeBF->getIndex();
|
|
||||||
} else if (hasValidIndex() && !CalleeBF->hasValidIndex()) {
|
|
||||||
return true;
|
|
||||||
} else if (!hasValidIndex() && CalleeBF->hasValidIndex()) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
return getAddress() < CalleeBF->getAddress();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Absolute symbol.
|
// Absolute symbol.
|
||||||
ErrorOr<uint64_t> CalleeAddressOrError = BC.getSymbolValue(*CalleeSymbol);
|
ErrorOr<uint64_t> CalleeAddressOrError = BC.getSymbolValue(*CalleeSymbol);
|
||||||
|
@ -473,16 +473,7 @@ Error ReorderFunctions::runOnFunctions(BinaryContext &BC) {
|
|||||||
[](BinaryFunction &BF) { return &BF; });
|
[](BinaryFunction &BF) { return &BF; });
|
||||||
|
|
||||||
// Sort functions by index.
|
// Sort functions by index.
|
||||||
llvm::stable_sort(SortedFunctions,
|
llvm::stable_sort(SortedFunctions, compareBinaryFunctionByIndex);
|
||||||
[](const BinaryFunction *A, const BinaryFunction *B) {
|
|
||||||
if (A->hasValidIndex() && B->hasValidIndex())
|
|
||||||
return A->getIndex() < B->getIndex();
|
|
||||||
if (A->hasValidIndex() && !B->hasValidIndex())
|
|
||||||
return true;
|
|
||||||
if (!A->hasValidIndex() && B->hasValidIndex())
|
|
||||||
return false;
|
|
||||||
return A->getAddress() < B->getAddress();
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const BinaryFunction *Func : SortedFunctions) {
|
for (const BinaryFunction *Func : SortedFunctions) {
|
||||||
if (!Func->hasValidIndex())
|
if (!Func->hasValidIndex())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user