On Fri, Oct 24, 2025 at 3:45 PM H.J. Lu <[email protected]> wrote:
>
> For x86, volatile memory can be used as source operand for loads. Add
> -fcombine-op-with-volatile-memory-load to allow operations with volatile
> memory load:
>
> 1. Add a field, try_combine_insn, to recog_data_d for the combine pass to
> pass the instruction information to general_operand.
> 2. Update recog_for_combine_1 to set recog_data.try_combine_insn and clear
> when calling recog.
> 3. Add a function, volatile_memory_ok, which returns true if volatile
> memory is used for load and -fcombine-op-with-volatile-memory-load is
> enabled.
> 4. Update general_operand to call volatile_memory_ok to determine if
> a memory operand is OK.
>
> Enabled at -O1 and higher for x86. It optimizes
>
> extern volatile int bar;
>
> int
> foo (int z)
> {
> z *= 123;
> return bar + z;
> }
>
> into
>
> foo:
> imull $123, %edi, %eax
> addl bar(%rip), %eax
> ret
>
> instead of
>
> foo:
> imull $123, %edi, %eax
> movl bar(%rip), %edx
> addl %edx, %eax
> ret
>
> Tested with Linux kernel 6.17.4 on Intel Core i7-1195G7.
I have built the latest git defconfig linux kernel with the patched
current gcc trunk. The patched compiler compiles the kernel without
problems and I was able to boot the built kernel under qemu:
Linux version 6.18.0-rc2-00120-g6fab32bb6508 (uros@fedora) (xgcc (GCC)
16.0.0 20251024 (experimental) [master r16-4588-g81a7f4fef35], GNU ld
version 2.44-6.fc42) #1 SMP PREEMPT_DYNAMIC Fri Oct 24 12:04:38 CEST
2025
The bloat-o-meter reported a small code size reduction (2763 bytes):
Total: Before=24913670, After=24910907, chg -0.01%
and some un-scientific counts of moves from memory:
$ grep "mov " objdump-old.txt | grep "(" | wc -l
931942
$ grep "mov " objdump-new.txt | grep "(" | wc -l
931336
So, net reduction of some 600 explicit moves, which correlates nicely
with the above code size reduction.
Uros.