diff options
author | Job Noorman <jnoorman@igalia.com> | 2023-05-04 08:57:54 +0200 |
---|---|---|
committer | Job Noorman <jnoorman@igalia.com> | 2023-05-04 09:08:52 +0200 |
commit | 9fb5af59095f3f3578e11b008c98ae56c03f0102 (patch) | |
tree | 51851bd2b3fb9e8407ddbbf2e24bacdf43ddd405 /lld/ELF | |
parent | 338c2489f63e1c34db047ac8e45efeeb88c8a067 (diff) | |
download | llvm-9fb5af59095f3f3578e11b008c98ae56c03f0102.tar.gz |
[lld][RISCV][NFC] Simplify symbol value calculation in relax
The `valueDelta` map was used to calculate the symbol value deltas from
the previous iteration. Since the symbol values themselves are also
updated every iteration, the following invariant holds:
```
sa[i].offset == sa[i].d->value + valueDelta[sa[i].d]
```
Note that `sa[i].offset` contains the original value of `sa[i].d` and is
never changed.
This means that the current way of updating symbol values can be
rewritten to not need the `valueDelta` map:
```
sa[i].d->value -= delta - valueDelta.find(sa[i].d)->second;
<=> (replace invariant)
sa[i].d->value -= delta - (sa[i].offset - sa[i].d->value);
<=>
sa[i].d->value = sa[i].d->value - (delta - (sa[i].offset - sa[i].d->value));
<=>
sa[i].d->value = sa[i].d->value - delta + sa[i].offset - sa[i].d->value;
<=>
sa[i].d->value = sa[i].offset - delta;
```
This patch implements this simplification. I believe this improves the
readability of the code as it took me quite some time to understand the
use of `valueDelta`. It might also have a slight performance benefit as
it removes one iteration over all relocations every relax iteration.
Reviewed By: MaskRay
Differential Revision: https://reviews.llvm.org/D149735
Diffstat (limited to 'lld/ELF')
-rw-r--r-- | lld/ELF/Arch/RISCV.cpp | 19 |
1 files changed, 2 insertions, 17 deletions
diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp index 26977015dab3..aa67617a5be2 100644 --- a/lld/ELF/Arch/RISCV.cpp +++ b/lld/ELF/Arch/RISCV.cpp @@ -662,23 +662,8 @@ static bool relax(InputSection &sec) { const uint64_t secAddr = sec.getVA(); auto &aux = *sec.relaxAux; bool changed = false; - - // Get st_value delta for symbols relative to this section from the previous - // iteration. - DenseMap<const Defined *, uint64_t> valueDelta; ArrayRef<SymbolAnchor> sa = ArrayRef(aux.anchors); uint32_t delta = 0; - for (auto [i, r] : llvm::enumerate(sec.relocs())) { - for (; sa.size() && sa[0].offset <= r.offset; sa = sa.slice(1)) - if (!sa[0].end) - valueDelta[sa[0].d] = delta; - delta = aux.relocDeltas[i]; - } - for (const SymbolAnchor &sa : sa) - if (!sa.end) - valueDelta[sa.d] = delta; - sa = ArrayRef(aux.anchors); - delta = 0; std::fill_n(aux.relocTypes.get(), sec.relocs().size(), R_RISCV_NONE); aux.writes.clear(); @@ -725,7 +710,7 @@ static bool relax(InputSection &sec) { if (sa[0].end) sa[0].d->size = sa[0].offset - delta - sa[0].d->value; else - sa[0].d->value -= delta - valueDelta.find(sa[0].d)->second; + sa[0].d->value = sa[0].offset - delta; } delta += remove; if (delta != cur) { @@ -738,7 +723,7 @@ static bool relax(InputSection &sec) { if (a.end) a.d->size = a.offset - delta - a.d->value; else - a.d->value -= delta - valueDelta.find(a.d)->second; + a.d->value = a.offset - delta; } // Inform assignAddresses that the size has changed. if (!isUInt<16>(delta)) |