Bug 111139 - RISC-V: improve scalar constants cost model
Summary: RISC-V: improve scalar constants cost model
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 13.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2023-08-24 17:44 UTC by Vineet Gupta
Modified: 2024-01-12 18:57 UTC (History)
6 users (show)

See Also:
Host:
Target: riscv
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-08-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vineet Gupta 2023-08-24 17:44:05 UTC
The current const cost determination in riscv_rtx_costs () and its children such as riscv_const_insns () needs improvements.

1. There's some likely inaccuracies with various heuristics.
2. Those heuristics are distributed in a bunch of places and better be consolidated.
3. We need to make const cost cpu/uarch tunable as hardware widgets like macro fusions could ammortize multi-insn const costs.


Some of the heuristics to cleanup/revisit:

1a. riscv_rtx_cost() returns 1 insn even if riscv_const_insns () returns > 1.

    case CONST:
      if ((cost = riscv_const_insns (x)) > 0)
	{
	  if (cost == 1 && outer_code == SET)
	    *total = COSTS_N_INSNS (1);
	  else if (outer_code == SET || GET_MODE (x) == VOIDmode)
	    *total = COSTS_N_INSNS (1);
	}

1b. riscv_const_insns () caps the const cost to 4 even if it higher with intent to force const pool. RV backend in general no longer favors const pools for large constants since 2e886eef7f2b5a ("RISC-V: Produce better code with complex constants [PR95632] [PR106602]"). This heuristic needs to be revisited.

1c. riscv_split_integer_cost () adds 2 to initial cost computed.
Comment 1 Palmer Dabbelt 2023-08-24 17:46:51 UTC
I think maybe Jeff said he was going to look at this?  IMO even just a refactoring here would be great, like Vineet is pointing out it's become a bit of a mess.

Also: Edwin is refactoring the rest of the types.
Comment 2 Vineet Gupta 2023-08-25 20:12:11 UTC
Test case to help drive some of this:

unsigned long long f5(unsigned long long i)
{
  return i * 0x0202020202020202ULL;
}