Skip to content

Commit

Permalink
Implement new RTL optimizations pass: fold-mem-offsets.
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Manolis Tsamis <manolis.tsamis@vrull.eu>
  • Loading branch information
Manolis Tsamis authored and ouuleilei-bot committed Jul 13, 2023
1 parent c2d62cd commit 9488322
Show file tree
Hide file tree
Showing 9 changed files with 821 additions and 0 deletions.
1 change: 1 addition & 0 deletions gcc/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,7 @@ OBJS = \
fixed-value.o \
fold-const.o \
fold-const-call.o \
fold-mem-offsets.o \
function.o \
function-abi.o \
function-tests.o \
Expand Down
4 changes: 4 additions & 0 deletions gcc/common.opt
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,10 @@ fcprop-registers
Common Var(flag_cprop_registers) Optimization
Perform a register copy-propagation optimization pass.

ffold-mem-offsets
Target Bool Var(flag_fold_mem_offsets) Init(1)
Fold instructions calculating memory offsets to the memory access instruction if possible.

fcrossjumping
Common Var(flag_crossjumping) Optimization
Perform cross-jumping optimization.
Expand Down
8 changes: 8 additions & 0 deletions gcc/doc/invoke.texi
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ Objective-C and Objective-C++ Dialects}.
-fauto-inc-dec -fbranch-probabilities
-fcaller-saves
-fcombine-stack-adjustments -fconserve-stack
-ffold-mem-offsets
-fcompare-elim -fcprop-registers -fcrossjumping
-fcse-follow-jumps -fcse-skip-blocks -fcx-fortran-rules
-fcx-limited-range
Expand Down Expand Up @@ -14147,6 +14148,13 @@ the comparison operation before register allocation is complete.

Enabled at levels @option{-O1}, @option{-O2}, @option{-O3}, @option{-Os}.

@opindex ffold-mem-offsets
@item -ffold-mem-offsets
@itemx -fno-fold-mem-offsets
Try to eliminate add instructions by folding them in memory loads/stores.

Enabled at levels @option{-O2}, @option{-O3}.

@opindex fcprop-registers
@item -fcprop-registers
After register allocation and post-register allocation instruction splitting,
Expand Down
Loading

0 comments on commit 9488322

Please sign in to comment.