public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/111673] New: assign_hard_reg() routine should scale save/restore costs of callee save registers with basic block frequency
@ 2023-10-03 10:05 jskumari at gcc dot gnu.org
  0 siblings, 0 replies; only message in thread
From: jskumari at gcc dot gnu.org @ 2023-10-03 10:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111673

            Bug ID: 111673
           Summary: assign_hard_reg() routine should scale save/restore
                    costs of callee save registers with basic block
                    frequency
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jskumari at gcc dot gnu.org
  Target Milestone: ---

In assign_hard_reg(), when computing the costs of the hard registers, the cost
of saving/restoring a callee-save hard register in epilogue/prologue is taken
into consideration. However, this cost is not scaled with the entry block
frequency. Without scaling, the cost of saving/restoring is quite small and
this can result in a callee-save register being chosen by assign_hard_reg()
even though there are free caller-save(volatile) registers available. 

Consider the following test:

int f(int);

int advance(int dz)
{
    if (dz > 0)
        return (dz + dz) * dz;
    else
        return dz * f(dz);
}


Input RTL to IRA pass:

  set r127, r3
  set r121, r127
  set r122, compare(r121, 0)
  if (r122 le 0) jump BB4 else jump BB3

BB3:
  set r123, r121*r121
  set r119, r123<<1
  jump BB5

BB4:
  set r3, call f(r3)
  set r128, r3
  set r119, r128*r121

BB5:
  set r3, r119
  return r3


When assign_hard_reg() is called for allocno r121, the cost for r31 is 0
(obtained from ALLOCNO_UPDATED_HARD_REG_COSTS). Since r31 on PowerPC is a
callee save register, we compute the cost for saving/restoring r31 in
prolog/epilog and this cost is 7. So the final cost for r31 is 7. And r31 is
assigned to allocno r121 since it has the lowest cost among the profitable
registers.
However, among profitable registers for allocno r121, there are caller save
registers (like r9) that could possibly be assigned to allocno r121. r9 has a
cost of 2040 (obtained from ALLOCNO_UPDATED_HARD_REG_COSTS). So it is not
chosen as cost of r31 is lesser.
But computation of save/restore costs for r31 is incorrect as it doesn’t take
into consideration the frequency of the basic blocks in which the save/restore
instructions will be placed. If the frequency is taken into consideration, then
cost of r31 is 7000 (frequency of entry bb is 1000). And this would result in
r9 being assigned to r121.

Since r31 is assigned to allocno r121, the above test does not get shrink
wrapped.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-10-03 10:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-03 10:05 [Bug rtl-optimization/111673] New: assign_hard_reg() routine should scale save/restore costs of callee save registers with basic block frequency jskumari at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).