On 7/15/2026 11:24 PM, Sebastian Huber wrote:
> Change the code to avoid emitting bitwise-or operations with a zero constant
> operand since they are effectively no-operations. The omitted bitwise-or
> operations are potentially relaxed atomic operations which cannot be optimized
> away once emitted, so the omission has to happen while generating the
> profiling
> code in instrument_decisions(), not as a later gimple optimization.
>
> For example, consider this test case:
>
> int a(int);
> int b(int);
> int g(int i)
> {
> if (i) {
> return a(i);
> } else {
> return b(i);
> }
> }
>
> On 64-bit RISC-V this change results in the following code changes for
> options -fprofile-update=atomic -fcondition-coverage -O2:
>
> --- test.s.old 2026-06-19 06:53:06.806622709 +0200
> +++ test.s.new 2026-06-19 06:53:29.187922134 +0200
> @@ -9,15 +9,13 @@
> .type g, @function
> g:
> lui a5,%hi(.LANCHOR0)
> - addi a5,a5,%lo(.LANCHOR0)
> beq a0,zero,.L2
> li a4,1
> + addi a5,a5,%lo(.LANCHOR0)
> amoor.d zero,a4,0(a5)
> - addi a5,a5,8
> - amoor.d zero,zero,0(a5)
> tail a
> .L2:
> - amoor.d zero,zero,0(a5)
> + addi a5,a5,%lo(.LANCHOR0)
> li a4,1
> addi a5,a5,8
> amoor.d zero,a4,0(a5)
> @@ -35,7 +33,7 @@
> .word 1110847786
> .zero 4
> .dword 0
> - .word -566726224
> + .word -566618674
> .word 903831156
> .dword .LC0
> .dword 0
>
> Tested on x86_64-pc-linux-gnu (gcov.exp, gcc.dg gcov*.c, tree-prof.exp)
> with no regressions, and manually cross-checked on 64-bit RISC-V for the
> asm diff shown above.
>
> gcc/ChangeLog:
>
> * tree-profile.cc (emit_bitwise_op): Build/fold the operation with
> gimple_build() instead of gimple_build_assign(), and return the
> folded result as-is instead of always materializing a new SSA
> name.
> (instrument_decisions): Do not emit bitwise-or operations with a
> zero constant operand.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/gcov-atomic-or-zero.c: New test.
> * gcc.misc-tests/gcov-35.c: New test.
> * gcc.misc-tests/gcov-36.c: New test.
>
> Signed-off-by: Sebastian Huber <[email protected]>
OK. Thanks for picking this up! And glad I didn't send you on a wild
goose chase with the basic approach!
Jeff