https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124667
Bug ID: 124667
Summary: Optimize load/store IOR|AND branch mispredict (former
bitmap_set_bit from GCC benchmark)
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: daniel.barboza at oss dot qualcomm.com
Target Milestone: ---
We had the following code in bitmap_set_bit:
----
BITMAP_WORD bit_val = ((BITMAP_WORD) 1) << bit_num;
if (ptr != 0)
{
bool res = (ptr->bits[word_num] & bit_val) == 0;
if (res)
ptr->bits[word_num] |= bit_val;
return res;
}
----
This was changed in PR119482 and we're not always doing the IOR, meaning we no
longer have to deal with this pattern in the GCC benchmark. But we'll keep
dealing with that everywhere else.
If we were to handle this same case in the wild one solution to eliminate the
branch mispredict in code generation is to recognize a scenario like this:
bit_val = 1 << num;
if ((ptr[x] & bit_val) == 0)
{
ptr[x] |= bit_val;
}
(...)
We're looking for a "ptr[x] will have bit_val set regardless" semantic, which
give us the opportunity to unconditionally do the bit_ior. This will eliminate
the cond and also tends to eliminate the bit_and as well if its result isn't
used anywhere else.
There's a similar case for a bit_clear, i.e. check if a bit is set and clear
it:
bit_val = 1 << num;
if ((ptr[x] & bit_val) != 0)
{
ptr[x] &= ~bit_val;
}
(...)
We can't use match.pd since it doesn't handle memory refs, thus we'll use
something else (e.g. cselim).
This does have a resemblance with another match.pd work (PR64567), in
particular in the restriction of handling just single bits in the bit_ior case.
I'm planning to send a fix proposal in the next few days.