https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118410
Bug ID: 118410
Summary: Use Zbb extensions to improve code generation for some
logicals
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: law at gcc dot gnu.org
Target Milestone: ---
The Zbb extension has andn, orn and xorn instructions which operate on two
register inputs X AND/OR/XOR ~Y.
For X op C we often have to synthesize C with multiple instructions. For a
subset of constants we can instead generate
X op' ~C
Essentially double-complementing the constant argument. In the case where ~C
can be synthesized with fewer instructions than C, this is a win.
Where op' is the -not version of the original op. For some constants that will
save us an instruction in synthesis step.
So something like
long orlow(long x) { return x | ((1L << 24) - 1); }
Currently generates something like this:
lui a1, 4096
addiw a1, a1, -1
or a0, a0, a1
But instead we could generate:
lui a1, -4096
orn a0, a0, a1
Now it's likely the case that the lui+addiw would fuse, so it may not help that
much. But it's ultimately smaller and at least as fast as the original.
Thanks to Piotr Fusik or i the LLVM project for pointing this out to me.