Skip to content

target dependent phase to improves memory accesses#229

Open
Alexehv77 wants to merge 1 commit into
arc-2026.09-devfrom
mem-opt
Open

target dependent phase to improves memory accesses#229
Alexehv77 wants to merge 1 commit into
arc-2026.09-devfrom
mem-opt

Conversation

@Alexehv77

Copy link
Copy Markdown

This phase identifies memory loads that use a base address calculated via instructions like sh1add. It traces the index register of that calculation back through previous instructions to find increments.
As a result instead of having a separate instruction to increment followed by a pointer to scale and add it, this pass merges the constant offset directly into the memory load. Thus it reduces the total number of instructions leading to smaller code size and faster execution.

Comment thread gcc/config/riscv/riscv-fold-mem.cc Outdated
Comment thread gcc/config/riscv/riscv-fold-mem.cc Outdated
Comment thread gcc/config/riscv/riscv-fold-mem.cc Outdated
Comment thread gcc/config/riscv/riscv.cc
&& !riscv_compressed_lw_address_p (XEXP (x, 0)))
cost++;

*total = COSTS_N_INSNS (cost + tune_param->memory_cost);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this for?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i changed it back - was from prior code where i tried to change change instruction costs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still there

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check now

Comment thread gcc/config/riscv/riscv-fold-mem.cc Outdated
Comment thread gcc/config/riscv/riscv-fold-mem.cc Outdated
Comment thread gcc/config/riscv/riscv-fold-mem.cc Outdated
Comment thread gcc/config/riscv/riscv-fold-mem.cc Outdated
HOST_WIDE_INT scaled_offset = cumulative_inc << shift_amount;
confirm_change_group();

validate_change(insn_addr, &XEXP(XEXP(and_rtx, 0), 0), root_reg, 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you changing insn_addr, can't you use root_reg directly in new_mem_addr?
You also don't check if insn_addr is only used by insn, so you now break other uses.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the insn_addr needs to pick the root of the chain of immediate additions so needs to be changed.
the output of insn_addr is not changed. I only change one of its inputs and the accumulation of the additions i pass it directly into the offset of the true dependent subsequent memory operations.

Comment thread gcc/config/riscv/riscv-fold-mem.cc Outdated
}

basic_block bb;
FOR_EACH_BB_FN(bb, fun) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of this? Can this be handled by cse or cprop passes instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tried placing the phase in various places where there were not available those after.

Comment thread gcc/config/riscv/riscv.cc Outdated
@Alexehv77 Alexehv77 force-pushed the mem-opt branch 3 times, most recently from b7893f6 to 6effe87 Compare April 10, 2026 09:59
@Alexehv77 Alexehv77 self-assigned this Apr 23, 2026
@Alexehv77 Alexehv77 force-pushed the mem-opt branch 4 times, most recently from 4964de9 to 57972ac Compare May 22, 2026 15:12
@Alexehv77 Alexehv77 requested a review from MichielDerhaeg May 22, 2026 15:20
@Alexehv77 Alexehv77 force-pushed the mem-opt branch 6 times, most recently from f18b1d7 to 3f16af7 Compare May 23, 2026 19:43
This phase identifies memory loads that use a base address
calculated via instructions like sh1add. It traces the index
register of that calculation back through previous instructions
to find increments.
As a result instead of having a separate instruction to increment
followed by a pointer to scale and add it, this pass
merges the constant offset directly into the memory load.
Thus it reduces the total number of instructions leading to smaller
code size and faster execution.

I added also a test that checks if offsets > 0 are generated
out memory accesses inside unrolled loops.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants