https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128442
Bug ID: 128442
Summary: Opportunity to turn lui+add into bseti
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: rtl-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: law at gcc dot gnu.org
Blocks: 26163
Target Milestone: ---
Target: riscv
This code (extracted from x264) shows a cse where we could turn lui+add into a
bseti, thus avoiding the constant synthesis. It's not terribly hot, just a few
million instances I suspect, and the lui isn't going to be terribly expensive
since it can issue anywhere.
Compile with -O2 -march=rv64gcb
typedef unsigned int uint32_t;
typedef struct x264_t x264_t;
struct x264_t
{
struct
{
int mv_max_fpel[2];
} mb;
};
int *oof;
extern void frob (int);
void
x264_me_search_ref (x264_t *h)
{
int mv_x_max = h->mb.mv_max_fpel[0];
int mv_y_max = h->mb.mv_max_fpel[1];
uint32_t mv_max = ((mv_x_max << 16) | (mv_y_max & 0x7FFF)) | 0x8000;
*oof = mv_max;
}
The key is realizing that the |0x8000 has been turned into a PLUS, and we
really want to turn it back into IOR since 0x8000 isn't a valid immediate for
addi and we can turn the bit on via bseti.
nonzero_bits doesn't seem to be giving me the expected results when I look at
things in combine, so it may be more than just a simple pattern addition to the
RISC-V backend.
Referenced Bugs:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=26163
[Bug 26163] [meta-bug] missed optimization in SPEC (2026, 2k17, 2k and 2k6 and
95)