Implement new RTL optimizations pass: fold-mem-offsets
commit04c9cf5c786b94fbe3f6f21f06cae73a7575ff7a
authorManolis Tsamis <manolis.tsamis@vrull.eu>
Mon, 16 Oct 2023 19:08:12 +0000 (16 13:08 -0600)
committerJeff Law <jlaw@ventanamicro.com>
Mon, 16 Oct 2023 19:08:57 +0000 (16 13:08 -0600)
tree9bea307c51146c9ac3324b16f57d56e958db64e5
parent964fd402c9b48eb4da91fb3e4e45d4560d6c676c
Implement new RTL optimizations pass: fold-mem-offsets

This is a new RTL pass that tries to optimize memory offset calculations
by moving them from add immediate instructions to the memory loads/stores.
For example it can transform this:

  addi t4,sp,16
  add  t2,a6,t4
  shl  t3,t2,1
  ld   a2,0(t3)
  addi a2,1
  sd   a2,8(t2)

into the following (one instruction less):

  add  t2,a6,sp
  shl  t3,t2,1
  ld   a2,32(t3)
  addi a2,1
  sd   a2,24(t2)

Although there are places where this is done already, this pass is more
powerful and can handle the more difficult cases that are currently not
optimized. Also, it runs late enough and can optimize away unnecessary
stack pointer calculations.

gcc/ChangeLog:

* Makefile.in: Add fold-mem-offsets.o.
* passes.def: Schedule a new pass.
* tree-pass.h (make_pass_fold_mem_offsets): Declare.
* common.opt: New options.
* doc/invoke.texi: Document new option.
* fold-mem-offsets.cc: New file.

gcc/testsuite/ChangeLog:

* gcc.target/riscv/fold-mem-offsets-1.c: New test.
* gcc.target/riscv/fold-mem-offsets-2.c: New test.
* gcc.target/riscv/fold-mem-offsets-3.c: New test.
* gcc.target/i386/pr52146.c: Adjust expected output.

Signed-off-by: Manolis Tsamis <manolis.tsamis@vrull.eu>
gcc/Makefile.in
gcc/common.opt
gcc/doc/invoke.texi
gcc/fold-mem-offsets.cc [new file with mode: 0644]
gcc/passes.def
gcc/testsuite/gcc.target/i386/pr52146.c
gcc/testsuite/gcc.target/riscv/fold-mem-offsets-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/fold-mem-offsets-2.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/fold-mem-offsets-3.c [new file with mode: 0644]
gcc/tree-pass.h