Skip to content

Commit 4514608

Browse files
committed
[ELF] relocateNonAlloc & ICF: replace random access iterators of relocations with input iterators. NFC
Also replace one `this->file` with a local variable as some function calls are opaque to the compiler, causing unneeded reload.
1 parent 5964c94 commit 4514608

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

lld/ELF/ICF.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,17 @@ bool ICF<ELFT>::constantEq(const InputSection *secA, ArrayRef<RelTy> ra,
239239
const InputSection *secB, ArrayRef<RelTy> rb) {
240240
if (ra.size() != rb.size())
241241
return false;
242-
for (size_t i = 0; i < ra.size(); ++i) {
243-
if (ra[i].r_offset != rb[i].r_offset ||
244-
ra[i].getType(config->isMips64EL) != rb[i].getType(config->isMips64EL))
242+
auto rai = ra.begin(), rae = ra.end(), rbi = rb.begin();
243+
for (; rai != rae; ++rai, ++rbi) {
244+
if (rai->r_offset != rbi->r_offset ||
245+
rai->getType(config->isMips64EL) != rbi->getType(config->isMips64EL))
245246
return false;
246247

247-
uint64_t addA = getAddend<ELFT>(ra[i]);
248-
uint64_t addB = getAddend<ELFT>(rb[i]);
248+
uint64_t addA = getAddend<ELFT>(*rai);
249+
uint64_t addB = getAddend<ELFT>(*rbi);
249250

250-
Symbol &sa = secA->file->getRelocTargetSym(ra[i]);
251-
Symbol &sb = secB->file->getRelocTargetSym(rb[i]);
251+
Symbol &sa = secA->file->getRelocTargetSym(*rai);
252+
Symbol &sb = secB->file->getRelocTargetSym(*rbi);
252253
if (&sa == &sb) {
253254
if (addA == addB)
254255
continue;
@@ -336,10 +337,11 @@ bool ICF<ELFT>::variableEq(const InputSection *secA, ArrayRef<RelTy> ra,
336337
const InputSection *secB, ArrayRef<RelTy> rb) {
337338
assert(ra.size() == rb.size());
338339

339-
for (size_t i = 0; i < ra.size(); ++i) {
340+
auto rai = ra.begin(), rae = ra.end(), rbi = rb.begin();
341+
for (; rai != rae; ++rai, ++rbi) {
340342
// The two sections must be identical.
341-
Symbol &sa = secA->file->getRelocTargetSym(ra[i]);
342-
Symbol &sb = secB->file->getRelocTargetSym(rb[i]);
343+
Symbol &sa = secA->file->getRelocTargetSym(*rai);
344+
Symbol &sb = secB->file->getRelocTargetSym(*rbi);
343345
if (&sa == &sb)
344346
continue;
345347

lld/ELF/InputSection.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -918,32 +918,32 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) {
918918
break;
919919
}
920920

921-
for (size_t i = 0, relsSize = rels.size(); i != relsSize; ++i) {
922-
const RelTy &rel = rels[i];
921+
const InputFile *f = this->file;
922+
for (auto it = rels.begin(), end = rels.end(); it != end; ++it) {
923+
const RelTy &rel = *it;
923924
const RelType type = rel.getType(config->isMips64EL);
924925
const uint64_t offset = rel.r_offset;
925926
uint8_t *bufLoc = buf + offset;
926927
int64_t addend = getAddend<ELFT>(rel);
927928
if (!RelTy::IsRela)
928929
addend += target.getImplicitAddend(bufLoc, type);
929930

930-
Symbol &sym = this->file->getRelocTargetSym(rel);
931+
Symbol &sym = f->getRelocTargetSym(rel);
931932
RelExpr expr = target.getRelExpr(type, sym, bufLoc);
932933
if (expr == R_NONE)
933934
continue;
934935
auto *ds = dyn_cast<Defined>(&sym);
935936

936937
if (emachine == EM_RISCV && type == R_RISCV_SET_ULEB128) {
937-
if (++i < relsSize &&
938-
rels[i].getType(/*isMips64EL=*/false) == R_RISCV_SUB_ULEB128 &&
939-
rels[i].r_offset == offset) {
938+
if (++it != end &&
939+
it->getType(/*isMips64EL=*/false) == R_RISCV_SUB_ULEB128 &&
940+
it->r_offset == offset) {
940941
uint64_t val;
941942
if (!ds && tombstone) {
942943
val = *tombstone;
943944
} else {
944945
val = sym.getVA(addend) -
945-
(this->file->getRelocTargetSym(rels[i]).getVA(0) +
946-
getAddend<ELFT>(rels[i]));
946+
(f->getRelocTargetSym(*it).getVA(0) + getAddend<ELFT>(*it));
947947
}
948948
if (overwriteULEB128(bufLoc, val) >= 0x80)
949949
errorOrWarn(getLocation(offset) + ": ULEB128 value " + Twine(val) +

0 commit comments

Comments
 (0)