Add `isRelExprOneOf` helper
authorSean Silva <chisophugis@gmail.com>
Thu, 1 Dec 2016 05:43:48 +0000 (05:43 +0000)
committerSean Silva <chisophugis@gmail.com>
Thu, 1 Dec 2016 05:43:48 +0000 (05:43 +0000)
commit2eed75926c411e14cfe4d5d4a612da092034e927
tree9a8c2f1b25c8ed0b28a22c2fbbb21989bce20028
parente5f23fb9ea2242576c899d80ff4e6247aa8d9c36
Add `isRelExprOneOf` helper

In various places in LLD's hot loops, we have expressions of the form
"E == R_FOO || E == R_BAR || ..." (E is a RelExpr).

Some of these expressions are quite long, and even though they usually go just
a very small number of ways and so should be well predicted, they can still
occupy branch predictor resources harming other parts of the code, or they
won't be predicted well if they overflow branch predictor resources or if the
branches are too dense and the branch predictor can't track them all (the
compiler can in theory avoid this, at a cost in text size). And some of these
expressions are so large and executed so frequently that even when
well-predicted they probably still have a nontrivial cost.

This speedup should be pretty portable. The cost of these simple bit tests is
independent of:

- the target we are linking for
- the distribution of RelExpr's for a given link (which can depend on how the
  input files were compiled)
- what compiler was used to compile LLD (it is just a simple bit test;
  hopefully the compiler gets it right!)
- adding new target-dependent relocations (e.g. needsPlt doesn't pay any extra
  cost checking R_PPC_PLT_OPD on x86-64 builds)

I did some rough measurements on clang-fsds and this patch gives over about 4%
speedup for a regular -O1 link, about 2.5% for -O3 --gc-sections and over 5%
for -O0. Sorry, I don't have my current machine set up for doing really
accurate measurements right now.

This also is just a bit cleaner. Thanks for Joerg for suggesting for
this approach.

Differential Revision: https://reviews.llvm.org/D27156

llvm-svn: 288314
lld/ELF/Relocations.cpp
lld/ELF/Relocations.h