Skip to content

Commit 9b4328f

Browse files
committed
[BOLT][NFC] Refactor RI::discoverFileObjects()
Minor refactoring to delete redundant code. Reviewed By: jobnoorman Differential Revision: https://reviews.llvm.org/D159525
1 parent 1e9b006 commit 9b4328f

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,6 @@ Error RewriteInstance::run() {
752752
void RewriteInstance::discoverFileObjects() {
753753
NamedRegionTimer T("discoverFileObjects", "discover file objects",
754754
TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
755-
FileSymRefs.clear();
756-
BC->getBinaryFunctions().clear();
757-
BC->clearBinaryData();
758755

759756
// For local symbols we want to keep track of associated FILE symbol name for
760757
// disambiguation by combined name.
@@ -891,26 +888,21 @@ void RewriteInstance::discoverFileObjects() {
891888
unsigned AnonymousId = 0;
892889

893890
// Regex object for matching cold fragments.
894-
Regex ColdFragment(".*\\.cold(\\.[0-9]+)?");
891+
const Regex ColdFragment(".*\\.cold(\\.[0-9]+)?");
895892

896893
const auto SortedSymbolsEnd =
897894
LastSymbol == SortedSymbols.end() ? LastSymbol : std::next(LastSymbol);
898895
for (auto Iter = SortedSymbols.begin(); Iter != SortedSymbolsEnd; ++Iter) {
899896
const SymbolRef &Symbol = Iter->Symbol;
900-
901-
// Keep undefined symbols for pretty printing?
902-
if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Undefined)
903-
continue;
904-
897+
const uint64_t SymbolAddress = Iter->Address;
898+
const auto SymbolFlags = cantFail(Symbol.getFlags());
905899
const SymbolRef::Type SymbolType = cantFail(Symbol.getType());
906900

907901
if (SymbolType == SymbolRef::ST_File)
908902
continue;
909903

910904
StringRef SymName = cantFail(Symbol.getName(), "cannot get symbol name");
911-
uint64_t Address =
912-
cantFail(Symbol.getAddress(), "cannot get symbol address");
913-
if (Address == 0) {
905+
if (SymbolAddress == 0) {
914906
if (opts::Verbosity >= 1 && SymbolType == SymbolRef::ST_Function)
915907
errs() << "BOLT-WARNING: function with 0 address seen\n";
916908
continue;
@@ -920,11 +912,12 @@ void RewriteInstance::discoverFileObjects() {
920912
if (SymName == "__hot_start" || SymName == "__hot_end")
921913
continue;
922914

923-
FileSymRefs[Address] = Symbol;
915+
FileSymRefs[SymbolAddress] = Symbol;
924916

925917
// Skip section symbols that will be registered by disassemblePLT().
926-
if ((cantFail(Symbol.getType()) == SymbolRef::ST_Debug)) {
927-
ErrorOr<BinarySection &> BSection = BC->getSectionForAddress(Address);
918+
if (SymbolType == SymbolRef::ST_Debug) {
919+
ErrorOr<BinarySection &> BSection =
920+
BC->getSectionForAddress(SymbolAddress);
928921
if (BSection && getPLTSectionInfo(BSection->getName()))
929922
continue;
930923
}
@@ -946,10 +939,10 @@ void RewriteInstance::discoverFileObjects() {
946939
std::string AlternativeName;
947940
if (Name.empty()) {
948941
UniqueName = "ANONYMOUS." + std::to_string(AnonymousId++);
949-
} else if (cantFail(Symbol.getFlags()) & SymbolRef::SF_Global) {
942+
} else if (SymbolFlags & SymbolRef::SF_Global) {
950943
if (const BinaryData *BD = BC->getBinaryDataByName(Name)) {
951944
if (BD->getSize() == ELFSymbolRef(Symbol).getSize() &&
952-
BD->getAddress() == Address) {
945+
BD->getAddress() == SymbolAddress) {
953946
if (opts::Verbosity > 1)
954947
errs() << "BOLT-WARNING: ignoring duplicate global symbol " << Name
955948
<< "\n";
@@ -985,14 +978,13 @@ void RewriteInstance::discoverFileObjects() {
985978

986979
uint64_t SymbolSize = ELFSymbolRef(Symbol).getSize();
987980
uint64_t SymbolAlignment = Symbol.getAlignment();
988-
unsigned SymbolFlags = cantFail(Symbol.getFlags());
989981

990982
auto registerName = [&](uint64_t FinalSize) {
991983
// Register names even if it's not a function, e.g. for an entry point.
992-
BC->registerNameAtAddress(UniqueName, Address, FinalSize, SymbolAlignment,
993-
SymbolFlags);
984+
BC->registerNameAtAddress(UniqueName, SymbolAddress, FinalSize,
985+
SymbolAlignment, SymbolFlags);
994986
if (!AlternativeName.empty())
995-
BC->registerNameAtAddress(AlternativeName, Address, FinalSize,
987+
BC->registerNameAtAddress(AlternativeName, SymbolAddress, FinalSize,
996988
SymbolAlignment, SymbolFlags);
997989
};
998990

@@ -1012,7 +1004,7 @@ void RewriteInstance::discoverFileObjects() {
10121004
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: considering symbol " << UniqueName
10131005
<< " for function\n");
10141006

1015-
if (Address == Section->getAddress() + Section->getSize()) {
1007+
if (SymbolAddress == Section->getAddress() + Section->getSize()) {
10161008
assert(SymbolSize == 0 &&
10171009
"unexpect non-zero sized symbol at end of section");
10181010
LLVM_DEBUG(
@@ -1038,11 +1030,12 @@ void RewriteInstance::discoverFileObjects() {
10381030
// their local labels. The only way to tell them apart is to look at
10391031
// symbol scope - global vs local.
10401032
if (PreviousFunction && SymbolType != SymbolRef::ST_Function) {
1041-
if (PreviousFunction->containsAddress(Address)) {
1033+
if (PreviousFunction->containsAddress(SymbolAddress)) {
10421034
if (PreviousFunction->isSymbolValidInScope(Symbol, SymbolSize)) {
10431035
LLVM_DEBUG(dbgs()
10441036
<< "BOLT-DEBUG: symbol is a function local symbol\n");
1045-
} else if (Address == PreviousFunction->getAddress() && !SymbolSize) {
1037+
} else if (SymbolAddress == PreviousFunction->getAddress() &&
1038+
!SymbolSize) {
10461039
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: ignoring symbol as a marker\n");
10471040
} else if (opts::Verbosity > 1) {
10481041
errs() << "BOLT-WARNING: symbol " << UniqueName
@@ -1059,8 +1052,8 @@ void RewriteInstance::discoverFileObjects() {
10591052
}
10601053
}
10611054

1062-
if (PreviousFunction && PreviousFunction->containsAddress(Address) &&
1063-
PreviousFunction->getAddress() != Address) {
1055+
if (PreviousFunction && PreviousFunction->containsAddress(SymbolAddress) &&
1056+
PreviousFunction->getAddress() != SymbolAddress) {
10641057
if (PreviousFunction->isSymbolValidInScope(Symbol, SymbolSize)) {
10651058
if (opts::Verbosity >= 1)
10661059
outs() << "BOLT-INFO: skipping possibly another entry for function "
@@ -1072,12 +1065,12 @@ void RewriteInstance::discoverFileObjects() {
10721065

10731066
registerName(0);
10741067

1075-
PreviousFunction->addEntryPointAtOffset(Address -
1068+
PreviousFunction->addEntryPointAtOffset(SymbolAddress -
10761069
PreviousFunction->getAddress());
10771070

10781071
// Remove the symbol from FileSymRefs so that we can skip it from
10791072
// in the future.
1080-
auto SI = FileSymRefs.find(Address);
1073+
auto SI = FileSymRefs.find(SymbolAddress);
10811074
assert(SI != FileSymRefs.end() && "symbol expected to be present");
10821075
assert(SI->second == Symbol && "wrong symbol found");
10831076
FileSymRefs.erase(SI);
@@ -1087,18 +1080,19 @@ void RewriteInstance::discoverFileObjects() {
10871080

10881081
// Checkout for conflicts with function data from FDEs.
10891082
bool IsSimple = true;
1090-
auto FDEI = CFIRdWrt->getFDEs().lower_bound(Address);
1083+
auto FDEI = CFIRdWrt->getFDEs().lower_bound(SymbolAddress);
10911084
if (FDEI != CFIRdWrt->getFDEs().end()) {
10921085
const dwarf::FDE &FDE = *FDEI->second;
1093-
if (FDEI->first != Address) {
1086+
if (FDEI->first != SymbolAddress) {
10941087
// There's no matching starting address in FDE. Make sure the previous
10951088
// FDE does not contain this address.
10961089
if (FDEI != CFIRdWrt->getFDEs().begin()) {
10971090
--FDEI;
10981091
const dwarf::FDE &PrevFDE = *FDEI->second;
10991092
uint64_t PrevStart = PrevFDE.getInitialLocation();
11001093
uint64_t PrevLength = PrevFDE.getAddressRange();
1101-
if (Address > PrevStart && Address < PrevStart + PrevLength) {
1094+
if (SymbolAddress > PrevStart &&
1095+
SymbolAddress < PrevStart + PrevLength) {
11021096
errs() << "BOLT-ERROR: function " << UniqueName
11031097
<< " is in conflict with FDE ["
11041098
<< Twine::utohexstr(PrevStart) << ", "
@@ -1115,11 +1109,11 @@ void RewriteInstance::discoverFileObjects() {
11151109
<< "; symbol table : " << SymbolSize << ". Using max size.\n";
11161110
}
11171111
SymbolSize = std::max(SymbolSize, FDE.getAddressRange());
1118-
if (BC->getBinaryDataAtAddress(Address)) {
1119-
BC->setBinaryDataSize(Address, SymbolSize);
1112+
if (BC->getBinaryDataAtAddress(SymbolAddress)) {
1113+
BC->setBinaryDataSize(SymbolAddress, SymbolSize);
11201114
} else {
11211115
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: No BD @ 0x"
1122-
<< Twine::utohexstr(Address) << "\n");
1116+
<< Twine::utohexstr(SymbolAddress) << "\n");
11231117
}
11241118
}
11251119
}
@@ -1128,7 +1122,7 @@ void RewriteInstance::discoverFileObjects() {
11281122
// Since function may not have yet obtained its real size, do a search
11291123
// using the list of registered functions instead of calling
11301124
// getBinaryFunctionAtAddress().
1131-
auto BFI = BC->getBinaryFunctions().find(Address);
1125+
auto BFI = BC->getBinaryFunctions().find(SymbolAddress);
11321126
if (BFI != BC->getBinaryFunctions().end()) {
11331127
BF = &BFI->second;
11341128
// Duplicate the function name. Make sure everything matches before we add
@@ -1142,23 +1136,26 @@ void RewriteInstance::discoverFileObjects() {
11421136
<< BF->getSize() << " new " << SymbolSize << "\n";
11431137
}
11441138
BF->setSize(std::max(SymbolSize, BF->getSize()));
1145-
BC->setBinaryDataSize(Address, BF->getSize());
1139+
BC->setBinaryDataSize(SymbolAddress, BF->getSize());
11461140
}
11471141
BF->addAlternativeName(UniqueName);
11481142
} else {
1149-
ErrorOr<BinarySection &> Section = BC->getSectionForAddress(Address);
1143+
ErrorOr<BinarySection &> Section =
1144+
BC->getSectionForAddress(SymbolAddress);
11501145
// Skip symbols from invalid sections
11511146
if (!Section) {
11521147
errs() << "BOLT-WARNING: " << UniqueName << " (0x"
1153-
<< Twine::utohexstr(Address) << ") does not have any section\n";
1148+
<< Twine::utohexstr(SymbolAddress)
1149+
<< ") does not have any section\n";
11541150
continue;
11551151
}
11561152

11571153
// Skip symbols from zero-sized sections.
11581154
if (!Section->getSize())
11591155
continue;
11601156

1161-
BF = BC->createBinaryFunction(UniqueName, *Section, Address, SymbolSize);
1157+
BF = BC->createBinaryFunction(UniqueName, *Section, SymbolAddress,
1158+
SymbolSize);
11621159
if (!IsSimple)
11631160
BF->setSimple(false);
11641161
}

0 commit comments

Comments
 (0)